wined3d: Get rid of a redundant strcat in shader_glsl_get_register_name().
[wine/hacks.git] / dlls / wined3d / glsl_shader.c
blob19d8bf135d418a7469549ddb17190c6463f9af1f
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
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 * D3D shader asm has swizzles on source parameters, and write masks for
25 * destination parameters. GLSL uses swizzles for both. The result of this is
26 * that for example "mov dst.xw, src.zyxw" becomes "dst.xw = src.zw" in GLSL.
27 * Ie, to generate a proper GLSL source swizzle, we need to take the D3D write
28 * mask for the destination parameter into account.
31 #include "config.h"
32 #include <limits.h>
33 #include <stdio.h>
34 #include "wined3d_private.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
37 WINE_DECLARE_DEBUG_CHANNEL(d3d_constants);
38 WINE_DECLARE_DEBUG_CHANNEL(d3d_caps);
39 WINE_DECLARE_DEBUG_CHANNEL(d3d);
41 #define GLINFO_LOCATION (*gl_info)
43 #define WINED3D_GLSL_SAMPLE_PROJECTED 0x1
44 #define WINED3D_GLSL_SAMPLE_RECT 0x2
45 #define WINED3D_GLSL_SAMPLE_LOD 0x4
47 typedef struct {
48 char reg_name[150];
49 char mask_str[6];
50 } glsl_dst_param_t;
52 typedef struct {
53 char reg_name[150];
54 char param_str[100];
55 } glsl_src_param_t;
57 typedef struct {
58 const char *name;
59 DWORD coord_mask;
60 } glsl_sample_function_t;
62 enum heap_node_op
64 HEAP_NODE_TRAVERSE_LEFT,
65 HEAP_NODE_TRAVERSE_RIGHT,
66 HEAP_NODE_POP,
69 struct constant_entry
71 unsigned int idx;
72 unsigned int version;
75 struct constant_heap
77 struct constant_entry *entries;
78 unsigned int *positions;
79 unsigned int size;
82 /* GLSL shader private data */
83 struct shader_glsl_priv {
84 struct hash_table_t *glsl_program_lookup;
85 struct glsl_shader_prog_link *glsl_program;
86 struct constant_heap vconst_heap;
87 struct constant_heap pconst_heap;
88 unsigned char *stack;
89 GLhandleARB depth_blt_program[tex_type_count];
90 UINT next_constant_version;
93 /* Struct to maintain data about a linked GLSL program */
94 struct glsl_shader_prog_link {
95 struct list vshader_entry;
96 struct list pshader_entry;
97 GLhandleARB programId;
98 GLint *vuniformF_locations;
99 GLint *puniformF_locations;
100 GLint vuniformI_locations[MAX_CONST_I];
101 GLint puniformI_locations[MAX_CONST_I];
102 GLint posFixup_location;
103 GLint rectFixup_location[MAX_FRAGMENT_SAMPLERS];
104 GLint bumpenvmat_location[MAX_TEXTURES];
105 GLint luminancescale_location[MAX_TEXTURES];
106 GLint luminanceoffset_location[MAX_TEXTURES];
107 GLint ycorrection_location;
108 GLenum vertex_color_clamp;
109 IWineD3DVertexShader *vshader;
110 IWineD3DPixelShader *pshader;
111 struct vs_compile_args vs_args;
112 struct ps_compile_args ps_args;
113 UINT constant_version;
116 typedef struct {
117 IWineD3DVertexShader *vshader;
118 IWineD3DPixelShader *pshader;
119 struct ps_compile_args ps_args;
120 struct vs_compile_args vs_args;
121 } glsl_program_key_t;
124 /** Prints the GLSL info log which will contain error messages if they exist */
125 static void print_glsl_info_log(const WineD3D_GL_Info *gl_info, GLhandleARB obj)
127 int infologLength = 0;
128 char *infoLog;
129 unsigned int i;
130 BOOL is_spam;
132 static const char * const spam[] =
134 "Vertex shader was successfully compiled to run on hardware.\n", /* fglrx */
135 "Fragment shader was successfully compiled to run on hardware.\n", /* fglrx */
136 "Fragment shader(s) linked, vertex shader(s) linked. \n ", /* fglrx, with \n */
137 "Fragment shader(s) linked, vertex shader(s) linked.", /* fglrx, no \n */
138 "Vertex shader(s) linked, no fragment shader(s) defined. \n ", /* fglrx, with \n */
139 "Vertex shader(s) linked, no fragment shader(s) defined.", /* fglrx, no \n */
140 "Fragment shader was successfully compiled to run on hardware.\n"
141 "WARNING: 0:2: extension 'GL_ARB_draw_buffers' is not supported",
142 "Fragment shader(s) linked, no vertex shader(s) defined.", /* fglrx, no \n */
143 "Fragment shader(s) linked, no vertex shader(s) defined. \n ", /* fglrx, with \n */
144 "WARNING: 0:2: extension 'GL_ARB_draw_buffers' is not supported\n" /* MacOS ati */
147 if (!TRACE_ON(d3d_shader) && !FIXME_ON(d3d_shader)) return;
149 GL_EXTCALL(glGetObjectParameterivARB(obj,
150 GL_OBJECT_INFO_LOG_LENGTH_ARB,
151 &infologLength));
153 /* A size of 1 is just a null-terminated string, so the log should be bigger than
154 * that if there are errors. */
155 if (infologLength > 1)
157 /* Fglrx doesn't terminate the string properly, but it tells us the proper length.
158 * So use HEAP_ZERO_MEMORY to avoid uninitialized bytes
160 infoLog = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, infologLength);
161 GL_EXTCALL(glGetInfoLogARB(obj, infologLength, NULL, infoLog));
162 is_spam = FALSE;
164 for(i = 0; i < sizeof(spam) / sizeof(spam[0]); i++) {
165 if(strcmp(infoLog, spam[i]) == 0) {
166 is_spam = TRUE;
167 break;
170 if(is_spam) {
171 TRACE("Spam received from GLSL shader #%u: %s\n", obj, debugstr_a(infoLog));
172 } else {
173 FIXME("Error received from GLSL shader #%u: %s\n", obj, debugstr_a(infoLog));
175 HeapFree(GetProcessHeap(), 0, infoLog);
180 * Loads (pixel shader) samplers
182 static void shader_glsl_load_psamplers(const WineD3D_GL_Info *gl_info, DWORD *tex_unit_map, GLhandleARB programId)
184 GLint name_loc;
185 int i;
186 char sampler_name[20];
188 for (i = 0; i < MAX_FRAGMENT_SAMPLERS; ++i) {
189 snprintf(sampler_name, sizeof(sampler_name), "Psampler%d", i);
190 name_loc = GL_EXTCALL(glGetUniformLocationARB(programId, sampler_name));
191 if (name_loc != -1) {
192 DWORD mapped_unit = tex_unit_map[i];
193 if (mapped_unit != WINED3D_UNMAPPED_STAGE && mapped_unit < GL_LIMITS(fragment_samplers))
195 TRACE("Loading %s for texture %d\n", sampler_name, mapped_unit);
196 GL_EXTCALL(glUniform1iARB(name_loc, mapped_unit));
197 checkGLcall("glUniform1iARB");
198 } else {
199 ERR("Trying to load sampler %s on unsupported unit %d\n", sampler_name, mapped_unit);
205 static void shader_glsl_load_vsamplers(const WineD3D_GL_Info *gl_info, DWORD *tex_unit_map, GLhandleARB programId)
207 GLint name_loc;
208 char sampler_name[20];
209 int i;
211 for (i = 0; i < MAX_VERTEX_SAMPLERS; ++i) {
212 snprintf(sampler_name, sizeof(sampler_name), "Vsampler%d", i);
213 name_loc = GL_EXTCALL(glGetUniformLocationARB(programId, sampler_name));
214 if (name_loc != -1) {
215 DWORD mapped_unit = tex_unit_map[MAX_FRAGMENT_SAMPLERS + i];
216 if (mapped_unit != WINED3D_UNMAPPED_STAGE && mapped_unit < GL_LIMITS(combined_samplers))
218 TRACE("Loading %s for texture %d\n", sampler_name, mapped_unit);
219 GL_EXTCALL(glUniform1iARB(name_loc, mapped_unit));
220 checkGLcall("glUniform1iARB");
221 } else {
222 ERR("Trying to load sampler %s on unsupported unit %d\n", sampler_name, mapped_unit);
228 static inline void walk_constant_heap(const WineD3D_GL_Info *gl_info, const float *constants,
229 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
231 int stack_idx = 0;
232 unsigned int heap_idx = 1;
233 unsigned int idx;
235 if (heap->entries[heap_idx].version <= version) return;
237 idx = heap->entries[heap_idx].idx;
238 if (constant_locations[idx] != -1) GL_EXTCALL(glUniform4fvARB(constant_locations[idx], 1, &constants[idx * 4]));
239 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
241 while (stack_idx >= 0)
243 /* Note that we fall through to the next case statement. */
244 switch(stack[stack_idx])
246 case HEAP_NODE_TRAVERSE_LEFT:
248 unsigned int left_idx = heap_idx << 1;
249 if (left_idx < heap->size && heap->entries[left_idx].version > version)
251 heap_idx = left_idx;
252 idx = heap->entries[heap_idx].idx;
253 if (constant_locations[idx] != -1)
254 GL_EXTCALL(glUniform4fvARB(constant_locations[idx], 1, &constants[idx * 4]));
256 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
257 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
258 break;
262 case HEAP_NODE_TRAVERSE_RIGHT:
264 unsigned int right_idx = (heap_idx << 1) + 1;
265 if (right_idx < heap->size && heap->entries[right_idx].version > version)
267 heap_idx = right_idx;
268 idx = heap->entries[heap_idx].idx;
269 if (constant_locations[idx] != -1)
270 GL_EXTCALL(glUniform4fvARB(constant_locations[idx], 1, &constants[idx * 4]));
272 stack[stack_idx++] = HEAP_NODE_POP;
273 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
274 break;
278 case HEAP_NODE_POP:
280 heap_idx >>= 1;
281 --stack_idx;
282 break;
286 checkGLcall("walk_constant_heap()");
289 static inline void apply_clamped_constant(const WineD3D_GL_Info *gl_info, GLint location, const GLfloat *data)
291 GLfloat clamped_constant[4];
293 if (location == -1) return;
295 clamped_constant[0] = data[0] < -1.0f ? -1.0f : data[0] > 1.0 ? 1.0 : data[0];
296 clamped_constant[1] = data[1] < -1.0f ? -1.0f : data[1] > 1.0 ? 1.0 : data[1];
297 clamped_constant[2] = data[2] < -1.0f ? -1.0f : data[2] > 1.0 ? 1.0 : data[2];
298 clamped_constant[3] = data[3] < -1.0f ? -1.0f : data[3] > 1.0 ? 1.0 : data[3];
300 GL_EXTCALL(glUniform4fvARB(location, 1, clamped_constant));
303 static inline void walk_constant_heap_clamped(const WineD3D_GL_Info *gl_info, const float *constants,
304 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
306 int stack_idx = 0;
307 unsigned int heap_idx = 1;
308 unsigned int idx;
310 if (heap->entries[heap_idx].version <= version) return;
312 idx = heap->entries[heap_idx].idx;
313 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
314 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
316 while (stack_idx >= 0)
318 /* Note that we fall through to the next case statement. */
319 switch(stack[stack_idx])
321 case HEAP_NODE_TRAVERSE_LEFT:
323 unsigned int left_idx = heap_idx << 1;
324 if (left_idx < heap->size && heap->entries[left_idx].version > version)
326 heap_idx = left_idx;
327 idx = heap->entries[heap_idx].idx;
328 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
330 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
331 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
332 break;
336 case HEAP_NODE_TRAVERSE_RIGHT:
338 unsigned int right_idx = (heap_idx << 1) + 1;
339 if (right_idx < heap->size && heap->entries[right_idx].version > version)
341 heap_idx = right_idx;
342 idx = heap->entries[heap_idx].idx;
343 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
345 stack[stack_idx++] = HEAP_NODE_POP;
346 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
347 break;
351 case HEAP_NODE_POP:
353 heap_idx >>= 1;
354 --stack_idx;
355 break;
359 checkGLcall("walk_constant_heap_clamped()");
362 /* Loads floating point constants (aka uniforms) into the currently set GLSL program. */
363 static void shader_glsl_load_constantsF(IWineD3DBaseShaderImpl *This, const WineD3D_GL_Info *gl_info,
364 const float *constants, const GLint *constant_locations, const struct constant_heap *heap,
365 unsigned char *stack, UINT version)
367 const local_constant *lconst;
369 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
370 if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.reg_maps.shader_version) == 1
371 && shader_is_pshader_version(This->baseShader.reg_maps.shader_version))
372 walk_constant_heap_clamped(gl_info, constants, constant_locations, heap, stack, version);
373 else
374 walk_constant_heap(gl_info, constants, constant_locations, heap, stack, version);
376 if (!This->baseShader.load_local_constsF)
378 TRACE("No need to load local float constants for this shader\n");
379 return;
382 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
383 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry)
385 GLint location = constant_locations[lconst->idx];
386 /* We found this uniform name in the program - go ahead and send the data */
387 if (location != -1) GL_EXTCALL(glUniform4fvARB(location, 1, (const GLfloat *)lconst->value));
389 checkGLcall("glUniform4fvARB()");
392 /* Loads integer constants (aka uniforms) into the currently set GLSL program. */
393 static void shader_glsl_load_constantsI(IWineD3DBaseShaderImpl *This, const WineD3D_GL_Info *gl_info,
394 const GLint locations[MAX_CONST_I], const int *constants, WORD constants_set)
396 unsigned int i;
397 struct list* ptr;
399 for (i = 0; constants_set; constants_set >>= 1, ++i)
401 if (!(constants_set & 1)) continue;
403 TRACE_(d3d_constants)("Loading constants %u: %i, %i, %i, %i\n",
404 i, constants[i*4], constants[i*4+1], constants[i*4+2], constants[i*4+3]);
406 /* We found this uniform name in the program - go ahead and send the data */
407 GL_EXTCALL(glUniform4ivARB(locations[i], 1, &constants[i*4]));
408 checkGLcall("glUniform4ivARB");
411 /* Load immediate constants */
412 ptr = list_head(&This->baseShader.constantsI);
413 while (ptr) {
414 const struct local_constant *lconst = LIST_ENTRY(ptr, const struct local_constant, entry);
415 unsigned int idx = lconst->idx;
416 const GLint *values = (const GLint *)lconst->value;
418 TRACE_(d3d_constants)("Loading local constants %i: %i, %i, %i, %i\n", idx,
419 values[0], values[1], values[2], values[3]);
421 /* We found this uniform name in the program - go ahead and send the data */
422 GL_EXTCALL(glUniform4ivARB(locations[idx], 1, values));
423 checkGLcall("glUniform4ivARB");
424 ptr = list_next(&This->baseShader.constantsI, ptr);
428 /* Loads boolean constants (aka uniforms) into the currently set GLSL program. */
429 static void shader_glsl_load_constantsB(IWineD3DBaseShaderImpl *This, const WineD3D_GL_Info *gl_info,
430 GLhandleARB programId, const BOOL *constants, WORD constants_set)
432 GLint tmp_loc;
433 unsigned int i;
434 char tmp_name[8];
435 char is_pshader = shader_is_pshader_version(This->baseShader.reg_maps.shader_version);
436 const char* prefix = is_pshader? "PB":"VB";
437 struct list* ptr;
439 /* TODO: Benchmark and see if it would be beneficial to store the
440 * locations of the constants to avoid looking up each time */
441 for (i = 0; constants_set; constants_set >>= 1, ++i)
443 if (!(constants_set & 1)) continue;
445 TRACE_(d3d_constants)("Loading constants %i: %i;\n", i, constants[i]);
447 /* TODO: Benchmark and see if it would be beneficial to store the
448 * locations of the constants to avoid looking up each time */
449 snprintf(tmp_name, sizeof(tmp_name), "%s[%i]", prefix, i);
450 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
451 if (tmp_loc != -1)
453 /* We found this uniform name in the program - go ahead and send the data */
454 GL_EXTCALL(glUniform1ivARB(tmp_loc, 1, &constants[i]));
455 checkGLcall("glUniform1ivARB");
459 /* Load immediate constants */
460 ptr = list_head(&This->baseShader.constantsB);
461 while (ptr) {
462 const struct local_constant *lconst = LIST_ENTRY(ptr, const struct local_constant, entry);
463 unsigned int idx = lconst->idx;
464 const GLint *values = (const GLint *)lconst->value;
466 TRACE_(d3d_constants)("Loading local constants %i: %i\n", idx, values[0]);
468 snprintf(tmp_name, sizeof(tmp_name), "%s[%i]", prefix, idx);
469 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
470 if (tmp_loc != -1) {
471 /* We found this uniform name in the program - go ahead and send the data */
472 GL_EXTCALL(glUniform1ivARB(tmp_loc, 1, values));
473 checkGLcall("glUniform1ivARB");
475 ptr = list_next(&This->baseShader.constantsB, ptr);
479 static void reset_program_constant_version(void *value, void *context)
481 struct glsl_shader_prog_link *entry = value;
482 entry->constant_version = 0;
486 * Loads the app-supplied constants into the currently set GLSL program.
488 static void shader_glsl_load_constants(
489 IWineD3DDevice* device,
490 char usePixelShader,
491 char useVertexShader) {
493 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) device;
494 struct shader_glsl_priv *priv = deviceImpl->shader_priv;
495 IWineD3DStateBlockImpl* stateBlock = deviceImpl->stateBlock;
496 const WineD3D_GL_Info *gl_info = &deviceImpl->adapter->gl_info;
498 GLhandleARB programId;
499 struct glsl_shader_prog_link *prog = priv->glsl_program;
500 UINT constant_version;
501 int i;
503 if (!prog) {
504 /* No GLSL program set - nothing to do. */
505 return;
507 programId = prog->programId;
508 constant_version = prog->constant_version;
510 if (useVertexShader) {
511 IWineD3DBaseShaderImpl* vshader = (IWineD3DBaseShaderImpl*) stateBlock->vertexShader;
513 /* Load DirectX 9 float constants/uniforms for vertex shader */
514 shader_glsl_load_constantsF(vshader, gl_info, stateBlock->vertexShaderConstantF,
515 prog->vuniformF_locations, &priv->vconst_heap, priv->stack, constant_version);
517 /* Load DirectX 9 integer constants/uniforms for vertex shader */
518 if(vshader->baseShader.uses_int_consts) {
519 shader_glsl_load_constantsI(vshader, gl_info, prog->vuniformI_locations,
520 stateBlock->vertexShaderConstantI, stateBlock->changed.vertexShaderConstantsI);
523 /* Load DirectX 9 boolean constants/uniforms for vertex shader */
524 if(vshader->baseShader.uses_bool_consts) {
525 shader_glsl_load_constantsB(vshader, gl_info, programId,
526 stateBlock->vertexShaderConstantB, stateBlock->changed.vertexShaderConstantsB);
529 /* Upload the position fixup params */
530 GL_EXTCALL(glUniform4fvARB(prog->posFixup_location, 1, &deviceImpl->posFixup[0]));
531 checkGLcall("glUniform4fvARB");
534 if (usePixelShader) {
536 IWineD3DBaseShaderImpl* pshader = (IWineD3DBaseShaderImpl*) stateBlock->pixelShader;
538 /* Load DirectX 9 float constants/uniforms for pixel shader */
539 shader_glsl_load_constantsF(pshader, gl_info, stateBlock->pixelShaderConstantF,
540 prog->puniformF_locations, &priv->pconst_heap, priv->stack, constant_version);
542 /* Load DirectX 9 integer constants/uniforms for pixel shader */
543 if(pshader->baseShader.uses_int_consts) {
544 shader_glsl_load_constantsI(pshader, gl_info, prog->puniformI_locations,
545 stateBlock->pixelShaderConstantI, stateBlock->changed.pixelShaderConstantsI);
548 /* Load DirectX 9 boolean constants/uniforms for pixel shader */
549 if(pshader->baseShader.uses_bool_consts) {
550 shader_glsl_load_constantsB(pshader, gl_info, programId,
551 stateBlock->pixelShaderConstantB, stateBlock->changed.pixelShaderConstantsB);
554 /* Upload the environment bump map matrix if needed. The needsbumpmat member specifies the texture stage to load the matrix from.
555 * It can't be 0 for a valid texbem instruction.
557 for(i = 0; i < ((IWineD3DPixelShaderImpl *) pshader)->numbumpenvmatconsts; i++) {
558 IWineD3DPixelShaderImpl *ps = (IWineD3DPixelShaderImpl *) pshader;
559 int stage = ps->luminanceconst[i].texunit;
561 const float *data = (const float *)&stateBlock->textureState[(int)ps->bumpenvmatconst[i].texunit][WINED3DTSS_BUMPENVMAT00];
562 GL_EXTCALL(glUniformMatrix2fvARB(prog->bumpenvmat_location[i], 1, 0, data));
563 checkGLcall("glUniformMatrix2fvARB");
565 /* texbeml needs the luminance scale and offset too. If texbeml is used, needsbumpmat
566 * is set too, so we can check that in the needsbumpmat check
568 if(ps->baseShader.reg_maps.luminanceparams[stage]) {
569 const GLfloat *scale = (const GLfloat *)&stateBlock->textureState[stage][WINED3DTSS_BUMPENVLSCALE];
570 const GLfloat *offset = (const GLfloat *)&stateBlock->textureState[stage][WINED3DTSS_BUMPENVLOFFSET];
572 GL_EXTCALL(glUniform1fvARB(prog->luminancescale_location[i], 1, scale));
573 checkGLcall("glUniform1fvARB");
574 GL_EXTCALL(glUniform1fvARB(prog->luminanceoffset_location[i], 1, offset));
575 checkGLcall("glUniform1fvARB");
579 if(((IWineD3DPixelShaderImpl *) pshader)->vpos_uniform) {
580 float correction_params[4];
581 if(deviceImpl->render_offscreen) {
582 correction_params[0] = 0.0;
583 correction_params[1] = 1.0;
584 } else {
585 /* position is window relative, not viewport relative */
586 correction_params[0] = ((IWineD3DSurfaceImpl *) deviceImpl->render_targets[0])->currentDesc.Height;
587 correction_params[1] = -1.0;
589 GL_EXTCALL(glUniform4fvARB(prog->ycorrection_location, 1, correction_params));
592 /* Constant loading for texture rect coord fixup. */
593 if (prog->ps_args.texrect_fixup) {
594 UINT fixup = prog->ps_args.texrect_fixup;
596 for (i = 0; fixup; fixup >>= 1, ++i) {
597 if (-1 != prog->rectFixup_location[i]) {
598 const IWineD3DBaseTextureImpl* const tex = (const IWineD3DBaseTextureImpl*) stateBlock->textures[i];
599 if (!tex) {
600 FIXME("Non-existant texture is flagged for NP2 texcoord fixup\n");
601 continue;
602 } else {
603 const float tex_dim[2] = {tex->baseTexture.pow2Matrix[0], tex->baseTexture.pow2Matrix[5]};
604 GL_EXTCALL(glUniform2fvARB(prog->rectFixup_location[i], 1, tex_dim));
611 if (priv->next_constant_version == UINT_MAX)
613 TRACE("Max constant version reached, resetting to 0.\n");
614 hash_table_for_each_entry(priv->glsl_program_lookup, reset_program_constant_version, NULL);
615 priv->next_constant_version = 1;
617 else
619 prog->constant_version = priv->next_constant_version++;
623 static inline void update_heap_entry(struct constant_heap *heap, unsigned int idx,
624 unsigned int heap_idx, DWORD new_version)
626 struct constant_entry *entries = heap->entries;
627 unsigned int *positions = heap->positions;
628 unsigned int parent_idx;
630 while (heap_idx > 1)
632 parent_idx = heap_idx >> 1;
634 if (new_version <= entries[parent_idx].version) break;
636 entries[heap_idx] = entries[parent_idx];
637 positions[entries[parent_idx].idx] = heap_idx;
638 heap_idx = parent_idx;
641 entries[heap_idx].version = new_version;
642 entries[heap_idx].idx = idx;
643 positions[idx] = heap_idx;
646 static void shader_glsl_update_float_vertex_constants(IWineD3DDevice *iface, UINT start, UINT count)
648 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
649 struct shader_glsl_priv *priv = This->shader_priv;
650 struct constant_heap *heap = &priv->vconst_heap;
651 UINT i;
653 for (i = start; i < count + start; ++i)
655 if (!This->stateBlock->changed.vertexShaderConstantsF[i])
656 update_heap_entry(heap, i, heap->size++, priv->next_constant_version);
657 else
658 update_heap_entry(heap, i, heap->positions[i], priv->next_constant_version);
662 static void shader_glsl_update_float_pixel_constants(IWineD3DDevice *iface, UINT start, UINT count)
664 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
665 struct shader_glsl_priv *priv = This->shader_priv;
666 struct constant_heap *heap = &priv->pconst_heap;
667 UINT i;
669 for (i = start; i < count + start; ++i)
671 if (!This->stateBlock->changed.pixelShaderConstantsF[i])
672 update_heap_entry(heap, i, heap->size++, priv->next_constant_version);
673 else
674 update_heap_entry(heap, i, heap->positions[i], priv->next_constant_version);
678 /** Generate the variable & register declarations for the GLSL output target */
679 static void shader_generate_glsl_declarations(IWineD3DBaseShader *iface, const shader_reg_maps *reg_maps,
680 SHADER_BUFFER *buffer, const WineD3D_GL_Info *gl_info,
681 const struct ps_compile_args *ps_args)
683 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
684 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) This->baseShader.device;
685 DWORD shader_version = reg_maps->shader_version;
686 unsigned int i, extra_constants_needed = 0;
687 const local_constant *lconst;
689 /* There are some minor differences between pixel and vertex shaders */
690 char pshader = shader_is_pshader_version(shader_version);
691 char prefix = pshader ? 'P' : 'V';
693 /* Prototype the subroutines */
694 for (i = 0; i < This->baseShader.limits.label; i++) {
695 if (reg_maps->labels[i])
696 shader_addline(buffer, "void subroutine%u();\n", i);
699 /* Declare the constants (aka uniforms) */
700 if (This->baseShader.limits.constant_float > 0) {
701 unsigned max_constantsF = min(This->baseShader.limits.constant_float,
702 (pshader ? GL_LIMITS(pshader_constantsF) : GL_LIMITS(vshader_constantsF)));
703 shader_addline(buffer, "uniform vec4 %cC[%u];\n", prefix, max_constantsF);
706 if (This->baseShader.limits.constant_int > 0)
707 shader_addline(buffer, "uniform ivec4 %cI[%u];\n", prefix, This->baseShader.limits.constant_int);
709 if (This->baseShader.limits.constant_bool > 0)
710 shader_addline(buffer, "uniform bool %cB[%u];\n", prefix, This->baseShader.limits.constant_bool);
712 if(!pshader) {
713 shader_addline(buffer, "uniform vec4 posFixup;\n");
714 /* Predeclaration; This function is added at link time based on the pixel shader.
715 * VS 3.0 shaders have an array OUT[] the shader writes to, earlier versions don't have
716 * that. We know the input to the reorder function at vertex shader compile time, so
717 * we can deal with that. The reorder function for a 1.x and 2.x vertex shader can just
718 * read gl_FrontColor. The output depends on the pixel shader. The reorder function for a
719 * 1.x and 2.x pshader or for fixed function will write gl_FrontColor, and for a 3.0 shader
720 * it will write to the varying array. Here we depend on the shader optimizer on sorting that
721 * out. The nvidia driver only does that if the parameter is inout instead of out, hence the
722 * inout.
724 if (shader_version >= WINED3DVS_VERSION(3, 0))
726 shader_addline(buffer, "void order_ps_input(in vec4[%u]);\n", MAX_REG_OUTPUT);
727 } else {
728 shader_addline(buffer, "void order_ps_input();\n");
730 } else {
731 IWineD3DPixelShaderImpl *ps_impl = (IWineD3DPixelShaderImpl *) This;
733 ps_impl->numbumpenvmatconsts = 0;
734 for(i = 0; i < (sizeof(reg_maps->bumpmat) / sizeof(reg_maps->bumpmat[0])); i++) {
735 if(!reg_maps->bumpmat[i]) {
736 continue;
739 ps_impl->bumpenvmatconst[(int) ps_impl->numbumpenvmatconsts].texunit = i;
740 shader_addline(buffer, "uniform mat2 bumpenvmat%d;\n", i);
742 if(reg_maps->luminanceparams) {
743 ps_impl->luminanceconst[(int) ps_impl->numbumpenvmatconsts].texunit = i;
744 shader_addline(buffer, "uniform float luminancescale%d;\n", i);
745 shader_addline(buffer, "uniform float luminanceoffset%d;\n", i);
746 extra_constants_needed++;
747 } else {
748 ps_impl->luminanceconst[(int) ps_impl->numbumpenvmatconsts].texunit = -1;
751 extra_constants_needed++;
752 ps_impl->numbumpenvmatconsts++;
755 if(ps_args->srgb_correction) {
756 shader_addline(buffer, "const vec4 srgb_mul_low = vec4(%f, %f, %f, %f);\n",
757 srgb_mul_low, srgb_mul_low, srgb_mul_low, srgb_mul_low);
758 shader_addline(buffer, "const vec4 srgb_comparison = vec4(%f, %f, %f, %f);\n",
759 srgb_cmp, srgb_cmp, srgb_cmp, srgb_cmp);
761 if(reg_maps->vpos || reg_maps->usesdsy) {
762 if(This->baseShader.limits.constant_float + extra_constants_needed + 1 < GL_LIMITS(pshader_constantsF)) {
763 shader_addline(buffer, "uniform vec4 ycorrection;\n");
764 ((IWineD3DPixelShaderImpl *) This)->vpos_uniform = 1;
765 extra_constants_needed++;
766 } else {
767 /* This happens because we do not have proper tracking of the constant registers that are
768 * actually used, only the max limit of the shader version
770 FIXME("Cannot find a free uniform for vpos correction params\n");
771 shader_addline(buffer, "const vec4 ycorrection = vec4(%f, %f, 0.0, 0.0);\n",
772 device->render_offscreen ? 0.0 : ((IWineD3DSurfaceImpl *) device->render_targets[0])->currentDesc.Height,
773 device->render_offscreen ? 1.0 : -1.0);
775 shader_addline(buffer, "vec4 vpos;\n");
779 /* Declare texture samplers */
780 for (i = 0; i < This->baseShader.limits.sampler; i++) {
781 if (reg_maps->samplers[i]) {
783 DWORD stype = reg_maps->samplers[i] & WINED3DSP_TEXTURETYPE_MASK;
784 switch (stype) {
786 case WINED3DSTT_1D:
787 shader_addline(buffer, "uniform sampler1D %csampler%u;\n", prefix, i);
788 break;
789 case WINED3DSTT_2D:
790 if(device->stateBlock->textures[i] &&
791 IWineD3DBaseTexture_GetTextureDimensions(device->stateBlock->textures[i]) == GL_TEXTURE_RECTANGLE_ARB) {
792 shader_addline(buffer, "uniform sampler2DRect %csampler%u;\n", prefix, i);
793 } else {
794 shader_addline(buffer, "uniform sampler2D %csampler%u;\n", prefix, i);
797 if(ps_args->texrect_fixup & (1 << i)) {
798 /* RECT textures in OpenGL use texcoords in the range [0,width]x[0,height]
799 * while D3D has them in the (normalized) [0,1]x[0,1] range.
800 * samplerRectFixup stores texture dimensions and is updated through
801 * shader_glsl_load_constants when the sampler changes. */
802 shader_addline(buffer, "uniform vec2 %csamplerRectFixup%u;\n", prefix, i);
804 break;
805 case WINED3DSTT_CUBE:
806 shader_addline(buffer, "uniform samplerCube %csampler%u;\n", prefix, i);
807 break;
808 case WINED3DSTT_VOLUME:
809 shader_addline(buffer, "uniform sampler3D %csampler%u;\n", prefix, i);
810 break;
811 default:
812 shader_addline(buffer, "uniform unsupported_sampler %csampler%u;\n", prefix, i);
813 FIXME("Unrecognized sampler type: %#x\n", stype);
814 break;
819 /* Declare address variables */
820 for (i = 0; i < This->baseShader.limits.address; i++) {
821 if (reg_maps->address[i])
822 shader_addline(buffer, "ivec4 A%d;\n", i);
825 /* Declare texture coordinate temporaries and initialize them */
826 for (i = 0; i < This->baseShader.limits.texcoord; i++) {
827 if (reg_maps->texcoord[i])
828 shader_addline(buffer, "vec4 T%u = gl_TexCoord[%u];\n", i, i);
831 /* Declare input register varyings. Only pixel shader, vertex shaders have that declared in the
832 * helper function shader that is linked in at link time
834 if (pshader && shader_version >= WINED3DPS_VERSION(3, 0))
836 if (use_vs(device->stateBlock))
838 shader_addline(buffer, "varying vec4 IN[%u];\n", GL_LIMITS(glsl_varyings) / 4);
839 } else {
840 /* TODO: Write a replacement shader for the fixed function vertex pipeline, so this isn't needed.
841 * For fixed function vertex processing + 3.0 pixel shader we need a separate function in the
842 * pixel shader that reads the fixed function color into the packed input registers.
844 shader_addline(buffer, "vec4 IN[%u];\n", GL_LIMITS(glsl_varyings) / 4);
848 /* Declare output register temporaries */
849 if(This->baseShader.limits.packed_output) {
850 shader_addline(buffer, "vec4 OUT[%u];\n", This->baseShader.limits.packed_output);
853 /* Declare temporary variables */
854 for(i = 0; i < This->baseShader.limits.temporary; i++) {
855 if (reg_maps->temporary[i])
856 shader_addline(buffer, "vec4 R%u;\n", i);
859 /* Declare attributes */
860 for (i = 0; i < This->baseShader.limits.attributes; i++) {
861 if (reg_maps->attributes[i])
862 shader_addline(buffer, "attribute vec4 attrib%i;\n", i);
865 /* Declare loop registers aLx */
866 for (i = 0; i < reg_maps->loop_depth; i++) {
867 shader_addline(buffer, "int aL%u;\n", i);
868 shader_addline(buffer, "int tmpInt%u;\n", i);
871 /* Temporary variables for matrix operations */
872 shader_addline(buffer, "vec4 tmp0;\n");
873 shader_addline(buffer, "vec4 tmp1;\n");
875 /* Local constants use a different name so they can be loaded once at shader link time
876 * They can't be hardcoded into the shader text via LC = {x, y, z, w}; because the
877 * float -> string conversion can cause precision loss.
879 if(!This->baseShader.load_local_constsF) {
880 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
881 shader_addline(buffer, "uniform vec4 %cLC%u;\n", prefix, lconst->idx);
885 /* Start the main program */
886 shader_addline(buffer, "void main() {\n");
887 if(pshader && reg_maps->vpos) {
888 /* DirectX apps expect integer values, while OpenGL drivers add approximately 0.5. This causes
889 * off-by-one problems as spotted by the vPos d3d9 visual test. Unfortunately the ATI cards do
890 * not add exactly 0.5, but rather something like 0.49999999 or 0.50000001, which still causes
891 * precision troubles when we just substract 0.5.
893 * To deal with that just floor() the position. This will eliminate the fraction on all cards.
895 * TODO: Test how that behaves with multisampling once we can enable multisampling in winex11.
897 * An advantage of floor is that it works even if the driver doesn't add 1/2. It is somewhat
898 * questionable if 1.5, 2.5, ... are the proper values to return in gl_FragCoord, even though
899 * coordinates specify the pixel centers instead of the pixel corners. This code will behave
900 * correctly on drivers that returns integer values.
902 shader_addline(buffer, "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
906 /*****************************************************************************
907 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
909 * For more information, see http://wiki.winehq.org/DirectX-Shaders
910 ****************************************************************************/
912 /* Prototypes */
913 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins, const DWORD param,
914 const DWORD addr_token, DWORD mask, glsl_src_param_t *src_param);
916 /** Used for opcode modifiers - They multiply the result by the specified amount */
917 static const char * const shift_glsl_tab[] = {
918 "", /* 0 (none) */
919 "2.0 * ", /* 1 (x2) */
920 "4.0 * ", /* 2 (x4) */
921 "8.0 * ", /* 3 (x8) */
922 "16.0 * ", /* 4 (x16) */
923 "32.0 * ", /* 5 (x32) */
924 "", /* 6 (x64) */
925 "", /* 7 (x128) */
926 "", /* 8 (d256) */
927 "", /* 9 (d128) */
928 "", /* 10 (d64) */
929 "", /* 11 (d32) */
930 "0.0625 * ", /* 12 (d16) */
931 "0.125 * ", /* 13 (d8) */
932 "0.25 * ", /* 14 (d4) */
933 "0.5 * " /* 15 (d2) */
936 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
937 static void shader_glsl_gen_modifier (
938 const DWORD instr,
939 const char *in_reg,
940 const char *in_regswizzle,
941 char *out_str) {
943 out_str[0] = 0;
945 switch (instr & WINED3DSP_SRCMOD_MASK) {
946 case WINED3DSPSM_DZ: /* Need to handle this in the instructions itself (texld & texcrd). */
947 case WINED3DSPSM_DW:
948 case WINED3DSPSM_NONE:
949 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
950 break;
951 case WINED3DSPSM_NEG:
952 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
953 break;
954 case WINED3DSPSM_NOT:
955 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
956 break;
957 case WINED3DSPSM_BIAS:
958 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
959 break;
960 case WINED3DSPSM_BIASNEG:
961 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
962 break;
963 case WINED3DSPSM_SIGN:
964 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
965 break;
966 case WINED3DSPSM_SIGNNEG:
967 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
968 break;
969 case WINED3DSPSM_COMP:
970 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
971 break;
972 case WINED3DSPSM_X2:
973 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
974 break;
975 case WINED3DSPSM_X2NEG:
976 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
977 break;
978 case WINED3DSPSM_ABS:
979 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
980 break;
981 case WINED3DSPSM_ABSNEG:
982 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
983 break;
984 default:
985 FIXME("Unhandled modifier %u\n", (instr & WINED3DSP_SRCMOD_MASK));
986 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
990 /** Writes the GLSL variable name that corresponds to the register that the
991 * DX opcode parameter is trying to access */
992 static void shader_glsl_get_register_name(WINED3DSHADER_PARAM_REGISTER_TYPE register_type, UINT register_idx,
993 BOOL rel_addr, const DWORD addr_token, char *register_name, BOOL *is_color,
994 const struct wined3d_shader_instruction *ins)
996 /* oPos, oFog and oPts in D3D */
997 static const char * const hwrastout_reg_names[] = { "gl_Position", "gl_FogFragCoord", "gl_PointSize" };
999 IWineD3DBaseShaderImpl *This = (IWineD3DBaseShaderImpl *)ins->shader;
1000 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
1001 const WineD3D_GL_Info* gl_info = &deviceImpl->adapter->gl_info;
1002 DWORD shader_version = This->baseShader.reg_maps.shader_version;
1003 char pshader = shader_is_pshader_version(shader_version);
1005 *is_color = FALSE;
1007 switch (register_type)
1009 case WINED3DSPR_TEMP:
1010 sprintf(register_name, "R%u", register_idx);
1011 break;
1012 case WINED3DSPR_INPUT:
1013 if (pshader) {
1014 /* Pixel shaders >= 3.0 */
1015 if (WINED3DSHADER_VERSION_MAJOR(shader_version) >= 3)
1017 DWORD in_count = GL_LIMITS(glsl_varyings) / 4;
1019 if (rel_addr)
1021 glsl_src_param_t rel_param;
1022 shader_glsl_add_src_param(ins, addr_token, 0, WINED3DSP_WRITEMASK_0, &rel_param);
1024 /* Removing a + 0 would be an obvious optimization, but macos doesn't see the NOP
1025 * operation there
1027 if (((IWineD3DPixelShaderImpl *)This)->input_reg_map[register_idx])
1029 if (((IWineD3DPixelShaderImpl *)This)->declared_in_count > in_count) {
1030 sprintf(register_name, "((%s + %u) > %d ? (%s + %u) > %d ? gl_SecondaryColor : gl_Color : IN[%s + %u])",
1031 rel_param.param_str, ((IWineD3DPixelShaderImpl *)This)->input_reg_map[register_idx], in_count - 1,
1032 rel_param.param_str, ((IWineD3DPixelShaderImpl *)This)->input_reg_map[register_idx], in_count,
1033 rel_param.param_str, ((IWineD3DPixelShaderImpl *)This)->input_reg_map[register_idx]);
1034 } else {
1035 sprintf(register_name, "IN[%s + %u]", rel_param.param_str,
1036 ((IWineD3DPixelShaderImpl *)This)->input_reg_map[register_idx]);
1038 } else {
1039 if (((IWineD3DPixelShaderImpl *)This)->declared_in_count > in_count) {
1040 sprintf(register_name, "((%s) > %d ? (%s) > %d ? gl_SecondaryColor : gl_Color : IN[%s])",
1041 rel_param.param_str, in_count - 1,
1042 rel_param.param_str, in_count,
1043 rel_param.param_str);
1044 } else {
1045 sprintf(register_name, "IN[%s]", rel_param.param_str);
1048 } else {
1049 DWORD idx = ((IWineD3DPixelShaderImpl *) This)->input_reg_map[register_idx];
1050 if (idx == in_count) {
1051 sprintf(register_name, "gl_Color");
1052 } else if (idx == in_count + 1) {
1053 sprintf(register_name, "gl_SecondaryColor");
1054 } else {
1055 sprintf(register_name, "IN[%u]", idx);
1058 } else {
1059 if (register_idx == 0)
1060 strcpy(register_name, "gl_Color");
1061 else
1062 strcpy(register_name, "gl_SecondaryColor");
1064 } else {
1065 if (((IWineD3DVertexShaderImpl *)This)->cur_args->swizzle_map & (1 << register_idx)) *is_color = TRUE;
1066 sprintf(register_name, "attrib%u", register_idx);
1068 break;
1069 case WINED3DSPR_CONST:
1071 const char prefix = pshader? 'P':'V';
1073 /* Relative addressing */
1074 if (rel_addr)
1076 /* Relative addressing on shaders 2.0+ have a relative address token,
1077 * prior to that, it was hard-coded as "A0.x" because there's only 1 register */
1078 if (WINED3DSHADER_VERSION_MAJOR(shader_version) >= 2)
1080 glsl_src_param_t rel_param;
1081 shader_glsl_add_src_param(ins, addr_token, 0, WINED3DSP_WRITEMASK_0, &rel_param);
1082 if (register_idx)
1084 sprintf(register_name, "%cC[%s + %u]", prefix, rel_param.param_str, register_idx);
1085 } else {
1086 sprintf(register_name, "%cC[%s]", prefix, rel_param.param_str);
1088 } else {
1089 if (register_idx)
1091 sprintf(register_name, "%cC[A0.x + %u]", prefix, register_idx);
1092 } else {
1093 sprintf(register_name, "%cC[A0.x]", prefix);
1097 } else {
1098 if (shader_constant_is_local(This, register_idx))
1100 sprintf(register_name, "%cLC%u", prefix, register_idx);
1101 } else {
1102 sprintf(register_name, "%cC[%u]", prefix, register_idx);
1106 break;
1108 case WINED3DSPR_CONSTINT:
1109 if (pshader)
1110 sprintf(register_name, "PI[%u]", register_idx);
1111 else
1112 sprintf(register_name, "VI[%u]", register_idx);
1113 break;
1114 case WINED3DSPR_CONSTBOOL:
1115 if (pshader)
1116 sprintf(register_name, "PB[%u]", register_idx);
1117 else
1118 sprintf(register_name, "VB[%u]", register_idx);
1119 break;
1120 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
1121 if (pshader) {
1122 sprintf(register_name, "T%u", register_idx);
1123 } else {
1124 sprintf(register_name, "A%u", register_idx);
1126 break;
1127 case WINED3DSPR_LOOP:
1128 sprintf(register_name, "aL%u", This->baseShader.cur_loop_regno - 1);
1129 break;
1130 case WINED3DSPR_SAMPLER:
1131 if (pshader)
1132 sprintf(register_name, "Psampler%u", register_idx);
1133 else
1134 sprintf(register_name, "Vsampler%u", register_idx);
1135 break;
1136 case WINED3DSPR_COLOROUT:
1137 if (register_idx >= GL_LIMITS(buffers))
1138 WARN("Write to render target %u, only %d supported\n", register_idx, 4);
1140 if (GL_SUPPORT(ARB_DRAW_BUFFERS)) {
1141 sprintf(register_name, "gl_FragData[%u]", register_idx);
1142 } else { /* On older cards with GLSL support like the GeforceFX there's only one buffer. */
1143 sprintf(register_name, "gl_FragColor");
1145 break;
1146 case WINED3DSPR_RASTOUT:
1147 sprintf(register_name, "%s", hwrastout_reg_names[register_idx]);
1148 break;
1149 case WINED3DSPR_DEPTHOUT:
1150 sprintf(register_name, "gl_FragDepth");
1151 break;
1152 case WINED3DSPR_ATTROUT:
1153 if (register_idx == 0)
1155 sprintf(register_name, "gl_FrontColor");
1156 } else {
1157 sprintf(register_name, "gl_FrontSecondaryColor");
1159 break;
1160 case WINED3DSPR_TEXCRDOUT:
1161 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
1162 if (WINED3DSHADER_VERSION_MAJOR(shader_version) >= 3) sprintf(register_name, "OUT[%u]", register_idx);
1163 else sprintf(register_name, "gl_TexCoord[%u]", register_idx);
1164 break;
1165 case WINED3DSPR_MISCTYPE:
1166 if (register_idx == 0)
1168 /* vPos */
1169 sprintf(register_name, "vpos");
1171 else if (register_idx == 1)
1173 /* Note that gl_FrontFacing is a bool, while vFace is
1174 * a float for which the sign determines front/back
1176 sprintf(register_name, "(gl_FrontFacing ? 1.0 : -1.0)");
1177 } else {
1178 FIXME("Unhandled misctype register %d\n", register_idx);
1179 sprintf(register_name, "unrecognized_register");
1181 break;
1182 default:
1183 FIXME("Unhandled register name Type(%d)\n", register_type);
1184 sprintf(register_name, "unrecognized_register");
1185 break;
1189 static void shader_glsl_write_mask_to_str(DWORD write_mask, char *str)
1191 *str++ = '.';
1192 if (write_mask & WINED3DSP_WRITEMASK_0) *str++ = 'x';
1193 if (write_mask & WINED3DSP_WRITEMASK_1) *str++ = 'y';
1194 if (write_mask & WINED3DSP_WRITEMASK_2) *str++ = 'z';
1195 if (write_mask & WINED3DSP_WRITEMASK_3) *str++ = 'w';
1196 *str = '\0';
1199 /* Get the GLSL write mask for the destination register */
1200 static DWORD shader_glsl_get_write_mask(const struct wined3d_shader_dst_param *param, char *write_mask)
1202 DWORD mask = param->write_mask;
1204 if (shader_is_scalar(param->register_type, param->register_idx))
1206 mask = WINED3DSP_WRITEMASK_0;
1207 *write_mask = '\0';
1209 else
1211 shader_glsl_write_mask_to_str(mask, write_mask);
1214 return mask;
1217 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask) {
1218 unsigned int size = 0;
1220 if (write_mask & WINED3DSP_WRITEMASK_0) ++size;
1221 if (write_mask & WINED3DSP_WRITEMASK_1) ++size;
1222 if (write_mask & WINED3DSP_WRITEMASK_2) ++size;
1223 if (write_mask & WINED3DSP_WRITEMASK_3) ++size;
1225 return size;
1228 static void shader_glsl_get_swizzle(const DWORD param, BOOL fixup, DWORD mask, char *swizzle_str) {
1229 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
1230 * but addressed as "rgba". To fix this we need to swap the register's x
1231 * and z components. */
1232 DWORD swizzle = (param & WINED3DSP_SWIZZLE_MASK) >> WINED3DSP_SWIZZLE_SHIFT;
1233 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
1234 char *ptr = swizzle_str;
1236 if (!shader_is_scalar(shader_get_regtype(param), param & WINED3DSP_REGNUM_MASK))
1238 *ptr++ = '.';
1239 /* swizzle bits fields: wwzzyyxx */
1240 if (mask & WINED3DSP_WRITEMASK_0) *ptr++ = swizzle_chars[swizzle & 0x03];
1241 if (mask & WINED3DSP_WRITEMASK_1) *ptr++ = swizzle_chars[(swizzle >> 2) & 0x03];
1242 if (mask & WINED3DSP_WRITEMASK_2) *ptr++ = swizzle_chars[(swizzle >> 4) & 0x03];
1243 if (mask & WINED3DSP_WRITEMASK_3) *ptr++ = swizzle_chars[(swizzle >> 6) & 0x03];
1246 *ptr = '\0';
1249 /* From a given parameter token, generate the corresponding GLSL string.
1250 * Also, return the actual register name and swizzle in case the
1251 * caller needs this information as well. */
1252 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
1253 const DWORD param, const DWORD addr_token, DWORD mask, glsl_src_param_t *src_param)
1255 BOOL is_color = FALSE;
1256 char swizzle_str[6];
1258 src_param->reg_name[0] = '\0';
1259 src_param->param_str[0] = '\0';
1260 swizzle_str[0] = '\0';
1262 shader_glsl_get_register_name(shader_get_regtype(param), param & WINED3DSP_REGNUM_MASK,
1263 param & WINED3DSHADER_ADDRMODE_RELATIVE, addr_token, src_param->reg_name, &is_color, ins);
1265 shader_glsl_get_swizzle(param, is_color, mask, swizzle_str);
1266 shader_glsl_gen_modifier(param, src_param->reg_name, swizzle_str, src_param->param_str);
1269 /* From a given parameter token, generate the corresponding GLSL string.
1270 * Also, return the actual register name and swizzle in case the
1271 * caller needs this information as well. */
1272 static DWORD shader_glsl_add_dst_param(const struct wined3d_shader_instruction *ins,
1273 const struct wined3d_shader_dst_param *wined3d_dst, glsl_dst_param_t *glsl_dst)
1275 BOOL is_color = FALSE;
1277 glsl_dst->mask_str[0] = '\0';
1278 glsl_dst->reg_name[0] = '\0';
1280 shader_glsl_get_register_name(wined3d_dst->register_type, wined3d_dst->register_idx,
1281 wined3d_dst->token & WINED3DSHADER_ADDRMODE_RELATIVE, wined3d_dst->addr_token,
1282 glsl_dst->reg_name, &is_color, ins);
1283 return shader_glsl_get_write_mask(wined3d_dst, glsl_dst->mask_str);
1286 /* Append the destination part of the instruction to the buffer, return the effective write mask */
1287 static DWORD shader_glsl_append_dst_ext(SHADER_BUFFER *buffer,
1288 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_dst_param *dst)
1290 glsl_dst_param_t glsl_dst;
1291 DWORD mask;
1292 int shift;
1294 mask = shader_glsl_add_dst_param(ins, dst, &glsl_dst);
1295 if (mask)
1297 shift = (dst->token & WINED3DSP_DSTSHIFT_MASK) >> WINED3DSP_DSTSHIFT_SHIFT;
1298 shader_addline(buffer, "%s%s = %s(", glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[shift]);
1301 return mask;
1304 /* Append the destination part of the instruction to the buffer, return the effective write mask */
1305 static DWORD shader_glsl_append_dst(SHADER_BUFFER *buffer, const struct wined3d_shader_instruction *ins)
1307 return shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0]);
1310 /** Process GLSL instruction modifiers */
1311 void shader_glsl_add_instruction_modifiers(const struct wined3d_shader_instruction *ins)
1313 glsl_dst_param_t dst_param;
1314 DWORD modifiers;
1316 if (!ins->dst_count) return;
1318 modifiers = ins->dst[0].modifiers;
1319 if (!modifiers) return;
1321 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
1323 if (modifiers & WINED3DSPDM_SATURATE)
1325 /* _SAT means to clamp the value of the register to between 0 and 1 */
1326 shader_addline(ins->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param.reg_name,
1327 dst_param.mask_str, dst_param.reg_name, dst_param.mask_str);
1330 if (modifiers & WINED3DSPDM_MSAMPCENTROID)
1332 FIXME("_centroid modifier not handled\n");
1335 if (modifiers & WINED3DSPDM_PARTIALPRECISION)
1337 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
1341 static inline const char *shader_get_comp_op(DWORD flags)
1343 DWORD op = (flags & INST_CONTROLS_MASK) >> INST_CONTROLS_SHIFT;
1344 switch (op) {
1345 case COMPARISON_GT: return ">";
1346 case COMPARISON_EQ: return "==";
1347 case COMPARISON_GE: return ">=";
1348 case COMPARISON_LT: return "<";
1349 case COMPARISON_NE: return "!=";
1350 case COMPARISON_LE: return "<=";
1351 default:
1352 FIXME("Unrecognized comparison value: %u\n", op);
1353 return "(\?\?)";
1357 static void shader_glsl_get_sample_function(DWORD sampler_type, DWORD flags, glsl_sample_function_t *sample_function)
1359 BOOL projected = flags & WINED3D_GLSL_SAMPLE_PROJECTED;
1360 BOOL texrect = flags & WINED3D_GLSL_SAMPLE_RECT;
1361 BOOL lod = flags & WINED3D_GLSL_SAMPLE_LOD;
1363 /* Note that there's no such thing as a projected cube texture. */
1364 switch(sampler_type) {
1365 case WINED3DSTT_1D:
1366 if(lod) {
1367 sample_function->name = projected ? "texture1DProjLod" : "texture1DLod";
1368 } else {
1369 sample_function->name = projected ? "texture1DProj" : "texture1D";
1371 sample_function->coord_mask = WINED3DSP_WRITEMASK_0;
1372 break;
1373 case WINED3DSTT_2D:
1374 if(texrect) {
1375 if(lod) {
1376 sample_function->name = projected ? "texture2DRectProjLod" : "texture2DRectLod";
1377 } else {
1378 sample_function->name = projected ? "texture2DRectProj" : "texture2DRect";
1380 } else {
1381 if(lod) {
1382 sample_function->name = projected ? "texture2DProjLod" : "texture2DLod";
1383 } else {
1384 sample_function->name = projected ? "texture2DProj" : "texture2D";
1387 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
1388 break;
1389 case WINED3DSTT_CUBE:
1390 if(lod) {
1391 sample_function->name = "textureCubeLod";
1392 } else {
1393 sample_function->name = "textureCube";
1395 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1396 break;
1397 case WINED3DSTT_VOLUME:
1398 if(lod) {
1399 sample_function->name = projected ? "texture3DProjLod" : "texture3DLod";
1400 } else {
1401 sample_function->name = projected ? "texture3DProj" : "texture3D";
1403 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1404 break;
1405 default:
1406 sample_function->name = "";
1407 sample_function->coord_mask = 0;
1408 FIXME("Unrecognized sampler type: %#x;\n", sampler_type);
1409 break;
1413 static void shader_glsl_append_fixup_arg(char *arguments, const char *reg_name,
1414 BOOL sign_fixup, enum fixup_channel_source channel_source)
1416 switch(channel_source)
1418 case CHANNEL_SOURCE_ZERO:
1419 strcat(arguments, "0.0");
1420 break;
1422 case CHANNEL_SOURCE_ONE:
1423 strcat(arguments, "1.0");
1424 break;
1426 case CHANNEL_SOURCE_X:
1427 strcat(arguments, reg_name);
1428 strcat(arguments, ".x");
1429 break;
1431 case CHANNEL_SOURCE_Y:
1432 strcat(arguments, reg_name);
1433 strcat(arguments, ".y");
1434 break;
1436 case CHANNEL_SOURCE_Z:
1437 strcat(arguments, reg_name);
1438 strcat(arguments, ".z");
1439 break;
1441 case CHANNEL_SOURCE_W:
1442 strcat(arguments, reg_name);
1443 strcat(arguments, ".w");
1444 break;
1446 default:
1447 FIXME("Unhandled channel source %#x\n", channel_source);
1448 strcat(arguments, "undefined");
1449 break;
1452 if (sign_fixup) strcat(arguments, " * 2.0 - 1.0");
1455 static void shader_glsl_color_correction(const struct wined3d_shader_instruction *ins, struct color_fixup_desc fixup)
1457 struct wined3d_shader_dst_param dst;
1458 unsigned int mask_size, remaining;
1459 glsl_dst_param_t dst_param;
1460 char arguments[256];
1461 DWORD mask;
1463 mask = 0;
1464 if (fixup.x_sign_fixup || fixup.x_source != CHANNEL_SOURCE_X) mask |= WINED3DSP_WRITEMASK_0;
1465 if (fixup.y_sign_fixup || fixup.y_source != CHANNEL_SOURCE_Y) mask |= WINED3DSP_WRITEMASK_1;
1466 if (fixup.z_sign_fixup || fixup.z_source != CHANNEL_SOURCE_Z) mask |= WINED3DSP_WRITEMASK_2;
1467 if (fixup.w_sign_fixup || fixup.w_source != CHANNEL_SOURCE_W) mask |= WINED3DSP_WRITEMASK_3;
1468 mask &= ins->dst[0].write_mask;
1470 if (!mask) return; /* Nothing to do */
1472 if (is_yuv_fixup(fixup))
1474 enum yuv_fixup yuv_fixup = get_yuv_fixup(fixup);
1475 FIXME("YUV fixup (%#x) not supported\n", yuv_fixup);
1476 return;
1479 mask_size = shader_glsl_get_write_mask_size(mask);
1481 dst = ins->dst[0];
1482 dst.write_mask = mask;
1483 dst.token = (dst.token & ~WINED3DSP_WRITEMASK_ALL) | dst.write_mask;
1484 shader_glsl_add_dst_param(ins, &dst, &dst_param);
1486 arguments[0] = '\0';
1487 remaining = mask_size;
1488 if (mask & WINED3DSP_WRITEMASK_0)
1490 shader_glsl_append_fixup_arg(arguments, dst_param.reg_name, fixup.x_sign_fixup, fixup.x_source);
1491 if (--remaining) strcat(arguments, ", ");
1493 if (mask & WINED3DSP_WRITEMASK_1)
1495 shader_glsl_append_fixup_arg(arguments, dst_param.reg_name, fixup.y_sign_fixup, fixup.y_source);
1496 if (--remaining) strcat(arguments, ", ");
1498 if (mask & WINED3DSP_WRITEMASK_2)
1500 shader_glsl_append_fixup_arg(arguments, dst_param.reg_name, fixup.z_sign_fixup, fixup.z_source);
1501 if (--remaining) strcat(arguments, ", ");
1503 if (mask & WINED3DSP_WRITEMASK_3)
1505 shader_glsl_append_fixup_arg(arguments, dst_param.reg_name, fixup.w_sign_fixup, fixup.w_source);
1506 if (--remaining) strcat(arguments, ", ");
1509 if (mask_size > 1)
1511 shader_addline(ins->buffer, "%s%s = vec%u(%s);\n",
1512 dst_param.reg_name, dst_param.mask_str, mask_size, arguments);
1514 else
1516 shader_addline(ins->buffer, "%s%s = %s;\n", dst_param.reg_name, dst_param.mask_str, arguments);
1520 static void PRINTF_ATTR(6, 7) shader_glsl_gen_sample_code(const struct wined3d_shader_instruction *ins,
1521 DWORD sampler, const glsl_sample_function_t *sample_function, DWORD swizzle,
1522 const char *bias, const char *coord_reg_fmt, ...)
1524 const char *sampler_base;
1525 char dst_swizzle[6];
1526 struct color_fixup_desc fixup;
1527 BOOL rect_fixup = FALSE;
1528 va_list args;
1530 shader_glsl_get_swizzle(swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
1532 if (shader_is_pshader_version(ins->reg_maps->shader_version))
1534 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)ins->shader;
1535 fixup = This->cur_args->color_fixup[sampler];
1536 sampler_base = "Psampler";
1538 if(This->cur_args->texrect_fixup & (1 << sampler)) {
1539 if(bias) {
1540 FIXME("Biased sampling from RECT textures is unsupported\n");
1541 } else {
1542 rect_fixup = TRUE;
1545 } else {
1546 sampler_base = "Vsampler";
1547 fixup = COLOR_FIXUP_IDENTITY; /* FIXME: Vshader color fixup */
1550 shader_glsl_append_dst(ins->buffer, ins);
1552 shader_addline(ins->buffer, "%s(%s%u, ", sample_function->name, sampler_base, sampler);
1554 va_start(args, coord_reg_fmt);
1555 shader_vaddline(ins->buffer, coord_reg_fmt, args);
1556 va_end(args);
1558 if(bias) {
1559 shader_addline(ins->buffer, ", %s)%s);\n", bias, dst_swizzle);
1560 } else {
1561 if (rect_fixup) {
1562 shader_addline(ins->buffer, " * PsamplerRectFixup%u)%s);\n", sampler, dst_swizzle);
1563 } else {
1564 shader_addline(ins->buffer, ")%s);\n", dst_swizzle);
1568 if(!is_identity_fixup(fixup)) {
1569 shader_glsl_color_correction(ins, fixup);
1573 /*****************************************************************************
1575 * Begin processing individual instruction opcodes
1577 ****************************************************************************/
1579 /* Generate GLSL arithmetic functions (dst = src1 + src2) */
1580 static void shader_glsl_arith(const struct wined3d_shader_instruction *ins)
1582 SHADER_BUFFER *buffer = ins->buffer;
1583 glsl_src_param_t src0_param;
1584 glsl_src_param_t src1_param;
1585 DWORD write_mask;
1586 char op;
1588 /* Determine the GLSL operator to use based on the opcode */
1589 switch (ins->handler_idx)
1591 case WINED3DSIH_MUL: op = '*'; break;
1592 case WINED3DSIH_ADD: op = '+'; break;
1593 case WINED3DSIH_SUB: op = '-'; break;
1594 default:
1595 op = ' ';
1596 FIXME("Opcode %#x not yet handled in GLSL\n", ins->handler_idx);
1597 break;
1600 write_mask = shader_glsl_append_dst(buffer, ins);
1601 shader_glsl_add_src_param(ins, ins->src[0], ins->src_addr[0], write_mask, &src0_param);
1602 shader_glsl_add_src_param(ins, ins->src[1], ins->src_addr[1], write_mask, &src1_param);
1603 shader_addline(buffer, "%s %c %s);\n", src0_param.param_str, op, src1_param.param_str);
1606 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
1607 static void shader_glsl_mov(const struct wined3d_shader_instruction *ins)
1609 SHADER_BUFFER *buffer = ins->buffer;
1610 glsl_src_param_t src0_param;
1611 DWORD write_mask;
1613 write_mask = shader_glsl_append_dst(buffer, ins);
1614 shader_glsl_add_src_param(ins, ins->src[0], ins->src_addr[0], write_mask, &src0_param);
1616 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
1617 * shader versions WINED3DSIO_MOVA is used for this. */
1618 if ((WINED3DSHADER_VERSION_MAJOR(ins->reg_maps->shader_version) == 1
1619 && !shader_is_pshader_version(ins->reg_maps->shader_version)
1620 && ins->dst[0].register_type == WINED3DSPR_ADDR))
1622 /* This is a simple floor() */
1623 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
1624 if (mask_size > 1) {
1625 shader_addline(buffer, "ivec%d(floor(%s)));\n", mask_size, src0_param.param_str);
1626 } else {
1627 shader_addline(buffer, "int(floor(%s)));\n", src0_param.param_str);
1630 else if(ins->handler_idx == WINED3DSIH_MOVA)
1632 /* We need to *round* to the nearest int here. */
1633 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
1634 if (mask_size > 1) {
1635 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);
1636 } else {
1637 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n", src0_param.param_str, src0_param.param_str);
1639 } else {
1640 shader_addline(buffer, "%s);\n", src0_param.param_str);
1644 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
1645 static void shader_glsl_dot(const struct wined3d_shader_instruction *ins)
1647 SHADER_BUFFER *buffer = ins->buffer;
1648 glsl_src_param_t src0_param;
1649 glsl_src_param_t src1_param;
1650 DWORD dst_write_mask, src_write_mask;
1651 unsigned int dst_size = 0;
1653 dst_write_mask = shader_glsl_append_dst(buffer, ins);
1654 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
1656 /* dp3 works on vec3, dp4 on vec4 */
1657 if (ins->handler_idx == WINED3DSIH_DP4)
1659 src_write_mask = WINED3DSP_WRITEMASK_ALL;
1660 } else {
1661 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1664 shader_glsl_add_src_param(ins, ins->src[0], ins->src_addr[0], src_write_mask, &src0_param);
1665 shader_glsl_add_src_param(ins, ins->src[1], ins->src_addr[1], src_write_mask, &src1_param);
1667 if (dst_size > 1) {
1668 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
1669 } else {
1670 shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
1674 /* Note that this instruction has some restrictions. The destination write mask
1675 * can't contain the w component, and the source swizzles have to be .xyzw */
1676 static void shader_glsl_cross(const struct wined3d_shader_instruction *ins)
1678 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1679 glsl_src_param_t src0_param;
1680 glsl_src_param_t src1_param;
1681 char dst_mask[6];
1683 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
1684 shader_glsl_append_dst(ins->buffer, ins);
1685 shader_glsl_add_src_param(ins, ins->src[0], ins->src_addr[0], src_mask, &src0_param);
1686 shader_glsl_add_src_param(ins, ins->src[1], ins->src_addr[1], src_mask, &src1_param);
1687 shader_addline(ins->buffer, "cross(%s, %s)%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
1690 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
1691 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
1692 * GLSL uses the value as-is. */
1693 static void shader_glsl_pow(const struct wined3d_shader_instruction *ins)
1695 SHADER_BUFFER *buffer = ins->buffer;
1696 glsl_src_param_t src0_param;
1697 glsl_src_param_t src1_param;
1698 DWORD dst_write_mask;
1699 unsigned int dst_size;
1701 dst_write_mask = shader_glsl_append_dst(buffer, ins);
1702 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
1704 shader_glsl_add_src_param(ins, ins->src[0], ins->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1705 shader_glsl_add_src_param(ins, ins->src[1], ins->src_addr[1], WINED3DSP_WRITEMASK_0, &src1_param);
1707 if (dst_size > 1) {
1708 shader_addline(buffer, "vec%d(pow(abs(%s), %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
1709 } else {
1710 shader_addline(buffer, "pow(abs(%s), %s));\n", src0_param.param_str, src1_param.param_str);
1714 /* Process the WINED3DSIO_LOG instruction in GLSL (dst = log2(|src0|))
1715 * Src0 is a scalar. Note that D3D uses the absolute of src0, while
1716 * GLSL uses the value as-is. */
1717 static void shader_glsl_log(const struct wined3d_shader_instruction *ins)
1719 SHADER_BUFFER *buffer = ins->buffer;
1720 glsl_src_param_t src0_param;
1721 DWORD dst_write_mask;
1722 unsigned int dst_size;
1724 dst_write_mask = shader_glsl_append_dst(buffer, ins);
1725 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
1727 shader_glsl_add_src_param(ins, ins->src[0], ins->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1729 if (dst_size > 1) {
1730 shader_addline(buffer, "vec%d(log2(abs(%s))));\n", dst_size, src0_param.param_str);
1731 } else {
1732 shader_addline(buffer, "log2(abs(%s)));\n", src0_param.param_str);
1736 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
1737 static void shader_glsl_map2gl(const struct wined3d_shader_instruction *ins)
1739 SHADER_BUFFER *buffer = ins->buffer;
1740 glsl_src_param_t src_param;
1741 const char *instruction;
1742 DWORD write_mask;
1743 unsigned i;
1745 /* Determine the GLSL function to use based on the opcode */
1746 /* TODO: Possibly make this a table for faster lookups */
1747 switch (ins->handler_idx)
1749 case WINED3DSIH_MIN: instruction = "min"; break;
1750 case WINED3DSIH_MAX: instruction = "max"; break;
1751 case WINED3DSIH_ABS: instruction = "abs"; break;
1752 case WINED3DSIH_FRC: instruction = "fract"; break;
1753 case WINED3DSIH_NRM: instruction = "normalize"; break;
1754 case WINED3DSIH_EXP: instruction = "exp2"; break;
1755 case WINED3DSIH_SGN: instruction = "sign"; break;
1756 case WINED3DSIH_DSX: instruction = "dFdx"; break;
1757 case WINED3DSIH_DSY: instruction = "ycorrection.y * dFdy"; break;
1758 default: instruction = "";
1759 FIXME("Opcode %#x not yet handled in GLSL\n", ins->handler_idx);
1760 break;
1763 write_mask = shader_glsl_append_dst(buffer, ins);
1765 shader_addline(buffer, "%s(", instruction);
1767 if (ins->src_count)
1769 shader_glsl_add_src_param(ins, ins->src[0], ins->src_addr[0], write_mask, &src_param);
1770 shader_addline(buffer, "%s", src_param.param_str);
1771 for (i = 1; i < ins->src_count; ++i)
1773 shader_glsl_add_src_param(ins, ins->src[i], ins->src_addr[i], write_mask, &src_param);
1774 shader_addline(buffer, ", %s", src_param.param_str);
1778 shader_addline(buffer, "));\n");
1781 /** Process the WINED3DSIO_EXPP instruction in GLSL:
1782 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
1783 * dst.x = 2^(floor(src))
1784 * dst.y = src - floor(src)
1785 * dst.z = 2^src (partial precision is allowed, but optional)
1786 * dst.w = 1.0;
1787 * For 2.0 shaders, just do this (honoring writemask and swizzle):
1788 * dst = 2^src; (partial precision is allowed, but optional)
1790 static void shader_glsl_expp(const struct wined3d_shader_instruction *ins)
1792 glsl_src_param_t src_param;
1794 shader_glsl_add_src_param(ins, ins->src[0], ins->src_addr[0], WINED3DSP_WRITEMASK_0, &src_param);
1796 if (ins->reg_maps->shader_version < WINED3DPS_VERSION(2,0))
1798 char dst_mask[6];
1800 shader_addline(ins->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
1801 shader_addline(ins->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
1802 shader_addline(ins->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
1803 shader_addline(ins->buffer, "tmp0.w = 1.0;\n");
1805 shader_glsl_append_dst(ins->buffer, ins);
1806 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
1807 shader_addline(ins->buffer, "tmp0%s);\n", dst_mask);
1808 } else {
1809 DWORD write_mask;
1810 unsigned int mask_size;
1812 write_mask = shader_glsl_append_dst(ins->buffer, ins);
1813 mask_size = shader_glsl_get_write_mask_size(write_mask);
1815 if (mask_size > 1) {
1816 shader_addline(ins->buffer, "vec%d(exp2(%s)));\n", mask_size, src_param.param_str);
1817 } else {
1818 shader_addline(ins->buffer, "exp2(%s));\n", src_param.param_str);
1823 /** Process the RCP (reciprocal or inverse) opcode in GLSL (dst = 1 / src) */
1824 static void shader_glsl_rcp(const struct wined3d_shader_instruction *ins)
1826 glsl_src_param_t src_param;
1827 DWORD write_mask;
1828 unsigned int mask_size;
1830 write_mask = shader_glsl_append_dst(ins->buffer, ins);
1831 mask_size = shader_glsl_get_write_mask_size(write_mask);
1832 shader_glsl_add_src_param(ins, ins->src[0], ins->src_addr[0], WINED3DSP_WRITEMASK_3, &src_param);
1834 if (mask_size > 1) {
1835 shader_addline(ins->buffer, "vec%d(1.0 / %s));\n", mask_size, src_param.param_str);
1836 } else {
1837 shader_addline(ins->buffer, "1.0 / %s);\n", src_param.param_str);
1841 static void shader_glsl_rsq(const struct wined3d_shader_instruction *ins)
1843 SHADER_BUFFER *buffer = ins->buffer;
1844 glsl_src_param_t src_param;
1845 DWORD write_mask;
1846 unsigned int mask_size;
1848 write_mask = shader_glsl_append_dst(buffer, ins);
1849 mask_size = shader_glsl_get_write_mask_size(write_mask);
1851 shader_glsl_add_src_param(ins, ins->src[0], ins->src_addr[0], WINED3DSP_WRITEMASK_3, &src_param);
1853 if (mask_size > 1) {
1854 shader_addline(buffer, "vec%d(inversesqrt(%s)));\n", mask_size, src_param.param_str);
1855 } else {
1856 shader_addline(buffer, "inversesqrt(%s));\n", src_param.param_str);
1860 /** Process signed comparison opcodes in GLSL. */
1861 static void shader_glsl_compare(const struct wined3d_shader_instruction *ins)
1863 glsl_src_param_t src0_param;
1864 glsl_src_param_t src1_param;
1865 DWORD write_mask;
1866 unsigned int mask_size;
1868 write_mask = shader_glsl_append_dst(ins->buffer, ins);
1869 mask_size = shader_glsl_get_write_mask_size(write_mask);
1870 shader_glsl_add_src_param(ins, ins->src[0], ins->src_addr[0], write_mask, &src0_param);
1871 shader_glsl_add_src_param(ins, ins->src[1], ins->src_addr[1], write_mask, &src1_param);
1873 if (mask_size > 1) {
1874 const char *compare;
1876 switch(ins->handler_idx)
1878 case WINED3DSIH_SLT: compare = "lessThan"; break;
1879 case WINED3DSIH_SGE: compare = "greaterThanEqual"; break;
1880 default: compare = "";
1881 FIXME("Can't handle opcode %#x\n", ins->handler_idx);
1884 shader_addline(ins->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
1885 src0_param.param_str, src1_param.param_str);
1886 } else {
1887 switch(ins->handler_idx)
1889 case WINED3DSIH_SLT:
1890 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
1891 * to return 0.0 but step returns 1.0 because step is not < x
1892 * An alternative is a bvec compare padded with an unused second component.
1893 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
1894 * issue. Playing with not() is not possible either because not() does not accept
1895 * a scalar.
1897 shader_addline(ins->buffer, "(%s < %s) ? 1.0 : 0.0);\n", src0_param.param_str, src1_param.param_str);
1898 break;
1899 case WINED3DSIH_SGE:
1900 /* Here we can use the step() function and safe a conditional */
1901 shader_addline(ins->buffer, "step(%s, %s));\n", src1_param.param_str, src0_param.param_str);
1902 break;
1903 default:
1904 FIXME("Can't handle opcode %#x\n", ins->handler_idx);
1910 /** Process CMP instruction in GLSL (dst = src0 >= 0.0 ? src1 : src2), per channel */
1911 static void shader_glsl_cmp(const struct wined3d_shader_instruction *ins)
1913 glsl_src_param_t src0_param;
1914 glsl_src_param_t src1_param;
1915 glsl_src_param_t src2_param;
1916 DWORD write_mask, cmp_channel = 0;
1917 unsigned int i, j;
1918 char mask_char[6];
1919 BOOL temp_destination = FALSE;
1921 if (shader_is_scalar(shader_get_regtype(ins->src[0]), ins->src[0] & WINED3DSP_REGNUM_MASK))
1923 write_mask = shader_glsl_append_dst(ins->buffer, ins);
1925 shader_glsl_add_src_param(ins, ins->src[0], ins->src_addr[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
1926 shader_glsl_add_src_param(ins, ins->src[1], ins->src_addr[1], write_mask, &src1_param);
1927 shader_glsl_add_src_param(ins, ins->src[2], ins->src_addr[2], write_mask, &src2_param);
1929 shader_addline(ins->buffer, "%s >= 0.0 ? %s : %s);\n",
1930 src0_param.param_str, src1_param.param_str, src2_param.param_str);
1931 } else {
1932 DWORD src0reg = ins->src[0] & WINED3DSP_REGNUM_MASK;
1933 DWORD src1reg = ins->src[1] & WINED3DSP_REGNUM_MASK;
1934 DWORD src2reg = ins->src[2] & WINED3DSP_REGNUM_MASK;
1935 DWORD src0regtype = shader_get_regtype(ins->src[0]);
1936 DWORD src1regtype = shader_get_regtype(ins->src[1]);
1937 DWORD src2regtype = shader_get_regtype(ins->src[2]);
1938 DWORD dstreg = ins->dst[0].register_idx;
1939 DWORD dstregtype = ins->dst[0].register_type;
1940 DWORD dst_mask = ins->dst[0].write_mask;
1941 struct wined3d_shader_dst_param dst = ins->dst[0];
1943 /* Cycle through all source0 channels */
1944 for (i=0; i<4; i++) {
1945 write_mask = 0;
1946 /* Find the destination channels which use the current source0 channel */
1947 for (j=0; j<4; j++) {
1948 if (((ins->src[0] >> (WINED3DSP_SWIZZLE_SHIFT + 2 * j)) & 0x3) == i)
1950 write_mask |= WINED3DSP_WRITEMASK_0 << j;
1951 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
1955 dst.write_mask = dst_mask & write_mask;
1956 dst.token = (dst.token & ~WINED3DSP_WRITEMASK_ALL) | dst.write_mask;
1958 /* Splitting the cmp instruction up in multiple lines imposes a problem:
1959 * The first lines may overwrite source parameters of the following lines.
1960 * Deal with that by using a temporary destination register if needed
1962 if ((src0reg == dstreg && src0regtype == dstregtype)
1963 || (src1reg == dstreg && src1regtype == dstregtype)
1964 || (src2reg == dstreg && src2regtype == dstregtype))
1966 write_mask = shader_glsl_get_write_mask(&dst, mask_char);
1967 if (!write_mask) continue;
1968 shader_addline(ins->buffer, "tmp0%s = (", mask_char);
1969 temp_destination = TRUE;
1970 } else {
1971 write_mask = shader_glsl_append_dst_ext(ins->buffer, ins, &dst);
1972 if (!write_mask) continue;
1975 shader_glsl_add_src_param(ins, ins->src[0], ins->src_addr[0], cmp_channel, &src0_param);
1976 shader_glsl_add_src_param(ins, ins->src[1], ins->src_addr[1], write_mask, &src1_param);
1977 shader_glsl_add_src_param(ins, ins->src[2], ins->src_addr[2], write_mask, &src2_param);
1979 shader_addline(ins->buffer, "%s >= 0.0 ? %s : %s);\n",
1980 src0_param.param_str, src1_param.param_str, src2_param.param_str);
1983 if(temp_destination) {
1984 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
1985 shader_glsl_append_dst(ins->buffer, ins);
1986 shader_addline(ins->buffer, "tmp0%s);\n", mask_char);
1992 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
1993 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
1994 * the compare is done per component of src0. */
1995 static void shader_glsl_cnd(const struct wined3d_shader_instruction *ins)
1997 struct wined3d_shader_dst_param dst;
1998 glsl_src_param_t src0_param;
1999 glsl_src_param_t src1_param;
2000 glsl_src_param_t src2_param;
2001 DWORD write_mask, cmp_channel = 0;
2002 unsigned int i, j;
2003 DWORD dst_mask;
2005 if (ins->reg_maps->shader_version < WINED3DPS_VERSION(1, 4))
2007 write_mask = shader_glsl_append_dst(ins->buffer, ins);
2008 shader_glsl_add_src_param(ins, ins->src[0], ins->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
2009 shader_glsl_add_src_param(ins, ins->src[1], ins->src_addr[1], write_mask, &src1_param);
2010 shader_glsl_add_src_param(ins, ins->src[2], ins->src_addr[2], write_mask, &src2_param);
2012 /* Fun: The D3DSI_COISSUE flag changes the semantic of the cnd instruction for < 1.4 shaders */
2013 if (ins->coissue)
2015 shader_addline(ins->buffer, "%s /* COISSUE! */);\n", src1_param.param_str);
2016 } else {
2017 shader_addline(ins->buffer, "%s > 0.5 ? %s : %s);\n",
2018 src0_param.param_str, src1_param.param_str, src2_param.param_str);
2020 return;
2022 /* Cycle through all source0 channels */
2023 dst_mask = ins->dst[0].write_mask;
2024 dst = ins->dst[0];
2025 for (i=0; i<4; i++) {
2026 write_mask = 0;
2027 /* Find the destination channels which use the current source0 channel */
2028 for (j=0; j<4; j++) {
2029 if (((ins->src[0] >> (WINED3DSP_SWIZZLE_SHIFT + 2 * j)) & 0x3) == i)
2031 write_mask |= WINED3DSP_WRITEMASK_0 << j;
2032 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
2036 dst.write_mask = dst_mask & write_mask;
2037 dst.token = (dst.token & ~WINED3DSP_WRITEMASK_ALL) | dst.write_mask;
2038 write_mask = shader_glsl_append_dst_ext(ins->buffer, ins, &dst);
2039 if (!write_mask) continue;
2041 shader_glsl_add_src_param(ins, ins->src[0], ins->src_addr[0], cmp_channel, &src0_param);
2042 shader_glsl_add_src_param(ins, ins->src[1], ins->src_addr[1], write_mask, &src1_param);
2043 shader_glsl_add_src_param(ins, ins->src[2], ins->src_addr[2], write_mask, &src2_param);
2045 shader_addline(ins->buffer, "%s > 0.5 ? %s : %s);\n",
2046 src0_param.param_str, src1_param.param_str, src2_param.param_str);
2050 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
2051 static void shader_glsl_mad(const struct wined3d_shader_instruction *ins)
2053 glsl_src_param_t src0_param;
2054 glsl_src_param_t src1_param;
2055 glsl_src_param_t src2_param;
2056 DWORD write_mask;
2058 write_mask = shader_glsl_append_dst(ins->buffer, ins);
2059 shader_glsl_add_src_param(ins, ins->src[0], ins->src_addr[0], write_mask, &src0_param);
2060 shader_glsl_add_src_param(ins, ins->src[1], ins->src_addr[1], write_mask, &src1_param);
2061 shader_glsl_add_src_param(ins, ins->src[2], ins->src_addr[2], write_mask, &src2_param);
2062 shader_addline(ins->buffer, "(%s * %s) + %s);\n",
2063 src0_param.param_str, src1_param.param_str, src2_param.param_str);
2066 /** Handles transforming all WINED3DSIO_M?x? opcodes for
2067 Vertex shaders to GLSL codes */
2068 static void shader_glsl_mnxn(const struct wined3d_shader_instruction *ins)
2070 int i;
2071 int nComponents = 0;
2072 struct wined3d_shader_dst_param tmp_dst = {0};
2073 struct wined3d_shader_instruction tmp_ins;
2075 memset(&tmp_ins, 0, sizeof(tmp_ins));
2077 /* Set constants for the temporary argument */
2078 tmp_ins.shader = ins->shader;
2079 tmp_ins.buffer = ins->buffer;
2080 tmp_ins.src[0] = ins->src[0];
2081 tmp_ins.src_addr[0] = ins->src_addr[0];
2082 tmp_ins.src_addr[1] = ins->src_addr[1];
2083 tmp_ins.reg_maps = ins->reg_maps;
2084 tmp_ins.dst_count = 1;
2085 tmp_ins.dst = &tmp_dst;
2086 tmp_ins.src_count = 2;
2088 switch(ins->handler_idx)
2090 case WINED3DSIH_M4x4:
2091 nComponents = 4;
2092 tmp_ins.handler_idx = WINED3DSIH_DP4;
2093 break;
2094 case WINED3DSIH_M4x3:
2095 nComponents = 3;
2096 tmp_ins.handler_idx = WINED3DSIH_DP4;
2097 break;
2098 case WINED3DSIH_M3x4:
2099 nComponents = 4;
2100 tmp_ins.handler_idx = WINED3DSIH_DP3;
2101 break;
2102 case WINED3DSIH_M3x3:
2103 nComponents = 3;
2104 tmp_ins.handler_idx = WINED3DSIH_DP3;
2105 break;
2106 case WINED3DSIH_M3x2:
2107 nComponents = 2;
2108 tmp_ins.handler_idx = WINED3DSIH_DP3;
2109 break;
2110 default:
2111 break;
2114 tmp_dst = ins->dst[0];
2115 for (i = 0; i < nComponents; ++i)
2117 tmp_dst.write_mask = WINED3DSP_WRITEMASK_0 << i;
2118 tmp_dst.token = (tmp_dst.token & ~WINED3DSP_WRITEMASK_ALL) | tmp_dst.write_mask;
2119 tmp_ins.src[1] = ins->src[1] + i;
2120 shader_glsl_dot(&tmp_ins);
2125 The LRP instruction performs a component-wise linear interpolation
2126 between the second and third operands using the first operand as the
2127 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
2128 This is equivalent to mix(src2, src1, src0);
2130 static void shader_glsl_lrp(const struct wined3d_shader_instruction *ins)
2132 glsl_src_param_t src0_param;
2133 glsl_src_param_t src1_param;
2134 glsl_src_param_t src2_param;
2135 DWORD write_mask;
2137 write_mask = shader_glsl_append_dst(ins->buffer, ins);
2139 shader_glsl_add_src_param(ins, ins->src[0], ins->src_addr[0], write_mask, &src0_param);
2140 shader_glsl_add_src_param(ins, ins->src[1], ins->src_addr[1], write_mask, &src1_param);
2141 shader_glsl_add_src_param(ins, ins->src[2], ins->src_addr[2], write_mask, &src2_param);
2143 shader_addline(ins->buffer, "mix(%s, %s, %s));\n",
2144 src2_param.param_str, src1_param.param_str, src0_param.param_str);
2147 /** Process the WINED3DSIO_LIT instruction in GLSL:
2148 * dst.x = dst.w = 1.0
2149 * dst.y = (src0.x > 0) ? src0.x
2150 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
2151 * where src.w is clamped at +- 128
2153 static void shader_glsl_lit(const struct wined3d_shader_instruction *ins)
2155 glsl_src_param_t src0_param;
2156 glsl_src_param_t src1_param;
2157 glsl_src_param_t src3_param;
2158 char dst_mask[6];
2160 shader_glsl_append_dst(ins->buffer, ins);
2161 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2163 shader_glsl_add_src_param(ins, ins->src[0], ins->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
2164 shader_glsl_add_src_param(ins, ins->src[0], ins->src_addr[0], WINED3DSP_WRITEMASK_1, &src1_param);
2165 shader_glsl_add_src_param(ins, ins->src[0], ins->src_addr[0], WINED3DSP_WRITEMASK_3, &src3_param);
2167 /* The sdk specifies the instruction like this
2168 * dst.x = 1.0;
2169 * if(src.x > 0.0) dst.y = src.x
2170 * else dst.y = 0.0.
2171 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
2172 * else dst.z = 0.0;
2173 * dst.w = 1.0;
2175 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
2176 * dst.x = 1.0 ... No further explanation needed
2177 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
2178 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
2179 * dst.w = 1.0. ... Nothing fancy.
2181 * So we still have one conditional in there. So do this:
2182 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
2184 * 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),
2185 * 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.
2186 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to
2188 shader_addline(ins->buffer,
2189 "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",
2190 src0_param.param_str, src1_param.param_str, src0_param.param_str, src3_param.param_str, dst_mask);
2193 /** Process the WINED3DSIO_DST instruction in GLSL:
2194 * dst.x = 1.0
2195 * dst.y = src0.x * src0.y
2196 * dst.z = src0.z
2197 * dst.w = src1.w
2199 static void shader_glsl_dst(const struct wined3d_shader_instruction *ins)
2201 glsl_src_param_t src0y_param;
2202 glsl_src_param_t src0z_param;
2203 glsl_src_param_t src1y_param;
2204 glsl_src_param_t src1w_param;
2205 char dst_mask[6];
2207 shader_glsl_append_dst(ins->buffer, ins);
2208 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2210 shader_glsl_add_src_param(ins, ins->src[0], ins->src_addr[0], WINED3DSP_WRITEMASK_1, &src0y_param);
2211 shader_glsl_add_src_param(ins, ins->src[0], ins->src_addr[0], WINED3DSP_WRITEMASK_2, &src0z_param);
2212 shader_glsl_add_src_param(ins, ins->src[1], ins->src_addr[1], WINED3DSP_WRITEMASK_1, &src1y_param);
2213 shader_glsl_add_src_param(ins, ins->src[1], ins->src_addr[1], WINED3DSP_WRITEMASK_3, &src1w_param);
2215 shader_addline(ins->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
2216 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
2219 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
2220 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
2221 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
2223 * dst.x = cos(src0.?)
2224 * dst.y = sin(src0.?)
2225 * dst.z = dst.z
2226 * dst.w = dst.w
2228 static void shader_glsl_sincos(const struct wined3d_shader_instruction *ins)
2230 glsl_src_param_t src0_param;
2231 DWORD write_mask;
2233 write_mask = shader_glsl_append_dst(ins->buffer, ins);
2234 shader_glsl_add_src_param(ins, ins->src[0], ins->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
2236 switch (write_mask) {
2237 case WINED3DSP_WRITEMASK_0:
2238 shader_addline(ins->buffer, "cos(%s));\n", src0_param.param_str);
2239 break;
2241 case WINED3DSP_WRITEMASK_1:
2242 shader_addline(ins->buffer, "sin(%s));\n", src0_param.param_str);
2243 break;
2245 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
2246 shader_addline(ins->buffer, "vec2(cos(%s), sin(%s)));\n", src0_param.param_str, src0_param.param_str);
2247 break;
2249 default:
2250 ERR("Write mask should be .x, .y or .xy\n");
2251 break;
2255 /** Process the WINED3DSIO_LOOP instruction in GLSL:
2256 * Start a for() loop where src1.y is the initial value of aL,
2257 * increment aL by src1.z for a total of src1.x iterations.
2258 * Need to use a temporary variable for this operation.
2260 /* FIXME: I don't think nested loops will work correctly this way. */
2261 static void shader_glsl_loop(const struct wined3d_shader_instruction *ins)
2263 glsl_src_param_t src1_param;
2264 IWineD3DBaseShaderImpl *shader = (IWineD3DBaseShaderImpl *)ins->shader;
2265 DWORD regtype = shader_get_regtype(ins->src[1]);
2266 DWORD reg = ins->src[1] & WINED3DSP_REGNUM_MASK;
2267 const DWORD *control_values = NULL;
2268 const local_constant *constant;
2270 shader_glsl_add_src_param(ins, ins->src[1], ins->src_addr[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(regtype == WINED3DSPR_CONSTINT) {
2278 LIST_FOR_EACH_ENTRY(constant, &shader->baseShader.constantsI, local_constant, entry) {
2279 if(constant->idx == reg) {
2280 control_values = constant->value;
2281 break;
2286 if(control_values) {
2287 if(control_values[2] > 0) {
2288 shader_addline(ins->buffer, "for (aL%u = %d; aL%u < (%d * %d + %d); aL%u += %d) {\n",
2289 shader->baseShader.cur_loop_depth, control_values[1],
2290 shader->baseShader.cur_loop_depth, control_values[0], control_values[2], control_values[1],
2291 shader->baseShader.cur_loop_depth, control_values[2]);
2292 } else if(control_values[2] == 0) {
2293 shader_addline(ins->buffer, "for (aL%u = %d, tmpInt%u = 0; tmpInt%u < %d; tmpInt%u++) {\n",
2294 shader->baseShader.cur_loop_depth, control_values[1], shader->baseShader.cur_loop_depth,
2295 shader->baseShader.cur_loop_depth, control_values[0],
2296 shader->baseShader.cur_loop_depth);
2297 } else {
2298 shader_addline(ins->buffer, "for (aL%u = %d; aL%u > (%d * %d + %d); aL%u += %d) {\n",
2299 shader->baseShader.cur_loop_depth, control_values[1],
2300 shader->baseShader.cur_loop_depth, control_values[0], control_values[2], control_values[1],
2301 shader->baseShader.cur_loop_depth, control_values[2]);
2303 } else {
2304 shader_addline(ins->buffer, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z) {\n",
2305 shader->baseShader.cur_loop_depth, shader->baseShader.cur_loop_regno,
2306 src1_param.reg_name, shader->baseShader.cur_loop_depth, src1_param.reg_name,
2307 shader->baseShader.cur_loop_depth, shader->baseShader.cur_loop_regno, src1_param.reg_name);
2310 shader->baseShader.cur_loop_depth++;
2311 shader->baseShader.cur_loop_regno++;
2314 static void shader_glsl_end(const struct wined3d_shader_instruction *ins)
2316 IWineD3DBaseShaderImpl *shader = (IWineD3DBaseShaderImpl *)ins->shader;
2318 shader_addline(ins->buffer, "}\n");
2320 if (ins->handler_idx == WINED3DSIH_ENDLOOP)
2322 shader->baseShader.cur_loop_depth--;
2323 shader->baseShader.cur_loop_regno--;
2326 if (ins->handler_idx == WINED3DSIH_ENDREP)
2328 shader->baseShader.cur_loop_depth--;
2332 static void shader_glsl_rep(const struct wined3d_shader_instruction *ins)
2334 IWineD3DBaseShaderImpl *shader = (IWineD3DBaseShaderImpl *)ins->shader;
2335 glsl_src_param_t src0_param;
2337 shader_glsl_add_src_param(ins, ins->src[0], ins->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
2338 shader_addline(ins->buffer, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
2339 shader->baseShader.cur_loop_depth, shader->baseShader.cur_loop_depth,
2340 src0_param.param_str, shader->baseShader.cur_loop_depth);
2341 shader->baseShader.cur_loop_depth++;
2344 static void shader_glsl_if(const struct wined3d_shader_instruction *ins)
2346 glsl_src_param_t src0_param;
2348 shader_glsl_add_src_param(ins, ins->src[0], ins->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
2349 shader_addline(ins->buffer, "if (%s) {\n", src0_param.param_str);
2352 static void shader_glsl_ifc(const struct wined3d_shader_instruction *ins)
2354 glsl_src_param_t src0_param;
2355 glsl_src_param_t src1_param;
2357 shader_glsl_add_src_param(ins, ins->src[0], ins->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
2358 shader_glsl_add_src_param(ins, ins->src[1], ins->src_addr[1], WINED3DSP_WRITEMASK_0, &src1_param);
2360 shader_addline(ins->buffer, "if (%s %s %s) {\n",
2361 src0_param.param_str, shader_get_comp_op(ins->flags), src1_param.param_str);
2364 static void shader_glsl_else(const struct wined3d_shader_instruction *ins)
2366 shader_addline(ins->buffer, "} else {\n");
2369 static void shader_glsl_break(const struct wined3d_shader_instruction *ins)
2371 shader_addline(ins->buffer, "break;\n");
2374 /* FIXME: According to MSDN the compare is done per component. */
2375 static void shader_glsl_breakc(const struct wined3d_shader_instruction *ins)
2377 glsl_src_param_t src0_param;
2378 glsl_src_param_t src1_param;
2380 shader_glsl_add_src_param(ins, ins->src[0], ins->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
2381 shader_glsl_add_src_param(ins, ins->src[1], ins->src_addr[1], WINED3DSP_WRITEMASK_0, &src1_param);
2383 shader_addline(ins->buffer, "if (%s %s %s) break;\n",
2384 src0_param.param_str, shader_get_comp_op(ins->flags), src1_param.param_str);
2387 static void shader_glsl_label(const struct wined3d_shader_instruction *ins)
2390 DWORD snum = (ins->src[0]) & WINED3DSP_REGNUM_MASK;
2391 shader_addline(ins->buffer, "}\n");
2392 shader_addline(ins->buffer, "void subroutine%u () {\n", snum);
2395 static void shader_glsl_call(const struct wined3d_shader_instruction *ins)
2397 DWORD snum = (ins->src[0]) & WINED3DSP_REGNUM_MASK;
2398 shader_addline(ins->buffer, "subroutine%u();\n", snum);
2401 static void shader_glsl_callnz(const struct wined3d_shader_instruction *ins)
2403 glsl_src_param_t src1_param;
2405 DWORD snum = (ins->src[0]) & WINED3DSP_REGNUM_MASK;
2406 shader_glsl_add_src_param(ins, ins->src[1], ins->src_addr[1], WINED3DSP_WRITEMASK_0, &src1_param);
2407 shader_addline(ins->buffer, "if (%s) subroutine%u();\n", src1_param.param_str, snum);
2410 /*********************************************
2411 * Pixel Shader Specific Code begins here
2412 ********************************************/
2413 static void pshader_glsl_tex(const struct wined3d_shader_instruction *ins)
2415 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)ins->shader;
2416 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
2417 DWORD shader_version = ins->reg_maps->shader_version;
2418 glsl_sample_function_t sample_function;
2419 DWORD sample_flags = 0;
2420 DWORD sampler_type;
2421 DWORD sampler_idx;
2422 DWORD mask = 0, swizzle;
2424 /* 1.0-1.4: Use destination register as sampler source.
2425 * 2.0+: Use provided sampler source. */
2426 if (shader_version < WINED3DPS_VERSION(2,0)) sampler_idx = ins->dst[0].register_idx;
2427 else sampler_idx = ins->src[1] & WINED3DSP_REGNUM_MASK;
2428 sampler_type = ins->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
2430 if (shader_version < WINED3DPS_VERSION(1,4))
2432 DWORD flags = deviceImpl->stateBlock->textureState[sampler_idx][WINED3DTSS_TEXTURETRANSFORMFLAGS];
2434 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
2435 if (flags & WINED3DTTFF_PROJECTED && sampler_type != WINED3DSTT_CUBE) {
2436 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
2437 switch (flags & ~WINED3DTTFF_PROJECTED) {
2438 case WINED3DTTFF_COUNT1: FIXME("WINED3DTTFF_PROJECTED with WINED3DTTFF_COUNT1?\n"); break;
2439 case WINED3DTTFF_COUNT2: mask = WINED3DSP_WRITEMASK_1; break;
2440 case WINED3DTTFF_COUNT3: mask = WINED3DSP_WRITEMASK_2; break;
2441 case WINED3DTTFF_COUNT4:
2442 case WINED3DTTFF_DISABLE: mask = WINED3DSP_WRITEMASK_3; break;
2446 else if (shader_version < WINED3DPS_VERSION(2,0))
2448 DWORD src_mod = ins->src[0] & WINED3DSP_SRCMOD_MASK;
2450 if (src_mod == WINED3DSPSM_DZ) {
2451 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
2452 mask = WINED3DSP_WRITEMASK_2;
2453 } else if (src_mod == WINED3DSPSM_DW) {
2454 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
2455 mask = WINED3DSP_WRITEMASK_3;
2457 } else {
2458 if (ins->flags & WINED3DSI_TEXLD_PROJECT)
2460 /* ps 2.0 texldp instruction always divides by the fourth component. */
2461 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
2462 mask = WINED3DSP_WRITEMASK_3;
2466 if(deviceImpl->stateBlock->textures[sampler_idx] &&
2467 IWineD3DBaseTexture_GetTextureDimensions(deviceImpl->stateBlock->textures[sampler_idx]) == GL_TEXTURE_RECTANGLE_ARB) {
2468 sample_flags |= WINED3D_GLSL_SAMPLE_RECT;
2471 shader_glsl_get_sample_function(sampler_type, sample_flags, &sample_function);
2472 mask |= sample_function.coord_mask;
2474 if (shader_version < WINED3DPS_VERSION(2,0)) swizzle = WINED3DVS_NOSWIZZLE;
2475 else swizzle = ins->src[1] & WINED3DSP_SWIZZLE_MASK;
2477 /* 1.0-1.3: Use destination register as coordinate source.
2478 1.4+: Use provided coordinate source register. */
2479 if (shader_version < WINED3DPS_VERSION(1,4))
2481 char coord_mask[6];
2482 shader_glsl_write_mask_to_str(mask, coord_mask);
2483 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL,
2484 "T%u%s", sampler_idx, coord_mask);
2485 } else {
2486 glsl_src_param_t coord_param;
2487 shader_glsl_add_src_param(ins, ins->src[0], ins->src_addr[0], mask, &coord_param);
2488 if (ins->flags & WINED3DSI_TEXLD_BIAS)
2490 glsl_src_param_t bias;
2491 shader_glsl_add_src_param(ins, ins->src[0], ins->src_addr[0], WINED3DSP_WRITEMASK_3, &bias);
2492 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, bias.param_str,
2493 "%s", coord_param.param_str);
2494 } else {
2495 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL,
2496 "%s", coord_param.param_str);
2501 static void shader_glsl_texldl(const struct wined3d_shader_instruction *ins)
2503 IWineD3DBaseShaderImpl *This = (IWineD3DBaseShaderImpl *)ins->shader;
2504 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
2505 glsl_sample_function_t sample_function;
2506 glsl_src_param_t coord_param, lod_param;
2507 DWORD sample_flags = WINED3D_GLSL_SAMPLE_LOD;
2508 DWORD sampler_type;
2509 DWORD sampler_idx;
2510 DWORD swizzle = ins->src[1] & WINED3DSP_SWIZZLE_MASK;
2512 sampler_idx = ins->src[1] & WINED3DSP_REGNUM_MASK;
2513 sampler_type = ins->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
2514 if(deviceImpl->stateBlock->textures[sampler_idx] &&
2515 IWineD3DBaseTexture_GetTextureDimensions(deviceImpl->stateBlock->textures[sampler_idx]) == GL_TEXTURE_RECTANGLE_ARB) {
2516 sample_flags |= WINED3D_GLSL_SAMPLE_RECT;
2518 shader_glsl_get_sample_function(sampler_type, sample_flags, &sample_function);
2519 shader_glsl_add_src_param(ins, ins->src[0], ins->src_addr[0], sample_function.coord_mask, &coord_param);
2521 shader_glsl_add_src_param(ins, ins->src[0], ins->src_addr[0], WINED3DSP_WRITEMASK_3, &lod_param);
2523 if (shader_is_pshader_version(ins->reg_maps->shader_version))
2525 /* The GLSL spec claims the Lod sampling functions are only supported in vertex shaders.
2526 * However, they seem to work just fine in fragment shaders as well. */
2527 WARN("Using %s in fragment shader.\n", sample_function.name);
2529 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, lod_param.param_str,
2530 "%s", coord_param.param_str);
2533 static void pshader_glsl_texcoord(const struct wined3d_shader_instruction *ins)
2535 /* FIXME: Make this work for more than just 2D textures */
2536 SHADER_BUFFER *buffer = ins->buffer;
2537 DWORD write_mask = shader_glsl_append_dst(ins->buffer, ins);
2539 if (ins->reg_maps->shader_version != WINED3DPS_VERSION(1,4))
2541 char dst_mask[6];
2543 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2544 shader_addline(buffer, "clamp(gl_TexCoord[%u], 0.0, 1.0)%s);\n",
2545 ins->dst[0].register_idx, dst_mask);
2546 } else {
2547 DWORD reg = ins->src[0] & WINED3DSP_REGNUM_MASK;
2548 DWORD src_mod = ins->src[0] & WINED3DSP_SRCMOD_MASK;
2549 char dst_swizzle[6];
2551 shader_glsl_get_swizzle(ins->src[0], FALSE, write_mask, dst_swizzle);
2553 if (src_mod == WINED3DSPSM_DZ) {
2554 glsl_src_param_t div_param;
2555 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
2556 shader_glsl_add_src_param(ins, ins->src[0], ins->src_addr[0], WINED3DSP_WRITEMASK_2, &div_param);
2558 if (mask_size > 1) {
2559 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
2560 } else {
2561 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
2563 } else if (src_mod == WINED3DSPSM_DW) {
2564 glsl_src_param_t div_param;
2565 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
2566 shader_glsl_add_src_param(ins, ins->src[0], ins->src_addr[0], WINED3DSP_WRITEMASK_3, &div_param);
2568 if (mask_size > 1) {
2569 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
2570 } else {
2571 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
2573 } else {
2574 shader_addline(buffer, "gl_TexCoord[%u]%s);\n", reg, dst_swizzle);
2579 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
2580 * Take a 3-component dot product of the TexCoord[dstreg] and src,
2581 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
2582 static void pshader_glsl_texdp3tex(const struct wined3d_shader_instruction *ins)
2584 glsl_src_param_t src0_param;
2585 glsl_sample_function_t sample_function;
2586 DWORD sampler_idx = ins->dst[0].register_idx;
2587 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2588 DWORD sampler_type = ins->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
2589 UINT mask_size;
2591 shader_glsl_add_src_param(ins, ins->src[0], ins->src_addr[0], src_mask, &src0_param);
2593 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
2594 * scalar, and projected sampling would require 4.
2596 * It is a dependent read - not valid with conditional NP2 textures
2598 shader_glsl_get_sample_function(sampler_type, 0, &sample_function);
2599 mask_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
2601 switch(mask_size)
2603 case 1:
2604 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DVS_NOSWIZZLE, NULL,
2605 "dot(gl_TexCoord[%u].xyz, %s)", sampler_idx, src0_param.param_str);
2606 break;
2608 case 2:
2609 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DVS_NOSWIZZLE, NULL,
2610 "vec2(dot(gl_TexCoord[%u].xyz, %s), 0.0)", sampler_idx, src0_param.param_str);
2611 break;
2613 case 3:
2614 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DVS_NOSWIZZLE, NULL,
2615 "vec3(dot(gl_TexCoord[%u].xyz, %s), 0.0, 0.0)", sampler_idx, src0_param.param_str);
2616 break;
2618 default:
2619 FIXME("Unexpected mask size %u\n", mask_size);
2620 break;
2624 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
2625 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
2626 static void pshader_glsl_texdp3(const struct wined3d_shader_instruction *ins)
2628 glsl_src_param_t src0_param;
2629 DWORD dstreg = ins->dst[0].register_idx;
2630 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2631 DWORD dst_mask;
2632 unsigned int mask_size;
2634 dst_mask = shader_glsl_append_dst(ins->buffer, ins);
2635 mask_size = shader_glsl_get_write_mask_size(dst_mask);
2636 shader_glsl_add_src_param(ins, ins->src[0], ins->src_addr[0], src_mask, &src0_param);
2638 if (mask_size > 1) {
2639 shader_addline(ins->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
2640 } else {
2641 shader_addline(ins->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
2645 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
2646 * Calculate the depth as dst.x / dst.y */
2647 static void pshader_glsl_texdepth(const struct wined3d_shader_instruction *ins)
2649 glsl_dst_param_t dst_param;
2651 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
2653 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
2654 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
2655 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
2656 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
2657 * >= 1.0 or < 0.0
2659 shader_addline(ins->buffer, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n",
2660 dst_param.reg_name, dst_param.reg_name);
2663 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
2664 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
2665 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
2666 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
2668 static void pshader_glsl_texm3x2depth(const struct wined3d_shader_instruction *ins)
2670 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2671 DWORD dstreg = ins->dst[0].register_idx;
2672 glsl_src_param_t src0_param;
2674 shader_glsl_add_src_param(ins, ins->src[0], ins->src_addr[0], src_mask, &src0_param);
2676 shader_addline(ins->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
2677 shader_addline(ins->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
2680 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
2681 * Calculate the 1st of a 2-row matrix multiplication. */
2682 static void pshader_glsl_texm3x2pad(const struct wined3d_shader_instruction *ins)
2684 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2685 DWORD reg = ins->dst[0].register_idx;
2686 SHADER_BUFFER *buffer = ins->buffer;
2687 glsl_src_param_t src0_param;
2689 shader_glsl_add_src_param(ins, ins->src[0], ins->src_addr[0], src_mask, &src0_param);
2690 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
2693 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
2694 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
2695 static void pshader_glsl_texm3x3pad(const struct wined3d_shader_instruction *ins)
2697 IWineD3DPixelShaderImpl *shader = (IWineD3DPixelShaderImpl *)ins->shader;
2698 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2699 DWORD reg = ins->dst[0].register_idx;
2700 SHADER_BUFFER *buffer = ins->buffer;
2701 SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
2702 glsl_src_param_t src0_param;
2704 shader_glsl_add_src_param(ins, ins->src[0], ins->src_addr[0], src_mask, &src0_param);
2705 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + current_state->current_row, reg, src0_param.param_str);
2706 current_state->texcoord_w[current_state->current_row++] = reg;
2709 static void pshader_glsl_texm3x2tex(const struct wined3d_shader_instruction *ins)
2711 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2712 DWORD reg = ins->dst[0].register_idx;
2713 SHADER_BUFFER *buffer = ins->buffer;
2714 glsl_src_param_t src0_param;
2715 DWORD sampler_type = ins->reg_maps->samplers[reg] & WINED3DSP_TEXTURETYPE_MASK;
2716 glsl_sample_function_t sample_function;
2718 shader_glsl_add_src_param(ins, ins->src[0], ins->src_addr[0], src_mask, &src0_param);
2719 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
2721 shader_glsl_get_sample_function(sampler_type, 0, &sample_function);
2723 /* Sample the texture using the calculated coordinates */
2724 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DVS_NOSWIZZLE, NULL, "tmp0.xy");
2727 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
2728 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
2729 static void pshader_glsl_texm3x3tex(const struct wined3d_shader_instruction *ins)
2731 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2732 glsl_src_param_t src0_param;
2733 DWORD reg = ins->dst[0].register_idx;
2734 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)ins->shader;
2735 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
2736 DWORD sampler_type = ins->reg_maps->samplers[reg] & WINED3DSP_TEXTURETYPE_MASK;
2737 glsl_sample_function_t sample_function;
2739 shader_glsl_add_src_param(ins, ins->src[0], ins->src_addr[0], src_mask, &src0_param);
2740 shader_addline(ins->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
2742 /* Dependent read, not valid with conditional NP2 */
2743 shader_glsl_get_sample_function(sampler_type, 0, &sample_function);
2745 /* Sample the texture using the calculated coordinates */
2746 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DVS_NOSWIZZLE, NULL, "tmp0.xyz");
2748 current_state->current_row = 0;
2751 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
2752 * Perform the 3rd row of a 3x3 matrix multiply */
2753 static void pshader_glsl_texm3x3(const struct wined3d_shader_instruction *ins)
2755 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2756 glsl_src_param_t src0_param;
2757 char dst_mask[6];
2758 DWORD reg = ins->dst[0].register_idx;
2759 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)ins->shader;
2760 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
2762 shader_glsl_add_src_param(ins, ins->src[0], ins->src_addr[0], src_mask, &src0_param);
2764 shader_glsl_append_dst(ins->buffer, ins);
2765 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2766 shader_addline(ins->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
2768 current_state->current_row = 0;
2771 /** Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
2772 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
2773 static void pshader_glsl_texm3x3spec(const struct wined3d_shader_instruction *ins)
2775 IWineD3DPixelShaderImpl *shader = (IWineD3DPixelShaderImpl *)ins->shader;
2776 DWORD reg = ins->dst[0].register_idx;
2777 glsl_src_param_t src0_param;
2778 glsl_src_param_t src1_param;
2779 SHADER_BUFFER *buffer = ins->buffer;
2780 SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
2781 DWORD stype = ins->reg_maps->samplers[reg] & WINED3DSP_TEXTURETYPE_MASK;
2782 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2783 glsl_sample_function_t sample_function;
2785 shader_glsl_add_src_param(ins, ins->src[0], ins->src_addr[0], src_mask, &src0_param);
2786 shader_glsl_add_src_param(ins, ins->src[1], ins->src_addr[1], src_mask, &src1_param);
2788 /* Perform the last matrix multiply operation */
2789 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
2790 /* Reflection calculation */
2791 shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
2793 /* Dependent read, not valid with conditional NP2 */
2794 shader_glsl_get_sample_function(stype, 0, &sample_function);
2796 /* Sample the texture */
2797 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DVS_NOSWIZZLE, NULL, "tmp0.xyz");
2799 current_state->current_row = 0;
2802 /** Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
2803 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
2804 static void pshader_glsl_texm3x3vspec(const struct wined3d_shader_instruction *ins)
2806 IWineD3DPixelShaderImpl *shader = (IWineD3DPixelShaderImpl *)ins->shader;
2807 DWORD reg = ins->dst[0].register_idx;
2808 SHADER_BUFFER *buffer = ins->buffer;
2809 SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
2810 glsl_src_param_t src0_param;
2811 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2812 DWORD sampler_type = ins->reg_maps->samplers[reg] & WINED3DSP_TEXTURETYPE_MASK;
2813 glsl_sample_function_t sample_function;
2815 shader_glsl_add_src_param(ins, ins->src[0], ins->src_addr[0], src_mask, &src0_param);
2817 /* Perform the last matrix multiply operation */
2818 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
2820 /* Construct the eye-ray vector from w coordinates */
2821 shader_addline(buffer, "tmp1.xyz = normalize(vec3(gl_TexCoord[%u].w, gl_TexCoord[%u].w, gl_TexCoord[%u].w));\n",
2822 current_state->texcoord_w[0], current_state->texcoord_w[1], reg);
2823 shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
2825 /* Dependent read, not valid with conditional NP2 */
2826 shader_glsl_get_sample_function(sampler_type, 0, &sample_function);
2828 /* Sample the texture using the calculated coordinates */
2829 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DVS_NOSWIZZLE, NULL, "tmp0.xyz");
2831 current_state->current_row = 0;
2834 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
2835 * Apply a fake bump map transform.
2836 * texbem is pshader <= 1.3 only, this saves a few version checks
2838 static void pshader_glsl_texbem(const struct wined3d_shader_instruction *ins)
2840 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)ins->shader;
2841 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
2842 glsl_sample_function_t sample_function;
2843 glsl_src_param_t coord_param;
2844 DWORD sampler_type;
2845 DWORD sampler_idx;
2846 DWORD mask;
2847 DWORD flags;
2848 char coord_mask[6];
2850 sampler_idx = ins->dst[0].register_idx;
2851 flags = deviceImpl->stateBlock->textureState[sampler_idx][WINED3DTSS_TEXTURETRANSFORMFLAGS];
2853 sampler_type = ins->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
2854 /* Dependent read, not valid with conditional NP2 */
2855 shader_glsl_get_sample_function(sampler_type, 0, &sample_function);
2856 mask = sample_function.coord_mask;
2858 shader_glsl_write_mask_to_str(mask, coord_mask);
2860 /* with projective textures, texbem only divides the static texture coord, not the displacement,
2861 * so we can't let the GL handle this.
2863 if (flags & WINED3DTTFF_PROJECTED) {
2864 DWORD div_mask=0;
2865 char coord_div_mask[3];
2866 switch (flags & ~WINED3DTTFF_PROJECTED) {
2867 case WINED3DTTFF_COUNT1: FIXME("WINED3DTTFF_PROJECTED with WINED3DTTFF_COUNT1?\n"); break;
2868 case WINED3DTTFF_COUNT2: div_mask = WINED3DSP_WRITEMASK_1; break;
2869 case WINED3DTTFF_COUNT3: div_mask = WINED3DSP_WRITEMASK_2; break;
2870 case WINED3DTTFF_COUNT4:
2871 case WINED3DTTFF_DISABLE: div_mask = WINED3DSP_WRITEMASK_3; break;
2873 shader_glsl_write_mask_to_str(div_mask, coord_div_mask);
2874 shader_addline(ins->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
2877 shader_glsl_add_src_param(ins, ins->src[0], ins->src_addr[0],
2878 WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &coord_param);
2880 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DVS_NOSWIZZLE, NULL,
2881 "T%u%s + vec4(bumpenvmat%d * %s, 0.0, 0.0)%s", sampler_idx, coord_mask, sampler_idx,
2882 coord_param.param_str, coord_mask);
2884 if (ins->handler_idx == WINED3DSIH_TEXBEML)
2886 glsl_src_param_t luminance_param;
2887 glsl_dst_param_t dst_param;
2889 shader_glsl_add_src_param(ins, ins->src[0], ins->src_addr[0], WINED3DSP_WRITEMASK_2, &luminance_param);
2890 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
2892 shader_addline(ins->buffer, "%s%s *= (%s * luminancescale%d + luminanceoffset%d);\n",
2893 dst_param.reg_name, dst_param.mask_str,
2894 luminance_param.param_str, sampler_idx, sampler_idx);
2898 static void pshader_glsl_bem(const struct wined3d_shader_instruction *ins)
2900 glsl_src_param_t src0_param, src1_param;
2901 DWORD sampler_idx = ins->dst[0].register_idx;
2903 shader_glsl_add_src_param(ins, ins->src[0], ins->src_addr[0],
2904 WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
2905 shader_glsl_add_src_param(ins, ins->src[1], ins->src_addr[1],
2906 WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
2908 shader_glsl_append_dst(ins->buffer, ins);
2909 shader_addline(ins->buffer, "%s + bumpenvmat%d * %s);\n",
2910 src0_param.param_str, sampler_idx, src1_param.param_str);
2913 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
2914 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
2915 static void pshader_glsl_texreg2ar(const struct wined3d_shader_instruction *ins)
2917 glsl_src_param_t src0_param;
2918 DWORD sampler_idx = ins->dst[0].register_idx;
2919 DWORD sampler_type = ins->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
2920 glsl_sample_function_t sample_function;
2922 shader_glsl_add_src_param(ins, ins->src[0], ins->src_addr[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
2924 shader_glsl_get_sample_function(sampler_type, 0, &sample_function);
2925 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DVS_NOSWIZZLE, NULL,
2926 "%s.wx", src0_param.reg_name);
2929 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
2930 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
2931 static void pshader_glsl_texreg2gb(const struct wined3d_shader_instruction *ins)
2933 glsl_src_param_t src0_param;
2934 DWORD sampler_idx = ins->dst[0].register_idx;
2935 DWORD sampler_type = ins->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
2936 glsl_sample_function_t sample_function;
2938 shader_glsl_add_src_param(ins, ins->src[0], ins->src_addr[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
2940 shader_glsl_get_sample_function(sampler_type, 0, &sample_function);
2941 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DVS_NOSWIZZLE, NULL,
2942 "%s.yz", src0_param.reg_name);
2945 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
2946 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
2947 static void pshader_glsl_texreg2rgb(const struct wined3d_shader_instruction *ins)
2949 glsl_src_param_t src0_param;
2950 DWORD sampler_idx = ins->dst[0].register_idx;
2951 DWORD sampler_type = ins->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
2952 glsl_sample_function_t sample_function;
2954 /* Dependent read, not valid with conditional NP2 */
2955 shader_glsl_get_sample_function(sampler_type, 0, &sample_function);
2956 shader_glsl_add_src_param(ins, ins->src[0], ins->src_addr[0], sample_function.coord_mask, &src0_param);
2958 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DVS_NOSWIZZLE, NULL,
2959 "%s", src0_param.param_str);
2962 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
2963 * If any of the first 3 components are < 0, discard this pixel */
2964 static void pshader_glsl_texkill(const struct wined3d_shader_instruction *ins)
2966 glsl_dst_param_t dst_param;
2968 /* The argument is a destination parameter, and no writemasks are allowed */
2969 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
2970 if ((ins->reg_maps->shader_version >= WINED3DPS_VERSION(2,0)))
2972 /* 2.0 shaders compare all 4 components in texkill */
2973 shader_addline(ins->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
2974 } else {
2975 /* 1.X shaders only compare the first 3 components, probably due to the nature of the texkill
2976 * instruction as a tex* instruction, and phase, which kills all a / w components. Even if all
2977 * 4 components are defined, only the first 3 are used
2979 shader_addline(ins->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
2983 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
2984 * dst = dot2(src0, src1) + src2 */
2985 static void pshader_glsl_dp2add(const struct wined3d_shader_instruction *ins)
2987 glsl_src_param_t src0_param;
2988 glsl_src_param_t src1_param;
2989 glsl_src_param_t src2_param;
2990 DWORD write_mask;
2991 unsigned int mask_size;
2993 write_mask = shader_glsl_append_dst(ins->buffer, ins);
2994 mask_size = shader_glsl_get_write_mask_size(write_mask);
2996 shader_glsl_add_src_param(ins, ins->src[0], ins->src_addr[0],
2997 WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
2998 shader_glsl_add_src_param(ins, ins->src[1], ins->src_addr[1],
2999 WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
3000 shader_glsl_add_src_param(ins, ins->src[2], ins->src_addr[2],
3001 WINED3DSP_WRITEMASK_0, &src2_param);
3003 if (mask_size > 1) {
3004 shader_addline(ins->buffer, "vec%d(dot(%s, %s) + %s));\n",
3005 mask_size, src0_param.param_str, src1_param.param_str, src2_param.param_str);
3006 } else {
3007 shader_addline(ins->buffer, "dot(%s, %s) + %s);\n",
3008 src0_param.param_str, src1_param.param_str, src2_param.param_str);
3012 static void pshader_glsl_input_pack(IWineD3DPixelShader *iface, SHADER_BUFFER *buffer,
3013 const struct wined3d_shader_semantic *semantics_in, const struct shader_reg_maps *reg_maps,
3014 enum vertexprocessing_mode vertexprocessing)
3016 unsigned int i;
3017 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)iface;
3019 for (i = 0; i < MAX_REG_INPUT; ++i)
3021 DWORD usage, usage_idx;
3022 char reg_mask[6];
3024 /* Unused */
3025 if (!reg_maps->packed_input[i]) continue;
3027 usage = semantics_in[i].usage;
3028 usage_idx = semantics_in[i].usage_idx;
3029 shader_glsl_get_write_mask(&semantics_in[i].reg, reg_mask);
3031 switch (usage)
3033 case WINED3DDECLUSAGE_TEXCOORD:
3034 if (usage_idx < 8 && vertexprocessing == pretransformed)
3035 shader_addline(buffer, "IN[%u]%s = gl_TexCoord[%u]%s;\n",
3036 This->input_reg_map[i], reg_mask, usage_idx, reg_mask);
3037 else
3038 shader_addline(buffer, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
3039 This->input_reg_map[i], reg_mask, reg_mask);
3040 break;
3042 case WINED3DDECLUSAGE_COLOR:
3043 if (usage_idx == 0)
3044 shader_addline(buffer, "IN[%u]%s = vec4(gl_Color)%s;\n",
3045 This->input_reg_map[i], reg_mask, reg_mask);
3046 else if (usage_idx == 1)
3047 shader_addline(buffer, "IN[%u]%s = vec4(gl_SecondaryColor)%s;\n",
3048 This->input_reg_map[i], reg_mask, reg_mask);
3049 else
3050 shader_addline(buffer, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
3051 This->input_reg_map[i], reg_mask, reg_mask);
3052 break;
3054 default:
3055 shader_addline(buffer, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
3056 This->input_reg_map[i], reg_mask, reg_mask);
3057 break;
3062 /*********************************************
3063 * Vertex Shader Specific Code begins here
3064 ********************************************/
3066 static void add_glsl_program_entry(struct shader_glsl_priv *priv, struct glsl_shader_prog_link *entry) {
3067 glsl_program_key_t *key;
3069 key = HeapAlloc(GetProcessHeap(), 0, sizeof(glsl_program_key_t));
3070 key->vshader = entry->vshader;
3071 key->pshader = entry->pshader;
3072 key->vs_args = entry->vs_args;
3073 key->ps_args = entry->ps_args;
3075 hash_table_put(priv->glsl_program_lookup, key, entry);
3078 static struct glsl_shader_prog_link *get_glsl_program_entry(struct shader_glsl_priv *priv,
3079 IWineD3DVertexShader *vshader, IWineD3DPixelShader *pshader, struct vs_compile_args *vs_args,
3080 struct ps_compile_args *ps_args) {
3081 glsl_program_key_t key;
3083 key.vshader = vshader;
3084 key.pshader = pshader;
3085 key.vs_args = *vs_args;
3086 key.ps_args = *ps_args;
3088 return hash_table_get(priv->glsl_program_lookup, &key);
3091 static void delete_glsl_program_entry(struct shader_glsl_priv *priv, const WineD3D_GL_Info *gl_info,
3092 struct glsl_shader_prog_link *entry)
3094 glsl_program_key_t *key;
3096 key = HeapAlloc(GetProcessHeap(), 0, sizeof(glsl_program_key_t));
3097 key->vshader = entry->vshader;
3098 key->pshader = entry->pshader;
3099 key->vs_args = entry->vs_args;
3100 key->ps_args = entry->ps_args;
3101 hash_table_remove(priv->glsl_program_lookup, key);
3103 GL_EXTCALL(glDeleteObjectARB(entry->programId));
3104 if (entry->vshader) list_remove(&entry->vshader_entry);
3105 if (entry->pshader) list_remove(&entry->pshader_entry);
3106 HeapFree(GetProcessHeap(), 0, entry->vuniformF_locations);
3107 HeapFree(GetProcessHeap(), 0, entry->puniformF_locations);
3108 HeapFree(GetProcessHeap(), 0, entry);
3111 static void handle_ps3_input(SHADER_BUFFER *buffer, const WineD3D_GL_Info *gl_info, const DWORD *map,
3112 const struct wined3d_shader_semantic *semantics_in, const struct shader_reg_maps *reg_maps_in,
3113 const struct wined3d_shader_semantic *semantics_out, const struct shader_reg_maps *reg_maps_out)
3115 unsigned int i, j;
3116 DWORD usage, usage_idx, usage_out, usage_idx_out;
3117 DWORD *set;
3118 DWORD in_idx;
3119 DWORD in_count = GL_LIMITS(glsl_varyings) / 4;
3120 char reg_mask[6], reg_mask_out[6];
3121 char destination[50];
3123 set = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*set) * (in_count + 2));
3125 if (!semantics_out) {
3126 /* Save gl_FrontColor & gl_FrontSecondaryColor before overwriting them. */
3127 shader_addline(buffer, "vec4 front_color = gl_FrontColor;\n");
3128 shader_addline(buffer, "vec4 front_secondary_color = gl_FrontSecondaryColor;\n");
3131 for(i = 0; i < MAX_REG_INPUT; i++) {
3132 if (!reg_maps_in->packed_input[i]) continue;
3134 in_idx = map[i];
3135 if (in_idx >= (in_count + 2)) {
3136 FIXME("More input varyings declared than supported, expect issues\n");
3137 continue;
3139 else if (map[i] == ~0U)
3141 /* Declared, but not read register */
3142 continue;
3145 if (in_idx == in_count) {
3146 sprintf(destination, "gl_FrontColor");
3147 } else if (in_idx == in_count + 1) {
3148 sprintf(destination, "gl_FrontSecondaryColor");
3149 } else {
3150 sprintf(destination, "IN[%u]", in_idx);
3153 usage = semantics_in[i].usage;
3154 usage_idx = semantics_in[i].usage_idx;
3155 set[map[i]] = shader_glsl_get_write_mask(&semantics_in[i].reg, reg_mask);
3157 if(!semantics_out) {
3158 switch(usage) {
3159 case WINED3DDECLUSAGE_COLOR:
3160 if (usage_idx == 0)
3161 shader_addline(buffer, "%s%s = front_color%s;\n",
3162 destination, reg_mask, reg_mask);
3163 else if (usage_idx == 1)
3164 shader_addline(buffer, "%s%s = front_secondary_color%s;\n",
3165 destination, reg_mask, reg_mask);
3166 else
3167 shader_addline(buffer, "%s%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
3168 destination, reg_mask, reg_mask);
3169 break;
3171 case WINED3DDECLUSAGE_TEXCOORD:
3172 if (usage_idx < 8) {
3173 shader_addline(buffer, "%s%s = gl_TexCoord[%u]%s;\n",
3174 destination, reg_mask, usage_idx, reg_mask);
3175 } else {
3176 shader_addline(buffer, "%s%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
3177 destination, reg_mask, reg_mask);
3179 break;
3181 case WINED3DDECLUSAGE_FOG:
3182 shader_addline(buffer, "%s%s = vec4(gl_FogFragCoord, 0.0, 0.0, 0.0)%s;\n",
3183 destination, reg_mask, reg_mask);
3184 break;
3186 default:
3187 shader_addline(buffer, "%s%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
3188 destination, reg_mask, reg_mask);
3190 } else {
3191 BOOL found = FALSE;
3192 for(j = 0; j < MAX_REG_OUTPUT; j++) {
3193 if (!reg_maps_out->packed_output[j]) continue;
3195 usage_out = semantics_out[j].usage;
3196 usage_idx_out = semantics_out[j].usage_idx;
3197 shader_glsl_get_write_mask(&semantics_out[j].reg, reg_mask_out);
3199 if(usage == usage_out &&
3200 usage_idx == usage_idx_out) {
3201 shader_addline(buffer, "%s%s = OUT[%u]%s;\n",
3202 destination, reg_mask, j, reg_mask);
3203 found = TRUE;
3206 if(!found) {
3207 shader_addline(buffer, "%s%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
3208 destination, reg_mask, reg_mask);
3213 /* This is solely to make the compiler / linker happy and avoid warning about undefined
3214 * varyings. It shouldn't result in any real code executed on the GPU, since all read
3215 * input varyings are assigned above, if the optimizer works properly.
3217 for(i = 0; i < in_count + 2; i++) {
3218 if(set[i] != WINED3DSP_WRITEMASK_ALL) {
3219 unsigned int size = 0;
3220 memset(reg_mask, 0, sizeof(reg_mask));
3221 if(!(set[i] & WINED3DSP_WRITEMASK_0)) {
3222 reg_mask[size] = 'x';
3223 size++;
3225 if(!(set[i] & WINED3DSP_WRITEMASK_1)) {
3226 reg_mask[size] = 'y';
3227 size++;
3229 if(!(set[i] & WINED3DSP_WRITEMASK_2)) {
3230 reg_mask[size] = 'z';
3231 size++;
3233 if(!(set[i] & WINED3DSP_WRITEMASK_3)) {
3234 reg_mask[size] = 'w';
3235 size++;
3238 if (i == in_count) {
3239 sprintf(destination, "gl_FrontColor");
3240 } else if (i == in_count + 1) {
3241 sprintf(destination, "gl_FrontSecondaryColor");
3242 } else {
3243 sprintf(destination, "IN[%u]", i);
3246 if (size == 1) {
3247 shader_addline(buffer, "%s.%s = 0.0;\n", destination, reg_mask);
3248 } else {
3249 shader_addline(buffer, "%s.%s = vec%u(0.0);\n", destination, reg_mask, size);
3254 HeapFree(GetProcessHeap(), 0, set);
3257 static GLhandleARB generate_param_reorder_function(IWineD3DVertexShader *vertexshader,
3258 IWineD3DPixelShader *pixelshader, const WineD3D_GL_Info *gl_info)
3260 GLhandleARB ret = 0;
3261 IWineD3DVertexShaderImpl *vs = (IWineD3DVertexShaderImpl *) vertexshader;
3262 IWineD3DPixelShaderImpl *ps = (IWineD3DPixelShaderImpl *) pixelshader;
3263 IWineD3DDeviceImpl *device;
3264 DWORD vs_major = WINED3DSHADER_VERSION_MAJOR(vs->baseShader.reg_maps.shader_version);
3265 DWORD ps_major = ps ? WINED3DSHADER_VERSION_MAJOR(ps->baseShader.reg_maps.shader_version) : 0;
3266 unsigned int i;
3267 SHADER_BUFFER buffer;
3268 DWORD usage, usage_idx, writemask;
3269 char reg_mask[6];
3270 const struct wined3d_shader_semantic *semantics_out;
3272 shader_buffer_init(&buffer);
3274 shader_addline(&buffer, "#version 120\n");
3276 if(vs_major < 3 && ps_major < 3) {
3277 /* That one is easy: The vertex shader writes to the builtin varyings, the pixel shader reads from them.
3278 * Take care about the texcoord .w fixup though if we're using the fixed function fragment pipeline
3280 device = (IWineD3DDeviceImpl *) vs->baseShader.device;
3281 if((GLINFO_LOCATION).set_texcoord_w && ps_major == 0 && vs_major > 0 &&
3282 !device->frag_pipe->ffp_proj_control) {
3283 shader_addline(&buffer, "void order_ps_input() {\n");
3284 for(i = 0; i < min(8, MAX_REG_TEXCRD); i++) {
3285 if(vs->baseShader.reg_maps.texcoord_mask[i] != 0 &&
3286 vs->baseShader.reg_maps.texcoord_mask[i] != WINED3DSP_WRITEMASK_ALL) {
3287 shader_addline(&buffer, "gl_TexCoord[%u].w = 1.0;\n", i);
3290 shader_addline(&buffer, "}\n");
3291 } else {
3292 shader_addline(&buffer, "void order_ps_input() { /* do nothing */ }\n");
3294 } else if(ps_major < 3 && vs_major >= 3) {
3295 /* The vertex shader writes to its own varyings, the pixel shader needs them in the builtin ones */
3296 semantics_out = vs->semantics_out;
3298 shader_addline(&buffer, "void order_ps_input(in vec4 OUT[%u]) {\n", MAX_REG_OUTPUT);
3299 for(i = 0; i < MAX_REG_OUTPUT; i++) {
3300 if (!vs->baseShader.reg_maps.packed_output[i]) continue;
3302 usage = semantics_out[i].usage;
3303 usage_idx = semantics_out[i].usage_idx;
3304 writemask = shader_glsl_get_write_mask(&semantics_out[i].reg, reg_mask);
3306 switch(usage) {
3307 case WINED3DDECLUSAGE_COLOR:
3308 if (usage_idx == 0)
3309 shader_addline(&buffer, "gl_FrontColor%s = OUT[%u]%s;\n", reg_mask, i, reg_mask);
3310 else if (usage_idx == 1)
3311 shader_addline(&buffer, "gl_FrontSecondaryColor%s = OUT[%u]%s;\n", reg_mask, i, reg_mask);
3312 break;
3314 case WINED3DDECLUSAGE_POSITION:
3315 shader_addline(&buffer, "gl_Position%s = OUT[%u]%s;\n", reg_mask, i, reg_mask);
3316 break;
3318 case WINED3DDECLUSAGE_TEXCOORD:
3319 if (usage_idx < 8) {
3320 if(!(GLINFO_LOCATION).set_texcoord_w || ps_major > 0) writemask |= WINED3DSP_WRITEMASK_3;
3322 shader_addline(&buffer, "gl_TexCoord[%u]%s = OUT[%u]%s;\n",
3323 usage_idx, reg_mask, i, reg_mask);
3324 if(!(writemask & WINED3DSP_WRITEMASK_3)) {
3325 shader_addline(&buffer, "gl_TexCoord[%u].w = 1.0;\n", usage_idx);
3328 break;
3330 case WINED3DDECLUSAGE_PSIZE:
3331 shader_addline(&buffer, "gl_PointSize = OUT[%u].x;\n", i);
3332 break;
3334 case WINED3DDECLUSAGE_FOG:
3335 shader_addline(&buffer, "gl_FogFragCoord = OUT[%u].%c;\n", i, reg_mask[1]);
3336 break;
3338 default:
3339 break;
3342 shader_addline(&buffer, "}\n");
3344 } else if(ps_major >= 3 && vs_major >= 3) {
3345 semantics_out = vs->semantics_out;
3347 /* This one is tricky: a 3.0 pixel shader reads from a 3.0 vertex shader */
3348 shader_addline(&buffer, "varying vec4 IN[%u];\n", GL_LIMITS(glsl_varyings) / 4);
3349 shader_addline(&buffer, "void order_ps_input(in vec4 OUT[%u]) {\n", MAX_REG_OUTPUT);
3351 /* First, sort out position and point size. Those are not passed to the pixel shader */
3352 for(i = 0; i < MAX_REG_OUTPUT; i++) {
3353 if (!vs->baseShader.reg_maps.packed_output[i]) continue;
3355 usage = semantics_out[i].usage;
3356 usage_idx = semantics_out[i].usage_idx;
3357 shader_glsl_get_write_mask(&semantics_out[i].reg, reg_mask);
3359 switch(usage) {
3360 case WINED3DDECLUSAGE_POSITION:
3361 shader_addline(&buffer, "gl_Position%s = OUT[%u]%s;\n", reg_mask, i, reg_mask);
3362 break;
3364 case WINED3DDECLUSAGE_PSIZE:
3365 shader_addline(&buffer, "gl_PointSize = OUT[%u].x;\n", i);
3366 break;
3368 default:
3369 break;
3373 /* Then, fix the pixel shader input */
3374 handle_ps3_input(&buffer, gl_info, ps->input_reg_map,
3375 ps->semantics_in, &ps->baseShader.reg_maps, semantics_out, &vs->baseShader.reg_maps);
3377 shader_addline(&buffer, "}\n");
3378 } else if(ps_major >= 3 && vs_major < 3) {
3379 shader_addline(&buffer, "varying vec4 IN[%u];\n", GL_LIMITS(glsl_varyings) / 4);
3380 shader_addline(&buffer, "void order_ps_input() {\n");
3381 /* The vertex shader wrote to the builtin varyings. There is no need to figure out position and
3382 * point size, but we depend on the optimizers kindness to find out that the pixel shader doesn't
3383 * read gl_TexCoord and gl_ColorX, otherwise we'll run out of varyings
3385 handle_ps3_input(&buffer, gl_info, ps->input_reg_map, ps->semantics_in, &ps->baseShader.reg_maps, NULL, NULL);
3386 shader_addline(&buffer, "}\n");
3387 } else {
3388 ERR("Unexpected vertex and pixel shader version condition: vs: %d, ps: %d\n", vs_major, ps_major);
3391 ret = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
3392 checkGLcall("glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB)");
3393 GL_EXTCALL(glShaderSourceARB(ret, 1, (const char**)&buffer.buffer, NULL));
3394 checkGLcall("glShaderSourceARB(ret, 1, &buffer.buffer, NULL)");
3395 GL_EXTCALL(glCompileShaderARB(ret));
3396 checkGLcall("glCompileShaderARB(ret)");
3398 shader_buffer_free(&buffer);
3399 return ret;
3402 static void hardcode_local_constants(IWineD3DBaseShaderImpl *shader, const WineD3D_GL_Info *gl_info,
3403 GLhandleARB programId, char prefix)
3405 const local_constant *lconst;
3406 GLint tmp_loc;
3407 const float *value;
3408 char glsl_name[8];
3410 LIST_FOR_EACH_ENTRY(lconst, &shader->baseShader.constantsF, local_constant, entry) {
3411 value = (const float *)lconst->value;
3412 snprintf(glsl_name, sizeof(glsl_name), "%cLC%u", prefix, lconst->idx);
3413 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
3414 GL_EXTCALL(glUniform4fvARB(tmp_loc, 1, value));
3416 checkGLcall("Hardcoding local constants\n");
3419 /** Sets the GLSL program ID for the given pixel and vertex shader combination.
3420 * It sets the programId on the current StateBlock (because it should be called
3421 * inside of the DrawPrimitive() part of the render loop).
3423 * If a program for the given combination does not exist, create one, and store
3424 * the program in the hash table. If it creates a program, it will link the
3425 * given objects, too.
3427 static void set_glsl_shader_program(IWineD3DDevice *iface, BOOL use_ps, BOOL use_vs) {
3428 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
3429 struct shader_glsl_priv *priv = This->shader_priv;
3430 const WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
3431 IWineD3DPixelShader *pshader = This->stateBlock->pixelShader;
3432 IWineD3DVertexShader *vshader = This->stateBlock->vertexShader;
3433 struct glsl_shader_prog_link *entry = NULL;
3434 GLhandleARB programId = 0;
3435 GLhandleARB reorder_shader_id = 0;
3436 unsigned int i;
3437 char glsl_name[8];
3438 GLhandleARB vshader_id, pshader_id;
3439 struct ps_compile_args ps_compile_args;
3440 struct vs_compile_args vs_compile_args;
3442 if(use_vs) {
3443 find_vs_compile_args((IWineD3DVertexShaderImpl*)This->stateBlock->vertexShader, This->stateBlock, &vs_compile_args);
3444 } else {
3445 /* FIXME: Do we really have to spend CPU cycles to generate a few zeroed bytes? */
3446 memset(&vs_compile_args, 0, sizeof(vs_compile_args));
3448 if(use_ps) {
3449 find_ps_compile_args((IWineD3DPixelShaderImpl*)This->stateBlock->pixelShader, This->stateBlock, &ps_compile_args);
3450 } else {
3451 /* FIXME: Do we really have to spend CPU cycles to generate a few zeroed bytes? */
3452 memset(&ps_compile_args, 0, sizeof(ps_compile_args));
3454 entry = get_glsl_program_entry(priv, vshader, pshader, &vs_compile_args, &ps_compile_args);
3455 if (entry) {
3456 priv->glsl_program = entry;
3457 return;
3460 /* If we get to this point, then no matching program exists, so we create one */
3461 programId = GL_EXTCALL(glCreateProgramObjectARB());
3462 TRACE("Created new GLSL shader program %u\n", programId);
3464 /* Create the entry */
3465 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link));
3466 entry->programId = programId;
3467 entry->vshader = vshader;
3468 entry->pshader = pshader;
3469 entry->vs_args = vs_compile_args;
3470 entry->ps_args = ps_compile_args;
3471 entry->constant_version = 0;
3472 /* Add the hash table entry */
3473 add_glsl_program_entry(priv, entry);
3475 /* Set the current program */
3476 priv->glsl_program = entry;
3478 if(use_vs) {
3479 vshader_id = find_gl_vshader((IWineD3DVertexShaderImpl *) vshader, &vs_compile_args);
3480 } else {
3481 vshader_id = 0;
3484 /* Attach GLSL vshader */
3485 if (vshader_id) {
3486 const unsigned int max_attribs = 16; /* TODO: Will this always be the case? It is at the moment... */
3487 char tmp_name[10];
3489 reorder_shader_id = generate_param_reorder_function(vshader, pshader, gl_info);
3490 TRACE("Attaching GLSL shader object %u to program %u\n", reorder_shader_id, programId);
3491 GL_EXTCALL(glAttachObjectARB(programId, reorder_shader_id));
3492 checkGLcall("glAttachObjectARB");
3493 /* Flag the reorder function for deletion, then it will be freed automatically when the program
3494 * is destroyed
3496 GL_EXTCALL(glDeleteObjectARB(reorder_shader_id));
3498 TRACE("Attaching GLSL shader object %u to program %u\n", vshader_id, programId);
3499 GL_EXTCALL(glAttachObjectARB(programId, vshader_id));
3500 checkGLcall("glAttachObjectARB");
3502 /* Bind vertex attributes to a corresponding index number to match
3503 * the same index numbers as ARB_vertex_programs (makes loading
3504 * vertex attributes simpler). With this method, we can use the
3505 * exact same code to load the attributes later for both ARB and
3506 * GLSL shaders.
3508 * We have to do this here because we need to know the Program ID
3509 * in order to make the bindings work, and it has to be done prior
3510 * to linking the GLSL program. */
3511 for (i = 0; i < max_attribs; ++i) {
3512 if (((IWineD3DBaseShaderImpl*)vshader)->baseShader.reg_maps.attributes[i]) {
3513 snprintf(tmp_name, sizeof(tmp_name), "attrib%i", i);
3514 GL_EXTCALL(glBindAttribLocationARB(programId, i, tmp_name));
3517 checkGLcall("glBindAttribLocationARB");
3519 list_add_head(&((IWineD3DBaseShaderImpl *)vshader)->baseShader.linked_programs, &entry->vshader_entry);
3522 if(use_ps) {
3523 pshader_id = find_gl_pshader((IWineD3DPixelShaderImpl *) pshader, &ps_compile_args);
3524 } else {
3525 pshader_id = 0;
3528 /* Attach GLSL pshader */
3529 if (pshader_id) {
3530 TRACE("Attaching GLSL shader object %u to program %u\n", pshader_id, programId);
3531 GL_EXTCALL(glAttachObjectARB(programId, pshader_id));
3532 checkGLcall("glAttachObjectARB");
3534 list_add_head(&((IWineD3DBaseShaderImpl *)pshader)->baseShader.linked_programs, &entry->pshader_entry);
3537 /* Link the program */
3538 TRACE("Linking GLSL shader program %u\n", programId);
3539 GL_EXTCALL(glLinkProgramARB(programId));
3540 print_glsl_info_log(&GLINFO_LOCATION, programId);
3542 entry->vuniformF_locations = HeapAlloc(GetProcessHeap(), 0, sizeof(GLhandleARB) * GL_LIMITS(vshader_constantsF));
3543 for (i = 0; i < GL_LIMITS(vshader_constantsF); ++i) {
3544 snprintf(glsl_name, sizeof(glsl_name), "VC[%i]", i);
3545 entry->vuniformF_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
3547 for (i = 0; i < MAX_CONST_I; ++i) {
3548 snprintf(glsl_name, sizeof(glsl_name), "VI[%i]", i);
3549 entry->vuniformI_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
3551 entry->puniformF_locations = HeapAlloc(GetProcessHeap(), 0, sizeof(GLhandleARB) * GL_LIMITS(pshader_constantsF));
3552 for (i = 0; i < GL_LIMITS(pshader_constantsF); ++i) {
3553 snprintf(glsl_name, sizeof(glsl_name), "PC[%i]", i);
3554 entry->puniformF_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
3556 for (i = 0; i < MAX_CONST_I; ++i) {
3557 snprintf(glsl_name, sizeof(glsl_name), "PI[%i]", i);
3558 entry->puniformI_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
3561 if(pshader) {
3562 for(i = 0; i < ((IWineD3DPixelShaderImpl*)pshader)->numbumpenvmatconsts; i++) {
3563 char name[32];
3564 sprintf(name, "bumpenvmat%d", ((IWineD3DPixelShaderImpl*)pshader)->bumpenvmatconst[i].texunit);
3565 entry->bumpenvmat_location[i] = GL_EXTCALL(glGetUniformLocationARB(programId, name));
3566 sprintf(name, "luminancescale%d", ((IWineD3DPixelShaderImpl*)pshader)->luminanceconst[i].texunit);
3567 entry->luminancescale_location[i] = GL_EXTCALL(glGetUniformLocationARB(programId, name));
3568 sprintf(name, "luminanceoffset%d", ((IWineD3DPixelShaderImpl*)pshader)->luminanceconst[i].texunit);
3569 entry->luminanceoffset_location[i] = GL_EXTCALL(glGetUniformLocationARB(programId, name));
3573 if (use_ps && ps_compile_args.texrect_fixup) {
3574 char name[32];
3575 for (i = 0; i < MAX_FRAGMENT_SAMPLERS; ++i) {
3576 if (ps_compile_args.texrect_fixup & (1 << i)) {
3577 sprintf(name, "PsamplerRectFixup%u", i);
3578 entry->rectFixup_location[i] = GL_EXTCALL(glGetUniformLocationARB(programId, name));
3579 } else {
3580 entry->rectFixup_location[i] = -1;
3585 entry->posFixup_location = GL_EXTCALL(glGetUniformLocationARB(programId, "posFixup"));
3586 entry->ycorrection_location = GL_EXTCALL(glGetUniformLocationARB(programId, "ycorrection"));
3587 checkGLcall("Find glsl program uniform locations");
3589 if (pshader
3590 && WINED3DSHADER_VERSION_MAJOR(((IWineD3DPixelShaderImpl *)pshader)->baseShader.reg_maps.shader_version) >= 3
3591 && ((IWineD3DPixelShaderImpl *)pshader)->declared_in_count > GL_LIMITS(glsl_varyings) / 4)
3593 TRACE("Shader %d needs vertex color clamping disabled\n", programId);
3594 entry->vertex_color_clamp = GL_FALSE;
3595 } else {
3596 entry->vertex_color_clamp = GL_FIXED_ONLY_ARB;
3599 /* Set the shader to allow uniform loading on it */
3600 GL_EXTCALL(glUseProgramObjectARB(programId));
3601 checkGLcall("glUseProgramObjectARB(programId)");
3603 /* Load the vertex and pixel samplers now. The function that finds the mappings makes sure
3604 * that it stays the same for each vertexshader-pixelshader pair(=linked glsl program). If
3605 * a pshader with fixed function pipeline is used there are no vertex samplers, and if a
3606 * vertex shader with fixed function pixel processing is used we make sure that the card
3607 * supports enough samplers to allow the max number of vertex samplers with all possible
3608 * fixed function fragment processing setups. So once the program is linked these samplers
3609 * won't change.
3611 if(vshader_id) {
3612 /* Load vertex shader samplers */
3613 shader_glsl_load_vsamplers(gl_info, This->texUnitMap, programId);
3615 if(pshader_id) {
3616 /* Load pixel shader samplers */
3617 shader_glsl_load_psamplers(gl_info, This->texUnitMap, programId);
3620 /* If the local constants do not have to be loaded with the environment constants,
3621 * load them now to have them hardcoded in the GLSL program. This saves some CPU cycles
3622 * later
3624 if(pshader && !((IWineD3DPixelShaderImpl*)pshader)->baseShader.load_local_constsF) {
3625 hardcode_local_constants((IWineD3DBaseShaderImpl *) pshader, gl_info, programId, 'P');
3627 if(vshader && !((IWineD3DVertexShaderImpl*)vshader)->baseShader.load_local_constsF) {
3628 hardcode_local_constants((IWineD3DBaseShaderImpl *) vshader, gl_info, programId, 'V');
3632 static GLhandleARB create_glsl_blt_shader(const WineD3D_GL_Info *gl_info, enum tex_types tex_type)
3634 GLhandleARB program_id;
3635 GLhandleARB vshader_id, pshader_id;
3636 static const char *blt_vshader[] =
3638 "#version 120\n"
3639 "void main(void)\n"
3640 "{\n"
3641 " gl_Position = gl_Vertex;\n"
3642 " gl_FrontColor = vec4(1.0);\n"
3643 " gl_TexCoord[0] = gl_MultiTexCoord0;\n"
3644 "}\n"
3647 static const char *blt_pshaders[tex_type_count] =
3649 /* tex_1d */
3650 NULL,
3651 /* tex_2d */
3652 "#version 120\n"
3653 "uniform sampler2D sampler;\n"
3654 "void main(void)\n"
3655 "{\n"
3656 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
3657 "}\n",
3658 /* tex_3d */
3659 NULL,
3660 /* tex_cube */
3661 "#version 120\n"
3662 "uniform samplerCube sampler;\n"
3663 "void main(void)\n"
3664 "{\n"
3665 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
3666 "}\n",
3667 /* tex_rect */
3668 "#version 120\n"
3669 "#extension GL_ARB_texture_rectangle : enable\n"
3670 "uniform sampler2DRect sampler;\n"
3671 "void main(void)\n"
3672 "{\n"
3673 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
3674 "}\n",
3677 if (!blt_pshaders[tex_type])
3679 FIXME("tex_type %#x not supported\n", tex_type);
3680 tex_type = tex_2d;
3683 vshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
3684 GL_EXTCALL(glShaderSourceARB(vshader_id, 1, blt_vshader, NULL));
3685 GL_EXTCALL(glCompileShaderARB(vshader_id));
3687 pshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
3688 GL_EXTCALL(glShaderSourceARB(pshader_id, 1, &blt_pshaders[tex_type], NULL));
3689 GL_EXTCALL(glCompileShaderARB(pshader_id));
3691 program_id = GL_EXTCALL(glCreateProgramObjectARB());
3692 GL_EXTCALL(glAttachObjectARB(program_id, vshader_id));
3693 GL_EXTCALL(glAttachObjectARB(program_id, pshader_id));
3694 GL_EXTCALL(glLinkProgramARB(program_id));
3696 print_glsl_info_log(&GLINFO_LOCATION, program_id);
3698 /* Once linked we can mark the shaders for deletion. They will be deleted once the program
3699 * is destroyed
3701 GL_EXTCALL(glDeleteObjectARB(vshader_id));
3702 GL_EXTCALL(glDeleteObjectARB(pshader_id));
3703 return program_id;
3706 static void shader_glsl_select(IWineD3DDevice *iface, BOOL usePS, BOOL useVS) {
3707 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
3708 struct shader_glsl_priv *priv = This->shader_priv;
3709 const WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
3710 GLhandleARB program_id = 0;
3711 GLenum old_vertex_color_clamp, current_vertex_color_clamp;
3713 old_vertex_color_clamp = priv->glsl_program ? priv->glsl_program->vertex_color_clamp : GL_FIXED_ONLY_ARB;
3715 if (useVS || usePS) set_glsl_shader_program(iface, usePS, useVS);
3716 else priv->glsl_program = NULL;
3718 current_vertex_color_clamp = priv->glsl_program ? priv->glsl_program->vertex_color_clamp : GL_FIXED_ONLY_ARB;
3720 if (old_vertex_color_clamp != current_vertex_color_clamp) {
3721 if (GL_SUPPORT(ARB_COLOR_BUFFER_FLOAT)) {
3722 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, current_vertex_color_clamp));
3723 checkGLcall("glClampColorARB");
3724 } else {
3725 FIXME("vertex color clamp needs to be changed, but extension not supported.\n");
3729 program_id = priv->glsl_program ? priv->glsl_program->programId : 0;
3730 if (program_id) TRACE("Using GLSL program %u\n", program_id);
3731 GL_EXTCALL(glUseProgramObjectARB(program_id));
3732 checkGLcall("glUseProgramObjectARB");
3735 static void shader_glsl_select_depth_blt(IWineD3DDevice *iface, enum tex_types tex_type) {
3736 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
3737 const WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
3738 struct shader_glsl_priv *priv = This->shader_priv;
3739 GLhandleARB *blt_program = &priv->depth_blt_program[tex_type];
3741 if (!*blt_program) {
3742 GLint loc;
3743 *blt_program = create_glsl_blt_shader(gl_info, tex_type);
3744 loc = GL_EXTCALL(glGetUniformLocationARB(*blt_program, "sampler"));
3745 GL_EXTCALL(glUseProgramObjectARB(*blt_program));
3746 GL_EXTCALL(glUniform1iARB(loc, 0));
3747 } else {
3748 GL_EXTCALL(glUseProgramObjectARB(*blt_program));
3752 static void shader_glsl_deselect_depth_blt(IWineD3DDevice *iface) {
3753 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
3754 const WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
3755 struct shader_glsl_priv *priv = This->shader_priv;
3756 GLhandleARB program_id;
3758 program_id = priv->glsl_program ? priv->glsl_program->programId : 0;
3759 if (program_id) TRACE("Using GLSL program %u\n", program_id);
3761 GL_EXTCALL(glUseProgramObjectARB(program_id));
3762 checkGLcall("glUseProgramObjectARB");
3765 static void shader_glsl_destroy(IWineD3DBaseShader *iface) {
3766 const struct list *linked_programs;
3767 IWineD3DBaseShaderImpl *This = (IWineD3DBaseShaderImpl *) iface;
3768 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *)This->baseShader.device;
3769 struct shader_glsl_priv *priv = device->shader_priv;
3770 const WineD3D_GL_Info *gl_info = &device->adapter->gl_info;
3771 IWineD3DPixelShaderImpl *ps = NULL;
3772 IWineD3DVertexShaderImpl *vs = NULL;
3774 /* Note: Do not use QueryInterface here to find out which shader type this is because this code
3775 * can be called from IWineD3DBaseShader::Release
3777 char pshader = shader_is_pshader_version(This->baseShader.reg_maps.shader_version);
3779 if(pshader) {
3780 ps = (IWineD3DPixelShaderImpl *) This;
3781 if(ps->num_gl_shaders == 0) return;
3782 if (priv->glsl_program && (IWineD3DBaseShader *)priv->glsl_program->pshader == iface)
3783 shader_glsl_select(This->baseShader.device, FALSE, FALSE);
3784 } else {
3785 vs = (IWineD3DVertexShaderImpl *) This;
3786 if(vs->num_gl_shaders == 0) return;
3787 if (priv->glsl_program && (IWineD3DBaseShader *)priv->glsl_program->vshader == iface)
3788 shader_glsl_select(This->baseShader.device, FALSE, FALSE);
3791 linked_programs = &This->baseShader.linked_programs;
3793 TRACE("Deleting linked programs\n");
3794 if (linked_programs->next) {
3795 struct glsl_shader_prog_link *entry, *entry2;
3797 if(pshader) {
3798 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs, struct glsl_shader_prog_link, pshader_entry) {
3799 delete_glsl_program_entry(priv, gl_info, entry);
3801 } else {
3802 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs, struct glsl_shader_prog_link, vshader_entry) {
3803 delete_glsl_program_entry(priv, gl_info, entry);
3808 if(pshader) {
3809 UINT i;
3811 ENTER_GL();
3812 for(i = 0; i < ps->num_gl_shaders; i++) {
3813 TRACE("deleting pshader %u\n", ps->gl_shaders[i].prgId);
3814 GL_EXTCALL(glDeleteObjectARB(ps->gl_shaders[i].prgId));
3815 checkGLcall("glDeleteObjectARB");
3817 LEAVE_GL();
3818 HeapFree(GetProcessHeap(), 0, ps->gl_shaders);
3819 ps->gl_shaders = NULL;
3820 ps->num_gl_shaders = 0;
3821 ps->shader_array_size = 0;
3822 } else {
3823 UINT i;
3825 ENTER_GL();
3826 for(i = 0; i < vs->num_gl_shaders; i++) {
3827 TRACE("deleting vshader %u\n", vs->gl_shaders[i].prgId);
3828 GL_EXTCALL(glDeleteObjectARB(vs->gl_shaders[i].prgId));
3829 checkGLcall("glDeleteObjectARB");
3831 LEAVE_GL();
3832 HeapFree(GetProcessHeap(), 0, vs->gl_shaders);
3833 vs->gl_shaders = NULL;
3834 vs->num_gl_shaders = 0;
3835 vs->shader_array_size = 0;
3839 static unsigned int glsl_program_key_hash(const void *key)
3841 const glsl_program_key_t *k = key;
3843 unsigned int hash = ((DWORD_PTR) k->vshader) | ((DWORD_PTR) k->pshader) << 16;
3844 hash += ~(hash << 15);
3845 hash ^= (hash >> 10);
3846 hash += (hash << 3);
3847 hash ^= (hash >> 6);
3848 hash += ~(hash << 11);
3849 hash ^= (hash >> 16);
3851 return hash;
3854 static BOOL glsl_program_key_compare(const void *keya, const void *keyb)
3856 const glsl_program_key_t *ka = keya;
3857 const glsl_program_key_t *kb = keyb;
3859 return ka->vshader == kb->vshader && ka->pshader == kb->pshader &&
3860 (memcmp(&ka->ps_args, &kb->ps_args, sizeof(kb->ps_args)) == 0) &&
3861 (memcmp(&ka->vs_args, &kb->vs_args, sizeof(kb->vs_args)) == 0);
3864 static BOOL constant_heap_init(struct constant_heap *heap, unsigned int constant_count)
3866 SIZE_T size = (constant_count + 1) * sizeof(*heap->entries) + constant_count * sizeof(*heap->positions);
3867 void *mem = HeapAlloc(GetProcessHeap(), 0, size);
3869 if (!mem)
3871 ERR("Failed to allocate memory\n");
3872 return FALSE;
3875 heap->entries = mem;
3876 heap->entries[1].version = 0;
3877 heap->positions = (unsigned int *)(heap->entries + constant_count + 1);
3878 heap->size = 1;
3880 return TRUE;
3883 static void constant_heap_free(struct constant_heap *heap)
3885 HeapFree(GetProcessHeap(), 0, heap->entries);
3888 static HRESULT shader_glsl_alloc(IWineD3DDevice *iface) {
3889 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
3890 const WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
3891 struct shader_glsl_priv *priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct shader_glsl_priv));
3892 SIZE_T stack_size = wined3d_log2i(max(GL_LIMITS(vshader_constantsF), GL_LIMITS(pshader_constantsF))) + 1;
3894 priv->stack = HeapAlloc(GetProcessHeap(), 0, stack_size * sizeof(*priv->stack));
3895 if (!priv->stack)
3897 ERR("Failed to allocate memory.\n");
3898 HeapFree(GetProcessHeap(), 0, priv);
3899 return E_OUTOFMEMORY;
3902 if (!constant_heap_init(&priv->vconst_heap, GL_LIMITS(vshader_constantsF)))
3904 ERR("Failed to initialize vertex shader constant heap\n");
3905 HeapFree(GetProcessHeap(), 0, priv->stack);
3906 HeapFree(GetProcessHeap(), 0, priv);
3907 return E_OUTOFMEMORY;
3910 if (!constant_heap_init(&priv->pconst_heap, GL_LIMITS(pshader_constantsF)))
3912 ERR("Failed to initialize pixel shader constant heap\n");
3913 constant_heap_free(&priv->vconst_heap);
3914 HeapFree(GetProcessHeap(), 0, priv->stack);
3915 HeapFree(GetProcessHeap(), 0, priv);
3916 return E_OUTOFMEMORY;
3919 priv->glsl_program_lookup = hash_table_create(glsl_program_key_hash, glsl_program_key_compare);
3920 priv->next_constant_version = 1;
3922 This->shader_priv = priv;
3923 return WINED3D_OK;
3926 static void shader_glsl_free(IWineD3DDevice *iface) {
3927 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
3928 const WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
3929 struct shader_glsl_priv *priv = This->shader_priv;
3930 int i;
3932 for (i = 0; i < tex_type_count; ++i)
3934 if (priv->depth_blt_program[i])
3936 GL_EXTCALL(glDeleteObjectARB(priv->depth_blt_program[i]));
3940 hash_table_destroy(priv->glsl_program_lookup, NULL, NULL);
3941 constant_heap_free(&priv->pconst_heap);
3942 constant_heap_free(&priv->vconst_heap);
3944 HeapFree(GetProcessHeap(), 0, This->shader_priv);
3945 This->shader_priv = NULL;
3948 static BOOL shader_glsl_dirty_const(IWineD3DDevice *iface) {
3949 /* TODO: GL_EXT_bindable_uniform can be used to share constants across shaders */
3950 return FALSE;
3953 static GLuint shader_glsl_generate_pshader(IWineD3DPixelShader *iface, SHADER_BUFFER *buffer, const struct ps_compile_args *args) {
3954 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)iface;
3955 const struct shader_reg_maps *reg_maps = &This->baseShader.reg_maps;
3956 CONST DWORD *function = This->baseShader.function;
3957 const char *fragcolor;
3958 const WineD3D_GL_Info *gl_info = &((IWineD3DDeviceImpl *)This->baseShader.device)->adapter->gl_info;
3960 /* Create the hw GLSL shader object and assign it as the shader->prgId */
3961 GLhandleARB shader_obj = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
3963 shader_addline(buffer, "#version 120\n");
3965 if (GL_SUPPORT(ARB_DRAW_BUFFERS)) {
3966 shader_addline(buffer, "#extension GL_ARB_draw_buffers : enable\n");
3968 if (GL_SUPPORT(ARB_TEXTURE_RECTANGLE)) {
3969 /* The spec says that it doesn't have to be explicitly enabled, but the nvidia
3970 * drivers write a warning if we don't do so
3972 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
3975 /* Base Declarations */
3976 shader_generate_glsl_declarations( (IWineD3DBaseShader*) This, reg_maps, buffer, &GLINFO_LOCATION, args);
3978 /* Pack 3.0 inputs */
3979 if (reg_maps->shader_version >= WINED3DPS_VERSION(3,0) && args->vp_mode != vertexshader) {
3980 pshader_glsl_input_pack(iface, buffer, This->semantics_in, reg_maps, args->vp_mode);
3983 /* Base Shader Body */
3984 shader_generate_main( (IWineD3DBaseShader*) This, buffer, reg_maps, function);
3986 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly */
3987 if (reg_maps->shader_version < WINED3DPS_VERSION(2,0))
3989 /* Some older cards like GeforceFX ones don't support multiple buffers, so also not gl_FragData */
3990 if(GL_SUPPORT(ARB_DRAW_BUFFERS))
3991 shader_addline(buffer, "gl_FragData[0] = R0;\n");
3992 else
3993 shader_addline(buffer, "gl_FragColor = R0;\n");
3996 if(GL_SUPPORT(ARB_DRAW_BUFFERS)) {
3997 fragcolor = "gl_FragData[0]";
3998 } else {
3999 fragcolor = "gl_FragColor";
4001 if(args->srgb_correction) {
4002 shader_addline(buffer, "tmp0.xyz = pow(%s.xyz, vec3(%f, %f, %f)) * vec3(%f, %f, %f) - vec3(%f, %f, %f);\n",
4003 fragcolor, srgb_pow, srgb_pow, srgb_pow, srgb_mul_high, srgb_mul_high, srgb_mul_high,
4004 srgb_sub_high, srgb_sub_high, srgb_sub_high);
4005 shader_addline(buffer, "tmp1.xyz = %s.xyz * srgb_mul_low.xyz;\n", fragcolor);
4006 shader_addline(buffer, "%s.x = %s.x < srgb_comparison.x ? tmp1.x : tmp0.x;\n", fragcolor, fragcolor);
4007 shader_addline(buffer, "%s.y = %s.y < srgb_comparison.y ? tmp1.y : tmp0.y;\n", fragcolor, fragcolor);
4008 shader_addline(buffer, "%s.z = %s.z < srgb_comparison.z ? tmp1.z : tmp0.z;\n", fragcolor, fragcolor);
4009 shader_addline(buffer, "%s = clamp(%s, 0.0, 1.0);\n", fragcolor, fragcolor);
4011 /* Pixel shader < 3.0 do not replace the fog stage.
4012 * This implements linear fog computation and blending.
4013 * TODO: non linear fog
4014 * NOTE: gl_Fog.start and gl_Fog.end don't hold fog start s and end e but
4015 * -1/(e-s) and e/(e-s) respectively.
4017 if(reg_maps->shader_version < WINED3DPS_VERSION(3,0)) {
4018 switch(args->fog) {
4019 case FOG_OFF: break;
4020 case FOG_LINEAR:
4021 shader_addline(buffer, "float fogstart = -1.0 / (gl_Fog.end - gl_Fog.start);\n");
4022 shader_addline(buffer, "float fogend = gl_Fog.end * -fogstart;\n");
4023 shader_addline(buffer, "float Fog = clamp(gl_FogFragCoord * fogstart + fogend, 0.0, 1.0);\n");
4024 shader_addline(buffer, "%s.xyz = mix(gl_Fog.color.xyz, %s.xyz, Fog);\n", fragcolor, fragcolor);
4025 break;
4026 case FOG_EXP:
4027 /* Fog = e^(-gl_Fog.density * gl_FogFragCoord) */
4028 shader_addline(buffer, "float Fog = exp(-gl_Fog.density * gl_FogFragCoord);\n");
4029 shader_addline(buffer, "Fog = clamp(Fog, 0.0, 1.0);\n");
4030 shader_addline(buffer, "%s.xyz = mix(gl_Fog.color.xyz, %s.xyz, Fog);\n", fragcolor, fragcolor);
4031 break;
4032 case FOG_EXP2:
4033 /* Fog = e^(-(gl_Fog.density * gl_FogFragCoord)^2) */
4034 shader_addline(buffer, "float Fog = exp(-gl_Fog.density * gl_Fog.density * gl_FogFragCoord * gl_FogFragCoord);\n");
4035 shader_addline(buffer, "Fog = clamp(Fog, 0.0, 1.0);\n");
4036 shader_addline(buffer, "%s.xyz = mix(gl_Fog.color.xyz, %s.xyz, Fog);\n", fragcolor, fragcolor);
4037 break;
4041 shader_addline(buffer, "}\n");
4043 TRACE("Compiling shader object %u\n", shader_obj);
4044 GL_EXTCALL(glShaderSourceARB(shader_obj, 1, (const char**)&buffer->buffer, NULL));
4045 GL_EXTCALL(glCompileShaderARB(shader_obj));
4046 print_glsl_info_log(&GLINFO_LOCATION, shader_obj);
4048 /* Store the shader object */
4049 return shader_obj;
4052 static GLuint shader_glsl_generate_vshader(IWineD3DVertexShader *iface, SHADER_BUFFER *buffer, const struct vs_compile_args *args) {
4053 IWineD3DVertexShaderImpl *This = (IWineD3DVertexShaderImpl *)iface;
4054 const struct shader_reg_maps *reg_maps = &This->baseShader.reg_maps;
4055 CONST DWORD *function = This->baseShader.function;
4056 const WineD3D_GL_Info *gl_info = &((IWineD3DDeviceImpl *)This->baseShader.device)->adapter->gl_info;
4058 /* Create the hw GLSL shader program and assign it as the shader->prgId */
4059 GLhandleARB shader_obj = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
4061 shader_addline(buffer, "#version 120\n");
4063 /* Base Declarations */
4064 shader_generate_glsl_declarations( (IWineD3DBaseShader*) This, reg_maps, buffer, &GLINFO_LOCATION, NULL);
4066 /* Base Shader Body */
4067 shader_generate_main( (IWineD3DBaseShader*) This, buffer, reg_maps, function);
4069 /* Unpack 3.0 outputs */
4070 if (reg_maps->shader_version >= WINED3DVS_VERSION(3,0)) shader_addline(buffer, "order_ps_input(OUT);\n");
4071 else shader_addline(buffer, "order_ps_input();\n");
4073 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
4074 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
4075 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
4076 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0)
4078 if(args->fog_src == VS_FOG_Z) {
4079 shader_addline(buffer, "gl_FogFragCoord = gl_Position.z;\n");
4080 } else if (!reg_maps->fog) {
4081 shader_addline(buffer, "gl_FogFragCoord = 0.0;\n");
4084 /* Write the final position.
4086 * OpenGL coordinates specify the center of the pixel while d3d coords specify
4087 * the corner. The offsets are stored in z and w in posFixup. posFixup.y contains
4088 * 1.0 or -1.0 to turn the rendering upside down for offscreen rendering. PosFixup.x
4089 * contains 1.0 to allow a mad.
4091 shader_addline(buffer, "gl_Position.y = gl_Position.y * posFixup.y;\n");
4092 shader_addline(buffer, "gl_Position.xy += posFixup.zw * gl_Position.ww;\n");
4094 /* Z coord [0;1]->[-1;1] mapping, see comment in transform_projection in state.c
4096 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However, shaders are run
4097 * before the homogeneous divide, so we have to take the w into account: z = ((z / w) * 2 - 1) * w,
4098 * which is the same as z = z * 2 - w.
4100 shader_addline(buffer, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
4102 shader_addline(buffer, "}\n");
4104 TRACE("Compiling shader object %u\n", shader_obj);
4105 GL_EXTCALL(glShaderSourceARB(shader_obj, 1, (const char**)&buffer->buffer, NULL));
4106 GL_EXTCALL(glCompileShaderARB(shader_obj));
4107 print_glsl_info_log(&GLINFO_LOCATION, shader_obj);
4109 return shader_obj;
4112 static void shader_glsl_get_caps(WINED3DDEVTYPE devtype, const WineD3D_GL_Info *gl_info, struct shader_caps *pCaps)
4114 /* Nvidia Geforce6/7 or Ati R4xx/R5xx cards with GLSL support, support VS 3.0 but older Nvidia/Ati
4115 * models with GLSL support only support 2.0. In case of nvidia we can detect VS 2.0 support using
4116 * vs_nv_version which is based on NV_vertex_program.
4117 * For Ati cards there's no way using glsl (it abstracts the lowlevel info away) and also not
4118 * using ARB_vertex_program. It is safe to assume that when a card supports pixel shader 2.0 it
4119 * supports vertex shader 2.0 too and the way around. We can detect ps2.0 using the maximum number
4120 * of native instructions, so use that here. For more info see the pixel shader versioning code below.
4122 if((GLINFO_LOCATION.vs_nv_version == VS_VERSION_20) || (GLINFO_LOCATION.ps_arb_max_instructions <= 512))
4123 pCaps->VertexShaderVersion = WINED3DVS_VERSION(2,0);
4124 else
4125 pCaps->VertexShaderVersion = WINED3DVS_VERSION(3,0);
4126 TRACE_(d3d_caps)("Hardware vertex shader version %d.%d enabled (GLSL)\n", (pCaps->VertexShaderVersion >> 8) & 0xff, pCaps->VertexShaderVersion & 0xff);
4127 pCaps->MaxVertexShaderConst = GL_LIMITS(vshader_constantsF);
4129 /* Older DX9-class videocards (GeforceFX / Radeon >9500/X*00) only support pixel shader 2.0/2.0a/2.0b.
4130 * In OpenGL the extensions related to GLSL abstract lowlevel GL info away which is needed
4131 * to distinguish between 2.0 and 3.0 (and 2.0a/2.0b). In case of Nvidia we use their fragment
4132 * program extensions. On other hardware including ATI GL_ARB_fragment_program offers the info
4133 * in max native instructions. Intel and others also offer the info in this extension but they
4134 * don't support GLSL (at least on Windows).
4136 * PS2.0 requires at least 96 instructions, 2.0a/2.0b go up to 512. Assume that if the number
4137 * of instructions is 512 or less we have to do with ps2.0 hardware.
4138 * 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.
4140 if((GLINFO_LOCATION.ps_nv_version == PS_VERSION_20) || (GLINFO_LOCATION.ps_arb_max_instructions <= 512))
4141 pCaps->PixelShaderVersion = WINED3DPS_VERSION(2,0);
4142 else
4143 pCaps->PixelShaderVersion = WINED3DPS_VERSION(3,0);
4145 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
4146 * Direct3D minimum requirement.
4148 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
4149 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
4151 * The problem is that the refrast clamps temporary results in the shader to
4152 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
4153 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
4154 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
4155 * offer a way to query this.
4157 pCaps->PixelShader1xMaxValue = 8.0;
4158 TRACE_(d3d_caps)("Hardware pixel shader version %d.%d enabled (GLSL)\n", (pCaps->PixelShaderVersion >> 8) & 0xff, pCaps->PixelShaderVersion & 0xff);
4161 static BOOL shader_glsl_color_fixup_supported(struct color_fixup_desc fixup)
4163 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
4165 TRACE("Checking support for fixup:\n");
4166 dump_color_fixup_desc(fixup);
4169 /* We support everything except YUV conversions. */
4170 if (!is_yuv_fixup(fixup))
4172 TRACE("[OK]\n");
4173 return TRUE;
4176 TRACE("[FAILED]\n");
4177 return FALSE;
4180 static const SHADER_HANDLER shader_glsl_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
4182 /* WINED3DSIH_ABS */ shader_glsl_map2gl,
4183 /* WINED3DSIH_ADD */ shader_glsl_arith,
4184 /* WINED3DSIH_BEM */ pshader_glsl_bem,
4185 /* WINED3DSIH_BREAK */ shader_glsl_break,
4186 /* WINED3DSIH_BREAKC */ shader_glsl_breakc,
4187 /* WINED3DSIH_BREAKP */ NULL,
4188 /* WINED3DSIH_CALL */ shader_glsl_call,
4189 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz,
4190 /* WINED3DSIH_CMP */ shader_glsl_cmp,
4191 /* WINED3DSIH_CND */ shader_glsl_cnd,
4192 /* WINED3DSIH_CRS */ shader_glsl_cross,
4193 /* WINED3DSIH_DCL */ NULL,
4194 /* WINED3DSIH_DEF */ NULL,
4195 /* WINED3DSIH_DEFB */ NULL,
4196 /* WINED3DSIH_DEFI */ NULL,
4197 /* WINED3DSIH_DP2ADD */ pshader_glsl_dp2add,
4198 /* WINED3DSIH_DP3 */ shader_glsl_dot,
4199 /* WINED3DSIH_DP4 */ shader_glsl_dot,
4200 /* WINED3DSIH_DST */ shader_glsl_dst,
4201 /* WINED3DSIH_DSX */ shader_glsl_map2gl,
4202 /* WINED3DSIH_DSY */ shader_glsl_map2gl,
4203 /* WINED3DSIH_ELSE */ shader_glsl_else,
4204 /* WINED3DSIH_ENDIF */ shader_glsl_end,
4205 /* WINED3DSIH_ENDLOOP */ shader_glsl_end,
4206 /* WINED3DSIH_ENDREP */ shader_glsl_end,
4207 /* WINED3DSIH_EXP */ shader_glsl_map2gl,
4208 /* WINED3DSIH_EXPP */ shader_glsl_expp,
4209 /* WINED3DSIH_FRC */ shader_glsl_map2gl,
4210 /* WINED3DSIH_IF */ shader_glsl_if,
4211 /* WINED3DSIH_IFC */ shader_glsl_ifc,
4212 /* WINED3DSIH_LABEL */ shader_glsl_label,
4213 /* WINED3DSIH_LIT */ shader_glsl_lit,
4214 /* WINED3DSIH_LOG */ shader_glsl_log,
4215 /* WINED3DSIH_LOGP */ shader_glsl_log,
4216 /* WINED3DSIH_LOOP */ shader_glsl_loop,
4217 /* WINED3DSIH_LRP */ shader_glsl_lrp,
4218 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn,
4219 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn,
4220 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn,
4221 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn,
4222 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn,
4223 /* WINED3DSIH_MAD */ shader_glsl_mad,
4224 /* WINED3DSIH_MAX */ shader_glsl_map2gl,
4225 /* WINED3DSIH_MIN */ shader_glsl_map2gl,
4226 /* WINED3DSIH_MOV */ shader_glsl_mov,
4227 /* WINED3DSIH_MOVA */ shader_glsl_mov,
4228 /* WINED3DSIH_MUL */ shader_glsl_arith,
4229 /* WINED3DSIH_NOP */ NULL,
4230 /* WINED3DSIH_NRM */ shader_glsl_map2gl,
4231 /* WINED3DSIH_PHASE */ NULL,
4232 /* WINED3DSIH_POW */ shader_glsl_pow,
4233 /* WINED3DSIH_RCP */ shader_glsl_rcp,
4234 /* WINED3DSIH_REP */ shader_glsl_rep,
4235 /* WINED3DSIH_RET */ NULL,
4236 /* WINED3DSIH_RSQ */ shader_glsl_rsq,
4237 /* WINED3DSIH_SETP */ NULL,
4238 /* WINED3DSIH_SGE */ shader_glsl_compare,
4239 /* WINED3DSIH_SGN */ shader_glsl_map2gl,
4240 /* WINED3DSIH_SINCOS */ shader_glsl_sincos,
4241 /* WINED3DSIH_SLT */ shader_glsl_compare,
4242 /* WINED3DSIH_SUB */ shader_glsl_arith,
4243 /* WINED3DSIH_TEX */ pshader_glsl_tex,
4244 /* WINED3DSIH_TEXBEM */ pshader_glsl_texbem,
4245 /* WINED3DSIH_TEXBEML */ pshader_glsl_texbem,
4246 /* WINED3DSIH_TEXCOORD */ pshader_glsl_texcoord,
4247 /* WINED3DSIH_TEXDEPTH */ pshader_glsl_texdepth,
4248 /* WINED3DSIH_TEXDP3 */ pshader_glsl_texdp3,
4249 /* WINED3DSIH_TEXDP3TEX */ pshader_glsl_texdp3tex,
4250 /* WINED3DSIH_TEXKILL */ pshader_glsl_texkill,
4251 /* WINED3DSIH_TEXLDD */ NULL,
4252 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl,
4253 /* WINED3DSIH_TEXM3x2DEPTH */ pshader_glsl_texm3x2depth,
4254 /* WINED3DSIH_TEXM3x2PAD */ pshader_glsl_texm3x2pad,
4255 /* WINED3DSIH_TEXM3x2TEX */ pshader_glsl_texm3x2tex,
4256 /* WINED3DSIH_TEXM3x3 */ pshader_glsl_texm3x3,
4257 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
4258 /* WINED3DSIH_TEXM3x3PAD */ pshader_glsl_texm3x3pad,
4259 /* WINED3DSIH_TEXM3x3SPEC */ pshader_glsl_texm3x3spec,
4260 /* WINED3DSIH_TEXM3x3TEX */ pshader_glsl_texm3x3tex,
4261 /* WINED3DSIH_TEXM3x3VSPEC */ pshader_glsl_texm3x3vspec,
4262 /* WINED3DSIH_TEXREG2AR */ pshader_glsl_texreg2ar,
4263 /* WINED3DSIH_TEXREG2GB */ pshader_glsl_texreg2gb,
4264 /* WINED3DSIH_TEXREG2RGB */ pshader_glsl_texreg2rgb,
4267 const shader_backend_t glsl_shader_backend = {
4268 shader_glsl_instruction_handler_table,
4269 shader_glsl_select,
4270 shader_glsl_select_depth_blt,
4271 shader_glsl_deselect_depth_blt,
4272 shader_glsl_update_float_vertex_constants,
4273 shader_glsl_update_float_pixel_constants,
4274 shader_glsl_load_constants,
4275 shader_glsl_destroy,
4276 shader_glsl_alloc,
4277 shader_glsl_free,
4278 shader_glsl_dirty_const,
4279 shader_glsl_generate_pshader,
4280 shader_glsl_generate_vshader,
4281 shader_glsl_get_caps,
4282 shader_glsl_color_fixup_supported,