Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace irr {
- namespace video {
- // Describes source of the shader (e.g. text string, name of the text file, pointer to IReadFile).
- class GPUShaderSource
- {
- public:
- GPUShaderSource(const c8* sourceString)
- : source(0)
- {
- if (sourceString && sourceString[0])
- {
- size_t s = strlen(sourceString);
- source = new c8[s + 1];
- strcpy(source, sourceString);
- }
- }
- GPUShaderSource(io::IReadFile* sourceFile)
- : source(0)
- {
- source = getFileContent(sourceFile);
- }
- GPUShaderSource(const io::path sourcePath)
- : source(0)
- {
- io::IReadFile* f = 0; // FIXME: cannot use here internal function call "io::createReadFile(sourcePath);"
- if (f)
- {
- source = getFileContent(f);
- f->drop();
- }
- }
- ~GPUShaderSource()
- {
- if (source)
- delete [] source;
- }
- inline c8* getSource() const
- {
- return source;
- }
- inline bool isValid() const
- {
- return source != 0;
- }
- private:
- c8* getFileContent(io::IReadFile* f)
- {
- if (f)
- {
- const long s = f->getSize();
- if (s > 0)
- {
- c8* c = new c8[s + 1];
- f->read(c, s);
- c[s] = 0;
- return c;
- }
- }
- return 0;
- }
- c8* source;
- };
- // Describes particular shader. This is base class to all shader types, and it contains all common data.
- template <class T_CompileTarget, T_CompileTarget T_CompileTargetDefaultValue>
- class GPUShaderInfo
- {
- public:
- GPUShaderInfo(const GPUShaderSource& source = GPUShaderSource((c8*)0), const c8* entryPoint = "main",
- T_CompileTarget compileTarget = T_CompileTargetDefaultValue)
- : sourceCode(0)
- , entryPoint(0)
- , compileTarget(compileTarget)
- {
- if (source.isValid())
- {
- size_t s = strlen(source.getSource());
- sourceCode = new c8[s + 1];
- strcpy(sourceCode, source.getSource());
- }
- if (entryPoint && entryPoint[0])
- {
- size_t s = strlen(entryPoint);
- this->entryPoint = new c8[s + 1];
- strcpy(this->entryPoint, entryPoint);
- }
- }
- ~GPUShaderInfo()
- {
- if (sourceCode)
- delete [] sourceCode;
- if (entryPoint)
- delete [] entryPoint;
- }
- inline bool isValid() const
- {
- return sourceCode && entryPoint;
- }
- inline c8* getSourceCode() const
- {
- return sourceCode;
- }
- inline c8* getEntryPoint() const
- {
- return entryPoint;
- }
- inline T_CompileTarget getCompileTarget() const
- {
- return compileTarget;
- }
- private:
- c8* sourceCode;
- c8* entryPoint;
- T_CompileTarget compileTarget;
- };
- typedef GPUShaderInfo<E_VERTEX_SHADER_TYPE, EVST_VS_1_1> GPUVertexShaderInfo;
- typedef GPUShaderInfo<E_PIXEL_SHADER_TYPE, EPST_PS_1_1> GPUPixelShaderInfo;
- // Vertex shader and pixel shader have very similar structure, that is why we have used template, but if they would go different in future,
- // we can do something similar to what we are doing bottom (with geometry shader) - and we will not break old code which uses it.
- class GPUGeometryShaderInfo : public GPUShaderInfo<E_GEOMETRY_SHADER_TYPE, EGST_GS_4_0>
- {
- public:
- GPUGeometryShaderInfo(const GPUShaderSource& source = GPUShaderSource((c8*)0), const c8* entryPoint = "main",
- E_GEOMETRY_SHADER_TYPE compileTarget = EGST_GS_4_0, scene::E_PRIMITIVE_TYPE inType = scene::EPT_TRIANGLES,
- scene::E_PRIMITIVE_TYPE outType = scene::EPT_TRIANGLE_STRIP, u32 outVertexCount = 0)
- : GPUShaderInfo<E_GEOMETRY_SHADER_TYPE, EGST_GS_4_0>(source, entryPoint, compileTarget)
- , inputPrimitiveType(inType)
- , outputPrimitiveType(outType)
- , outputMaxVertexCount(outVertexCount)
- {
- }
- inline scene::E_PRIMITIVE_TYPE getInputPrimitiveType() const
- {
- return inputPrimitiveType;
- }
- inline scene::E_PRIMITIVE_TYPE getOutputPrimitiveType() const
- {
- return outputPrimitiveType;
- }
- inline u32 getOutputMaxVertexCount() const
- {
- return outputMaxVertexCount;
- }
- private:
- scene::E_PRIMITIVE_TYPE inputPrimitiveType;
- scene::E_PRIMITIVE_TYPE outputPrimitiveType;
- u32 outputMaxVertexCount;
- };
- // Describes shader material. Contains all necessary data to call IGPUProgrammingServices::addHighLevelShaderMaterial().
- class GPUShaderMaterialInfo
- {
- public:
- GPUShaderMaterialInfo(const GPUVertexShaderInfo& vertexShader, const GPUPixelShaderInfo& pixelShader = GPUPixelShaderInfo((c8*)0),
- const GPUGeometryShaderInfo& geometryShader = GPUGeometryShaderInfo((c8*)0), IShaderConstantSetCallBack* constantSetCallBack = 0,
- E_MATERIAL_TYPE baseMaterial = EMT_SOLID, s32 userData = 0, E_GPU_SHADING_LANGUAGE shadingLanguage = EGSL_DEFAULT)
- : vertexShader(vertexShader)
- , pixelShader(pixelShader)
- , geometryShader(geometryShader)
- , constantSetCallBack(constantSetCallBack)
- , baseMaterial(baseMaterial)
- , userData(userData)
- , shadingLanguage(shadingLanguage)
- {
- }
- inline bool isValid() const
- {
- // we assume that whole the material is valid, if any shader is valid
- return vertexShader.isValid() || pixelShader.isValid() || geometryShader.isValid();
- }
- inline const GPUVertexShaderInfo& getVertexShader() const
- {
- return vertexShader;
- }
- inline const GPUPixelShaderInfo& getPixelShader() const
- {
- return pixelShader;
- }
- inline const GPUGeometryShaderInfo& getGeometryShader() const
- {
- return geometryShader;
- }
- inline IShaderConstantSetCallBack* getConstantSetCallBack() const
- {
- return constantSetCallBack;
- }
- inline E_MATERIAL_TYPE getBaseMaterial() const
- {
- return baseMaterial;
- }
- inline u32 getUserData() const
- {
- return userData;
- }
- inline E_GPU_SHADING_LANGUAGE getShadingLanguage() const
- {
- return shadingLanguage;
- }
- private:
- GPUVertexShaderInfo vertexShader;
- GPUPixelShaderInfo pixelShader;
- GPUGeometryShaderInfo geometryShader;
- IShaderConstantSetCallBack* constantSetCallBack;
- E_MATERIAL_TYPE baseMaterial;
- s32 userData;
- E_GPU_SHADING_LANGUAGE shadingLanguage;
- };
- // this is hypotetic implementation of addHighLevelShaderMaterial()
- // please note: it crashes since thisGPU pointer is 0.
- s32 addHighLevelShaderMaterial(const GPUShaderMaterialInfo& materialInfo)
- {
- if (!materialInfo.isValid())
- return -1;
- const GPUVertexShaderInfo& vs = materialInfo.getVertexShader();
- const GPUPixelShaderInfo& ps = materialInfo.getPixelShader();
- const GPUGeometryShaderInfo& gs = materialInfo.getGeometryShader();
- IGPUProgrammingServices* thisGPU = 0; // FIXME: lets assume we are defined inside IGPUProgrammingServices
- // then addHighLevelShaderMaterial() from Irrlicht core would look like:
- // NEXT LINE LEADS TO CRASH
- return thisGPU->addHighLevelShaderMaterial(
- vs.isValid() ? vs.getSourceCode() : (const c8*)0,
- 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)
- vs.isValid() ? vs.getCompileTarget() : (E_VERTEX_SHADER_TYPE)0,
- ps.isValid() ? ps.getSourceCode() : (const c8*)0,
- ps.isValid() ? ps.getEntryPoint() : (const c8*)0,
- ps.isValid() ? ps.getCompileTarget() : (E_PIXEL_SHADER_TYPE)0,
- gs.isValid() ? gs.getSourceCode() : (const c8*)0,
- gs.isValid() ? gs.getEntryPoint() : (const c8*)0,
- gs.isValid() ? gs.getCompileTarget() : (E_GEOMETRY_SHADER_TYPE)0,
- gs.isValid() ? gs.getInputPrimitiveType() : (scene::E_PRIMITIVE_TYPE)0,
- gs.isValid() ? gs.getOutputPrimitiveType() : (scene::E_PRIMITIVE_TYPE)0,
- gs.isValid() ? gs.getOutputMaxVertexCount() : (u32)0,
- materialInfo.getConstantSetCallBack(),
- materialInfo.getBaseMaterial(),
- materialInfo.getUserData(),
- materialInfo.getShadingLanguage()); // here "shadingLang" argument:
- // i don't know actually the truth so it should be clarifyed:
- // now i assume that each material defines language type for all shaders in it -- if it is not correct,
- // that "shadingLanguage" should be simply moved from GPUShaderMaterialInfo to GPUShaderInfo class
- }
- } // end of video
- } // end of irr
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement