File: FrameBuffer.h

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 (234 lines) | stat: -rw-r--r-- 5,408 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// Copyright 2009 Intel Corporation
// SPDX-License-Identifier: Apache-2.0

#pragma once

// ospray
#include "ISPCDeviceObject.h"
#include "common/Data.h"
#include "common/FeatureFlagsEnum.h"
#include "fb/ImageOp.h"
#include "ospray/ospray.h"
#include "rkcommon/utility/ArrayView.h"
// ispc shared
#include "FrameBufferShared.h"

#include <atomic>
#include <random>

namespace ospray {

struct Camera;
struct FrameBufferView;

// abstract frame buffer class
struct OSPRAY_SDK_INTERFACE FrameBuffer
    : public AddStructShared<ISPCDeviceObject, ispc::FrameBuffer>
{
  // app-mappable format of the color buffer. make sure that this
  // matches the definition on the ISPC side
  using ColorBufferFormat = OSPFrameBufferFormat;

  FrameBuffer(api::ISPCDevice &device,
      const vec2i &size,
      ColorBufferFormat colorBufferFormat,
      const uint32 channels,
      const FeatureFlagsOther ffo);

  virtual ~FrameBuffer() override = default;

  virtual void commit() override;

  virtual const void *mapBuffer(OSPFrameBufferChannel channel) = 0;

  virtual void unmap(const void *mappedMem) = 0;

  // clear (the specified channels of) this frame buffer
  virtual void clear();

  // Get number of pixels per render task, in x and y direction
  vec2i getRenderTaskSize() const;

  // Return the number of render tasks in the x and y direction
  // This is the kernel launch dims to render the image
  virtual vec2i getNumRenderTasks() const = 0;

  virtual uint32_t getTotalRenderTasks() const = 0;

  // Get the device-side render task IDs
  virtual utility::ArrayView<uint32_t> getRenderTaskIDs(
      const float errorThreshold, const uint32_t spp) = 0;

  vec2i getNumPixels() const;

  ColorBufferFormat getColorBufferFormat() const;

  virtual float getVariance() const;

  virtual float taskError(const uint32_t taskID) const = 0;

  virtual void beginFrame();

  // Invoke post-processing by calling all FrameOps
  virtual devicert::AsyncEvent postProcess() = 0;

  // common function to help printf-debugging, every derived class should
  // override this
  virtual std::string toString() const override;

  void setCompletedEvent(OSPSyncEvent event);
  OSPSyncEvent getLatestCompleteEvent() const;
  void waitForEvent(OSPSyncEvent event) const;

  virtual float getCurrentProgress() const;

  virtual void cancelFrame();
  bool frameCancelled() const;

  bool hasColorBuf() const;
  bool hasVarianceBuf() const;
  bool hasNormalBuf() const;
  bool hasAlbedoBuf() const;
  bool hasPrimitiveIDBuf() const;
  bool hasObjectIDBuf() const;
  bool hasInstanceIDBuf() const;

  bool doAccumulation() const;

  uint32 getChannelFlags() const;

  int32_t getFrameID() const;
  void setFrameID(int32_t id);

  FeatureFlags getFeatureFlags() const;

#ifdef OSPRAY_TARGET_SYCL
  sycl::nd_range<3> getDispatchRange(const size_t numTasks) const;
#endif

 protected:
  const vec2i size;
  ColorBufferFormat colorBufferFormat;

  // Frame buffer optional buffers
  bool hasColorBuffer;
  bool hasDepthBuffer;
  bool hasVarianceBuffer;
  bool hasNormalBuffer;
  bool hasAlbedoBuffer;
  bool hasPrimitiveIDBuffer;
  bool hasObjectIDBuffer;
  bool hasInstanceIDBuffer;

  // indicates whether the app requested this frame buffer to do accumulation
  bool doAccum;

  float frameVariance{inf};

  std::atomic<bool> cancelRender{false};

  std::atomic<OSPSyncEvent> stagesCompleted{OSP_FRAME_FINISHED};

  Ref<const DataT<ImageOp *>> imageOpData;

  FeatureFlagsOther featureFlags{FFO_NONE};

  int32_t minimumAdaptiveFrames(const uint32_t spp) const;
  bool accumulationFinished() const;

 private:
  // for consistent reproducibility of variance accumulation
  constexpr static uint32_t mtSeed = 43;
  std::mt19937 mtGen{mtSeed};
  bool pickOdd{false};
};

OSPTYPEFOR_SPECIALIZATION(FrameBuffer *, OSP_FRAMEBUFFER);

inline vec2i FrameBuffer::getNumPixels() const
{
  return size;
}

inline FrameBuffer::ColorBufferFormat FrameBuffer::getColorBufferFormat() const
{
  return colorBufferFormat;
}

inline bool FrameBuffer::hasColorBuf() const
{
  return hasColorBuffer;
}

inline bool FrameBuffer::hasVarianceBuf() const
{
  return hasVarianceBuffer;
}

inline bool FrameBuffer::hasNormalBuf() const
{
  return hasNormalBuffer;
}

inline bool FrameBuffer::hasAlbedoBuf() const
{
  return hasAlbedoBuffer;
}

inline bool FrameBuffer::hasPrimitiveIDBuf() const
{
  return hasPrimitiveIDBuffer;
}

inline bool FrameBuffer::hasObjectIDBuf() const
{
  return hasObjectIDBuffer;
}

inline bool FrameBuffer::hasInstanceIDBuf() const
{
  return hasInstanceIDBuffer;
}

inline bool FrameBuffer::doAccumulation() const
{
  return doAccum;
}

inline int32_t FrameBuffer::getFrameID() const
{
  return getSh()->frameID;
}

inline void FrameBuffer::setFrameID(int32_t id)
{
  getSh()->frameID = id;
}

inline FeatureFlags FrameBuffer::getFeatureFlags() const
{
  FeatureFlags ff;
  ff.other = featureFlags;
  return ff;
}

#ifdef OSPRAY_TARGET_SYCL
inline sycl::nd_range<3> FrameBuffer::getDispatchRange(
    const size_t numTasks) const
{
  const vec2i ts = getRenderTaskSize();
  return sycl::nd_range<3>({numTasks, ts.y, ts.x}, {1, ts.y, ts.x});
}
#endif

inline int32_t FrameBuffer::minimumAdaptiveFrames(const uint32_t spp) const
{
  return std::max(2u, 16 / spp);
}

inline bool FrameBuffer::accumulationFinished() const
{
  return getSh()->targetFrames && getFrameID() >= getSh()->targetFrames;
}

} // namespace ospray