File: Renderer.cpp

package info (click to toggle)
ospray 3.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,048 kB
  • sloc: cpp: 80,569; ansic: 951; sh: 805; makefile: 170; python: 69
file content (182 lines) | stat: -rw-r--r-- 5,168 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Copyright 2009 Intel Corporation
// SPDX-License-Identifier: Apache-2.0

// ospray
#include "Renderer.h"
#include "LoadBalancer.h"
#include "Material.h"
#include "camera/Camera.h"
#include "common/Instance.h"
#include "common/World.h"
#include "fb/FrameBuffer.h"
#include "geometry/GeometricModel.h"
#include "ospray/OSPEnums.h"
#include "pf/PixelFilter.h"
#ifndef OSPRAY_TARGET_SYCL
#include "render/Renderer_ispc.h"
#include "render/util_ispc.h"
#endif

namespace ospray {

// Renderer definitions ///////////////////////////////////////////////////////

Renderer::Renderer(api::ISPCDevice &device)
    : AddStructShared(device.getDRTDevice(), device),
      device(device),
      drtDevice(device.getDRTDevice())
{
  managedObjectType = OSP_RENDERER;
  pixelFilter = nullptr;
}

std::string Renderer::toString() const
{
  return "ospray::Renderer";
}

void Renderer::commit()
{
  spp = std::max(1, getParam<int>("pixelSamples", 1));
  const float mipBias = getParam<float>("mipMapBias", 0.f);
  const uint32_t maxDepth = std::max(0, getParam<int>("maxPathLength", 20));
  const float minContribution = getParam<float>("minContribution", 0.001f);
  errorThreshold = getParam<float>("varianceThreshold", 0.f);

  maxDepthTexture = getParamObject<Texture2D>("map_maxDepth");
  backplate = getParamObject<Texture2D>("map_backplate");
  featureFlags.reset();
  if (maxDepthTexture || backplate)
    featureFlags.other |= FFO_TEXTURE_IN_RENDERER;

  if (maxDepthTexture) {
    if (maxDepthTexture->format != OSP_TEXTURE_R32F
        || maxDepthTexture->filter != OSP_TEXTURE_FILTER_NEAREST) {
      static WarnOnce warning(
          "maxDepthTexture provided to the renderer "
          "needs to be of type OSP_TEXTURE_R32F and have "
          "the OSP_TEXTURE_FILTER_NEAREST flag");
    }
  }

  vec3f bgColor3 = getParam<vec3f>(
      "backgroundColor", vec3f(getParam<float>("backgroundColor", 0.f)));
  bgColor = getParam<vec4f>("backgroundColor", vec4f(bgColor3, 0.f));

  // Handle materials assigned to renderer
  materialArray = nullptr;
  getSh()->material = nullptr;
  materialData = getParamDataT<Material *>("material");
  if (materialData) {
    for (auto &&mat : *materialData)
      featureFlags |= mat->getFeatureFlags();

    materialArray = devicert::make_buffer_shared_unique<ispc::Material *>(
        getISPCDevice().getDRTDevice(),
        createArrayOfSh<ispc::Material>(*materialData));
    getSh()->numMaterials = materialArray->size();
    getSh()->material = materialArray->sharedPtr();
  }

  getSh()->spp = spp;
  getSh()->maxDepth = maxDepth;
  getSh()->minContribution = minContribution;
  getSh()->bgColor = bgColor;
  getSh()->backplate = backplate ? backplate->getSh() : nullptr;
  getSh()->maxDepthTexture =
      maxDepthTexture ? maxDepthTexture->getSh() : nullptr;

  setupPixelFilter();
  getSh()->pixelFilter = pixelFilter ? pixelFilter->getSh() : nullptr;
  getSh()->mipBiasFactor = powf(2, mipBias);
}

OSPPickResult Renderer::pick(
    FrameBuffer *fb, Camera *camera, World *world, const vec2f &screenPos)
{
  OSPPickResult res;

  res.instance = nullptr;
  res.model = nullptr;
  res.primID = RTC_INVALID_GEOMETRY_ID;

#ifndef OSPRAY_TARGET_SYCL
  int instID = RTC_INVALID_GEOMETRY_ID;
  int geomID = RTC_INVALID_GEOMETRY_ID;
  int primID = RTC_INVALID_GEOMETRY_ID;

  ispc::Renderer_pick(getSh(),
      fb->getSh(),
      camera->getSh(),
      world->getSh(),
      (const ispc::vec2f &)screenPos,
      (ispc::vec3f &)res.worldPosition[0],
      instID,
      geomID,
      primID,
      res.hasHit);

  if (res.hasHit) {
    auto *instance = (*world->instances)[instID];
    auto *group = instance->group.ptr;
    if (!group->geometricModels) {
      res.hasHit = false;
      return res;
    }
    auto *model = (*group->geometricModels)[geomID];

    instance->refInc();
    model->refInc();

    res.instance = (OSPInstance)instance;
    res.model = (OSPGeometricModel)model;
    res.primID = static_cast<uint32_t>(primID);
  }
#else
  // Silence unused parameter warning
  (void)fb;
  (void)camera;
  (void)world;
  (void)screenPos;
#endif

  return res;
}

void Renderer::setupPixelFilter()
{
  const auto pixelFilterType = (OSPPixelFilterType)getParam<uint32_t>(
      "pixelFilter", OSPPixelFilterType::OSP_PIXELFILTER_GAUSS);
  pixelFilter = nullptr;
  switch (pixelFilterType) {
  case OSPPixelFilterType::OSP_PIXELFILTER_BOX: {
    pixelFilter = new BoxPixelFilter(getISPCDevice());
    break;
  }
  case OSPPixelFilterType::OSP_PIXELFILTER_POINT: {
    pixelFilter = new PointPixelFilter(getISPCDevice());
    break;
  }
  case OSPPixelFilterType::OSP_PIXELFILTER_BLACKMAN_HARRIS: {
    pixelFilter = new BlackmanHarrisLUTPixelFilter(getISPCDevice());
    break;
  }
  case OSPPixelFilterType::OSP_PIXELFILTER_MITCHELL: {
    pixelFilter = new MitchellNetravaliLUTPixelFilter(getISPCDevice());
    break;
  }
  case OSPPixelFilterType::OSP_PIXELFILTER_GAUSS:
  default: {
    pixelFilter = new GaussianLUTPixelFilter(getISPCDevice());
    break;
  }
  }
  if (pixelFilter) {
    // Need to remove extra local ref
    pixelFilter->refDec();
  }
}

OSPTYPEFOR_DEFINITION(Renderer *);

} // namespace ospray