Advertisement
Guest User

Untitled

a guest
Feb 15th, 2012
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.50 KB | None | 0 0
  1. namespace irr {
  2. namespace video {
  3.  
  4. // Describes source of the shader (e.g. text string, name of the text file, pointer to IReadFile).
  5. class GPUShaderSource
  6. {
  7. public:
  8.  
  9.         GPUShaderSource(const c8* sourceString)
  10.                 : source(0)
  11.         {
  12.                 if (sourceString && sourceString[0])
  13.                 {
  14.                         size_t s = strlen(sourceString);
  15.                         source = new c8[s + 1];
  16.                         strcpy(source, sourceString);
  17.                 }
  18.         }
  19.        
  20.         GPUShaderSource(io::IReadFile* sourceFile)
  21.                 : source(0)
  22.         {
  23.                 source = getFileContent(sourceFile);
  24.         }
  25.  
  26.         GPUShaderSource(const io::path sourcePath)
  27.                 : source(0)
  28.         {
  29.                 io::IReadFile* f = 0; // FIXME: cannot use here internal function call "io::createReadFile(sourcePath);"
  30.                 if (f)
  31.                 {
  32.                         source = getFileContent(f);
  33.                         f->drop();
  34.                 }
  35.         }
  36.  
  37.         ~GPUShaderSource()
  38.         {
  39.                 if (source)
  40.                         delete [] source;
  41.         }
  42.  
  43.         inline c8* getSource() const
  44.         {
  45.                 return source;
  46.         }
  47.  
  48.         inline bool isValid() const
  49.         {
  50.                 return source != 0;
  51.         }
  52.  
  53. private:
  54.  
  55.         c8* getFileContent(io::IReadFile* f)
  56.         {
  57.                 if (f)
  58.                 {
  59.                         const long s = f->getSize();
  60.                         if (s > 0)
  61.                         {
  62.                                 c8* c = new c8[s + 1];
  63.                                 f->read(c, s);
  64.                                 c[s] = 0;
  65.  
  66.                                 return c;
  67.                         }
  68.                 }
  69.  
  70.                 return 0;
  71.         }
  72.  
  73.         c8* source;
  74. };
  75.  
  76. // Describes particular shader. This is base class to all shader types, and it contains all common data.
  77. template <class T_CompileTarget, T_CompileTarget T_CompileTargetDefaultValue>
  78. class GPUShaderInfo
  79. {
  80. public:
  81.  
  82.         GPUShaderInfo(const GPUShaderSource& source = GPUShaderSource((c8*)0), const c8* entryPoint = "main",
  83.                 T_CompileTarget compileTarget = T_CompileTargetDefaultValue)
  84.                 : sourceCode(0)
  85.                 , entryPoint(0)
  86.                 , compileTarget(compileTarget)
  87.         {
  88.                 if (source.isValid())
  89.                 {
  90.                         size_t s = strlen(source.getSource());
  91.                         sourceCode = new c8[s + 1];
  92.                         strcpy(sourceCode, source.getSource());
  93.                 }
  94.  
  95.                 if (entryPoint && entryPoint[0])
  96.                 {
  97.                         size_t s = strlen(entryPoint);
  98.                         this->entryPoint = new c8[s + 1];
  99.                         strcpy(this->entryPoint, entryPoint);
  100.                 }
  101.         }
  102.  
  103.         ~GPUShaderInfo()
  104.         {
  105.                 if (sourceCode)
  106.                         delete [] sourceCode;
  107.  
  108.                 if (entryPoint)
  109.                         delete [] entryPoint;
  110.         }
  111.  
  112.         inline bool isValid() const
  113.         {
  114.                 return sourceCode && entryPoint;
  115.         }
  116.  
  117.         inline c8* getSourceCode() const
  118.         {
  119.                 return sourceCode;
  120.         }
  121.  
  122.         inline c8* getEntryPoint() const
  123.         {
  124.                 return entryPoint;
  125.         }
  126.  
  127.         inline T_CompileTarget getCompileTarget() const
  128.         {
  129.                 return compileTarget;
  130.         }
  131.  
  132. private:
  133.        
  134.         c8* sourceCode;
  135.         c8* entryPoint;
  136.         T_CompileTarget compileTarget;
  137. };
  138.  
  139. typedef GPUShaderInfo<E_VERTEX_SHADER_TYPE, EVST_VS_1_1> GPUVertexShaderInfo;
  140. typedef GPUShaderInfo<E_PIXEL_SHADER_TYPE, EPST_PS_1_1> GPUPixelShaderInfo;
  141.  
  142. // Vertex shader and pixel shader have very similar structure, that is why we have used template, but if they would go different in future,
  143. // we can do something similar to what we are doing bottom (with geometry shader) - and we will not break old code which uses it.
  144.  
  145. class GPUGeometryShaderInfo : public GPUShaderInfo<E_GEOMETRY_SHADER_TYPE, EGST_GS_4_0>
  146. {
  147. public:
  148.  
  149.         GPUGeometryShaderInfo(const GPUShaderSource& source = GPUShaderSource((c8*)0), const c8* entryPoint = "main",
  150.                 E_GEOMETRY_SHADER_TYPE compileTarget = EGST_GS_4_0, scene::E_PRIMITIVE_TYPE inType = scene::EPT_TRIANGLES,
  151.                 scene::E_PRIMITIVE_TYPE outType = scene::EPT_TRIANGLE_STRIP, u32 outVertexCount = 0)
  152.                 : GPUShaderInfo<E_GEOMETRY_SHADER_TYPE, EGST_GS_4_0>(source, entryPoint, compileTarget)
  153.                 , inputPrimitiveType(inType)
  154.                 , outputPrimitiveType(outType)
  155.                 , outputMaxVertexCount(outVertexCount)
  156.         {
  157.         }
  158.  
  159.         inline scene::E_PRIMITIVE_TYPE getInputPrimitiveType() const
  160.         {
  161.                 return inputPrimitiveType;
  162.         }
  163.  
  164.         inline scene::E_PRIMITIVE_TYPE getOutputPrimitiveType() const
  165.         {
  166.                 return outputPrimitiveType;
  167.         }
  168.  
  169.         inline u32 getOutputMaxVertexCount() const
  170.         {
  171.                 return outputMaxVertexCount;
  172.         }
  173.  
  174. private:
  175.  
  176.         scene::E_PRIMITIVE_TYPE inputPrimitiveType;
  177.         scene::E_PRIMITIVE_TYPE outputPrimitiveType;
  178.         u32 outputMaxVertexCount;
  179. };
  180.  
  181. // Describes shader material. Contains all necessary data to call IGPUProgrammingServices::addHighLevelShaderMaterial().
  182. class GPUShaderMaterialInfo
  183. {
  184. public:
  185.  
  186.         GPUShaderMaterialInfo(const GPUVertexShaderInfo& vertexShader, const GPUPixelShaderInfo& pixelShader = GPUPixelShaderInfo((c8*)0),
  187.                 const GPUGeometryShaderInfo& geometryShader = GPUGeometryShaderInfo((c8*)0), IShaderConstantSetCallBack* constantSetCallBack = 0,
  188.                 E_MATERIAL_TYPE baseMaterial = EMT_SOLID, s32 userData = 0, E_GPU_SHADING_LANGUAGE shadingLanguage = EGSL_DEFAULT)
  189.                 : vertexShader(vertexShader)
  190.                 , pixelShader(pixelShader)
  191.                 , geometryShader(geometryShader)
  192.                 , constantSetCallBack(constantSetCallBack)
  193.                 , baseMaterial(baseMaterial)
  194.                 , userData(userData)
  195.                 , shadingLanguage(shadingLanguage)
  196.         {
  197.         }
  198.  
  199.         inline bool isValid() const
  200.         {
  201.                 // we assume that whole the material is valid, if any shader is valid
  202.                 return vertexShader.isValid() || pixelShader.isValid() || geometryShader.isValid();
  203.         }
  204.  
  205.         inline const GPUVertexShaderInfo& getVertexShader() const
  206.         {
  207.                 return vertexShader;
  208.         }
  209.  
  210.         inline const GPUPixelShaderInfo& getPixelShader() const
  211.         {
  212.                 return pixelShader;
  213.         }
  214.  
  215.         inline const GPUGeometryShaderInfo& getGeometryShader() const
  216.         {
  217.                 return geometryShader;
  218.         }
  219.  
  220.         inline IShaderConstantSetCallBack* getConstantSetCallBack() const
  221.         {
  222.                 return constantSetCallBack;
  223.         }
  224.  
  225.         inline E_MATERIAL_TYPE getBaseMaterial() const
  226.         {
  227.                 return baseMaterial;
  228.         }
  229.  
  230.         inline u32 getUserData() const
  231.         {
  232.                 return userData;
  233.         }
  234.  
  235.         inline E_GPU_SHADING_LANGUAGE getShadingLanguage() const
  236.         {
  237.                 return shadingLanguage;
  238.         }
  239.  
  240. private:
  241.  
  242.         GPUVertexShaderInfo vertexShader;
  243.         GPUPixelShaderInfo pixelShader;
  244.         GPUGeometryShaderInfo geometryShader;
  245.         IShaderConstantSetCallBack* constantSetCallBack;
  246.         E_MATERIAL_TYPE baseMaterial;
  247.         s32 userData;
  248.         E_GPU_SHADING_LANGUAGE shadingLanguage;
  249. };
  250.  
  251. // this is hypotetic implementation of addHighLevelShaderMaterial()
  252. // please note: it crashes since thisGPU pointer is 0.
  253.  
  254. s32 addHighLevelShaderMaterial(const GPUShaderMaterialInfo& materialInfo)
  255. {
  256.         if (!materialInfo.isValid())
  257.                 return -1;
  258.  
  259.         const GPUVertexShaderInfo& vs = materialInfo.getVertexShader();
  260.         const GPUPixelShaderInfo& ps = materialInfo.getPixelShader();
  261.         const GPUGeometryShaderInfo& gs = materialInfo.getGeometryShader();
  262.  
  263.         IGPUProgrammingServices* thisGPU = 0; // FIXME: lets assume we are defined inside IGPUProgrammingServices
  264.         // then addHighLevelShaderMaterial() from Irrlicht core would look like:
  265.  
  266.         // NEXT LINE LEADS TO CRASH
  267.         return thisGPU->addHighLevelShaderMaterial(
  268.                 vs.isValid() ? vs.getSourceCode() : (const c8*)0,
  269.                 vs.isValid() ? vs.getEntryPoint() : (const c8*)0, // please note, we don't need to duplicate default value "main", we already defined it once in proper constructor (same for all other "(type)0" expressions)
  270.                 vs.isValid() ? vs.getCompileTarget() : (E_VERTEX_SHADER_TYPE)0,
  271.                 ps.isValid() ? ps.getSourceCode() : (const c8*)0,
  272.                 ps.isValid() ? ps.getEntryPoint() : (const c8*)0,
  273.                 ps.isValid() ? ps.getCompileTarget() : (E_PIXEL_SHADER_TYPE)0,
  274.                 gs.isValid() ? gs.getSourceCode() : (const c8*)0,
  275.                 gs.isValid() ? gs.getEntryPoint() : (const c8*)0,
  276.                 gs.isValid() ? gs.getCompileTarget() : (E_GEOMETRY_SHADER_TYPE)0,
  277.                 gs.isValid() ? gs.getInputPrimitiveType() : (scene::E_PRIMITIVE_TYPE)0,
  278.                 gs.isValid() ? gs.getOutputPrimitiveType() : (scene::E_PRIMITIVE_TYPE)0,
  279.                 gs.isValid() ? gs.getOutputMaxVertexCount() : (u32)0,
  280.                 materialInfo.getConstantSetCallBack(),
  281.                 materialInfo.getBaseMaterial(),
  282.                 materialInfo.getUserData(),
  283.                 materialInfo.getShadingLanguage()); // here "shadingLang" argument:
  284.                         // i don't know actually the truth so it should be clarifyed:
  285.                         // now i assume that each material defines language type for all shaders in it -- if it is not correct,
  286.                         // that "shadingLanguage" should be simply moved from GPUShaderMaterialInfo to GPUShaderInfo class
  287. }
  288.  
  289. } // end of video
  290. } // end of irr
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement