1 | // |
---|
2 | // Lol Engine |
---|
3 | // |
---|
4 | // Copyright: (c) 2010-2012 Sam Hocevar <sam@hocevar.net> |
---|
5 | // This program is free software; you can redistribute it and/or |
---|
6 | // modify it under the terms of the Do What The Fuck You Want To |
---|
7 | // Public License, Version 2, as published by Sam Hocevar. See |
---|
8 | // http://sam.zoy.org/projects/COPYING.WTFPL for more details. |
---|
9 | // |
---|
10 | |
---|
11 | #if defined HAVE_CONFIG_H |
---|
12 | # include "config.h" |
---|
13 | #endif |
---|
14 | |
---|
15 | #include <cstring> |
---|
16 | #include <cstdio> |
---|
17 | |
---|
18 | #ifdef WIN32 |
---|
19 | # define WIN32_LEAN_AND_MEAN |
---|
20 | # include <windows.h> |
---|
21 | # if defined USE_D3D9 |
---|
22 | # include <algorithm> |
---|
23 | using std::min; |
---|
24 | using std::max; |
---|
25 | # include <d3d9.h> |
---|
26 | # include <d3dx9shader.h> |
---|
27 | # endif |
---|
28 | #endif |
---|
29 | |
---|
30 | #include "core.h" |
---|
31 | #include "lolgl.h" |
---|
32 | |
---|
33 | using namespace std; |
---|
34 | |
---|
35 | #if defined USE_D3D9 |
---|
36 | extern IDirect3DDevice9 *g_d3ddevice; |
---|
37 | #elif defined _XBOX |
---|
38 | extern D3DDevice *g_d3ddevice; |
---|
39 | #endif |
---|
40 | |
---|
41 | namespace lol |
---|
42 | { |
---|
43 | |
---|
44 | /* |
---|
45 | * Shader implementation class |
---|
46 | */ |
---|
47 | |
---|
48 | class ShaderData |
---|
49 | { |
---|
50 | friend class Shader; |
---|
51 | |
---|
52 | private: |
---|
53 | #if defined USE_D3D9 |
---|
54 | IDirect3DVertexShader9 *vert_shader; |
---|
55 | IDirect3DPixelShader9 *frag_shader; |
---|
56 | ID3DXConstantTable *vert_table, *frag_table; |
---|
57 | #elif defined _XBOX |
---|
58 | D3DVertexShader *vert_shader; |
---|
59 | D3DPixelShader *frag_shader; |
---|
60 | ID3DXConstantTable *vert_table, *frag_table; |
---|
61 | #elif !defined __CELLOS_LV2__ |
---|
62 | GLuint prog_id, vert_id, frag_id; |
---|
63 | #else |
---|
64 | CGprogram vert_id, frag_id; |
---|
65 | #endif |
---|
66 | uint32_t vert_crc, frag_crc; |
---|
67 | |
---|
68 | /* Shader patcher */ |
---|
69 | static int GetVersion(); |
---|
70 | static void Patch(char *dst, char const *vert, char const *frag); |
---|
71 | |
---|
72 | /* Global shader cache */ |
---|
73 | static Shader *shaders[]; |
---|
74 | static Hash<char const *> hash; |
---|
75 | static int nshaders; |
---|
76 | }; |
---|
77 | |
---|
78 | Shader *ShaderData::shaders[256]; |
---|
79 | Hash<char const *> ShaderData::hash; |
---|
80 | int ShaderData::nshaders = 0; |
---|
81 | |
---|
82 | /* |
---|
83 | * Public Shader class |
---|
84 | */ |
---|
85 | |
---|
86 | Shader *Shader::Create(char const *lolfx) |
---|
87 | { |
---|
88 | char *src = new char[strlen(lolfx) + 2]; |
---|
89 | memcpy(src + 1, lolfx, strlen(lolfx) + 1); |
---|
90 | src[0] = '\n'; |
---|
91 | |
---|
92 | /* Parse the crap */ |
---|
93 | Array<char const *, char const *> sections; |
---|
94 | char *key = NULL; |
---|
95 | for (char *parser = src; *parser; ) |
---|
96 | { |
---|
97 | if (key == NULL && (parser[0] == '\n' || parser[0] == '\r') |
---|
98 | && parser[1] == '[') |
---|
99 | { |
---|
100 | *parser = '\0'; |
---|
101 | parser += 2; |
---|
102 | key = parser; |
---|
103 | } |
---|
104 | else if (key && parser[0] == ']') |
---|
105 | { |
---|
106 | *parser++ = '\0'; |
---|
107 | } |
---|
108 | else if (key && (parser[0] == '\n' || parser[0] == '\r')) |
---|
109 | { |
---|
110 | sections.Push(key, parser); |
---|
111 | parser++; |
---|
112 | key = NULL; |
---|
113 | } |
---|
114 | else |
---|
115 | { |
---|
116 | parser++; |
---|
117 | } |
---|
118 | } |
---|
119 | |
---|
120 | char const *vert = NULL, *frag = NULL; |
---|
121 | for (int i = 0; i < sections.Count(); i++) |
---|
122 | { |
---|
123 | #if !defined __CELLOS_LV2__ && !defined _XBOX && !defined USE_D3D9 |
---|
124 | if (!strcmp(sections[i].m1, "vert.glsl")) |
---|
125 | vert = sections[i].m2; |
---|
126 | if (!strcmp(sections[i].m1, "frag.glsl")) |
---|
127 | frag = sections[i].m2; |
---|
128 | #else |
---|
129 | if (!strcmp(sections[i].m1, "vert.hlsl")) |
---|
130 | vert = sections[i].m2; |
---|
131 | if (!strcmp(sections[i].m1, "frag.hlsl")) |
---|
132 | frag = sections[i].m2; |
---|
133 | #endif |
---|
134 | } |
---|
135 | |
---|
136 | /* FIXME: we don’t know how to handle these yet. */ |
---|
137 | if (!vert) |
---|
138 | Log::Error("no vertex shader found… sorry, I’m gonna crash now.\n"); |
---|
139 | if (!frag) |
---|
140 | Log::Error("no fragment shader found… sorry, I’m gonna crash now.\n"); |
---|
141 | |
---|
142 | uint32_t new_vert_crc = ShaderData::hash(vert); |
---|
143 | uint32_t new_frag_crc = ShaderData::hash(frag); |
---|
144 | |
---|
145 | for (int n = 0; n < ShaderData::nshaders; n++) |
---|
146 | { |
---|
147 | if (ShaderData::shaders[n]->data->vert_crc == new_vert_crc |
---|
148 | && ShaderData::shaders[n]->data->frag_crc == new_frag_crc) |
---|
149 | { |
---|
150 | delete[] src; |
---|
151 | return ShaderData::shaders[n]; |
---|
152 | } |
---|
153 | } |
---|
154 | |
---|
155 | Shader *ret = new Shader(vert, frag); |
---|
156 | ShaderData::shaders[ShaderData::nshaders] = ret; |
---|
157 | ShaderData::nshaders++; |
---|
158 | |
---|
159 | delete[] src; |
---|
160 | return ret; |
---|
161 | } |
---|
162 | |
---|
163 | void Shader::Destroy(Shader *shader) |
---|
164 | { |
---|
165 | /* XXX: do nothing! the shader should remain in cache */ |
---|
166 | (void)shader; |
---|
167 | } |
---|
168 | |
---|
169 | Shader::Shader(char const *vert, char const *frag) |
---|
170 | : data(new ShaderData()) |
---|
171 | { |
---|
172 | #if defined USE_D3D9 || defined _XBOX |
---|
173 | ID3DXBuffer *shader_code, *error_msg; |
---|
174 | HRESULT hr; |
---|
175 | D3DXMACRO macros[] = |
---|
176 | { |
---|
177 | #if defined _XBOX |
---|
178 | { "_XBOX", "1" }, |
---|
179 | #endif |
---|
180 | { NULL, NULL } |
---|
181 | }; |
---|
182 | #elif !defined __CELLOS_LV2__ |
---|
183 | char buf[4096], errbuf[4096]; |
---|
184 | char const *shader = buf; |
---|
185 | GLint status; |
---|
186 | GLsizei len; |
---|
187 | #else |
---|
188 | /* Initialise the runtime shader compiler. FIXME: this needs only |
---|
189 | * to be done once. */ |
---|
190 | cgRTCgcInit(); |
---|
191 | #endif |
---|
192 | |
---|
193 | /* Compile vertex shader */ |
---|
194 | data->vert_crc = ShaderData::hash(vert); |
---|
195 | #if defined USE_D3D9 || defined _XBOX |
---|
196 | hr = D3DXCompileShader(vert, (UINT)strlen(vert), macros, NULL, "main", |
---|
197 | "vs_3_0", 0, &shader_code, &error_msg, |
---|
198 | &data->vert_table); |
---|
199 | if (FAILED(hr)) |
---|
200 | { |
---|
201 | Log::Error("failed to compile vertex shader: %s", |
---|
202 | error_msg ? error_msg->GetBufferPointer() : "error"); |
---|
203 | Log::Error("shader source:\n%s\n", vert); |
---|
204 | } |
---|
205 | g_d3ddevice->CreateVertexShader((DWORD *)shader_code->GetBufferPointer(), |
---|
206 | &data->vert_shader); |
---|
207 | shader_code->Release(); |
---|
208 | #elif !defined __CELLOS_LV2__ |
---|
209 | ShaderData::Patch(buf, vert, NULL); |
---|
210 | data->vert_id = glCreateShader(GL_VERTEX_SHADER); |
---|
211 | glShaderSource(data->vert_id, 1, &shader, NULL); |
---|
212 | glCompileShader(data->vert_id); |
---|
213 | |
---|
214 | glGetShaderInfoLog(data->vert_id, sizeof(errbuf), &len, errbuf); |
---|
215 | glGetShaderiv(data->vert_id, GL_COMPILE_STATUS, &status); |
---|
216 | if (status != GL_TRUE) |
---|
217 | { |
---|
218 | Log::Error("failed to compile vertex shader: %s", errbuf); |
---|
219 | Log::Error("shader source:\n%s\n", buf); |
---|
220 | } |
---|
221 | else if (len > 16) |
---|
222 | { |
---|
223 | Log::Debug("compile log for vertex shader: %s", errbuf); |
---|
224 | Log::Debug("shader source:\n%s\n", buf); |
---|
225 | } |
---|
226 | #else |
---|
227 | data->vert_id = cgCreateProgram(cgCreateContext(), CG_SOURCE, vert, |
---|
228 | cgGLGetLatestProfile(CG_GL_VERTEX), |
---|
229 | NULL, NULL); |
---|
230 | if (data->vert_id == NULL) |
---|
231 | { |
---|
232 | Log::Error("failed to compile vertex shader"); |
---|
233 | Log::Error("shader source:\n%s\n", vert); |
---|
234 | } |
---|
235 | #endif |
---|
236 | |
---|
237 | /* Compile fragment shader */ |
---|
238 | data->frag_crc = ShaderData::hash(frag); |
---|
239 | #if defined USE_D3D9 || defined _XBOX |
---|
240 | hr = D3DXCompileShader(frag, (UINT)strlen(frag), macros, NULL, "main", |
---|
241 | "ps_3_0", 0, &shader_code, &error_msg, |
---|
242 | &data->frag_table); |
---|
243 | if (FAILED(hr)) |
---|
244 | { |
---|
245 | Log::Error("failed to compile fragment shader: %s", |
---|
246 | error_msg ? error_msg->GetBufferPointer() : "error"); |
---|
247 | Log::Error("shader source:\n%s\n", frag); |
---|
248 | } |
---|
249 | g_d3ddevice->CreatePixelShader((DWORD *)shader_code->GetBufferPointer(), |
---|
250 | &data->frag_shader); |
---|
251 | shader_code->Release(); |
---|
252 | #elif !defined __CELLOS_LV2__ |
---|
253 | ShaderData::Patch(buf, NULL, frag); |
---|
254 | data->frag_id = glCreateShader(GL_FRAGMENT_SHADER); |
---|
255 | glShaderSource(data->frag_id, 1, &shader, NULL); |
---|
256 | glCompileShader(data->frag_id); |
---|
257 | |
---|
258 | glGetShaderInfoLog(data->frag_id, sizeof(errbuf), &len, errbuf); |
---|
259 | glGetShaderiv(data->frag_id, GL_COMPILE_STATUS, &status); |
---|
260 | if (status != GL_TRUE) |
---|
261 | { |
---|
262 | Log::Error("failed to compile fragment shader: %s", errbuf); |
---|
263 | Log::Error("shader source:\n%s\n", buf); |
---|
264 | } |
---|
265 | else if (len > 16) |
---|
266 | { |
---|
267 | Log::Debug("compile log for fragment shader: %s", errbuf); |
---|
268 | Log::Debug("shader source:\n%s\n", buf); |
---|
269 | } |
---|
270 | #else |
---|
271 | data->frag_id = cgCreateProgram(cgCreateContext(), CG_SOURCE, frag, |
---|
272 | cgGLGetLatestProfile(CG_GL_FRAGMENT), |
---|
273 | NULL, NULL); |
---|
274 | if (data->frag_id == NULL) |
---|
275 | { |
---|
276 | Log::Error("failed to compile fragment shader"); |
---|
277 | Log::Error("shader source:\n%s\n", frag); |
---|
278 | } |
---|
279 | #endif |
---|
280 | |
---|
281 | #if defined USE_D3D9 || defined _XBOX |
---|
282 | /* FIXME: this is only debug code, we don't need it. */ |
---|
283 | D3DXCONSTANTTABLE_DESC desc; |
---|
284 | data->frag_table->GetDesc(&desc); |
---|
285 | for (int i = 0; i < desc.Constants; i++) |
---|
286 | { |
---|
287 | D3DXCONSTANT_DESC cdesc; |
---|
288 | UINT count = 1; |
---|
289 | D3DXHANDLE h = data->frag_table->GetConstant(NULL, i); |
---|
290 | data->frag_table->GetConstantDesc(h, &cdesc, &count); |
---|
291 | } |
---|
292 | data->vert_table->GetDesc(&desc); |
---|
293 | for (int i = 0; i < desc.Constants; i++) |
---|
294 | { |
---|
295 | D3DXCONSTANT_DESC cdesc; |
---|
296 | UINT count = 1; |
---|
297 | D3DXHANDLE h = data->vert_table->GetConstant(NULL, i); |
---|
298 | data->frag_table->GetConstantDesc(h, &cdesc, &count); |
---|
299 | } |
---|
300 | #elif !defined __CELLOS_LV2__ |
---|
301 | /* Create program */ |
---|
302 | data->prog_id = glCreateProgram(); |
---|
303 | glAttachShader(data->prog_id, data->vert_id); |
---|
304 | glAttachShader(data->prog_id, data->frag_id); |
---|
305 | |
---|
306 | glLinkProgram(data->prog_id); |
---|
307 | glGetProgramInfoLog(data->prog_id, sizeof(errbuf), &len, errbuf); |
---|
308 | glGetProgramiv(data->prog_id, GL_LINK_STATUS, &status); |
---|
309 | if (status != GL_TRUE) |
---|
310 | { |
---|
311 | Log::Error("failed to link program: %s", errbuf); |
---|
312 | } |
---|
313 | else if (len > 16) |
---|
314 | { |
---|
315 | Log::Debug("link log for program: %s", errbuf); |
---|
316 | } |
---|
317 | glValidateProgram(data->prog_id); |
---|
318 | #endif |
---|
319 | } |
---|
320 | |
---|
321 | ShaderAttrib Shader::GetAttribLocation(char const *attr, |
---|
322 | VertexUsage usage, int index) const |
---|
323 | { |
---|
324 | ShaderAttrib ret; |
---|
325 | ret.m_flags = (uint64_t)(uint16_t)usage << 16; |
---|
326 | ret.m_flags |= (uint64_t)(uint16_t)index; |
---|
327 | #if defined USE_D3D9 || defined _XBOX |
---|
328 | #elif !defined __CELLOS_LV2__ |
---|
329 | GLint l = glGetAttribLocation(data->prog_id, attr); |
---|
330 | if (l < 0) |
---|
331 | { |
---|
332 | Log::Warn("tried to query invalid attribute: %s\n", attr); |
---|
333 | l = 0; |
---|
334 | } |
---|
335 | ret.m_flags |= (uint64_t)(uint32_t)l << 32; |
---|
336 | #else |
---|
337 | /* FIXME: can we do this at all on the PS3? */ |
---|
338 | #endif |
---|
339 | return ret; |
---|
340 | } |
---|
341 | |
---|
342 | ShaderUniform Shader::GetUniformLocation(char const *uni) const |
---|
343 | { |
---|
344 | ShaderUniform ret; |
---|
345 | #if defined USE_D3D9 || defined _XBOX |
---|
346 | /* Global variables are prefixed with "$" */ |
---|
347 | char tmpname[128]; |
---|
348 | sprintf(tmpname, "$%s", uni); |
---|
349 | D3DXCONSTANT_DESC cdesc; |
---|
350 | D3DXHANDLE hr; |
---|
351 | UINT count; |
---|
352 | |
---|
353 | count = 0; |
---|
354 | hr = data->frag_table->GetConstantByName(NULL, tmpname); |
---|
355 | if (hr) |
---|
356 | data->frag_table->GetConstantDesc(hr, &cdesc, &count); |
---|
357 | if (count) |
---|
358 | { |
---|
359 | ret.frag = cdesc.RegisterIndex; |
---|
360 | ret.flags |= 1; |
---|
361 | } |
---|
362 | |
---|
363 | count = 0; |
---|
364 | hr = data->vert_table->GetConstantByName(NULL, tmpname); |
---|
365 | if (hr) |
---|
366 | data->vert_table->GetConstantDesc(hr, &cdesc, &count); |
---|
367 | if (count) |
---|
368 | { |
---|
369 | ret.vert = cdesc.RegisterIndex; |
---|
370 | ret.flags |= 2; |
---|
371 | } |
---|
372 | #elif !defined __CELLOS_LV2__ |
---|
373 | ret.frag = (uintptr_t)glGetUniformLocation(data->prog_id, uni); |
---|
374 | ret.vert = 0; |
---|
375 | #else |
---|
376 | ret.frag = (uintptr_t)cgGetNamedParameter(data->frag_id, uni); |
---|
377 | ret.vert = (uintptr_t)cgGetNamedParameter(data->vert_id, uni); |
---|
378 | #endif |
---|
379 | return ret; |
---|
380 | } |
---|
381 | |
---|
382 | void Shader::SetUniform(ShaderUniform const &uni, int i) |
---|
383 | { |
---|
384 | #if defined USE_D3D9 || defined _XBOX |
---|
385 | SetUniform(uni, ivec4(i, 0, 0, 0)); |
---|
386 | #elif !defined __CELLOS_LV2__ |
---|
387 | glUniform1i(uni.frag, i); |
---|
388 | #else |
---|
389 | /* FIXME: does this exist at all? cgGLSetParameter1i doesn't. */ |
---|
390 | #endif |
---|
391 | } |
---|
392 | |
---|
393 | void Shader::SetUniform(ShaderUniform const &uni, ivec2 const &v) |
---|
394 | { |
---|
395 | #if defined USE_D3D9 || defined _XBOX |
---|
396 | SetUniform(uni, ivec4(v, 0, 0)); |
---|
397 | #elif !defined __CELLOS_LV2__ |
---|
398 | glUniform2i(uni.frag, v.x, v.y); |
---|
399 | #else |
---|
400 | /* FIXME: does this exist at all? */ |
---|
401 | #endif |
---|
402 | } |
---|
403 | |
---|
404 | void Shader::SetUniform(ShaderUniform const &uni, ivec3 const &v) |
---|
405 | { |
---|
406 | #if defined USE_D3D9 || defined _XBOX |
---|
407 | SetUniform(uni, ivec4(v, 0)); |
---|
408 | #elif !defined __CELLOS_LV2__ |
---|
409 | glUniform3i(uni.frag, v.x, v.y, v.z); |
---|
410 | #else |
---|
411 | /* FIXME: does this exist at all? */ |
---|
412 | #endif |
---|
413 | } |
---|
414 | |
---|
415 | void Shader::SetUniform(ShaderUniform const &uni, ivec4 const &v) |
---|
416 | { |
---|
417 | #if defined USE_D3D9 || defined _XBOX |
---|
418 | if (uni.flags & 1) |
---|
419 | g_d3ddevice->SetPixelShaderConstantI((UINT)uni.frag, &v[0], 1); |
---|
420 | if (uni.flags & 2) |
---|
421 | g_d3ddevice->SetVertexShaderConstantI((UINT)uni.vert, &v[0], 1); |
---|
422 | #elif !defined __CELLOS_LV2__ |
---|
423 | glUniform4i(uni.frag, v.x, v.y, v.z, v.w); |
---|
424 | #else |
---|
425 | /* FIXME: does this exist at all? */ |
---|
426 | #endif |
---|
427 | } |
---|
428 | |
---|
429 | void Shader::SetUniform(ShaderUniform const &uni, float f) |
---|
430 | { |
---|
431 | #if defined USE_D3D9 || defined _XBOX |
---|
432 | SetUniform(uni, vec4(f, 0, 0, 0)); |
---|
433 | #elif !defined __CELLOS_LV2__ |
---|
434 | glUniform1f(uni.frag, f); |
---|
435 | #else |
---|
436 | if (uni.frag) |
---|
437 | cgGLSetParameter1f((CGparameter)uni.frag, f); |
---|
438 | if (uni.vert) |
---|
439 | cgGLSetParameter1f((CGparameter)uni.vert, f); |
---|
440 | #endif |
---|
441 | } |
---|
442 | |
---|
443 | void Shader::SetUniform(ShaderUniform const &uni, vec2 const &v) |
---|
444 | { |
---|
445 | #if defined USE_D3D9 || defined _XBOX |
---|
446 | SetUniform(uni, vec4(v, 0, 0)); |
---|
447 | #elif !defined __CELLOS_LV2__ |
---|
448 | glUniform2fv(uni.frag, 1, &v[0]); |
---|
449 | #else |
---|
450 | if (uni.frag) |
---|
451 | cgGLSetParameter2fv((CGparameter)uni.frag, &v[0]); |
---|
452 | if (uni.vert) |
---|
453 | cgGLSetParameter2fv((CGparameter)uni.vert, &v[0]); |
---|
454 | #endif |
---|
455 | } |
---|
456 | |
---|
457 | void Shader::SetUniform(ShaderUniform const &uni, vec3 const &v) |
---|
458 | { |
---|
459 | #if defined USE_D3D9 || defined _XBOX |
---|
460 | SetUniform(uni, vec4(v, 0)); |
---|
461 | #elif !defined __CELLOS_LV2__ |
---|
462 | glUniform3fv(uni.frag, 1, &v[0]); |
---|
463 | #else |
---|
464 | if (uni.frag) |
---|
465 | cgGLSetParameter3fv((CGparameter)uni.frag, &v[0]); |
---|
466 | if (uni.vert) |
---|
467 | cgGLSetParameter3fv((CGparameter)uni.vert, &v[0]); |
---|
468 | #endif |
---|
469 | } |
---|
470 | |
---|
471 | void Shader::SetUniform(ShaderUniform const &uni, vec4 const &v) |
---|
472 | { |
---|
473 | #if defined USE_D3D9 || defined _XBOX |
---|
474 | if (uni.flags & 1) |
---|
475 | g_d3ddevice->SetPixelShaderConstantF((UINT)uni.frag, &v[0], 1); |
---|
476 | if (uni.flags & 2) |
---|
477 | g_d3ddevice->SetVertexShaderConstantF((UINT)uni.vert, &v[0], 1); |
---|
478 | #elif !defined __CELLOS_LV2__ |
---|
479 | glUniform4fv(uni.frag, 1, &v[0]); |
---|
480 | #else |
---|
481 | if (uni.frag) |
---|
482 | cgGLSetParameter4fv((CGparameter)uni.frag, &v[0]); |
---|
483 | if (uni.vert) |
---|
484 | cgGLSetParameter4fv((CGparameter)uni.vert, &v[0]); |
---|
485 | #endif |
---|
486 | } |
---|
487 | |
---|
488 | void Shader::SetUniform(ShaderUniform const &uni, mat2 const &m) |
---|
489 | { |
---|
490 | #if defined USE_D3D9 || defined _XBOX |
---|
491 | /* FIXME: do we need padding here like for the mat3 version? */ |
---|
492 | if (uni.flags & 1) |
---|
493 | g_d3ddevice->SetPixelShaderConstantF((UINT)uni.frag, &m[0][0], 1); |
---|
494 | if (uni.flags & 2) |
---|
495 | g_d3ddevice->SetVertexShaderConstantF((UINT)uni.vert, &m[0][0], 1); |
---|
496 | #elif !defined __CELLOS_LV2__ |
---|
497 | glUniformMatrix2fv(uni.frag, 1, GL_FALSE, &m[0][0]); |
---|
498 | #else |
---|
499 | mat4 tmp(m, 1.0f, 1.0f); |
---|
500 | if (uni.frag) |
---|
501 | cgGLSetMatrixParameterfc((CGparameter)uni.frag, &m[0][0]); |
---|
502 | if (uni.vert) |
---|
503 | cgGLSetMatrixParameterfc((CGparameter)uni.vert, &m[0][0]); |
---|
504 | #endif |
---|
505 | } |
---|
506 | |
---|
507 | void Shader::SetUniform(ShaderUniform const &uni, mat3 const &m) |
---|
508 | { |
---|
509 | #if defined USE_D3D9 || defined _XBOX |
---|
510 | /* Padding matrix columns is necessary on DirectX. We need to create |
---|
511 | * a new data structure; a 4×4 matrix will do. */ |
---|
512 | mat4 tmp(m, 1.0f); |
---|
513 | if (uni.flags & 1) |
---|
514 | g_d3ddevice->SetPixelShaderConstantF((UINT)uni.frag, &tmp[0][0], 3); |
---|
515 | if (uni.flags & 2) |
---|
516 | g_d3ddevice->SetVertexShaderConstantF((UINT)uni.vert, &tmp[0][0], 3); |
---|
517 | #elif !defined __CELLOS_LV2__ |
---|
518 | glUniformMatrix3fv(uni.frag, 1, GL_FALSE, &m[0][0]); |
---|
519 | #else |
---|
520 | /* FIXME: check it's the proper way to do this */ |
---|
521 | mat4 tmp(m, 1.0f); |
---|
522 | if (uni.frag) |
---|
523 | cgGLSetMatrixParameterfc((CGparameter)uni.frag, &m[0][0]); |
---|
524 | if (uni.vert) |
---|
525 | cgGLSetMatrixParameterfc((CGparameter)uni.vert, &m[0][0]); |
---|
526 | #endif |
---|
527 | } |
---|
528 | |
---|
529 | void Shader::SetUniform(ShaderUniform const &uni, mat4 const &m) |
---|
530 | { |
---|
531 | #if defined USE_D3D9 || defined _XBOX |
---|
532 | if (uni.flags & 1) |
---|
533 | g_d3ddevice->SetPixelShaderConstantF((UINT)uni.frag, &m[0][0], 4); |
---|
534 | if (uni.flags & 2) |
---|
535 | g_d3ddevice->SetVertexShaderConstantF((UINT)uni.vert, &m[0][0], 4); |
---|
536 | #elif !defined __CELLOS_LV2__ |
---|
537 | glUniformMatrix4fv(uni.frag, 1, GL_FALSE, &m[0][0]); |
---|
538 | #else |
---|
539 | if (uni.frag) |
---|
540 | cgGLSetMatrixParameterfc((CGparameter)uni.frag, &m[0][0]); |
---|
541 | if (uni.vert) |
---|
542 | cgGLSetMatrixParameterfc((CGparameter)uni.vert, &m[0][0]); |
---|
543 | #endif |
---|
544 | } |
---|
545 | |
---|
546 | void Shader::SetUniform(ShaderUniform const &uni, ShaderTexture tex, int index) |
---|
547 | { |
---|
548 | #if defined USE_D3D9 || defined _XBOX |
---|
549 | g_d3ddevice->SetTexture(index, (LPDIRECT3DTEXTURE9)tex.m_flags); |
---|
550 | #elif !defined __CELLOS_LV2__ |
---|
551 | glActiveTexture(GL_TEXTURE0 + index); |
---|
552 | //glEnable(GL_TEXTURE_2D); |
---|
553 | glBindTexture(GL_TEXTURE_2D, (int)tex.m_flags); |
---|
554 | SetUniform(uni, index); |
---|
555 | #else |
---|
556 | /* FIXME: unimplemented */ |
---|
557 | #endif |
---|
558 | } |
---|
559 | |
---|
560 | void Shader::Bind() const |
---|
561 | { |
---|
562 | #if defined USE_D3D9 || defined _XBOX |
---|
563 | HRESULT hr; |
---|
564 | hr = g_d3ddevice->SetVertexShader(data->vert_shader); |
---|
565 | hr = g_d3ddevice->SetPixelShader(data->frag_shader); |
---|
566 | #elif !defined __CELLOS_LV2__ |
---|
567 | glUseProgram(data->prog_id); |
---|
568 | #else |
---|
569 | cgGLEnableProfile(cgGLGetLatestProfile(CG_GL_VERTEX)); |
---|
570 | cgGLBindProgram(data->vert_id); |
---|
571 | cgGLEnableProfile(cgGLGetLatestProfile(CG_GL_FRAGMENT)); |
---|
572 | cgGLBindProgram(data->frag_id); |
---|
573 | #endif |
---|
574 | } |
---|
575 | |
---|
576 | void Shader::Unbind() const |
---|
577 | { |
---|
578 | #if defined USE_D3D9 || defined _XBOX |
---|
579 | HRESULT hr; |
---|
580 | hr = g_d3ddevice->SetVertexShader(NULL); |
---|
581 | hr = g_d3ddevice->SetPixelShader(NULL); |
---|
582 | #elif !defined __CELLOS_LV2__ |
---|
583 | /* FIXME: untested */ |
---|
584 | glUseProgram(0); |
---|
585 | #else |
---|
586 | /* FIXME: untested */ |
---|
587 | cgGLDisableProfile(cgGLGetLatestProfile(CG_GL_VERTEX)); |
---|
588 | cgGLDisableProfile(cgGLGetLatestProfile(CG_GL_FRAGMENT)); |
---|
589 | #endif |
---|
590 | } |
---|
591 | |
---|
592 | Shader::~Shader() |
---|
593 | { |
---|
594 | #if defined USE_D3D9 || defined _XBOX |
---|
595 | data->vert_shader->Release(); |
---|
596 | data->vert_table->Release(); |
---|
597 | data->frag_shader->Release(); |
---|
598 | data->frag_table->Release(); |
---|
599 | #elif !defined __CELLOS_LV2__ |
---|
600 | glDetachShader(data->prog_id, data->vert_id); |
---|
601 | glDetachShader(data->prog_id, data->frag_id); |
---|
602 | glDeleteShader(data->vert_id); |
---|
603 | glDeleteShader(data->frag_id); |
---|
604 | glDeleteProgram(data->prog_id); |
---|
605 | #else |
---|
606 | cgDestroyProgram(data->vert_id); |
---|
607 | cgDestroyProgram(data->frag_id); |
---|
608 | #endif |
---|
609 | delete data; |
---|
610 | } |
---|
611 | |
---|
612 | /* Try to detect shader compiler features */ |
---|
613 | int ShaderData::GetVersion() |
---|
614 | { |
---|
615 | static int version = 0; |
---|
616 | |
---|
617 | #if !defined USE_D3D9 && !defined _XBOX && !defined __CELLOS_LV2__ |
---|
618 | if (!version) |
---|
619 | { |
---|
620 | #if defined HAVE_GLES_2X |
---|
621 | /* GLES 2.x supports #version 100, that's all. */ |
---|
622 | return 100; |
---|
623 | #else |
---|
624 | char buf[4096]; |
---|
625 | GLsizei len; |
---|
626 | |
---|
627 | int id = glCreateShader(GL_VERTEX_SHADER); |
---|
628 | |
---|
629 | /* Can we compile 1.30 shaders? */ |
---|
630 | char const *test130 = |
---|
631 | "#version 130\n" |
---|
632 | "void main() { gl_Position = vec4(0.0, 0.0, 0.0, 0.0); }"; |
---|
633 | glShaderSource(id, 1, &test130, NULL); |
---|
634 | glCompileShader(id); |
---|
635 | glGetShaderInfoLog(id, sizeof(buf), &len, buf); |
---|
636 | if (len <= 0) |
---|
637 | version = 130; |
---|
638 | |
---|
639 | /* If not, can we compile 1.20 shaders? */ |
---|
640 | if (!version) |
---|
641 | { |
---|
642 | char const *test120 = |
---|
643 | "#version 120\n" |
---|
644 | "void main() { gl_Position = vec4(0.0, 0.0, 0.0, 0.0); }"; |
---|
645 | glShaderSource(id, 1, &test120, NULL); |
---|
646 | glCompileShader(id); |
---|
647 | glGetShaderInfoLog(id, sizeof(buf), &len, buf); |
---|
648 | if (len <= 0) |
---|
649 | version = 120; |
---|
650 | } |
---|
651 | |
---|
652 | /* Otherwise, assume we can compile 1.10 shaders. */ |
---|
653 | if (!version) |
---|
654 | version = 110; |
---|
655 | |
---|
656 | glDeleteShader(id); |
---|
657 | #endif |
---|
658 | } |
---|
659 | #endif |
---|
660 | |
---|
661 | return version; |
---|
662 | } |
---|
663 | |
---|
664 | /* Simple shader source patching for old GLSL versions. |
---|
665 | */ |
---|
666 | void ShaderData::Patch(char *dst, char const *vert, char const *frag) |
---|
667 | { |
---|
668 | int ver_driver = GetVersion(); |
---|
669 | |
---|
670 | strcpy(dst, vert ? vert : frag); |
---|
671 | if (ver_driver >= 130) |
---|
672 | return; |
---|
673 | |
---|
674 | int ver_shader = 110; |
---|
675 | char *parser = strstr(dst, "#version"); |
---|
676 | if (parser) |
---|
677 | ver_shader = atoi(parser + strlen("#version")); |
---|
678 | |
---|
679 | /* This is GL ES, we only know version 100. */ |
---|
680 | if (ver_shader > 100 && ver_driver == 100) |
---|
681 | { |
---|
682 | /* FIXME: this isn't elegant but honestly, we don't care, this |
---|
683 | * whole file is going to die soon. */ |
---|
684 | char *p = strstr(dst, "#version"); |
---|
685 | if (p) |
---|
686 | { |
---|
687 | p += 8; |
---|
688 | while (*p == ' ') |
---|
689 | p++; |
---|
690 | if (p[0] == '1' && p[1] && p[2]) |
---|
691 | p[1] = p[2] = '0'; |
---|
692 | } |
---|
693 | } |
---|
694 | |
---|
695 | if (ver_shader > 120 && ver_driver <= 120) |
---|
696 | { |
---|
697 | char const *end = dst + strlen(dst) + 1; |
---|
698 | |
---|
699 | /* Find main() */ |
---|
700 | parser = strstr(dst, "main"); |
---|
701 | if (!parser) return; |
---|
702 | parser = strstr(parser, "("); |
---|
703 | if (!parser) return; |
---|
704 | parser = strstr(parser, ")"); |
---|
705 | if (!parser) return; |
---|
706 | parser = strstr(parser, "{"); |
---|
707 | if (!parser) return; |
---|
708 | char *main = parser + 1; |
---|
709 | |
---|
710 | /* Perform main() replaces */ |
---|
711 | char const * const main_replaces[] = |
---|
712 | { |
---|
713 | #if 0 |
---|
714 | "in vec2 in_Vertex;", "vec2 in_Vertex = gl_Vertex.xy;", |
---|
715 | "in vec3 in_Vertex;", "vec3 in_Vertex = gl_Vertex.xyz;", |
---|
716 | "in vec4 in_Vertex;", "vec4 in_Vertex = gl_Vertex.xyzw;", |
---|
717 | |
---|
718 | "in vec2 in_Color;", "vec2 in_Color = gl_Color.xy;", |
---|
719 | "in vec3 in_Color;", "vec3 in_Color = gl_Color.xyz;", |
---|
720 | "in vec4 in_Color;", "vec4 in_Color = gl_Color.xyzw;", |
---|
721 | |
---|
722 | "in vec2 in_MultiTexCoord0;", |
---|
723 | "vec2 in_MultiTexCoord0 = gl_MultiTexCoord0.xy;", |
---|
724 | "in vec2 in_MultiTexCoord1;", |
---|
725 | "vec2 in_MultiTexCoord1 = gl_MultiTexCoord1.xy;", |
---|
726 | "in vec2 in_MultiTexCoord2;", |
---|
727 | "vec2 in_MultiTexCoord2 = gl_MultiTexCoord2.xy;", |
---|
728 | "in vec2 in_MultiTexCoord3;", |
---|
729 | "vec2 in_MultiTexCoord3 = gl_MultiTexCoord3.xy;", |
---|
730 | "in vec2 in_MultiTexCoord4;", |
---|
731 | "vec2 in_MultiTexCoord4 = gl_MultiTexCoord4.xy;", |
---|
732 | "in vec2 in_MultiTexCoord5;", |
---|
733 | "vec2 in_MultiTexCoord5 = gl_MultiTexCoord5.xy;", |
---|
734 | "in vec2 in_MultiTexCoord6;", |
---|
735 | "vec2 in_MultiTexCoord6 = gl_MultiTexCoord6.xy;", |
---|
736 | "in vec2 in_MultiTexCoord7;", |
---|
737 | "vec2 in_MultiTexCoord7 = gl_MultiTexCoord7.xy;", |
---|
738 | #endif |
---|
739 | |
---|
740 | NULL |
---|
741 | }; |
---|
742 | |
---|
743 | for (char const * const *rep = main_replaces; rep[0]; rep += 2) |
---|
744 | { |
---|
745 | char *match = strstr(dst, rep[0]); |
---|
746 | if (match && match < main) |
---|
747 | { |
---|
748 | size_t l0 = strlen(rep[0]); |
---|
749 | size_t l1 = strlen(rep[1]); |
---|
750 | memmove(main + l1, main, end - main); |
---|
751 | memcpy(main, rep[1], l1); |
---|
752 | memset(match, ' ', l0); |
---|
753 | main += l1; |
---|
754 | end += l1; |
---|
755 | } |
---|
756 | } |
---|
757 | |
---|
758 | /* Perform small replaces */ |
---|
759 | char const * const fast_replaces[] = |
---|
760 | { |
---|
761 | "#version 130", "#version 120", |
---|
762 | "in vec2", vert ? "attribute vec2" : "varying vec2", |
---|
763 | "in vec3", vert ? "attribute vec3" : "varying vec3", |
---|
764 | "in vec4", vert ? "attribute vec4" : "varying vec4", |
---|
765 | "in mat4", vert ? "attribute mat4" : "varying mat4", |
---|
766 | "out vec2", "varying vec2", |
---|
767 | "out vec3", "varying vec3", |
---|
768 | "out vec4", "varying vec4", |
---|
769 | "out mat4", "varying mat4", |
---|
770 | NULL |
---|
771 | }; |
---|
772 | |
---|
773 | for (char const * const *rep = fast_replaces; rep[0]; rep += 2) |
---|
774 | { |
---|
775 | char *match; |
---|
776 | while ((match = strstr(dst, rep[0]))) |
---|
777 | { |
---|
778 | size_t l0 = strlen(rep[0]); |
---|
779 | size_t l1 = strlen(rep[1]); |
---|
780 | |
---|
781 | if (l1 > l0) |
---|
782 | memmove(match + l1, match + l0, (end - match) - l0); |
---|
783 | memcpy(match, rep[1], l1); |
---|
784 | if (l1 < l0) |
---|
785 | memset(match + l0, ' ', l1 - l0); |
---|
786 | end += l1 - l0; |
---|
787 | } |
---|
788 | } |
---|
789 | } |
---|
790 | } |
---|
791 | |
---|
792 | } /* namespace lol */ |
---|
793 | |
---|