VTK
vtkOpenGLGPUVolumeRayCastMapper.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkOpenGLGPUVolumeRayCastMapper.h
5 
6  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7  All rights reserved.
8  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10  This software is distributed WITHOUT ANY WARRANTY; without even
11  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12  PURPOSE. See the above copyright notice for more information.
13 
14 =========================================================================*/
60 #ifndef vtkOpenGLGPUVolumeRayCastMapper_h
61 #define vtkOpenGLGPUVolumeRayCastMapper_h
62 #include <map> // For methods
63 
64 #include "vtkNew.h" // For vtkNew
65 #include "vtkRenderingVolumeOpenGL2Module.h" // For export macro
67 #include "vtkShader.h" // For methods
68 #include "vtkSmartPointer.h" // For smartptr
69 
70 
73 class vtkOpenGLCamera;
78 class vtkShaderProgram;
79 class vtkTextureObject;
80 class vtkVolume;
82 class vtkVolumeTexture;
83 class vtkOpenGLUniforms;
84 
85 class VTKRENDERINGVOLUMEOPENGL2_EXPORT vtkOpenGLGPUVolumeRayCastMapper :
87 {
88 public:
90 
91  enum Passes
92  {
94  DepthPass = 1
95  };
96 
98  void PrintSelf( ostream& os, vtkIndent indent ) override;
99 
100  // Description:
101  // Low level API to enable access to depth texture in
102  // RenderToTexture mode. It will return either nullptr if
103  // RenderToImage was never turned on or texture captured
104  // the last time RenderToImage was on.
105  vtkTextureObject* GetDepthTexture();
106 
107  // Description:
108  // Low level API to enable access to color texture in
109  // RenderToTexture mode. It will return either nullptr if
110  // RenderToImage was never turned on or texture captured
111  // the last time RenderToImage was on.
112  vtkTextureObject* GetColorTexture();
113 
114  // Description:
115  // Low level API to export the depth texture as vtkImageData in
116  // RenderToImage mode.
117  void GetDepthImage(vtkImageData* im) override;
118 
119  // Description:
120  // Low level API to export the color texture as vtkImageData in
121  // RenderToImage mode.
122  void GetColorImage(vtkImageData* im) override;
123 
124  // Description:
125  // Mapper can have multiple passes and internally it will set
126  // the state. The state can not be set externally explicitly
127  // but can be set indirectly depending on the options set by
128  // the user.
129  vtkGetMacro(CurrentPass, int);
130 
131  // Sets a depth texture for this mapper to use
132  // This allows many mappers to use the same
133  // texture reducing GPU usage. If this is set
134  // the standard depth texture code is skipped
135  // The depth texture should be activated
136  // and deactivated outside of this class
137  void SetSharedDepthTexture(vtkTextureObject *nt);
138 
144  void SetPartitions(unsigned short x, unsigned short y, unsigned short z);
145 
154  bool PreLoadData(vtkRenderer* ren, vtkVolume* vol);
155 
157 
164  void AddShaderReplacement(
165  vtkShader::Type shaderType, // vertex, fragment, etc
166  const std::string& originalValue,
167  bool replaceFirst, // do this replacement before the default
168  const std::string& replacementValue,
169  bool replaceAll);
170  void ClearShaderReplacement(
171  vtkShader::Type shaderType, // vertex, fragment, etc
172  const std::string& originalValue,
173  bool replaceFirst);
174  void ClearAllShaderReplacements(vtkShader::Type shaderType);
175  void ClearAllShaderReplacements();
177 
179 
185  vtkSetStringMacro(VertexShaderCode);
186  vtkGetStringMacro(VertexShaderCode);
187  vtkSetStringMacro(FragmentShaderCode);
188  vtkGetStringMacro(FragmentShaderCode);
190 
192 
196  vtkGetObjectMacro(FragmentCustomUniforms,vtkOpenGLUniforms);
197  vtkGetObjectMacro(VertexCustomUniforms,vtkOpenGLUniforms);
199 
200  // Description:
201  // Delete OpenGL objects.
202  // \post done: this->OpenGLObjectsCreated==0
203  void ReleaseGraphicsResources(vtkWindow *window) override;
204 
205 protected:
208 
210 
211  // Description:
212  // Build vertex and fragment shader for the volume rendering
213  void BuildDepthPassShader(vtkRenderer* ren, vtkVolume* vol,
214  int noOfComponents,
215  int independentComponents);
216 
217  // Description:
218  // Build vertex and fragment shader for the volume rendering
219  void BuildShader(vtkRenderer* ren);
220 
221  // TODO Take these out as these are no longer needed
222  // Methods called by the AMR Volume Mapper.
223  void PreRender(vtkRenderer * vtkNotUsed(ren),
224  vtkVolume *vtkNotUsed(vol),
225  double vtkNotUsed(datasetBounds)[6],
226  double vtkNotUsed(scalarRange)[2],
227  int vtkNotUsed(noOfComponents),
228  unsigned int vtkNotUsed(numberOfLevels)) override {};
229 
230  // \pre input is up-to-date
231  void RenderBlock(vtkRenderer *vtkNotUsed(ren),
232  vtkVolume *vtkNotUsed(vol),
233  unsigned int vtkNotUsed(level)) override {}
234 
235  void PostRender(vtkRenderer *vtkNotUsed(ren),
236  int vtkNotUsed(noOfComponents)) override {}
237 
238  // Description:
239  // Rendering volume on GPU
240  void GPURender(vtkRenderer *ren, vtkVolume *vol) override;
241 
242  // Description:
243  // Method that performs the actual rendering given a volume and a shader
244  void DoGPURender(vtkRenderer* ren,
245  vtkOpenGLCamera* cam,
246  vtkShaderProgram* shaderProgram);
247 
248  // Description:
249  // Update the reduction factor of the render viewport (this->ReductionFactor)
250  // according to the time spent in seconds to render the previous frame
251  // (this->TimeToDraw) and a time in seconds allocated to render the next
252  // frame (allocatedTime).
253  // \pre valid_current_reduction_range: this->ReductionFactor>0.0 && this->ReductionFactor<=1.0
254  // \pre positive_TimeToDraw: this->TimeToDraw>=0.0
255  // \pre positive_time: allocatedTime>0
256  // \post valid_new_reduction_range: this->ReductionFactor>0.0 && this->ReductionFactor<=1.0
257  void ComputeReductionFactor(double allocatedTime);
258 
259  // Description:
260  // Empty implementation.
261  void GetReductionRatio(double* ratio) override
262  {
263  ratio[0] = ratio[1] = ratio[2] = 1.0;
264  }
265 
266 
267  // Description:
268  // Empty implementation.
269  int IsRenderSupported(vtkRenderWindow *vtkNotUsed(window),
270  vtkVolumeProperty *vtkNotUsed(property)) override
271  {
272  return 1;
273  }
274 
276 
279  vtkMTimeType GetRenderPassStageMTime(vtkVolume* vol);
280 
284  void GetShaderTemplate(std::map<vtkShader::Type, vtkShader*>& shaders);
285 
289  void ReplaceShaderValues(std::map<vtkShader::Type, vtkShader*>& shaders,
290  vtkRenderer* ren, vtkVolume* vol, int numComps);
291 
296  void ReplaceShaderCustomUniforms(
297  std::map<vtkShader::Type, vtkShader*>& shaders );
298  void ReplaceShaderBase(std::map<vtkShader::Type, vtkShader*>& shaders,
299  vtkRenderer* ren, vtkVolume* vol, int numComps);
300  void ReplaceShaderTermination(std::map<vtkShader::Type, vtkShader*>& shaders,
301  vtkRenderer* ren, vtkVolume* vol, int numComps);
302  void ReplaceShaderShading(std::map<vtkShader::Type, vtkShader*>& shaders,
303  vtkRenderer* ren, vtkVolume* vol, int numComps);
304  void ReplaceShaderCompute(std::map<vtkShader::Type, vtkShader*>& shaders,
305  vtkRenderer* ren, vtkVolume* vol, int numComps);
306  void ReplaceShaderCropping(std::map<vtkShader::Type, vtkShader*>& shaders,
307  vtkRenderer* ren, vtkVolume* vol, int numComps);
308  void ReplaceShaderClipping(std::map<vtkShader::Type, vtkShader*>& shaders,
309  vtkRenderer* ren, vtkVolume* vol, int numComps);
310  void ReplaceShaderMasking(std::map<vtkShader::Type, vtkShader*>& shaders,
311  vtkRenderer* ren, vtkVolume* vol, int numComps);
312  void ReplaceShaderPicking(std::map<vtkShader::Type, vtkShader*>& shaders,
313  vtkRenderer* ren, vtkVolume* vol, int numComps);
314  void ReplaceShaderRTT(std::map<vtkShader::Type, vtkShader*>& shaders,
315  vtkRenderer* ren, vtkVolume* vol, int numComps);
316  void ReplaceShaderRenderPass(std::map<vtkShader::Type, vtkShader*>& shaders,
317  vtkVolume* vol, bool prePass);
318 
322  void SetShaderParametersRenderPass();
323 
331 
336  std::map<const vtkShader::ReplacementSpec, vtkShader::ReplacementValue>
338 
341 
342 public:
344  using VolumeInputMap = std::map<int, vtkVolumeInputHelper>;
346 
347 private:
348  class vtkInternal;
349  vtkInternal* Impl;
350 
351  friend class vtkVolumeTexture;
352 
354  const vtkOpenGLGPUVolumeRayCastMapper&) = delete;
355  void operator=(const vtkOpenGLGPUVolumeRayCastMapper&) = delete;
356 };
357 
358 #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:50
vtkNew< vtkOpenGLUniforms > VertexCustomUniforms
std::map< const vtkShader::ReplacementSpec, vtkShader::ReplacementValue > UserShaderReplacements
Container for a set of TransferFunction2D instances.
vtkGenericOpenGLResourceFreeCallback * ResourceCallback
vtkTypeUInt32 vtkMTimeType
Definition: vtkType.h:302
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.
Type
Available shader types.
Definition: vtkShader.h:48
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.
void PreRender(vtkRenderer *vtkNotUsed(ren), vtkVolume *vtkNotUsed(vol), double vtkNotUsed(datasetBounds)[6], double vtkNotUsed(scalarRange)[2], int vtkNotUsed(noOfComponents), unsigned int vtkNotUsed(numberOfLevels)) override
abstract specification for renderers
Definition: vtkRenderer.h:63
window superclass for vtkRenderWindow
Definition: vtkWindow.h:37
OpenGL implementation of volume rendering through ray-casting.
a simple class to control print indentation
Definition: vtkIndent.h:39
topologically and geometrically regular array of data
Definition: vtkImageData.h:45
void RenderBlock(vtkRenderer *vtkNotUsed(ren), vtkVolume *vtkNotUsed(vol), unsigned int vtkNotUsed(level)) override
void PostRender(vtkRenderer *vtkNotUsed(ren), int vtkNotUsed(noOfComponents)) override
represents the common properties for rendering a volume.
abstracts an OpenGL texture object.
create a window for renderers to draw into
vtkNew< vtkOpenGLUniforms > FragmentCustomUniforms
int IsRenderSupported(vtkRenderWindow *vtkNotUsed(window), vtkVolumeProperty *vtkNotUsed(property)) override
Based on hardware and properties, we may or may not be able to render using 3D texture mapping...
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
OpenGL camera.
void GetReductionRatio(double *ratio) override
Ray casting performed on the GPU.
static vtkGPUVolumeRayCastMapper * New()
vtkNew< vtkInformation > LastRenderPassInfo
Caches the vtkOpenGLRenderPass::RenderPasses() information.
The ShaderProgram uses one or more Shader objects.