1 ////////////////////////////////////////////////////////////////////// 2 // 3 // Pixie 4 // 5 // Copyright � 1999 - 2003, Okan Arikan 6 // 7 // Contact: okan@cs.utexas.edu 8 // 9 // This library is free software; you can redistribute it and/or 10 // modify it under the terms of the GNU Lesser General Public 11 // License as published by the Free Software Foundation; either 12 // version 2.1 of the License, or (at your option) any later version. 13 // 14 // This library is distributed in the hope that it will be useful, 15 // but WITHOUT ANY WARRANTY; without even the implied warranty of 16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 // Lesser General Public License for more details. 18 // 19 // You should have received a copy of the GNU Lesser General Public 20 // License along with this library; if not, write to the Free Software 21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 22 // 23 /////////////////////////////////////////////////////////////////////// 24 /////////////////////////////////////////////////////////////////////// 25 // 26 // File : shader.h 27 // Classes : CShader 28 // CShaderInstance 29 // Description : 30 // 31 //////////////////////////////////////////////////////////////////////// 32 #ifndef SHADER_H 33 #define SHADER_H 34 35 #include "common/global.h" 36 #include "common/containers.h" 37 #include "common/os.h" 38 #include "xform.h" 39 #include "rendererc.h" 40 #include "fileResource.h" 41 #include "ri.h" 42 #include "variable.h" 43 #include "refCounter.h" 44 45 // Forward references 46 class CShader; 47 class CShaderInstance; 48 class CMemPage; 49 class CShadingContext; 50 class CAttributes; 51 class CDSO; 52 53 // Meanings of the accessor field of TArgument 54 const unsigned int SL_IMMEDIATE_OPERAND = 0; // Constants 55 const unsigned int SL_GLOBAL_OPERAND = 1; // Global variable references 56 const unsigned int SL_VARYING_OPERAND = 2; // Local variable references (this includes parameters) 57 58 // This structure holds an argument for a code 59 typedef struct { 60 unsigned char numItems; // The number of items to step for this variable (0 for constants,parameters, cardinality for variables,globals) 61 unsigned char accessor; // The type of the variable (SL_IMMEDIATE,SL_PARAMETER,SL_VARIABLE,SL_GLOBAL) 62 unsigned char bytesPerItem; // The number of bytes per item 63 unsigned char varyingStep; // Used to store whether operand is uniform (step 0) or varying (step) ... important PADDING also 64 unsigned int index; // The index of the variable in the corresponding entry array 65 } TArgument; 66 67 // This structure holds a code 68 // In the future, we may want to make this a class 69 // and have the shading language opcodes subclass from it 70 typedef struct { 71 int opcode; // The opcode 72 unsigned char uniform; // TRUE if all the arguments are uniform 73 unsigned char numArguments; // The number of stack arguments 74 unsigned char padding0; // PADDING ... Not used ... PADDING 75 unsigned char padding1; // PADDING ... Not used ... PADDING 76 TArgument *arguments; // The array of arguments 77 CDSO *dso; // If this is a DSO, points to the DSO function 78 } TCode; 79 80 // Shader types 81 const unsigned int SL_SURFACE = 0; 82 const unsigned int SL_LIGHTSOURCE = 1; 83 const unsigned int SL_DISPLACEMENT = 2; 84 const unsigned int SL_ATMOSPHERE = 3; 85 const unsigned int SL_IMAGER = 4; 86 87 // Shader flags 88 const unsigned int SHADERFLAGS_NONAMBIENT = 1; 89 const unsigned int SHADERFLAGS_NONDIFFUSE = 2; 90 const unsigned int SHADERFLAGS_NONSPECULAR = 4; 91 92 93 94 /////////////////////////////////////////////////////////////////////// 95 // Class : CShaderData 96 // Description : This class records extra data for a shader instance 97 // Comments : 98 class CShaderData { 99 public: CShaderData()100 CShaderData() { } ~CShaderData()101 virtual ~CShaderData() { } 102 }; 103 104 /////////////////////////////////////////////////////////////////////// 105 // Class : CLightShaderData 106 // Description : This class records data for a light 107 // Comments : 108 class CLightShaderData : public CShaderData { 109 public: 110 int nonDiffuseIndex; 111 int nonDiffuseStep; 112 int nonSpecularIndex; 113 int nonSpecularStep; 114 CLightShaderData()115 CLightShaderData() { 116 nonDiffuseIndex = -1; 117 nonSpecularIndex = -1; 118 nonDiffuseStep = 0; 119 nonSpecularStep = 0; 120 } 121 }; 122 123 124 125 /////////////////////////////////////////////////////////////////////// 126 // Class : CShader 127 // Description : This class encapsulates a shader 128 // Comments : 129 class CShader : public CFileResource { 130 public: 131 CShader(const char *); 132 ~CShader(); 133 134 int type; // Type of the shader 135 136 CVariable *parameters; // List of parameters 137 138 void *memory; // The memory base allocated for this shader 139 TCode *codeArea; // The code array 140 141 void **constantEntries; // The constant entries 142 143 int *varyingSizes; // The size of a variable (if negative, it is uniform) 144 145 char **strings; // Strings used by the shader 146 147 int numGlobals; // Number of global parameters 148 int numStrings; // Number of strings 149 int numVariables; // Number of variables 150 151 int codeEntryPoint; // Index into code array where the actual code starts 152 int initEntryPoint; // Index into code array where the init code starts 153 154 int usedParameters; 155 156 unsigned int flags; // shadows of parent data (to support hcShaders) 157 CShaderData *data; // Additional data (owned by CShader) 158 159 friend CShader *parseShader(const char *,const char *); 160 void analyse(); 161 }; 162 163 164 165 /////////////////////////////////////////////////////////////////////// 166 // Class : CShaderInstance 167 // Description : This class encapsulates an instance of a shader 168 // Comments : 169 class CShaderInstance : public CRefCounter { 170 public: 171 CShaderInstance(CAttributes *,CXform *); 172 virtual ~CShaderInstance(); 173 174 virtual void illuminate(CShadingContext *,float **) = 0; 175 virtual void setParameters(int,const char **,const void **) = 0; 176 virtual int getParameter(const char *,void *,CVariable**,int*) = 0; 177 virtual void execute(CShadingContext *,float **) = 0; 178 virtual unsigned int requiredParameters() = 0; 179 virtual const char *getName() = 0; 180 virtual float **prepare(CMemPage*&,float **,int) = 0; 181 182 void createCategories(); 183 184 CVariable *parameters; // The list of parameter (cloned from the parent) 185 CXform *xform; 186 int *categories; // Categories for light shaders 187 unsigned int flags; 188 CShaderData *data; // Extra non-instance data 189 // simply referenced here (not owned) 190 }; 191 192 /////////////////////////////////////////////////////////////////////// 193 // Class : CProgrammableShaderInstance 194 // Description : This class encapsulates an instance of a programmable shader 195 // Comments : 196 class CProgrammableShaderInstance : public CShaderInstance { 197 198 /////////////////////////////////////////////////////////////////////// 199 // Class : CAllocatedString 200 // Description : We use this class to keep track of the allocated strings for parameters 201 // Comments : 202 class CAllocatedString { 203 public: 204 char *string; 205 CAllocatedString *next; 206 }; 207 public: 208 CProgrammableShaderInstance(CShader *,CAttributes *,CXform *); 209 virtual ~CProgrammableShaderInstance(); 210 211 void illuminate(CShadingContext *,float **); 212 void setParameters(int,const char **,const void **); 213 int getParameter(const char *,void *,CVariable**,int*); // Get the value of a parameter 214 void execute(CShadingContext *,float **); // Execute the shader 215 unsigned int requiredParameters(); 216 const char *getName(); 217 float **prepare(CMemPage*&,float **,int); 218 219 220 CAllocatedString *strings; // The strings we allocated for parameters 221 CShader *parent; // The parent shader 222 private: 223 int setParameter(const char *,const void *); 224 }; 225 226 #endif 227 228