diff options
Diffstat (limited to '')
-rw-r--r-- | libraries/irrlicht-1.8/source/Irrlicht/CD3D9CgMaterialRenderer.cpp | 222 |
1 files changed, 222 insertions, 0 deletions
diff --git a/libraries/irrlicht-1.8/source/Irrlicht/CD3D9CgMaterialRenderer.cpp b/libraries/irrlicht-1.8/source/Irrlicht/CD3D9CgMaterialRenderer.cpp new file mode 100644 index 0000000..8220a7e --- /dev/null +++ b/libraries/irrlicht-1.8/source/Irrlicht/CD3D9CgMaterialRenderer.cpp | |||
@@ -0,0 +1,222 @@ | |||
1 | // Copyright (C) 2012-2012 Patryk Nadrowski | ||
2 | // This file is part of the "Irrlicht Engine". | ||
3 | // For conditions of distribution and use, see copyright notice in irrlicht.h | ||
4 | |||
5 | #include "IrrCompileConfig.h" | ||
6 | #if defined(_IRR_COMPILE_WITH_DIRECT3D_9_) && defined(_IRR_COMPILE_WITH_CG_) | ||
7 | |||
8 | #include "CD3D9CgMaterialRenderer.h" | ||
9 | #include "CD3D9Driver.h" | ||
10 | #include "CD3D9Texture.h" | ||
11 | |||
12 | namespace irr | ||
13 | { | ||
14 | namespace video | ||
15 | { | ||
16 | |||
17 | CD3D9CgUniformSampler2D::CD3D9CgUniformSampler2D(const CGparameter& parameter, bool global) : CCgUniform(parameter, global) | ||
18 | { | ||
19 | Type = CG_SAMPLER2D; | ||
20 | } | ||
21 | |||
22 | void CD3D9CgUniformSampler2D::update(const void* data, const SMaterial& material) const | ||
23 | { | ||
24 | s32* Data = (s32*)data; | ||
25 | s32 LayerID = *Data; | ||
26 | |||
27 | if (material.TextureLayer[LayerID].Texture) | ||
28 | { | ||
29 | IDirect3DBaseTexture9* Texture = reinterpret_cast<irr::video::CD3D9Texture*>(material.TextureLayer[LayerID].Texture)->getDX9Texture(); | ||
30 | |||
31 | cgD3D9SetTextureParameter(Parameter, Texture); | ||
32 | } | ||
33 | } | ||
34 | |||
35 | CD3D9CgMaterialRenderer::CD3D9CgMaterialRenderer(CD3D9Driver* driver, s32& materialType, | ||
36 | const c8* vertexProgram, const c8* vertexEntry, E_VERTEX_SHADER_TYPE vertexProfile, | ||
37 | const c8* fragmentProgram, const c8* fragmentEntry, E_PIXEL_SHADER_TYPE fragmentProfile, | ||
38 | const c8* geometryProgram, const c8* geometryEntry, E_GEOMETRY_SHADER_TYPE geometryProfile, | ||
39 | scene::E_PRIMITIVE_TYPE inType, scene::E_PRIMITIVE_TYPE outType, u32 vertices, | ||
40 | IShaderConstantSetCallBack* callback, IMaterialRenderer* baseMaterial, s32 userData) : | ||
41 | Driver(driver), CCgMaterialRenderer(callback, baseMaterial, userData) | ||
42 | { | ||
43 | #ifdef _DEBUG | ||
44 | setDebugName("CD3D9CgMaterialRenderer"); | ||
45 | #endif | ||
46 | |||
47 | init(materialType, vertexProgram, vertexEntry, vertexProfile, fragmentProgram, fragmentEntry, fragmentProfile, | ||
48 | geometryProgram, geometryEntry, geometryProfile, inType, outType, vertices); | ||
49 | } | ||
50 | |||
51 | CD3D9CgMaterialRenderer::~CD3D9CgMaterialRenderer() | ||
52 | { | ||
53 | if (VertexProgram) | ||
54 | { | ||
55 | cgD3D9UnloadProgram(VertexProgram); | ||
56 | cgDestroyProgram(VertexProgram); | ||
57 | } | ||
58 | if (FragmentProgram) | ||
59 | { | ||
60 | cgD3D9UnloadProgram(FragmentProgram); | ||
61 | cgDestroyProgram(FragmentProgram); | ||
62 | } | ||
63 | /*if (GeometryProgram) | ||
64 | { | ||
65 | cgD3D9UnloadProgram(GeometryProgram); | ||
66 | cgDestroyProgram(GeometryProgram); | ||
67 | }*/ | ||
68 | } | ||
69 | |||
70 | void CD3D9CgMaterialRenderer::OnSetMaterial(const SMaterial& material, const SMaterial& lastMaterial, bool resetAllRenderstates, IMaterialRendererServices* services) | ||
71 | { | ||
72 | Material = material; | ||
73 | |||
74 | if (material.MaterialType != lastMaterial.MaterialType || resetAllRenderstates) | ||
75 | { | ||
76 | if (VertexProgram) | ||
77 | cgD3D9BindProgram(VertexProgram); | ||
78 | |||
79 | if (FragmentProgram) | ||
80 | cgD3D9BindProgram(FragmentProgram); | ||
81 | |||
82 | /*if (GeometryProgram) | ||
83 | cgD3D9BindProgram(GeometryProgram);*/ | ||
84 | |||
85 | if (BaseMaterial) | ||
86 | BaseMaterial->OnSetMaterial(material, material, true, this); | ||
87 | } | ||
88 | |||
89 | if (CallBack) | ||
90 | CallBack->OnSetMaterial(material); | ||
91 | |||
92 | Driver->setBasicRenderStates(material, lastMaterial, resetAllRenderstates); | ||
93 | } | ||
94 | |||
95 | bool CD3D9CgMaterialRenderer::OnRender(IMaterialRendererServices* services, E_VERTEX_TYPE vtxtype) | ||
96 | { | ||
97 | if (CallBack && (VertexProgram || FragmentProgram || GeometryProgram)) | ||
98 | CallBack->OnSetConstants(this, UserData); | ||
99 | |||
100 | return true; | ||
101 | } | ||
102 | |||
103 | void CD3D9CgMaterialRenderer::OnUnsetMaterial() | ||
104 | { | ||
105 | if (VertexProgram) | ||
106 | cgD3D9UnbindProgram(VertexProgram); | ||
107 | if (FragmentProgram) | ||
108 | cgD3D9UnbindProgram(FragmentProgram); | ||
109 | /*if (GeometryProgram) | ||
110 | cgD3D9UnbindProgram(GeometryProgram);*/ | ||
111 | |||
112 | if (BaseMaterial) | ||
113 | BaseMaterial->OnUnsetMaterial(); | ||
114 | |||
115 | Material = IdentityMaterial;; | ||
116 | } | ||
117 | |||
118 | void CD3D9CgMaterialRenderer::setBasicRenderStates(const SMaterial& material, const SMaterial& lastMaterial, bool resetAllRenderstates) | ||
119 | { | ||
120 | Driver->setBasicRenderStates(material, lastMaterial, resetAllRenderstates); | ||
121 | } | ||
122 | |||
123 | IVideoDriver* CD3D9CgMaterialRenderer::getVideoDriver() | ||
124 | { | ||
125 | return Driver; | ||
126 | } | ||
127 | |||
128 | void CD3D9CgMaterialRenderer::init(s32& materialType, | ||
129 | const c8* vertexProgram, const c8* vertexEntry, E_VERTEX_SHADER_TYPE vertexProfile, | ||
130 | const c8* fragmentProgram, const c8* fragmentEntry, E_PIXEL_SHADER_TYPE fragmentProfile, | ||
131 | const c8* geometryProgram, const c8* geometryEntry, E_GEOMETRY_SHADER_TYPE geometryProfile, | ||
132 | scene::E_PRIMITIVE_TYPE inType, scene::E_PRIMITIVE_TYPE outType, u32 vertices) | ||
133 | { | ||
134 | bool Status = true; | ||
135 | CGerror Error = CG_NO_ERROR; | ||
136 | materialType = -1; | ||
137 | |||
138 | // TODO: add profile selection | ||
139 | |||
140 | if (vertexProgram) | ||
141 | { | ||
142 | VertexProfile = cgD3D9GetLatestVertexProfile(); | ||
143 | |||
144 | if (VertexProfile) | ||
145 | VertexProgram = cgCreateProgram(Driver->getCgContext(), CG_SOURCE, vertexProgram, VertexProfile, vertexEntry, 0); | ||
146 | |||
147 | if (!VertexProgram) | ||
148 | { | ||
149 | Error = cgGetError(); | ||
150 | os::Printer::log("Cg vertex program failed to compile:", ELL_ERROR); | ||
151 | os::Printer::log(cgGetLastListing(Driver->getCgContext()), ELL_ERROR); | ||
152 | |||
153 | Status = false; | ||
154 | } | ||
155 | else | ||
156 | cgD3D9LoadProgram(VertexProgram, 0, 0); | ||
157 | } | ||
158 | |||
159 | if (fragmentProgram) | ||
160 | { | ||
161 | FragmentProfile = cgD3D9GetLatestPixelProfile(); | ||
162 | |||
163 | if (FragmentProfile) | ||
164 | FragmentProgram = cgCreateProgram(Driver->getCgContext(), CG_SOURCE, fragmentProgram, FragmentProfile, fragmentEntry, 0); | ||
165 | |||
166 | if (!FragmentProgram) | ||
167 | { | ||
168 | Error = cgGetError(); | ||
169 | os::Printer::log("Cg fragment program failed to compile:", ELL_ERROR); | ||
170 | os::Printer::log(cgGetLastListing(Driver->getCgContext()), ELL_ERROR); | ||
171 | |||
172 | Status = false; | ||
173 | } | ||
174 | else | ||
175 | cgD3D9LoadProgram(FragmentProgram, 0, 0); | ||
176 | } | ||
177 | |||
178 | /*if (geometryProgram) | ||
179 | { | ||
180 | GeometryProfile = cgD3D9GetLatestGeometryProfile(); | ||
181 | |||
182 | if (GeometryProfile) | ||
183 | GeometryProgram = cgCreateProgram(Driver->getCgContext(), CG_SOURCE, geometryProgram, GeometryProfile, geometryEntry, 0); | ||
184 | |||
185 | if (!GeometryProgram) | ||
186 | { | ||
187 | Error = cgGetError(); | ||
188 | os::Printer::log("Cg geometry program failed to compile:", ELL_ERROR); | ||
189 | os::Printer::log(cgGetLastListing(Driver->getCgContext()), ELL_ERROR); | ||
190 | |||
191 | Status = false; | ||
192 | } | ||
193 | else | ||
194 | cgD3D9LoadProgram(GeometryProgram, 0, 0); | ||
195 | }*/ | ||
196 | |||
197 | getUniformList(); | ||
198 | |||
199 | // create D3D9 specifics sampler uniforms. | ||
200 | for(unsigned int i = 0; i < UniformInfo.size(); ++i) | ||
201 | { | ||
202 | if (UniformInfo[i]->getType() == CG_SAMPLER2D) | ||
203 | { | ||
204 | bool IsGlobal = true; | ||
205 | |||
206 | if (UniformInfo[i]->getSpace() == CG_PROGRAM) | ||
207 | IsGlobal = false; | ||
208 | |||
209 | CCgUniform* Uniform = new CD3D9CgUniformSampler2D(UniformInfo[i]->getParameter(), IsGlobal); | ||
210 | delete UniformInfo[i]; | ||
211 | UniformInfo[i] = Uniform; | ||
212 | } | ||
213 | } | ||
214 | |||
215 | if (Status) | ||
216 | materialType = Driver->addMaterialRenderer(this); | ||
217 | } | ||
218 | |||
219 | } | ||
220 | } | ||
221 | |||
222 | #endif | ||