vtkUniforms.h 7.76 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
/*=========================================================================

  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