VTK  9.7.1
vtkOpenGLGPUVolumeRayCastMapper.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2 // SPDX-License-Identifier: BSD-3-Clause
61 #ifndef vtkOpenGLGPUVolumeRayCastMapper_h
62 #define vtkOpenGLGPUVolumeRayCastMapper_h
63 #include <map> // For methods
64 
66 #include "vtkNew.h" // For vtkNew
67 #include "vtkRenderingVolumeOpenGL2Module.h" // For export macro
68 #include "vtkShader.h" // For methods
69 #include "vtkSmartPointer.h" // For smartptr
70 #include "vtkWrappingHints.h" // For VTK_MARSHALAUTO
71 
72 VTK_ABI_NAMESPACE_BEGIN
75 class vtkOpenGLCamera;
76 class vtkOpenGLTransferFunctions2D;
77 class vtkOpenGLVolumeGradientOpacityTables;
78 class vtkOpenGLVolumeOpacityTables;
79 class vtkOpenGLVolumeRGBTables;
80 class vtkShaderProgram;
81 class vtkTextureObject;
82 class vtkVolume;
84 class vtkVolumeTexture;
86 
87 class VTKRENDERINGVOLUMEOPENGL2_EXPORT VTK_MARSHALAUTO vtkOpenGLGPUVolumeRayCastMapper
89 {
90 public:
92 
93  enum Passes
94  {
95  RenderPass = 0,
96  DepthPass = 1
97  };
98 
100  void PrintSelf(ostream& os, vtkIndent indent) override;
101 
102  // Description:
103  // Low level API to enable access to depth texture in
104  // RenderToTexture mode. It will return either nullptr if
105  // RenderToImage was never turned on or texture captured
106  // the last time RenderToImage was on.
107  vtkTextureObject* GetDepthTexture();
108 
109  // Description:
110  // Low level API to enable access to color texture in
111  // RenderToTexture mode. It will return either nullptr if
112  // RenderToImage was never turned on or texture captured
113  // the last time RenderToImage was on.
114  vtkTextureObject* GetColorTexture();
115 
116  // Description:
117  // Low level API to export the depth texture as vtkImageData in
118  // RenderToImage mode.
119  void GetDepthImage(vtkImageData* im) override;
120 
121  // Description:
122  // Low level API to export the color texture as vtkImageData in
123  // RenderToImage mode.
124  void GetColorImage(vtkImageData* im) override;
125 
126  // Description:
127  // Mapper can have multiple passes and internally it will set
128  // the state. The state can not be set externally explicitly
129  // but can be set indirectly depending on the options set by
130  // the user.
131  vtkGetMacro(CurrentPass, int);
132 
133  // Sets a depth texture for this mapper to use
134  // This allows many mappers to use the same
135  // texture reducing GPU usage. If this is set
136  // the standard depth texture code is skipped
137  // The depth texture should be activated
138  // and deactivated outside of this class
139  void SetSharedDepthTexture(vtkTextureObject* nt);
140 
146  void SetPartitions(unsigned short x, unsigned short y, unsigned short z);
147 
156  bool PreLoadData(vtkRenderer* ren, vtkVolume* vol);
157 
158  // Description:
159  // Delete OpenGL objects.
160  // \post done: this->OpenGLObjectsCreated==0
161  void ReleaseGraphicsResources(vtkWindow* window) override;
162 
163 protected:
166 
168 
169  // Description:
170  // Build vertex and fragment shader for the volume rendering
171  void BuildDepthPassShader(
172  vtkRenderer* ren, vtkVolume* vol, int noOfComponents, int independentComponents);
173 
174  // Description:
175  // Build vertex and fragment shader for the volume rendering
176  void BuildShader(vtkRenderer* ren);
177 
178  // TODO Take these out as these are no longer needed
179  // Methods called by the AMR Volume Mapper.
180  void PreRender(vtkRenderer* vtkNotUsed(ren), vtkVolume* vtkNotUsed(vol),
181  double vtkNotUsed(datasetBounds)[6], double vtkNotUsed(scalarRange)[2],
182  int vtkNotUsed(noOfComponents), unsigned int vtkNotUsed(numberOfLevels)) override
183  {
184  }
185 
186  // \pre input is up-to-date
187  void RenderBlock(vtkRenderer* vtkNotUsed(ren), vtkVolume* vtkNotUsed(vol),
188  unsigned int vtkNotUsed(level)) override
189  {
190  }
191 
192  void PostRender(vtkRenderer* vtkNotUsed(ren), int vtkNotUsed(noOfComponents)) override {}
193 
194  // Description:
195  // Rendering volume on GPU
196  void GPURender(vtkRenderer* ren, vtkVolume* vol) override;
197 
198  // Description:
199  // Method that performs the actual rendering given a volume and a shader
200  void DoGPURender(vtkRenderer* ren, vtkOpenGLCamera* cam, vtkShaderProgram* shaderProgram,
201  vtkOpenGLShaderProperty* shaderProperty);
202 
203  // Description:
204  // Update the reduction factor of the render viewport (this->ReductionFactor)
205  // according to the time spent in seconds to render the previous frame
206  // (this->TimeToDraw) and a time in seconds allocated to render the next
207  // frame (allocatedTime).
208  // \pre valid_current_reduction_range: this->ReductionFactor>0.0 && this->ReductionFactor<=1.0
209  // \pre positive_TimeToDraw: this->TimeToDraw>=0.0
210  // \pre positive_time: allocatedTime>0
211  // \post valid_new_reduction_range: this->ReductionFactor>0.0 && this->ReductionFactor<=1.0
212  void ComputeReductionFactor(double allocatedTime);
213 
214  // Description:
215  // Returns a reduction ratio for each dimension
216  // This ratio is computed from MaxMemoryInBytes and MaxMemoryFraction so that the total
217  // memory usage of the resampled image, by the returned ratio, does not exceed
218  // `MaxMemoryInBytes * MaxMemoryFraction`
219  // \pre input is up-to-date
220  // \post Aspect ratio of image is always kept
221  // - for a 1D image `ratio[1] == ratio[2] == 1`
222  // - for a 2D image `ratio[0] == ratio[1]` and `ratio[2] == 1`
223  // - for a 3D image `ratio[0] == ratio[1] == ratio[2]`
224  void GetReductionRatio(double* ratio) override;
225 
226  // Description:
227  // Empty implementation.
229  vtkRenderWindow* vtkNotUsed(window), vtkVolumeProperty* vtkNotUsed(property)) override
230  {
231  return 1;
232  }
233 
235 
238  vtkMTimeType GetRenderPassStageMTime(vtkVolume* vol);
239 
243  void GetShaderTemplate(
244  std::map<vtkShader::Type, vtkShader*>& shaders, vtkOpenGLShaderProperty* p);
245 
249  void ReplaceShaderValues(
250  std::map<vtkShader::Type, vtkShader*>& shaders, vtkRenderer* ren, vtkVolume* vol, int numComps);
251 
256  void ReplaceShaderCustomUniforms(
257  std::map<vtkShader::Type, vtkShader*>& shaders, vtkOpenGLShaderProperty* p);
258  void ReplaceShaderBase(
259  std::map<vtkShader::Type, vtkShader*>& shaders, vtkRenderer* ren, vtkVolume* vol, int numComps);
260  void ReplaceShaderTermination(
261  std::map<vtkShader::Type, vtkShader*>& shaders, vtkRenderer* ren, vtkVolume* vol, int numComps);
262  void ReplaceShaderShading(
263  std::map<vtkShader::Type, vtkShader*>& shaders, vtkRenderer* ren, vtkVolume* vol, int numComps);
264  void ReplaceShaderCompute(
265  std::map<vtkShader::Type, vtkShader*>& shaders, vtkRenderer* ren, vtkVolume* vol, int numComps);
266  void ReplaceShaderCropping(
267  std::map<vtkShader::Type, vtkShader*>& shaders, vtkRenderer* ren, vtkVolume* vol, int numComps);
268  void ReplaceShaderClipping(
269  std::map<vtkShader::Type, vtkShader*>& shaders, vtkRenderer* ren, vtkVolume* vol, int numComps);
270  void ReplaceShaderMasking(
271  std::map<vtkShader::Type, vtkShader*>& shaders, vtkRenderer* ren, vtkVolume* vol, int numComps);
272  void ReplaceShaderPicking(
273  std::map<vtkShader::Type, vtkShader*>& shaders, vtkRenderer* ren, vtkVolume* vol, int numComps);
274  void ReplaceShaderRTT(
275  std::map<vtkShader::Type, vtkShader*>& shaders, vtkRenderer* ren, vtkVolume* vol, int numComps);
276  void ReplaceShaderRenderPass(
277  std::map<vtkShader::Type, vtkShader*>& shaders, vtkVolume* vol, bool prePass);
278 
282  void SetShaderParametersRenderPass();
283 
291 
294 
295 public:
297  using VolumeInputMap = std::map<int, vtkVolumeInputHelper>;
299 
300 private:
301  class vtkInternal;
302  vtkInternal* Impl;
303 
304  friend class vtkVolumeTexture;
305 
307  void operator=(const vtkOpenGLGPUVolumeRayCastMapper&) = delete;
308 };
309 
310 VTK_ABI_NAMESPACE_END
311 #endif // vtkOpenGLGPUVolumeRayCastMapper_h
abstract interface for implicit functions
virtual void GPURender(vtkRenderer *, vtkVolume *)
Handled in the subclass - the actual render method.
void ReleaseGraphicsResources(vtkWindow *) override
Release any graphics resources that are being consumed by this mapper.
represents a volume (data & properties) in a rendered scene
Definition: vtkVolume.h:40
vtkGenericOpenGLResourceFreeCallback * ResourceCallback
vtkTypeUInt32 vtkMTimeType
Definition: vtkType.h:318
Creates and manages the volume texture rendered by vtkOpenGLGPUVolumeRayCastMapper.
std::map< int, vtkVolumeInputHelper > VolumeInputMap
virtual void GetDepthImage(vtkImageData *)
Low level API to export the depth texture as vtkImageData in RenderToImage mode.
Convenience container for internal structures specific to a volume input.
virtual void GetColorImage(vtkImageData *)
Low level API to export the color texture as vtkImageData in RenderToImage mode.
abstract specification for renderers
Definition: vtkRenderer.h:63
represent GPU shader properties
void PreRender(vtkRenderer *ren, vtkVolume *vol, double datasetBounds[6], double scalarRange[2], int noOfComponents, unsigned int numberOfLevels) override
window superclass for vtkRenderWindow
Definition: vtkWindow.h:41
OpenGL implementation of volume rendering through ray-casting.
a simple class to control print indentation
Definition: vtkIndent.h:28
topologically and geometrically regular array of data
Definition: vtkImageData.h:45
void PostRender(vtkRenderer *ren, int noOfComponents) override
virtual void GetReductionRatio(double ratio[3])=0
Return how much the dataset has to be reduced in each dimension to fit on the GPU.
void RenderBlock(vtkRenderer *ren, vtkVolume *vol, unsigned int level) override
represents the common properties for rendering a volume.
abstracts an OpenGL texture object.
create a window for renderers to draw into
#define VTK_MARSHALAUTO
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
OpenGL camera.
Ray casting performed on the GPU.
int IsRenderSupported(vtkRenderWindow *window, vtkVolumeProperty *property) override
Based on hardware and properties, we may or may not be able to render using 3D texture mapping...
static vtkGPUVolumeRayCastMapper * New()
vtkNew< vtkInformation > LastRenderPassInfo
Caches the vtkOpenGLRenderPass::RenderPasses() information.
The ShaderProgram uses one or more Shader objects.