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