Commit b8ab2d69 authored by Simon Drouin's avatar Simon Drouin

New class vtkOpenGLUniform defines custom uniform variables in shaders

* a Unique instance of vtkOpenGLUniform is embeded in vtkOpenGLGPUVolumeRayCastMapper

* Uniform variables that are added through the new class are automatically defined in internal shaders of mappers.

* New class is used to set value of uniform variables without modifying the shader.
parent 075c4b3d
......@@ -12,6 +12,7 @@ set(Module_SRCS
......@@ -48,6 +48,8 @@ bool g_skip;
float g_currentT;
float g_terminatePointMax;
......@@ -23,6 +23,8 @@
/// Uniforms, attributes, and globals
......@@ -61,6 +61,7 @@
#include <vtkOpenGLShaderCache.h>
#include "vtkOpenGLState.h"
#include <vtkOpenGLVertexArrayObject.h>
#include "vtkOpenGLUniforms.h"
#include <vtkMultiVolume.h>
#include <vtkPixelBufferObject.h>
#include <vtkPixelExtent.h>
......@@ -2428,6 +2429,21 @@ void vtkOpenGLGPUVolumeRayCastMapper::GetShaderTemplate(
void vtkOpenGLGPUVolumeRayCastMapper::ReplaceShaderCustomUniforms(
std::map<vtkShader::Type, vtkShader*>& shaders )
vtkShader* vertexShader = shaders[vtkShader::Vertex];
vtkShader* fragmentShader = shaders[vtkShader::Fragment];
void vtkOpenGLGPUVolumeRayCastMapper::ReplaceShaderBase(
std::map<vtkShader::Type, vtkShader*>& shaders,
......@@ -2895,6 +2911,10 @@ void vtkOpenGLGPUVolumeRayCastMapper::ReplaceShaderValues(
this->ReplaceShaderRenderPass(shaders, vol, true);
// Custom uniform variables replacements
// Base methods replacements
this->ReplaceShaderBase(shaders, ren, vol, noOfComponents);
......@@ -3375,6 +3395,8 @@ bool vtkOpenGLGPUVolumeRayCastMapper::vtkInternal::ShaderRebuildNeeded(vtkCamera
return (this->NeedToInitializeResources ||
this->VolumePropertyChanged ||
this->Parent->GetMTime() > this->ShaderBuildTime.GetMTime() ||
this->Parent->GetFragmentCustomUniforms()->GetUniformListMTime() > this->ShaderBuildTime.GetMTime() ||
this->Parent->GetVertexCustomUniforms()->GetUniformListMTime() > this->ShaderBuildTime.GetMTime() ||
cam->GetParallelProjection() != this->LastProjectionParallel ||
this->SelectionStateTime.GetMTime() > this->ShaderBuildTime.GetMTime() ||
renderPassTime > this->ShaderBuildTime ||
......@@ -3862,6 +3884,10 @@ void vtkOpenGLGPUVolumeRayCastMapper::DoGPURender(vtkRenderer* ren,
// Upload the value of user-defined uniforms in the program
this->VertexCustomUniforms->SetUniforms( prog );
this->FragmentCustomUniforms->SetUniforms( prog );
if (!this->Impl->MultiVolume)
......@@ -80,6 +80,7 @@ class vtkTextureObject;
class vtkVolume;
class vtkVolumeInputHelper;
class vtkVolumeTexture;
class vtkOpenGLUniforms;
public vtkGPUVolumeRayCastMapper
......@@ -187,6 +188,15 @@ public:
* The Uniforms object allows to set custom uniform variables
* that are used in replacement shader code.
// Description:
// Delete OpenGL objects.
// \post done: this->OpenGLObjectsCreated==0
......@@ -283,6 +293,8 @@ protected:
* RenderPass string replacements on shader templates called from
* ReplaceShaderValues.
void ReplaceShaderCustomUniforms(
std::map<vtkShader::Type, vtkShader*>& shaders );
void ReplaceShaderBase(std::map<vtkShader::Type, vtkShader*>& shaders,
vtkRenderer* ren, vtkVolume* vol, int numComps);
void ReplaceShaderTermination(std::map<vtkShader::Type, vtkShader*>& shaders,
......@@ -324,6 +336,9 @@ protected:
std::map<const vtkShader::ReplacementSpec, vtkShader::ReplacementValue>
vtkNew<vtkOpenGLUniforms> FragmentCustomUniforms;
vtkNew<vtkOpenGLUniforms> VertexCustomUniforms;
using VolumeInput = vtkVolumeInputHelper;
using VolumeInputMap = std::map<int, vtkVolumeInputHelper>;
This diff is collapsed.
Program: Visualization Toolkit
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or for details.
This software is distributed WITHOUT ANY WARRANTY; without even
PURPOSE. See the above copyright notice for more information.
* @class vtkOpenGLUniform
* @brief helper class to set custom uniform variables in GLSL shaders.
* This class implements all SetUniform* functions supported by vtkShaderProgram but instead of
* directly calling the underlying OpenGL functions, it caches the name and value of the variable
* and provides a mechanism for client mappers to set all cached variables at once in a generic way.
#ifndef vtkOpenGLUniforms_h
#define vtkOpenGLUniforms_h
#include "vtkObject.h"
#include "vtkRenderingVolumeOpenGL2Module.h" // For export macro
#include <string> // For member functions
class vtkUniformInternals;
class vtkMatrix3x3;
class vtkMatrix4x4;
class vtkShaderProgram;
class VTKRENDERINGVOLUMEOPENGL2_EXPORT vtkOpenGLUniforms : public vtkObject
static vtkOpenGLUniforms *New();
vtkTypeMacro(vtkOpenGLUniforms, vtkObject);
void PrintSelf(ostream& os, vtkIndent indent) override;
std::string GetDeclarations();
bool SetUniforms( vtkShaderProgram * p );
vtkMTimeType GetUniformListMTime();
/** Add the @p name uniform variable with value @p defaultValue. */
void AddUniformi (const char *name, int defaultValue);
void AddUniformf(const char *name, float defaultValue);
void AddUniform2i(const char *name, const int defaultValue[2]);
void AddUniform2f(const char *name, const float defaultValue[2]);
void AddUniform3f(const char *name, const float defaultValue[3]);
void AddUniform3f(const char *name, const double defaultValue[3]);
void AddUniform4f(const char *name, const float defaultValue[4]);
void AddUniform3uc(const char *name, const unsigned char defaultValue[3]); // maybe remove
void AddUniform4uc(const char *name, const unsigned char defaultValue[4]); // maybe remove
void AddUniformMatrix(const char *name, vtkMatrix3x3 *defaultValue);
void AddUniformMatrix(const char *name, vtkMatrix4x4 *defaultValue);
void AddUniformMatrix3x3(const char *name, float *defaultValue);
void AddUniformMatrix4x4(const char *name, float *defaultValue);
/** Add the @p name uniform array to @p f with @p count elements */
void AddUniform1iv(const char *name, const int count, const int *f);
void AddUniform1fv (const char *name, const int count, const float *f);
void AddUniform2fv (const char *name, const int count, const float(*f)[2]);
void AddUniform3fv (const char *name, const int count, const float(*f)[3]);
void AddUniform4fv (const char *name, const int count, const float(*f)[4]);
void AddUniformMatrix4x4v (const char *name, const int count, float *v);
/** Remove uniform variable named @p name */
void RemoveUniform(const char *name);
/** Remove all uniform variables */
void RemoveAllUniforms();
/** Set the @p name uniform value to @p v. */
void SetUniformi(const char *name, int v);
void SetUniformf(const char *name, float v);
void SetUniform2i(const char *name, const int v[2]);
void SetUniform2f(const char *name, const float v[2]);
void SetUniform3f(const char *name, const float v[3]);
void SetUniform3f(const char *name, const double v[3]);
void SetUniform4f(const char *name, const float v[4]);
void SetUniform3uc(const char *name, const unsigned char v[3]); // maybe remove
void SetUniform4uc(const char *name, const unsigned char v[4]); // maybe remove
void SetUniformMatrix(const char *name, vtkMatrix3x3 *v);
void SetUniformMatrix(const char *name, vtkMatrix4x4 *v);
void SetUniformMatrix3x3(const char *name, float *v);
void SetUniformMatrix4x4(const char *name, float *v);
/** Set the @p name uniform array to @p f with @p count elements */
void SetUniform1iv(const char *name, const int count, const int *f);
void SetUniform1fv(const char *name, const int count, const float *f);
void SetUniform2fv(const char *name, const int count, const float (*f)[2]);
void SetUniform3fv(const char *name, const int count, const float (*f)[3]);
void SetUniform4fv(const char *name, const int count, const float (*f)[4]);
void SetUniformMatrix4x4v(const char *name, const int count, float *v);
~vtkOpenGLUniforms() override;
vtkOpenGLUniforms(const vtkOpenGLUniforms&) = delete;
void operator=(const vtkOpenGLUniforms&) = delete;
vtkUniformInternals * Internals;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment