push c6bab2db4fc296bd36abf8f7d9cf443d4a73048e
[wine/hacks.git] / dlls / wined3d / glsl_shader.c
blob35adc50f8360bdf78b8b7b1293e9f5b8d30dd91c
1 /*
2 * GLSL pixel and vertex shader implementation
4 * Copyright 2006 Jason Green
5 * Copyright 2006-2007 Henri Verbeet
6 * Copyright 2007-2008 Stefan Dösinger for CodeWeavers
7 * Copyright 2009 Henri Verbeet for CodeWeavers
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 * D3D shader asm has swizzles on source parameters, and write masks for
26 * destination parameters. GLSL uses swizzles for both. The result of this is
27 * that for example "mov dst.xw, src.zyxw" becomes "dst.xw = src.zw" in GLSL.
28 * Ie, to generate a proper GLSL source swizzle, we need to take the D3D write
29 * mask for the destination parameter into account.
32 #include "config.h"
33 #include <limits.h>
34 #include <stdio.h>
35 #include "wined3d_private.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
38 WINE_DECLARE_DEBUG_CHANNEL(d3d_constants);
39 WINE_DECLARE_DEBUG_CHANNEL(d3d_caps);
40 WINE_DECLARE_DEBUG_CHANNEL(d3d);
42 #define GLINFO_LOCATION (*gl_info)
44 #define WINED3D_GLSL_SAMPLE_PROJECTED 0x1
45 #define WINED3D_GLSL_SAMPLE_RECT 0x2
46 #define WINED3D_GLSL_SAMPLE_LOD 0x4
48 typedef struct {
49 char reg_name[150];
50 char mask_str[6];
51 } glsl_dst_param_t;
53 typedef struct {
54 char reg_name[150];
55 char param_str[100];
56 } glsl_src_param_t;
58 typedef struct {
59 const char *name;
60 DWORD coord_mask;
61 } glsl_sample_function_t;
63 enum heap_node_op
65 HEAP_NODE_TRAVERSE_LEFT,
66 HEAP_NODE_TRAVERSE_RIGHT,
67 HEAP_NODE_POP,
70 struct constant_entry
72 unsigned int idx;
73 unsigned int version;
76 struct constant_heap
78 struct constant_entry *entries;
79 unsigned int *positions;
80 unsigned int size;
83 /* GLSL shader private data */
84 struct shader_glsl_priv {
85 struct hash_table_t *glsl_program_lookup;
86 struct glsl_shader_prog_link *glsl_program;
87 struct constant_heap vconst_heap;
88 struct constant_heap pconst_heap;
89 unsigned char *stack;
90 GLhandleARB depth_blt_program[tex_type_count];
91 UINT next_constant_version;
94 /* Struct to maintain data about a linked GLSL program */
95 struct glsl_shader_prog_link {
96 struct list vshader_entry;
97 struct list pshader_entry;
98 GLhandleARB programId;
99 GLint *vuniformF_locations;
100 GLint *puniformF_locations;
101 GLint vuniformI_locations[MAX_CONST_I];
102 GLint puniformI_locations[MAX_CONST_I];
103 GLint posFixup_location;
104 GLint np2Fixup_location[MAX_FRAGMENT_SAMPLERS];
105 GLint bumpenvmat_location[MAX_TEXTURES];
106 GLint luminancescale_location[MAX_TEXTURES];
107 GLint luminanceoffset_location[MAX_TEXTURES];
108 GLint ycorrection_location;
109 GLenum vertex_color_clamp;
110 IWineD3DVertexShader *vshader;
111 IWineD3DPixelShader *pshader;
112 struct vs_compile_args vs_args;
113 struct ps_compile_args ps_args;
114 UINT constant_version;
117 typedef struct {
118 IWineD3DVertexShader *vshader;
119 IWineD3DPixelShader *pshader;
120 struct ps_compile_args ps_args;
121 struct vs_compile_args vs_args;
122 } glsl_program_key_t;
125 /** Prints the GLSL info log which will contain error messages if they exist */
126 static void print_glsl_info_log(const WineD3D_GL_Info *gl_info, GLhandleARB obj)
128 int infologLength = 0;
129 char *infoLog;
130 unsigned int i;
131 BOOL is_spam;
133 static const char * const spam[] =
135 "Vertex shader was successfully compiled to run on hardware.\n", /* fglrx */
136 "Fragment shader was successfully compiled to run on hardware.\n", /* fglrx */
137 "Fragment shader(s) linked, vertex shader(s) linked. \n ", /* fglrx, with \n */
138 "Fragment shader(s) linked, vertex shader(s) linked.", /* fglrx, no \n */
139 "Vertex shader(s) linked, no fragment shader(s) defined. \n ", /* fglrx, with \n */
140 "Vertex shader(s) linked, no fragment shader(s) defined.", /* fglrx, no \n */
141 "Fragment shader was successfully compiled to run on hardware.\n"
142 "WARNING: 0:2: extension 'GL_ARB_draw_buffers' is not supported",
143 "Fragment shader(s) linked, no vertex shader(s) defined.", /* fglrx, no \n */
144 "Fragment shader(s) linked, no vertex shader(s) defined. \n ", /* fglrx, with \n */
145 "WARNING: 0:2: extension 'GL_ARB_draw_buffers' is not supported\n" /* MacOS ati */
148 if (!TRACE_ON(d3d_shader) && !FIXME_ON(d3d_shader)) return;
150 GL_EXTCALL(glGetObjectParameterivARB(obj,
151 GL_OBJECT_INFO_LOG_LENGTH_ARB,
152 &infologLength));
154 /* A size of 1 is just a null-terminated string, so the log should be bigger than
155 * that if there are errors. */
156 if (infologLength > 1)
158 /* Fglrx doesn't terminate the string properly, but it tells us the proper length.
159 * So use HEAP_ZERO_MEMORY to avoid uninitialized bytes
161 infoLog = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, infologLength);
162 GL_EXTCALL(glGetInfoLogARB(obj, infologLength, NULL, infoLog));
163 is_spam = FALSE;
165 for(i = 0; i < sizeof(spam) / sizeof(spam[0]); i++) {
166 if(strcmp(infoLog, spam[i]) == 0) {
167 is_spam = TRUE;
168 break;
171 if(is_spam) {
172 TRACE("Spam received from GLSL shader #%u: %s\n", obj, debugstr_a(infoLog));
173 } else {
174 FIXME("Error received from GLSL shader #%u: %s\n", obj, debugstr_a(infoLog));
176 HeapFree(GetProcessHeap(), 0, infoLog);
181 * Loads (pixel shader) samplers
183 static void shader_glsl_load_psamplers(const WineD3D_GL_Info *gl_info, DWORD *tex_unit_map, GLhandleARB programId)
185 GLint name_loc;
186 int i;
187 char sampler_name[20];
189 for (i = 0; i < MAX_FRAGMENT_SAMPLERS; ++i) {
190 snprintf(sampler_name, sizeof(sampler_name), "Psampler%d", i);
191 name_loc = GL_EXTCALL(glGetUniformLocationARB(programId, sampler_name));
192 if (name_loc != -1) {
193 DWORD mapped_unit = tex_unit_map[i];
194 if (mapped_unit != WINED3D_UNMAPPED_STAGE && mapped_unit < GL_LIMITS(fragment_samplers))
196 TRACE("Loading %s for texture %d\n", sampler_name, mapped_unit);
197 GL_EXTCALL(glUniform1iARB(name_loc, mapped_unit));
198 checkGLcall("glUniform1iARB");
199 } else {
200 ERR("Trying to load sampler %s on unsupported unit %d\n", sampler_name, mapped_unit);
206 static void shader_glsl_load_vsamplers(const WineD3D_GL_Info *gl_info, DWORD *tex_unit_map, GLhandleARB programId)
208 GLint name_loc;
209 char sampler_name[20];
210 int i;
212 for (i = 0; i < MAX_VERTEX_SAMPLERS; ++i) {
213 snprintf(sampler_name, sizeof(sampler_name), "Vsampler%d", i);
214 name_loc = GL_EXTCALL(glGetUniformLocationARB(programId, sampler_name));
215 if (name_loc != -1) {
216 DWORD mapped_unit = tex_unit_map[MAX_FRAGMENT_SAMPLERS + i];
217 if (mapped_unit != WINED3D_UNMAPPED_STAGE && mapped_unit < GL_LIMITS(combined_samplers))
219 TRACE("Loading %s for texture %d\n", sampler_name, mapped_unit);
220 GL_EXTCALL(glUniform1iARB(name_loc, mapped_unit));
221 checkGLcall("glUniform1iARB");
222 } else {
223 ERR("Trying to load sampler %s on unsupported unit %d\n", sampler_name, mapped_unit);
229 static inline void walk_constant_heap(const WineD3D_GL_Info *gl_info, const float *constants,
230 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
232 int stack_idx = 0;
233 unsigned int heap_idx = 1;
234 unsigned int idx;
236 if (heap->entries[heap_idx].version <= version) return;
238 idx = heap->entries[heap_idx].idx;
239 if (constant_locations[idx] != -1) GL_EXTCALL(glUniform4fvARB(constant_locations[idx], 1, &constants[idx * 4]));
240 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
242 while (stack_idx >= 0)
244 /* Note that we fall through to the next case statement. */
245 switch(stack[stack_idx])
247 case HEAP_NODE_TRAVERSE_LEFT:
249 unsigned int left_idx = heap_idx << 1;
250 if (left_idx < heap->size && heap->entries[left_idx].version > version)
252 heap_idx = left_idx;
253 idx = heap->entries[heap_idx].idx;
254 if (constant_locations[idx] != -1)
255 GL_EXTCALL(glUniform4fvARB(constant_locations[idx], 1, &constants[idx * 4]));
257 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
258 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
259 break;
263 case HEAP_NODE_TRAVERSE_RIGHT:
265 unsigned int right_idx = (heap_idx << 1) + 1;
266 if (right_idx < heap->size && heap->entries[right_idx].version > version)
268 heap_idx = right_idx;
269 idx = heap->entries[heap_idx].idx;
270 if (constant_locations[idx] != -1)
271 GL_EXTCALL(glUniform4fvARB(constant_locations[idx], 1, &constants[idx * 4]));
273 stack[stack_idx++] = HEAP_NODE_POP;
274 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
275 break;
279 case HEAP_NODE_POP:
281 heap_idx >>= 1;
282 --stack_idx;
283 break;
287 checkGLcall("walk_constant_heap()");
290 static inline void apply_clamped_constant(const WineD3D_GL_Info *gl_info, GLint location, const GLfloat *data)
292 GLfloat clamped_constant[4];
294 if (location == -1) return;
296 clamped_constant[0] = data[0] < -1.0f ? -1.0f : data[0] > 1.0 ? 1.0 : data[0];
297 clamped_constant[1] = data[1] < -1.0f ? -1.0f : data[1] > 1.0 ? 1.0 : data[1];
298 clamped_constant[2] = data[2] < -1.0f ? -1.0f : data[2] > 1.0 ? 1.0 : data[2];
299 clamped_constant[3] = data[3] < -1.0f ? -1.0f : data[3] > 1.0 ? 1.0 : data[3];
301 GL_EXTCALL(glUniform4fvARB(location, 1, clamped_constant));
304 static inline void walk_constant_heap_clamped(const WineD3D_GL_Info *gl_info, const float *constants,
305 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
307 int stack_idx = 0;
308 unsigned int heap_idx = 1;
309 unsigned int idx;
311 if (heap->entries[heap_idx].version <= version) return;
313 idx = heap->entries[heap_idx].idx;
314 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
315 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
317 while (stack_idx >= 0)
319 /* Note that we fall through to the next case statement. */
320 switch(stack[stack_idx])
322 case HEAP_NODE_TRAVERSE_LEFT:
324 unsigned int left_idx = heap_idx << 1;
325 if (left_idx < heap->size && heap->entries[left_idx].version > version)
327 heap_idx = left_idx;
328 idx = heap->entries[heap_idx].idx;
329 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
331 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
332 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
333 break;
337 case HEAP_NODE_TRAVERSE_RIGHT:
339 unsigned int right_idx = (heap_idx << 1) + 1;
340 if (right_idx < heap->size && heap->entries[right_idx].version > version)
342 heap_idx = right_idx;
343 idx = heap->entries[heap_idx].idx;
344 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
346 stack[stack_idx++] = HEAP_NODE_POP;
347 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
348 break;
352 case HEAP_NODE_POP:
354 heap_idx >>= 1;
355 --stack_idx;
356 break;
360 checkGLcall("walk_constant_heap_clamped()");
363 /* Loads floating point constants (aka uniforms) into the currently set GLSL program. */
364 static void shader_glsl_load_constantsF(IWineD3DBaseShaderImpl *This, const WineD3D_GL_Info *gl_info,
365 const float *constants, const GLint *constant_locations, const struct constant_heap *heap,
366 unsigned char *stack, UINT version)
368 const local_constant *lconst;
370 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
371 if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.reg_maps.shader_version) == 1
372 && shader_is_pshader_version(This->baseShader.reg_maps.shader_version))
373 walk_constant_heap_clamped(gl_info, constants, constant_locations, heap, stack, version);
374 else
375 walk_constant_heap(gl_info, constants, constant_locations, heap, stack, version);
377 if (!This->baseShader.load_local_constsF)
379 TRACE("No need to load local float constants for this shader\n");
380 return;
383 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
384 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry)
386 GLint location = constant_locations[lconst->idx];
387 /* We found this uniform name in the program - go ahead and send the data */
388 if (location != -1) GL_EXTCALL(glUniform4fvARB(location, 1, (const GLfloat *)lconst->value));
390 checkGLcall("glUniform4fvARB()");
393 /* Loads integer constants (aka uniforms) into the currently set GLSL program. */
394 static void shader_glsl_load_constantsI(IWineD3DBaseShaderImpl *This, const WineD3D_GL_Info *gl_info,
395 const GLint locations[MAX_CONST_I], const int *constants, WORD constants_set)
397 unsigned int i;
398 struct list* ptr;
400 for (i = 0; constants_set; constants_set >>= 1, ++i)
402 if (!(constants_set & 1)) continue;
404 TRACE_(d3d_constants)("Loading constants %u: %i, %i, %i, %i\n",
405 i, constants[i*4], constants[i*4+1], constants[i*4+2], constants[i*4+3]);
407 /* We found this uniform name in the program - go ahead and send the data */
408 GL_EXTCALL(glUniform4ivARB(locations[i], 1, &constants[i*4]));
409 checkGLcall("glUniform4ivARB");
412 /* Load immediate constants */
413 ptr = list_head(&This->baseShader.constantsI);
414 while (ptr) {
415 const struct local_constant *lconst = LIST_ENTRY(ptr, const struct local_constant, entry);
416 unsigned int idx = lconst->idx;
417 const GLint *values = (const GLint *)lconst->value;
419 TRACE_(d3d_constants)("Loading local constants %i: %i, %i, %i, %i\n", idx,
420 values[0], values[1], values[2], values[3]);
422 /* We found this uniform name in the program - go ahead and send the data */
423 GL_EXTCALL(glUniform4ivARB(locations[idx], 1, values));
424 checkGLcall("glUniform4ivARB");
425 ptr = list_next(&This->baseShader.constantsI, ptr);
429 /* Loads boolean constants (aka uniforms) into the currently set GLSL program. */
430 static void shader_glsl_load_constantsB(IWineD3DBaseShaderImpl *This, const WineD3D_GL_Info *gl_info,
431 GLhandleARB programId, const BOOL *constants, WORD constants_set)
433 GLint tmp_loc;
434 unsigned int i;
435 char tmp_name[8];
436 char is_pshader = shader_is_pshader_version(This->baseShader.reg_maps.shader_version);
437 const char* prefix = is_pshader? "PB":"VB";
438 struct list* ptr;
440 /* TODO: Benchmark and see if it would be beneficial to store the
441 * locations of the constants to avoid looking up each time */
442 for (i = 0; constants_set; constants_set >>= 1, ++i)
444 if (!(constants_set & 1)) continue;
446 TRACE_(d3d_constants)("Loading constants %i: %i;\n", i, constants[i]);
448 /* TODO: Benchmark and see if it would be beneficial to store the
449 * locations of the constants to avoid looking up each time */
450 snprintf(tmp_name, sizeof(tmp_name), "%s[%i]", prefix, i);
451 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
452 if (tmp_loc != -1)
454 /* We found this uniform name in the program - go ahead and send the data */
455 GL_EXTCALL(glUniform1ivARB(tmp_loc, 1, &constants[i]));
456 checkGLcall("glUniform1ivARB");
460 /* Load immediate constants */
461 ptr = list_head(&This->baseShader.constantsB);
462 while (ptr) {
463 const struct local_constant *lconst = LIST_ENTRY(ptr, const struct local_constant, entry);
464 unsigned int idx = lconst->idx;
465 const GLint *values = (const GLint *)lconst->value;
467 TRACE_(d3d_constants)("Loading local constants %i: %i\n", idx, values[0]);
469 snprintf(tmp_name, sizeof(tmp_name), "%s[%i]", prefix, idx);
470 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
471 if (tmp_loc != -1) {
472 /* We found this uniform name in the program - go ahead and send the data */
473 GL_EXTCALL(glUniform1ivARB(tmp_loc, 1, values));
474 checkGLcall("glUniform1ivARB");
476 ptr = list_next(&This->baseShader.constantsB, ptr);
480 static void reset_program_constant_version(void *value, void *context)
482 struct glsl_shader_prog_link *entry = value;
483 entry->constant_version = 0;
487 * Loads the texture dimensions for NP2 fixup into the currently set GLSL program.
489 static void shader_glsl_load_np2fixup_constants(
490 IWineD3DDevice* device,
491 char usePixelShader,
492 char useVertexShader) {
494 const IWineD3DDeviceImpl* deviceImpl = (const IWineD3DDeviceImpl*) device;
495 const struct glsl_shader_prog_link* prog = ((struct shader_glsl_priv *)(deviceImpl->shader_priv))->glsl_program;
497 if (!prog) {
498 /* No GLSL program set - nothing to do. */
499 return;
502 if (!usePixelShader) {
503 /* NP2 texcoord fixup is (currently) only done for pixelshaders. */
504 return;
507 if (prog->ps_args.np2_fixup) {
508 UINT i;
509 UINT fixup = prog->ps_args.np2_fixup;
510 const WineD3D_GL_Info* gl_info = &deviceImpl->adapter->gl_info;
511 const IWineD3DStateBlockImpl* stateBlock = (const IWineD3DStateBlockImpl*) deviceImpl->stateBlock;
513 for (i = 0; fixup; fixup >>= 1, ++i) {
514 if (-1 != prog->np2Fixup_location[i]) {
515 const IWineD3DBaseTextureImpl* const tex = (const IWineD3DBaseTextureImpl*) stateBlock->textures[i];
516 if (!tex) {
517 FIXME("Nonexistent texture is flagged for NP2 texcoord fixup\n");
518 continue;
519 } else {
520 const float tex_dim[2] = {tex->baseTexture.pow2Matrix[0], tex->baseTexture.pow2Matrix[5]};
521 GL_EXTCALL(glUniform2fvARB(prog->np2Fixup_location[i], 1, tex_dim));
529 * Loads the app-supplied constants into the currently set GLSL program.
531 static void shader_glsl_load_constants(
532 IWineD3DDevice* device,
533 char usePixelShader,
534 char useVertexShader) {
536 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) device;
537 struct shader_glsl_priv *priv = deviceImpl->shader_priv;
538 IWineD3DStateBlockImpl* stateBlock = deviceImpl->stateBlock;
539 const WineD3D_GL_Info *gl_info = &deviceImpl->adapter->gl_info;
541 GLhandleARB programId;
542 struct glsl_shader_prog_link *prog = priv->glsl_program;
543 UINT constant_version;
544 int i;
546 if (!prog) {
547 /* No GLSL program set - nothing to do. */
548 return;
550 programId = prog->programId;
551 constant_version = prog->constant_version;
553 if (useVertexShader) {
554 IWineD3DBaseShaderImpl* vshader = (IWineD3DBaseShaderImpl*) stateBlock->vertexShader;
556 /* Load DirectX 9 float constants/uniforms for vertex shader */
557 shader_glsl_load_constantsF(vshader, gl_info, stateBlock->vertexShaderConstantF,
558 prog->vuniformF_locations, &priv->vconst_heap, priv->stack, constant_version);
560 /* Load DirectX 9 integer constants/uniforms for vertex shader */
561 if(vshader->baseShader.uses_int_consts) {
562 shader_glsl_load_constantsI(vshader, gl_info, prog->vuniformI_locations,
563 stateBlock->vertexShaderConstantI, stateBlock->changed.vertexShaderConstantsI);
566 /* Load DirectX 9 boolean constants/uniforms for vertex shader */
567 if(vshader->baseShader.uses_bool_consts) {
568 shader_glsl_load_constantsB(vshader, gl_info, programId,
569 stateBlock->vertexShaderConstantB, stateBlock->changed.vertexShaderConstantsB);
572 /* Upload the position fixup params */
573 GL_EXTCALL(glUniform4fvARB(prog->posFixup_location, 1, &deviceImpl->posFixup[0]));
574 checkGLcall("glUniform4fvARB");
577 if (usePixelShader) {
579 IWineD3DBaseShaderImpl* pshader = (IWineD3DBaseShaderImpl*) stateBlock->pixelShader;
581 /* Load DirectX 9 float constants/uniforms for pixel shader */
582 shader_glsl_load_constantsF(pshader, gl_info, stateBlock->pixelShaderConstantF,
583 prog->puniformF_locations, &priv->pconst_heap, priv->stack, constant_version);
585 /* Load DirectX 9 integer constants/uniforms for pixel shader */
586 if(pshader->baseShader.uses_int_consts) {
587 shader_glsl_load_constantsI(pshader, gl_info, prog->puniformI_locations,
588 stateBlock->pixelShaderConstantI, stateBlock->changed.pixelShaderConstantsI);
591 /* Load DirectX 9 boolean constants/uniforms for pixel shader */
592 if(pshader->baseShader.uses_bool_consts) {
593 shader_glsl_load_constantsB(pshader, gl_info, programId,
594 stateBlock->pixelShaderConstantB, stateBlock->changed.pixelShaderConstantsB);
597 /* Upload the environment bump map matrix if needed. The needsbumpmat member specifies the texture stage to load the matrix from.
598 * It can't be 0 for a valid texbem instruction.
600 for(i = 0; i < ((IWineD3DPixelShaderImpl *) pshader)->numbumpenvmatconsts; i++) {
601 IWineD3DPixelShaderImpl *ps = (IWineD3DPixelShaderImpl *) pshader;
602 int stage = ps->luminanceconst[i].texunit;
604 const float *data = (const float *)&stateBlock->textureState[(int)ps->bumpenvmatconst[i].texunit][WINED3DTSS_BUMPENVMAT00];
605 GL_EXTCALL(glUniformMatrix2fvARB(prog->bumpenvmat_location[i], 1, 0, data));
606 checkGLcall("glUniformMatrix2fvARB");
608 /* texbeml needs the luminance scale and offset too. If texbeml is used, needsbumpmat
609 * is set too, so we can check that in the needsbumpmat check
611 if(ps->baseShader.reg_maps.luminanceparams[stage]) {
612 const GLfloat *scale = (const GLfloat *)&stateBlock->textureState[stage][WINED3DTSS_BUMPENVLSCALE];
613 const GLfloat *offset = (const GLfloat *)&stateBlock->textureState[stage][WINED3DTSS_BUMPENVLOFFSET];
615 GL_EXTCALL(glUniform1fvARB(prog->luminancescale_location[i], 1, scale));
616 checkGLcall("glUniform1fvARB");
617 GL_EXTCALL(glUniform1fvARB(prog->luminanceoffset_location[i], 1, offset));
618 checkGLcall("glUniform1fvARB");
622 if(((IWineD3DPixelShaderImpl *) pshader)->vpos_uniform) {
623 float correction_params[4];
624 if(deviceImpl->render_offscreen) {
625 correction_params[0] = 0.0;
626 correction_params[1] = 1.0;
627 } else {
628 /* position is window relative, not viewport relative */
629 correction_params[0] = ((IWineD3DSurfaceImpl *) deviceImpl->render_targets[0])->currentDesc.Height;
630 correction_params[1] = -1.0;
632 GL_EXTCALL(glUniform4fvARB(prog->ycorrection_location, 1, correction_params));
636 if (priv->next_constant_version == UINT_MAX)
638 TRACE("Max constant version reached, resetting to 0.\n");
639 hash_table_for_each_entry(priv->glsl_program_lookup, reset_program_constant_version, NULL);
640 priv->next_constant_version = 1;
642 else
644 prog->constant_version = priv->next_constant_version++;
648 static inline void update_heap_entry(struct constant_heap *heap, unsigned int idx,
649 unsigned int heap_idx, DWORD new_version)
651 struct constant_entry *entries = heap->entries;
652 unsigned int *positions = heap->positions;
653 unsigned int parent_idx;
655 while (heap_idx > 1)
657 parent_idx = heap_idx >> 1;
659 if (new_version <= entries[parent_idx].version) break;
661 entries[heap_idx] = entries[parent_idx];
662 positions[entries[parent_idx].idx] = heap_idx;
663 heap_idx = parent_idx;
666 entries[heap_idx].version = new_version;
667 entries[heap_idx].idx = idx;
668 positions[idx] = heap_idx;
671 static void shader_glsl_update_float_vertex_constants(IWineD3DDevice *iface, UINT start, UINT count)
673 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
674 struct shader_glsl_priv *priv = This->shader_priv;
675 struct constant_heap *heap = &priv->vconst_heap;
676 UINT i;
678 for (i = start; i < count + start; ++i)
680 if (!This->stateBlock->changed.vertexShaderConstantsF[i])
681 update_heap_entry(heap, i, heap->size++, priv->next_constant_version);
682 else
683 update_heap_entry(heap, i, heap->positions[i], priv->next_constant_version);
687 static void shader_glsl_update_float_pixel_constants(IWineD3DDevice *iface, UINT start, UINT count)
689 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
690 struct shader_glsl_priv *priv = This->shader_priv;
691 struct constant_heap *heap = &priv->pconst_heap;
692 UINT i;
694 for (i = start; i < count + start; ++i)
696 if (!This->stateBlock->changed.pixelShaderConstantsF[i])
697 update_heap_entry(heap, i, heap->size++, priv->next_constant_version);
698 else
699 update_heap_entry(heap, i, heap->positions[i], priv->next_constant_version);
703 /** Generate the variable & register declarations for the GLSL output target */
704 static void shader_generate_glsl_declarations(IWineD3DBaseShader *iface, const shader_reg_maps *reg_maps,
705 SHADER_BUFFER *buffer, const WineD3D_GL_Info *gl_info,
706 const struct ps_compile_args *ps_args)
708 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
709 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) This->baseShader.device;
710 DWORD shader_version = reg_maps->shader_version;
711 unsigned int i, extra_constants_needed = 0;
712 const local_constant *lconst;
714 /* There are some minor differences between pixel and vertex shaders */
715 char pshader = shader_is_pshader_version(shader_version);
716 char prefix = pshader ? 'P' : 'V';
718 /* Prototype the subroutines */
719 for (i = 0; i < This->baseShader.limits.label; i++) {
720 if (reg_maps->labels[i])
721 shader_addline(buffer, "void subroutine%u();\n", i);
724 /* Declare the constants (aka uniforms) */
725 if (This->baseShader.limits.constant_float > 0) {
726 unsigned max_constantsF;
727 if(pshader) {
728 max_constantsF = GL_LIMITS(pshader_constantsF) - (MAX_CONST_B / 4) - MAX_CONST_I - 2;
729 max_constantsF = min(This->baseShader.limits.constant_float, max_constantsF);
730 } else {
731 /* Subtract the other potential uniforms from the max available (bools, ints, and 1 row of projection matrix) */
732 max_constantsF = GL_LIMITS(vshader_constantsF) - (MAX_CONST_B / 4) - MAX_CONST_I - 1;
733 max_constantsF = min(This->baseShader.limits.constant_float, max_constantsF);
735 shader_addline(buffer, "uniform vec4 %cC[%u];\n", prefix, max_constantsF);
738 if (This->baseShader.limits.constant_int > 0)
739 shader_addline(buffer, "uniform ivec4 %cI[%u];\n", prefix, This->baseShader.limits.constant_int);
741 if (This->baseShader.limits.constant_bool > 0)
742 shader_addline(buffer, "uniform bool %cB[%u];\n", prefix, This->baseShader.limits.constant_bool);
744 if(!pshader) {
745 shader_addline(buffer, "uniform vec4 posFixup;\n");
746 /* Predeclaration; This function is added at link time based on the pixel shader.
747 * VS 3.0 shaders have an array OUT[] the shader writes to, earlier versions don't have
748 * that. We know the input to the reorder function at vertex shader compile time, so
749 * we can deal with that. The reorder function for a 1.x and 2.x vertex shader can just
750 * read gl_FrontColor. The output depends on the pixel shader. The reorder function for a
751 * 1.x and 2.x pshader or for fixed function will write gl_FrontColor, and for a 3.0 shader
752 * it will write to the varying array. Here we depend on the shader optimizer on sorting that
753 * out. The nvidia driver only does that if the parameter is inout instead of out, hence the
754 * inout.
756 if (shader_version >= WINED3DVS_VERSION(3, 0))
758 shader_addline(buffer, "void order_ps_input(in vec4[%u]);\n", MAX_REG_OUTPUT);
759 } else {
760 shader_addline(buffer, "void order_ps_input();\n");
762 } else {
763 IWineD3DPixelShaderImpl *ps_impl = (IWineD3DPixelShaderImpl *) This;
765 ps_impl->numbumpenvmatconsts = 0;
766 for(i = 0; i < (sizeof(reg_maps->bumpmat) / sizeof(reg_maps->bumpmat[0])); i++) {
767 if(!reg_maps->bumpmat[i]) {
768 continue;
771 ps_impl->bumpenvmatconst[(int) ps_impl->numbumpenvmatconsts].texunit = i;
772 shader_addline(buffer, "uniform mat2 bumpenvmat%d;\n", i);
774 if(reg_maps->luminanceparams) {
775 ps_impl->luminanceconst[(int) ps_impl->numbumpenvmatconsts].texunit = i;
776 shader_addline(buffer, "uniform float luminancescale%d;\n", i);
777 shader_addline(buffer, "uniform float luminanceoffset%d;\n", i);
778 extra_constants_needed++;
779 } else {
780 ps_impl->luminanceconst[(int) ps_impl->numbumpenvmatconsts].texunit = -1;
783 extra_constants_needed++;
784 ps_impl->numbumpenvmatconsts++;
787 if(ps_args->srgb_correction) {
788 shader_addline(buffer, "const vec4 srgb_mul_low = vec4(%f, %f, %f, %f);\n",
789 srgb_mul_low, srgb_mul_low, srgb_mul_low, srgb_mul_low);
790 shader_addline(buffer, "const vec4 srgb_comparison = vec4(%f, %f, %f, %f);\n",
791 srgb_cmp, srgb_cmp, srgb_cmp, srgb_cmp);
793 if(reg_maps->vpos || reg_maps->usesdsy) {
794 if(This->baseShader.limits.constant_float + extra_constants_needed + 1 < GL_LIMITS(pshader_constantsF)) {
795 shader_addline(buffer, "uniform vec4 ycorrection;\n");
796 ((IWineD3DPixelShaderImpl *) This)->vpos_uniform = 1;
797 extra_constants_needed++;
798 } else {
799 /* This happens because we do not have proper tracking of the constant registers that are
800 * actually used, only the max limit of the shader version
802 FIXME("Cannot find a free uniform for vpos correction params\n");
803 shader_addline(buffer, "const vec4 ycorrection = vec4(%f, %f, 0.0, 0.0);\n",
804 device->render_offscreen ? 0.0 : ((IWineD3DSurfaceImpl *) device->render_targets[0])->currentDesc.Height,
805 device->render_offscreen ? 1.0 : -1.0);
807 shader_addline(buffer, "vec4 vpos;\n");
811 /* Declare texture samplers */
812 for (i = 0; i < This->baseShader.limits.sampler; i++) {
813 if (reg_maps->samplers[i]) {
815 DWORD stype = reg_maps->samplers[i] & WINED3DSP_TEXTURETYPE_MASK;
816 switch (stype) {
818 case WINED3DSTT_1D:
819 shader_addline(buffer, "uniform sampler1D %csampler%u;\n", prefix, i);
820 break;
821 case WINED3DSTT_2D:
822 if(device->stateBlock->textures[i] &&
823 IWineD3DBaseTexture_GetTextureDimensions(device->stateBlock->textures[i]) == GL_TEXTURE_RECTANGLE_ARB) {
824 shader_addline(buffer, "uniform sampler2DRect %csampler%u;\n", prefix, i);
825 } else {
826 shader_addline(buffer, "uniform sampler2D %csampler%u;\n", prefix, i);
829 if (pshader && ps_args->np2_fixup & (1 << i))
831 /* NP2/RECT textures in OpenGL use texcoords in the range [0,width]x[0,height]
832 * while D3D has them in the (normalized) [0,1]x[0,1] range.
833 * samplerNP2Fixup stores texture dimensions and is updated through
834 * shader_glsl_load_np2fixup_constants when the sampler changes. */
835 shader_addline(buffer, "uniform vec2 %csamplerNP2Fixup%u;\n", prefix, i);
837 break;
838 case WINED3DSTT_CUBE:
839 shader_addline(buffer, "uniform samplerCube %csampler%u;\n", prefix, i);
840 break;
841 case WINED3DSTT_VOLUME:
842 shader_addline(buffer, "uniform sampler3D %csampler%u;\n", prefix, i);
843 break;
844 default:
845 shader_addline(buffer, "uniform unsupported_sampler %csampler%u;\n", prefix, i);
846 FIXME("Unrecognized sampler type: %#x\n", stype);
847 break;
852 /* Declare address variables */
853 for (i = 0; i < This->baseShader.limits.address; i++) {
854 if (reg_maps->address[i])
855 shader_addline(buffer, "ivec4 A%d;\n", i);
858 /* Declare texture coordinate temporaries and initialize them */
859 for (i = 0; i < This->baseShader.limits.texcoord; i++) {
860 if (reg_maps->texcoord[i])
861 shader_addline(buffer, "vec4 T%u = gl_TexCoord[%u];\n", i, i);
864 /* Declare input register varyings. Only pixel shader, vertex shaders have that declared in the
865 * helper function shader that is linked in at link time
867 if (pshader && shader_version >= WINED3DPS_VERSION(3, 0))
869 if (use_vs(device->stateBlock))
871 shader_addline(buffer, "varying vec4 IN[%u];\n", GL_LIMITS(glsl_varyings) / 4);
872 } else {
873 /* TODO: Write a replacement shader for the fixed function vertex pipeline, so this isn't needed.
874 * For fixed function vertex processing + 3.0 pixel shader we need a separate function in the
875 * pixel shader that reads the fixed function color into the packed input registers.
877 shader_addline(buffer, "vec4 IN[%u];\n", GL_LIMITS(glsl_varyings) / 4);
881 /* Declare output register temporaries */
882 if(This->baseShader.limits.packed_output) {
883 shader_addline(buffer, "vec4 OUT[%u];\n", This->baseShader.limits.packed_output);
886 /* Declare temporary variables */
887 for(i = 0; i < This->baseShader.limits.temporary; i++) {
888 if (reg_maps->temporary[i])
889 shader_addline(buffer, "vec4 R%u;\n", i);
892 /* Declare attributes */
893 for (i = 0; i < This->baseShader.limits.attributes; i++) {
894 if (reg_maps->attributes[i])
895 shader_addline(buffer, "attribute vec4 attrib%i;\n", i);
898 /* Declare loop registers aLx */
899 for (i = 0; i < reg_maps->loop_depth; i++) {
900 shader_addline(buffer, "int aL%u;\n", i);
901 shader_addline(buffer, "int tmpInt%u;\n", i);
904 /* Temporary variables for matrix operations */
905 shader_addline(buffer, "vec4 tmp0;\n");
906 shader_addline(buffer, "vec4 tmp1;\n");
908 /* Local constants use a different name so they can be loaded once at shader link time
909 * They can't be hardcoded into the shader text via LC = {x, y, z, w}; because the
910 * float -> string conversion can cause precision loss.
912 if(!This->baseShader.load_local_constsF) {
913 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
914 shader_addline(buffer, "uniform vec4 %cLC%u;\n", prefix, lconst->idx);
918 /* Start the main program */
919 shader_addline(buffer, "void main() {\n");
920 if(pshader && reg_maps->vpos) {
921 /* DirectX apps expect integer values, while OpenGL drivers add approximately 0.5. This causes
922 * off-by-one problems as spotted by the vPos d3d9 visual test. Unfortunately the ATI cards do
923 * not add exactly 0.5, but rather something like 0.49999999 or 0.50000001, which still causes
924 * precision troubles when we just substract 0.5.
926 * To deal with that just floor() the position. This will eliminate the fraction on all cards.
928 * TODO: Test how that behaves with multisampling once we can enable multisampling in winex11.
930 * An advantage of floor is that it works even if the driver doesn't add 1/2. It is somewhat
931 * questionable if 1.5, 2.5, ... are the proper values to return in gl_FragCoord, even though
932 * coordinates specify the pixel centers instead of the pixel corners. This code will behave
933 * correctly on drivers that returns integer values.
935 shader_addline(buffer, "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
939 /*****************************************************************************
940 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
942 * For more information, see http://wiki.winehq.org/DirectX-Shaders
943 ****************************************************************************/
945 /* Prototypes */
946 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
947 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, glsl_src_param_t *glsl_src);
949 /** Used for opcode modifiers - They multiply the result by the specified amount */
950 static const char * const shift_glsl_tab[] = {
951 "", /* 0 (none) */
952 "2.0 * ", /* 1 (x2) */
953 "4.0 * ", /* 2 (x4) */
954 "8.0 * ", /* 3 (x8) */
955 "16.0 * ", /* 4 (x16) */
956 "32.0 * ", /* 5 (x32) */
957 "", /* 6 (x64) */
958 "", /* 7 (x128) */
959 "", /* 8 (d256) */
960 "", /* 9 (d128) */
961 "", /* 10 (d64) */
962 "", /* 11 (d32) */
963 "0.0625 * ", /* 12 (d16) */
964 "0.125 * ", /* 13 (d8) */
965 "0.25 * ", /* 14 (d4) */
966 "0.5 * " /* 15 (d2) */
969 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
970 static void shader_glsl_gen_modifier(DWORD src_modifier, const char *in_reg, const char *in_regswizzle, char *out_str)
972 out_str[0] = 0;
974 switch (src_modifier)
976 case WINED3DSPSM_DZ: /* Need to handle this in the instructions itself (texld & texcrd). */
977 case WINED3DSPSM_DW:
978 case WINED3DSPSM_NONE:
979 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
980 break;
981 case WINED3DSPSM_NEG:
982 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
983 break;
984 case WINED3DSPSM_NOT:
985 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
986 break;
987 case WINED3DSPSM_BIAS:
988 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
989 break;
990 case WINED3DSPSM_BIASNEG:
991 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
992 break;
993 case WINED3DSPSM_SIGN:
994 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
995 break;
996 case WINED3DSPSM_SIGNNEG:
997 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
998 break;
999 case WINED3DSPSM_COMP:
1000 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
1001 break;
1002 case WINED3DSPSM_X2:
1003 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
1004 break;
1005 case WINED3DSPSM_X2NEG:
1006 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
1007 break;
1008 case WINED3DSPSM_ABS:
1009 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
1010 break;
1011 case WINED3DSPSM_ABSNEG:
1012 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
1013 break;
1014 default:
1015 FIXME("Unhandled modifier %u\n", src_modifier);
1016 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
1020 /** Writes the GLSL variable name that corresponds to the register that the
1021 * DX opcode parameter is trying to access */
1022 static void shader_glsl_get_register_name(WINED3DSHADER_PARAM_REGISTER_TYPE register_type, UINT register_idx,
1023 const struct wined3d_shader_src_param *rel_addr, char *register_name, BOOL *is_color,
1024 const struct wined3d_shader_instruction *ins)
1026 /* oPos, oFog and oPts in D3D */
1027 static const char * const hwrastout_reg_names[] = { "gl_Position", "gl_FogFragCoord", "gl_PointSize" };
1029 IWineD3DBaseShaderImpl *This = (IWineD3DBaseShaderImpl *)ins->ctx->shader;
1030 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
1031 const WineD3D_GL_Info* gl_info = &deviceImpl->adapter->gl_info;
1032 DWORD shader_version = This->baseShader.reg_maps.shader_version;
1033 char pshader = shader_is_pshader_version(shader_version);
1035 *is_color = FALSE;
1037 switch (register_type)
1039 case WINED3DSPR_TEMP:
1040 sprintf(register_name, "R%u", register_idx);
1041 break;
1042 case WINED3DSPR_INPUT:
1043 if (pshader) {
1044 /* Pixel shaders >= 3.0 */
1045 if (WINED3DSHADER_VERSION_MAJOR(shader_version) >= 3)
1047 DWORD in_count = GL_LIMITS(glsl_varyings) / 4;
1049 if (rel_addr)
1051 glsl_src_param_t rel_param;
1052 shader_glsl_add_src_param(ins, rel_addr, WINED3DSP_WRITEMASK_0, &rel_param);
1054 /* Removing a + 0 would be an obvious optimization, but macos doesn't see the NOP
1055 * operation there
1057 if (((IWineD3DPixelShaderImpl *)This)->input_reg_map[register_idx])
1059 if (((IWineD3DPixelShaderImpl *)This)->declared_in_count > in_count) {
1060 sprintf(register_name, "((%s + %u) > %d ? (%s + %u) > %d ? gl_SecondaryColor : gl_Color : IN[%s + %u])",
1061 rel_param.param_str, ((IWineD3DPixelShaderImpl *)This)->input_reg_map[register_idx], in_count - 1,
1062 rel_param.param_str, ((IWineD3DPixelShaderImpl *)This)->input_reg_map[register_idx], in_count,
1063 rel_param.param_str, ((IWineD3DPixelShaderImpl *)This)->input_reg_map[register_idx]);
1064 } else {
1065 sprintf(register_name, "IN[%s + %u]", rel_param.param_str,
1066 ((IWineD3DPixelShaderImpl *)This)->input_reg_map[register_idx]);
1068 } else {
1069 if (((IWineD3DPixelShaderImpl *)This)->declared_in_count > in_count) {
1070 sprintf(register_name, "((%s) > %d ? (%s) > %d ? gl_SecondaryColor : gl_Color : IN[%s])",
1071 rel_param.param_str, in_count - 1,
1072 rel_param.param_str, in_count,
1073 rel_param.param_str);
1074 } else {
1075 sprintf(register_name, "IN[%s]", rel_param.param_str);
1078 } else {
1079 DWORD idx = ((IWineD3DPixelShaderImpl *) This)->input_reg_map[register_idx];
1080 if (idx == in_count) {
1081 sprintf(register_name, "gl_Color");
1082 } else if (idx == in_count + 1) {
1083 sprintf(register_name, "gl_SecondaryColor");
1084 } else {
1085 sprintf(register_name, "IN[%u]", idx);
1088 } else {
1089 if (register_idx == 0)
1090 strcpy(register_name, "gl_Color");
1091 else
1092 strcpy(register_name, "gl_SecondaryColor");
1094 } else {
1095 if (((IWineD3DVertexShaderImpl *)This)->cur_args->swizzle_map & (1 << register_idx)) *is_color = TRUE;
1096 sprintf(register_name, "attrib%u", register_idx);
1098 break;
1099 case WINED3DSPR_CONST:
1101 const char prefix = pshader? 'P':'V';
1103 /* Relative addressing */
1104 if (rel_addr)
1106 glsl_src_param_t rel_param;
1107 shader_glsl_add_src_param(ins, rel_addr, WINED3DSP_WRITEMASK_0, &rel_param);
1108 if (register_idx)
1109 sprintf(register_name, "%cC[%s + %u]", prefix, rel_param.param_str, register_idx);
1110 else
1111 sprintf(register_name, "%cC[%s]", prefix, rel_param.param_str);
1112 } else {
1113 if (shader_constant_is_local(This, register_idx))
1115 sprintf(register_name, "%cLC%u", prefix, register_idx);
1116 } else {
1117 sprintf(register_name, "%cC[%u]", prefix, register_idx);
1121 break;
1123 case WINED3DSPR_CONSTINT:
1124 if (pshader)
1125 sprintf(register_name, "PI[%u]", register_idx);
1126 else
1127 sprintf(register_name, "VI[%u]", register_idx);
1128 break;
1129 case WINED3DSPR_CONSTBOOL:
1130 if (pshader)
1131 sprintf(register_name, "PB[%u]", register_idx);
1132 else
1133 sprintf(register_name, "VB[%u]", register_idx);
1134 break;
1135 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
1136 if (pshader) {
1137 sprintf(register_name, "T%u", register_idx);
1138 } else {
1139 sprintf(register_name, "A%u", register_idx);
1141 break;
1142 case WINED3DSPR_LOOP:
1143 sprintf(register_name, "aL%u", This->baseShader.cur_loop_regno - 1);
1144 break;
1145 case WINED3DSPR_SAMPLER:
1146 if (pshader)
1147 sprintf(register_name, "Psampler%u", register_idx);
1148 else
1149 sprintf(register_name, "Vsampler%u", register_idx);
1150 break;
1151 case WINED3DSPR_COLOROUT:
1152 if (register_idx >= GL_LIMITS(buffers))
1153 WARN("Write to render target %u, only %d supported\n", register_idx, 4);
1155 if (GL_SUPPORT(ARB_DRAW_BUFFERS)) {
1156 sprintf(register_name, "gl_FragData[%u]", register_idx);
1157 } else { /* On older cards with GLSL support like the GeforceFX there's only one buffer. */
1158 sprintf(register_name, "gl_FragColor");
1160 break;
1161 case WINED3DSPR_RASTOUT:
1162 sprintf(register_name, "%s", hwrastout_reg_names[register_idx]);
1163 break;
1164 case WINED3DSPR_DEPTHOUT:
1165 sprintf(register_name, "gl_FragDepth");
1166 break;
1167 case WINED3DSPR_ATTROUT:
1168 if (register_idx == 0)
1170 sprintf(register_name, "gl_FrontColor");
1171 } else {
1172 sprintf(register_name, "gl_FrontSecondaryColor");
1174 break;
1175 case WINED3DSPR_TEXCRDOUT:
1176 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
1177 if (WINED3DSHADER_VERSION_MAJOR(shader_version) >= 3) sprintf(register_name, "OUT[%u]", register_idx);
1178 else sprintf(register_name, "gl_TexCoord[%u]", register_idx);
1179 break;
1180 case WINED3DSPR_MISCTYPE:
1181 if (register_idx == 0)
1183 /* vPos */
1184 sprintf(register_name, "vpos");
1186 else if (register_idx == 1)
1188 /* Note that gl_FrontFacing is a bool, while vFace is
1189 * a float for which the sign determines front/back
1191 sprintf(register_name, "(gl_FrontFacing ? 1.0 : -1.0)");
1192 } else {
1193 FIXME("Unhandled misctype register %d\n", register_idx);
1194 sprintf(register_name, "unrecognized_register");
1196 break;
1197 default:
1198 FIXME("Unhandled register name Type(%d)\n", register_type);
1199 sprintf(register_name, "unrecognized_register");
1200 break;
1204 static void shader_glsl_write_mask_to_str(DWORD write_mask, char *str)
1206 *str++ = '.';
1207 if (write_mask & WINED3DSP_WRITEMASK_0) *str++ = 'x';
1208 if (write_mask & WINED3DSP_WRITEMASK_1) *str++ = 'y';
1209 if (write_mask & WINED3DSP_WRITEMASK_2) *str++ = 'z';
1210 if (write_mask & WINED3DSP_WRITEMASK_3) *str++ = 'w';
1211 *str = '\0';
1214 /* Get the GLSL write mask for the destination register */
1215 static DWORD shader_glsl_get_write_mask(const struct wined3d_shader_dst_param *param, char *write_mask)
1217 DWORD mask = param->write_mask;
1219 if (shader_is_scalar(param->register_type, param->register_idx))
1221 mask = WINED3DSP_WRITEMASK_0;
1222 *write_mask = '\0';
1224 else
1226 shader_glsl_write_mask_to_str(mask, write_mask);
1229 return mask;
1232 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask) {
1233 unsigned int size = 0;
1235 if (write_mask & WINED3DSP_WRITEMASK_0) ++size;
1236 if (write_mask & WINED3DSP_WRITEMASK_1) ++size;
1237 if (write_mask & WINED3DSP_WRITEMASK_2) ++size;
1238 if (write_mask & WINED3DSP_WRITEMASK_3) ++size;
1240 return size;
1243 static void shader_glsl_swizzle_to_str(const DWORD swizzle, BOOL fixup, DWORD mask, char *str)
1245 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
1246 * but addressed as "rgba". To fix this we need to swap the register's x
1247 * and z components. */
1248 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
1250 *str++ = '.';
1251 /* swizzle bits fields: wwzzyyxx */
1252 if (mask & WINED3DSP_WRITEMASK_0) *str++ = swizzle_chars[swizzle & 0x03];
1253 if (mask & WINED3DSP_WRITEMASK_1) *str++ = swizzle_chars[(swizzle >> 2) & 0x03];
1254 if (mask & WINED3DSP_WRITEMASK_2) *str++ = swizzle_chars[(swizzle >> 4) & 0x03];
1255 if (mask & WINED3DSP_WRITEMASK_3) *str++ = swizzle_chars[(swizzle >> 6) & 0x03];
1256 *str = '\0';
1259 static void shader_glsl_get_swizzle(const struct wined3d_shader_src_param *param,
1260 BOOL fixup, DWORD mask, char *swizzle_str)
1262 if (shader_is_scalar(param->register_type, param->register_idx))
1263 *swizzle_str = '\0';
1264 else
1265 shader_glsl_swizzle_to_str(param->swizzle, fixup, mask, swizzle_str);
1268 /* From a given parameter token, generate the corresponding GLSL string.
1269 * Also, return the actual register name and swizzle in case the
1270 * caller needs this information as well. */
1271 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
1272 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, glsl_src_param_t *glsl_src)
1274 BOOL is_color = FALSE;
1275 char swizzle_str[6];
1277 glsl_src->reg_name[0] = '\0';
1278 glsl_src->param_str[0] = '\0';
1279 swizzle_str[0] = '\0';
1281 shader_glsl_get_register_name(wined3d_src->register_type, wined3d_src->register_idx,
1282 wined3d_src->rel_addr, glsl_src->reg_name, &is_color, ins);
1284 shader_glsl_get_swizzle(wined3d_src, is_color, mask, swizzle_str);
1285 shader_glsl_gen_modifier(wined3d_src->modifiers, glsl_src->reg_name, swizzle_str, glsl_src->param_str);
1288 /* From a given parameter token, generate the corresponding GLSL string.
1289 * Also, return the actual register name and swizzle in case the
1290 * caller needs this information as well. */
1291 static DWORD shader_glsl_add_dst_param(const struct wined3d_shader_instruction *ins,
1292 const struct wined3d_shader_dst_param *wined3d_dst, glsl_dst_param_t *glsl_dst)
1294 BOOL is_color = FALSE;
1296 glsl_dst->mask_str[0] = '\0';
1297 glsl_dst->reg_name[0] = '\0';
1299 shader_glsl_get_register_name(wined3d_dst->register_type, wined3d_dst->register_idx,
1300 wined3d_dst->rel_addr, glsl_dst->reg_name, &is_color, ins);
1301 return shader_glsl_get_write_mask(wined3d_dst, glsl_dst->mask_str);
1304 /* Append the destination part of the instruction to the buffer, return the effective write mask */
1305 static DWORD shader_glsl_append_dst_ext(SHADER_BUFFER *buffer,
1306 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_dst_param *dst)
1308 glsl_dst_param_t glsl_dst;
1309 DWORD mask;
1311 mask = shader_glsl_add_dst_param(ins, dst, &glsl_dst);
1312 if (mask) shader_addline(buffer, "%s%s = %s(", glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
1314 return mask;
1317 /* Append the destination part of the instruction to the buffer, return the effective write mask */
1318 static DWORD shader_glsl_append_dst(SHADER_BUFFER *buffer, const struct wined3d_shader_instruction *ins)
1320 return shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0]);
1323 /** Process GLSL instruction modifiers */
1324 void shader_glsl_add_instruction_modifiers(const struct wined3d_shader_instruction *ins)
1326 glsl_dst_param_t dst_param;
1327 DWORD modifiers;
1329 if (!ins->dst_count) return;
1331 modifiers = ins->dst[0].modifiers;
1332 if (!modifiers) return;
1334 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
1336 if (modifiers & WINED3DSPDM_SATURATE)
1338 /* _SAT means to clamp the value of the register to between 0 and 1 */
1339 shader_addline(ins->ctx->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param.reg_name,
1340 dst_param.mask_str, dst_param.reg_name, dst_param.mask_str);
1343 if (modifiers & WINED3DSPDM_MSAMPCENTROID)
1345 FIXME("_centroid modifier not handled\n");
1348 if (modifiers & WINED3DSPDM_PARTIALPRECISION)
1350 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
1354 static inline const char *shader_get_comp_op(DWORD op)
1356 switch (op) {
1357 case COMPARISON_GT: return ">";
1358 case COMPARISON_EQ: return "==";
1359 case COMPARISON_GE: return ">=";
1360 case COMPARISON_LT: return "<";
1361 case COMPARISON_NE: return "!=";
1362 case COMPARISON_LE: return "<=";
1363 default:
1364 FIXME("Unrecognized comparison value: %u\n", op);
1365 return "(\?\?)";
1369 static void shader_glsl_get_sample_function(DWORD sampler_type, DWORD flags, glsl_sample_function_t *sample_function)
1371 BOOL projected = flags & WINED3D_GLSL_SAMPLE_PROJECTED;
1372 BOOL texrect = flags & WINED3D_GLSL_SAMPLE_RECT;
1373 BOOL lod = flags & WINED3D_GLSL_SAMPLE_LOD;
1375 /* Note that there's no such thing as a projected cube texture. */
1376 switch(sampler_type) {
1377 case WINED3DSTT_1D:
1378 if(lod) {
1379 sample_function->name = projected ? "texture1DProjLod" : "texture1DLod";
1380 } else {
1381 sample_function->name = projected ? "texture1DProj" : "texture1D";
1383 sample_function->coord_mask = WINED3DSP_WRITEMASK_0;
1384 break;
1385 case WINED3DSTT_2D:
1386 if(texrect) {
1387 if(lod) {
1388 sample_function->name = projected ? "texture2DRectProjLod" : "texture2DRectLod";
1389 } else {
1390 sample_function->name = projected ? "texture2DRectProj" : "texture2DRect";
1392 } else {
1393 if(lod) {
1394 sample_function->name = projected ? "texture2DProjLod" : "texture2DLod";
1395 } else {
1396 sample_function->name = projected ? "texture2DProj" : "texture2D";
1399 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
1400 break;
1401 case WINED3DSTT_CUBE:
1402 if(lod) {
1403 sample_function->name = "textureCubeLod";
1404 } else {
1405 sample_function->name = "textureCube";
1407 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1408 break;
1409 case WINED3DSTT_VOLUME:
1410 if(lod) {
1411 sample_function->name = projected ? "texture3DProjLod" : "texture3DLod";
1412 } else {
1413 sample_function->name = projected ? "texture3DProj" : "texture3D";
1415 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1416 break;
1417 default:
1418 sample_function->name = "";
1419 sample_function->coord_mask = 0;
1420 FIXME("Unrecognized sampler type: %#x;\n", sampler_type);
1421 break;
1425 static void shader_glsl_append_fixup_arg(char *arguments, const char *reg_name,
1426 BOOL sign_fixup, enum fixup_channel_source channel_source)
1428 switch(channel_source)
1430 case CHANNEL_SOURCE_ZERO:
1431 strcat(arguments, "0.0");
1432 break;
1434 case CHANNEL_SOURCE_ONE:
1435 strcat(arguments, "1.0");
1436 break;
1438 case CHANNEL_SOURCE_X:
1439 strcat(arguments, reg_name);
1440 strcat(arguments, ".x");
1441 break;
1443 case CHANNEL_SOURCE_Y:
1444 strcat(arguments, reg_name);
1445 strcat(arguments, ".y");
1446 break;
1448 case CHANNEL_SOURCE_Z:
1449 strcat(arguments, reg_name);
1450 strcat(arguments, ".z");
1451 break;
1453 case CHANNEL_SOURCE_W:
1454 strcat(arguments, reg_name);
1455 strcat(arguments, ".w");
1456 break;
1458 default:
1459 FIXME("Unhandled channel source %#x\n", channel_source);
1460 strcat(arguments, "undefined");
1461 break;
1464 if (sign_fixup) strcat(arguments, " * 2.0 - 1.0");
1467 static void shader_glsl_color_correction(const struct wined3d_shader_instruction *ins, struct color_fixup_desc fixup)
1469 struct wined3d_shader_dst_param dst;
1470 unsigned int mask_size, remaining;
1471 glsl_dst_param_t dst_param;
1472 char arguments[256];
1473 DWORD mask;
1475 mask = 0;
1476 if (fixup.x_sign_fixup || fixup.x_source != CHANNEL_SOURCE_X) mask |= WINED3DSP_WRITEMASK_0;
1477 if (fixup.y_sign_fixup || fixup.y_source != CHANNEL_SOURCE_Y) mask |= WINED3DSP_WRITEMASK_1;
1478 if (fixup.z_sign_fixup || fixup.z_source != CHANNEL_SOURCE_Z) mask |= WINED3DSP_WRITEMASK_2;
1479 if (fixup.w_sign_fixup || fixup.w_source != CHANNEL_SOURCE_W) mask |= WINED3DSP_WRITEMASK_3;
1480 mask &= ins->dst[0].write_mask;
1482 if (!mask) return; /* Nothing to do */
1484 if (is_yuv_fixup(fixup))
1486 enum yuv_fixup yuv_fixup = get_yuv_fixup(fixup);
1487 FIXME("YUV fixup (%#x) not supported\n", yuv_fixup);
1488 return;
1491 mask_size = shader_glsl_get_write_mask_size(mask);
1493 dst = ins->dst[0];
1494 dst.write_mask = mask;
1495 shader_glsl_add_dst_param(ins, &dst, &dst_param);
1497 arguments[0] = '\0';
1498 remaining = mask_size;
1499 if (mask & WINED3DSP_WRITEMASK_0)
1501 shader_glsl_append_fixup_arg(arguments, dst_param.reg_name, fixup.x_sign_fixup, fixup.x_source);
1502 if (--remaining) strcat(arguments, ", ");
1504 if (mask & WINED3DSP_WRITEMASK_1)
1506 shader_glsl_append_fixup_arg(arguments, dst_param.reg_name, fixup.y_sign_fixup, fixup.y_source);
1507 if (--remaining) strcat(arguments, ", ");
1509 if (mask & WINED3DSP_WRITEMASK_2)
1511 shader_glsl_append_fixup_arg(arguments, dst_param.reg_name, fixup.z_sign_fixup, fixup.z_source);
1512 if (--remaining) strcat(arguments, ", ");
1514 if (mask & WINED3DSP_WRITEMASK_3)
1516 shader_glsl_append_fixup_arg(arguments, dst_param.reg_name, fixup.w_sign_fixup, fixup.w_source);
1517 if (--remaining) strcat(arguments, ", ");
1520 if (mask_size > 1)
1522 shader_addline(ins->ctx->buffer, "%s%s = vec%u(%s);\n",
1523 dst_param.reg_name, dst_param.mask_str, mask_size, arguments);
1525 else
1527 shader_addline(ins->ctx->buffer, "%s%s = %s;\n", dst_param.reg_name, dst_param.mask_str, arguments);
1531 static void PRINTF_ATTR(6, 7) shader_glsl_gen_sample_code(const struct wined3d_shader_instruction *ins,
1532 DWORD sampler, const glsl_sample_function_t *sample_function, DWORD swizzle,
1533 const char *bias, const char *coord_reg_fmt, ...)
1535 const char *sampler_base;
1536 char dst_swizzle[6];
1537 struct color_fixup_desc fixup;
1538 BOOL np2_fixup = FALSE;
1539 va_list args;
1541 shader_glsl_swizzle_to_str(swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
1543 if (shader_is_pshader_version(ins->ctx->reg_maps->shader_version))
1545 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)ins->ctx->shader;
1546 fixup = This->cur_args->color_fixup[sampler];
1547 sampler_base = "Psampler";
1549 if(This->cur_args->np2_fixup & (1 << sampler)) {
1550 if(bias) {
1551 FIXME("Biased sampling from NP2 textures is unsupported\n");
1552 } else {
1553 np2_fixup = TRUE;
1556 } else {
1557 sampler_base = "Vsampler";
1558 fixup = COLOR_FIXUP_IDENTITY; /* FIXME: Vshader color fixup */
1561 shader_glsl_append_dst(ins->ctx->buffer, ins);
1563 shader_addline(ins->ctx->buffer, "%s(%s%u, ", sample_function->name, sampler_base, sampler);
1565 va_start(args, coord_reg_fmt);
1566 shader_vaddline(ins->ctx->buffer, coord_reg_fmt, args);
1567 va_end(args);
1569 if(bias) {
1570 shader_addline(ins->ctx->buffer, ", %s)%s);\n", bias, dst_swizzle);
1571 } else {
1572 if (np2_fixup) {
1573 shader_addline(ins->ctx->buffer, " * PsamplerNP2Fixup%u)%s);\n", sampler, dst_swizzle);
1574 } else {
1575 shader_addline(ins->ctx->buffer, ")%s);\n", dst_swizzle);
1579 if(!is_identity_fixup(fixup)) {
1580 shader_glsl_color_correction(ins, fixup);
1584 /*****************************************************************************
1586 * Begin processing individual instruction opcodes
1588 ****************************************************************************/
1590 /* Generate GLSL arithmetic functions (dst = src1 + src2) */
1591 static void shader_glsl_arith(const struct wined3d_shader_instruction *ins)
1593 SHADER_BUFFER *buffer = ins->ctx->buffer;
1594 glsl_src_param_t src0_param;
1595 glsl_src_param_t src1_param;
1596 DWORD write_mask;
1597 char op;
1599 /* Determine the GLSL operator to use based on the opcode */
1600 switch (ins->handler_idx)
1602 case WINED3DSIH_MUL: op = '*'; break;
1603 case WINED3DSIH_ADD: op = '+'; break;
1604 case WINED3DSIH_SUB: op = '-'; break;
1605 default:
1606 op = ' ';
1607 FIXME("Opcode %#x not yet handled in GLSL\n", ins->handler_idx);
1608 break;
1611 write_mask = shader_glsl_append_dst(buffer, ins);
1612 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
1613 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
1614 shader_addline(buffer, "%s %c %s);\n", src0_param.param_str, op, src1_param.param_str);
1617 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
1618 static void shader_glsl_mov(const struct wined3d_shader_instruction *ins)
1620 SHADER_BUFFER *buffer = ins->ctx->buffer;
1621 glsl_src_param_t src0_param;
1622 DWORD write_mask;
1624 write_mask = shader_glsl_append_dst(buffer, ins);
1625 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
1627 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
1628 * shader versions WINED3DSIO_MOVA is used for this. */
1629 if ((WINED3DSHADER_VERSION_MAJOR(ins->ctx->reg_maps->shader_version) == 1
1630 && !shader_is_pshader_version(ins->ctx->reg_maps->shader_version)
1631 && ins->dst[0].register_type == WINED3DSPR_ADDR))
1633 /* This is a simple floor() */
1634 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
1635 if (mask_size > 1) {
1636 shader_addline(buffer, "ivec%d(floor(%s)));\n", mask_size, src0_param.param_str);
1637 } else {
1638 shader_addline(buffer, "int(floor(%s)));\n", src0_param.param_str);
1641 else if(ins->handler_idx == WINED3DSIH_MOVA)
1643 /* We need to *round* to the nearest int here. */
1644 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
1645 if (mask_size > 1) {
1646 shader_addline(buffer, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n", mask_size, src0_param.param_str, mask_size, src0_param.param_str);
1647 } else {
1648 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n", src0_param.param_str, src0_param.param_str);
1650 } else {
1651 shader_addline(buffer, "%s);\n", src0_param.param_str);
1655 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
1656 static void shader_glsl_dot(const struct wined3d_shader_instruction *ins)
1658 SHADER_BUFFER *buffer = ins->ctx->buffer;
1659 glsl_src_param_t src0_param;
1660 glsl_src_param_t src1_param;
1661 DWORD dst_write_mask, src_write_mask;
1662 unsigned int dst_size = 0;
1664 dst_write_mask = shader_glsl_append_dst(buffer, ins);
1665 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
1667 /* dp3 works on vec3, dp4 on vec4 */
1668 if (ins->handler_idx == WINED3DSIH_DP4)
1670 src_write_mask = WINED3DSP_WRITEMASK_ALL;
1671 } else {
1672 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1675 shader_glsl_add_src_param(ins, &ins->src[0], src_write_mask, &src0_param);
1676 shader_glsl_add_src_param(ins, &ins->src[1], src_write_mask, &src1_param);
1678 if (dst_size > 1) {
1679 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
1680 } else {
1681 shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
1685 /* Note that this instruction has some restrictions. The destination write mask
1686 * can't contain the w component, and the source swizzles have to be .xyzw */
1687 static void shader_glsl_cross(const struct wined3d_shader_instruction *ins)
1689 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1690 glsl_src_param_t src0_param;
1691 glsl_src_param_t src1_param;
1692 char dst_mask[6];
1694 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
1695 shader_glsl_append_dst(ins->ctx->buffer, ins);
1696 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
1697 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
1698 shader_addline(ins->ctx->buffer, "cross(%s, %s)%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
1701 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
1702 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
1703 * GLSL uses the value as-is. */
1704 static void shader_glsl_pow(const struct wined3d_shader_instruction *ins)
1706 SHADER_BUFFER *buffer = ins->ctx->buffer;
1707 glsl_src_param_t src0_param;
1708 glsl_src_param_t src1_param;
1709 DWORD dst_write_mask;
1710 unsigned int dst_size;
1712 dst_write_mask = shader_glsl_append_dst(buffer, ins);
1713 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
1715 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
1716 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
1718 if (dst_size > 1) {
1719 shader_addline(buffer, "vec%d(pow(abs(%s), %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
1720 } else {
1721 shader_addline(buffer, "pow(abs(%s), %s));\n", src0_param.param_str, src1_param.param_str);
1725 /* Process the WINED3DSIO_LOG instruction in GLSL (dst = log2(|src0|))
1726 * Src0 is a scalar. Note that D3D uses the absolute of src0, while
1727 * GLSL uses the value as-is. */
1728 static void shader_glsl_log(const struct wined3d_shader_instruction *ins)
1730 SHADER_BUFFER *buffer = ins->ctx->buffer;
1731 glsl_src_param_t src0_param;
1732 DWORD dst_write_mask;
1733 unsigned int dst_size;
1735 dst_write_mask = shader_glsl_append_dst(buffer, ins);
1736 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
1738 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
1740 if (dst_size > 1) {
1741 shader_addline(buffer, "vec%d(log2(abs(%s))));\n", dst_size, src0_param.param_str);
1742 } else {
1743 shader_addline(buffer, "log2(abs(%s)));\n", src0_param.param_str);
1747 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
1748 static void shader_glsl_map2gl(const struct wined3d_shader_instruction *ins)
1750 SHADER_BUFFER *buffer = ins->ctx->buffer;
1751 glsl_src_param_t src_param;
1752 const char *instruction;
1753 DWORD write_mask;
1754 unsigned i;
1756 /* Determine the GLSL function to use based on the opcode */
1757 /* TODO: Possibly make this a table for faster lookups */
1758 switch (ins->handler_idx)
1760 case WINED3DSIH_MIN: instruction = "min"; break;
1761 case WINED3DSIH_MAX: instruction = "max"; break;
1762 case WINED3DSIH_ABS: instruction = "abs"; break;
1763 case WINED3DSIH_FRC: instruction = "fract"; break;
1764 case WINED3DSIH_NRM: instruction = "normalize"; break;
1765 case WINED3DSIH_EXP: instruction = "exp2"; break;
1766 case WINED3DSIH_SGN: instruction = "sign"; break;
1767 case WINED3DSIH_DSX: instruction = "dFdx"; break;
1768 case WINED3DSIH_DSY: instruction = "ycorrection.y * dFdy"; break;
1769 default: instruction = "";
1770 FIXME("Opcode %#x not yet handled in GLSL\n", ins->handler_idx);
1771 break;
1774 write_mask = shader_glsl_append_dst(buffer, ins);
1776 shader_addline(buffer, "%s(", instruction);
1778 if (ins->src_count)
1780 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
1781 shader_addline(buffer, "%s", src_param.param_str);
1782 for (i = 1; i < ins->src_count; ++i)
1784 shader_glsl_add_src_param(ins, &ins->src[i], write_mask, &src_param);
1785 shader_addline(buffer, ", %s", src_param.param_str);
1789 shader_addline(buffer, "));\n");
1792 /** Process the WINED3DSIO_EXPP instruction in GLSL:
1793 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
1794 * dst.x = 2^(floor(src))
1795 * dst.y = src - floor(src)
1796 * dst.z = 2^src (partial precision is allowed, but optional)
1797 * dst.w = 1.0;
1798 * For 2.0 shaders, just do this (honoring writemask and swizzle):
1799 * dst = 2^src; (partial precision is allowed, but optional)
1801 static void shader_glsl_expp(const struct wined3d_shader_instruction *ins)
1803 glsl_src_param_t src_param;
1805 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
1807 if (ins->ctx->reg_maps->shader_version < WINED3DPS_VERSION(2,0))
1809 char dst_mask[6];
1811 shader_addline(ins->ctx->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
1812 shader_addline(ins->ctx->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
1813 shader_addline(ins->ctx->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
1814 shader_addline(ins->ctx->buffer, "tmp0.w = 1.0;\n");
1816 shader_glsl_append_dst(ins->ctx->buffer, ins);
1817 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
1818 shader_addline(ins->ctx->buffer, "tmp0%s);\n", dst_mask);
1819 } else {
1820 DWORD write_mask;
1821 unsigned int mask_size;
1823 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
1824 mask_size = shader_glsl_get_write_mask_size(write_mask);
1826 if (mask_size > 1) {
1827 shader_addline(ins->ctx->buffer, "vec%d(exp2(%s)));\n", mask_size, src_param.param_str);
1828 } else {
1829 shader_addline(ins->ctx->buffer, "exp2(%s));\n", src_param.param_str);
1834 /** Process the RCP (reciprocal or inverse) opcode in GLSL (dst = 1 / src) */
1835 static void shader_glsl_rcp(const struct wined3d_shader_instruction *ins)
1837 glsl_src_param_t src_param;
1838 DWORD write_mask;
1839 unsigned int mask_size;
1841 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
1842 mask_size = shader_glsl_get_write_mask_size(write_mask);
1843 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src_param);
1845 if (mask_size > 1) {
1846 shader_addline(ins->ctx->buffer, "vec%d(1.0 / %s));\n", mask_size, src_param.param_str);
1847 } else {
1848 shader_addline(ins->ctx->buffer, "1.0 / %s);\n", src_param.param_str);
1852 static void shader_glsl_rsq(const struct wined3d_shader_instruction *ins)
1854 SHADER_BUFFER *buffer = ins->ctx->buffer;
1855 glsl_src_param_t src_param;
1856 DWORD write_mask;
1857 unsigned int mask_size;
1859 write_mask = shader_glsl_append_dst(buffer, ins);
1860 mask_size = shader_glsl_get_write_mask_size(write_mask);
1862 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src_param);
1864 if (mask_size > 1) {
1865 shader_addline(buffer, "vec%d(inversesqrt(%s)));\n", mask_size, src_param.param_str);
1866 } else {
1867 shader_addline(buffer, "inversesqrt(%s));\n", src_param.param_str);
1871 /** Process signed comparison opcodes in GLSL. */
1872 static void shader_glsl_compare(const struct wined3d_shader_instruction *ins)
1874 glsl_src_param_t src0_param;
1875 glsl_src_param_t src1_param;
1876 DWORD write_mask;
1877 unsigned int mask_size;
1879 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
1880 mask_size = shader_glsl_get_write_mask_size(write_mask);
1881 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
1882 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
1884 if (mask_size > 1) {
1885 const char *compare;
1887 switch(ins->handler_idx)
1889 case WINED3DSIH_SLT: compare = "lessThan"; break;
1890 case WINED3DSIH_SGE: compare = "greaterThanEqual"; break;
1891 default: compare = "";
1892 FIXME("Can't handle opcode %#x\n", ins->handler_idx);
1895 shader_addline(ins->ctx->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
1896 src0_param.param_str, src1_param.param_str);
1897 } else {
1898 switch(ins->handler_idx)
1900 case WINED3DSIH_SLT:
1901 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
1902 * to return 0.0 but step returns 1.0 because step is not < x
1903 * An alternative is a bvec compare padded with an unused second component.
1904 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
1905 * issue. Playing with not() is not possible either because not() does not accept
1906 * a scalar.
1908 shader_addline(ins->ctx->buffer, "(%s < %s) ? 1.0 : 0.0);\n",
1909 src0_param.param_str, src1_param.param_str);
1910 break;
1911 case WINED3DSIH_SGE:
1912 /* Here we can use the step() function and safe a conditional */
1913 shader_addline(ins->ctx->buffer, "step(%s, %s));\n", src1_param.param_str, src0_param.param_str);
1914 break;
1915 default:
1916 FIXME("Can't handle opcode %#x\n", ins->handler_idx);
1922 /** Process CMP instruction in GLSL (dst = src0 >= 0.0 ? src1 : src2), per channel */
1923 static void shader_glsl_cmp(const struct wined3d_shader_instruction *ins)
1925 glsl_src_param_t src0_param;
1926 glsl_src_param_t src1_param;
1927 glsl_src_param_t src2_param;
1928 DWORD write_mask, cmp_channel = 0;
1929 unsigned int i, j;
1930 char mask_char[6];
1931 BOOL temp_destination = FALSE;
1933 if (shader_is_scalar(ins->src[0].register_type, ins->src[0].register_idx))
1935 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
1937 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
1938 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
1939 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
1941 shader_addline(ins->ctx->buffer, "%s >= 0.0 ? %s : %s);\n",
1942 src0_param.param_str, src1_param.param_str, src2_param.param_str);
1943 } else {
1944 DWORD dst_mask = ins->dst[0].write_mask;
1945 struct wined3d_shader_dst_param dst = ins->dst[0];
1947 /* Cycle through all source0 channels */
1948 for (i=0; i<4; i++) {
1949 write_mask = 0;
1950 /* Find the destination channels which use the current source0 channel */
1951 for (j=0; j<4; j++) {
1952 if (((ins->src[0].swizzle >> (2 * j)) & 0x3) == i)
1954 write_mask |= WINED3DSP_WRITEMASK_0 << j;
1955 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
1958 dst.write_mask = dst_mask & write_mask;
1960 /* Splitting the cmp instruction up in multiple lines imposes a problem:
1961 * The first lines may overwrite source parameters of the following lines.
1962 * Deal with that by using a temporary destination register if needed
1964 if ((ins->src[0].register_idx == ins->dst[0].register_idx
1965 && ins->src[0].register_type == ins->dst[0].register_type)
1966 || (ins->src[1].register_idx == ins->dst[0].register_idx
1967 && ins->src[1].register_type == ins->dst[0].register_type)
1968 || (ins->src[2].register_idx == ins->dst[0].register_idx
1969 && ins->src[2].register_type == ins->dst[0].register_type))
1971 write_mask = shader_glsl_get_write_mask(&dst, mask_char);
1972 if (!write_mask) continue;
1973 shader_addline(ins->ctx->buffer, "tmp0%s = (", mask_char);
1974 temp_destination = TRUE;
1975 } else {
1976 write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst);
1977 if (!write_mask) continue;
1980 shader_glsl_add_src_param(ins, &ins->src[0], cmp_channel, &src0_param);
1981 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
1982 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
1984 shader_addline(ins->ctx->buffer, "%s >= 0.0 ? %s : %s);\n",
1985 src0_param.param_str, src1_param.param_str, src2_param.param_str);
1988 if(temp_destination) {
1989 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
1990 shader_glsl_append_dst(ins->ctx->buffer, ins);
1991 shader_addline(ins->ctx->buffer, "tmp0%s);\n", mask_char);
1997 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
1998 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
1999 * the compare is done per component of src0. */
2000 static void shader_glsl_cnd(const struct wined3d_shader_instruction *ins)
2002 struct wined3d_shader_dst_param dst;
2003 glsl_src_param_t src0_param;
2004 glsl_src_param_t src1_param;
2005 glsl_src_param_t src2_param;
2006 DWORD write_mask, cmp_channel = 0;
2007 unsigned int i, j;
2008 DWORD dst_mask;
2010 if (ins->ctx->reg_maps->shader_version < WINED3DPS_VERSION(1, 4))
2012 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2013 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2014 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2015 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2017 /* Fun: The D3DSI_COISSUE flag changes the semantic of the cnd instruction for < 1.4 shaders */
2018 if (ins->coissue)
2020 shader_addline(ins->ctx->buffer, "%s /* COISSUE! */);\n", src1_param.param_str);
2021 } else {
2022 shader_addline(ins->ctx->buffer, "%s > 0.5 ? %s : %s);\n",
2023 src0_param.param_str, src1_param.param_str, src2_param.param_str);
2025 return;
2027 /* Cycle through all source0 channels */
2028 dst_mask = ins->dst[0].write_mask;
2029 dst = ins->dst[0];
2030 for (i=0; i<4; i++) {
2031 write_mask = 0;
2032 /* Find the destination channels which use the current source0 channel */
2033 for (j=0; j<4; j++) {
2034 if (((ins->src[0].swizzle >> (2 * j)) & 0x3) == i)
2036 write_mask |= WINED3DSP_WRITEMASK_0 << j;
2037 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
2041 dst.write_mask = dst_mask & write_mask;
2042 write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst);
2043 if (!write_mask) continue;
2045 shader_glsl_add_src_param(ins, &ins->src[0], cmp_channel, &src0_param);
2046 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2047 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2049 shader_addline(ins->ctx->buffer, "%s > 0.5 ? %s : %s);\n",
2050 src0_param.param_str, src1_param.param_str, src2_param.param_str);
2054 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
2055 static void shader_glsl_mad(const struct wined3d_shader_instruction *ins)
2057 glsl_src_param_t src0_param;
2058 glsl_src_param_t src1_param;
2059 glsl_src_param_t src2_param;
2060 DWORD write_mask;
2062 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2063 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2064 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2065 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2066 shader_addline(ins->ctx->buffer, "(%s * %s) + %s);\n",
2067 src0_param.param_str, src1_param.param_str, src2_param.param_str);
2070 /** Handles transforming all WINED3DSIO_M?x? opcodes for
2071 Vertex shaders to GLSL codes */
2072 static void shader_glsl_mnxn(const struct wined3d_shader_instruction *ins)
2074 int i;
2075 int nComponents = 0;
2076 struct wined3d_shader_dst_param tmp_dst = {0};
2077 struct wined3d_shader_src_param tmp_src[2] = {{0}};
2078 struct wined3d_shader_instruction tmp_ins;
2080 memset(&tmp_ins, 0, sizeof(tmp_ins));
2082 /* Set constants for the temporary argument */
2083 tmp_ins.ctx = ins->ctx;
2084 tmp_ins.dst_count = 1;
2085 tmp_ins.dst = &tmp_dst;
2086 tmp_ins.src_count = 2;
2087 tmp_ins.src = tmp_src;
2089 switch(ins->handler_idx)
2091 case WINED3DSIH_M4x4:
2092 nComponents = 4;
2093 tmp_ins.handler_idx = WINED3DSIH_DP4;
2094 break;
2095 case WINED3DSIH_M4x3:
2096 nComponents = 3;
2097 tmp_ins.handler_idx = WINED3DSIH_DP4;
2098 break;
2099 case WINED3DSIH_M3x4:
2100 nComponents = 4;
2101 tmp_ins.handler_idx = WINED3DSIH_DP3;
2102 break;
2103 case WINED3DSIH_M3x3:
2104 nComponents = 3;
2105 tmp_ins.handler_idx = WINED3DSIH_DP3;
2106 break;
2107 case WINED3DSIH_M3x2:
2108 nComponents = 2;
2109 tmp_ins.handler_idx = WINED3DSIH_DP3;
2110 break;
2111 default:
2112 break;
2115 tmp_dst = ins->dst[0];
2116 tmp_src[0] = ins->src[0];
2117 tmp_src[1] = ins->src[1];
2118 for (i = 0; i < nComponents; ++i)
2120 tmp_dst.write_mask = WINED3DSP_WRITEMASK_0 << i;
2121 shader_glsl_dot(&tmp_ins);
2122 ++tmp_src[1].register_idx;
2127 The LRP instruction performs a component-wise linear interpolation
2128 between the second and third operands using the first operand as the
2129 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
2130 This is equivalent to mix(src2, src1, src0);
2132 static void shader_glsl_lrp(const struct wined3d_shader_instruction *ins)
2134 glsl_src_param_t src0_param;
2135 glsl_src_param_t src1_param;
2136 glsl_src_param_t src2_param;
2137 DWORD write_mask;
2139 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2141 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2142 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2143 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2145 shader_addline(ins->ctx->buffer, "mix(%s, %s, %s));\n",
2146 src2_param.param_str, src1_param.param_str, src0_param.param_str);
2149 /** Process the WINED3DSIO_LIT instruction in GLSL:
2150 * dst.x = dst.w = 1.0
2151 * dst.y = (src0.x > 0) ? src0.x
2152 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
2153 * where src.w is clamped at +- 128
2155 static void shader_glsl_lit(const struct wined3d_shader_instruction *ins)
2157 glsl_src_param_t src0_param;
2158 glsl_src_param_t src1_param;
2159 glsl_src_param_t src3_param;
2160 char dst_mask[6];
2162 shader_glsl_append_dst(ins->ctx->buffer, ins);
2163 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2165 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2166 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src1_param);
2167 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src3_param);
2169 /* The sdk specifies the instruction like this
2170 * dst.x = 1.0;
2171 * if(src.x > 0.0) dst.y = src.x
2172 * else dst.y = 0.0.
2173 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
2174 * else dst.z = 0.0;
2175 * dst.w = 1.0;
2177 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
2178 * dst.x = 1.0 ... No further explanation needed
2179 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
2180 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
2181 * dst.w = 1.0. ... Nothing fancy.
2183 * So we still have one conditional in there. So do this:
2184 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
2186 * step(0.0, x) will return 1 if src.x > 0.0, and 0 otherwise. So if y is 0 we get pow(0.0 * 1.0, power),
2187 * which sets dst.z to 0. If y > 0, but x = 0.0, we get pow(y * 0.0, power), which results in 0 too.
2188 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to
2190 shader_addline(ins->ctx->buffer,
2191 "vec4(1.0, max(%s, 0.0), pow(max(0.0, %s) * step(0.0, %s), clamp(%s, -128.0, 128.0)), 1.0)%s);\n",
2192 src0_param.param_str, src1_param.param_str, src0_param.param_str, src3_param.param_str, dst_mask);
2195 /** Process the WINED3DSIO_DST instruction in GLSL:
2196 * dst.x = 1.0
2197 * dst.y = src0.x * src0.y
2198 * dst.z = src0.z
2199 * dst.w = src1.w
2201 static void shader_glsl_dst(const struct wined3d_shader_instruction *ins)
2203 glsl_src_param_t src0y_param;
2204 glsl_src_param_t src0z_param;
2205 glsl_src_param_t src1y_param;
2206 glsl_src_param_t src1w_param;
2207 char dst_mask[6];
2209 shader_glsl_append_dst(ins->ctx->buffer, ins);
2210 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2212 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src0y_param);
2213 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &src0z_param);
2214 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_1, &src1y_param);
2215 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_3, &src1w_param);
2217 shader_addline(ins->ctx->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
2218 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
2221 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
2222 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
2223 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
2225 * dst.x = cos(src0.?)
2226 * dst.y = sin(src0.?)
2227 * dst.z = dst.z
2228 * dst.w = dst.w
2230 static void shader_glsl_sincos(const struct wined3d_shader_instruction *ins)
2232 glsl_src_param_t src0_param;
2233 DWORD write_mask;
2235 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2236 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2238 switch (write_mask) {
2239 case WINED3DSP_WRITEMASK_0:
2240 shader_addline(ins->ctx->buffer, "cos(%s));\n", src0_param.param_str);
2241 break;
2243 case WINED3DSP_WRITEMASK_1:
2244 shader_addline(ins->ctx->buffer, "sin(%s));\n", src0_param.param_str);
2245 break;
2247 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
2248 shader_addline(ins->ctx->buffer, "vec2(cos(%s), sin(%s)));\n", src0_param.param_str, src0_param.param_str);
2249 break;
2251 default:
2252 ERR("Write mask should be .x, .y or .xy\n");
2253 break;
2257 /** Process the WINED3DSIO_LOOP instruction in GLSL:
2258 * Start a for() loop where src1.y is the initial value of aL,
2259 * increment aL by src1.z for a total of src1.x iterations.
2260 * Need to use a temporary variable for this operation.
2262 /* FIXME: I don't think nested loops will work correctly this way. */
2263 static void shader_glsl_loop(const struct wined3d_shader_instruction *ins)
2265 glsl_src_param_t src1_param;
2266 IWineD3DBaseShaderImpl *shader = (IWineD3DBaseShaderImpl *)ins->ctx->shader;
2267 const DWORD *control_values = NULL;
2268 const local_constant *constant;
2270 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &src1_param);
2272 /* Try to hardcode the loop control parameters if possible. Direct3D 9 class hardware doesn't support real
2273 * varying indexing, but Microsoft designed this feature for Shader model 2.x+. If the loop control is
2274 * known at compile time, the GLSL compiler can unroll the loop, and replace indirect addressing with direct
2275 * addressing.
2277 if (ins->src[1].register_type == WINED3DSPR_CONSTINT)
2279 LIST_FOR_EACH_ENTRY(constant, &shader->baseShader.constantsI, local_constant, entry) {
2280 if (constant->idx == ins->src[1].register_idx)
2282 control_values = constant->value;
2283 break;
2288 if(control_values) {
2289 if(control_values[2] > 0) {
2290 shader_addline(ins->ctx->buffer, "for (aL%u = %d; aL%u < (%d * %d + %d); aL%u += %d) {\n",
2291 shader->baseShader.cur_loop_depth, control_values[1],
2292 shader->baseShader.cur_loop_depth, control_values[0], control_values[2], control_values[1],
2293 shader->baseShader.cur_loop_depth, control_values[2]);
2294 } else if(control_values[2] == 0) {
2295 shader_addline(ins->ctx->buffer, "for (aL%u = %d, tmpInt%u = 0; tmpInt%u < %d; tmpInt%u++) {\n",
2296 shader->baseShader.cur_loop_depth, control_values[1], shader->baseShader.cur_loop_depth,
2297 shader->baseShader.cur_loop_depth, control_values[0],
2298 shader->baseShader.cur_loop_depth);
2299 } else {
2300 shader_addline(ins->ctx->buffer, "for (aL%u = %d; aL%u > (%d * %d + %d); aL%u += %d) {\n",
2301 shader->baseShader.cur_loop_depth, control_values[1],
2302 shader->baseShader.cur_loop_depth, control_values[0], control_values[2], control_values[1],
2303 shader->baseShader.cur_loop_depth, control_values[2]);
2305 } else {
2306 shader_addline(ins->ctx->buffer,
2307 "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z) {\n",
2308 shader->baseShader.cur_loop_depth, shader->baseShader.cur_loop_regno,
2309 src1_param.reg_name, shader->baseShader.cur_loop_depth, src1_param.reg_name,
2310 shader->baseShader.cur_loop_depth, shader->baseShader.cur_loop_regno, src1_param.reg_name);
2313 shader->baseShader.cur_loop_depth++;
2314 shader->baseShader.cur_loop_regno++;
2317 static void shader_glsl_end(const struct wined3d_shader_instruction *ins)
2319 IWineD3DBaseShaderImpl *shader = (IWineD3DBaseShaderImpl *)ins->ctx->shader;
2321 shader_addline(ins->ctx->buffer, "}\n");
2323 if (ins->handler_idx == WINED3DSIH_ENDLOOP)
2325 shader->baseShader.cur_loop_depth--;
2326 shader->baseShader.cur_loop_regno--;
2329 if (ins->handler_idx == WINED3DSIH_ENDREP)
2331 shader->baseShader.cur_loop_depth--;
2335 static void shader_glsl_rep(const struct wined3d_shader_instruction *ins)
2337 IWineD3DBaseShaderImpl *shader = (IWineD3DBaseShaderImpl *)ins->ctx->shader;
2338 glsl_src_param_t src0_param;
2340 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2341 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
2342 shader->baseShader.cur_loop_depth, shader->baseShader.cur_loop_depth,
2343 src0_param.param_str, shader->baseShader.cur_loop_depth);
2344 shader->baseShader.cur_loop_depth++;
2347 static void shader_glsl_if(const struct wined3d_shader_instruction *ins)
2349 glsl_src_param_t src0_param;
2351 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2352 shader_addline(ins->ctx->buffer, "if (%s) {\n", src0_param.param_str);
2355 static void shader_glsl_ifc(const struct wined3d_shader_instruction *ins)
2357 glsl_src_param_t src0_param;
2358 glsl_src_param_t src1_param;
2360 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2361 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
2363 shader_addline(ins->ctx->buffer, "if (%s %s %s) {\n",
2364 src0_param.param_str, shader_get_comp_op(ins->flags), src1_param.param_str);
2367 static void shader_glsl_else(const struct wined3d_shader_instruction *ins)
2369 shader_addline(ins->ctx->buffer, "} else {\n");
2372 static void shader_glsl_break(const struct wined3d_shader_instruction *ins)
2374 shader_addline(ins->ctx->buffer, "break;\n");
2377 /* FIXME: According to MSDN the compare is done per component. */
2378 static void shader_glsl_breakc(const struct wined3d_shader_instruction *ins)
2380 glsl_src_param_t src0_param;
2381 glsl_src_param_t src1_param;
2383 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2384 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
2386 shader_addline(ins->ctx->buffer, "if (%s %s %s) break;\n",
2387 src0_param.param_str, shader_get_comp_op(ins->flags), src1_param.param_str);
2390 static void shader_glsl_label(const struct wined3d_shader_instruction *ins)
2392 shader_addline(ins->ctx->buffer, "}\n");
2393 shader_addline(ins->ctx->buffer, "void subroutine%u () {\n", ins->src[0].register_idx);
2396 static void shader_glsl_call(const struct wined3d_shader_instruction *ins)
2398 shader_addline(ins->ctx->buffer, "subroutine%u();\n", ins->src[0].register_idx);
2401 static void shader_glsl_callnz(const struct wined3d_shader_instruction *ins)
2403 glsl_src_param_t src1_param;
2405 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
2406 shader_addline(ins->ctx->buffer, "if (%s) subroutine%u();\n", src1_param.param_str, ins->src[0].register_idx);
2409 /*********************************************
2410 * Pixel Shader Specific Code begins here
2411 ********************************************/
2412 static void pshader_glsl_tex(const struct wined3d_shader_instruction *ins)
2414 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)ins->ctx->shader;
2415 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
2416 DWORD shader_version = ins->ctx->reg_maps->shader_version;
2417 glsl_sample_function_t sample_function;
2418 DWORD sample_flags = 0;
2419 DWORD sampler_type;
2420 DWORD sampler_idx;
2421 DWORD mask = 0, swizzle;
2423 /* 1.0-1.4: Use destination register as sampler source.
2424 * 2.0+: Use provided sampler source. */
2425 if (shader_version < WINED3DPS_VERSION(2,0)) sampler_idx = ins->dst[0].register_idx;
2426 else sampler_idx = ins->src[1].register_idx;
2427 sampler_type = ins->ctx->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
2429 if (shader_version < WINED3DPS_VERSION(1,4))
2431 DWORD flags = deviceImpl->stateBlock->textureState[sampler_idx][WINED3DTSS_TEXTURETRANSFORMFLAGS];
2433 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
2434 if (flags & WINED3DTTFF_PROJECTED && sampler_type != WINED3DSTT_CUBE) {
2435 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
2436 switch (flags & ~WINED3DTTFF_PROJECTED) {
2437 case WINED3DTTFF_COUNT1: FIXME("WINED3DTTFF_PROJECTED with WINED3DTTFF_COUNT1?\n"); break;
2438 case WINED3DTTFF_COUNT2: mask = WINED3DSP_WRITEMASK_1; break;
2439 case WINED3DTTFF_COUNT3: mask = WINED3DSP_WRITEMASK_2; break;
2440 case WINED3DTTFF_COUNT4:
2441 case WINED3DTTFF_DISABLE: mask = WINED3DSP_WRITEMASK_3; break;
2445 else if (shader_version < WINED3DPS_VERSION(2,0))
2447 DWORD src_mod = ins->src[0].modifiers;
2449 if (src_mod == WINED3DSPSM_DZ) {
2450 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
2451 mask = WINED3DSP_WRITEMASK_2;
2452 } else if (src_mod == WINED3DSPSM_DW) {
2453 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
2454 mask = WINED3DSP_WRITEMASK_3;
2456 } else {
2457 if (ins->flags & WINED3DSI_TEXLD_PROJECT)
2459 /* ps 2.0 texldp instruction always divides by the fourth component. */
2460 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
2461 mask = WINED3DSP_WRITEMASK_3;
2465 if(deviceImpl->stateBlock->textures[sampler_idx] &&
2466 IWineD3DBaseTexture_GetTextureDimensions(deviceImpl->stateBlock->textures[sampler_idx]) == GL_TEXTURE_RECTANGLE_ARB) {
2467 sample_flags |= WINED3D_GLSL_SAMPLE_RECT;
2470 shader_glsl_get_sample_function(sampler_type, sample_flags, &sample_function);
2471 mask |= sample_function.coord_mask;
2473 if (shader_version < WINED3DPS_VERSION(2,0)) swizzle = WINED3DSP_NOSWIZZLE;
2474 else swizzle = ins->src[1].swizzle;
2476 /* 1.0-1.3: Use destination register as coordinate source.
2477 1.4+: Use provided coordinate source register. */
2478 if (shader_version < WINED3DPS_VERSION(1,4))
2480 char coord_mask[6];
2481 shader_glsl_write_mask_to_str(mask, coord_mask);
2482 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL,
2483 "T%u%s", sampler_idx, coord_mask);
2484 } else {
2485 glsl_src_param_t coord_param;
2486 shader_glsl_add_src_param(ins, &ins->src[0], mask, &coord_param);
2487 if (ins->flags & WINED3DSI_TEXLD_BIAS)
2489 glsl_src_param_t bias;
2490 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &bias);
2491 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, bias.param_str,
2492 "%s", coord_param.param_str);
2493 } else {
2494 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL,
2495 "%s", coord_param.param_str);
2500 static void shader_glsl_texldl(const struct wined3d_shader_instruction *ins)
2502 IWineD3DBaseShaderImpl *This = (IWineD3DBaseShaderImpl *)ins->ctx->shader;
2503 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
2504 glsl_sample_function_t sample_function;
2505 glsl_src_param_t coord_param, lod_param;
2506 DWORD sample_flags = WINED3D_GLSL_SAMPLE_LOD;
2507 DWORD sampler_type;
2508 DWORD sampler_idx;
2509 DWORD swizzle = ins->src[1].swizzle;
2511 sampler_idx = ins->src[1].register_idx;
2512 sampler_type = ins->ctx->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
2513 if(deviceImpl->stateBlock->textures[sampler_idx] &&
2514 IWineD3DBaseTexture_GetTextureDimensions(deviceImpl->stateBlock->textures[sampler_idx]) == GL_TEXTURE_RECTANGLE_ARB) {
2515 sample_flags |= WINED3D_GLSL_SAMPLE_RECT;
2517 shader_glsl_get_sample_function(sampler_type, sample_flags, &sample_function);
2518 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
2520 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
2522 if (shader_is_pshader_version(ins->ctx->reg_maps->shader_version))
2524 /* The GLSL spec claims the Lod sampling functions are only supported in vertex shaders.
2525 * However, they seem to work just fine in fragment shaders as well. */
2526 WARN("Using %s in fragment shader.\n", sample_function.name);
2528 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, lod_param.param_str,
2529 "%s", coord_param.param_str);
2532 static void pshader_glsl_texcoord(const struct wined3d_shader_instruction *ins)
2534 /* FIXME: Make this work for more than just 2D textures */
2535 SHADER_BUFFER *buffer = ins->ctx->buffer;
2536 DWORD write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2538 if (ins->ctx->reg_maps->shader_version != WINED3DPS_VERSION(1,4))
2540 char dst_mask[6];
2542 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2543 shader_addline(buffer, "clamp(gl_TexCoord[%u], 0.0, 1.0)%s);\n",
2544 ins->dst[0].register_idx, dst_mask);
2545 } else {
2546 DWORD reg = ins->src[0].register_idx;
2547 DWORD src_mod = ins->src[0].modifiers;
2548 char dst_swizzle[6];
2550 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
2552 if (src_mod == WINED3DSPSM_DZ) {
2553 glsl_src_param_t div_param;
2554 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
2555 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &div_param);
2557 if (mask_size > 1) {
2558 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
2559 } else {
2560 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
2562 } else if (src_mod == WINED3DSPSM_DW) {
2563 glsl_src_param_t div_param;
2564 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
2565 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &div_param);
2567 if (mask_size > 1) {
2568 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
2569 } else {
2570 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
2572 } else {
2573 shader_addline(buffer, "gl_TexCoord[%u]%s);\n", reg, dst_swizzle);
2578 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
2579 * Take a 3-component dot product of the TexCoord[dstreg] and src,
2580 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
2581 static void pshader_glsl_texdp3tex(const struct wined3d_shader_instruction *ins)
2583 glsl_src_param_t src0_param;
2584 glsl_sample_function_t sample_function;
2585 DWORD sampler_idx = ins->dst[0].register_idx;
2586 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2587 DWORD sampler_type = ins->ctx->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
2588 UINT mask_size;
2590 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
2592 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
2593 * scalar, and projected sampling would require 4.
2595 * It is a dependent read - not valid with conditional NP2 textures
2597 shader_glsl_get_sample_function(sampler_type, 0, &sample_function);
2598 mask_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
2600 switch(mask_size)
2602 case 1:
2603 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL,
2604 "dot(gl_TexCoord[%u].xyz, %s)", sampler_idx, src0_param.param_str);
2605 break;
2607 case 2:
2608 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL,
2609 "vec2(dot(gl_TexCoord[%u].xyz, %s), 0.0)", sampler_idx, src0_param.param_str);
2610 break;
2612 case 3:
2613 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL,
2614 "vec3(dot(gl_TexCoord[%u].xyz, %s), 0.0, 0.0)", sampler_idx, src0_param.param_str);
2615 break;
2617 default:
2618 FIXME("Unexpected mask size %u\n", mask_size);
2619 break;
2623 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
2624 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
2625 static void pshader_glsl_texdp3(const struct wined3d_shader_instruction *ins)
2627 glsl_src_param_t src0_param;
2628 DWORD dstreg = ins->dst[0].register_idx;
2629 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2630 DWORD dst_mask;
2631 unsigned int mask_size;
2633 dst_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2634 mask_size = shader_glsl_get_write_mask_size(dst_mask);
2635 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
2637 if (mask_size > 1) {
2638 shader_addline(ins->ctx->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
2639 } else {
2640 shader_addline(ins->ctx->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
2644 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
2645 * Calculate the depth as dst.x / dst.y */
2646 static void pshader_glsl_texdepth(const struct wined3d_shader_instruction *ins)
2648 glsl_dst_param_t dst_param;
2650 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
2652 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
2653 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
2654 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
2655 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
2656 * >= 1.0 or < 0.0
2658 shader_addline(ins->ctx->buffer, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n",
2659 dst_param.reg_name, dst_param.reg_name);
2662 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
2663 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
2664 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
2665 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
2667 static void pshader_glsl_texm3x2depth(const struct wined3d_shader_instruction *ins)
2669 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2670 DWORD dstreg = ins->dst[0].register_idx;
2671 glsl_src_param_t src0_param;
2673 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
2675 shader_addline(ins->ctx->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
2676 shader_addline(ins->ctx->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
2679 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
2680 * Calculate the 1st of a 2-row matrix multiplication. */
2681 static void pshader_glsl_texm3x2pad(const struct wined3d_shader_instruction *ins)
2683 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2684 DWORD reg = ins->dst[0].register_idx;
2685 SHADER_BUFFER *buffer = ins->ctx->buffer;
2686 glsl_src_param_t src0_param;
2688 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
2689 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
2692 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
2693 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
2694 static void pshader_glsl_texm3x3pad(const struct wined3d_shader_instruction *ins)
2696 IWineD3DPixelShaderImpl *shader = (IWineD3DPixelShaderImpl *)ins->ctx->shader;
2697 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2698 DWORD reg = ins->dst[0].register_idx;
2699 SHADER_BUFFER *buffer = ins->ctx->buffer;
2700 SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
2701 glsl_src_param_t src0_param;
2703 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
2704 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + current_state->current_row, reg, src0_param.param_str);
2705 current_state->texcoord_w[current_state->current_row++] = reg;
2708 static void pshader_glsl_texm3x2tex(const struct wined3d_shader_instruction *ins)
2710 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2711 DWORD reg = ins->dst[0].register_idx;
2712 SHADER_BUFFER *buffer = ins->ctx->buffer;
2713 glsl_src_param_t src0_param;
2714 DWORD sampler_type = ins->ctx->reg_maps->samplers[reg] & WINED3DSP_TEXTURETYPE_MASK;
2715 glsl_sample_function_t sample_function;
2717 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
2718 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
2720 shader_glsl_get_sample_function(sampler_type, 0, &sample_function);
2722 /* Sample the texture using the calculated coordinates */
2723 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, "tmp0.xy");
2726 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
2727 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
2728 static void pshader_glsl_texm3x3tex(const struct wined3d_shader_instruction *ins)
2730 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2731 glsl_src_param_t src0_param;
2732 DWORD reg = ins->dst[0].register_idx;
2733 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)ins->ctx->shader;
2734 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
2735 DWORD sampler_type = ins->ctx->reg_maps->samplers[reg] & WINED3DSP_TEXTURETYPE_MASK;
2736 glsl_sample_function_t sample_function;
2738 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
2739 shader_addline(ins->ctx->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
2741 /* Dependent read, not valid with conditional NP2 */
2742 shader_glsl_get_sample_function(sampler_type, 0, &sample_function);
2744 /* Sample the texture using the calculated coordinates */
2745 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, "tmp0.xyz");
2747 current_state->current_row = 0;
2750 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
2751 * Perform the 3rd row of a 3x3 matrix multiply */
2752 static void pshader_glsl_texm3x3(const struct wined3d_shader_instruction *ins)
2754 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2755 glsl_src_param_t src0_param;
2756 char dst_mask[6];
2757 DWORD reg = ins->dst[0].register_idx;
2758 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)ins->ctx->shader;
2759 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
2761 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
2763 shader_glsl_append_dst(ins->ctx->buffer, ins);
2764 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2765 shader_addline(ins->ctx->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
2767 current_state->current_row = 0;
2770 /** Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
2771 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
2772 static void pshader_glsl_texm3x3spec(const struct wined3d_shader_instruction *ins)
2774 IWineD3DPixelShaderImpl *shader = (IWineD3DPixelShaderImpl *)ins->ctx->shader;
2775 DWORD reg = ins->dst[0].register_idx;
2776 glsl_src_param_t src0_param;
2777 glsl_src_param_t src1_param;
2778 SHADER_BUFFER *buffer = ins->ctx->buffer;
2779 SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
2780 DWORD stype = ins->ctx->reg_maps->samplers[reg] & WINED3DSP_TEXTURETYPE_MASK;
2781 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2782 glsl_sample_function_t sample_function;
2784 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
2785 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
2787 /* Perform the last matrix multiply operation */
2788 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
2789 /* Reflection calculation */
2790 shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
2792 /* Dependent read, not valid with conditional NP2 */
2793 shader_glsl_get_sample_function(stype, 0, &sample_function);
2795 /* Sample the texture */
2796 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, "tmp0.xyz");
2798 current_state->current_row = 0;
2801 /** Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
2802 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
2803 static void pshader_glsl_texm3x3vspec(const struct wined3d_shader_instruction *ins)
2805 IWineD3DPixelShaderImpl *shader = (IWineD3DPixelShaderImpl *)ins->ctx->shader;
2806 DWORD reg = ins->dst[0].register_idx;
2807 SHADER_BUFFER *buffer = ins->ctx->buffer;
2808 SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
2809 glsl_src_param_t src0_param;
2810 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2811 DWORD sampler_type = ins->ctx->reg_maps->samplers[reg] & WINED3DSP_TEXTURETYPE_MASK;
2812 glsl_sample_function_t sample_function;
2814 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
2816 /* Perform the last matrix multiply operation */
2817 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
2819 /* Construct the eye-ray vector from w coordinates */
2820 shader_addline(buffer, "tmp1.xyz = normalize(vec3(gl_TexCoord[%u].w, gl_TexCoord[%u].w, gl_TexCoord[%u].w));\n",
2821 current_state->texcoord_w[0], current_state->texcoord_w[1], reg);
2822 shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
2824 /* Dependent read, not valid with conditional NP2 */
2825 shader_glsl_get_sample_function(sampler_type, 0, &sample_function);
2827 /* Sample the texture using the calculated coordinates */
2828 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, "tmp0.xyz");
2830 current_state->current_row = 0;
2833 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
2834 * Apply a fake bump map transform.
2835 * texbem is pshader <= 1.3 only, this saves a few version checks
2837 static void pshader_glsl_texbem(const struct wined3d_shader_instruction *ins)
2839 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)ins->ctx->shader;
2840 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
2841 glsl_sample_function_t sample_function;
2842 glsl_src_param_t coord_param;
2843 DWORD sampler_type;
2844 DWORD sampler_idx;
2845 DWORD mask;
2846 DWORD flags;
2847 char coord_mask[6];
2849 sampler_idx = ins->dst[0].register_idx;
2850 flags = deviceImpl->stateBlock->textureState[sampler_idx][WINED3DTSS_TEXTURETRANSFORMFLAGS];
2852 sampler_type = ins->ctx->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
2853 /* Dependent read, not valid with conditional NP2 */
2854 shader_glsl_get_sample_function(sampler_type, 0, &sample_function);
2855 mask = sample_function.coord_mask;
2857 shader_glsl_write_mask_to_str(mask, coord_mask);
2859 /* with projective textures, texbem only divides the static texture coord, not the displacement,
2860 * so we can't let the GL handle this.
2862 if (flags & WINED3DTTFF_PROJECTED) {
2863 DWORD div_mask=0;
2864 char coord_div_mask[3];
2865 switch (flags & ~WINED3DTTFF_PROJECTED) {
2866 case WINED3DTTFF_COUNT1: FIXME("WINED3DTTFF_PROJECTED with WINED3DTTFF_COUNT1?\n"); break;
2867 case WINED3DTTFF_COUNT2: div_mask = WINED3DSP_WRITEMASK_1; break;
2868 case WINED3DTTFF_COUNT3: div_mask = WINED3DSP_WRITEMASK_2; break;
2869 case WINED3DTTFF_COUNT4:
2870 case WINED3DTTFF_DISABLE: div_mask = WINED3DSP_WRITEMASK_3; break;
2872 shader_glsl_write_mask_to_str(div_mask, coord_div_mask);
2873 shader_addline(ins->ctx->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
2876 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &coord_param);
2878 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL,
2879 "T%u%s + vec4(bumpenvmat%d * %s, 0.0, 0.0)%s", sampler_idx, coord_mask, sampler_idx,
2880 coord_param.param_str, coord_mask);
2882 if (ins->handler_idx == WINED3DSIH_TEXBEML)
2884 glsl_src_param_t luminance_param;
2885 glsl_dst_param_t dst_param;
2887 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &luminance_param);
2888 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
2890 shader_addline(ins->ctx->buffer, "%s%s *= (%s * luminancescale%d + luminanceoffset%d);\n",
2891 dst_param.reg_name, dst_param.mask_str,
2892 luminance_param.param_str, sampler_idx, sampler_idx);
2896 static void pshader_glsl_bem(const struct wined3d_shader_instruction *ins)
2898 glsl_src_param_t src0_param, src1_param;
2899 DWORD sampler_idx = ins->dst[0].register_idx;
2901 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
2902 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
2904 shader_glsl_append_dst(ins->ctx->buffer, ins);
2905 shader_addline(ins->ctx->buffer, "%s + bumpenvmat%d * %s);\n",
2906 src0_param.param_str, sampler_idx, src1_param.param_str);
2909 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
2910 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
2911 static void pshader_glsl_texreg2ar(const struct wined3d_shader_instruction *ins)
2913 glsl_src_param_t src0_param;
2914 DWORD sampler_idx = ins->dst[0].register_idx;
2915 DWORD sampler_type = ins->ctx->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
2916 glsl_sample_function_t sample_function;
2918 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
2920 shader_glsl_get_sample_function(sampler_type, 0, &sample_function);
2921 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL,
2922 "%s.wx", src0_param.reg_name);
2925 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
2926 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
2927 static void pshader_glsl_texreg2gb(const struct wined3d_shader_instruction *ins)
2929 glsl_src_param_t src0_param;
2930 DWORD sampler_idx = ins->dst[0].register_idx;
2931 DWORD sampler_type = ins->ctx->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
2932 glsl_sample_function_t sample_function;
2934 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
2936 shader_glsl_get_sample_function(sampler_type, 0, &sample_function);
2937 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL,
2938 "%s.yz", src0_param.reg_name);
2941 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
2942 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
2943 static void pshader_glsl_texreg2rgb(const struct wined3d_shader_instruction *ins)
2945 glsl_src_param_t src0_param;
2946 DWORD sampler_idx = ins->dst[0].register_idx;
2947 DWORD sampler_type = ins->ctx->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
2948 glsl_sample_function_t sample_function;
2950 /* Dependent read, not valid with conditional NP2 */
2951 shader_glsl_get_sample_function(sampler_type, 0, &sample_function);
2952 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &src0_param);
2954 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL,
2955 "%s", src0_param.param_str);
2958 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
2959 * If any of the first 3 components are < 0, discard this pixel */
2960 static void pshader_glsl_texkill(const struct wined3d_shader_instruction *ins)
2962 glsl_dst_param_t dst_param;
2964 /* The argument is a destination parameter, and no writemasks are allowed */
2965 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
2966 if ((ins->ctx->reg_maps->shader_version >= WINED3DPS_VERSION(2,0)))
2968 /* 2.0 shaders compare all 4 components in texkill */
2969 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
2970 } else {
2971 /* 1.X shaders only compare the first 3 components, probably due to the nature of the texkill
2972 * instruction as a tex* instruction, and phase, which kills all a / w components. Even if all
2973 * 4 components are defined, only the first 3 are used
2975 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
2979 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
2980 * dst = dot2(src0, src1) + src2 */
2981 static void pshader_glsl_dp2add(const struct wined3d_shader_instruction *ins)
2983 glsl_src_param_t src0_param;
2984 glsl_src_param_t src1_param;
2985 glsl_src_param_t src2_param;
2986 DWORD write_mask;
2987 unsigned int mask_size;
2989 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2990 mask_size = shader_glsl_get_write_mask_size(write_mask);
2992 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
2993 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
2994 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &src2_param);
2996 if (mask_size > 1) {
2997 shader_addline(ins->ctx->buffer, "vec%d(dot(%s, %s) + %s));\n",
2998 mask_size, src0_param.param_str, src1_param.param_str, src2_param.param_str);
2999 } else {
3000 shader_addline(ins->ctx->buffer, "dot(%s, %s) + %s);\n",
3001 src0_param.param_str, src1_param.param_str, src2_param.param_str);
3005 static void pshader_glsl_input_pack(IWineD3DPixelShader *iface, SHADER_BUFFER *buffer,
3006 const struct wined3d_shader_semantic *semantics_in, const struct shader_reg_maps *reg_maps,
3007 enum vertexprocessing_mode vertexprocessing)
3009 unsigned int i;
3010 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)iface;
3012 for (i = 0; i < MAX_REG_INPUT; ++i)
3014 DWORD usage, usage_idx;
3015 char reg_mask[6];
3017 /* Unused */
3018 if (!reg_maps->packed_input[i]) continue;
3020 usage = semantics_in[i].usage;
3021 usage_idx = semantics_in[i].usage_idx;
3022 shader_glsl_get_write_mask(&semantics_in[i].reg, reg_mask);
3024 switch (usage)
3026 case WINED3DDECLUSAGE_TEXCOORD:
3027 if (usage_idx < 8 && vertexprocessing == pretransformed)
3028 shader_addline(buffer, "IN[%u]%s = gl_TexCoord[%u]%s;\n",
3029 This->input_reg_map[i], reg_mask, usage_idx, reg_mask);
3030 else
3031 shader_addline(buffer, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
3032 This->input_reg_map[i], reg_mask, reg_mask);
3033 break;
3035 case WINED3DDECLUSAGE_COLOR:
3036 if (usage_idx == 0)
3037 shader_addline(buffer, "IN[%u]%s = vec4(gl_Color)%s;\n",
3038 This->input_reg_map[i], reg_mask, reg_mask);
3039 else if (usage_idx == 1)
3040 shader_addline(buffer, "IN[%u]%s = vec4(gl_SecondaryColor)%s;\n",
3041 This->input_reg_map[i], reg_mask, reg_mask);
3042 else
3043 shader_addline(buffer, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
3044 This->input_reg_map[i], reg_mask, reg_mask);
3045 break;
3047 default:
3048 shader_addline(buffer, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
3049 This->input_reg_map[i], reg_mask, reg_mask);
3050 break;
3055 /*********************************************
3056 * Vertex Shader Specific Code begins here
3057 ********************************************/
3059 static void add_glsl_program_entry(struct shader_glsl_priv *priv, struct glsl_shader_prog_link *entry) {
3060 glsl_program_key_t *key;
3062 key = HeapAlloc(GetProcessHeap(), 0, sizeof(glsl_program_key_t));
3063 key->vshader = entry->vshader;
3064 key->pshader = entry->pshader;
3065 key->vs_args = entry->vs_args;
3066 key->ps_args = entry->ps_args;
3068 hash_table_put(priv->glsl_program_lookup, key, entry);
3071 static struct glsl_shader_prog_link *get_glsl_program_entry(struct shader_glsl_priv *priv,
3072 IWineD3DVertexShader *vshader, IWineD3DPixelShader *pshader, struct vs_compile_args *vs_args,
3073 struct ps_compile_args *ps_args) {
3074 glsl_program_key_t key;
3076 key.vshader = vshader;
3077 key.pshader = pshader;
3078 key.vs_args = *vs_args;
3079 key.ps_args = *ps_args;
3081 return hash_table_get(priv->glsl_program_lookup, &key);
3084 static void delete_glsl_program_entry(struct shader_glsl_priv *priv, const WineD3D_GL_Info *gl_info,
3085 struct glsl_shader_prog_link *entry)
3087 glsl_program_key_t *key;
3089 key = HeapAlloc(GetProcessHeap(), 0, sizeof(glsl_program_key_t));
3090 key->vshader = entry->vshader;
3091 key->pshader = entry->pshader;
3092 key->vs_args = entry->vs_args;
3093 key->ps_args = entry->ps_args;
3094 hash_table_remove(priv->glsl_program_lookup, key);
3096 GL_EXTCALL(glDeleteObjectARB(entry->programId));
3097 if (entry->vshader) list_remove(&entry->vshader_entry);
3098 if (entry->pshader) list_remove(&entry->pshader_entry);
3099 HeapFree(GetProcessHeap(), 0, entry->vuniformF_locations);
3100 HeapFree(GetProcessHeap(), 0, entry->puniformF_locations);
3101 HeapFree(GetProcessHeap(), 0, entry);
3104 static void handle_ps3_input(SHADER_BUFFER *buffer, const WineD3D_GL_Info *gl_info, const DWORD *map,
3105 const struct wined3d_shader_semantic *semantics_in, const struct shader_reg_maps *reg_maps_in,
3106 const struct wined3d_shader_semantic *semantics_out, const struct shader_reg_maps *reg_maps_out)
3108 unsigned int i, j;
3109 DWORD usage, usage_idx, usage_out, usage_idx_out;
3110 DWORD *set;
3111 DWORD in_idx;
3112 DWORD in_count = GL_LIMITS(glsl_varyings) / 4;
3113 char reg_mask[6], reg_mask_out[6];
3114 char destination[50];
3116 set = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*set) * (in_count + 2));
3118 if (!semantics_out) {
3119 /* Save gl_FrontColor & gl_FrontSecondaryColor before overwriting them. */
3120 shader_addline(buffer, "vec4 front_color = gl_FrontColor;\n");
3121 shader_addline(buffer, "vec4 front_secondary_color = gl_FrontSecondaryColor;\n");
3124 for(i = 0; i < MAX_REG_INPUT; i++) {
3125 if (!reg_maps_in->packed_input[i]) continue;
3127 in_idx = map[i];
3128 if (in_idx >= (in_count + 2)) {
3129 FIXME("More input varyings declared than supported, expect issues\n");
3130 continue;
3132 else if (map[i] == ~0U)
3134 /* Declared, but not read register */
3135 continue;
3138 if (in_idx == in_count) {
3139 sprintf(destination, "gl_FrontColor");
3140 } else if (in_idx == in_count + 1) {
3141 sprintf(destination, "gl_FrontSecondaryColor");
3142 } else {
3143 sprintf(destination, "IN[%u]", in_idx);
3146 usage = semantics_in[i].usage;
3147 usage_idx = semantics_in[i].usage_idx;
3148 set[map[i]] = shader_glsl_get_write_mask(&semantics_in[i].reg, reg_mask);
3150 if(!semantics_out) {
3151 switch(usage) {
3152 case WINED3DDECLUSAGE_COLOR:
3153 if (usage_idx == 0)
3154 shader_addline(buffer, "%s%s = front_color%s;\n",
3155 destination, reg_mask, reg_mask);
3156 else if (usage_idx == 1)
3157 shader_addline(buffer, "%s%s = front_secondary_color%s;\n",
3158 destination, reg_mask, reg_mask);
3159 else
3160 shader_addline(buffer, "%s%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
3161 destination, reg_mask, reg_mask);
3162 break;
3164 case WINED3DDECLUSAGE_TEXCOORD:
3165 if (usage_idx < 8) {
3166 shader_addline(buffer, "%s%s = gl_TexCoord[%u]%s;\n",
3167 destination, reg_mask, usage_idx, reg_mask);
3168 } else {
3169 shader_addline(buffer, "%s%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
3170 destination, reg_mask, reg_mask);
3172 break;
3174 case WINED3DDECLUSAGE_FOG:
3175 shader_addline(buffer, "%s%s = vec4(gl_FogFragCoord, 0.0, 0.0, 0.0)%s;\n",
3176 destination, reg_mask, reg_mask);
3177 break;
3179 default:
3180 shader_addline(buffer, "%s%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
3181 destination, reg_mask, reg_mask);
3183 } else {
3184 BOOL found = FALSE;
3185 for(j = 0; j < MAX_REG_OUTPUT; j++) {
3186 if (!reg_maps_out->packed_output[j]) continue;
3188 usage_out = semantics_out[j].usage;
3189 usage_idx_out = semantics_out[j].usage_idx;
3190 shader_glsl_get_write_mask(&semantics_out[j].reg, reg_mask_out);
3192 if(usage == usage_out &&
3193 usage_idx == usage_idx_out) {
3194 shader_addline(buffer, "%s%s = OUT[%u]%s;\n",
3195 destination, reg_mask, j, reg_mask);
3196 found = TRUE;
3199 if(!found) {
3200 shader_addline(buffer, "%s%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
3201 destination, reg_mask, reg_mask);
3206 /* This is solely to make the compiler / linker happy and avoid warning about undefined
3207 * varyings. It shouldn't result in any real code executed on the GPU, since all read
3208 * input varyings are assigned above, if the optimizer works properly.
3210 for(i = 0; i < in_count + 2; i++) {
3211 if(set[i] != WINED3DSP_WRITEMASK_ALL) {
3212 unsigned int size = 0;
3213 memset(reg_mask, 0, sizeof(reg_mask));
3214 if(!(set[i] & WINED3DSP_WRITEMASK_0)) {
3215 reg_mask[size] = 'x';
3216 size++;
3218 if(!(set[i] & WINED3DSP_WRITEMASK_1)) {
3219 reg_mask[size] = 'y';
3220 size++;
3222 if(!(set[i] & WINED3DSP_WRITEMASK_2)) {
3223 reg_mask[size] = 'z';
3224 size++;
3226 if(!(set[i] & WINED3DSP_WRITEMASK_3)) {
3227 reg_mask[size] = 'w';
3228 size++;
3231 if (i == in_count) {
3232 sprintf(destination, "gl_FrontColor");
3233 } else if (i == in_count + 1) {
3234 sprintf(destination, "gl_FrontSecondaryColor");
3235 } else {
3236 sprintf(destination, "IN[%u]", i);
3239 if (size == 1) {
3240 shader_addline(buffer, "%s.%s = 0.0;\n", destination, reg_mask);
3241 } else {
3242 shader_addline(buffer, "%s.%s = vec%u(0.0);\n", destination, reg_mask, size);
3247 HeapFree(GetProcessHeap(), 0, set);
3250 static GLhandleARB generate_param_reorder_function(IWineD3DVertexShader *vertexshader,
3251 IWineD3DPixelShader *pixelshader, const WineD3D_GL_Info *gl_info)
3253 GLhandleARB ret = 0;
3254 IWineD3DVertexShaderImpl *vs = (IWineD3DVertexShaderImpl *) vertexshader;
3255 IWineD3DPixelShaderImpl *ps = (IWineD3DPixelShaderImpl *) pixelshader;
3256 IWineD3DDeviceImpl *device;
3257 DWORD vs_major = WINED3DSHADER_VERSION_MAJOR(vs->baseShader.reg_maps.shader_version);
3258 DWORD ps_major = ps ? WINED3DSHADER_VERSION_MAJOR(ps->baseShader.reg_maps.shader_version) : 0;
3259 unsigned int i;
3260 SHADER_BUFFER buffer;
3261 DWORD usage, usage_idx, writemask;
3262 char reg_mask[6];
3263 const struct wined3d_shader_semantic *semantics_out;
3265 shader_buffer_init(&buffer);
3267 shader_addline(&buffer, "#version 120\n");
3269 if(vs_major < 3 && ps_major < 3) {
3270 /* That one is easy: The vertex shader writes to the builtin varyings, the pixel shader reads from them.
3271 * Take care about the texcoord .w fixup though if we're using the fixed function fragment pipeline
3273 device = (IWineD3DDeviceImpl *) vs->baseShader.device;
3274 if((GLINFO_LOCATION).set_texcoord_w && ps_major == 0 && vs_major > 0 &&
3275 !device->frag_pipe->ffp_proj_control) {
3276 shader_addline(&buffer, "void order_ps_input() {\n");
3277 for(i = 0; i < min(8, MAX_REG_TEXCRD); i++) {
3278 if(vs->baseShader.reg_maps.texcoord_mask[i] != 0 &&
3279 vs->baseShader.reg_maps.texcoord_mask[i] != WINED3DSP_WRITEMASK_ALL) {
3280 shader_addline(&buffer, "gl_TexCoord[%u].w = 1.0;\n", i);
3283 shader_addline(&buffer, "}\n");
3284 } else {
3285 shader_addline(&buffer, "void order_ps_input() { /* do nothing */ }\n");
3287 } else if(ps_major < 3 && vs_major >= 3) {
3288 /* The vertex shader writes to its own varyings, the pixel shader needs them in the builtin ones */
3289 semantics_out = vs->semantics_out;
3291 shader_addline(&buffer, "void order_ps_input(in vec4 OUT[%u]) {\n", MAX_REG_OUTPUT);
3292 for(i = 0; i < MAX_REG_OUTPUT; i++) {
3293 if (!vs->baseShader.reg_maps.packed_output[i]) continue;
3295 usage = semantics_out[i].usage;
3296 usage_idx = semantics_out[i].usage_idx;
3297 writemask = shader_glsl_get_write_mask(&semantics_out[i].reg, reg_mask);
3299 switch(usage) {
3300 case WINED3DDECLUSAGE_COLOR:
3301 if (usage_idx == 0)
3302 shader_addline(&buffer, "gl_FrontColor%s = OUT[%u]%s;\n", reg_mask, i, reg_mask);
3303 else if (usage_idx == 1)
3304 shader_addline(&buffer, "gl_FrontSecondaryColor%s = OUT[%u]%s;\n", reg_mask, i, reg_mask);
3305 break;
3307 case WINED3DDECLUSAGE_POSITION:
3308 shader_addline(&buffer, "gl_Position%s = OUT[%u]%s;\n", reg_mask, i, reg_mask);
3309 break;
3311 case WINED3DDECLUSAGE_TEXCOORD:
3312 if (usage_idx < 8) {
3313 if(!(GLINFO_LOCATION).set_texcoord_w || ps_major > 0) writemask |= WINED3DSP_WRITEMASK_3;
3315 shader_addline(&buffer, "gl_TexCoord[%u]%s = OUT[%u]%s;\n",
3316 usage_idx, reg_mask, i, reg_mask);
3317 if(!(writemask & WINED3DSP_WRITEMASK_3)) {
3318 shader_addline(&buffer, "gl_TexCoord[%u].w = 1.0;\n", usage_idx);
3321 break;
3323 case WINED3DDECLUSAGE_PSIZE:
3324 shader_addline(&buffer, "gl_PointSize = OUT[%u].x;\n", i);
3325 break;
3327 case WINED3DDECLUSAGE_FOG:
3328 shader_addline(&buffer, "gl_FogFragCoord = OUT[%u].%c;\n", i, reg_mask[1]);
3329 break;
3331 default:
3332 break;
3335 shader_addline(&buffer, "}\n");
3337 } else if(ps_major >= 3 && vs_major >= 3) {
3338 semantics_out = vs->semantics_out;
3340 /* This one is tricky: a 3.0 pixel shader reads from a 3.0 vertex shader */
3341 shader_addline(&buffer, "varying vec4 IN[%u];\n", GL_LIMITS(glsl_varyings) / 4);
3342 shader_addline(&buffer, "void order_ps_input(in vec4 OUT[%u]) {\n", MAX_REG_OUTPUT);
3344 /* First, sort out position and point size. Those are not passed to the pixel shader */
3345 for(i = 0; i < MAX_REG_OUTPUT; i++) {
3346 if (!vs->baseShader.reg_maps.packed_output[i]) continue;
3348 usage = semantics_out[i].usage;
3349 usage_idx = semantics_out[i].usage_idx;
3350 shader_glsl_get_write_mask(&semantics_out[i].reg, reg_mask);
3352 switch(usage) {
3353 case WINED3DDECLUSAGE_POSITION:
3354 shader_addline(&buffer, "gl_Position%s = OUT[%u]%s;\n", reg_mask, i, reg_mask);
3355 break;
3357 case WINED3DDECLUSAGE_PSIZE:
3358 shader_addline(&buffer, "gl_PointSize = OUT[%u].x;\n", i);
3359 break;
3361 default:
3362 break;
3366 /* Then, fix the pixel shader input */
3367 handle_ps3_input(&buffer, gl_info, ps->input_reg_map,
3368 ps->semantics_in, &ps->baseShader.reg_maps, semantics_out, &vs->baseShader.reg_maps);
3370 shader_addline(&buffer, "}\n");
3371 } else if(ps_major >= 3 && vs_major < 3) {
3372 shader_addline(&buffer, "varying vec4 IN[%u];\n", GL_LIMITS(glsl_varyings) / 4);
3373 shader_addline(&buffer, "void order_ps_input() {\n");
3374 /* The vertex shader wrote to the builtin varyings. There is no need to figure out position and
3375 * point size, but we depend on the optimizers kindness to find out that the pixel shader doesn't
3376 * read gl_TexCoord and gl_ColorX, otherwise we'll run out of varyings
3378 handle_ps3_input(&buffer, gl_info, ps->input_reg_map, ps->semantics_in, &ps->baseShader.reg_maps, NULL, NULL);
3379 shader_addline(&buffer, "}\n");
3380 } else {
3381 ERR("Unexpected vertex and pixel shader version condition: vs: %d, ps: %d\n", vs_major, ps_major);
3384 ret = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
3385 checkGLcall("glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB)");
3386 GL_EXTCALL(glShaderSourceARB(ret, 1, (const char**)&buffer.buffer, NULL));
3387 checkGLcall("glShaderSourceARB(ret, 1, &buffer.buffer, NULL)");
3388 GL_EXTCALL(glCompileShaderARB(ret));
3389 checkGLcall("glCompileShaderARB(ret)");
3391 shader_buffer_free(&buffer);
3392 return ret;
3395 static void hardcode_local_constants(IWineD3DBaseShaderImpl *shader, const WineD3D_GL_Info *gl_info,
3396 GLhandleARB programId, char prefix)
3398 const local_constant *lconst;
3399 GLint tmp_loc;
3400 const float *value;
3401 char glsl_name[8];
3403 LIST_FOR_EACH_ENTRY(lconst, &shader->baseShader.constantsF, local_constant, entry) {
3404 value = (const float *)lconst->value;
3405 snprintf(glsl_name, sizeof(glsl_name), "%cLC%u", prefix, lconst->idx);
3406 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
3407 GL_EXTCALL(glUniform4fvARB(tmp_loc, 1, value));
3409 checkGLcall("Hardcoding local constants\n");
3412 /** Sets the GLSL program ID for the given pixel and vertex shader combination.
3413 * It sets the programId on the current StateBlock (because it should be called
3414 * inside of the DrawPrimitive() part of the render loop).
3416 * If a program for the given combination does not exist, create one, and store
3417 * the program in the hash table. If it creates a program, it will link the
3418 * given objects, too.
3420 static void set_glsl_shader_program(IWineD3DDevice *iface, BOOL use_ps, BOOL use_vs) {
3421 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
3422 struct shader_glsl_priv *priv = This->shader_priv;
3423 const WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
3424 IWineD3DPixelShader *pshader = This->stateBlock->pixelShader;
3425 IWineD3DVertexShader *vshader = This->stateBlock->vertexShader;
3426 struct glsl_shader_prog_link *entry = NULL;
3427 GLhandleARB programId = 0;
3428 GLhandleARB reorder_shader_id = 0;
3429 unsigned int i;
3430 char glsl_name[8];
3431 GLhandleARB vshader_id, pshader_id;
3432 struct ps_compile_args ps_compile_args;
3433 struct vs_compile_args vs_compile_args;
3435 if(use_vs) {
3436 find_vs_compile_args((IWineD3DVertexShaderImpl*)This->stateBlock->vertexShader, This->stateBlock, &vs_compile_args);
3437 } else {
3438 /* FIXME: Do we really have to spend CPU cycles to generate a few zeroed bytes? */
3439 memset(&vs_compile_args, 0, sizeof(vs_compile_args));
3441 if(use_ps) {
3442 find_ps_compile_args((IWineD3DPixelShaderImpl*)This->stateBlock->pixelShader, This->stateBlock, &ps_compile_args);
3443 } else {
3444 /* FIXME: Do we really have to spend CPU cycles to generate a few zeroed bytes? */
3445 memset(&ps_compile_args, 0, sizeof(ps_compile_args));
3447 entry = get_glsl_program_entry(priv, vshader, pshader, &vs_compile_args, &ps_compile_args);
3448 if (entry) {
3449 priv->glsl_program = entry;
3450 return;
3453 /* If we get to this point, then no matching program exists, so we create one */
3454 programId = GL_EXTCALL(glCreateProgramObjectARB());
3455 TRACE("Created new GLSL shader program %u\n", programId);
3457 /* Create the entry */
3458 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link));
3459 entry->programId = programId;
3460 entry->vshader = vshader;
3461 entry->pshader = pshader;
3462 entry->vs_args = vs_compile_args;
3463 entry->ps_args = ps_compile_args;
3464 entry->constant_version = 0;
3465 /* Add the hash table entry */
3466 add_glsl_program_entry(priv, entry);
3468 /* Set the current program */
3469 priv->glsl_program = entry;
3471 if(use_vs) {
3472 vshader_id = find_gl_vshader((IWineD3DVertexShaderImpl *) vshader, &vs_compile_args);
3473 } else {
3474 vshader_id = 0;
3477 /* Attach GLSL vshader */
3478 if (vshader_id) {
3479 const unsigned int max_attribs = 16; /* TODO: Will this always be the case? It is at the moment... */
3480 char tmp_name[10];
3482 reorder_shader_id = generate_param_reorder_function(vshader, pshader, gl_info);
3483 TRACE("Attaching GLSL shader object %u to program %u\n", reorder_shader_id, programId);
3484 GL_EXTCALL(glAttachObjectARB(programId, reorder_shader_id));
3485 checkGLcall("glAttachObjectARB");
3486 /* Flag the reorder function for deletion, then it will be freed automatically when the program
3487 * is destroyed
3489 GL_EXTCALL(glDeleteObjectARB(reorder_shader_id));
3491 TRACE("Attaching GLSL shader object %u to program %u\n", vshader_id, programId);
3492 GL_EXTCALL(glAttachObjectARB(programId, vshader_id));
3493 checkGLcall("glAttachObjectARB");
3495 /* Bind vertex attributes to a corresponding index number to match
3496 * the same index numbers as ARB_vertex_programs (makes loading
3497 * vertex attributes simpler). With this method, we can use the
3498 * exact same code to load the attributes later for both ARB and
3499 * GLSL shaders.
3501 * We have to do this here because we need to know the Program ID
3502 * in order to make the bindings work, and it has to be done prior
3503 * to linking the GLSL program. */
3504 for (i = 0; i < max_attribs; ++i) {
3505 if (((IWineD3DBaseShaderImpl*)vshader)->baseShader.reg_maps.attributes[i]) {
3506 snprintf(tmp_name, sizeof(tmp_name), "attrib%i", i);
3507 GL_EXTCALL(glBindAttribLocationARB(programId, i, tmp_name));
3510 checkGLcall("glBindAttribLocationARB");
3512 list_add_head(&((IWineD3DBaseShaderImpl *)vshader)->baseShader.linked_programs, &entry->vshader_entry);
3515 if(use_ps) {
3516 pshader_id = find_gl_pshader((IWineD3DPixelShaderImpl *) pshader, &ps_compile_args);
3517 } else {
3518 pshader_id = 0;
3521 /* Attach GLSL pshader */
3522 if (pshader_id) {
3523 TRACE("Attaching GLSL shader object %u to program %u\n", pshader_id, programId);
3524 GL_EXTCALL(glAttachObjectARB(programId, pshader_id));
3525 checkGLcall("glAttachObjectARB");
3527 list_add_head(&((IWineD3DBaseShaderImpl *)pshader)->baseShader.linked_programs, &entry->pshader_entry);
3530 /* Link the program */
3531 TRACE("Linking GLSL shader program %u\n", programId);
3532 GL_EXTCALL(glLinkProgramARB(programId));
3533 print_glsl_info_log(&GLINFO_LOCATION, programId);
3535 entry->vuniformF_locations = HeapAlloc(GetProcessHeap(), 0, sizeof(GLhandleARB) * GL_LIMITS(vshader_constantsF));
3536 for (i = 0; i < GL_LIMITS(vshader_constantsF); ++i) {
3537 snprintf(glsl_name, sizeof(glsl_name), "VC[%i]", i);
3538 entry->vuniformF_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
3540 for (i = 0; i < MAX_CONST_I; ++i) {
3541 snprintf(glsl_name, sizeof(glsl_name), "VI[%i]", i);
3542 entry->vuniformI_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
3544 entry->puniformF_locations = HeapAlloc(GetProcessHeap(), 0, sizeof(GLhandleARB) * GL_LIMITS(pshader_constantsF));
3545 for (i = 0; i < GL_LIMITS(pshader_constantsF); ++i) {
3546 snprintf(glsl_name, sizeof(glsl_name), "PC[%i]", i);
3547 entry->puniformF_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
3549 for (i = 0; i < MAX_CONST_I; ++i) {
3550 snprintf(glsl_name, sizeof(glsl_name), "PI[%i]", i);
3551 entry->puniformI_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
3554 if(pshader) {
3555 for(i = 0; i < ((IWineD3DPixelShaderImpl*)pshader)->numbumpenvmatconsts; i++) {
3556 char name[32];
3557 sprintf(name, "bumpenvmat%d", ((IWineD3DPixelShaderImpl*)pshader)->bumpenvmatconst[i].texunit);
3558 entry->bumpenvmat_location[i] = GL_EXTCALL(glGetUniformLocationARB(programId, name));
3559 sprintf(name, "luminancescale%d", ((IWineD3DPixelShaderImpl*)pshader)->luminanceconst[i].texunit);
3560 entry->luminancescale_location[i] = GL_EXTCALL(glGetUniformLocationARB(programId, name));
3561 sprintf(name, "luminanceoffset%d", ((IWineD3DPixelShaderImpl*)pshader)->luminanceconst[i].texunit);
3562 entry->luminanceoffset_location[i] = GL_EXTCALL(glGetUniformLocationARB(programId, name));
3566 if (use_ps && ps_compile_args.np2_fixup) {
3567 char name[32];
3568 for (i = 0; i < MAX_FRAGMENT_SAMPLERS; ++i) {
3569 if (ps_compile_args.np2_fixup & (1 << i)) {
3570 sprintf(name, "PsamplerNP2Fixup%u", i);
3571 entry->np2Fixup_location[i] = GL_EXTCALL(glGetUniformLocationARB(programId, name));
3572 } else {
3573 entry->np2Fixup_location[i] = -1;
3578 entry->posFixup_location = GL_EXTCALL(glGetUniformLocationARB(programId, "posFixup"));
3579 entry->ycorrection_location = GL_EXTCALL(glGetUniformLocationARB(programId, "ycorrection"));
3580 checkGLcall("Find glsl program uniform locations");
3582 if (pshader
3583 && WINED3DSHADER_VERSION_MAJOR(((IWineD3DPixelShaderImpl *)pshader)->baseShader.reg_maps.shader_version) >= 3
3584 && ((IWineD3DPixelShaderImpl *)pshader)->declared_in_count > GL_LIMITS(glsl_varyings) / 4)
3586 TRACE("Shader %d needs vertex color clamping disabled\n", programId);
3587 entry->vertex_color_clamp = GL_FALSE;
3588 } else {
3589 entry->vertex_color_clamp = GL_FIXED_ONLY_ARB;
3592 /* Set the shader to allow uniform loading on it */
3593 GL_EXTCALL(glUseProgramObjectARB(programId));
3594 checkGLcall("glUseProgramObjectARB(programId)");
3596 /* Load the vertex and pixel samplers now. The function that finds the mappings makes sure
3597 * that it stays the same for each vertexshader-pixelshader pair(=linked glsl program). If
3598 * a pshader with fixed function pipeline is used there are no vertex samplers, and if a
3599 * vertex shader with fixed function pixel processing is used we make sure that the card
3600 * supports enough samplers to allow the max number of vertex samplers with all possible
3601 * fixed function fragment processing setups. So once the program is linked these samplers
3602 * won't change.
3604 if(vshader_id) {
3605 /* Load vertex shader samplers */
3606 shader_glsl_load_vsamplers(gl_info, This->texUnitMap, programId);
3608 if(pshader_id) {
3609 /* Load pixel shader samplers */
3610 shader_glsl_load_psamplers(gl_info, This->texUnitMap, programId);
3613 /* If the local constants do not have to be loaded with the environment constants,
3614 * load them now to have them hardcoded in the GLSL program. This saves some CPU cycles
3615 * later
3617 if(pshader && !((IWineD3DPixelShaderImpl*)pshader)->baseShader.load_local_constsF) {
3618 hardcode_local_constants((IWineD3DBaseShaderImpl *) pshader, gl_info, programId, 'P');
3620 if(vshader && !((IWineD3DVertexShaderImpl*)vshader)->baseShader.load_local_constsF) {
3621 hardcode_local_constants((IWineD3DBaseShaderImpl *) vshader, gl_info, programId, 'V');
3625 static GLhandleARB create_glsl_blt_shader(const WineD3D_GL_Info *gl_info, enum tex_types tex_type)
3627 GLhandleARB program_id;
3628 GLhandleARB vshader_id, pshader_id;
3629 static const char *blt_vshader[] =
3631 "#version 120\n"
3632 "void main(void)\n"
3633 "{\n"
3634 " gl_Position = gl_Vertex;\n"
3635 " gl_FrontColor = vec4(1.0);\n"
3636 " gl_TexCoord[0] = gl_MultiTexCoord0;\n"
3637 "}\n"
3640 static const char *blt_pshaders[tex_type_count] =
3642 /* tex_1d */
3643 NULL,
3644 /* tex_2d */
3645 "#version 120\n"
3646 "uniform sampler2D sampler;\n"
3647 "void main(void)\n"
3648 "{\n"
3649 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
3650 "}\n",
3651 /* tex_3d */
3652 NULL,
3653 /* tex_cube */
3654 "#version 120\n"
3655 "uniform samplerCube sampler;\n"
3656 "void main(void)\n"
3657 "{\n"
3658 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
3659 "}\n",
3660 /* tex_rect */
3661 "#version 120\n"
3662 "#extension GL_ARB_texture_rectangle : enable\n"
3663 "uniform sampler2DRect sampler;\n"
3664 "void main(void)\n"
3665 "{\n"
3666 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
3667 "}\n",
3670 if (!blt_pshaders[tex_type])
3672 FIXME("tex_type %#x not supported\n", tex_type);
3673 tex_type = tex_2d;
3676 vshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
3677 GL_EXTCALL(glShaderSourceARB(vshader_id, 1, blt_vshader, NULL));
3678 GL_EXTCALL(glCompileShaderARB(vshader_id));
3680 pshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
3681 GL_EXTCALL(glShaderSourceARB(pshader_id, 1, &blt_pshaders[tex_type], NULL));
3682 GL_EXTCALL(glCompileShaderARB(pshader_id));
3684 program_id = GL_EXTCALL(glCreateProgramObjectARB());
3685 GL_EXTCALL(glAttachObjectARB(program_id, vshader_id));
3686 GL_EXTCALL(glAttachObjectARB(program_id, pshader_id));
3687 GL_EXTCALL(glLinkProgramARB(program_id));
3689 print_glsl_info_log(&GLINFO_LOCATION, program_id);
3691 /* Once linked we can mark the shaders for deletion. They will be deleted once the program
3692 * is destroyed
3694 GL_EXTCALL(glDeleteObjectARB(vshader_id));
3695 GL_EXTCALL(glDeleteObjectARB(pshader_id));
3696 return program_id;
3699 static void shader_glsl_select(IWineD3DDevice *iface, BOOL usePS, BOOL useVS) {
3700 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
3701 struct shader_glsl_priv *priv = This->shader_priv;
3702 const WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
3703 GLhandleARB program_id = 0;
3704 GLenum old_vertex_color_clamp, current_vertex_color_clamp;
3706 old_vertex_color_clamp = priv->glsl_program ? priv->glsl_program->vertex_color_clamp : GL_FIXED_ONLY_ARB;
3708 if (useVS || usePS) set_glsl_shader_program(iface, usePS, useVS);
3709 else priv->glsl_program = NULL;
3711 current_vertex_color_clamp = priv->glsl_program ? priv->glsl_program->vertex_color_clamp : GL_FIXED_ONLY_ARB;
3713 if (old_vertex_color_clamp != current_vertex_color_clamp) {
3714 if (GL_SUPPORT(ARB_COLOR_BUFFER_FLOAT)) {
3715 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, current_vertex_color_clamp));
3716 checkGLcall("glClampColorARB");
3717 } else {
3718 FIXME("vertex color clamp needs to be changed, but extension not supported.\n");
3722 program_id = priv->glsl_program ? priv->glsl_program->programId : 0;
3723 if (program_id) TRACE("Using GLSL program %u\n", program_id);
3724 GL_EXTCALL(glUseProgramObjectARB(program_id));
3725 checkGLcall("glUseProgramObjectARB");
3728 static void shader_glsl_select_depth_blt(IWineD3DDevice *iface, enum tex_types tex_type) {
3729 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
3730 const WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
3731 struct shader_glsl_priv *priv = This->shader_priv;
3732 GLhandleARB *blt_program = &priv->depth_blt_program[tex_type];
3734 if (!*blt_program) {
3735 GLint loc;
3736 *blt_program = create_glsl_blt_shader(gl_info, tex_type);
3737 loc = GL_EXTCALL(glGetUniformLocationARB(*blt_program, "sampler"));
3738 GL_EXTCALL(glUseProgramObjectARB(*blt_program));
3739 GL_EXTCALL(glUniform1iARB(loc, 0));
3740 } else {
3741 GL_EXTCALL(glUseProgramObjectARB(*blt_program));
3745 static void shader_glsl_deselect_depth_blt(IWineD3DDevice *iface) {
3746 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
3747 const WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
3748 struct shader_glsl_priv *priv = This->shader_priv;
3749 GLhandleARB program_id;
3751 program_id = priv->glsl_program ? priv->glsl_program->programId : 0;
3752 if (program_id) TRACE("Using GLSL program %u\n", program_id);
3754 GL_EXTCALL(glUseProgramObjectARB(program_id));
3755 checkGLcall("glUseProgramObjectARB");
3758 static void shader_glsl_destroy(IWineD3DBaseShader *iface) {
3759 const struct list *linked_programs;
3760 IWineD3DBaseShaderImpl *This = (IWineD3DBaseShaderImpl *) iface;
3761 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *)This->baseShader.device;
3762 struct shader_glsl_priv *priv = device->shader_priv;
3763 const WineD3D_GL_Info *gl_info = &device->adapter->gl_info;
3764 IWineD3DPixelShaderImpl *ps = NULL;
3765 IWineD3DVertexShaderImpl *vs = NULL;
3767 /* Note: Do not use QueryInterface here to find out which shader type this is because this code
3768 * can be called from IWineD3DBaseShader::Release
3770 char pshader = shader_is_pshader_version(This->baseShader.reg_maps.shader_version);
3772 if(pshader) {
3773 ps = (IWineD3DPixelShaderImpl *) This;
3774 if(ps->num_gl_shaders == 0) return;
3775 if (priv->glsl_program && (IWineD3DBaseShader *)priv->glsl_program->pshader == iface)
3776 shader_glsl_select(This->baseShader.device, FALSE, FALSE);
3777 } else {
3778 vs = (IWineD3DVertexShaderImpl *) This;
3779 if(vs->num_gl_shaders == 0) return;
3780 if (priv->glsl_program && (IWineD3DBaseShader *)priv->glsl_program->vshader == iface)
3781 shader_glsl_select(This->baseShader.device, FALSE, FALSE);
3784 linked_programs = &This->baseShader.linked_programs;
3786 TRACE("Deleting linked programs\n");
3787 if (linked_programs->next) {
3788 struct glsl_shader_prog_link *entry, *entry2;
3790 if(pshader) {
3791 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs, struct glsl_shader_prog_link, pshader_entry) {
3792 delete_glsl_program_entry(priv, gl_info, entry);
3794 } else {
3795 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs, struct glsl_shader_prog_link, vshader_entry) {
3796 delete_glsl_program_entry(priv, gl_info, entry);
3801 if(pshader) {
3802 UINT i;
3804 ENTER_GL();
3805 for(i = 0; i < ps->num_gl_shaders; i++) {
3806 TRACE("deleting pshader %u\n", ps->gl_shaders[i].prgId);
3807 GL_EXTCALL(glDeleteObjectARB(ps->gl_shaders[i].prgId));
3808 checkGLcall("glDeleteObjectARB");
3810 LEAVE_GL();
3811 HeapFree(GetProcessHeap(), 0, ps->gl_shaders);
3812 ps->gl_shaders = NULL;
3813 ps->num_gl_shaders = 0;
3814 ps->shader_array_size = 0;
3815 } else {
3816 UINT i;
3818 ENTER_GL();
3819 for(i = 0; i < vs->num_gl_shaders; i++) {
3820 TRACE("deleting vshader %u\n", vs->gl_shaders[i].prgId);
3821 GL_EXTCALL(glDeleteObjectARB(vs->gl_shaders[i].prgId));
3822 checkGLcall("glDeleteObjectARB");
3824 LEAVE_GL();
3825 HeapFree(GetProcessHeap(), 0, vs->gl_shaders);
3826 vs->gl_shaders = NULL;
3827 vs->num_gl_shaders = 0;
3828 vs->shader_array_size = 0;
3832 static unsigned int glsl_program_key_hash(const void *key)
3834 const glsl_program_key_t *k = key;
3836 unsigned int hash = ((DWORD_PTR) k->vshader) | ((DWORD_PTR) k->pshader) << 16;
3837 hash += ~(hash << 15);
3838 hash ^= (hash >> 10);
3839 hash += (hash << 3);
3840 hash ^= (hash >> 6);
3841 hash += ~(hash << 11);
3842 hash ^= (hash >> 16);
3844 return hash;
3847 static BOOL glsl_program_key_compare(const void *keya, const void *keyb)
3849 const glsl_program_key_t *ka = keya;
3850 const glsl_program_key_t *kb = keyb;
3852 return ka->vshader == kb->vshader && ka->pshader == kb->pshader &&
3853 (memcmp(&ka->ps_args, &kb->ps_args, sizeof(kb->ps_args)) == 0) &&
3854 (memcmp(&ka->vs_args, &kb->vs_args, sizeof(kb->vs_args)) == 0);
3857 static BOOL constant_heap_init(struct constant_heap *heap, unsigned int constant_count)
3859 SIZE_T size = (constant_count + 1) * sizeof(*heap->entries) + constant_count * sizeof(*heap->positions);
3860 void *mem = HeapAlloc(GetProcessHeap(), 0, size);
3862 if (!mem)
3864 ERR("Failed to allocate memory\n");
3865 return FALSE;
3868 heap->entries = mem;
3869 heap->entries[1].version = 0;
3870 heap->positions = (unsigned int *)(heap->entries + constant_count + 1);
3871 heap->size = 1;
3873 return TRUE;
3876 static void constant_heap_free(struct constant_heap *heap)
3878 HeapFree(GetProcessHeap(), 0, heap->entries);
3881 static HRESULT shader_glsl_alloc(IWineD3DDevice *iface) {
3882 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
3883 const WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
3884 struct shader_glsl_priv *priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct shader_glsl_priv));
3885 SIZE_T stack_size = wined3d_log2i(max(GL_LIMITS(vshader_constantsF), GL_LIMITS(pshader_constantsF))) + 1;
3887 priv->stack = HeapAlloc(GetProcessHeap(), 0, stack_size * sizeof(*priv->stack));
3888 if (!priv->stack)
3890 ERR("Failed to allocate memory.\n");
3891 HeapFree(GetProcessHeap(), 0, priv);
3892 return E_OUTOFMEMORY;
3895 if (!constant_heap_init(&priv->vconst_heap, GL_LIMITS(vshader_constantsF)))
3897 ERR("Failed to initialize vertex shader constant heap\n");
3898 HeapFree(GetProcessHeap(), 0, priv->stack);
3899 HeapFree(GetProcessHeap(), 0, priv);
3900 return E_OUTOFMEMORY;
3903 if (!constant_heap_init(&priv->pconst_heap, GL_LIMITS(pshader_constantsF)))
3905 ERR("Failed to initialize pixel shader constant heap\n");
3906 constant_heap_free(&priv->vconst_heap);
3907 HeapFree(GetProcessHeap(), 0, priv->stack);
3908 HeapFree(GetProcessHeap(), 0, priv);
3909 return E_OUTOFMEMORY;
3912 priv->glsl_program_lookup = hash_table_create(glsl_program_key_hash, glsl_program_key_compare);
3913 priv->next_constant_version = 1;
3915 This->shader_priv = priv;
3916 return WINED3D_OK;
3919 static void shader_glsl_free(IWineD3DDevice *iface) {
3920 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
3921 const WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
3922 struct shader_glsl_priv *priv = This->shader_priv;
3923 int i;
3925 for (i = 0; i < tex_type_count; ++i)
3927 if (priv->depth_blt_program[i])
3929 GL_EXTCALL(glDeleteObjectARB(priv->depth_blt_program[i]));
3933 hash_table_destroy(priv->glsl_program_lookup, NULL, NULL);
3934 constant_heap_free(&priv->pconst_heap);
3935 constant_heap_free(&priv->vconst_heap);
3937 HeapFree(GetProcessHeap(), 0, This->shader_priv);
3938 This->shader_priv = NULL;
3941 static BOOL shader_glsl_dirty_const(IWineD3DDevice *iface) {
3942 /* TODO: GL_EXT_bindable_uniform can be used to share constants across shaders */
3943 return FALSE;
3946 static GLuint shader_glsl_generate_pshader(IWineD3DPixelShader *iface, SHADER_BUFFER *buffer, const struct ps_compile_args *args) {
3947 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)iface;
3948 const struct shader_reg_maps *reg_maps = &This->baseShader.reg_maps;
3949 CONST DWORD *function = This->baseShader.function;
3950 const char *fragcolor;
3951 const WineD3D_GL_Info *gl_info = &((IWineD3DDeviceImpl *)This->baseShader.device)->adapter->gl_info;
3953 /* Create the hw GLSL shader object and assign it as the shader->prgId */
3954 GLhandleARB shader_obj = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
3956 shader_addline(buffer, "#version 120\n");
3958 if (GL_SUPPORT(ARB_DRAW_BUFFERS)) {
3959 shader_addline(buffer, "#extension GL_ARB_draw_buffers : enable\n");
3961 if (GL_SUPPORT(ARB_TEXTURE_RECTANGLE)) {
3962 /* The spec says that it doesn't have to be explicitly enabled, but the nvidia
3963 * drivers write a warning if we don't do so
3965 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
3968 /* Base Declarations */
3969 shader_generate_glsl_declarations( (IWineD3DBaseShader*) This, reg_maps, buffer, &GLINFO_LOCATION, args);
3971 /* Pack 3.0 inputs */
3972 if (reg_maps->shader_version >= WINED3DPS_VERSION(3,0) && args->vp_mode != vertexshader) {
3973 pshader_glsl_input_pack(iface, buffer, This->semantics_in, reg_maps, args->vp_mode);
3976 /* Base Shader Body */
3977 shader_generate_main( (IWineD3DBaseShader*) This, buffer, reg_maps, function);
3979 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly */
3980 if (reg_maps->shader_version < WINED3DPS_VERSION(2,0))
3982 /* Some older cards like GeforceFX ones don't support multiple buffers, so also not gl_FragData */
3983 if(GL_SUPPORT(ARB_DRAW_BUFFERS))
3984 shader_addline(buffer, "gl_FragData[0] = R0;\n");
3985 else
3986 shader_addline(buffer, "gl_FragColor = R0;\n");
3989 if(GL_SUPPORT(ARB_DRAW_BUFFERS)) {
3990 fragcolor = "gl_FragData[0]";
3991 } else {
3992 fragcolor = "gl_FragColor";
3994 if(args->srgb_correction) {
3995 shader_addline(buffer, "tmp0.xyz = pow(%s.xyz, vec3(%f, %f, %f)) * vec3(%f, %f, %f) - vec3(%f, %f, %f);\n",
3996 fragcolor, srgb_pow, srgb_pow, srgb_pow, srgb_mul_high, srgb_mul_high, srgb_mul_high,
3997 srgb_sub_high, srgb_sub_high, srgb_sub_high);
3998 shader_addline(buffer, "tmp1.xyz = %s.xyz * srgb_mul_low.xyz;\n", fragcolor);
3999 shader_addline(buffer, "%s.x = %s.x < srgb_comparison.x ? tmp1.x : tmp0.x;\n", fragcolor, fragcolor);
4000 shader_addline(buffer, "%s.y = %s.y < srgb_comparison.y ? tmp1.y : tmp0.y;\n", fragcolor, fragcolor);
4001 shader_addline(buffer, "%s.z = %s.z < srgb_comparison.z ? tmp1.z : tmp0.z;\n", fragcolor, fragcolor);
4002 shader_addline(buffer, "%s = clamp(%s, 0.0, 1.0);\n", fragcolor, fragcolor);
4004 /* Pixel shader < 3.0 do not replace the fog stage.
4005 * This implements linear fog computation and blending.
4006 * TODO: non linear fog
4007 * NOTE: gl_Fog.start and gl_Fog.end don't hold fog start s and end e but
4008 * -1/(e-s) and e/(e-s) respectively.
4010 if(reg_maps->shader_version < WINED3DPS_VERSION(3,0)) {
4011 switch(args->fog) {
4012 case FOG_OFF: break;
4013 case FOG_LINEAR:
4014 shader_addline(buffer, "float fogstart = -1.0 / (gl_Fog.end - gl_Fog.start);\n");
4015 shader_addline(buffer, "float fogend = gl_Fog.end * -fogstart;\n");
4016 shader_addline(buffer, "float Fog = clamp(gl_FogFragCoord * fogstart + fogend, 0.0, 1.0);\n");
4017 shader_addline(buffer, "%s.xyz = mix(gl_Fog.color.xyz, %s.xyz, Fog);\n", fragcolor, fragcolor);
4018 break;
4019 case FOG_EXP:
4020 /* Fog = e^(-gl_Fog.density * gl_FogFragCoord) */
4021 shader_addline(buffer, "float Fog = exp(-gl_Fog.density * gl_FogFragCoord);\n");
4022 shader_addline(buffer, "Fog = clamp(Fog, 0.0, 1.0);\n");
4023 shader_addline(buffer, "%s.xyz = mix(gl_Fog.color.xyz, %s.xyz, Fog);\n", fragcolor, fragcolor);
4024 break;
4025 case FOG_EXP2:
4026 /* Fog = e^(-(gl_Fog.density * gl_FogFragCoord)^2) */
4027 shader_addline(buffer, "float Fog = exp(-gl_Fog.density * gl_Fog.density * gl_FogFragCoord * gl_FogFragCoord);\n");
4028 shader_addline(buffer, "Fog = clamp(Fog, 0.0, 1.0);\n");
4029 shader_addline(buffer, "%s.xyz = mix(gl_Fog.color.xyz, %s.xyz, Fog);\n", fragcolor, fragcolor);
4030 break;
4034 shader_addline(buffer, "}\n");
4036 TRACE("Compiling shader object %u\n", shader_obj);
4037 GL_EXTCALL(glShaderSourceARB(shader_obj, 1, (const char**)&buffer->buffer, NULL));
4038 GL_EXTCALL(glCompileShaderARB(shader_obj));
4039 print_glsl_info_log(&GLINFO_LOCATION, shader_obj);
4041 /* Store the shader object */
4042 return shader_obj;
4045 static GLuint shader_glsl_generate_vshader(IWineD3DVertexShader *iface, SHADER_BUFFER *buffer, const struct vs_compile_args *args) {
4046 IWineD3DVertexShaderImpl *This = (IWineD3DVertexShaderImpl *)iface;
4047 const struct shader_reg_maps *reg_maps = &This->baseShader.reg_maps;
4048 CONST DWORD *function = This->baseShader.function;
4049 const WineD3D_GL_Info *gl_info = &((IWineD3DDeviceImpl *)This->baseShader.device)->adapter->gl_info;
4051 /* Create the hw GLSL shader program and assign it as the shader->prgId */
4052 GLhandleARB shader_obj = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
4054 shader_addline(buffer, "#version 120\n");
4056 /* Base Declarations */
4057 shader_generate_glsl_declarations( (IWineD3DBaseShader*) This, reg_maps, buffer, &GLINFO_LOCATION, NULL);
4059 /* Base Shader Body */
4060 shader_generate_main( (IWineD3DBaseShader*) This, buffer, reg_maps, function);
4062 /* Unpack 3.0 outputs */
4063 if (reg_maps->shader_version >= WINED3DVS_VERSION(3,0)) shader_addline(buffer, "order_ps_input(OUT);\n");
4064 else shader_addline(buffer, "order_ps_input();\n");
4066 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
4067 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
4068 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
4069 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0)
4071 if(args->fog_src == VS_FOG_Z) {
4072 shader_addline(buffer, "gl_FogFragCoord = gl_Position.z;\n");
4073 } else if (!reg_maps->fog) {
4074 shader_addline(buffer, "gl_FogFragCoord = 0.0;\n");
4077 /* Write the final position.
4079 * OpenGL coordinates specify the center of the pixel while d3d coords specify
4080 * the corner. The offsets are stored in z and w in posFixup. posFixup.y contains
4081 * 1.0 or -1.0 to turn the rendering upside down for offscreen rendering. PosFixup.x
4082 * contains 1.0 to allow a mad.
4084 shader_addline(buffer, "gl_Position.y = gl_Position.y * posFixup.y;\n");
4085 shader_addline(buffer, "gl_Position.xy += posFixup.zw * gl_Position.ww;\n");
4087 /* Z coord [0;1]->[-1;1] mapping, see comment in transform_projection in state.c
4089 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However, shaders are run
4090 * before the homogeneous divide, so we have to take the w into account: z = ((z / w) * 2 - 1) * w,
4091 * which is the same as z = z * 2 - w.
4093 shader_addline(buffer, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
4095 shader_addline(buffer, "}\n");
4097 TRACE("Compiling shader object %u\n", shader_obj);
4098 GL_EXTCALL(glShaderSourceARB(shader_obj, 1, (const char**)&buffer->buffer, NULL));
4099 GL_EXTCALL(glCompileShaderARB(shader_obj));
4100 print_glsl_info_log(&GLINFO_LOCATION, shader_obj);
4102 return shader_obj;
4105 static void shader_glsl_get_caps(WINED3DDEVTYPE devtype, const WineD3D_GL_Info *gl_info, struct shader_caps *pCaps)
4107 /* Nvidia Geforce6/7 or Ati R4xx/R5xx cards with GLSL support, support VS 3.0 but older Nvidia/Ati
4108 * models with GLSL support only support 2.0. In case of nvidia we can detect VS 2.0 support using
4109 * vs_nv_version which is based on NV_vertex_program.
4110 * For Ati cards there's no way using glsl (it abstracts the lowlevel info away) and also not
4111 * using ARB_vertex_program. It is safe to assume that when a card supports pixel shader 2.0 it
4112 * supports vertex shader 2.0 too and the way around. We can detect ps2.0 using the maximum number
4113 * of native instructions, so use that here. For more info see the pixel shader versioning code below.
4115 if((GLINFO_LOCATION.vs_nv_version == VS_VERSION_20) || (GLINFO_LOCATION.ps_arb_max_instructions <= 512))
4116 pCaps->VertexShaderVersion = WINED3DVS_VERSION(2,0);
4117 else
4118 pCaps->VertexShaderVersion = WINED3DVS_VERSION(3,0);
4119 TRACE_(d3d_caps)("Hardware vertex shader version %d.%d enabled (GLSL)\n", (pCaps->VertexShaderVersion >> 8) & 0xff, pCaps->VertexShaderVersion & 0xff);
4120 /* Subtract the other potential uniforms from the max available (bools, ints, and 1 row of projection matrix) */
4121 pCaps->MaxVertexShaderConst = GL_LIMITS(vshader_constantsF) - (MAX_CONST_B / 4) - MAX_CONST_I - 1;
4123 /* Older DX9-class videocards (GeforceFX / Radeon >9500/X*00) only support pixel shader 2.0/2.0a/2.0b.
4124 * In OpenGL the extensions related to GLSL abstract lowlevel GL info away which is needed
4125 * to distinguish between 2.0 and 3.0 (and 2.0a/2.0b). In case of Nvidia we use their fragment
4126 * program extensions. On other hardware including ATI GL_ARB_fragment_program offers the info
4127 * in max native instructions. Intel and others also offer the info in this extension but they
4128 * don't support GLSL (at least on Windows).
4130 * PS2.0 requires at least 96 instructions, 2.0a/2.0b go up to 512. Assume that if the number
4131 * of instructions is 512 or less we have to do with ps2.0 hardware.
4132 * NOTE: ps3.0 hardware requires 512 or more instructions but ati and nvidia offer 'enough' (1024 vs 4096) on their most basic ps3.0 hardware.
4134 if((GLINFO_LOCATION.ps_nv_version == PS_VERSION_20) || (GLINFO_LOCATION.ps_arb_max_instructions <= 512))
4135 pCaps->PixelShaderVersion = WINED3DPS_VERSION(2,0);
4136 else
4137 pCaps->PixelShaderVersion = WINED3DPS_VERSION(3,0);
4139 /* Subtract the other potential uniforms from the max available (bools & ints), and 2 states for fog.
4140 * In theory the texbem instruction may need one more shader constant too. But lets assume
4141 * that a sm <= 1.3 shader does not need all the uniforms provided by a glsl-capable card,
4142 * and lets not take away a uniform needlessly from all other shaders.
4144 pCaps->MaxPixelShaderConst = GL_LIMITS(pshader_constantsF) - (MAX_CONST_B / 4) - MAX_CONST_I - 2;
4146 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
4147 * Direct3D minimum requirement.
4149 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
4150 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
4152 * The problem is that the refrast clamps temporary results in the shader to
4153 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
4154 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
4155 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
4156 * offer a way to query this.
4158 pCaps->PixelShader1xMaxValue = 8.0;
4159 TRACE_(d3d_caps)("Hardware pixel shader version %d.%d enabled (GLSL)\n", (pCaps->PixelShaderVersion >> 8) & 0xff, pCaps->PixelShaderVersion & 0xff);
4162 static BOOL shader_glsl_color_fixup_supported(struct color_fixup_desc fixup)
4164 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
4166 TRACE("Checking support for fixup:\n");
4167 dump_color_fixup_desc(fixup);
4170 /* We support everything except YUV conversions. */
4171 if (!is_yuv_fixup(fixup))
4173 TRACE("[OK]\n");
4174 return TRUE;
4177 TRACE("[FAILED]\n");
4178 return FALSE;
4181 static const SHADER_HANDLER shader_glsl_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
4183 /* WINED3DSIH_ABS */ shader_glsl_map2gl,
4184 /* WINED3DSIH_ADD */ shader_glsl_arith,
4185 /* WINED3DSIH_BEM */ pshader_glsl_bem,
4186 /* WINED3DSIH_BREAK */ shader_glsl_break,
4187 /* WINED3DSIH_BREAKC */ shader_glsl_breakc,
4188 /* WINED3DSIH_BREAKP */ NULL,
4189 /* WINED3DSIH_CALL */ shader_glsl_call,
4190 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz,
4191 /* WINED3DSIH_CMP */ shader_glsl_cmp,
4192 /* WINED3DSIH_CND */ shader_glsl_cnd,
4193 /* WINED3DSIH_CRS */ shader_glsl_cross,
4194 /* WINED3DSIH_DCL */ NULL,
4195 /* WINED3DSIH_DEF */ NULL,
4196 /* WINED3DSIH_DEFB */ NULL,
4197 /* WINED3DSIH_DEFI */ NULL,
4198 /* WINED3DSIH_DP2ADD */ pshader_glsl_dp2add,
4199 /* WINED3DSIH_DP3 */ shader_glsl_dot,
4200 /* WINED3DSIH_DP4 */ shader_glsl_dot,
4201 /* WINED3DSIH_DST */ shader_glsl_dst,
4202 /* WINED3DSIH_DSX */ shader_glsl_map2gl,
4203 /* WINED3DSIH_DSY */ shader_glsl_map2gl,
4204 /* WINED3DSIH_ELSE */ shader_glsl_else,
4205 /* WINED3DSIH_ENDIF */ shader_glsl_end,
4206 /* WINED3DSIH_ENDLOOP */ shader_glsl_end,
4207 /* WINED3DSIH_ENDREP */ shader_glsl_end,
4208 /* WINED3DSIH_EXP */ shader_glsl_map2gl,
4209 /* WINED3DSIH_EXPP */ shader_glsl_expp,
4210 /* WINED3DSIH_FRC */ shader_glsl_map2gl,
4211 /* WINED3DSIH_IF */ shader_glsl_if,
4212 /* WINED3DSIH_IFC */ shader_glsl_ifc,
4213 /* WINED3DSIH_LABEL */ shader_glsl_label,
4214 /* WINED3DSIH_LIT */ shader_glsl_lit,
4215 /* WINED3DSIH_LOG */ shader_glsl_log,
4216 /* WINED3DSIH_LOGP */ shader_glsl_log,
4217 /* WINED3DSIH_LOOP */ shader_glsl_loop,
4218 /* WINED3DSIH_LRP */ shader_glsl_lrp,
4219 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn,
4220 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn,
4221 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn,
4222 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn,
4223 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn,
4224 /* WINED3DSIH_MAD */ shader_glsl_mad,
4225 /* WINED3DSIH_MAX */ shader_glsl_map2gl,
4226 /* WINED3DSIH_MIN */ shader_glsl_map2gl,
4227 /* WINED3DSIH_MOV */ shader_glsl_mov,
4228 /* WINED3DSIH_MOVA */ shader_glsl_mov,
4229 /* WINED3DSIH_MUL */ shader_glsl_arith,
4230 /* WINED3DSIH_NOP */ NULL,
4231 /* WINED3DSIH_NRM */ shader_glsl_map2gl,
4232 /* WINED3DSIH_PHASE */ NULL,
4233 /* WINED3DSIH_POW */ shader_glsl_pow,
4234 /* WINED3DSIH_RCP */ shader_glsl_rcp,
4235 /* WINED3DSIH_REP */ shader_glsl_rep,
4236 /* WINED3DSIH_RET */ NULL,
4237 /* WINED3DSIH_RSQ */ shader_glsl_rsq,
4238 /* WINED3DSIH_SETP */ NULL,
4239 /* WINED3DSIH_SGE */ shader_glsl_compare,
4240 /* WINED3DSIH_SGN */ shader_glsl_map2gl,
4241 /* WINED3DSIH_SINCOS */ shader_glsl_sincos,
4242 /* WINED3DSIH_SLT */ shader_glsl_compare,
4243 /* WINED3DSIH_SUB */ shader_glsl_arith,
4244 /* WINED3DSIH_TEX */ pshader_glsl_tex,
4245 /* WINED3DSIH_TEXBEM */ pshader_glsl_texbem,
4246 /* WINED3DSIH_TEXBEML */ pshader_glsl_texbem,
4247 /* WINED3DSIH_TEXCOORD */ pshader_glsl_texcoord,
4248 /* WINED3DSIH_TEXDEPTH */ pshader_glsl_texdepth,
4249 /* WINED3DSIH_TEXDP3 */ pshader_glsl_texdp3,
4250 /* WINED3DSIH_TEXDP3TEX */ pshader_glsl_texdp3tex,
4251 /* WINED3DSIH_TEXKILL */ pshader_glsl_texkill,
4252 /* WINED3DSIH_TEXLDD */ NULL,
4253 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl,
4254 /* WINED3DSIH_TEXM3x2DEPTH */ pshader_glsl_texm3x2depth,
4255 /* WINED3DSIH_TEXM3x2PAD */ pshader_glsl_texm3x2pad,
4256 /* WINED3DSIH_TEXM3x2TEX */ pshader_glsl_texm3x2tex,
4257 /* WINED3DSIH_TEXM3x3 */ pshader_glsl_texm3x3,
4258 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
4259 /* WINED3DSIH_TEXM3x3PAD */ pshader_glsl_texm3x3pad,
4260 /* WINED3DSIH_TEXM3x3SPEC */ pshader_glsl_texm3x3spec,
4261 /* WINED3DSIH_TEXM3x3TEX */ pshader_glsl_texm3x3tex,
4262 /* WINED3DSIH_TEXM3x3VSPEC */ pshader_glsl_texm3x3vspec,
4263 /* WINED3DSIH_TEXREG2AR */ pshader_glsl_texreg2ar,
4264 /* WINED3DSIH_TEXREG2GB */ pshader_glsl_texreg2gb,
4265 /* WINED3DSIH_TEXREG2RGB */ pshader_glsl_texreg2rgb,
4268 const shader_backend_t glsl_shader_backend = {
4269 shader_glsl_instruction_handler_table,
4270 shader_glsl_select,
4271 shader_glsl_select_depth_blt,
4272 shader_glsl_deselect_depth_blt,
4273 shader_glsl_update_float_vertex_constants,
4274 shader_glsl_update_float_pixel_constants,
4275 shader_glsl_load_constants,
4276 shader_glsl_load_np2fixup_constants,
4277 shader_glsl_destroy,
4278 shader_glsl_alloc,
4279 shader_glsl_free,
4280 shader_glsl_dirty_const,
4281 shader_glsl_generate_pshader,
4282 shader_glsl_generate_vshader,
4283 shader_glsl_get_caps,
4284 shader_glsl_color_fixup_supported,