From 717c542d2942ab3d1224bd1879dab235ce63c627 Mon Sep 17 00:00:00 2001 From: Ben Boeckel <ben.boeckel@kitware.com> Date: Thu, 28 Mar 2019 15:47:24 -0400 Subject: [PATCH] Revert "Merge topic 'vtkShaderProperty'" This reverts commit 075f7997d5a3cb42bc1ba515829ed1b45f999a87, reversing changes made to a7add686f9a8bd0b4ac82545e16b3494d861a248. This change was causing issues in ParaView and since ParaView is in its release cycle, reverting it is easier than fixing it right now. See !5345 for discussion. Merge-request: !5096 --- .../Cxx/TestPDBBallAndStickShadowsDOFSSAA.cxx | 6 +- Rendering/Core/CMakeLists.txt | 2 - Rendering/Core/vtkProp.cxx | 34 - Rendering/Core/vtkProp.h | 12 - Rendering/Core/vtkShaderProperty.cxx | 71 -- Rendering/Core/vtkShaderProperty.h | 149 --- Rendering/Core/vtkUniforms.cxx | 80 -- Rendering/Core/vtkUniforms.h | 179 --- Rendering/OpenGL2/CMakeLists.txt | 4 - Rendering/OpenGL2/Testing/Cxx/TestCubeMap.cxx | 11 +- .../OpenGL2/Testing/Cxx/TestCubeMap2.cxx | 14 +- .../OpenGL2/Testing/Cxx/TestSphereMap.cxx | 15 +- .../OpenGL2/Testing/Cxx/TestUserShader.cxx | 20 +- .../OpenGL2/Testing/Cxx/TestUserShader2.cxx | 15 +- .../OpenGL2/Testing/Python/TestUserShader2.py | 5 +- Rendering/OpenGL2/glsl/vtkPolyDataFS.glsl | 2 - Rendering/OpenGL2/glsl/vtkPolyDataVS.glsl | 2 - .../OpenGL2/vtkCompositePolyDataMapper2.cxx | 8 +- Rendering/OpenGL2/vtkOpenGLPolyDataMapper.cxx | 256 ++-- Rendering/OpenGL2/vtkOpenGLPolyDataMapper.h | 52 +- Rendering/OpenGL2/vtkOpenGLShaderProperty.cxx | 243 ---- Rendering/OpenGL2/vtkOpenGLShaderProperty.h | 148 --- Rendering/OpenGL2/vtkOpenGLSkybox.cxx | 15 +- Rendering/OpenGL2/vtkOpenGLUniforms.cxx | 1069 ----------------- Rendering/OpenGL2/vtkOpenGLUniforms.h | 159 --- .../Python/TestGPURayCastUserShader.py | 14 +- Rendering/VolumeOpenGL2/CMakeLists.txt | 1 + .../Testing/Cxx/TestGPURayCastUserShader.cxx | 22 +- .../Testing/Cxx/TestGPURayCastUserShader2.cxx | 20 +- .../vtkOpenGLGPUVolumeRayCastMapper.cxx | 204 +++- .../vtkOpenGLGPUVolumeRayCastMapper.h | 60 +- Rendering/VolumeOpenGL2/vtkOpenGLUniforms.cxx | 593 +++++++++ Rendering/VolumeOpenGL2/vtkOpenGLUniforms.h | 110 ++ 33 files changed, 1091 insertions(+), 2504 deletions(-) delete mode 100644 Rendering/Core/vtkShaderProperty.cxx delete mode 100644 Rendering/Core/vtkShaderProperty.h delete mode 100644 Rendering/Core/vtkUniforms.cxx delete mode 100644 Rendering/Core/vtkUniforms.h delete mode 100644 Rendering/OpenGL2/vtkOpenGLShaderProperty.cxx delete mode 100644 Rendering/OpenGL2/vtkOpenGLShaderProperty.h delete mode 100644 Rendering/OpenGL2/vtkOpenGLUniforms.cxx delete mode 100644 Rendering/OpenGL2/vtkOpenGLUniforms.h create mode 100644 Rendering/VolumeOpenGL2/vtkOpenGLUniforms.cxx create mode 100644 Rendering/VolumeOpenGL2/vtkOpenGLUniforms.h diff --git a/Domains/ChemistryOpenGL2/Testing/Cxx/TestPDBBallAndStickShadowsDOFSSAA.cxx b/Domains/ChemistryOpenGL2/Testing/Cxx/TestPDBBallAndStickShadowsDOFSSAA.cxx index f078e1e0fe6..d354cd02186 100644 --- a/Domains/ChemistryOpenGL2/Testing/Cxx/TestPDBBallAndStickShadowsDOFSSAA.cxx +++ b/Domains/ChemistryOpenGL2/Testing/Cxx/TestPDBBallAndStickShadowsDOFSSAA.cxx @@ -23,7 +23,6 @@ #include "vtkOpenGLSphereMapper.h" #include "vtkNew.h" #include "vtkProperty.h" -#include "vtkShaderProperty.h" #include "vtkRenderWindowInteractor.h" #include "vtkRenderWindow.h" #include "vtkRenderer.h" @@ -88,11 +87,10 @@ int TestPDBBallAndStickShadowsDOFSSAA(int argc, char *argv[]) actor->GetProperty()->SetSpecular(0.4); actor->GetProperty()->SetSpecularPower(40); - vtkShaderProperty * sp = actor->GetShaderProperty(); - // we override the default shader very slightly so that // the ambient color component is scaled off the diffuse - sp->AddFragmentShaderReplacement( + molmapper->GetFastAtomMapper()->AddShaderReplacement( + vtkShader::Fragment, // in the fragment shader "//VTK::Color::Impl", true, // before the standard replacements "//VTK::Color::Impl\n" // we still want the default diff --git a/Rendering/Core/CMakeLists.txt b/Rendering/Core/CMakeLists.txt index 16658234cc6..2041d21060d 100644 --- a/Rendering/Core/CMakeLists.txt +++ b/Rendering/Core/CMakeLists.txt @@ -85,7 +85,6 @@ set(classes vtkRendererSource vtkSelectVisiblePoints vtkShaderDeviceAdapter2 - vtkShaderProperty vtkSkybox vtkTextActor vtkTextActor3D @@ -94,7 +93,6 @@ set(classes vtkTransformCoordinateSystems vtkTransformInterpolator vtkTupleInterpolator - vtkUniforms vtkViewDependentErrorMetric vtkViewport vtkVisibilitySort diff --git a/Rendering/Core/vtkProp.cxx b/Rendering/Core/vtkProp.cxx index a39682c6ee0..5e8e7a593e1 100644 --- a/Rendering/Core/vtkProp.cxx +++ b/Rendering/Core/vtkProp.cxx @@ -21,7 +21,6 @@ #include "vtkInformationKey.h" #include "vtkInformationIntegerKey.h" #include "vtkInformationDoubleVectorKey.h" -#include "vtkShaderProperty.h" #include <cassert> vtkCxxSetObjectMacro(vtkProp,PropertyKeys,vtkInformation); @@ -50,8 +49,6 @@ vtkProp::vtkProp() this->Consumers = nullptr; this->PropertyKeys=nullptr; - - this->ShaderProperty = nullptr; } //---------------------------------------------------------------------------- @@ -68,11 +65,6 @@ vtkProp::~vtkProp() { this->PropertyKeys->Delete(); } - - if (this->ShaderProperty) - { - this->ShaderProperty->UnRegister(this); - } } //---------------------------------------------------------------------------- @@ -89,7 +81,6 @@ void vtkProp::ShallowCopy(vtkProp *prop) this->Visibility = prop->GetVisibility(); this->Pickable = prop->GetPickable(); this->Dragable = prop->GetDragable(); - this->SetShaderProperty(prop->GetShaderProperty()); } //---------------------------------------------------------------------------- @@ -362,28 +353,3 @@ bool vtkProp::RenderFilteredOverlay(vtkViewport *v, } return result; } - -void vtkProp::SetShaderProperty(vtkShaderProperty *property) -{ - if( this->ShaderProperty != property ) - { - if (this->ShaderProperty != nullptr) {this->ShaderProperty->UnRegister(this);} - this->ShaderProperty = property; - if (this->ShaderProperty != nullptr) - { - this->ShaderProperty->Register(this); - } - this->Modified(); - } -} - -vtkShaderProperty *vtkProp::GetShaderProperty() -{ - if( this->ShaderProperty == nullptr ) - { - this->ShaderProperty = vtkShaderProperty::New(); - this->ShaderProperty->Register(this); - this->ShaderProperty->Delete(); - } - return this->ShaderProperty; -} diff --git a/Rendering/Core/vtkProp.h b/Rendering/Core/vtkProp.h index 16b59fa8543..175f929ff90 100644 --- a/Rendering/Core/vtkProp.h +++ b/Rendering/Core/vtkProp.h @@ -42,7 +42,6 @@ class vtkWindow; class vtkInformation; class vtkInformationIntegerKey; class vtkInformationDoubleVectorKey; -class vtkShaderProperty; class VTKRENDERINGCORE_EXPORT vtkProp : public vtkObject { @@ -433,14 +432,6 @@ public: int IsConsumer(vtkObject *c); //@} - //@{ - /** - * Set/Get the shader property. - */ - virtual void SetShaderProperty(vtkShaderProperty *property); - virtual vtkShaderProperty *GetShaderProperty(); - //@} - protected: vtkProp(); ~vtkProp() override; @@ -465,9 +456,6 @@ protected: vtkInformation *PropertyKeys; - // User-defined shader replacement and uniform variables - vtkShaderProperty *ShaderProperty; - private: vtkProp(const vtkProp&) = delete; void operator=(const vtkProp&) = delete; diff --git a/Rendering/Core/vtkShaderProperty.cxx b/Rendering/Core/vtkShaderProperty.cxx deleted file mode 100644 index a4e1f4d6e66..00000000000 --- a/Rendering/Core/vtkShaderProperty.cxx +++ /dev/null @@ -1,71 +0,0 @@ -/*========================================================================= - - Program: Visualization Toolkit - Module: vtkShaderProperty.cxx - - Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen - All rights reserved. - See Copyright.txt or http://www.kitware.com/Copyright.htm for details. - - This software is distributed WITHOUT ANY WARRANTY; without even - the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR - PURPOSE. See the above copyright notice for more information. - -=========================================================================*/ -#include "vtkShaderProperty.h" - -#include "vtkObjectFactory.h" -#include "vtkUniforms.h" -#include <algorithm> - -vtkAbstractObjectFactoryNewMacro(vtkShaderProperty); - -vtkShaderProperty::vtkShaderProperty() -{ - this->VertexShaderCode = nullptr; - this->FragmentShaderCode = nullptr; - this->GeometryShaderCode = nullptr; -} - -vtkShaderProperty::~vtkShaderProperty() -{ - this->SetVertexShaderCode(nullptr); - this->SetFragmentShaderCode(nullptr); - this->SetGeometryShaderCode(nullptr); -} - -void vtkShaderProperty::DeepCopy(vtkShaderProperty *p) -{ - this->SetVertexShaderCode(p->GetVertexShaderCode()); - this->SetFragmentShaderCode(p->GetFragmentShaderCode()); - this->SetGeometryShaderCode(p->GetGeometryShaderCode()); -} - -vtkMTimeType vtkShaderProperty::GetShaderMTime() -{ - vtkMTimeType fragUniformMTime = this->FragmentCustomUniforms->GetUniformListMTime(); - vtkMTimeType vertUniformMTime = this->VertexCustomUniforms->GetUniformListMTime(); - vtkMTimeType geomUniformMTime = this->GeometryCustomUniforms->GetUniformListMTime(); - return std::max( { this->ShaderReplacementTime.GetMTime(), fragUniformMTime, vertUniformMTime, geomUniformMTime } ); -} - -bool vtkShaderProperty::HasVertexShaderCode() -{ - return this->VertexShaderCode && strcmp(this->VertexShaderCode, "") != 0; -} - -bool vtkShaderProperty::HasFragmentShaderCode() -{ - return this->FragmentShaderCode && strcmp(this->FragmentShaderCode, "") != 0; -} - -bool vtkShaderProperty::HasGeometryShaderCode() -{ - return this->GeometryShaderCode && strcmp(this->GeometryShaderCode, "") != 0; -} - -//----------------------------------------------------------------------------- -void vtkShaderProperty::PrintSelf(ostream& os, vtkIndent indent) -{ - this->Superclass::PrintSelf(os, indent); -} diff --git a/Rendering/Core/vtkShaderProperty.h b/Rendering/Core/vtkShaderProperty.h deleted file mode 100644 index 8c5f68cc5ac..00000000000 --- a/Rendering/Core/vtkShaderProperty.h +++ /dev/null @@ -1,149 +0,0 @@ -/*========================================================================= - - Program: Visualization Toolkit - Module: vtkShaderProperty.h - - Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen - All rights reserved. - See Copyright.txt or http://www.kitware.com/Copyright.htm for details. - - This software is distributed WITHOUT ANY WARRANTY; without even - the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR - PURPOSE. See the above copyright notice for more information. - -=========================================================================*/ -/** - * @class vtkShaderProperty - * @brief represent GPU shader properties - * - * vtkShaderProperty is used to hold user-defined modifications of a - * GPU shader program used in a mapper. - * - * @sa - * vtkVolume vtkOpenGLUniform - * - * @par Thanks: - * Developed by Simon Drouin (sdrouin2@bwh.harvard.edu) at Brigham and Women's Hospital. - * -*/ - -#ifndef vtkShaderProperty_h -#define vtkShaderProperty_h - -#include "vtkObject.h" -#include "vtkRenderingCoreModule.h" // For export macro -#include "vtkNew.h" // For iVars - -class vtkUniforms; - -class VTKRENDERINGCORE_EXPORT vtkShaderProperty : public vtkObject -{ -public: - vtkTypeMacro(vtkShaderProperty,vtkObject); - void PrintSelf(ostream& os, vtkIndent indent) override; - - /** - * Construct object with no shader replacements - */ - static vtkShaderProperty *New(); - - /** - * Assign one property to another. - */ - void DeepCopy(vtkShaderProperty *p); - - vtkMTimeType GetShaderMTime(); - - //@{ - /** - * Allow the program to set the shader codes used directly - * instead of using the built in templates. Be aware, if - * set, this template will be used for all cases, - * primitive types, picking etc. - */ - bool HasVertexShaderCode(); - bool HasFragmentShaderCode(); - bool HasGeometryShaderCode(); - vtkSetStringMacro(VertexShaderCode); - vtkGetStringMacro(VertexShaderCode); - vtkSetStringMacro(FragmentShaderCode); - vtkGetStringMacro(FragmentShaderCode); - vtkSetStringMacro(GeometryShaderCode); - vtkGetStringMacro(GeometryShaderCode); - //@} - - //@{ - /** - * The Uniforms object allows to set custom uniform variables - * that are used in replacement shader code. - */ - vtkGetObjectMacro(FragmentCustomUniforms,vtkUniforms); - vtkGetObjectMacro(VertexCustomUniforms,vtkUniforms); - vtkGetObjectMacro(GeometryCustomUniforms,vtkUniforms); - //@} - - //@{ - /** - * This function enables you to apply your own substitutions - * to the shader creation process. The shader code in this class - * is created by applying a bunch of string replacements to a - * shader template. Using this function you can apply your - * own string replacements to add features you desire. - */ - virtual void AddVertexShaderReplacement( - const std::string& originalValue, - bool replaceFirst, // do this replacement before the default - const std::string& replacementValue, - bool replaceAll) = 0; - virtual void AddFragmentShaderReplacement( - const std::string& originalValue, - bool replaceFirst, // do this replacement before the default - const std::string& replacementValue, - bool replaceAll) = 0; - virtual void AddGeometryShaderReplacement( - const std::string& originalValue, - bool replaceFirst, // do this replacement before the default - const std::string& replacementValue, - bool replaceAll) = 0; - virtual int GetNumberOfShaderReplacements() = 0; - virtual std::string GetNthShaderReplacementTypeAsString( vtkIdType index ) = 0; - virtual void GetNthShaderReplacement( - vtkIdType index, - std::string & name, - bool & replaceFirst, - std::string & replacementValue, - bool & replaceAll ) = 0; - virtual void ClearVertexShaderReplacement( - const std::string& originalValue, - bool replaceFirst) = 0; - virtual void ClearFragmentShaderReplacement( - const std::string& originalValue, - bool replaceFirst) = 0; - virtual void ClearGeometryShaderReplacement( - const std::string& originalValue, - bool replaceFirst) = 0; - virtual void ClearAllVertexShaderReplacements() = 0; - virtual void ClearAllFragmentShaderReplacements() = 0; - virtual void ClearAllGeometryShaderReplacements() = 0; - virtual void ClearAllShaderReplacements() = 0; - //@} - -protected: - vtkShaderProperty(); - ~vtkShaderProperty() override; - - char *VertexShaderCode; - char *FragmentShaderCode; - char *GeometryShaderCode; - vtkTimeStamp ShaderReplacementTime; - - vtkNew<vtkUniforms> FragmentCustomUniforms; - vtkNew<vtkUniforms> VertexCustomUniforms; - vtkNew<vtkUniforms> GeometryCustomUniforms; - -private: - vtkShaderProperty(const vtkShaderProperty&) = delete; - void operator=(const vtkShaderProperty&) = delete; -}; - -#endif diff --git a/Rendering/Core/vtkUniforms.cxx b/Rendering/Core/vtkUniforms.cxx deleted file mode 100644 index 37c4aadc73a..00000000000 --- a/Rendering/Core/vtkUniforms.cxx +++ /dev/null @@ -1,80 +0,0 @@ -#include "vtkUniforms.h" -#include "vtkObjectFactory.h" - -//---------------------------------------------------------------------------- -// Return nullptr if no override is supplied. -vtkAbstractObjectFactoryNewMacro(vtkUniforms) - -//----------------------------------------------------------------------------- -void vtkUniforms::PrintSelf(ostream& os, vtkIndent indent) -{ - this->Superclass::PrintSelf(os, indent); -} - -std::string vtkUniforms::TupleTypeToString( TupleType tt ) -{ - std::string str; - switch (tt) - { - case vtkUniforms::TupleTypeScalar: - str = "TupleTypeScalar"; - break; - case vtkUniforms::TupleTypeVector: - str = "TupleTypeVector"; - break; - case vtkUniforms::TupleTypeMatrix: - str = "TupleTypeMatrix"; - break; - default: - str = "TupleTypeInvalid"; - break; - } - return str; -} - -vtkUniforms::TupleType vtkUniforms::StringToTupleType( const std::string & s ) -{ - if( s == "TupleTypeScalar" ) - { - return vtkUniforms::TupleTypeScalar; - } - else if( s == "TupleTypeVector" ) - { - return vtkUniforms::TupleTypeVector; - } - else if( s == "TupleTypeMatrix" ) - { - return vtkUniforms::TupleTypeMatrix; - } - return vtkUniforms::TupleTypeInvalid; -} - -/* We only support int and float as internal data types for uniform variables */ -std::string vtkUniforms::ScalarTypeToString( int scalarType ) -{ - if( scalarType == VTK_INT ) - { - return "int"; - } - else if ( scalarType == VTK_FLOAT ) - { - return "float"; - } - return "invalid"; -} - -int vtkUniforms::StringToScalarType( const std::string & s ) -{ - if( s == "int" ) - { - return VTK_INT; - } - else if( s == "float" ) - { - return VTK_FLOAT; - } - else - { - return VTK_VOID; - } -} diff --git a/Rendering/Core/vtkUniforms.h b/Rendering/Core/vtkUniforms.h deleted file mode 100644 index 3a6116a6fe3..00000000000 --- a/Rendering/Core/vtkUniforms.h +++ /dev/null @@ -1,179 +0,0 @@ -/*========================================================================= - - Program: Visualization Toolkit - - Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen - All rights reserved. - See Copyright.txt or http://www.kitware.com/Copyright.htm for details. - - This software is distributed WITHOUT ANY WARRANTY; without even - the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR - PURPOSE. See the above copyright notice for more information. - -=========================================================================*/ -/** - * @class vtkUniforms - * @brief helper class to set custom uniform variables in GPU shaders. - * - * This class implements a generic mechanism to declare and set the value of custom uniform - * variables to be used in GPU shader programs used by mappers. It allows users who specify - * custom shader code for mappers to change the value of the variable they define without - * triggering a costly rebuild of the shader. This class is used mostly as an interface and - * the implementation is found in graphics api specific derived classes (e.g.: vtkOpenGLUniforms). - * - * @sa - * vtkOpenGLUniforms vtkShaderProperty - * - * @par Thanks: - * Developed by Simon Drouin (sdrouin2@bwh.harvard.edu) at Brigham and Women's Hospital. -*/ - -#ifndef vtkUniforms_h -#define vtkUniforms_h - -#include "vtkObject.h" -#include "vtkRenderingCoreModule.h" // For export macro -#include <vector> // member function parameters - -class vtkMatrix3x3; -class vtkMatrix4x4; - -class VTKRENDERINGCORE_EXPORT vtkUniforms : public vtkObject -{ -public: - static vtkUniforms *New(); - vtkTypeMacro(vtkUniforms, vtkObject); - void PrintSelf(ostream& os, vtkIndent indent) override; - - virtual vtkMTimeType GetUniformListMTime() = 0; - - //@{ - /** - * Types of tuples that can be stored : scalar, vector, matrix - */ - //@} - enum TupleType - { - TupleTypeInvalid = 0, - TupleTypeScalar, - TupleTypeVector, - TupleTypeMatrix, - NumberOfTupleTypes - }; - - /** Convert between TupleType and string */ - static std::string TupleTypeToString( TupleType tt ); - static TupleType StringToTupleType( const std::string & s ); - - /** Convert between scalar types an string */ - static std::string ScalarTypeToString( int scalaType ); - static int StringToScalarType( const std::string & s ); - - /** Remove uniform variable named @p name */ - virtual void RemoveUniform(const char *name) = 0; - - /** Remove all uniform variables */ - virtual void RemoveAllUniforms() = 0; - - /** Generic setters and getter. Set and Get the value of - * uniform variable @p name, with TupleType @p tt, number - * of components @p nbComponents and values stored in - * @p value. These functions simplify io of uniforms */ - virtual void SetUniform( const char * name, vtkUniforms::TupleType tt, int nbComponents, const std::vector<int> & value ) = 0; - virtual void SetUniform( const char * name, vtkUniforms::TupleType tt, int nbComponents, const std::vector<float> & value ) = 0; - virtual bool GetUniform( const char * name, std::vector<int> & value ) = 0; - virtual bool GetUniform( const char * name, std::vector<float> & value ) = 0; - - /** Set the @p name uniform value to @p v. */ - virtual void SetUniformi(const char *name, int v) = 0; - virtual void SetUniformf(const char *name, float v) = 0; - virtual void SetUniform2i(const char *name, const int v[2]) = 0; - virtual void SetUniform2f(const char *name, const float v[2]) = 0; - virtual void SetUniform3f(const char *name, const float v[3]) = 0; - virtual void SetUniform4f(const char *name, const float v[4]) = 0; - virtual void SetUniformMatrix3x3(const char *name, float *v) = 0; - virtual void SetUniformMatrix4x4(const char *name, float *v) = 0; - - /** Set the @p name uniform array to @p f with @p count elements */ - virtual void SetUniform1iv(const char *name, const int count, const int *f) = 0; - virtual void SetUniform1fv(const char *name, const int count, const float *f) = 0; - virtual void SetUniform2fv(const char *name, const int count, const float (*f)[2]) = 0; - virtual void SetUniform3fv(const char *name, const int count, const float (*f)[3]) = 0; - virtual void SetUniform4fv(const char *name, const int count, const float (*f)[4]) = 0; - virtual void SetUniformMatrix4x4v(const char *name, const int count, float *v) = 0; - - /** Set the @p name uniform to @p v. - * The following are convenience functions and do not reflect - * the way the data is stored and sent to OpenGL. Data is - * converted to match one of the basic supported types */ - virtual void SetUniform3f(const char *name, const double v[3]) = 0; - virtual void SetUniform3uc(const char *name, const unsigned char v[3]) = 0; // maybe remove - virtual void SetUniform4uc(const char *name, const unsigned char v[4]) = 0; // maybe remove - virtual void SetUniformMatrix(const char *name, vtkMatrix3x3 *v) = 0; - virtual void SetUniformMatrix(const char *name, vtkMatrix4x4 *v) = 0; - - /** Get the @p name uniform value. Returns true on success. */ - virtual bool GetUniformi(const char *name, int& v) = 0; - virtual bool GetUniformf(const char *name, float& v) = 0; - virtual bool GetUniform2i(const char *name, int v[2]) = 0; - virtual bool GetUniform2f(const char *name, float v[2]) = 0; - virtual bool GetUniform3f(const char *name, float v[3]) = 0; - virtual bool GetUniform4f(const char *name, float v[4]) = 0; - virtual bool GetUniformMatrix3x3(const char *name, float *v) = 0; - virtual bool GetUniformMatrix4x4(const char *name, float *v) = 0; - - /** Get the @p name uniform to @p v. - * The following are convenience functions and do not reflect - * the way the data is stored and sent to OpenGL. Data is - * converted from one of the basic supported types */ - virtual bool GetUniform3f(const char *name, double v[3]) = 0; - virtual bool GetUniform3uc(const char *name, unsigned char v[3]) = 0; - virtual bool GetUniform4uc(const char *name, unsigned char v[4]) = 0; - virtual bool GetUniformMatrix(const char *name, vtkMatrix3x3 *v) = 0; - virtual bool GetUniformMatrix(const char *name, vtkMatrix4x4 *v) = 0; - - /** Get the @p name uniform vector to @p f with. - Buffer must be pre-allocated based on vector size provided by - */ - virtual bool GetUniform1iv(const char *name, std::vector<int>& f) = 0; - virtual bool GetUniform1fv(const char *name, std::vector<float>& f) = 0; - virtual bool GetUniform2fv(const char *name, std::vector<float>& f) = 0; - virtual bool GetUniform3fv(const char *name, std::vector<float>& f) = 0; - virtual bool GetUniform4fv(const char *name, std::vector<float>& f) = 0; - virtual bool GetUniformMatrix4x4v(const char *name, std::vector<float>& f) = 0; - - /** Get number of all uniforms stored in this class */ - virtual int GetNumberOfUniforms() = 0; - - /** Get number of all uniforms stored in this class. - Valid range is between 0 and GetNumberOfUniforms() - 1.*/ - virtual const char* GetNthUniformName(vtkIdType uniformIndex) = 0; - - /** Get type of scalars stored in uniform @p name */ - virtual int GetUniformScalarType(const char *name) = 0; - - /** Get the tuple type stored in uniform @p name. This can be a scalar, - * a vector of a matrix. */ - virtual TupleType GetUniformTupleType(const char *name) = 0; - - /** Get the number of components stored in each tuple of uniform @p name. - * for example, a uniform with tuples of matrix type and 9 components - * contains 3x3 matrices */ - virtual int GetUniformNumberOfComponents(const char *name) = 0; - - /** Number of tuples of uniform @p name that contains a variable-size vector. - * For example, for 3 components uniforms of type vector, this is the number - * of triplets. */ - virtual int GetUniformNumberOfTuples(const char *name) = 0; - -protected: - vtkUniforms() {} - ~vtkUniforms() override {} - -private: - vtkUniforms(const vtkUniforms&) = delete; - void operator=(const vtkUniforms&) = delete; - -}; - -#endif diff --git a/Rendering/OpenGL2/CMakeLists.txt b/Rendering/OpenGL2/CMakeLists.txt index bb5f9db7a5a..3fe9635160f 100644 --- a/Rendering/OpenGL2/CMakeLists.txt +++ b/Rendering/OpenGL2/CMakeLists.txt @@ -54,7 +54,6 @@ set(classes vtkOpenGLRenderWindow vtkOpenGLRenderer vtkOpenGLShaderCache - vtkOpenGLShaderProperty vtkOpenGLSkybox vtkOpenGLSphereMapper vtkOpenGLState @@ -63,7 +62,6 @@ set(classes vtkOpenGLTextActor3D vtkOpenGLTextMapper vtkOpenGLTexture - vtkOpenGLUniforms vtkOpenGLVertexArrayObject vtkOpenGLVertexBufferObject vtkOpenGLVertexBufferObjectCache @@ -174,8 +172,6 @@ set(opengl_overrides PolyDataMapper PolyDataMapper2D Property - ShaderProperty - Uniforms Renderer RenderTimerLog Skybox diff --git a/Rendering/OpenGL2/Testing/Cxx/TestCubeMap.cxx b/Rendering/OpenGL2/Testing/Cxx/TestCubeMap.cxx index ffe1082a049..938d9d0f357 100644 --- a/Rendering/OpenGL2/Testing/Cxx/TestCubeMap.cxx +++ b/Rendering/OpenGL2/Testing/Cxx/TestCubeMap.cxx @@ -28,7 +28,6 @@ #include "vtkRenderWindow.h" #include "vtkRenderWindowInteractor.h" #include "vtkShaderProgram.h" -#include "vtkShaderProperty.h" #include "vtkSmartPointer.h" #include "vtkTestUtilities.h" #include "vtkTexture.h" @@ -87,17 +86,17 @@ int TestCubeMap(int argc, char *argv[]) actor->SetTexture(texture); actor->SetMapper(mapper); - vtkShaderProperty * sp = actor->GetShaderProperty(); - // Add new code in default VTK vertex shader - sp->AddVertexShaderReplacement( + mapper->AddShaderReplacement( + vtkShader::Vertex, "//VTK::PositionVC::Dec", // replace the normal block true, // before the standard replacements "//VTK::PositionVC::Dec\n" // we still want the default "out vec3 TexCoords;\n", false // only do it once ); - sp->AddVertexShaderReplacement( + mapper->AddShaderReplacement( + vtkShader::Vertex, "//VTK::PositionVC::Impl", // replace the normal block true, // before the standard replacements "//VTK::PositionVC::Impl\n" // we still want the default @@ -107,7 +106,7 @@ int TestCubeMap(int argc, char *argv[]) ); // Replace VTK fragment shader - sp->SetFragmentShaderCode( + mapper->SetFragmentShaderCode( "//VTK::System::Dec\n" // always start with this line "//VTK::Output::Dec\n" // always have this line in your FS "in vec3 TexCoords;\n" diff --git a/Rendering/OpenGL2/Testing/Cxx/TestCubeMap2.cxx b/Rendering/OpenGL2/Testing/Cxx/TestCubeMap2.cxx index cae78ee1975..3d9e5faa8a1 100644 --- a/Rendering/OpenGL2/Testing/Cxx/TestCubeMap2.cxx +++ b/Rendering/OpenGL2/Testing/Cxx/TestCubeMap2.cxx @@ -29,7 +29,6 @@ #include "vtkRenderWindow.h" #include "vtkRenderWindowInteractor.h" #include "vtkShaderProgram.h" -#include "vtkShaderProperty.h" #include "vtkSkybox.h" #include "vtkSmartPointer.h" #include "vtkTestUtilities.h" @@ -114,15 +113,16 @@ int TestCubeMap2(int argc, char *argv[]) actor->SetTexture(texture); actor->SetMapper(mapper); - vtkShaderProperty * sp = actor->GetShaderProperty(); - sp->AddVertexShaderReplacement( + mapper->AddShaderReplacement( + vtkShader::Vertex, "//VTK::PositionVC::Dec", // replace true, // before the standard replacements "//VTK::PositionVC::Dec\n" // we still want the default "out vec3 TexCoords;\n", false // only do it once ); - sp->AddVertexShaderReplacement( + mapper->AddShaderReplacement( + vtkShader::Vertex, "//VTK::PositionVC::Impl", // replace true, // before the standard replacements "//VTK::PositionVC::Impl\n" // we still want the default @@ -130,14 +130,16 @@ int TestCubeMap2(int argc, char *argv[]) "TexCoords.xyz = reflect(vertexMC.xyz - camPos, normalize(normalMC));\n", false // only do it once ); - sp->AddFragmentShaderReplacement( + mapper->AddShaderReplacement( + vtkShader::Fragment, "//VTK::Light::Dec", // replace true, // before the standard replacements "//VTK::Light::Dec\n" // we still want the default "in vec3 TexCoords;\n", false // only do it once ); - sp->AddFragmentShaderReplacement( + mapper->AddShaderReplacement( + vtkShader::Fragment, "//VTK::Light::Impl", // replace true, // before the standard replacements " vec3 cubeColor = texture(actortexture, normalize(TexCoords)).xyz;\n" diff --git a/Rendering/OpenGL2/Testing/Cxx/TestSphereMap.cxx b/Rendering/OpenGL2/Testing/Cxx/TestSphereMap.cxx index 9440507a70d..3eddbeb2ac8 100644 --- a/Rendering/OpenGL2/Testing/Cxx/TestSphereMap.cxx +++ b/Rendering/OpenGL2/Testing/Cxx/TestSphereMap.cxx @@ -28,7 +28,6 @@ #include "vtkRenderWindow.h" #include "vtkRenderWindowInteractor.h" #include "vtkShaderProgram.h" -#include "vtkShaderProperty.h" #include "vtkSkybox.h" #include "vtkSmartPointer.h" #include "vtkTestUtilities.h" @@ -94,16 +93,16 @@ int TestSphereMap(int argc, char *argv[]) actor->SetTexture(texture); actor->SetMapper(mapper); - vtkShaderProperty * sp = actor->GetShaderProperty(); - - sp->AddVertexShaderReplacement( + mapper->AddShaderReplacement( + vtkShader::Vertex, "//VTK::PositionVC::Dec", // replace true, // before the standard replacements "//VTK::PositionVC::Dec\n" // we still want the default "out vec3 TexCoords;\n", false // only do it once ); - sp->AddVertexShaderReplacement( + mapper->AddShaderReplacement( + vtkShader::Vertex, "//VTK::PositionVC::Impl", // replace true, // before the standard replacements "//VTK::PositionVC::Impl\n" // we still want the default @@ -111,14 +110,16 @@ int TestSphereMap(int argc, char *argv[]) "TexCoords.xyz = reflect(vertexMC.xyz - camPos, normalize(normalMC));\n", false // only do it once ); - sp->AddFragmentShaderReplacement( + mapper->AddShaderReplacement( + vtkShader::Fragment, "//VTK::Light::Dec", // replace true, // before the standard replacements "//VTK::Light::Dec\n" // we still want the default "in vec3 TexCoords;\n", false // only do it once ); - sp->AddFragmentShaderReplacement( + mapper->AddShaderReplacement( + vtkShader::Fragment, "//VTK::Light::Impl", // replace true, // before the standard replacements "//VTK::Light::Impl\n" diff --git a/Rendering/OpenGL2/Testing/Cxx/TestUserShader.cxx b/Rendering/OpenGL2/Testing/Cxx/TestUserShader.cxx index 8c845f5e091..3e8bd9ec2ea 100644 --- a/Rendering/OpenGL2/Testing/Cxx/TestUserShader.cxx +++ b/Rendering/OpenGL2/Testing/Cxx/TestUserShader.cxx @@ -21,7 +21,6 @@ #include "vtkNew.h" #include "vtkProperty.h" #include "vtkPolyDataNormals.h" -#include "vtkShaderProperty.h" #include "vtkTriangleMeshPointNormals.h" #include "vtkRegressionTestImage.h" @@ -75,41 +74,46 @@ int TestUserShader(int argc, char *argv[]) // Then we modify the fragment shader to set the diffuse color // based on that normal. First lets modify the vertex // shader - vtkShaderProperty * sp = actor->GetShaderProperty(); - sp->AddVertexShaderReplacement( + mapper->AddShaderReplacement( + vtkShader::Vertex, "//VTK::Normal::Dec", // replace the normal block true, // before the standard replacements "//VTK::Normal::Dec\n" // we still want the default " out vec3 myNormalMCVSOutput;\n", //but we add this false // only do it once ); - sp->AddVertexShaderReplacement( + mapper->AddShaderReplacement( + vtkShader::Vertex, "//VTK::Normal::Impl", // replace the normal block true, // before the standard replacements "//VTK::Normal::Impl\n" // we still want the default " myNormalMCVSOutput = normalMC;\n", //but we add this false // only do it once ); - sp->AddVertexShaderReplacement( + mapper->AddShaderReplacement( + vtkShader::Vertex, "//VTK::Color::Impl", // dummy replacement for testing clear method true, "VTK::Color::Impl\n", false ); - sp->ClearVertexShaderReplacement( + mapper->ClearShaderReplacement( + vtkShader::Vertex, // clear our dummy replacement "//VTK::Color::Impl", true ); // now modify the fragment shader - sp->AddFragmentShaderReplacement( + mapper->AddShaderReplacement( + vtkShader::Fragment, // in the fragment shader "//VTK::Normal::Dec", // replace the normal block true, // before the standard replacements "//VTK::Normal::Dec\n" // we still want the default " in vec3 myNormalMCVSOutput;\n", //but we add this false // only do it once ); - sp->AddFragmentShaderReplacement( + mapper->AddShaderReplacement( + vtkShader::Fragment, // in the fragment shader "//VTK::Normal::Impl", // replace the normal block true, // before the standard replacements "//VTK::Normal::Impl\n" // we still want the default calc diff --git a/Rendering/OpenGL2/Testing/Cxx/TestUserShader2.cxx b/Rendering/OpenGL2/Testing/Cxx/TestUserShader2.cxx index 7c97576998a..40850207929 100644 --- a/Rendering/OpenGL2/Testing/Cxx/TestUserShader2.cxx +++ b/Rendering/OpenGL2/Testing/Cxx/TestUserShader2.cxx @@ -25,7 +25,6 @@ #include "vtkRenderWindowInteractor.h" #include "vtkRenderer.h" #include "vtkShaderProgram.h" -#include "vtkShaderProperty.h" #include "vtkTestUtilities.h" #include "vtkTimerLog.h" #include "vtkTriangleMeshPointNormals.h" @@ -123,15 +122,13 @@ int TestUserShader2(int argc, char *argv[]) actor->GetProperty()->SetSpecularPower(20.0); actor->GetProperty()->SetOpacity(1.0); - vtkShaderProperty * sp = actor->GetShaderProperty(); - // Clear all custom shader tag replacements // The following code is mainly for regression testing as we do not have any // custom shader replacements. - sp->ClearAllVertexShaderReplacements(); - sp->ClearAllFragmentShaderReplacements(); - sp->ClearAllGeometryShaderReplacements(); - sp->ClearAllShaderReplacements(); + mapper->ClearAllShaderReplacements(vtkShader::Vertex); + mapper->ClearAllShaderReplacements(vtkShader::Fragment); + mapper->ClearAllShaderReplacements(vtkShader::Geometry); + mapper->ClearAllShaderReplacements(); // Use our own hardcoded shader code. Generally this is a bad idea in a // general purpose program as there are so many things VTK supports that @@ -139,7 +136,7 @@ int TestUserShader2(int argc, char *argv[]) // know what your data will be like it can be very useful. The mapper will set // a bunch of uniforms regardless of if you are using them. But feel free to // use them :-) - sp->SetVertexShaderCode( + mapper->SetVertexShaderCode( "//VTK::System::Dec\n" // always start with this line "in vec4 vertexMC;\n" // use the default normal decl as the mapper @@ -156,7 +153,7 @@ int TestUserShader2(int argc, char *argv[]) " gl_Position = tmpPos*vec4(0.2+0.8*abs(tmpPos.x),0.2+0.8*abs(tmpPos.y),1.0,1.0);\n" "}\n" ); - sp->SetFragmentShaderCode( + mapper->SetFragmentShaderCode( "//VTK::System::Dec\n" // always start with this line "//VTK::Output::Dec\n" // always have this line in your FS "in vec3 normalVCVSOutput;\n" diff --git a/Rendering/OpenGL2/Testing/Python/TestUserShader2.py b/Rendering/OpenGL2/Testing/Python/TestUserShader2.py index 0f063762232..6cefc396898 100755 --- a/Rendering/OpenGL2/Testing/Python/TestUserShader2.py +++ b/Rendering/OpenGL2/Testing/Python/TestUserShader2.py @@ -62,8 +62,7 @@ actor.GetProperty().SetAmbient(0.5) actor.GetProperty().SetSpecularPower(20.0) actor.GetProperty().SetOpacity(1.0) -sp = actor.GetShaderProperty() -sp.SetVertexShaderCode( +mapper.SetVertexShaderCode( "//VTK::System::Dec\n" "in vec4 vertexMC;\n" "//VTK::Normal::Dec\n" @@ -74,7 +73,7 @@ sp.SetVertexShaderCode( " gl_Position = tmpPos*vec4(0.2+0.8*abs(tmpPos.x),0.2+0.8*abs(tmpPos.y),1.0,1.0);\n" "}\n" ) -sp.SetFragmentShaderCode( +mapper.SetFragmentShaderCode( "//VTK::System::Dec\n" "//VTK::Output::Dec\n" "in vec3 normalVCVSOutput;\n" diff --git a/Rendering/OpenGL2/glsl/vtkPolyDataFS.glsl b/Rendering/OpenGL2/glsl/vtkPolyDataFS.glsl index 63880887e32..da3bf8ef8e2 100644 --- a/Rendering/OpenGL2/glsl/vtkPolyDataFS.glsl +++ b/Rendering/OpenGL2/glsl/vtkPolyDataFS.glsl @@ -18,8 +18,6 @@ uniform int PrimitiveIDOffset; -//VTK::CustomUniforms::Dec - // VC position of this fragment //VTK::PositionVC::Dec diff --git a/Rendering/OpenGL2/glsl/vtkPolyDataVS.glsl b/Rendering/OpenGL2/glsl/vtkPolyDataVS.glsl index 90ec0910b6d..5a4e4c0da27 100644 --- a/Rendering/OpenGL2/glsl/vtkPolyDataVS.glsl +++ b/Rendering/OpenGL2/glsl/vtkPolyDataVS.glsl @@ -17,8 +17,6 @@ in vec4 vertexMC; -//VTK::CustomUniforms::Dec - // frag position in VC //VTK::PositionVC::Dec diff --git a/Rendering/OpenGL2/vtkCompositePolyDataMapper2.cxx b/Rendering/OpenGL2/vtkCompositePolyDataMapper2.cxx index a3384364bb7..568ea8665d6 100644 --- a/Rendering/OpenGL2/vtkCompositePolyDataMapper2.cxx +++ b/Rendering/OpenGL2/vtkCompositePolyDataMapper2.cxx @@ -39,7 +39,6 @@ #include "vtkOpenGLTexture.h" #include "vtkOpenGLVertexBufferObject.h" #include "vtkOpenGLVertexBufferObjectGroup.h" -#include "vtkOpenGLShaderProperty.h" #include "vtkPointData.h" #include "vtkPolyData.h" #include "vtkProperty.h" @@ -1550,14 +1549,12 @@ void vtkCompositePolyDataMapper2::CopyMapperValuesToHelper(vtkCompositeMapperHel helper->SetCompositeIdArrayName(this->GetCompositeIdArrayName()); helper->SetProcessIdArrayName(this->GetProcessIdArrayName()); helper->SetCellIdArrayName(this->GetCellIdArrayName()); - helper->SetStatic(1); -#ifndef VTK_LEGACY_REMOVE helper->ClearAllShaderReplacements(); helper->SetVertexShaderCode(this->GetVertexShaderCode()); helper->SetGeometryShaderCode(this->GetGeometryShaderCode()); helper->SetFragmentShaderCode(this->GetFragmentShaderCode()); - const vtkOpenGLShaderProperty::ReplacementMap allReps = this->LegacyShaderProperty->GetAllShaderReplacements(); - for (auto& repl : allReps) + helper->SetStatic(1); + for (auto& repl : this->UserShaderReplacements) { const vtkShader::ReplacementSpec& spec = repl.first; const vtkShader::ReplacementValue& values = repl.second; @@ -1565,7 +1562,6 @@ void vtkCompositePolyDataMapper2::CopyMapperValuesToHelper(vtkCompositeMapperHel helper->AddShaderReplacement(spec.ShaderType, spec.OriginalValue, spec.ReplaceFirst, values.Replacement, values.ReplaceAll); } -#endif } //----------------------------------------------------------------------------- diff --git a/Rendering/OpenGL2/vtkOpenGLPolyDataMapper.cxx b/Rendering/OpenGL2/vtkOpenGLPolyDataMapper.cxx index 7f9c23582aa..6e598968ca3 100644 --- a/Rendering/OpenGL2/vtkOpenGLPolyDataMapper.cxx +++ b/Rendering/OpenGL2/vtkOpenGLPolyDataMapper.cxx @@ -48,8 +48,6 @@ #include "vtkOpenGLVertexBufferObject.h" #include "vtkOpenGLVertexBufferObjectCache.h" #include "vtkOpenGLVertexBufferObjectGroup.h" -#include "vtkOpenGLShaderProperty.h" -#include "vtkOpenGLUniforms.h" #include "vtkPointData.h" #include "vtkPolyData.h" #include "vtkProperty.h" @@ -105,6 +103,10 @@ vtkOpenGLPolyDataMapper::vtkOpenGLPolyDataMapper() this->LastBoundBO = nullptr; + this->VertexShaderCode = nullptr; + this->FragmentShaderCode = nullptr; + this->GeometryShaderCode = nullptr; + for (int i = PrimitiveStart; i < PrimitiveEnd; i++) { this->LastLightComplexity[&this->Primitives[i]] = -1; @@ -166,6 +168,9 @@ vtkOpenGLPolyDataMapper::~vtkOpenGLPolyDataMapper() this->VBOs->Delete(); this->VBOs = nullptr; + this->SetVertexShaderCode(nullptr); + this->SetFragmentShaderCode(nullptr); + this->SetGeometryShaderCode(nullptr); delete TimerQuery; } @@ -211,8 +216,6 @@ void vtkOpenGLPolyDataMapper::ReleaseGraphicsResources(vtkWindow* win) this->Modified(); } -#ifndef VTK_LEGACY_REMOVE - //----------------------------------------------------------------------------- void vtkOpenGLPolyDataMapper::AddShaderReplacement( vtkShader::Type shaderType, // vertex, fragment, etc @@ -221,8 +224,16 @@ void vtkOpenGLPolyDataMapper::AddShaderReplacement( const std::string& replacementValue, bool replaceAll) { - VTK_LEGACY_REPLACED_BODY(vtkOpenGLPolyDataMapper::AddShaderReplacement, "VTK 8.2", vtkOpenGLShaderProperty::AddShaderReplacement); - this->LegacyShaderProperty->AddShaderReplacement(shaderType,originalValue,replaceFirst,replacementValue,replaceAll); + vtkShader::ReplacementSpec spec; + spec.ShaderType = shaderType; + spec.OriginalValue = originalValue; + spec.ReplaceFirst = replaceFirst; + + vtkShader::ReplacementValue values; + values.Replacement = replacementValue; + values.ReplaceAll = replaceAll; + + this->UserShaderReplacements[spec] = values; this->Modified(); } @@ -232,66 +243,66 @@ void vtkOpenGLPolyDataMapper::ClearShaderReplacement( const std::string& originalValue, bool replaceFirst) { - VTK_LEGACY_REPLACED_BODY(vtkOpenGLPolyDataMapper::ClearShaderReplacement, "VTK 8.2", vtkOpenGLShaderProperty::ClearShaderReplacement); - this->LegacyShaderProperty->ClearShaderReplacement(shaderType, originalValue, replaceFirst); - this->Modified(); + vtkShader::ReplacementSpec spec; + spec.ShaderType = shaderType; + spec.OriginalValue = originalValue; + spec.ReplaceFirst = replaceFirst; + + typedef std::map<const vtkShader::ReplacementSpec, + vtkShader::ReplacementValue>::iterator RIter; + RIter found = this->UserShaderReplacements.find(spec); + if (found != this->UserShaderReplacements.end()) + { + this->UserShaderReplacements.erase(found); + this->Modified(); + } } //----------------------------------------------------------------------------- void vtkOpenGLPolyDataMapper::ClearAllShaderReplacements( vtkShader::Type shaderType) { - VTK_LEGACY_REPLACED_BODY(vtkOpenGLPolyDataMapper::ClearAllShaderReplacements, "VTK 8.2", vtkOpenGLShaderProperty::ClearAllShaderReplacements); - this->LegacyShaderProperty->ClearAllShaderReplacements(shaderType); - this->Modified(); -} - -//----------------------------------------------------------------------------- -void vtkOpenGLPolyDataMapper::ClearAllShaderReplacements() -{ - this->LegacyShaderProperty->ClearAllShaderReplacements(); - this->Modified(); -} - - void vtkOpenGLPolyDataMapper::SetVertexShaderCode(const char* code) + // First clear all shader code + if ((shaderType == vtkShader::Vertex) && this->VertexShaderCode) { - VTK_LEGACY_REPLACED_BODY(vtkOpenGLPolyDataMapper::SetVertexShaderCode, "VTK 8.2", vtkOpenGLShaderProperty::SetVertexShaderCode); - this->LegacyShaderProperty->SetVertexShaderCode(code); - this->Modified(); + this->SetVertexShaderCode(nullptr); } - - char* vtkOpenGLPolyDataMapper::GetVertexShaderCode() + else if ((shaderType == vtkShader::Fragment) && this->FragmentShaderCode) { - VTK_LEGACY_REPLACED_BODY(vtkOpenGLPolyDataMapper::GetVertexShaderCode, "VTK 8.2", vtkOpenGLShaderProperty::GetVertexShaderCode); - return this->LegacyShaderProperty->GetVertexShaderCode(); + this->SetFragmentShaderCode(nullptr); } - - void vtkOpenGLPolyDataMapper::SetFragmentShaderCode(const char* code) + else if ((shaderType == vtkShader::Geometry) && this->GeometryShaderCode) { - VTK_LEGACY_REPLACED_BODY(vtkOpenGLPolyDataMapper::SetFragmentShaderCode, "VTK 8.2", vtkOpenGLShaderProperty::SetFragmentShaderCode); - this->LegacyShaderProperty->SetFragmentShaderCode(code); - this->Modified(); + this->SetGeometryShaderCode(nullptr); } - char* vtkOpenGLPolyDataMapper::GetFragmentShaderCode() + // Now clear custom tag replacements + std::map<const vtkShader::ReplacementSpec, + vtkShader::ReplacementValue>::iterator rIter; + for (rIter = this->UserShaderReplacements.begin(); + rIter != this->UserShaderReplacements.end();) { - VTK_LEGACY_REPLACED_BODY(vtkOpenGLPolyDataMapper::GetFragmentShaderCode, "VTK 8.2", vtkOpenGLShaderProperty::GetFragmentShaderCode); - return this->LegacyShaderProperty->GetFragmentShaderCode(); - } - - void vtkOpenGLPolyDataMapper::SetGeometryShaderCode(const char* code) - { - VTK_LEGACY_REPLACED_BODY(vtkOpenGLPolyDataMapper::SetGeometryShaderCode, "VTK 8.2", vtkOpenGLShaderProperty::SetGeometryShaderCode); - this->LegacyShaderProperty->SetGeometryShaderCode(code); - this->Modified(); + if (rIter->first.ShaderType == shaderType) + { + this->UserShaderReplacements.erase(rIter++); + this->Modified(); + } + else + { + ++rIter; + } } +} - char* vtkOpenGLPolyDataMapper::GetGeometryShaderCode() - { - VTK_LEGACY_REPLACED_BODY(vtkOpenGLPolyDataMapper::GetGeometryShaderCode, "VTK 8.2", vtkOpenGLShaderProperty::GetGeometryShaderCode); - return this->LegacyShaderProperty->GetGeometryShaderCode(); - } -#endif +//----------------------------------------------------------------------------- +void vtkOpenGLPolyDataMapper::ClearAllShaderReplacements() +{ + this->SetVertexShaderCode(nullptr); + this->SetFragmentShaderCode(nullptr); + this->SetGeometryShaderCode(nullptr); + this->UserShaderReplacements.clear(); + this->Modified(); +} //----------------------------------------------------------------------------- void vtkOpenGLPolyDataMapper::BuildShaders( @@ -300,69 +311,40 @@ void vtkOpenGLPolyDataMapper::BuildShaders( { this->GetShaderTemplate(shaders, ren, actor); - // user specified pre replacements - vtkOpenGLShaderProperty * sp = vtkOpenGLShaderProperty::SafeDownCast( actor->GetShaderProperty() ); - vtkOpenGLShaderProperty::ReplacementMap repMap = sp->GetAllShaderReplacements(); - for ( auto i : repMap ) - { - if (i.first.ReplaceFirst) - { - std::string ssrc = shaders[i.first.ShaderType]->GetSource(); - vtkShaderProgram::Substitute(ssrc, - i.first.OriginalValue, - i.second.Replacement, - i.second.ReplaceAll); - shaders[i.first.ShaderType]->SetSource(ssrc); - } - } - -#ifndef VTK_LEGACY_REMOVE - vtkOpenGLShaderProperty::ReplacementMap repMapl = this->LegacyShaderProperty->GetAllShaderReplacements(); - for ( auto i : repMapl ) - { - if (i.first.ReplaceFirst) - { - std::string ssrc = shaders[i.first.ShaderType]->GetSource(); - vtkShaderProgram::Substitute(ssrc, - i.first.OriginalValue, - i.second.Replacement, - i.second.ReplaceAll); - shaders[i.first.ShaderType]->SetSource(ssrc); - } - } -#endif + typedef std::map<const vtkShader::ReplacementSpec, + vtkShader::ReplacementValue>::const_iterator RIter; - this->ReplaceShaderValues(shaders, ren, actor); - - // user specified post replacements - for ( auto i : repMap ) + // user specified pre replacements + for (RIter i = this->UserShaderReplacements.begin(); + i != this->UserShaderReplacements.end(); ++i) { - if (!i.first.ReplaceFirst) + if (i->first.ReplaceFirst) { - std::string ssrc = shaders[i.first.ShaderType]->GetSource(); + std::string ssrc = shaders[i->first.ShaderType]->GetSource(); vtkShaderProgram::Substitute(ssrc, - i.first.OriginalValue, - i.second.Replacement, - i.second.ReplaceAll); - shaders[i.first.ShaderType]->SetSource(ssrc); + i->first.OriginalValue, + i->second.Replacement, + i->second.ReplaceAll); + shaders[i->first.ShaderType]->SetSource(ssrc); } } -#ifndef VTK_LEGACY_REMOVE - for ( auto i : repMapl ) + this->ReplaceShaderValues(shaders, ren, actor); + + // user specified post replacements + for (RIter i = this->UserShaderReplacements.begin(); + i != this->UserShaderReplacements.end(); ++i) { - if (!i.first.ReplaceFirst) + if (!i->first.ReplaceFirst) { - std::string ssrc = shaders[i.first.ShaderType]->GetSource(); + std::string ssrc = shaders[i->first.ShaderType]->GetSource(); vtkShaderProgram::Substitute(ssrc, - i.first.OriginalValue, - i.second.Replacement, - i.second.ReplaceAll); - shaders[i.first.ShaderType]->SetSource(ssrc); + i->first.OriginalValue, + i->second.Replacement, + i->second.ReplaceAll); + shaders[i->first.ShaderType]->SetSource(ssrc); } } -#endif - } //----------------------------------------------------------------------------- @@ -525,47 +507,28 @@ void vtkOpenGLPolyDataMapper::GetShaderTemplate( std::map<vtkShader::Type, vtkShader *> shaders, vtkRenderer *ren, vtkActor *actor) { - vtkShaderProperty * sp = actor->GetShaderProperty(); - if( sp->HasVertexShaderCode() ) - { - shaders[vtkShader::Vertex]->SetSource(sp->GetVertexShaderCode()); - } -#ifndef VTK_LEGACY_REMOVE - else if( this->LegacyShaderProperty->HasVertexShaderCode() ) + if (this->VertexShaderCode && strcmp(this->VertexShaderCode,"") != 0) { - shaders[vtkShader::Vertex]->SetSource(this->LegacyShaderProperty->GetVertexShaderCode()); + shaders[vtkShader::Vertex]->SetSource(this->VertexShaderCode); } -#endif else { shaders[vtkShader::Vertex]->SetSource(vtkPolyDataVS); } - if( sp->HasFragmentShaderCode() ) - { - shaders[vtkShader::Fragment]->SetSource(sp->GetFragmentShaderCode()); - } -#ifndef VTK_LEGACY_REMOVE - else if( this->LegacyShaderProperty->HasFragmentShaderCode() ) + if (this->FragmentShaderCode && strcmp(this->FragmentShaderCode,"") != 0) { - shaders[vtkShader::Fragment]->SetSource(this->LegacyShaderProperty->GetFragmentShaderCode()); + shaders[vtkShader::Fragment]->SetSource(this->FragmentShaderCode); } -#endif else { shaders[vtkShader::Fragment]->SetSource(vtkPolyDataFS); } - if( sp->HasGeometryShaderCode() ) - { - shaders[vtkShader::Geometry]->SetSource(sp->GetGeometryShaderCode()); - } -#ifndef VTK_LEGACY_REMOVE - else if( this->LegacyShaderProperty->HasGeometryShaderCode() ) + if (this->GeometryShaderCode && strcmp(this->GeometryShaderCode,"") != 0) { - shaders[vtkShader::Geometry]->SetSource(this->LegacyShaderProperty->GetGeometryShaderCode()); + shaders[vtkShader::Geometry]->SetSource(this->GeometryShaderCode); } -#endif else { if (this->HaveWideLines(ren, actor)) @@ -622,32 +585,6 @@ void vtkOpenGLPolyDataMapper::ReplaceShaderRenderPass( shaders[vtkShader::Fragment]->SetSource(FSSource); } -//------------------------------------------------------------------------------ -void vtkOpenGLPolyDataMapper::ReplaceShaderCustomUniforms( - std::map<vtkShader::Type, vtkShader *> shaders, - vtkActor *actor) -{ - vtkShaderProperty * sp = actor->GetShaderProperty(); - - vtkShader* vertexShader = shaders[vtkShader::Vertex]; - vtkOpenGLUniforms * vu = static_cast<vtkOpenGLUniforms*>(sp->GetVertexCustomUniforms()); - vtkShaderProgram::Substitute(vertexShader, - "//VTK::CustomUniforms::Dec", - vu->GetDeclarations()); - - vtkShader* fragmentShader = shaders[vtkShader::Fragment]; - vtkOpenGLUniforms * fu = static_cast<vtkOpenGLUniforms*>(sp->GetFragmentCustomUniforms()); - vtkShaderProgram::Substitute(fragmentShader, - "//VTK::CustomUniforms::Dec", - fu->GetDeclarations()); - - vtkShader* geometryShader = shaders[vtkShader::Geometry]; - vtkOpenGLUniforms * gu = static_cast<vtkOpenGLUniforms*>(sp->GetGeometryCustomUniforms()); - vtkShaderProgram::Substitute(geometryShader, - "//VTK::CustomUniforms::Dec", - gu->GetDeclarations()); -} - //------------------------------------------------------------------------------ void vtkOpenGLPolyDataMapper::ReplaceShaderColor( std::map<vtkShader::Type, vtkShader *> shaders, @@ -1696,7 +1633,6 @@ void vtkOpenGLPolyDataMapper::ReplaceShaderValues( vtkRenderer *ren, vtkActor *actor) { this->ReplaceShaderRenderPass(shaders, ren, actor, true); - this->ReplaceShaderCustomUniforms(shaders,actor); this->ReplaceShaderColor(shaders, ren, actor); this->ReplaceShaderNormal(shaders, ren, actor); this->ReplaceShaderLight(shaders, ren, actor); @@ -1894,7 +1830,6 @@ void vtkOpenGLPolyDataMapper::UpdateShaders( if (cellBO.Program) { - this->SetCustomUniforms( cellBO, actor); this->SetMapperShaderParameters(cellBO, ren, actor); this->SetPropertyShaderParameters(cellBO, ren, actor); this->SetCameraShaderParameters(cellBO, ren, actor); @@ -1907,17 +1842,6 @@ void vtkOpenGLPolyDataMapper::UpdateShaders( vtkOpenGLCheckErrorMacro("failed after UpdateShader"); } -void vtkOpenGLPolyDataMapper::SetCustomUniforms( vtkOpenGLHelper & cellBO, vtkActor *actor) -{ - vtkShaderProperty * sp = actor->GetShaderProperty(); - auto vu = static_cast<vtkOpenGLUniforms*>(sp->GetVertexCustomUniforms()); - vu->SetUniforms( cellBO.Program ); - auto fu = static_cast<vtkOpenGLUniforms*>(sp->GetFragmentCustomUniforms()); - fu->SetUniforms( cellBO.Program ); - auto gu = static_cast<vtkOpenGLUniforms*>(sp->GetGeometryCustomUniforms()); - gu->SetUniforms( cellBO.Program ); -} - void vtkOpenGLPolyDataMapper::SetMapperShaderParameters(vtkOpenGLHelper &cellBO, vtkRenderer* ren, vtkActor *actor) { @@ -3166,11 +3090,9 @@ void vtkOpenGLPolyDataMapper::ShallowCopy(vtkAbstractMapper *mapper) this->SetCompositeIdArrayName(m->GetCompositeIdArrayName()); this->SetProcessIdArrayName(m->GetProcessIdArrayName()); this->SetCellIdArrayName(m->GetCellIdArrayName()); -#ifndef VTK_LEGACY_REMOVE this->SetVertexShaderCode(m->GetVertexShaderCode()); this->SetGeometryShaderCode(m->GetGeometryShaderCode()); this->SetFragmentShaderCode(m->GetFragmentShaderCode()); -#endif } // Now do superclass diff --git a/Rendering/OpenGL2/vtkOpenGLPolyDataMapper.h b/Rendering/OpenGL2/vtkOpenGLPolyDataMapper.h index 535c2c732d2..e7bd6e709c4 100644 --- a/Rendering/OpenGL2/vtkOpenGLPolyDataMapper.h +++ b/Rendering/OpenGL2/vtkOpenGLPolyDataMapper.h @@ -46,7 +46,6 @@ class vtkPoints; class vtkTexture; class vtkTextureObject; class vtkTransform; -class vtkOpenGLShaderProperty; class VTKRENDERINGOPENGL2_EXPORT vtkOpenGLPolyDataMapper : public vtkPolyDataMapper @@ -142,7 +141,7 @@ public: vtkGetStringMacro(CompositeIdArrayName); //@} -#ifndef VTK_LEGACY_REMOVE + //@{ /** * This function enables you to apply your own substitutions @@ -150,21 +149,19 @@ public: * is created by applying a bunch of string replacements to a * shader template. Using this function you can apply your * own string replacements to add features you desire. - * - * @deprecated Replaced By vtkShaderProperty::{Add,Clear,ClearAll}ShaderReplacements as of VTK 8.2. */ - VTK_LEGACY(void AddShaderReplacement( + void AddShaderReplacement( vtkShader::Type shaderType, // vertex, fragment, etc const std::string& originalValue, bool replaceFirst, // do this replacement before the default const std::string& replacementValue, - bool replaceAll);) - VTK_LEGACY(void ClearShaderReplacement( + bool replaceAll); + void ClearShaderReplacement( vtkShader::Type shaderType, // vertex, fragment, etc const std::string& originalValue, - bool replaceFirst);) - VTK_LEGACY(void ClearAllShaderReplacements(vtkShader::Type shaderType);) - VTK_LEGACY(void ClearAllShaderReplacements();) + bool replaceFirst); + void ClearAllShaderReplacements(vtkShader::Type shaderType); + void ClearAllShaderReplacements(); //@} //@{ @@ -173,17 +170,14 @@ public: * instead of using the built in templates. Be aware, if * set, this template will be used for all cases, * primitive types, picking etc. - * - * @deprecated Replaced By vtkShaderProperty::Get*ShaderCode as of VTK 8.2. */ - VTK_LEGACY(virtual void SetVertexShaderCode(const char* code);) - VTK_LEGACY(virtual char* GetVertexShaderCode();) - VTK_LEGACY(virtual void SetFragmentShaderCode(const char* code);) - VTK_LEGACY(virtual char* GetFragmentShaderCode();) - VTK_LEGACY(virtual void SetGeometryShaderCode(const char* code);) - VTK_LEGACY(virtual char* GetGeometryShaderCode();) + vtkSetStringMacro(VertexShaderCode); + vtkGetStringMacro(VertexShaderCode); + vtkSetStringMacro(FragmentShaderCode); + vtkGetStringMacro(FragmentShaderCode); + vtkSetStringMacro(GeometryShaderCode); + vtkGetStringMacro(GeometryShaderCode); //@} -#endif /** * Make a shallow copy of this mapper. @@ -325,9 +319,6 @@ protected: virtual void ReplaceShaderRenderPass( std::map<vtkShader::Type, vtkShader *> shaders, vtkRenderer *ren, vtkActor *act, bool prePass); - virtual void ReplaceShaderCustomUniforms( - std::map<vtkShader::Type, vtkShader *> shaders, - vtkActor *act); virtual void ReplaceShaderColor( std::map<vtkShader::Type, vtkShader *> shaders, vtkRenderer *ren, vtkActor *act); @@ -360,11 +351,6 @@ protected: vtkRenderer *ren, vtkActor *act); //@} - /** - * Set the value of user-defined uniform variables, called by UpdateShader - */ - virtual void SetCustomUniforms( vtkOpenGLHelper & cellBO, vtkActor *actor); - /** * Set the shader parameters related to the mapper/input data, called by UpdateShader */ @@ -501,6 +487,9 @@ protected: char* ProcessIdArrayName; char* CompositeIdArrayName; + std::map<const vtkShader::ReplacementSpec, vtkShader::ReplacementValue> + UserShaderReplacements; + class ExtraAttributeValue { public: @@ -511,12 +500,9 @@ protected: }; std::map<std::string,ExtraAttributeValue> ExtraAttributes; - // Store shader properties on this class by legacy shader replacement functions - // This should disapear when the functions are deprecated -#ifndef VTK_LEGACY_REMOVE - vtkNew<vtkOpenGLShaderProperty> LegacyShaderProperty; -#endif - + char *VertexShaderCode; + char *FragmentShaderCode; + char *GeometryShaderCode; vtkOpenGLRenderTimer *TimerQuery; // are we currently drawing spheres/tubes diff --git a/Rendering/OpenGL2/vtkOpenGLShaderProperty.cxx b/Rendering/OpenGL2/vtkOpenGLShaderProperty.cxx deleted file mode 100644 index 8e99eaeb54c..00000000000 --- a/Rendering/OpenGL2/vtkOpenGLShaderProperty.cxx +++ /dev/null @@ -1,243 +0,0 @@ -/*========================================================================= - - Program: Visualization Toolkit - Module: vtkShaderProperty.cxx - - Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen - All rights reserved. - See Copyright.txt or http://www.kitware.com/Copyright.htm for details. - - This software is distributed WITHOUT ANY WARRANTY; without even - the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR - PURPOSE. See the above copyright notice for more information. - -=========================================================================*/ -#include "vtkOpenGLShaderProperty.h" - -#include "vtkObjectFactory.h" -#include "vtkOpenGLUniforms.h" -#include <algorithm> - -vtkStandardNewMacro(vtkOpenGLShaderProperty); - -vtkOpenGLShaderProperty::vtkOpenGLShaderProperty() = default; - -vtkOpenGLShaderProperty::~vtkOpenGLShaderProperty() = default; - -void vtkOpenGLShaderProperty::DeepCopy(vtkOpenGLShaderProperty *p) -{ - vtkShaderProperty::DeepCopy(p); -} - -vtkMTimeType vtkOpenGLShaderProperty::GetShaderMTime() -{ - vtkMTimeType fragUniformMTime = this->FragmentCustomUniforms->GetUniformListMTime(); - vtkMTimeType vertUniformMTime = this->VertexCustomUniforms->GetUniformListMTime(); - return std::max( { this->ShaderReplacementTime.GetMTime(), fragUniformMTime, vertUniformMTime } ); -} - -void vtkOpenGLShaderProperty::AddVertexShaderReplacement( - const std::string& originalValue, - bool replaceFirst, // do this replacement before the default - const std::string& replacementValue, - bool replaceAll) -{ - this->AddShaderReplacement(vtkShader::Vertex,originalValue,replaceFirst,replacementValue,replaceAll); -} - -void vtkOpenGLShaderProperty::AddFragmentShaderReplacement( - const std::string& originalValue, - bool replaceFirst, // do this replacement before the default - const std::string& replacementValue, - bool replaceAll) -{ - this->AddShaderReplacement(vtkShader::Fragment,originalValue,replaceFirst,replacementValue,replaceAll); -} - -void vtkOpenGLShaderProperty::AddGeometryShaderReplacement( - const std::string& originalValue, - bool replaceFirst, // do this replacement before the default - const std::string& replacementValue, - bool replaceAll) -{ - this->AddShaderReplacement(vtkShader::Geometry,originalValue,replaceFirst,replacementValue,replaceAll); -} - -int vtkOpenGLShaderProperty::GetNumberOfShaderReplacements() -{ - return static_cast<int>(UserShaderReplacements.size()); -} - -std::string vtkOpenGLShaderProperty::GetNthShaderReplacementTypeAsString( vtkIdType index ) -{ - if( index >= static_cast<vtkIdType>(this->UserShaderReplacements.size()) ) - { - vtkErrorMacro( << "Trying to access out of bound shader replacement."); - return std::string(""); - } - ReplacementMap::iterator it = this->UserShaderReplacements.begin(); - std::advance(it, index); - if( it->first.ShaderType == vtkShader::Vertex ) - { - return std::string("Vertex"); - } - return std::string("Fragment"); -} - -void vtkOpenGLShaderProperty::GetNthShaderReplacement( - vtkIdType index, - std::string & name, - bool & replaceFirst, - std::string & replacementValue, - bool & replaceAll ) -{ - if( index >= static_cast<vtkIdType>(this->UserShaderReplacements.size()) ) - { - vtkErrorMacro( << "Trying to access out of bound shader replacement."); - } - ReplacementMap::iterator it = this->UserShaderReplacements.begin(); - std::advance(it, index); - name = it->first.OriginalValue; - replaceFirst = it->first.ReplaceFirst; - replacementValue = it->second.Replacement; - replaceAll = it->second.ReplaceAll; -} - -void vtkOpenGLShaderProperty::ClearVertexShaderReplacement( - const std::string& originalValue, - bool replaceFirst) -{ - this->ClearShaderReplacement(vtkShader::Vertex,originalValue,replaceFirst); -} - -void vtkOpenGLShaderProperty::ClearFragmentShaderReplacement( - const std::string& originalValue, - bool replaceFirst) -{ - this->ClearShaderReplacement(vtkShader::Fragment,originalValue,replaceFirst); -} - -void vtkOpenGLShaderProperty::ClearGeometryShaderReplacement( - const std::string& originalValue, - bool replaceFirst) -{ - this->ClearShaderReplacement(vtkShader::Geometry,originalValue,replaceFirst); -} - -void vtkOpenGLShaderProperty::ClearAllVertexShaderReplacements() -{ - this->ClearAllShaderReplacements(vtkShader::Vertex); -} - -void vtkOpenGLShaderProperty::ClearAllFragmentShaderReplacements() -{ - this->ClearAllShaderReplacements(vtkShader::Fragment); -} - -void vtkOpenGLShaderProperty::ClearAllGeometryShaderReplacements() -{ - this->ClearAllShaderReplacements(vtkShader::Geometry); -} - -//----------------------------------------------------------------------------- -void vtkOpenGLShaderProperty::ClearAllShaderReplacements() -{ - this->SetVertexShaderCode(nullptr); - this->SetFragmentShaderCode(nullptr); - if (!this->UserShaderReplacements.empty()) - { - this->UserShaderReplacements.clear(); - } - this->ShaderReplacementTime.Modified(); - Modified(); -} - -//----------------------------------------------------------------------------- -void vtkOpenGLShaderProperty::AddShaderReplacement( - vtkShader::Type shaderType, // vertex, fragment, etc - const std::string& originalValue, - bool replaceFirst, // do this replacement before the default - const std::string& replacementValue, - bool replaceAll) -{ - vtkShader::ReplacementSpec spec; - spec.ShaderType = shaderType; - spec.OriginalValue = originalValue; - spec.ReplaceFirst = replaceFirst; - - vtkShader::ReplacementValue values; - values.Replacement = replacementValue; - values.ReplaceAll = replaceAll; - - this->UserShaderReplacements[spec] = values; - this->ShaderReplacementTime.Modified(); - Modified(); -} - -//----------------------------------------------------------------------------- -void vtkOpenGLShaderProperty::ClearShaderReplacement( - vtkShader::Type shaderType, // vertex, fragment, etc - const std::string& originalValue, - bool replaceFirst) -{ - vtkShader::ReplacementSpec spec; - spec.ShaderType = shaderType; - spec.OriginalValue = originalValue; - spec.ReplaceFirst = replaceFirst; - - typedef std::map<const vtkShader::ReplacementSpec, - vtkShader::ReplacementValue>::iterator RIter; - RIter found = this->UserShaderReplacements.find(spec); - if (found != this->UserShaderReplacements.end()) - { - this->UserShaderReplacements.erase(found); - this->ShaderReplacementTime.Modified(); - Modified(); - } -} - -//----------------------------------------------------------------------------- -void vtkOpenGLShaderProperty::ClearAllShaderReplacements( - vtkShader::Type shaderType) -{ - bool modified = false; - // First clear all shader code - if ((shaderType == vtkShader::Vertex) && this->VertexShaderCode) - { - this->SetVertexShaderCode(nullptr); - modified = true; - } - else if ((shaderType == vtkShader::Fragment) && this->FragmentShaderCode) - { - this->SetFragmentShaderCode(nullptr); - modified = true; - } - - // Now clear custom tag replacements - std::map<const vtkShader::ReplacementSpec, - vtkShader::ReplacementValue>::iterator rIter; - for (rIter = this->UserShaderReplacements.begin(); - rIter != this->UserShaderReplacements.end();) - { - if (rIter->first.ShaderType == shaderType) - { - this->UserShaderReplacements.erase(rIter++); - modified = true; - } - else - { - ++rIter; - } - } - if (modified) - { - this->ShaderReplacementTime.Modified(); - Modified(); - } -} - -//----------------------------------------------------------------------------- -void vtkOpenGLShaderProperty::PrintSelf(ostream& os, vtkIndent indent) -{ - this->Superclass::PrintSelf(os, indent); -} diff --git a/Rendering/OpenGL2/vtkOpenGLShaderProperty.h b/Rendering/OpenGL2/vtkOpenGLShaderProperty.h deleted file mode 100644 index 39fe28b4a16..00000000000 --- a/Rendering/OpenGL2/vtkOpenGLShaderProperty.h +++ /dev/null @@ -1,148 +0,0 @@ -/*========================================================================= - - Program: Visualization Toolkit - Module: vtkShaderProperty.h - - Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen - All rights reserved. - See Copyright.txt or http://www.kitware.com/Copyright.htm for details. - - This software is distributed WITHOUT ANY WARRANTY; without even - the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR - PURPOSE. See the above copyright notice for more information. - -=========================================================================*/ -/** - * @class vtkOpenGLShaderProperty - * @brief represent GPU shader properties - * - * vtkOpenGLShaderProperty is used to hold user-defined modifications of a - * GPU shader program used in a mapper. - * - * @sa - * vtkShaderProperty vtkUniforms vtkOpenGLUniform - * - * @par Thanks: - * Developed by Simon Drouin (sdrouin2@bwh.harvard.edu) at Brigham and Women's Hospital. - * -*/ - -#ifndef vtkOpenGLShaderProperty_h -#define vtkOpenGLShaderProperty_h - -#include "vtkShaderProperty.h" -#include "vtkRenderingOpenGL2Module.h" // For export macro -#include "vtkShader.h" // For methods (shader types) -#include <map> // used for ivar - -class vtkOpenGLUniforms; - -class VTKRENDERINGOPENGL2_EXPORT vtkOpenGLShaderProperty : public vtkShaderProperty -{ -public: - vtkTypeMacro(vtkOpenGLShaderProperty,vtkShaderProperty); - void PrintSelf(ostream& os, vtkIndent indent) override; - - /** - * Construct object with no shader replacements - */ - static vtkOpenGLShaderProperty *New(); - - /** - * Assign one property to another. - */ - void DeepCopy(vtkOpenGLShaderProperty *p); - - /** - * @brief GetShaderMTime returns the last time a modification - * was made that affected the code of the shader (either code - * replacement was changed or one or more uniform variables were - * added or removed. This timestamp can be used by mappers to - * determine if the shader must be recompiled. Simply changing - * the value of an existing uniform variable doesn't affect this - * timestamp as it doesn't change the shader code. - * @return timestamp of the last modificatio - */ - vtkMTimeType GetShaderMTime(); - - virtual void AddVertexShaderReplacement( - const std::string& originalValue, - bool replaceFirst, // do this replacement before the default - const std::string& replacementValue, - bool replaceAll) override; - virtual void AddFragmentShaderReplacement( - const std::string& originalValue, - bool replaceFirst, // do this replacement before the default - const std::string& replacementValue, - bool replaceAll) override; - virtual void AddGeometryShaderReplacement( - const std::string& originalValue, - bool replaceFirst, // do this replacement before the default - const std::string& replacementValue, - bool replaceAll) override; - - virtual int GetNumberOfShaderReplacements() override; - virtual std::string GetNthShaderReplacementTypeAsString( vtkIdType index ) override; - virtual void GetNthShaderReplacement( - vtkIdType index, - std::string & name, - bool & replaceFirst, - std::string & replacementValue, - bool & replaceAll ) override; - - virtual void ClearVertexShaderReplacement( - const std::string& originalValue, - bool replaceFirst) override; - virtual void ClearFragmentShaderReplacement( - const std::string& originalValue, - bool replaceFirst) override; - virtual void ClearGeometryShaderReplacement( - const std::string& originalValue, - bool replaceFirst) override; - virtual void ClearAllVertexShaderReplacements() override; - virtual void ClearAllFragmentShaderReplacements() override; - virtual void ClearAllGeometryShaderReplacements() override; - virtual void ClearAllShaderReplacements() override; - - //@{ - /** - * This function enables you to apply your own substitutions - * to the shader creation process. The shader code in this class - * is created by applying a bunch of string replacements to a - * shader template. Using this function you can apply your - * own string replacements to add features you desire. - */ - void AddShaderReplacement( - vtkShader::Type shaderType, // vertex, fragment, etc - const std::string& originalValue, - bool replaceFirst, // do this replacement before the default - const std::string& replacementValue, - bool replaceAll); - void ClearShaderReplacement( - vtkShader::Type shaderType, // vertex, fragment, etc - const std::string& originalValue, - bool replaceFirst); - void ClearAllShaderReplacements(vtkShader::Type shaderType); - //@} - - /** - * @brief GetAllShaderReplacements returns all user-specified shader - * replacements. It is provided for iteration purpuses only (const) - * and is mainly used by mappers when building the shaders. - * @return const reference to internal map holding all replacements - */ - typedef std::map<const vtkShader::ReplacementSpec, vtkShader::ReplacementValue> ReplacementMap; - const ReplacementMap & GetAllShaderReplacements() { return this->UserShaderReplacements; } - -protected: - vtkOpenGLShaderProperty(); - ~vtkOpenGLShaderProperty() override; - - ReplacementMap UserShaderReplacements; - -private: - vtkOpenGLShaderProperty(const vtkOpenGLShaderProperty&) = delete; - void operator=(const vtkOpenGLShaderProperty&) = delete; -}; - -#endif diff --git a/Rendering/OpenGL2/vtkOpenGLSkybox.cxx b/Rendering/OpenGL2/vtkOpenGLSkybox.cxx index 8d13564afde..ee2b7f0aaee 100644 --- a/Rendering/OpenGL2/vtkOpenGLSkybox.cxx +++ b/Rendering/OpenGL2/vtkOpenGLSkybox.cxx @@ -27,7 +27,6 @@ #include "vtkOpenGLError.h" #include "vtkRenderWindow.h" #include "vtkShaderProgram.h" -#include "vtkOpenGLShaderProperty.h" #include "vtkTexture.h" #include <cmath> @@ -58,8 +57,7 @@ vtkOpenGLSkybox::vtkOpenGLSkybox() this->SetMapper(this->CubeMapper); this->OpenGLActor->SetMapper(this->CubeMapper); - vtkOpenGLShaderProperty * sp = vtkOpenGLShaderProperty::SafeDownCast(this->OpenGLActor->GetShaderProperty()); - sp->AddShaderReplacement( + this->CubeMapper->AddShaderReplacement( vtkShader::Vertex, "//VTK::PositionVC::Dec", // replace true, // before the standard replacements @@ -67,7 +65,7 @@ vtkOpenGLSkybox::vtkOpenGLSkybox() "out vec3 TexCoords;\n", false // only do it once ); - sp->AddShaderReplacement( + this->CubeMapper->AddShaderReplacement( vtkShader::Vertex, "//VTK::PositionVC::Impl", // replace true, // before the standard replacements @@ -118,11 +116,10 @@ void vtkOpenGLSkybox::Render(vtkRenderer *ren, vtkMapper *mapper) if (this->LastProjection != this->Projection) { - vtkOpenGLShaderProperty * sp = vtkOpenGLShaderProperty::SafeDownCast(this->OpenGLActor->GetShaderProperty()); if (this->Projection == vtkSkybox::Cube) { // Replace VTK fragment shader - sp->SetFragmentShaderCode( + this->CubeMapper->SetFragmentShaderCode( "//VTK::System::Dec\n" // always start with this line "//VTK::Output::Dec\n" // always have this line in your FS "in vec3 TexCoords;\n" @@ -136,7 +133,7 @@ void vtkOpenGLSkybox::Render(vtkRenderer *ren, vtkMapper *mapper) if (this->Projection == vtkSkybox::Sphere) { // Replace VTK fragment shader - sp->SetFragmentShaderCode( + this->CubeMapper->SetFragmentShaderCode( "//VTK::System::Dec\n" // always start with this line "//VTK::Output::Dec\n" // always have this line in your FS "in vec3 TexCoords;\n" @@ -158,7 +155,7 @@ void vtkOpenGLSkybox::Render(vtkRenderer *ren, vtkMapper *mapper) if (this->Projection == vtkSkybox::StereoSphere) { // Replace VTK fragment shader - sp->SetFragmentShaderCode( + this->CubeMapper->SetFragmentShaderCode( "//VTK::System::Dec\n" // always start with this line "//VTK::Output::Dec\n" // always have this line in your FS "in vec3 TexCoords;\n" @@ -181,7 +178,7 @@ void vtkOpenGLSkybox::Render(vtkRenderer *ren, vtkMapper *mapper) if (this->Projection == vtkSkybox::Floor) { // Replace VTK fragment shader - sp->SetFragmentShaderCode( + this->CubeMapper->SetFragmentShaderCode( "//VTK::System::Dec\n" // always start with this line "//VTK::Output::Dec\n" // always have this line in your FS "in vec3 TexCoords;\n" diff --git a/Rendering/OpenGL2/vtkOpenGLUniforms.cxx b/Rendering/OpenGL2/vtkOpenGLUniforms.cxx deleted file mode 100644 index 3987ef19725..00000000000 --- a/Rendering/OpenGL2/vtkOpenGLUniforms.cxx +++ /dev/null @@ -1,1069 +0,0 @@ -#include "vtkOpenGLUniforms.h" -#include "vtkObjectFactory.h" -#include "vtkShaderProgram.h" -#include "vtkMatrix3x3.h" -#include "vtkMatrix4x4.h" -#include <vector> -#include <cstring> -#include <cmath> - -vtkStandardNewMacro(vtkOpenGLUniforms) - -// temporary patch: Some Android builds don't have std::to_string -#include <sstream> -namespace patch -{ - template < typename T > - std::string to_string( const T& n ) - { - std::ostringstream stm ; - stm << n ; - return stm.str() ; - } -} - -class Uniform -{ -public: - virtual ~Uniform() {} - virtual int GetScalarType() = 0; - virtual vtkIdType GetNumberOfTuples() = 0; - virtual vtkUniforms::TupleType GetTupleType() = 0; - virtual int GetNumberOfComponents() = 0; - virtual std::string GetGlslDeclaration( const char * name ) = 0; - virtual bool SetUniform( const char * name, vtkShaderProgram * ) = 0; - virtual bool GetGenericValue( std::vector<int> & value ) = 0; - virtual bool GetGenericValue( std::vector<float> & value ) = 0; - virtual void PrintSelf( const char * name, ostream&, vtkIndent ) = 0; -protected: - -}; - -template< typename scalarType, vtkUniforms::TupleType tupleType, int nbComponents > -class UniformT : public Uniform -{ -public: - UniformT() { } - vtkIdType GetNumberOfTuples() override { return static_cast<vtkIdType>(values.size()/nbComponents); } - vtkUniforms::TupleType GetTupleType() override { return tupleType; } - int GetNumberOfComponents() override { return nbComponents; } - void SetValue( scalarType value ) { values.resize(1); values[0] = value; } - void SetValue( const std::vector<scalarType> & value ) { values = value; } - const std::vector<scalarType> & GetValue() { return values; } - void PrintSelf( const char * name, ostream &os, vtkIndent indent ) override - { - os << indent << name << ":"; - vtkIndent inNext = indent.GetNextIndent(); - for( int i = 0; i < GetNumberOfTuples(); ++i ) - { - PrintOne( i, os, inNext); - if( i < GetNumberOfTuples() - 1 ) - { - os << std::endl << inNext; - } - } - } -protected: - void PrintOne( vtkIdType tupleIndex, ostream & os, vtkIndent indent ) - { - int nbComp = GetNumberOfComponents(); - if( GetTupleType() == vtkUniforms::TupleTypeScalar ) - { - os << values[tupleIndex] << std::endl; - } - else if( GetTupleType() == vtkUniforms::TupleTypeVector ) - { - PrintVec( tupleIndex * nbComp, nbComp, os ); - } - else - { - int w = static_cast<int>(sqrt(nbComp)); - for( int i = 0; i < w; ++i ) - { - PrintVec((tupleIndex+i)*nbComp, nbComp, os ); - if( i < w - 1 ) - { - os << std::endl << indent; - } - } - } - } - void PrintVec( vtkIdType index, int nbComp, ostream & os ) - { - os << "[ "; - for( int i = 0; i < nbComp - 1 ; ++i ) - { - os << values[index+i] << ", "; - } - os << values[index+nbComp-1] << " ]"; - } - std::vector<scalarType> values; -}; - -template<vtkUniforms::TupleType tupleType, int nbComponents> -class Uniformi : public UniformT<int,tupleType,nbComponents> -{ -public: - int GetScalarType() override { return VTK_INT; } - bool GetGenericValue( std::vector<int> & value ) override { value = this->values; return true; } - bool GetGenericValue( std::vector<float> & ) override { return false; } -}; - -template<vtkUniforms::TupleType tupleType, int nbComponents> -class Uniformf : public UniformT<float,tupleType,nbComponents> -{ -public: - int GetScalarType() override { return VTK_FLOAT; } - bool GetGenericValue( std::vector<int> & ) override { return false; } - bool GetGenericValue( std::vector<float> & value ) override { value = this->values; return false; } -}; - -class UniformScalari : public Uniformi<vtkUniforms::TupleTypeScalar,1> -{ -public: - std::string GetGlslDeclaration( const char * name ) override - { - return std::string("uniform int ") + name + ";\n"; - } - bool SetUniform( const char * name, vtkShaderProgram * p ) override - { - return p->SetUniformi( name, values[0] ); - } - void PrintSelf( const char * name, ostream &os, vtkIndent indent ) override - { - os << indent << name << ": " << values[0] << std::endl; - } -}; - -class UniformScalarf : public Uniformf<vtkUniforms::TupleTypeScalar,1> -{ -public: - std::string GetGlslDeclaration( const char * name ) override - { - return std::string("uniform float ") + name + ";\n"; - } - bool SetUniform( const char * name, vtkShaderProgram * p ) override - { - return p->SetUniformf( name, values[0] ); - } -}; - -class UniformVec2i : public Uniformi<vtkUniforms::TupleTypeVector,2> -{ -public: - std::string GetGlslDeclaration( const char * name ) override - { - return std::string("uniform ivec2 ") + name + ";\n"; - } - bool SetUniform( const char * name, vtkShaderProgram * p ) override - { - return p->SetUniform2i( name, values.data() ); - } -}; - -class UniformVec2f : public Uniformf<vtkUniforms::TupleTypeVector,2> -{ -public: - std::string GetGlslDeclaration( const char * name ) override - { - return std::string("uniform vec2 ") + name + ";\n"; - } - bool SetUniform( const char * name, vtkShaderProgram * p ) override - { - return p->SetUniform2f( name, values.data() ); - } -}; - -class UniformVec3f : public Uniformf<vtkUniforms::TupleTypeVector,3> -{ -public: - std::string GetGlslDeclaration( const char * name ) override - { - return std::string("uniform vec3 ") + name + ";\n"; - } - bool SetUniform( const char * name, vtkShaderProgram * p ) override - { - return p->SetUniform3f( name, values.data() ); - } -}; - -class UniformVec4f : public Uniformf<vtkUniforms::TupleTypeVector,4> -{ -public: - std::string GetGlslDeclaration( const char * name ) override - { - return std::string("uniform vec4 ") + name + ";\n"; - } - bool SetUniform( const char * name, vtkShaderProgram * p ) override - { - return p->SetUniform4f( name, values.data() ); - } -}; - -class UniformMat3f : public Uniformf<vtkUniforms::TupleTypeMatrix,9> -{ -public: - std::string GetGlslDeclaration( const char * name ) override - { - return std::string("uniform mat3 ") + name + ";\n"; - } - bool SetUniform( const char * name, vtkShaderProgram * p ) override - { - return p->SetUniformMatrix3x3( name, values.data() ); - } -}; - -class UniformMat4f : public Uniformf<vtkUniforms::TupleTypeMatrix,16> -{ -public: - std::string GetGlslDeclaration( const char * name ) override - { - return std::string("uniform mat4 ") + name + ";\n"; - } - bool SetUniform( const char * name, vtkShaderProgram * p ) override - { - return p->SetUniformMatrix4x4( name, values.data() ); - } -}; - -class Uniform1iv : public Uniformi<vtkUniforms::TupleTypeScalar,1> -{ -public: - std::string GetGlslDeclaration( const char * name ) override - { - return std::string("uniform int ") + name + "[" + patch::to_string(GetNumberOfTuples()) + "];\n"; - } - bool SetUniform( const char * name, vtkShaderProgram * p ) override - { - return p->SetUniform1iv( name, GetNumberOfTuples(), values.data() ); - } -}; - -class Uniform1fv : public Uniformf<vtkUniforms::TupleTypeScalar,1> -{ -public: - std::string GetGlslDeclaration( const char * name ) override - { - return std::string("uniform float ") + name + "[" + patch::to_string(GetNumberOfTuples()) + "];\n"; - } - bool SetUniform( const char * name, vtkShaderProgram * p ) override - { - return p->SetUniform1fv( name, GetNumberOfTuples(), values.data() ); - } -}; - -class Uniform2fv : public Uniformf<vtkUniforms::TupleTypeVector,2> -{ -public: - std::string GetGlslDeclaration( const char * name ) override - { - return std::string("uniform vec2 ") + name + "[" + patch::to_string(GetNumberOfTuples()) + "];\n"; - } - bool SetUniform( const char * name, vtkShaderProgram * p ) override - { - return p->SetUniform2fv( name, GetNumberOfTuples(), reinterpret_cast<const float(*)[2]>(values.data()) ); - } -}; - -class Uniform3fv : public Uniformf<vtkUniforms::TupleTypeVector,3> -{ -public: - std::string GetGlslDeclaration( const char * name ) override - { - return std::string("uniform vec3 ") + name + "[" + patch::to_string(GetNumberOfTuples()) + "];\n"; - } - bool SetUniform( const char * name, vtkShaderProgram * p ) override - { - return p->SetUniform3fv( name, GetNumberOfTuples(), reinterpret_cast<const float(*)[3]>(values.data()) ); - } -}; - -class Uniform4fv : public Uniformf<vtkUniforms::TupleTypeVector,4> -{ -public: - std::string GetGlslDeclaration( const char * name ) override - { - return std::string("uniform vec4 ") + name + "[" + patch::to_string(GetNumberOfTuples()) + "];\n"; - } - bool SetUniform( const char * name, vtkShaderProgram * p ) override - { - return p->SetUniform4fv( name, GetNumberOfTuples(), reinterpret_cast<const float(*)[4]>(values.data()) ); - } -}; - -class UniformMat4fv : public Uniformf<vtkUniforms::TupleTypeMatrix,16> -{ -public: - std::string GetGlslDeclaration( const char * name ) override - { - return std::string("uniform mat4 ") + name + "[" + patch::to_string(GetNumberOfTuples()) + "];\n"; - } - bool SetUniform( const char * name, vtkShaderProgram * p ) override - { - return p->SetUniformMatrix4x4v( name, GetNumberOfTuples(), values.data() ); - } -}; - - -class vtkUniformInternals : public vtkObject -{ - -public: - - static vtkUniformInternals *New(); - vtkTypeMacro(vtkUniformInternals, vtkObject); - - void SetParent( vtkOpenGLUniforms * uni ) { this->Parent = uni; } - - vtkMTimeType GetUniformListMTime() { return this->UniformListMTime; } - - void PrintSelf(ostream& os, vtkIndent indent) override - { - for( auto & uni : Uniforms ) - { - uni.second->PrintSelf( uni.first.c_str(), os, indent ); - } - } - - void RemoveUniform(const char *name) - { - UniformMap::iterator it = Uniforms.find( name ); - if( it != Uniforms.end() ) - { - delete (*it).second; - Uniforms.erase( it ); - } - UniformListMTime.Modified(); - Parent->Modified(); - } - - void RemoveAllUniforms() - { - for( auto & uni : Uniforms ) - { - delete uni.second; - } - Uniforms.clear(); - UniformListMTime.Modified(); - Parent->Modified(); - } - - template< class dataT, class uniformT > - void SetUniformValue( const char *name, const dataT & value ) - { - UniformMap::iterator it = Uniforms.find( name ); - uniformT * uni = nullptr; - if( it != Uniforms.end() ) - { - uni = dynamic_cast<uniformT*>(it->second); - if(uni) - { - uni->SetValue( value ); - Parent->Modified(); - } - else - { - vtkErrorMacro( << "Trying to set the value of uniform variable of a different type: " << name << endl ); - } - } - else - { - uni = new uniformT; - uni->SetValue(value); - Uniforms[std::string(name)] = uni; - UniformListMTime.Modified(); - Parent->Modified(); - } - } - - template< class dataT, class uniformT > - const std::vector<dataT> * GetUniformValue(const char * name) - { - UniformMap::iterator it = Uniforms.find(name); - if (it != Uniforms.end()) - { - uniformT * uni = dynamic_cast<uniformT*>(it->second); - if (uni) - { - return &(uni->GetValue()); - } - } - return nullptr; - } - - template< typename scalarT, typename uniformT > - bool GetUniformValue (const char *name, scalarT * v) - { - const std::vector<scalarT> * val = GetUniformValue<scalarT,uniformT>(name); - if( val ) - { - std::copy(val->begin(), val->end(), v); - return true; - } - return false; - } - - template< typename scalarT, typename uniformT > - bool GetUniformValue (const char *name, std::vector<scalarT>& v) - { - const std::vector<scalarT> * val = GetUniformValue<scalarT,uniformT>(name); - if( val ) - { - v = *val; - return true; - } - return false; - } - - // In this case, get the uniform value based only on the scalar type - // without any guaranty on the tuple type and number of components - // This function is used by generic getters. - template< typename scalarT > - bool GetGenericUniformValue( const char * name, std::vector<scalarT> & v ) - { - UniformMap::iterator it = Uniforms.find(name); - if (it != Uniforms.end()) - { - Uniform * uni = it->second; - uni->GetGenericValue( v ); - return true; - } - return false; - } - - std::string GetDeclarations() - { - std::string res; - for( auto & uni : Uniforms ) - { - res += uni.second->GetGlslDeclaration( uni.first.c_str() ); - } - return res; - } - - bool SetUniforms( vtkShaderProgram * p ) - { - bool res = true; - for( auto & uni : Uniforms ) - { - bool r = uni.second->SetUniform( uni.first.c_str(), p ); - if( !r ) - { - vtkErrorMacro( << "vtkOpenGLUniform: couldn't set custom uniform variable " << uni.first << endl ); - } - res &= r; - } - return res; - } - - int GetNumberOfUniforms() - { - return static_cast<vtkIdType>(this->Uniforms.size()); - } - - const char* GetNthUniformName(vtkIdType uniformIndex) - { - if (uniformIndex >= this->GetNumberOfUniforms()) - { - return nullptr; - } - - UniformMap::iterator it = this->Uniforms.begin(); - std::advance(it, uniformIndex); - return it->first.c_str(); - } - - Uniform * GetUniform( const char *name ) - { - UniformMap::iterator it = Uniforms.find(name); - if (it != Uniforms.end()) - { - return it->second; - } - return nullptr; - } - - int GetUniformScalarType(const char *name) - { - Uniform * uniform = this->GetUniform(name); - if (uniform == nullptr) - { - return VTK_VOID; - } - return uniform->GetScalarType(); - } - - vtkUniforms::TupleType GetUniformTupleType(const char *name) - { - Uniform *uniform = this->GetUniform(name); - if (uniform == nullptr) - { - return vtkUniforms::TupleTypeInvalid; - } - return uniform->GetTupleType(); - } - - int GetNumberOfTuples(const char *name) - { - Uniform * uniform = this->GetUniform(name); - if (uniform == nullptr) - { - return 0; - } - return uniform->GetNumberOfTuples(); - } - - int GetUniformNumberOfComponents(const char *name) - { - Uniform * uniform = this->GetUniform(name); - if (uniform == nullptr) - { - return 0; - } - return uniform->GetNumberOfComponents(); - } - -protected: - - vtkUniformInternals() {} - ~vtkUniformInternals() override - { - RemoveAllUniforms(); - } - -private: - vtkUniformInternals(const vtkUniformInternals&) = delete; - void operator=(const vtkUniformInternals&) = delete; - - vtkTimeStamp UniformListMTime; - - vtkOpenGLUniforms * Parent; - - typedef std::map<std::string,Uniform*> UniformMap; - UniformMap Uniforms; -}; - -vtkStandardNewMacro(vtkUniformInternals); - -vtkOpenGLUniforms::vtkOpenGLUniforms() -{ - this->Internals = vtkUniformInternals::New(); - this->Internals->SetParent( this ); -} - -vtkOpenGLUniforms::~vtkOpenGLUniforms() -{ - this->Internals->Delete(); -} - -void vtkOpenGLUniforms::RemoveUniform(const char *name) -{ - this->Internals->RemoveUniform(name); -} - -void vtkOpenGLUniforms::RemoveAllUniforms() -{ - this->Internals->RemoveAllUniforms(); -} - -// Utility function to convert fixed size array to std::vector -template< typename T > -std::vector<T> arrayToVec( const T * in, int count ) -{ - return std::vector<T>( in, in + count ); -} - -//--------------------------------------------------------------------------------------- -// Generic Setters and Getters (useful for IO) -//--------------------------------------------------------------------------------------- - -void vtkOpenGLUniforms::SetUniform( const char * name, vtkUniforms::TupleType tt, int nbComponents, const std::vector<int> & value ) -{ - if( tt == vtkUniforms::TupleTypeScalar ) - { - if( value.size() == 1 ) - { - this->Internals->SetUniformValue<int,UniformScalari>(name, value[0]); - } - else if( value.size() > 1 ) - { - this->Internals->SetUniformValue<std::vector<int>,Uniform1iv>(name,value); - } - else - { - vtkErrorMacro(<<"Uniform type doesn't match input value."); - } - } - else if( tt == vtkUniforms::TupleTypeVector ) - { - if( nbComponents == 2 ) - { - if( value.size() == 2 ) - { - this->Internals->SetUniformValue<std::vector<int>,UniformVec2i>(name,value); - } - else - { - vtkErrorMacro(<<"Uniform type doesn't match input value.") - } - } - } - else - { - vtkErrorMacro(<<"Invalid tuple type"); - } -} - -void vtkOpenGLUniforms::SetUniform( const char * name, vtkUniforms::TupleType tt, int nbComponents, const std::vector<float> & value ) -{ - if( tt == vtkUniforms::TupleTypeScalar ) - { - if( value.size() == 1 ) - { - this->Internals->SetUniformValue<float,UniformScalarf>(name, value[0]); - } - else if( value.size() > 1 ) - { - this->Internals->SetUniformValue<std::vector<float>,Uniform1fv>(name,value); - } - else - { - vtkErrorMacro(<<"Uniform type doesn't match input value."); - } - } - else if( tt == vtkUniforms::TupleTypeVector ) - { - if( nbComponents == 2 ) - { - if( value.size() == 2 ) - { - this->Internals->SetUniformValue<std::vector<float>,UniformVec2f>(name,value); - } - else if( value.size() > 2 && value.size() % 2 == 0 ) - { - this->Internals->SetUniformValue<std::vector<float>,Uniform2fv>(name,value); - } - else - { - vtkErrorMacro(<<"Uniform type doesn't match input value."); - } - } - if( nbComponents == 3 ) - { - if( value.size() == 3 ) - { - this->Internals->SetUniformValue<std::vector<float>,UniformVec3f>(name,value); - } - else if( value.size() > 3 && value.size() % 3 == 0 ) - { - this->Internals->SetUniformValue<std::vector<float>,Uniform3fv>(name,value); - } - else - { - vtkErrorMacro(<<"Uniform type doesn't match input value."); - } - } - if( nbComponents == 4 ) - { - if( value.size() == 4 ) - { - this->Internals->SetUniformValue<std::vector<float>,UniformVec4f>(name,value); - } - else if( value.size() > 4 && value.size() % 4 == 0 ) - { - this->Internals->SetUniformValue<std::vector<float>,Uniform4fv>(name,value); - } - else - { - vtkErrorMacro(<<"Uniform type doesn't match input value."); - } - } - } - else if( tt == vtkUniforms::TupleTypeMatrix ) - { - if( nbComponents == 9 ) - { - if( value.size() == 9 ) - { - this->Internals->SetUniformValue<std::vector<float>,UniformMat3f>(name,value); - } - else - { - vtkErrorMacro(<<"Uniform type doesn't match input value."); - } - } - else if( nbComponents == 16 ) - { - if( value.size() == 16 ) - { - this->Internals->SetUniformValue<std::vector<float>,UniformMat4f>(name,value); - } - else if( value.size() > 16 && value.size() % 16 == 0 ) - { - this->Internals->SetUniformValue<std::vector<float>,UniformMat4fv>(name,value); - } - else - { - vtkErrorMacro(<<"Uniform type doesn't match input value."); - } - } - else - { - vtkErrorMacro(<<"Uniform type doesn't match input value."); - } - } - else - { - vtkErrorMacro(<<"Invalid tuple type"); - } -} - -bool vtkOpenGLUniforms::GetUniform( const char * name, std::vector<int> & value ) -{ - return this->Internals->GetGenericUniformValue( name, value ); -} - -bool vtkOpenGLUniforms::GetUniform( const char * name, std::vector<float> & value ) -{ - return this->Internals->GetGenericUniformValue( name, value ); -} - -//--------------------------------------------------------------------------------------- -// Basic setters -//--------------------------------------------------------------------------------------- - -void vtkOpenGLUniforms::SetUniformi (const char *name, int v) -{ - this->Internals->SetUniformValue<int,UniformScalari>(name, v); -} - -void vtkOpenGLUniforms::SetUniformf (const char *name, float v) -{ - this->Internals->SetUniformValue<float,UniformScalarf>(name,v); -} - -void vtkOpenGLUniforms::SetUniform2i (const char *name, const int v[2]) -{ - std::vector<int> sv = arrayToVec(v,2); - this->Internals->SetUniformValue<std::vector<int>,UniformVec2i>(name,sv); -} - -void vtkOpenGLUniforms::SetUniform2f (const char *name, const float v[2]) -{ - std::vector<float> sv = arrayToVec(v,2); - this->Internals->SetUniformValue<std::vector<float>,UniformVec2f>(name,sv); -} - -void vtkOpenGLUniforms::SetUniform3f (const char *name, const float v[3]) -{ - std::vector<float> sv = arrayToVec(v,3); - this->Internals->SetUniformValue<std::vector<float>,UniformVec3f>(name,sv); -} - -void vtkOpenGLUniforms::SetUniform4f (const char *name, const float v[4]) -{ - std::vector<float> sv = arrayToVec(v,4); - this->Internals->SetUniformValue<std::vector<float>,UniformVec4f>(name,sv); -} - -void vtkOpenGLUniforms::SetUniformMatrix3x3 (const char *name, float *v) -{ - std::vector<float> sv = arrayToVec(v,9); - this->Internals->SetUniformValue<std::vector<float>,UniformMat3f>(name,sv); -} - -void vtkOpenGLUniforms::SetUniformMatrix4x4 (const char *name, float *v) -{ - std::vector<float> sv = arrayToVec(v,16); - this->Internals->SetUniformValue<std::vector<float>,UniformMat4f>(name,sv); -} - -void vtkOpenGLUniforms::SetUniform1iv (const char *name, const int count, const int *v) -{ - std::vector<int> sv = arrayToVec(v,count); - this->Internals->SetUniformValue<std::vector<int>,Uniform1iv>(name,sv); -} - -void vtkOpenGLUniforms::SetUniform1fv (const char *name, const int count, const float *v) -{ - std::vector<float> sv = arrayToVec(v,count); - this->Internals->SetUniformValue<std::vector<float>,Uniform1fv>(name,sv); -} - -void vtkOpenGLUniforms::SetUniform2fv (const char *name, const int count, const float(*v)[2]) -{ - std::vector<float> sv = arrayToVec(reinterpret_cast<const float*>(v),count*2); - this->Internals->SetUniformValue<std::vector<float>,Uniform2fv>(name,sv); -} - -void vtkOpenGLUniforms::SetUniform3fv (const char *name, const int count, const float(*v)[3]) -{ - std::vector<float> sv = arrayToVec(reinterpret_cast<const float*>(v),count*3); - this->Internals->SetUniformValue<std::vector<float>,Uniform3fv>(name,sv); -} - -void vtkOpenGLUniforms::SetUniform4fv (const char *name, const int count, const float(*v)[4]) -{ - std::vector<float> sv = arrayToVec(reinterpret_cast<const float*>(v),count*4); - this->Internals->SetUniformValue<std::vector<float>,Uniform4fv>(name,sv); -} - -void vtkOpenGLUniforms::SetUniformMatrix4x4v (const char *name, const int count, float *v) -{ - std::vector<float> sv = arrayToVec( v, count * 16 ); - this->Internals->SetUniformValue<std::vector<float>,UniformMat4fv>(name,sv); -} - -//--------------------------------------------------------------------------------------- -// Convenience setters (data undergoes conversion) -//--------------------------------------------------------------------------------------- - -void vtkOpenGLUniforms::SetUniform3f (const char *name, const double v[3]) -{ - std::vector<float> sv; - sv.reserve(3); - for( int i = 0; i < 3; ++i ) - { - sv.push_back( static_cast<float>(v[i]) ); - } - this->Internals->SetUniformValue<std::vector<float>,UniformVec3f>(name,sv); -} - -void vtkOpenGLUniforms::SetUniform3uc (const char *name, const unsigned char v[3]) -{ - std::vector<float> sv( {v[0] / 255.0f, v[1] / 255.0f, v[2] / 255.0f} ); - this->Internals->SetUniformValue<std::vector<float>,UniformVec3f>(name,sv); -} - -void vtkOpenGLUniforms::SetUniform4uc (const char *name, const unsigned char v[4]) -{ - std::vector<float> sv( {v[0] / 255.0f, v[1] / 255.0f, v[2] / 255.0f, v[3] / 255.0f} ); - this->Internals->SetUniformValue<std::vector<float>,UniformVec4f>(name,sv); -} - -void vtkOpenGLUniforms::SetUniformMatrix (const char *name, vtkMatrix3x3 *v) -{ - std::vector<float> sv; - sv.reserve(9); - for( int i = 0; i < 3; ++i ) - { - for( int j = 0; j < 3; ++j ) - { - sv.push_back( static_cast<float>( v->GetElement( i, j) ) ); - } - } - this->Internals->SetUniformValue<std::vector<float>,UniformMat3f>(name,sv); -} - -void vtkOpenGLUniforms::SetUniformMatrix (const char *name, vtkMatrix4x4 *v) -{ - std::vector<float> sv; - sv.reserve(16); - for( int i = 0; i < 4; ++i ) - { - for( int j = 0; j < 4; ++j ) - { - sv.push_back( static_cast<float>( v->GetElement( i, j) ) ); - } - } - this->Internals->SetUniformValue<std::vector<float>,UniformMat4f>(name,sv); -} - -//--------------------------------------------------------------------------------------- -// Type specific getters -//--------------------------------------------------------------------------------------- - -bool vtkOpenGLUniforms::GetUniformi (const char *name, int& v) -{ - return this->Internals->GetUniformValue<int,UniformScalari>(name, reinterpret_cast<int*>(&v) ); -} - -bool vtkOpenGLUniforms::GetUniformf (const char *name, float& v) -{ - return this->Internals->GetUniformValue<float,UniformScalarf>(name, reinterpret_cast<float*>(&v) ); -} - -bool vtkOpenGLUniforms::GetUniform2i (const char *name, int v[2]) -{ - return this->Internals->GetUniformValue<int,UniformVec2i>(name, reinterpret_cast<int*>(v) ); -} - -bool vtkOpenGLUniforms::GetUniform2f (const char *name, float v[2]) -{ - return this->Internals->GetUniformValue<float,UniformVec2f>(name, reinterpret_cast<float*>(v) ); -} - -bool vtkOpenGLUniforms::GetUniform3f (const char *name, float v[3]) -{ - return this->Internals->GetUniformValue<float,UniformVec3f>(name, reinterpret_cast<float*>(v) ); -} - -bool vtkOpenGLUniforms::GetUniform4f (const char *name, float v[4]) -{ - return this->Internals->GetUniformValue<float,UniformVec4f>(name, reinterpret_cast<float*>(v) ); -} - -bool vtkOpenGLUniforms::GetUniformMatrix3x3 (const char *name, float *v) -{ - return this->Internals->GetUniformValue<float,UniformMat3f>(name, v); -} - -bool vtkOpenGLUniforms::GetUniformMatrix4x4 (const char *name, float *v) -{ - return this->Internals->GetUniformValue<float,UniformMat4f>(name, v); -} - -bool vtkOpenGLUniforms::GetUniform1iv (const char *name, std::vector<int>& v) -{ - return this->Internals->GetUniformValue<int,UniformScalari>(name, v); -} - -bool vtkOpenGLUniforms::GetUniform1fv (const char *name, std::vector<float>& v) -{ - return this->Internals->GetUniformValue<float,UniformScalarf>(name, v); -} - -bool vtkOpenGLUniforms::GetUniform2fv (const char *name, std::vector<float>& v) -{ - return this->Internals->GetUniformValue<float,UniformVec2f>(name, v); -} - -bool vtkOpenGLUniforms::GetUniform3fv (const char *name, std::vector<float>& v) -{ - return this->Internals->GetUniformValue<float,UniformVec3f>(name, v); -} - -bool vtkOpenGLUniforms::GetUniform4fv (const char *name, std::vector<float>& v) -{ - return this->Internals->GetUniformValue<float,UniformVec4f>(name, v); -} - -bool vtkOpenGLUniforms::GetUniformMatrix4x4v (const char *name, std::vector<float>& v) -{ - return this->Internals->GetUniformValue<float,UniformMat4f>(name, v); -} - -//--------------------------------------------------------------------------------------- -// Convenience getters (with type conversion) -//--------------------------------------------------------------------------------------- - -bool vtkOpenGLUniforms::GetUniform3f(const char *name, double v[3]) -{ - const std::vector<float> * val = this->Internals->GetUniformValue<float,UniformVec3f>(name); - if( val ) - { - v[0] = static_cast<double>( (*val)[0] ); - v[1] = static_cast<double>( (*val)[1] ); - v[2] = static_cast<double>( (*val)[2] ); - return true; - } - return false; -} - -bool vtkOpenGLUniforms::GetUniform3uc (const char *name, unsigned char v[3]) -{ - const std::vector<float> * val = this->Internals->GetUniformValue<float,UniformVec3f>(name); - if( val ) - { - v[0] = static_cast<unsigned char>( std::round( (*val)[0] * 255.0f ) ); - v[1] = static_cast<unsigned char>( std::round( (*val)[1] * 255.0f ) ); - v[2] = static_cast<unsigned char>( std::round( (*val)[2] * 255.0f ) ); - return true; - } - return false; -} - -bool vtkOpenGLUniforms::GetUniform4uc (const char *name, unsigned char v[4]) -{ - const std::vector<float> * val = this->Internals->GetUniformValue<float,UniformVec4f>(name); - if( val ) - { - v[0] = static_cast<unsigned char>( std::round( (*val)[0] * 255.0f ) ); - v[1] = static_cast<unsigned char>( std::round( (*val)[1] * 255.0f ) ); - v[2] = static_cast<unsigned char>( std::round( (*val)[2] * 255.0f ) ); - v[3] = static_cast<unsigned char>( std::round( (*val)[3] * 255.0f ) ); - return true; - } - return false; -} - -bool vtkOpenGLUniforms::GetUniformMatrix (const char *name, vtkMatrix3x3 *v) -{ - const std::vector<float> * val = this->Internals->GetUniformValue<float,UniformMat3f>(name); - if( val ) - { - for( unsigned i = 0; i < 3; ++i ) - { - for( unsigned j = 0; j < 3; ++j ) - { - v->SetElement( static_cast<int>(i), static_cast<int>(j), static_cast<double>((*val)[3*i+j]) ); - } - } - return true; - } - return false; -} - -bool vtkOpenGLUniforms::GetUniformMatrix (const char *name, vtkMatrix4x4 *v) -{ - const std::vector<float> * val = this->Internals->GetUniformValue<float,UniformMat4f>(name); - if( val ) - { - for( unsigned i = 0; i < 4; ++i ) - { - for( unsigned j = 0; j < 4; ++j ) - { - v->SetElement( static_cast<int>(i), static_cast<int>(j), static_cast<double>((*val)[4*i+j]) ); - } - } - return true; - } - return false; -} - -std::string vtkOpenGLUniforms::GetDeclarations() -{ return this->Internals->GetDeclarations(); } - -bool vtkOpenGLUniforms::SetUniforms( vtkShaderProgram * p ) -{ return this->Internals->SetUniforms(p); } - -vtkMTimeType vtkOpenGLUniforms::GetUniformListMTime() -{ - return this->Internals->GetUniformListMTime(); -} - -int vtkOpenGLUniforms::GetNumberOfUniforms() -{ - return this->Internals->GetNumberOfUniforms(); -} - -const char* vtkOpenGLUniforms::GetNthUniformName(vtkIdType uniformIndex) -{ - return this->Internals->GetNthUniformName(uniformIndex); -} - -int vtkOpenGLUniforms::GetUniformScalarType(const char *name) -{ - return this->Internals->GetUniformScalarType(name); -} - -vtkUniforms::TupleType vtkOpenGLUniforms::GetUniformTupleType(const char *name) -{ - return this->Internals->GetUniformTupleType(name); -} - -int vtkOpenGLUniforms::GetUniformNumberOfComponents(const char *name) -{ - return this->Internals->GetUniformNumberOfComponents(name); -} - -int vtkOpenGLUniforms::GetUniformNumberOfTuples(const char *name) -{ - return this->Internals->GetNumberOfTuples(name); -} - -void vtkOpenGLUniforms::PrintSelf(ostream& os, vtkIndent indent) -{ - this->Superclass::PrintSelf(os,indent); - this->Internals->PrintSelf(os,indent); -} diff --git a/Rendering/OpenGL2/vtkOpenGLUniforms.h b/Rendering/OpenGL2/vtkOpenGLUniforms.h deleted file mode 100644 index fc74482788d..00000000000 --- a/Rendering/OpenGL2/vtkOpenGLUniforms.h +++ /dev/null @@ -1,159 +0,0 @@ -/*========================================================================= - - Program: Visualization Toolkit - - Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen - All rights reserved. - See Copyright.txt or http://www.kitware.com/Copyright.htm for details. - - This software is distributed WITHOUT ANY WARRANTY; without even - the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR - 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. - * - * The basic types of GLSL uniform variables supported by the class are the following: int, float, - * vec2i, vec3, vec4, mat3, mat4, int[], float[], vec2i[], vec3[], vec4[], mat4[]. All other - * types supported by Set* functions undergo the same type conversions implemented in vtkShaderProgram. - * - * @par Thanks: - * Developed by Simon Drouin (sdrouin2@bwh.harvard.edu) at Brigham and Women's Hospital. -*/ - -#ifndef vtkOpenGLUniforms_h -#define vtkOpenGLUniforms_h - -#include "vtkUniforms.h" -#include "vtkRenderingOpenGL2Module.h" // For export macro -#include <string> // For member functions - -class vtkUniformInternals; -class vtkShaderProgram; - -class VTKRENDERINGOPENGL2_EXPORT vtkOpenGLUniforms : public vtkUniforms -{ -public: - static vtkOpenGLUniforms *New(); - vtkTypeMacro(vtkOpenGLUniforms, vtkUniforms); - void PrintSelf(ostream& os, vtkIndent indent) override; - - std::string GetDeclarations(); - bool SetUniforms( vtkShaderProgram * p ); - vtkMTimeType GetUniformListMTime() override; - - /** Remove uniform variable named @p name */ - void RemoveUniform(const char *name) override; - - /** Remove all uniform variables */ - void RemoveAllUniforms() override; - - /** Generic setters and getter. Set and Get the value of - * uniform variable @p name, with TupleType @p tt, number - * of components @p nbComponents and values stored in - * @p value. These functions simplify io of uniforms */ - void SetUniform( const char * name, vtkUniforms::TupleType tt, int nbComponents, const std::vector<int> & value ) override; - void SetUniform( const char * name, vtkUniforms::TupleType tt, int nbComponents, const std::vector<float> & value ) override; - bool GetUniform( const char * name, std::vector<int> & value ) override; - bool GetUniform( const char * name, std::vector<float> & value ) override; - - /** Set the @p name uniform value to @p v. */ - void SetUniformi(const char *name, int v) override; - void SetUniformf(const char *name, float v) override; - void SetUniform2i(const char *name, const int v[2]) override; - void SetUniform2f(const char *name, const float v[2]) override; - void SetUniform3f(const char *name, const float v[3]) override; - void SetUniform4f(const char *name, const float v[4]) override; - void SetUniformMatrix3x3(const char *name, float *v) override; - void SetUniformMatrix4x4(const char *name, float *v) override; - - /** Set the @p name uniform array to @p f with @p count elements */ - void SetUniform1iv(const char *name, const int count, const int *f) override; - void SetUniform1fv(const char *name, const int count, const float *f) override; - void SetUniform2fv(const char *name, const int count, const float (*f)[2]) override; - void SetUniform3fv(const char *name, const int count, const float (*f)[3]) override; - void SetUniform4fv(const char *name, const int count, const float (*f)[4]) override; - void SetUniformMatrix4x4v(const char *name, const int count, float *v) override; - - /** Set the @p name uniform to @p v. - * The following are convenience functions and do not reflect - * the way the data is stored and sent to OpenGL. Data is - * converted to match one of the basic supported types */ - void SetUniform3f(const char *name, const double v[3]) override; - void SetUniform3uc(const char *name, const unsigned char v[3]) override; // maybe remove - void SetUniform4uc(const char *name, const unsigned char v[4]) override; // maybe remove - void SetUniformMatrix(const char *name, vtkMatrix3x3 *v) override; - void SetUniformMatrix(const char *name, vtkMatrix4x4 *v) override; - - /** Get the @p name uniform value. Returns true on success. */ - bool GetUniformi(const char *name, int& v) override; - bool GetUniformf(const char *name, float& v) override; - bool GetUniform2i(const char *name, int v[2]) override; - bool GetUniform2f(const char *name, float v[2]) override; - bool GetUniform3f(const char *name, float v[3]) override; - bool GetUniform4f(const char *name, float v[4]) override; - bool GetUniformMatrix3x3(const char *name, float *v) override; - bool GetUniformMatrix4x4(const char *name, float *v) override; - - /** Get the @p name uniform to @p v. - * The following are convenience functions and do not reflect - * the way the data is stored and sent to OpenGL. Data is - * converted from one of the basic supported types */ - bool GetUniform3f(const char *name, double v[3]) override; - bool GetUniform3uc(const char *name, unsigned char v[3]) override; - bool GetUniform4uc(const char *name, unsigned char v[4]) override; - bool GetUniformMatrix(const char *name, vtkMatrix3x3 *v) override; - bool GetUniformMatrix(const char *name, vtkMatrix4x4 *v) override; - - /** Get the @p name uniform vector to @p f with. - Buffer must be pre-allocated based on vector size provided by - */ - bool GetUniform1iv(const char *name, std::vector<int>& f) override; - bool GetUniform1fv(const char *name, std::vector<float>& f) override; - bool GetUniform2fv(const char *name, std::vector<float>& f) override; - bool GetUniform3fv(const char *name, std::vector<float>& f) override; - bool GetUniform4fv(const char *name, std::vector<float>& f) override; - bool GetUniformMatrix4x4v(const char *name, std::vector<float>& f) override; - - /** Get number of all uniforms stored in this class */ - int GetNumberOfUniforms() override; - - /** Get number of all uniforms stored in this class. - Valid range is between 0 and GetNumberOfUniforms() - 1.*/ - const char* GetNthUniformName(vtkIdType uniformIndex) override; - - /** Get type of scalars stored in uniform @p name */ - virtual int GetUniformScalarType(const char *name) override; - - /** Get the tuple type stored in uniform @p name. This can be a scalar, - * a vector of a matrix. */ - TupleType GetUniformTupleType(const char *name) override; - - /** Get the number of components stored in each tuple of uniform @p name. - * for example, a uniform with tuples of matrix type and 9 components - * contains 3x3 matrices */ - int GetUniformNumberOfComponents(const char *name) override; - - /** Get length of a uniform @p name that contains a variable-size vector. - Size includes number of tuples. For example, 3fv returns 3 x number of triplets. */ - int GetUniformNumberOfTuples(const char *name) override; - -protected: - - vtkOpenGLUniforms(); - ~vtkOpenGLUniforms() override; - -private: - vtkOpenGLUniforms(const vtkOpenGLUniforms&) = delete; - void operator=(const vtkOpenGLUniforms&) = delete; - - vtkUniformInternals * Internals; -}; - -#endif diff --git a/Rendering/Volume/Testing/Python/TestGPURayCastUserShader.py b/Rendering/Volume/Testing/Python/TestGPURayCastUserShader.py index b628a98674b..346a3f1c52c 100755 --- a/Rendering/Volume/Testing/Python/TestGPURayCastUserShader.py +++ b/Rendering/Volume/Testing/Python/TestGPURayCastUserShader.py @@ -65,8 +65,8 @@ mapper.SetInputConnection(reader.GetOutputPort()) mapper.SetUseJittering(1) # Modify the shader to color based on the depth of the translucent voxel -shaderProperty = vtk.vtkShaderProperty() -shaderProperty.AddFragmentShaderReplacement( +mapper.AddShaderReplacement( + vtk.vtkShader.Fragment, # Replace in the fragment shader "//VTK::Base::Dec", # Source string to replace True, # before the standard replacements "//VTK::Base::Dec" # We still want the default @@ -74,7 +74,8 @@ shaderProperty.AddFragmentShaderReplacement( "\n vec3 l_opaqueFragPos;", False # only do it once i.e. only replace the first match ) -shaderProperty.AddFragmentShaderReplacement( +mapper.AddShaderReplacement( + vtk.vtkShader.Fragment, "//VTK::Base::Init", True, "//VTK::Base::Init\n" @@ -82,7 +83,8 @@ shaderProperty.AddFragmentShaderReplacement( "\n l_opaqueFragPos = vec3(0.0);", False ) -shaderProperty.AddFragmentShaderReplacement( +mapper.AddShaderReplacement( + vtk.vtkShader.Fragment, "//VTK::Base::Impl", True, "//VTK::Base::Impl" @@ -93,7 +95,8 @@ shaderProperty.AddFragmentShaderReplacement( "\n }", False ) -shaderProperty.AddFragmentShaderReplacement( +mapper.AddShaderReplacement( + vtk.vtkShader.Fragment, "//VTK::RenderToImage::Exit", True, "//VTK::RenderToImage::Exit" @@ -115,7 +118,6 @@ shaderProperty.AddFragmentShaderReplacement( ) volume = vtk.vtkVolume() -volume.SetShaderProperty(shaderProperty) volume.SetMapper(mapper) volume.SetProperty(volumeProperty) diff --git a/Rendering/VolumeOpenGL2/CMakeLists.txt b/Rendering/VolumeOpenGL2/CMakeLists.txt index dce18631a7a..3e365214656 100644 --- a/Rendering/VolumeOpenGL2/CMakeLists.txt +++ b/Rendering/VolumeOpenGL2/CMakeLists.txt @@ -4,6 +4,7 @@ set(classes vtkOpenGLProjectedTetrahedraMapper vtkOpenGLRayCastImageDisplayHelper vtkOpenGLTransferFunction2D + vtkOpenGLUniforms vtkOpenGLVolumeGradientOpacityTable vtkOpenGLVolumeOpacityTable vtkOpenGLVolumeRGBTable diff --git a/Rendering/VolumeOpenGL2/Testing/Cxx/TestGPURayCastUserShader.cxx b/Rendering/VolumeOpenGL2/Testing/Cxx/TestGPURayCastUserShader.cxx index 77ed119eea3..12dc418adc1 100644 --- a/Rendering/VolumeOpenGL2/Testing/Cxx/TestGPURayCastUserShader.cxx +++ b/Rendering/VolumeOpenGL2/Testing/Cxx/TestGPURayCastUserShader.cxx @@ -34,7 +34,6 @@ #include "vtkTestUtilities.h" #include "vtkVolume.h" #include "vtkVolumeProperty.h" -#include "vtkShaderProperty.h" int TestGPURayCastUserShader(int argc, char* argv[]) { @@ -77,10 +76,9 @@ int TestGPURayCastUserShader(int argc, char* argv[]) mapper->SetInputConnection(reader->GetOutputPort()); mapper->SetUseJittering(1); - vtkNew<vtkShaderProperty> shaderProperty; - // Modify the shader to color based on the depth of the translucent voxel - shaderProperty->AddFragmentShaderReplacement( + mapper->AddShaderReplacement( + vtkShader::Fragment, // Replace in the fragment shader "//VTK::Base::Dec", // Source string to replace true, // before the standard replacements "//VTK::Base::Dec" // We still want the default @@ -88,14 +86,16 @@ int TestGPURayCastUserShader(int argc, char* argv[]) "\n vec3 l_opaqueFragPos;", false // only do it once i.e. only replace the first match ); - shaderProperty->AddFragmentShaderReplacement( + mapper->AddShaderReplacement( + vtkShader::Fragment, "//VTK::Base::Init", true, "//VTK::Base::Init\n" "\n l_updateDepth = true;" "\n l_opaqueFragPos = vec3(0.0);", false); - shaderProperty->AddFragmentShaderReplacement( + mapper->AddShaderReplacement( + vtkShader::Fragment, "//VTK::Base::Impl", true, "//VTK::Base::Impl" @@ -105,7 +105,8 @@ int TestGPURayCastUserShader(int argc, char* argv[]) "\n l_updateDepth = false;" "\n }", false); - shaderProperty->AddFragmentShaderReplacement( + mapper->AddShaderReplacement( + vtkShader::Fragment, "//VTK::RenderToImage::Exit", true, "//VTK::RenderToImage::Exit" @@ -124,19 +125,20 @@ int TestGPURayCastUserShader(int argc, char* argv[]) "\n (gl_DepthRange.far + gl_DepthRange.near)), 1.0);" "\n }", false); - shaderProperty->AddFragmentShaderReplacement( // add dummy replacement + mapper->AddShaderReplacement( + vtkShader::Fragment, // dummy replacement to test clear method "//VTK::ComputeGradient::Dec", true, "VTK::ComputeGradient::Dec", false); - shaderProperty->ClearFragmentShaderReplacement( // clear dummy replacement + mapper->ClearShaderReplacement( + vtkShader::Fragment, // clear the dummy replacement "//VTK::ComputeGradient::Dec", true); vtkNew<vtkVolume> volume; volume->SetMapper(mapper.GetPointer()); volume->SetProperty(volumeProperty.GetPointer()); - volume->SetShaderProperty(shaderProperty.GetPointer()); vtkNew<vtkRenderWindow> renWin; renWin->SetMultiSamples(0); diff --git a/Rendering/VolumeOpenGL2/Testing/Cxx/TestGPURayCastUserShader2.cxx b/Rendering/VolumeOpenGL2/Testing/Cxx/TestGPURayCastUserShader2.cxx index f18b789e638..6e70a88db53 100644 --- a/Rendering/VolumeOpenGL2/Testing/Cxx/TestGPURayCastUserShader2.cxx +++ b/Rendering/VolumeOpenGL2/Testing/Cxx/TestGPURayCastUserShader2.cxx @@ -34,7 +34,6 @@ #include "vtkTestUtilities.h" #include "vtkVolume.h" #include "vtkVolumeProperty.h" -#include "vtkShaderProperty.h" #include <TestGPURayCastUserShader2_FS.h> @@ -85,27 +84,24 @@ int TestGPURayCastUserShader2(int argc, char* argv[]) vtkNew<vtkOpenGLGPUVolumeRayCastMapper> mapper; mapper->SetInputConnection(reader->GetOutputPort()); mapper->SetUseJittering(0); + // Clear all custom shader tag replacements + // The following code is mainly for regression testing as we do not have any + // custom shader replacements. + mapper->ClearAllShaderReplacements(vtkShader::Vertex); + mapper->ClearAllShaderReplacements(vtkShader::Fragment); + mapper->ClearAllShaderReplacements(vtkShader::Geometry); + mapper->ClearAllShaderReplacements(); // Tell the mapper to use the min and max of the color function nodes as the // lookup table range instead of the volume scalar range. mapper->SetColorRangeType(vtkGPUVolumeRayCastMapper::NATIVE); - vtkNew<vtkShaderProperty> shaderProperty; - // Clear all custom shader tag replacements - // The following code is mainly for regression testing as we do not have any - // custom shader replacements. - shaderProperty->ClearAllVertexShaderReplacements(); - shaderProperty->ClearAllFragmentShaderReplacements(); - shaderProperty->ClearAllGeometryShaderReplacements(); - shaderProperty->ClearAllShaderReplacements(); - // Modify the shader to color based on the depth of the translucent voxel - shaderProperty->SetFragmentShaderCode(TestGPURayCastUserShader2_FS); + mapper->SetFragmentShaderCode(TestGPURayCastUserShader2_FS); vtkNew<vtkVolume> volume; volume->SetMapper(mapper.GetPointer()); volume->SetProperty(volumeProperty.GetPointer()); - volume->SetShaderProperty(shaderProperty); vtkNew<vtkRenderWindow> renWin; renWin->SetMultiSamples(0); diff --git a/Rendering/VolumeOpenGL2/vtkOpenGLGPUVolumeRayCastMapper.cxx b/Rendering/VolumeOpenGL2/vtkOpenGLGPUVolumeRayCastMapper.cxx index 3ebe241190e..80e01f0c287 100644 --- a/Rendering/VolumeOpenGL2/vtkOpenGLGPUVolumeRayCastMapper.cxx +++ b/Rendering/VolumeOpenGL2/vtkOpenGLGPUVolumeRayCastMapper.cxx @@ -59,7 +59,6 @@ #include <vtkOpenGLRenderWindow.h> #include "vtkOpenGLResourceFreeCallback.h" #include <vtkOpenGLShaderCache.h> -#include <vtkOpenGLShaderProperty.h> #include "vtkOpenGLState.h" #include <vtkOpenGLVertexArrayObject.h> #include "vtkOpenGLUniforms.h" @@ -376,7 +375,7 @@ public: void FinishRendering(int numComponents); - inline bool ShaderRebuildNeeded(vtkCamera* cam, vtkVolume* vol, vtkMTimeType renderPassTime); + inline bool ShaderRebuildNeeded(vtkCamera* cam, vtkMTimeType renderPassTime); bool VolumePropertyChanged = true; //@{ @@ -469,6 +468,7 @@ public: vtkSmartPointer<vtkOpenGLVolumeRGBTable> Mask1RGBTable; vtkSmartPointer<vtkOpenGLVolumeRGBTable> Mask2RGBTable; + vtkTimeStamp ShaderReplacementTime; vtkTimeStamp ShaderBuildTime; vtkNew<vtkMatrix4x4> InverseProjectionMat; @@ -2169,6 +2169,8 @@ vtkOpenGLGPUVolumeRayCastMapper::vtkOpenGLGPUVolumeRayCastMapper() this->Impl = new vtkInternal(this); this->ReductionFactor = 1.0; this->CurrentPass = RenderPass; + this->VertexShaderCode = nullptr; + this->FragmentShaderCode = nullptr; this->ResourceCallback = new vtkOpenGLResourceFreeCallback<vtkOpenGLGPUVolumeRayCastMapper>( @@ -2192,6 +2194,8 @@ vtkOpenGLGPUVolumeRayCastMapper::~vtkOpenGLGPUVolumeRayCastMapper() this->Impl = nullptr; this->AssembledInputs.clear(); + this->SetVertexShaderCode(nullptr); + this->SetFragmentShaderCode(nullptr); } //---------------------------------------------------------------------------- @@ -2292,16 +2296,108 @@ void vtkOpenGLGPUVolumeRayCastMapper::ReleaseGraphicsResources( this->Impl->ReleaseResourcesTime.Modified(); } +//----------------------------------------------------------------------------- +void vtkOpenGLGPUVolumeRayCastMapper::AddShaderReplacement( + vtkShader::Type shaderType, // vertex, fragment, etc + const std::string& originalValue, + bool replaceFirst, // do this replacement before the default + const std::string& replacementValue, + bool replaceAll) +{ + vtkShader::ReplacementSpec spec; + spec.ShaderType = shaderType; + spec.OriginalValue = originalValue; + spec.ReplaceFirst = replaceFirst; + + vtkShader::ReplacementValue values; + values.Replacement = replacementValue; + values.ReplaceAll = replaceAll; + + this->UserShaderReplacements[spec] = values; + this->Impl->ShaderReplacementTime.Modified(); +} + +//----------------------------------------------------------------------------- +void vtkOpenGLGPUVolumeRayCastMapper::ClearShaderReplacement( + vtkShader::Type shaderType, // vertex, fragment, etc + const std::string& originalValue, + bool replaceFirst) +{ + vtkShader::ReplacementSpec spec; + spec.ShaderType = shaderType; + spec.OriginalValue = originalValue; + spec.ReplaceFirst = replaceFirst; + + typedef std::map<const vtkShader::ReplacementSpec, + vtkShader::ReplacementValue>::iterator RIter; + RIter found = this->UserShaderReplacements.find(spec); + if (found != this->UserShaderReplacements.end()) + { + this->UserShaderReplacements.erase(found); + this->Impl->ShaderReplacementTime.Modified(); + } +} + +//----------------------------------------------------------------------------- +void vtkOpenGLGPUVolumeRayCastMapper::ClearAllShaderReplacements( + vtkShader::Type shaderType) +{ + bool modified = false; + // First clear all shader code + if ((shaderType == vtkShader::Vertex) && this->VertexShaderCode) + { + this->SetVertexShaderCode(nullptr); + modified = true; + } + else if ((shaderType == vtkShader::Fragment) && this->FragmentShaderCode) + { + this->SetFragmentShaderCode(nullptr); + modified = true; + } + + // Now clear custom tag replacements + std::map<const vtkShader::ReplacementSpec, + vtkShader::ReplacementValue>::iterator rIter; + for (rIter = this->UserShaderReplacements.begin(); + rIter != this->UserShaderReplacements.end();) + { + if (rIter->first.ShaderType == shaderType) + { + this->UserShaderReplacements.erase(rIter++); + modified = true; + } + else + { + ++rIter; + } + } + if (modified) + { + this->Impl->ShaderReplacementTime.Modified(); + } +} + +//----------------------------------------------------------------------------- +void vtkOpenGLGPUVolumeRayCastMapper::ClearAllShaderReplacements() +{ + this->SetVertexShaderCode(nullptr); + this->SetFragmentShaderCode(nullptr); + if (!this->UserShaderReplacements.empty()) + { + this->UserShaderReplacements.clear(); + this->Impl->ShaderReplacementTime.Modified(); + } +} //----------------------------------------------------------------------------- void vtkOpenGLGPUVolumeRayCastMapper::GetShaderTemplate( - std::map<vtkShader::Type, vtkShader*>& shaders, vtkOpenGLShaderProperty * p) + std::map<vtkShader::Type, vtkShader*>& shaders) { if (shaders[vtkShader::Vertex]) { - if( p->HasVertexShaderCode()) + if (this->VertexShaderCode && strcmp(this->VertexShaderCode, "") != 0) { - shaders[vtkShader::Vertex]->SetSource(p->GetVertexShaderCode()); + shaders[vtkShader::Vertex]->SetSource(this->VertexShaderCode); } else { @@ -2311,9 +2407,9 @@ void vtkOpenGLGPUVolumeRayCastMapper::GetShaderTemplate( if (shaders[vtkShader::Fragment]) { - if(p->HasFragmentShaderCode()) + if (this->FragmentShaderCode && strcmp(this->FragmentShaderCode, "") != 0) { - shaders[vtkShader::Fragment]->SetSource(p->GetFragmentShaderCode()); + shaders[vtkShader::Fragment]->SetSource(this->FragmentShaderCode); } else { @@ -2329,25 +2425,17 @@ void vtkOpenGLGPUVolumeRayCastMapper::GetShaderTemplate( //----------------------------------------------------------------------------- void vtkOpenGLGPUVolumeRayCastMapper::ReplaceShaderCustomUniforms( - std::map<vtkShader::Type, vtkShader*>& shaders, vtkOpenGLShaderProperty * p ) + std::map<vtkShader::Type, vtkShader*>& shaders ) { vtkShader* vertexShader = shaders[vtkShader::Vertex]; - vtkOpenGLUniforms * vu = static_cast<vtkOpenGLUniforms*>(p->GetVertexCustomUniforms()); vtkShaderProgram::Substitute(vertexShader, "//VTK::CustomUniforms::Dec", - vu->GetDeclarations()); + this->VertexCustomUniforms->GetDeclarations()); vtkShader* fragmentShader = shaders[vtkShader::Fragment]; - vtkOpenGLUniforms * fu = static_cast<vtkOpenGLUniforms*>(p->GetFragmentCustomUniforms()); vtkShaderProgram::Substitute(fragmentShader, "//VTK::CustomUniforms::Dec", - fu->GetDeclarations()); - - vtkShader* geometryShader = shaders[vtkShader::Geometry]; - vtkOpenGLUniforms * gu = static_cast<vtkOpenGLUniforms*>(p->GetGeometryCustomUniforms()); - vtkShaderProgram::Substitute(geometryShader, - "//VTK::CustomUniforms::Dec", - gu->GetDeclarations()); + this->FragmentCustomUniforms->GetDeclarations()); } //----------------------------------------------------------------------------- @@ -2777,7 +2865,6 @@ void vtkOpenGLGPUVolumeRayCastMapper::ReplaceShaderValues( { // Every volume should have a property (cannot be nullptr); vtkVolumeProperty* volumeProperty = vol->GetProperty(); - auto shaderProperty = vtkOpenGLShaderProperty::SafeDownCast(vol->GetShaderProperty()); if (volumeProperty->GetShade()) { @@ -2820,7 +2907,7 @@ void vtkOpenGLGPUVolumeRayCastMapper::ReplaceShaderValues( // Custom uniform variables replacements //--------------------------------------------------------------------------- - this->ReplaceShaderCustomUniforms(shaders,shaderProperty); + this->ReplaceShaderCustomUniforms(shaders); // Base methods replacements //--------------------------------------------------------------------------- @@ -2886,40 +2973,44 @@ void vtkOpenGLGPUVolumeRayCastMapper::BuildShader(vtkRenderer* ren) geometryShader->SetType(vtkShader::Geometry); shaders[vtkShader::Geometry] = geometryShader; - auto vol = this->Impl->GetActiveVolume(); + this->GetShaderTemplate(shaders); - vtkOpenGLShaderProperty * sp = vtkOpenGLShaderProperty::SafeDownCast(vol->GetShaderProperty()); - this->GetShaderTemplate(shaders, sp); + typedef std::map<const vtkShader::ReplacementSpec, + vtkShader::ReplacementValue>::const_iterator RIter; // user specified pre replacements - vtkOpenGLShaderProperty::ReplacementMap repMap = sp->GetAllShaderReplacements(); - for ( auto i : repMap ) + for (RIter i = this->UserShaderReplacements.begin(); + i != this->UserShaderReplacements.end(); + ++i) { - if (i.first.ReplaceFirst) - { - std::string ssrc = shaders[i.first.ShaderType]->GetSource(); - vtkShaderProgram::Substitute(ssrc, - i.first.OriginalValue, - i.second.Replacement, - i.second.ReplaceAll); - shaders[i.first.ShaderType]->SetSource(ssrc); - } + if (i->first.ReplaceFirst) + { + std::string ssrc = shaders[i->first.ShaderType]->GetSource(); + vtkShaderProgram::Substitute(ssrc, + i->first.OriginalValue, + i->second.Replacement, + i->second.ReplaceAll); + shaders[i->first.ShaderType]->SetSource(ssrc); + } } + auto vol = this->Impl->GetActiveVolume(); auto numComp = this->AssembledInputs[0].Texture->GetLoadedScalars()->GetNumberOfComponents(); this->ReplaceShaderValues(shaders, ren, vol, numComp); // user specified post replacements - for ( auto i : repMap ) + for (RIter i = this->UserShaderReplacements.begin(); + i != this->UserShaderReplacements.end(); + ++i) { - if (!i.first.ReplaceFirst) + if (!i->first.ReplaceFirst) { - std::string ssrc = shaders[i.first.ShaderType]->GetSource(); + std::string ssrc = shaders[i->first.ShaderType]->GetSource(); vtkShaderProgram::Substitute(ssrc, - i.first.OriginalValue, - i.second.Replacement, - i.second.ReplaceAll); - shaders[i.first.ShaderType]->SetSource(ssrc); + i->first.OriginalValue, + i->second.Replacement, + i->second.ReplaceAll); + shaders[i->first.ShaderType]->SetSource(ssrc); } } @@ -3255,7 +3346,7 @@ void vtkOpenGLGPUVolumeRayCastMapper::GPURender(vtkRenderer* ren, } vtkVolumeStateRAII glState(renWin->GetState(), this->Impl->PreserveGLState); - if (this->Impl->ShaderRebuildNeeded(cam, vol, renderPassTime)) + if (this->Impl->ShaderRebuildNeeded(cam, renderPassTime)) { this->Impl->LastProjectionParallel = cam->GetParallelProjection(); this->BuildShader(ren); @@ -3268,18 +3359,17 @@ void vtkOpenGLGPUVolumeRayCastMapper::GPURender(vtkRenderer* ren, this->Impl->ShaderProgram); } - vtkOpenGLShaderProperty * shaderProperty = vtkOpenGLShaderProperty::SafeDownCast(vol->GetShaderProperty()); if (this->RenderToImage) { this->Impl->SetupRenderToTexture(ren); this->Impl->SetRenderToImageParameters(this->Impl->ShaderProgram); - this->DoGPURender(ren, cam, this->Impl->ShaderProgram,shaderProperty); + this->DoGPURender(ren, cam, this->Impl->ShaderProgram); this->Impl->ExitRenderToTexture(ren); } else { this->Impl->BeginImageSample(ren); - this->DoGPURender(ren, cam, this->Impl->ShaderProgram,shaderProperty); + this->DoGPURender(ren, cam, this->Impl->ShaderProgram); this->Impl->EndImageSample(ren); } @@ -3294,15 +3384,17 @@ void vtkOpenGLGPUVolumeRayCastMapper::GPURender(vtkRenderer* ren, //---------------------------------------------------------------------------- bool vtkOpenGLGPUVolumeRayCastMapper::vtkInternal::ShaderRebuildNeeded(vtkCamera* cam, - vtkVolume* vol, vtkMTimeType renderPassTime) + vtkMTimeType renderPassTime) { return (this->NeedToInitializeResources || this->VolumePropertyChanged || - vol->GetShaderProperty()->GetShaderMTime() > this->ShaderBuildTime.GetMTime() || 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 ); + renderPassTime > this->ShaderBuildTime || + this->ShaderReplacementTime > this->ShaderBuildTime); } //---------------------------------------------------------------------------- @@ -3313,7 +3405,6 @@ void vtkOpenGLGPUVolumeRayCastMapper::vtkInternal::RenderWithDepthPass( auto& input = this->Parent->AssembledInputs[0]; auto vol = input.Volume; auto volumeProperty = vol->GetProperty(); - auto shaderProperty = vtkOpenGLShaderProperty::SafeDownCast(vol->GetShaderProperty()); if (this->NeedToInitializeResources || volumeProperty->GetMTime() > this->DepthPassSetupTime.GetMTime() || @@ -3321,7 +3412,7 @@ void vtkOpenGLGPUVolumeRayCastMapper::vtkInternal::RenderWithDepthPass( cam->GetParallelProjection() != this->LastProjectionParallel || this->SelectionStateTime.GetMTime() > this->ShaderBuildTime.GetMTime() || renderPassTime > this->ShaderBuildTime || - shaderProperty->GetShaderMTime() > this->ShaderBuildTime) + this->ShaderReplacementTime > this->ShaderBuildTime) { this->LastProjectionParallel = cam->GetParallelProjection(); @@ -3377,7 +3468,7 @@ void vtkOpenGLGPUVolumeRayCastMapper::vtkInternal::RenderWithDepthPass( this->DPDepthBufferTextureObject->Activate(); this->ShaderProgram->SetUniformi("in_depthPassSampler", this->DPDepthBufferTextureObject->GetTextureUnit()); - this->Parent->DoGPURender(ren, cam, this->ShaderProgram,shaderProperty); + this->Parent->DoGPURender(ren, cam, this->ShaderProgram); this->DPDepthBufferTextureObject->Deactivate(); if (this->IsPicking) @@ -3780,8 +3871,7 @@ void vtkOpenGLGPUVolumeRayCastMapper::vtkInternal::FinishRendering( //---------------------------------------------------------------------------- void vtkOpenGLGPUVolumeRayCastMapper::DoGPURender(vtkRenderer* ren, vtkOpenGLCamera* cam, - vtkShaderProgram* prog, - vtkOpenGLShaderProperty* shaderProperty) + vtkShaderProgram* prog) { if (!prog) { @@ -3789,12 +3879,8 @@ void vtkOpenGLGPUVolumeRayCastMapper::DoGPURender(vtkRenderer* ren, } // Upload the value of user-defined uniforms in the program - auto vu = static_cast<vtkOpenGLUniforms*>(shaderProperty->GetVertexCustomUniforms()); - vu->SetUniforms( prog ); - auto fu = static_cast<vtkOpenGLUniforms*>(shaderProperty->GetFragmentCustomUniforms()); - fu->SetUniforms( prog ); - auto gu = static_cast<vtkOpenGLUniforms*>(shaderProperty->GetGeometryCustomUniforms()); - gu->SetUniforms( prog ); + this->VertexCustomUniforms->SetUniforms( prog ); + this->FragmentCustomUniforms->SetUniforms( prog ); this->SetShaderParametersRenderPass(); if (!this->Impl->MultiVolume) diff --git a/Rendering/VolumeOpenGL2/vtkOpenGLGPUVolumeRayCastMapper.h b/Rendering/VolumeOpenGL2/vtkOpenGLGPUVolumeRayCastMapper.h index a9202db1a99..dcd903101c7 100644 --- a/Rendering/VolumeOpenGL2/vtkOpenGLGPUVolumeRayCastMapper.h +++ b/Rendering/VolumeOpenGL2/vtkOpenGLGPUVolumeRayCastMapper.h @@ -80,7 +80,7 @@ class vtkTextureObject; class vtkVolume; class vtkVolumeInputHelper; class vtkVolumeTexture; -class vtkOpenGLShaderProperty; +class vtkOpenGLUniforms; class VTKRENDERINGVOLUMEOPENGL2_EXPORT vtkOpenGLGPUVolumeRayCastMapper : public vtkGPUVolumeRayCastMapper @@ -153,6 +153,50 @@ public: */ bool PreLoadData(vtkRenderer* ren, vtkVolume* vol); + //@{ + /** + * This function enables you to apply your own substitutions + * to the shader creation process. The shader code in this class + * is created by applying a bunch of string replacements to a + * shader template. Using this function you can apply your + * own string replacements to add features you desire. + */ + void AddShaderReplacement( + vtkShader::Type shaderType, // vertex, fragment, etc + const std::string& originalValue, + bool replaceFirst, // do this replacement before the default + const std::string& replacementValue, + bool replaceAll); + void ClearShaderReplacement( + vtkShader::Type shaderType, // vertex, fragment, etc + const std::string& originalValue, + bool replaceFirst); + void ClearAllShaderReplacements(vtkShader::Type shaderType); + void ClearAllShaderReplacements(); + //@} + + //@{ + /** + * Allow the program to set the shader codes used directly + * instead of using the built in templates. Be aware, if + * set, this template will be used for all cases, + * primitive types, picking etc. + */ + vtkSetStringMacro(VertexShaderCode); + vtkGetStringMacro(VertexShaderCode); + vtkSetStringMacro(FragmentShaderCode); + vtkGetStringMacro(FragmentShaderCode); + //@} + + //@{ + /** + * The Uniforms object allows to set custom uniform variables + * that are used in replacement shader code. + */ + vtkGetObjectMacro(FragmentCustomUniforms,vtkOpenGLUniforms); + vtkGetObjectMacro(VertexCustomUniforms,vtkOpenGLUniforms); + //@} + // Description: // Delete OpenGL objects. // \post done: this->OpenGLObjectsCreated==0 @@ -199,8 +243,7 @@ protected: // Method that performs the actual rendering given a volume and a shader void DoGPURender(vtkRenderer* ren, vtkOpenGLCamera* cam, - vtkShaderProgram* shaderProgram, - vtkOpenGLShaderProperty* shaderProperty); + vtkShaderProgram* shaderProgram); // Description: // Update the reduction factor of the render viewport (this->ReductionFactor) @@ -238,7 +281,7 @@ protected: /** * Create the basic shader template strings before substitutions */ - void GetShaderTemplate(std::map<vtkShader::Type, vtkShader*>& shaders, vtkOpenGLShaderProperty * p); + void GetShaderTemplate(std::map<vtkShader::Type, vtkShader*>& shaders); /** * Perform string replacements on the shader templates @@ -251,7 +294,7 @@ protected: * ReplaceShaderValues. */ void ReplaceShaderCustomUniforms( - std::map<vtkShader::Type, vtkShader*>& shaders, vtkOpenGLShaderProperty * p ); + 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, @@ -288,6 +331,13 @@ protected: double ReductionFactor; int CurrentPass; + char *VertexShaderCode; + char *FragmentShaderCode; + std::map<const vtkShader::ReplacementSpec, vtkShader::ReplacementValue> + UserShaderReplacements; + + vtkNew<vtkOpenGLUniforms> FragmentCustomUniforms; + vtkNew<vtkOpenGLUniforms> VertexCustomUniforms; public: using VolumeInput = vtkVolumeInputHelper; diff --git a/Rendering/VolumeOpenGL2/vtkOpenGLUniforms.cxx b/Rendering/VolumeOpenGL2/vtkOpenGLUniforms.cxx new file mode 100644 index 00000000000..42cfc56e908 --- /dev/null +++ b/Rendering/VolumeOpenGL2/vtkOpenGLUniforms.cxx @@ -0,0 +1,593 @@ +#include "vtkOpenGLUniforms.h" +#include "vtkObjectFactory.h" +#include "vtkShaderProgram.h" +#include "vtkMatrix3x3.h" +#include "vtkMatrix4x4.h" +#include <vector> +#include <cstring> + +vtkStandardNewMacro(vtkOpenGLUniforms) + +// temporary patch: Some Android builds don't have std::to_string +#include <sstream> +namespace patch +{ + template < typename T > + std::string to_string( const T& n ) + { + std::ostringstream stm ; + stm << n ; + return stm.str() ; + } +} + +class vtkCustomUniform +{ +public: + virtual ~vtkCustomUniform() {} + virtual std::string GetGlslDec( const std::string & ) { return std::string(); } + virtual bool SetUniform( const char *, vtkShaderProgram * ) { return false; } + virtual void PrintSelf( const char *, ostream&, vtkIndent ) {} +}; + +class vtkCustomUniformi : public vtkCustomUniform +{ +public: + vtkCustomUniformi( int val ) { value = val; } + void SetValue( int val ) { value = val; } + std::string GetGlslDec( const std::string & name ) override { return std::string("uniform int ") + name + ";\n"; } + bool SetUniform( const char * name, vtkShaderProgram * p ) override { return p->SetUniformi( name, value ); } + void PrintSelf( const char * name, ostream& os, vtkIndent indent ) override + { os << indent << name << ": " << value << endl;} +protected: + int value; +}; + +class vtkCustomUniformf : public vtkCustomUniform +{ +public: + vtkCustomUniformf( float val ) { value = val; } + void SetValue( float val ) { value = val; } + std::string GetGlslDec( const std::string & name ) override { return std::string("uniform float ") + name + ";\n"; } + bool SetUniform( const char * name, vtkShaderProgram * p ) override { return p->SetUniformf( name, value ); } + void PrintSelf( const char * name, ostream& os, vtkIndent indent ) override + { os << indent << name << ": " << value << endl;} +protected: + float value; +}; + +class vtkCustomUniform2i : public vtkCustomUniform +{ +public: + vtkCustomUniform2i( const int val[2] ) { value[0] = val[0]; value[1] = val[1]; } + void SetValue( const int val[2] ) { value[0] = val[0]; value[1] = val[1]; } + std::string GetGlslDec( const std::string & name ) override { return std::string("uniform ivec2 ") + name + ";\n"; } + bool SetUniform( const char * name, vtkShaderProgram * p ) override { return p->SetUniform2i( name, value ); } + void PrintSelf( const char * name, ostream& os, vtkIndent indent ) override + { os << indent << name << ": ( " << value[0] << ", " << value[1] << " )" << endl;} +protected: + int value[2]; +}; + +class vtkCustomUniform2f : public vtkCustomUniform +{ +public: + vtkCustomUniform2f( const float val[2] ) { value[0] = val[0]; value[1] = val[1]; } + void SetValue( const float val[2] ) { value[0] = val[0]; value[1] = val[1]; } + std::string GetGlslDec( const std::string & name ) override { return std::string("uniform vec2 ") + name + ";\n"; } + bool SetUniform( const char * name, vtkShaderProgram * p ) override { return p->SetUniform2f( name, value ); } + void PrintSelf( const char * name, ostream& os, vtkIndent indent ) override + { os << indent << name << ": ( " << value[0] << ", " << value[1] << " )" << endl;} +protected: + float value[2]; +}; + +class vtkCustomUniform3f : public vtkCustomUniform +{ +public: + vtkCustomUniform3f( const float val[3] ) { value[0] = val[0]; value[1] = val[1]; value[2] = val[2]; } + void SetValue( const float val[3] ) { value[0] = val[0]; value[1] = val[1]; value[2] = val[2]; } + std::string GetGlslDec( const std::string & name ) override { return std::string("uniform vec3 ") + name + ";\n"; } + bool SetUniform( const char * name, vtkShaderProgram * p ) override { return p->SetUniform3f( name, value ); } + void PrintSelf( const char * name, ostream& os, vtkIndent indent ) override + { os << indent << name << ": ( " << value[0] << ", " << value[1] << ", " << value[2] << " )" << endl;} +protected: + float value[3]; +}; + +class vtkCustomUniform3d : public vtkCustomUniform +{ +public: + vtkCustomUniform3d( const double val[3] ) { value[0] = val[0]; value[1] = val[1]; value[2] = val[2]; } + void SetValue( const double val[3] ) { value[0] = val[0]; value[1] = val[1]; value[2] = val[2]; } + std::string GetGlslDec( const std::string & name ) override { return std::string("uniform vec3 ") + name + ";\n"; } + bool SetUniform( const char * name, vtkShaderProgram * p ) override { return p->SetUniform3f( name, value ); } + void PrintSelf( const char * name, ostream& os, vtkIndent indent ) override + { os << indent << name << ": ( " << value[0] << ", " << value[1] << ", " << value[2] << " )" << endl;} +protected: + double value[3]; +}; + +class vtkCustomUniform4f : public vtkCustomUniform +{ +public: + vtkCustomUniform4f( const float val[4] ) { value[0] = val[0]; value[1] = val[1]; value[2] = val[2]; value[3] = val[3]; } + void SetValue( const float val[4] ) { value[0] = val[0]; value[1] = val[1]; value[2] = val[2]; value[3] = val[3]; } + std::string GetGlslDec( const std::string & name ) override { return std::string("uniform vec4 ") + name + ";\n"; } + bool SetUniform( const char * name, vtkShaderProgram * p ) override { return p->SetUniform4f( name, value ); } + void PrintSelf( const char * name, ostream& os, vtkIndent indent ) override + { os << indent << name << ": ( " << value[0] << ", " << value[1] << ", " << value[2] << ", " << value[3] << " )" << endl;} +protected: + float value[4]; +}; + +class vtkCustomUniform3uc : public vtkCustomUniform +{ +public: + vtkCustomUniform3uc( const unsigned char v[3] ) { value[0] = v[0]; value[1] = v[1]; value[2] = v[2]; } + void SetValue( const unsigned char v[3] ) { value[0] = v[0]; value[1] = v[1]; value[2] = v[2]; } + std::string GetGlslDec( const std::string & name ) override { return std::string("uniform vec3 ") + name + ";\n"; } + bool SetUniform( const char * name, vtkShaderProgram * p ) override { return p->SetUniform3uc( name, value ); } + void PrintSelf( const char * name, ostream& os, vtkIndent indent ) override + { os << indent << name << ": ( " << value[0] << ", " << value[1] << ", " << value[2] << " )" << endl;} +protected: + unsigned char value[3]; +}; + +class vtkCustomUniform4uc : public vtkCustomUniform +{ +public: + vtkCustomUniform4uc( const unsigned char v[4] ) { value[0] = v[0]; value[1] = v[1]; value[2] = v[2]; value[3] = v[3]; } + void SetValue( const unsigned char v[4] ) { value[0] = v[0]; value[1] = v[1]; value[2] = v[2]; value[3] = v[3]; } + std::string GetGlslDec( const std::string & name ) override { return std::string("uniform vec4 ") + name + ";\n"; } + bool SetUniform( const char * name, vtkShaderProgram * p ) override { return p->SetUniform4uc( name, value ); } + void PrintSelf( const char * name, ostream& os, vtkIndent indent ) override + { os << indent << name << ": ( " << value[0] << ", " << value[1] << ", " << value[2] << ", " << value[3] << " )" << endl;} +protected: + unsigned char value[4]; +}; + +class vtkCustomUniformVtkMatrix3x3 : public vtkCustomUniform +{ +public: + vtkCustomUniformVtkMatrix3x3( vtkMatrix3x3 * v ) { value = vtkMatrix3x3::New(); value->DeepCopy( v ); } + void SetValue( vtkMatrix3x3 * v ) { value = vtkMatrix3x3::New(); value->DeepCopy( v ); } + virtual ~vtkCustomUniformVtkMatrix3x3() override { value->Delete(); } + std::string GetGlslDec( const std::string & name ) override { return std::string("uniform mat3 ") + name + ";\n"; } + bool SetUniform( const char * name, vtkShaderProgram * p ) override { return p->SetUniformMatrix( name, value ); } + void PrintSelf( const char * name, ostream& os, vtkIndent indent ) override + { os << indent << name << endl; value->PrintSelf(os,indent); os << endl;} +protected: + vtkMatrix3x3 *value; +}; + +class vtkCustomUniformVtkMatrix4x4 : public vtkCustomUniform +{ +public: + vtkCustomUniformVtkMatrix4x4( vtkMatrix4x4 * v ) { value = vtkMatrix4x4::New(); value->DeepCopy( v ); } + void SetValue( vtkMatrix4x4 * v ) { value = vtkMatrix4x4::New(); value->DeepCopy( v ); } + virtual ~vtkCustomUniformVtkMatrix4x4() override { value->Delete(); } + std::string GetGlslDec( const std::string & name ) override { return std::string("uniform mat4 ") + name + ";\n"; } + bool SetUniform( const char * name, vtkShaderProgram * p ) override { return p->SetUniformMatrix( name, value ); } + void PrintSelf( const char * name, ostream& os, vtkIndent indent ) override + { os << indent << name << endl; value->PrintSelf(os,indent); os << endl;} +protected: + vtkMatrix4x4 *value; +}; + +class vtkCustomUniformMatrix3x3 : public vtkCustomUniform +{ +public: + vtkCustomUniformMatrix3x3( float * v ) { std::memcpy( value, v, sizeof value ); } + void SetValue( float * v ) { std::memcpy( value, v, sizeof value ); } + std::string GetGlslDec( const std::string & name ) override { return std::string("uniform mat3 ") + name + ";\n"; } + bool SetUniform( const char * name, vtkShaderProgram * p ) override { return p->SetUniformMatrix3x3( name, value ); } + void PrintSelf( const char * name, ostream& os, vtkIndent indent ) override + { + os << indent << name << ": " << endl; + for( int i = 0; i < 3; ++i ) + { + os << indent << "( "; + for( int j = 0; j < 3; ++j ) + { + os << value[i*3+j]; + if( j < 2 ) os << ", "; + } + os << " )" << endl; + } + os << endl; + } +protected: + float value[9]; +}; + +class vtkCustomUniformMatrix4x4 : public vtkCustomUniform +{ +public: + vtkCustomUniformMatrix4x4( float * v ) { std::memcpy( value, v, sizeof value ); } + void SetValue( float * v ) { std::memcpy( value, v, sizeof value ); } + std::string GetGlslDec( const std::string & name ) override { return std::string("uniform mat4 ") + name + ";\n"; } + bool SetUniform( const char * name, vtkShaderProgram * p ) override { return p->SetUniformMatrix4x4( name, value ); } + void PrintSelf( const char * name, ostream& os, vtkIndent indent ) override + { + os << indent << name << ": " << endl; + for( int i = 0; i < 4; ++i ) + { + os << indent << "( "; + for( int j = 0; j < 4; ++j ) + { + os << value[i*4+j]; + if( j < 3 ) os << ", "; + } + os << " )" << endl; + } + os << endl; + } +protected: + float value[16]; +}; + +class vtkCustomUniform1iv : public vtkCustomUniform +{ +public: + vtkCustomUniform1iv( const int count, const int *v ) { SetValue(count,v); } + void SetValue( const int count, const int *v ) { value.assign( v, v + count ); } + std::string GetGlslDec( const std::string & name ) override + { return std::string("uniform int ") + name + "[" + patch::to_string(value.size()) + "];\n"; } + bool SetUniform( const char * name, vtkShaderProgram * p ) override + { return p->SetUniform1iv( name, static_cast<int>(value.size()), value.data() ); } + void PrintSelf( const char * name, ostream& os, vtkIndent indent ) override + { os << indent << name << ": "; for( auto & v : value ) os << v << " "; os << endl; } +protected: + std::vector<int> value; +}; + +class vtkCustomUniform1fv : public vtkCustomUniform +{ +public: + vtkCustomUniform1fv( const int count, const float *v ) { SetValue(count,v); } + void SetValue( const int count, const float *v ) { value.assign( v, v + count ); } + std::string GetGlslDec( const std::string & name ) override + { return std::string("uniform float ") + name + "[" + patch::to_string(value.size()) + "];\n"; } + bool SetUniform( const char * name, vtkShaderProgram * p ) override + { return p->SetUniform1fv( name, static_cast<int>(value.size()), value.data() ); } + void PrintSelf( const char * name, ostream& os, vtkIndent indent ) override + { os << indent << name << ": "; for( auto & v : value ) os << v << " "; os << endl; } +protected: + std::vector<float> value; +}; + +class vtkCustomUniform2fv : public vtkCustomUniform +{ +public: + vtkCustomUniform2fv( const int count, const float (*v)[2] ) { SetValue(count,v); } + void SetValue( const int count, const float (*v)[2] ) + { value.assign( reinterpret_cast<const float*>(v), reinterpret_cast<const float*>(v + 2*count ) ); } + std::string GetGlslDec( const std::string & name ) override + { return std::string("uniform vec2 ") + name + "[" + patch::to_string(value.size()/2) + "];\n"; } + bool SetUniform( const char * name, vtkShaderProgram * p ) override + { return p->SetUniform2fv( name, static_cast<int>(value.size()/2), reinterpret_cast<const float(*)[2]>(value.data()) ); } + void PrintSelf( const char * name, ostream& os, vtkIndent indent ) override + { + os << indent << name << ": "; + for( std::vector<float>::size_type i = 0; i < value.size()/2; ++i ) + os << "( " << value[2*i] << ", " << value[2*i+1] << " ) "; + os << endl; + } +protected: + std::vector<float> value; +}; + +class vtkCustomUniform3fv : public vtkCustomUniform +{ +public: + vtkCustomUniform3fv( const int count, const float (*v)[3] ) { SetValue(count,v); } + void SetValue( const int count, const float (*v)[3] ) { value.assign( reinterpret_cast<const float*>(v), reinterpret_cast<const float*>(v + 3*count) ); } + std::string GetGlslDec( const std::string & name ) override + { return std::string("uniform vec3 ") + name + "[" + patch::to_string(value.size()/3) + "];\n"; } + bool SetUniform( const char * name, vtkShaderProgram * p ) override + { return p->SetUniform3fv( name, static_cast<int>(value.size()/3), reinterpret_cast<const float(*)[3]>(value.data()) ); } + void PrintSelf( const char * name, ostream& os, vtkIndent indent ) override + { + os << indent << name << ": "; + for( std::vector<float>::size_type i = 0; i < value.size()/3; ++i ) + os << "( " << value[3*i] << ", " << value[3*i+1] << ", " << value[3*i+2] << " ) "; + os << endl; + } +protected: + std::vector<float> value; +}; + +class vtkCustomUniform4fv : public vtkCustomUniform +{ +public: + vtkCustomUniform4fv( const int count, const float (*v)[4] ) { SetValue(count,v); } + void SetValue( const int count, const float (*v)[4] ) { value.assign( reinterpret_cast<const float*>(v), reinterpret_cast<const float*>(v + 4*count) ); } + std::string GetGlslDec( const std::string & name ) override + { return std::string("uniform vec4 ") + name + "[" + patch::to_string(value.size()/4) + "];\n"; } + bool SetUniform( const char * name, vtkShaderProgram * p ) override + { return p->SetUniform4fv( name, static_cast<int>(value.size()/4), reinterpret_cast<const float(*)[4]>(value.data()) ); } + void PrintSelf( const char * name, ostream& os, vtkIndent indent ) override + { + os << indent << name << ": "; + for( std::vector<float>::size_type i = 0; i < value.size()/4; ++i ) + os << "( " << value[4*i] << ", " << value[4*i+1] << ", " << value[4*i+2] << ", " << value[4*i+3] << " ) "; + os << endl; + } +protected: + std::vector<float> value; +}; + +class vtkCustomUniformMatrix4x4v : public vtkCustomUniform +{ +public: + vtkCustomUniformMatrix4x4v( const int count, const float *v ) { SetValue(count,v); } + void SetValue( const int count, const float *v ) { value.assign( v, v + 16*count ); } + std::string GetGlslDec( const std::string & name ) override + { return std::string("uniform mat4 ") + name + "[" + patch::to_string(value.size()/16) + "];\n"; } + bool SetUniform( const char * name, vtkShaderProgram * p ) override + { return p->SetUniformMatrix4x4v( name, static_cast<int>(value.size()/16), value.data() ); } + void PrintSelf( const char * name, ostream& os, vtkIndent indent ) override + { + os << indent << name << ": " << endl; + for( std::vector<float>::size_type mat = 0; mat < value.size()/16; ++mat ) + { + for( std::vector<float>::size_type i = 0; i < 4; ++i ) + { + os << indent << "( "; + for( std::vector<float>::size_type j = 0; j < 4; ++j ) + { + os << value[mat*16+i*4+j]; + if( j < 3 ) os << ", "; + } + os << " )" << endl; + } + os << endl << endl; + } + } +protected: + std::vector<float> value; +}; + +class vtkUniformInternals : public vtkObject +{ + +public: + + static vtkUniformInternals *New(); + vtkTypeMacro(vtkUniformInternals, vtkObject); + void PrintSelf(ostream& os, vtkIndent indent) override + { + for( auto & uni : Uniforms ) + { + uni.second->PrintSelf( uni.first.c_str(), os, indent ); + } + } + + template< class dataT, class uniformT > + void AddUniform( const char * name, dataT defaultValue ) + { + UniformMapT::iterator it = Uniforms.find( name ); + if( it != Uniforms.end() ) + { + vtkErrorMacro( << "vtkOpenGLUniform: overwriting existing uniform variable: " << name << endl ); + delete (*it).second; + Uniforms.erase( it ); + } + Uniforms[std::string(name)] = new uniformT(defaultValue); + Modified(); + } + + template< class dataT, class uniformT > + void AddUniform( const char * name, int count, dataT defaultValue ) + { + UniformMapT::iterator it = Uniforms.find( name ); + if( it != Uniforms.end() ) + { + vtkErrorMacro( << "vtkOpenGLUniform: overwriting existing uniform variable: " << name << endl ); + delete (*it).second; + Uniforms.erase( it ); + } + Uniforms[std::string(name)] = new uniformT(count, defaultValue); + Modified(); + } + + void RemoveUniform(const char *name) + { + UniformMapT::iterator it = Uniforms.find( name ); + if( it != Uniforms.end() ) + { + delete (*it).second; + Uniforms.erase( it ); + } + Modified(); + } + + void RemoveAllUniforms() + { + for( auto & uni : Uniforms ) + { + delete uni.second; + } + Uniforms.clear(); + Modified(); + } + + template< class dataT, class uniformT > + void SetUniform( const char *name, const dataT value ) + { + UniformMapT::iterator it = Uniforms.find( name ); + if( it != Uniforms.end() ) + { + uniformT * uni = dynamic_cast<uniformT*>(it->second); + uni->SetValue( value ); + } + else + { + vtkErrorMacro( << "Trying to set the value of undefined uniform variable: " << name << endl ); + } + } + + template< class dataT, class uniformT > + void SetUniform( const char *name, int count, const dataT value ) + { + UniformMapT::iterator it = Uniforms.find( name ); + if( it != Uniforms.end() ) + { + uniformT * uni = dynamic_cast<uniformT*>(it->second); + uni->SetValue( count, value ); + } + else + { + vtkErrorMacro( << "Trying to set the value of undefined uniform variable: " << name << endl ); + } + } + + std::string GetDeclarations() + { + std::string res; + for( auto & uni : Uniforms ) + { + res += uni.second->GetGlslDec( uni.first ); + } + return res; + } + + bool SetUniforms( vtkShaderProgram * p ) + { + bool res = true; + for( auto & uni : Uniforms ) + { + bool r = uni.second->SetUniform( uni.first.c_str(), p ); + if( !r ) + vtkErrorMacro( << "vtkOpenGLUniform: couldn't set custom uniform variable " << uni.first << endl ); + res &= r; + } + return res; + } + +protected: + + vtkUniformInternals() {} + ~vtkUniformInternals() override + { + RemoveAllUniforms(); + } + +private: + vtkUniformInternals(const vtkUniformInternals&) = delete; + void operator=(const vtkUniformInternals&) = delete; + + typedef std::map<std::string,vtkCustomUniform*> UniformMapT; + UniformMapT Uniforms; +}; + +vtkStandardNewMacro(vtkUniformInternals); + +vtkOpenGLUniforms::vtkOpenGLUniforms() +{ + this->Internals = vtkUniformInternals::New(); +} + +vtkOpenGLUniforms::~vtkOpenGLUniforms() +{ + this->Internals->Delete(); +} + +void vtkOpenGLUniforms::AddUniformi (const char *name, int defaultValue) +{ this->Internals->AddUniform<int,vtkCustomUniformi>( name, defaultValue ); } +void vtkOpenGLUniforms::AddUniformf(const char *name, float defaultValue) +{ this->Internals->AddUniform<float,vtkCustomUniformf>( name, defaultValue ); } +void vtkOpenGLUniforms::AddUniform2i(const char *name, const int defaultValue[2]) +{ this->Internals->AddUniform<const int[2],vtkCustomUniform2i>( name, defaultValue ); } +void vtkOpenGLUniforms::AddUniform2f(const char *name, const float defaultValue[2]) +{ this->Internals->AddUniform<const float[2],vtkCustomUniform2f>( name, defaultValue ); } +void vtkOpenGLUniforms::AddUniform3f(const char *name, const float defaultValue[3]) +{ this->Internals->AddUniform<const float[3],vtkCustomUniform3f>( name, defaultValue ); } +void vtkOpenGLUniforms::AddUniform3f(const char *name, const double defaultValue[3]) +{ this->Internals->AddUniform<const double[3],vtkCustomUniform3d>( name, defaultValue ); } +void vtkOpenGLUniforms::AddUniform4f(const char *name, const float defaultValue[4]) +{ this->Internals->AddUniform<const float[4],vtkCustomUniform4f>( name, defaultValue ); } +void vtkOpenGLUniforms::AddUniform3uc(const char *name, const unsigned char defaultValue[3]) // maybe remove +{ this->Internals->AddUniform<const unsigned char[3],vtkCustomUniform3uc>( name, defaultValue ); } +void vtkOpenGLUniforms::AddUniform4uc(const char *name, const unsigned char defaultValue[4]) // maybe remove +{ this->Internals->AddUniform<const unsigned char[4],vtkCustomUniform4uc>( name, defaultValue ); } +void vtkOpenGLUniforms::AddUniformMatrix(const char *name, vtkMatrix3x3 *defaultValue) +{ this->Internals->AddUniform<vtkMatrix3x3*,vtkCustomUniformVtkMatrix3x3>( name, defaultValue ); } +void vtkOpenGLUniforms::AddUniformMatrix(const char *name, vtkMatrix4x4 *defaultValue) +{ this->Internals->AddUniform<vtkMatrix4x4*,vtkCustomUniformVtkMatrix4x4>( name, defaultValue ); } +void vtkOpenGLUniforms::AddUniformMatrix3x3(const char *name, float *defaultValue) +{ this->Internals->AddUniform<float*,vtkCustomUniformMatrix3x3>( name, defaultValue ); } +void vtkOpenGLUniforms::AddUniformMatrix4x4(const char *name, float *defaultValue) +{ this->Internals->AddUniform<float*,vtkCustomUniformMatrix4x4>( name, defaultValue ); } +void vtkOpenGLUniforms::AddUniform1iv(const char *name, const int count, const int *f) +{ this->Internals->AddUniform<const int *,vtkCustomUniform1iv>(name,count,f); } +void vtkOpenGLUniforms::AddUniform1fv (const char *name, const int count, const float *f) +{ this->Internals->AddUniform<const float *,vtkCustomUniform1fv>(name,count,f); } +void vtkOpenGLUniforms::AddUniform2fv (const char *name, const int count, const float(*f)[2]) +{ this->Internals->AddUniform<const float(*)[2],vtkCustomUniform2fv>(name,count,f); } +void vtkOpenGLUniforms::AddUniform3fv (const char *name, const int count, const float(*f)[3]) +{ this->Internals->AddUniform<const float(*)[3],vtkCustomUniform3fv>(name,count,f); } +void vtkOpenGLUniforms::AddUniform4fv (const char *name, const int count, const float(*f)[4]) +{ this->Internals->AddUniform<const float(*)[4],vtkCustomUniform4fv>(name,count,f); } +void vtkOpenGLUniforms::AddUniformMatrix4x4v (const char *name, const int count, float *v) +{ this->Internals->AddUniform<float *,vtkCustomUniformMatrix4x4v>(name,count,v); } + +void vtkOpenGLUniforms::RemoveUniform(const char *name) { this->Internals->RemoveUniform(name); } +void vtkOpenGLUniforms::RemoveAllUniforms() { this->Internals->RemoveAllUniforms(); } + +void vtkOpenGLUniforms::SetUniformi (const char *name, int v) +{ this->Internals->SetUniform<int,vtkCustomUniformi>(name, v); } +void vtkOpenGLUniforms::SetUniformf (const char *name, float v) +{ this->Internals->SetUniform<float,vtkCustomUniformf>(name,v); } +void vtkOpenGLUniforms::SetUniform2i (const char *name, const int v[2]) +{ this->Internals->SetUniform<const int[2],vtkCustomUniform2i>(name,v); } +void vtkOpenGLUniforms::SetUniform2f (const char *name, const float v[2]) +{ this->Internals->SetUniform<const float[2],vtkCustomUniform2f>(name,v); } +void vtkOpenGLUniforms::SetUniform3f (const char *name, const float v[3]) +{ this->Internals->SetUniform<const float[3],vtkCustomUniform3f>(name,v); } +void vtkOpenGLUniforms::SetUniform3f (const char *name, const double v[3]) +{ this->Internals->SetUniform<const double[3],vtkCustomUniform3d>(name,v); } +void vtkOpenGLUniforms::SetUniform4f (const char *name, const float v[4]) +{ this->Internals->SetUniform<const float[4],vtkCustomUniform4f>(name,v); } +void vtkOpenGLUniforms::SetUniform3uc (const char *name, const unsigned char v[3]) +{ this->Internals->SetUniform<const unsigned char[3],vtkCustomUniform3uc>(name,v); } +void vtkOpenGLUniforms::SetUniform4uc (const char *name, const unsigned char v[4]) +{ this->Internals->SetUniform<const unsigned char[4],vtkCustomUniform4uc>(name,v); } +void vtkOpenGLUniforms::SetUniformMatrix (const char *name, vtkMatrix3x3 *v) +{ this->Internals->SetUniform<vtkMatrix3x3*,vtkCustomUniformVtkMatrix3x3>(name,v); } +void vtkOpenGLUniforms::SetUniformMatrix (const char *name, vtkMatrix4x4 *v) +{ this->Internals->SetUniform<vtkMatrix4x4*,vtkCustomUniformVtkMatrix4x4>(name,v); } +void vtkOpenGLUniforms::SetUniformMatrix3x3 (const char *name, float *v) +{ this->Internals->SetUniform<float*,vtkCustomUniformMatrix3x3>(name,v); } +void vtkOpenGLUniforms::SetUniformMatrix4x4 (const char *name, float *v) +{ this->Internals->SetUniform<float*,vtkCustomUniformMatrix4x4>(name,v); } +void vtkOpenGLUniforms::SetUniform1iv (const char *name, const int count, const int *f) +{ this->Internals->SetUniform<const int *,vtkCustomUniform1iv>(name,count,f); } +void vtkOpenGLUniforms::SetUniform1fv (const char *name, const int count, const float *f) +{ this->Internals->SetUniform<const float*,vtkCustomUniform1fv>(name,count,f); } +void vtkOpenGLUniforms::SetUniform2fv (const char *name, const int count, const float(*f)[2]) +{ this->Internals->SetUniform<const float(*)[2],vtkCustomUniform2fv>(name,count,f); } +void vtkOpenGLUniforms::SetUniform3fv (const char *name, const int count, const float(*f)[3]) +{ this->Internals->SetUniform<const float(*)[3],vtkCustomUniform3fv>(name,count,f); } +void vtkOpenGLUniforms::SetUniform4fv (const char *name, const int count, const float(*f)[4]) +{ this->Internals->SetUniform<const float(*)[4],vtkCustomUniform4fv>(name,count,f); } +void vtkOpenGLUniforms::SetUniformMatrix4x4v (const char *name, const int count, float *v) +{ this->Internals->SetUniform<float*,vtkCustomUniformMatrix4x4v>(name,count,v); } + +std::string vtkOpenGLUniforms::GetDeclarations() +{ return this->Internals->GetDeclarations(); } + +bool vtkOpenGLUniforms::SetUniforms( vtkShaderProgram * p ) +{ return this->Internals->SetUniforms(p); } + +vtkMTimeType vtkOpenGLUniforms::GetUniformListMTime() +{ + return this->Internals->GetMTime(); +} + +void vtkOpenGLUniforms::PrintSelf(ostream& os, vtkIndent indent) +{ + this->Superclass::PrintSelf(os,indent); + this->Internals->PrintSelf(os,indent); +} diff --git a/Rendering/VolumeOpenGL2/vtkOpenGLUniforms.h b/Rendering/VolumeOpenGL2/vtkOpenGLUniforms.h new file mode 100644 index 00000000000..39a37211d59 --- /dev/null +++ b/Rendering/VolumeOpenGL2/vtkOpenGLUniforms.h @@ -0,0 +1,110 @@ +/*========================================================================= + + Program: Visualization Toolkit + + Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen + All rights reserved. + See Copyright.txt or http://www.kitware.com/Copyright.htm for details. + + This software is distributed WITHOUT ANY WARRANTY; without even + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + 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 +{ +public: + 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); + +protected: + vtkOpenGLUniforms(); + ~vtkOpenGLUniforms() override; + +private: + vtkOpenGLUniforms(const vtkOpenGLUniforms&) = delete; + void operator=(const vtkOpenGLUniforms&) = delete; + + vtkUniformInternals * Internals; +}; + +#endif -- GitLab