push 3cf4bc9cdb38c4fc36232fe19b9b904fd4d068e9
[wine/hacks.git] / dlls / wined3d / glsl_shader.c
blob3f54170033e82e2e9769774f5a99970299a76d63
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 <stdio.h>
33 #include "wined3d_private.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
36 WINE_DECLARE_DEBUG_CHANNEL(d3d_constants);
37 WINE_DECLARE_DEBUG_CHANNEL(d3d_caps);
38 WINE_DECLARE_DEBUG_CHANNEL(d3d);
40 #define GLINFO_LOCATION (*gl_info)
42 typedef struct {
43 char reg_name[150];
44 char mask_str[6];
45 } glsl_dst_param_t;
47 typedef struct {
48 char reg_name[150];
49 char param_str[100];
50 } glsl_src_param_t;
52 typedef struct {
53 const char *name;
54 DWORD coord_mask;
55 } glsl_sample_function_t;
57 /* GLSL shader private data */
58 struct shader_glsl_priv {
59 struct hash_table_t *glsl_program_lookup;
60 const struct glsl_shader_prog_link *glsl_program;
61 GLhandleARB depth_blt_program[tex_type_count];
64 /* Struct to maintain data about a linked GLSL program */
65 struct glsl_shader_prog_link {
66 struct list vshader_entry;
67 struct list pshader_entry;
68 GLhandleARB programId;
69 GLhandleARB *vuniformF_locations;
70 GLhandleARB *puniformF_locations;
71 GLhandleARB vuniformI_locations[MAX_CONST_I];
72 GLhandleARB puniformI_locations[MAX_CONST_I];
73 GLhandleARB posFixup_location;
74 GLhandleARB bumpenvmat_location[MAX_TEXTURES];
75 GLhandleARB luminancescale_location[MAX_TEXTURES];
76 GLhandleARB luminanceoffset_location[MAX_TEXTURES];
77 GLhandleARB ycorrection_location;
78 GLenum vertex_color_clamp;
79 GLhandleARB vshader;
80 IWineD3DPixelShader *pshader;
81 struct ps_compile_args ps_args;
84 typedef struct {
85 GLhandleARB vshader;
86 IWineD3DPixelShader *pshader;
87 struct ps_compile_args ps_args;
88 } glsl_program_key_t;
91 /** Prints the GLSL info log which will contain error messages if they exist */
92 static void print_glsl_info_log(const WineD3D_GL_Info *gl_info, GLhandleARB obj)
94 int infologLength = 0;
95 char *infoLog;
96 unsigned int i;
97 BOOL is_spam;
99 static const char * const spam[] =
101 "Vertex shader was successfully compiled to run on hardware.\n", /* fglrx */
102 "Fragment shader was successfully compiled to run on hardware.\n", /* fglrx */
103 "Fragment shader(s) linked, vertex shader(s) linked. \n ", /* fglrx, with \n */
104 "Fragment shader(s) linked, vertex shader(s) linked.", /* fglrx, no \n */
105 "Vertex shader(s) linked, no fragment shader(s) defined. \n ", /* fglrx, with \n */
106 "Vertex shader(s) linked, no fragment shader(s) defined.", /* fglrx, no \n */
107 "Fragment shader was successfully compiled to run on hardware.\nWARNING: 0:1: extension 'GL_ARB_draw_buffers' is not supported",
108 "Fragment shader(s) linked, no vertex shader(s) defined.", /* fglrx, no \n */
109 "Fragment shader(s) linked, no vertex shader(s) defined. \n ", /* fglrx, with \n */
110 "WARNING: 0:2: extension 'GL_ARB_draw_buffers' is not supported\n" /* MacOS ati */
113 GL_EXTCALL(glGetObjectParameterivARB(obj,
114 GL_OBJECT_INFO_LOG_LENGTH_ARB,
115 &infologLength));
117 /* A size of 1 is just a null-terminated string, so the log should be bigger than
118 * that if there are errors. */
119 if (infologLength > 1)
121 /* Fglrx doesn't terminate the string properly, but it tells us the proper length.
122 * So use HEAP_ZERO_MEMORY to avoid uninitialized bytes
124 infoLog = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, infologLength);
125 GL_EXTCALL(glGetInfoLogARB(obj, infologLength, NULL, infoLog));
126 is_spam = FALSE;
128 for(i = 0; i < sizeof(spam) / sizeof(spam[0]); i++) {
129 if(strcmp(infoLog, spam[i]) == 0) {
130 is_spam = TRUE;
131 break;
134 if(is_spam) {
135 TRACE("Spam received from GLSL shader #%u: %s\n", obj, debugstr_a(infoLog));
136 } else {
137 FIXME("Error received from GLSL shader #%u: %s\n", obj, debugstr_a(infoLog));
139 HeapFree(GetProcessHeap(), 0, infoLog);
144 * Loads (pixel shader) samplers
146 static void shader_glsl_load_psamplers(const WineD3D_GL_Info *gl_info, IWineD3DStateBlock *iface, GLhandleARB programId)
148 IWineD3DStateBlockImpl* stateBlock = (IWineD3DStateBlockImpl*) iface;
149 GLhandleARB name_loc;
150 int i;
151 char sampler_name[20];
153 for (i = 0; i < MAX_FRAGMENT_SAMPLERS; ++i) {
154 snprintf(sampler_name, sizeof(sampler_name), "Psampler%d", i);
155 name_loc = GL_EXTCALL(glGetUniformLocationARB(programId, sampler_name));
156 if (name_loc != -1) {
157 int mapped_unit = stateBlock->wineD3DDevice->texUnitMap[i];
158 if (mapped_unit != -1 && mapped_unit < GL_LIMITS(fragment_samplers)) {
159 TRACE("Loading %s for texture %d\n", sampler_name, mapped_unit);
160 GL_EXTCALL(glUniform1iARB(name_loc, mapped_unit));
161 checkGLcall("glUniform1iARB");
162 } else {
163 ERR("Trying to load sampler %s on unsupported unit %d\n", sampler_name, mapped_unit);
169 static void shader_glsl_load_vsamplers(const WineD3D_GL_Info *gl_info, IWineD3DStateBlock *iface, GLhandleARB programId)
171 IWineD3DStateBlockImpl* stateBlock = (IWineD3DStateBlockImpl*) iface;
172 GLhandleARB name_loc;
173 char sampler_name[20];
174 int i;
176 for (i = 0; i < MAX_VERTEX_SAMPLERS; ++i) {
177 snprintf(sampler_name, sizeof(sampler_name), "Vsampler%d", i);
178 name_loc = GL_EXTCALL(glGetUniformLocationARB(programId, sampler_name));
179 if (name_loc != -1) {
180 int mapped_unit = stateBlock->wineD3DDevice->texUnitMap[MAX_FRAGMENT_SAMPLERS + i];
181 if (mapped_unit != -1 && mapped_unit < GL_LIMITS(combined_samplers)) {
182 TRACE("Loading %s for texture %d\n", sampler_name, mapped_unit);
183 GL_EXTCALL(glUniform1iARB(name_loc, mapped_unit));
184 checkGLcall("glUniform1iARB");
185 } else {
186 ERR("Trying to load sampler %s on unsupported unit %d\n", sampler_name, mapped_unit);
192 /**
193 * Loads floating point constants (aka uniforms) into the currently set GLSL program.
194 * When constant_list == NULL, it will load all the constants.
196 static void shader_glsl_load_constantsF(IWineD3DBaseShaderImpl *This, const WineD3D_GL_Info *gl_info,
197 unsigned int max_constants, const float *constants, const GLhandleARB *constant_locations,
198 const struct list *constant_list)
200 const constants_entry *constant;
201 const local_constant *lconst;
202 GLhandleARB tmp_loc;
203 DWORD i, j, k;
204 const DWORD *idx;
206 if (TRACE_ON(d3d_shader)) {
207 LIST_FOR_EACH_ENTRY(constant, constant_list, constants_entry, entry) {
208 idx = constant->idx;
209 j = constant->count;
210 while (j--) {
211 i = *idx++;
212 tmp_loc = constant_locations[i];
213 if (tmp_loc != -1) {
214 TRACE_(d3d_constants)("Loading constants %i: %f, %f, %f, %f\n", i,
215 constants[i * 4 + 0], constants[i * 4 + 1],
216 constants[i * 4 + 2], constants[i * 4 + 3]);
222 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
223 if(WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) == 1 &&
224 shader_is_pshader_version(This->baseShader.hex_version)) {
225 float lcl_const[4];
227 LIST_FOR_EACH_ENTRY(constant, constant_list, constants_entry, entry) {
228 idx = constant->idx;
229 j = constant->count;
230 while (j--) {
231 i = *idx++;
232 tmp_loc = constant_locations[i];
233 if (tmp_loc != -1) {
234 /* We found this uniform name in the program - go ahead and send the data */
235 k = i * 4;
236 if(constants[k + 0] < -1.0) lcl_const[0] = -1.0;
237 else if(constants[k + 0] > 1.0) lcl_const[0] = 1.0;
238 else lcl_const[0] = constants[k + 0];
239 if(constants[k + 1] < -1.0) lcl_const[1] = -1.0;
240 else if(constants[k + 1] > 1.0) lcl_const[1] = 1.0;
241 else lcl_const[1] = constants[k + 1];
242 if(constants[k + 2] < -1.0) lcl_const[2] = -1.0;
243 else if(constants[k + 2] > 1.0) lcl_const[2] = 1.0;
244 else lcl_const[2] = constants[k + 2];
245 if(constants[k + 3] < -1.0) lcl_const[3] = -1.0;
246 else if(constants[k + 3] > 1.0) lcl_const[3] = 1.0;
247 else lcl_const[3] = constants[k + 3];
249 GL_EXTCALL(glUniform4fvARB(tmp_loc, 1, lcl_const));
253 } else {
254 LIST_FOR_EACH_ENTRY(constant, constant_list, constants_entry, entry) {
255 idx = constant->idx;
256 j = constant->count;
257 while (j--) {
258 i = *idx++;
259 tmp_loc = constant_locations[i];
260 if (tmp_loc != -1) {
261 /* We found this uniform name in the program - go ahead and send the data */
262 GL_EXTCALL(glUniform4fvARB(tmp_loc, 1, constants + (i * 4)));
267 checkGLcall("glUniform4fvARB()");
269 if(!This->baseShader.load_local_constsF) {
270 TRACE("No need to load local float constants for this shader\n");
271 return;
274 /* Load immediate constants */
275 if (TRACE_ON(d3d_shader)) {
276 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
277 tmp_loc = constant_locations[lconst->idx];
278 if (tmp_loc != -1) {
279 const GLfloat *values = (const GLfloat *)lconst->value;
280 TRACE_(d3d_constants)("Loading local constants %i: %f, %f, %f, %f\n", lconst->idx,
281 values[0], values[1], values[2], values[3]);
285 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
286 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
287 tmp_loc = constant_locations[lconst->idx];
288 if (tmp_loc != -1) {
289 /* We found this uniform name in the program - go ahead and send the data */
290 GL_EXTCALL(glUniform4fvARB(tmp_loc, 1, (const GLfloat *)lconst->value));
293 checkGLcall("glUniform4fvARB()");
296 /* Loads integer constants (aka uniforms) into the currently set GLSL program. */
297 static void shader_glsl_load_constantsI(IWineD3DBaseShaderImpl *This, const WineD3D_GL_Info *gl_info,
298 const GLhandleARB locations[MAX_CONST_I], const int *constants, WORD constants_set)
300 unsigned int i;
301 struct list* ptr;
303 for (i = 0; constants_set; constants_set >>= 1, ++i)
305 if (!(constants_set & 1)) continue;
307 TRACE_(d3d_constants)("Loading constants %u: %i, %i, %i, %i\n",
308 i, constants[i*4], constants[i*4+1], constants[i*4+2], constants[i*4+3]);
310 /* We found this uniform name in the program - go ahead and send the data */
311 GL_EXTCALL(glUniform4ivARB(locations[i], 1, &constants[i*4]));
312 checkGLcall("glUniform4ivARB");
315 /* Load immediate constants */
316 ptr = list_head(&This->baseShader.constantsI);
317 while (ptr) {
318 const struct local_constant *lconst = LIST_ENTRY(ptr, const struct local_constant, entry);
319 unsigned int idx = lconst->idx;
320 const GLint *values = (const GLint *)lconst->value;
322 TRACE_(d3d_constants)("Loading local constants %i: %i, %i, %i, %i\n", idx,
323 values[0], values[1], values[2], values[3]);
325 /* We found this uniform name in the program - go ahead and send the data */
326 GL_EXTCALL(glUniform4ivARB(locations[idx], 1, values));
327 checkGLcall("glUniform4ivARB");
328 ptr = list_next(&This->baseShader.constantsI, ptr);
332 /* Loads boolean constants (aka uniforms) into the currently set GLSL program. */
333 static void shader_glsl_load_constantsB(IWineD3DBaseShaderImpl *This, const WineD3D_GL_Info *gl_info,
334 GLhandleARB programId, const BOOL *constants, WORD constants_set)
336 GLhandleARB tmp_loc;
337 unsigned int i;
338 char tmp_name[8];
339 char is_pshader = shader_is_pshader_version(This->baseShader.hex_version);
340 const char* prefix = is_pshader? "PB":"VB";
341 struct list* ptr;
343 /* TODO: Benchmark and see if it would be beneficial to store the
344 * locations of the constants to avoid looking up each time */
345 for (i = 0; constants_set; constants_set >>= 1, ++i)
347 if (!(constants_set & 1)) continue;
349 TRACE_(d3d_constants)("Loading constants %i: %i;\n", i, constants[i]);
351 /* TODO: Benchmark and see if it would be beneficial to store the
352 * locations of the constants to avoid looking up each time */
353 snprintf(tmp_name, sizeof(tmp_name), "%s[%i]", prefix, i);
354 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
355 if (tmp_loc != -1)
357 /* We found this uniform name in the program - go ahead and send the data */
358 GL_EXTCALL(glUniform1ivARB(tmp_loc, 1, &constants[i]));
359 checkGLcall("glUniform1ivARB");
363 /* Load immediate constants */
364 ptr = list_head(&This->baseShader.constantsB);
365 while (ptr) {
366 const struct local_constant *lconst = LIST_ENTRY(ptr, const struct local_constant, entry);
367 unsigned int idx = lconst->idx;
368 const GLint *values = (const GLint *)lconst->value;
370 TRACE_(d3d_constants)("Loading local constants %i: %i\n", idx, values[0]);
372 snprintf(tmp_name, sizeof(tmp_name), "%s[%i]", prefix, idx);
373 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
374 if (tmp_loc != -1) {
375 /* We found this uniform name in the program - go ahead and send the data */
376 GL_EXTCALL(glUniform1ivARB(tmp_loc, 1, values));
377 checkGLcall("glUniform1ivARB");
379 ptr = list_next(&This->baseShader.constantsB, ptr);
386 * Loads the app-supplied constants into the currently set GLSL program.
388 static void shader_glsl_load_constants(
389 IWineD3DDevice* device,
390 char usePixelShader,
391 char useVertexShader) {
393 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) device;
394 const struct shader_glsl_priv *priv = (struct shader_glsl_priv *)deviceImpl->shader_priv;
395 IWineD3DStateBlockImpl* stateBlock = deviceImpl->stateBlock;
396 const WineD3D_GL_Info *gl_info = &deviceImpl->adapter->gl_info;
398 const GLhandleARB *constant_locations;
399 const struct list *constant_list;
400 GLhandleARB programId;
401 const struct glsl_shader_prog_link *prog = priv->glsl_program;
402 int i;
404 if (!prog) {
405 /* No GLSL program set - nothing to do. */
406 return;
408 programId = prog->programId;
410 if (useVertexShader) {
411 IWineD3DBaseShaderImpl* vshader = (IWineD3DBaseShaderImpl*) stateBlock->vertexShader;
413 constant_locations = prog->vuniformF_locations;
414 constant_list = &stateBlock->set_vconstantsF;
416 /* Load DirectX 9 float constants/uniforms for vertex shader */
417 shader_glsl_load_constantsF(vshader, gl_info, GL_LIMITS(vshader_constantsF),
418 stateBlock->vertexShaderConstantF, constant_locations, constant_list);
420 /* Load DirectX 9 integer constants/uniforms for vertex shader */
421 if(vshader->baseShader.uses_int_consts) {
422 shader_glsl_load_constantsI(vshader, gl_info, prog->vuniformI_locations,
423 stateBlock->vertexShaderConstantI, stateBlock->changed.vertexShaderConstantsI);
426 /* Load DirectX 9 boolean constants/uniforms for vertex shader */
427 if(vshader->baseShader.uses_bool_consts) {
428 shader_glsl_load_constantsB(vshader, gl_info, programId,
429 stateBlock->vertexShaderConstantB, stateBlock->changed.vertexShaderConstantsB);
432 /* Upload the position fixup params */
433 GL_EXTCALL(glUniform4fvARB(prog->posFixup_location, 1, &deviceImpl->posFixup[0]));
434 checkGLcall("glUniform4fvARB");
437 if (usePixelShader) {
439 IWineD3DBaseShaderImpl* pshader = (IWineD3DBaseShaderImpl*) stateBlock->pixelShader;
441 constant_locations = prog->puniformF_locations;
442 constant_list = &stateBlock->set_pconstantsF;
444 /* Load DirectX 9 float constants/uniforms for pixel shader */
445 shader_glsl_load_constantsF(pshader, gl_info, GL_LIMITS(pshader_constantsF),
446 stateBlock->pixelShaderConstantF, constant_locations, constant_list);
448 /* Load DirectX 9 integer constants/uniforms for pixel shader */
449 if(pshader->baseShader.uses_int_consts) {
450 shader_glsl_load_constantsI(pshader, gl_info, prog->puniformI_locations,
451 stateBlock->pixelShaderConstantI, stateBlock->changed.pixelShaderConstantsI);
454 /* Load DirectX 9 boolean constants/uniforms for pixel shader */
455 if(pshader->baseShader.uses_bool_consts) {
456 shader_glsl_load_constantsB(pshader, gl_info, programId,
457 stateBlock->pixelShaderConstantB, stateBlock->changed.pixelShaderConstantsB);
460 /* Upload the environment bump map matrix if needed. The needsbumpmat member specifies the texture stage to load the matrix from.
461 * It can't be 0 for a valid texbem instruction.
463 for(i = 0; i < ((IWineD3DPixelShaderImpl *) pshader)->numbumpenvmatconsts; i++) {
464 IWineD3DPixelShaderImpl *ps = (IWineD3DPixelShaderImpl *) pshader;
465 int stage = ps->luminanceconst[i].texunit;
467 const float *data = (const float *)&stateBlock->textureState[(int)ps->bumpenvmatconst[i].texunit][WINED3DTSS_BUMPENVMAT00];
468 GL_EXTCALL(glUniformMatrix2fvARB(prog->bumpenvmat_location[i], 1, 0, data));
469 checkGLcall("glUniformMatrix2fvARB");
471 /* texbeml needs the luminance scale and offset too. If texbeml is used, needsbumpmat
472 * is set too, so we can check that in the needsbumpmat check
474 if(ps->baseShader.reg_maps.luminanceparams[stage]) {
475 const GLfloat *scale = (const GLfloat *)&stateBlock->textureState[stage][WINED3DTSS_BUMPENVLSCALE];
476 const GLfloat *offset = (const GLfloat *)&stateBlock->textureState[stage][WINED3DTSS_BUMPENVLOFFSET];
478 GL_EXTCALL(glUniform1fvARB(prog->luminancescale_location[i], 1, scale));
479 checkGLcall("glUniform1fvARB");
480 GL_EXTCALL(glUniform1fvARB(prog->luminanceoffset_location[i], 1, offset));
481 checkGLcall("glUniform1fvARB");
485 if(((IWineD3DPixelShaderImpl *) pshader)->vpos_uniform) {
486 float correction_params[4];
487 if(deviceImpl->render_offscreen) {
488 correction_params[0] = 0.0;
489 correction_params[1] = 1.0;
490 } else {
491 /* position is window relative, not viewport relative */
492 correction_params[0] = ((IWineD3DSurfaceImpl *) deviceImpl->render_targets[0])->currentDesc.Height;
493 correction_params[1] = -1.0;
495 GL_EXTCALL(glUniform4fvARB(prog->ycorrection_location, 1, correction_params));
500 /** Generate the variable & register declarations for the GLSL output target */
501 static void shader_generate_glsl_declarations(IWineD3DBaseShader *iface, const shader_reg_maps *reg_maps,
502 SHADER_BUFFER *buffer, const WineD3D_GL_Info *gl_info)
504 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
505 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) This->baseShader.device;
506 unsigned int i, extra_constants_needed = 0;
507 const local_constant *lconst;
509 /* There are some minor differences between pixel and vertex shaders */
510 char pshader = shader_is_pshader_version(This->baseShader.hex_version);
511 char prefix = pshader ? 'P' : 'V';
513 /* Prototype the subroutines */
514 for (i = 0; i < This->baseShader.limits.label; i++) {
515 if (reg_maps->labels[i])
516 shader_addline(buffer, "void subroutine%u();\n", i);
519 /* Declare the constants (aka uniforms) */
520 if (This->baseShader.limits.constant_float > 0) {
521 unsigned max_constantsF = min(This->baseShader.limits.constant_float,
522 (pshader ? GL_LIMITS(pshader_constantsF) : GL_LIMITS(vshader_constantsF)));
523 shader_addline(buffer, "uniform vec4 %cC[%u];\n", prefix, max_constantsF);
526 if (This->baseShader.limits.constant_int > 0)
527 shader_addline(buffer, "uniform ivec4 %cI[%u];\n", prefix, This->baseShader.limits.constant_int);
529 if (This->baseShader.limits.constant_bool > 0)
530 shader_addline(buffer, "uniform bool %cB[%u];\n", prefix, This->baseShader.limits.constant_bool);
532 if(!pshader) {
533 shader_addline(buffer, "uniform vec4 posFixup;\n");
534 /* Predeclaration; This function is added at link time based on the pixel shader.
535 * VS 3.0 shaders have an array OUT[] the shader writes to, earlier versions don't have
536 * that. We know the input to the reorder function at vertex shader compile time, so
537 * we can deal with that. The reorder function for a 1.x and 2.x vertex shader can just
538 * read gl_FrontColor. The output depends on the pixel shader. The reorder function for a
539 * 1.x and 2.x pshader or for fixed function will write gl_FrontColor, and for a 3.0 shader
540 * it will write to the varying array. Here we depend on the shader optimizer on sorting that
541 * out. The nvidia driver only does that if the parameter is inout instead of out, hence the
542 * inout.
544 if(This->baseShader.hex_version >= WINED3DVS_VERSION(3, 0)) {
545 shader_addline(buffer, "void order_ps_input(in vec4[%u]);\n", MAX_REG_OUTPUT);
546 } else {
547 shader_addline(buffer, "void order_ps_input();\n");
549 } else {
550 IWineD3DPixelShaderImpl *ps_impl = (IWineD3DPixelShaderImpl *) This;
552 ps_impl->numbumpenvmatconsts = 0;
553 for(i = 0; i < (sizeof(reg_maps->bumpmat) / sizeof(reg_maps->bumpmat[0])); i++) {
554 if(!reg_maps->bumpmat[i]) {
555 continue;
558 ps_impl->bumpenvmatconst[(int) ps_impl->numbumpenvmatconsts].texunit = i;
559 shader_addline(buffer, "uniform mat2 bumpenvmat%d;\n", i);
561 if(reg_maps->luminanceparams) {
562 ps_impl->luminanceconst[(int) ps_impl->numbumpenvmatconsts].texunit = i;
563 shader_addline(buffer, "uniform float luminancescale%d;\n", i);
564 shader_addline(buffer, "uniform float luminanceoffset%d;\n", i);
565 extra_constants_needed++;
566 } else {
567 ps_impl->luminanceconst[(int) ps_impl->numbumpenvmatconsts].texunit = -1;
570 extra_constants_needed++;
571 ps_impl->numbumpenvmatconsts++;
574 if(device->stateBlock->renderState[WINED3DRS_SRGBWRITEENABLE]) {
575 shader_addline(buffer, "const vec4 srgb_mul_low = vec4(%f, %f, %f, %f);\n",
576 srgb_mul_low, srgb_mul_low, srgb_mul_low, srgb_mul_low);
577 shader_addline(buffer, "const vec4 srgb_comparison = vec4(%f, %f, %f, %f);\n",
578 srgb_cmp, srgb_cmp, srgb_cmp, srgb_cmp);
580 if(reg_maps->vpos || reg_maps->usesdsy) {
581 if(This->baseShader.limits.constant_float + extra_constants_needed + 1 < GL_LIMITS(pshader_constantsF)) {
582 shader_addline(buffer, "uniform vec4 ycorrection;\n");
583 ((IWineD3DPixelShaderImpl *) This)->vpos_uniform = 1;
584 extra_constants_needed++;
585 } else {
586 /* This happens because we do not have proper tracking of the constant registers that are
587 * actually used, only the max limit of the shader version
589 FIXME("Cannot find a free uniform for vpos correction params\n");
590 shader_addline(buffer, "const vec4 ycorrection = vec4(%f, %f, 0.0, 0.0);\n",
591 device->render_offscreen ? 0.0 : ((IWineD3DSurfaceImpl *) device->render_targets[0])->currentDesc.Height,
592 device->render_offscreen ? 1.0 : -1.0);
594 shader_addline(buffer, "vec4 vpos;\n");
598 /* Declare texture samplers */
599 for (i = 0; i < This->baseShader.limits.sampler; i++) {
600 if (reg_maps->samplers[i]) {
602 DWORD stype = reg_maps->samplers[i] & WINED3DSP_TEXTURETYPE_MASK;
603 switch (stype) {
605 case WINED3DSTT_1D:
606 shader_addline(buffer, "uniform sampler1D %csampler%u;\n", prefix, i);
607 break;
608 case WINED3DSTT_2D:
609 if(device->stateBlock->textures[i] &&
610 IWineD3DBaseTexture_GetTextureDimensions(device->stateBlock->textures[i]) == GL_TEXTURE_RECTANGLE_ARB) {
611 shader_addline(buffer, "uniform sampler2DRect %csampler%u;\n", prefix, i);
612 } else {
613 shader_addline(buffer, "uniform sampler2D %csampler%u;\n", prefix, i);
615 break;
616 case WINED3DSTT_CUBE:
617 shader_addline(buffer, "uniform samplerCube %csampler%u;\n", prefix, i);
618 break;
619 case WINED3DSTT_VOLUME:
620 shader_addline(buffer, "uniform sampler3D %csampler%u;\n", prefix, i);
621 break;
622 default:
623 shader_addline(buffer, "uniform unsupported_sampler %csampler%u;\n", prefix, i);
624 FIXME("Unrecognized sampler type: %#x\n", stype);
625 break;
630 /* Declare address variables */
631 for (i = 0; i < This->baseShader.limits.address; i++) {
632 if (reg_maps->address[i])
633 shader_addline(buffer, "ivec4 A%d;\n", i);
636 /* Declare texture coordinate temporaries and initialize them */
637 for (i = 0; i < This->baseShader.limits.texcoord; i++) {
638 if (reg_maps->texcoord[i])
639 shader_addline(buffer, "vec4 T%u = gl_TexCoord[%u];\n", i, i);
642 /* Declare input register varyings. Only pixel shader, vertex shaders have that declared in the
643 * helper function shader that is linked in at link time
645 if(pshader && This->baseShader.hex_version >= WINED3DPS_VERSION(3, 0)) {
646 if(use_vs(device)) {
647 shader_addline(buffer, "varying vec4 IN[%u];\n", GL_LIMITS(glsl_varyings) / 4);
648 } else {
649 /* TODO: Write a replacement shader for the fixed function vertex pipeline, so this isn't needed.
650 * For fixed function vertex processing + 3.0 pixel shader we need a separate function in the
651 * pixel shader that reads the fixed function color into the packed input registers.
653 shader_addline(buffer, "vec4 IN[%u];\n", GL_LIMITS(glsl_varyings) / 4);
657 /* Declare output register temporaries */
658 if(This->baseShader.limits.packed_output) {
659 shader_addline(buffer, "vec4 OUT[%u];\n", This->baseShader.limits.packed_output);
662 /* Declare temporary variables */
663 for(i = 0; i < This->baseShader.limits.temporary; i++) {
664 if (reg_maps->temporary[i])
665 shader_addline(buffer, "vec4 R%u;\n", i);
668 /* Declare attributes */
669 for (i = 0; i < This->baseShader.limits.attributes; i++) {
670 if (reg_maps->attributes[i])
671 shader_addline(buffer, "attribute vec4 attrib%i;\n", i);
674 /* Declare loop registers aLx */
675 for (i = 0; i < reg_maps->loop_depth; i++) {
676 shader_addline(buffer, "int aL%u;\n", i);
677 shader_addline(buffer, "int tmpInt%u;\n", i);
680 /* Temporary variables for matrix operations */
681 shader_addline(buffer, "vec4 tmp0;\n");
682 shader_addline(buffer, "vec4 tmp1;\n");
684 /* Local constants use a different name so they can be loaded once at shader link time
685 * They can't be hardcoded into the shader text via LC = {x, y, z, w}; because the
686 * float -> string conversion can cause precision loss.
688 if(!This->baseShader.load_local_constsF) {
689 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
690 shader_addline(buffer, "uniform vec4 %cLC%u;\n", prefix, lconst->idx);
694 /* Start the main program */
695 shader_addline(buffer, "void main() {\n");
696 if(pshader && reg_maps->vpos) {
697 /* DirectX apps expect integer values, while OpenGL drivers add approximately 0.5. This causes
698 * off-by-one problems as spotted by the vPos d3d9 visual test. Unfortunately the ATI cards do
699 * not add exactly 0.5, but rather something like 0.49999999 or 0.50000001, which still causes
700 * precision troubles when we just substract 0.5.
702 * To deal with that just floor() the position. This will eliminate the fraction on all cards.
704 * TODO: Test how that behaves with multisampling once we can enable multisampling in winex11.
706 * An advantage of floor is that it works even if the driver doesn't add 1/2. It is somewhat
707 * questionable if 1.5, 2.5, ... are the proper values to return in gl_FragCoord, even though
708 * coordinates specify the pixel centers instead of the pixel corners. This code will behave
709 * correctly on drivers that returns integer values.
711 shader_addline(buffer, "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
715 /*****************************************************************************
716 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
718 * For more information, see http://wiki.winehq.org/DirectX-Shaders
719 ****************************************************************************/
721 /* Prototypes */
722 static void shader_glsl_add_src_param(const SHADER_OPCODE_ARG *arg, const DWORD param,
723 const DWORD addr_token, DWORD mask, glsl_src_param_t *src_param);
725 /** Used for opcode modifiers - They multiply the result by the specified amount */
726 static const char * const shift_glsl_tab[] = {
727 "", /* 0 (none) */
728 "2.0 * ", /* 1 (x2) */
729 "4.0 * ", /* 2 (x4) */
730 "8.0 * ", /* 3 (x8) */
731 "16.0 * ", /* 4 (x16) */
732 "32.0 * ", /* 5 (x32) */
733 "", /* 6 (x64) */
734 "", /* 7 (x128) */
735 "", /* 8 (d256) */
736 "", /* 9 (d128) */
737 "", /* 10 (d64) */
738 "", /* 11 (d32) */
739 "0.0625 * ", /* 12 (d16) */
740 "0.125 * ", /* 13 (d8) */
741 "0.25 * ", /* 14 (d4) */
742 "0.5 * " /* 15 (d2) */
745 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
746 static void shader_glsl_gen_modifier (
747 const DWORD instr,
748 const char *in_reg,
749 const char *in_regswizzle,
750 char *out_str) {
752 out_str[0] = 0;
754 if (instr == WINED3DSIO_TEXKILL)
755 return;
757 switch (instr & WINED3DSP_SRCMOD_MASK) {
758 case WINED3DSPSM_DZ: /* Need to handle this in the instructions itself (texld & texcrd). */
759 case WINED3DSPSM_DW:
760 case WINED3DSPSM_NONE:
761 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
762 break;
763 case WINED3DSPSM_NEG:
764 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
765 break;
766 case WINED3DSPSM_NOT:
767 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
768 break;
769 case WINED3DSPSM_BIAS:
770 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
771 break;
772 case WINED3DSPSM_BIASNEG:
773 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
774 break;
775 case WINED3DSPSM_SIGN:
776 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
777 break;
778 case WINED3DSPSM_SIGNNEG:
779 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
780 break;
781 case WINED3DSPSM_COMP:
782 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
783 break;
784 case WINED3DSPSM_X2:
785 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
786 break;
787 case WINED3DSPSM_X2NEG:
788 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
789 break;
790 case WINED3DSPSM_ABS:
791 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
792 break;
793 case WINED3DSPSM_ABSNEG:
794 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
795 break;
796 default:
797 FIXME("Unhandled modifier %u\n", (instr & WINED3DSP_SRCMOD_MASK));
798 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
802 /** Writes the GLSL variable name that corresponds to the register that the
803 * DX opcode parameter is trying to access */
804 static void shader_glsl_get_register_name(const DWORD param, const DWORD addr_token,
805 char *regstr, BOOL *is_color, const SHADER_OPCODE_ARG *arg)
807 /* oPos, oFog and oPts in D3D */
808 static const char * const hwrastout_reg_names[] = { "gl_Position", "gl_FogFragCoord", "gl_PointSize" };
810 DWORD reg = param & WINED3DSP_REGNUM_MASK;
811 DWORD regtype = shader_get_regtype(param);
812 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) arg->shader;
813 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
814 const WineD3D_GL_Info* gl_info = &deviceImpl->adapter->gl_info;
816 char pshader = shader_is_pshader_version(This->baseShader.hex_version);
817 char tmpStr[150];
819 *is_color = FALSE;
821 switch (regtype) {
822 case WINED3DSPR_TEMP:
823 sprintf(tmpStr, "R%u", reg);
824 break;
825 case WINED3DSPR_INPUT:
826 if (pshader) {
827 /* Pixel shaders >= 3.0 */
828 if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 3) {
829 DWORD in_count = GL_LIMITS(glsl_varyings) / 4;
831 if (param & WINED3DSHADER_ADDRMODE_RELATIVE) {
832 glsl_src_param_t rel_param;
833 shader_glsl_add_src_param(arg, addr_token, 0, WINED3DSP_WRITEMASK_0, &rel_param);
835 /* Removing a + 0 would be an obvious optimization, but macos doesn't see the NOP
836 * operation there
838 if(((IWineD3DPixelShaderImpl *) This)->input_reg_map[reg]) {
839 if (((IWineD3DPixelShaderImpl *)This)->declared_in_count > in_count) {
840 sprintf(tmpStr, "((%s + %u) > %d ? (%s + %u) > %d ? gl_SecondaryColor : gl_Color : IN[%s + %u])",
841 rel_param.param_str, ((IWineD3DPixelShaderImpl *)This)->input_reg_map[reg], in_count - 1,
842 rel_param.param_str, ((IWineD3DPixelShaderImpl *)This)->input_reg_map[reg], in_count,
843 rel_param.param_str, ((IWineD3DPixelShaderImpl *)This)->input_reg_map[reg]);
844 } else {
845 sprintf(tmpStr, "IN[%s + %u]", rel_param.param_str, ((IWineD3DPixelShaderImpl *)This)->input_reg_map[reg]);
847 } else {
848 if (((IWineD3DPixelShaderImpl *)This)->declared_in_count > in_count) {
849 sprintf(tmpStr, "((%s) > %d ? (%s) > %d ? gl_SecondaryColor : gl_Color : IN[%s])",
850 rel_param.param_str, in_count - 1,
851 rel_param.param_str, in_count,
852 rel_param.param_str);
853 } else {
854 sprintf(tmpStr, "IN[%s]", rel_param.param_str);
857 } else {
858 DWORD idx = ((IWineD3DPixelShaderImpl *) This)->input_reg_map[reg];
859 if (idx == in_count) {
860 sprintf(tmpStr, "gl_Color");
861 } else if (idx == in_count + 1) {
862 sprintf(tmpStr, "gl_SecondaryColor");
863 } else {
864 sprintf(tmpStr, "IN[%u]", idx);
867 } else {
868 if (reg==0)
869 strcpy(tmpStr, "gl_Color");
870 else
871 strcpy(tmpStr, "gl_SecondaryColor");
873 } else {
874 if (vshader_input_is_color((IWineD3DVertexShader*) This, reg))
875 *is_color = TRUE;
876 sprintf(tmpStr, "attrib%u", reg);
878 break;
879 case WINED3DSPR_CONST:
881 const char prefix = pshader? 'P':'V';
883 /* Relative addressing */
884 if (param & WINED3DSHADER_ADDRMODE_RELATIVE) {
886 /* Relative addressing on shaders 2.0+ have a relative address token,
887 * prior to that, it was hard-coded as "A0.x" because there's only 1 register */
888 if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 2) {
889 glsl_src_param_t rel_param;
890 shader_glsl_add_src_param(arg, addr_token, 0, WINED3DSP_WRITEMASK_0, &rel_param);
891 if(reg) {
892 sprintf(tmpStr, "%cC[%s + %u]", prefix, rel_param.param_str, reg);
893 } else {
894 sprintf(tmpStr, "%cC[%s]", prefix, rel_param.param_str);
896 } else {
897 if(reg) {
898 sprintf(tmpStr, "%cC[A0.x + %u]", prefix, reg);
899 } else {
900 sprintf(tmpStr, "%cC[A0.x]", prefix);
904 } else {
905 if(shader_constant_is_local(This, reg)) {
906 sprintf(tmpStr, "%cLC%u", prefix, reg);
907 } else {
908 sprintf(tmpStr, "%cC[%u]", prefix, reg);
912 break;
914 case WINED3DSPR_CONSTINT:
915 if (pshader)
916 sprintf(tmpStr, "PI[%u]", reg);
917 else
918 sprintf(tmpStr, "VI[%u]", reg);
919 break;
920 case WINED3DSPR_CONSTBOOL:
921 if (pshader)
922 sprintf(tmpStr, "PB[%u]", reg);
923 else
924 sprintf(tmpStr, "VB[%u]", reg);
925 break;
926 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
927 if (pshader) {
928 sprintf(tmpStr, "T%u", reg);
929 } else {
930 sprintf(tmpStr, "A%u", reg);
932 break;
933 case WINED3DSPR_LOOP:
934 sprintf(tmpStr, "aL%u", This->baseShader.cur_loop_regno - 1);
935 break;
936 case WINED3DSPR_SAMPLER:
937 if (pshader)
938 sprintf(tmpStr, "Psampler%u", reg);
939 else
940 sprintf(tmpStr, "Vsampler%u", reg);
941 break;
942 case WINED3DSPR_COLOROUT:
943 if (reg >= GL_LIMITS(buffers)) {
944 WARN("Write to render target %u, only %d supported\n", reg, 4);
946 if (GL_SUPPORT(ARB_DRAW_BUFFERS)) {
947 sprintf(tmpStr, "gl_FragData[%u]", reg);
948 } else { /* On older cards with GLSL support like the GeforceFX there's only one buffer. */
949 sprintf(tmpStr, "gl_FragColor");
951 break;
952 case WINED3DSPR_RASTOUT:
953 sprintf(tmpStr, "%s", hwrastout_reg_names[reg]);
954 break;
955 case WINED3DSPR_DEPTHOUT:
956 sprintf(tmpStr, "gl_FragDepth");
957 break;
958 case WINED3DSPR_ATTROUT:
959 if (reg == 0) {
960 sprintf(tmpStr, "gl_FrontColor");
961 } else {
962 sprintf(tmpStr, "gl_FrontSecondaryColor");
964 break;
965 case WINED3DSPR_TEXCRDOUT:
966 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
967 if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 3)
968 sprintf(tmpStr, "OUT[%u]", reg);
969 else
970 sprintf(tmpStr, "gl_TexCoord[%u]", reg);
971 break;
972 case WINED3DSPR_MISCTYPE:
973 if (reg == 0) {
974 /* vPos */
975 sprintf(tmpStr, "vpos");
976 } else if (reg == 1){
977 /* Note that gl_FrontFacing is a bool, while vFace is
978 * a float for which the sign determines front/back
980 sprintf(tmpStr, "(gl_FrontFacing ? 1.0 : -1.0)");
981 } else {
982 FIXME("Unhandled misctype register %d\n", reg);
983 sprintf(tmpStr, "unrecognized_register");
985 break;
986 default:
987 FIXME("Unhandled register name Type(%d)\n", regtype);
988 sprintf(tmpStr, "unrecognized_register");
989 break;
992 strcat(regstr, tmpStr);
995 /* Get the GLSL write mask for the destination register */
996 static DWORD shader_glsl_get_write_mask(const DWORD param, char *write_mask) {
997 char *ptr = write_mask;
998 DWORD mask = param & WINED3DSP_WRITEMASK_ALL;
1000 if (shader_is_scalar(param)) {
1001 mask = WINED3DSP_WRITEMASK_0;
1002 } else {
1003 *ptr++ = '.';
1004 if (param & WINED3DSP_WRITEMASK_0) *ptr++ = 'x';
1005 if (param & WINED3DSP_WRITEMASK_1) *ptr++ = 'y';
1006 if (param & WINED3DSP_WRITEMASK_2) *ptr++ = 'z';
1007 if (param & WINED3DSP_WRITEMASK_3) *ptr++ = 'w';
1010 *ptr = '\0';
1012 return mask;
1015 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask) {
1016 unsigned int size = 0;
1018 if (write_mask & WINED3DSP_WRITEMASK_0) ++size;
1019 if (write_mask & WINED3DSP_WRITEMASK_1) ++size;
1020 if (write_mask & WINED3DSP_WRITEMASK_2) ++size;
1021 if (write_mask & WINED3DSP_WRITEMASK_3) ++size;
1023 return size;
1026 static void shader_glsl_get_swizzle(const DWORD param, BOOL fixup, DWORD mask, char *swizzle_str) {
1027 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
1028 * but addressed as "rgba". To fix this we need to swap the register's x
1029 * and z components. */
1030 DWORD swizzle = (param & WINED3DSP_SWIZZLE_MASK) >> WINED3DSP_SWIZZLE_SHIFT;
1031 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
1032 char *ptr = swizzle_str;
1034 if (!shader_is_scalar(param)) {
1035 *ptr++ = '.';
1036 /* swizzle bits fields: wwzzyyxx */
1037 if (mask & WINED3DSP_WRITEMASK_0) *ptr++ = swizzle_chars[swizzle & 0x03];
1038 if (mask & WINED3DSP_WRITEMASK_1) *ptr++ = swizzle_chars[(swizzle >> 2) & 0x03];
1039 if (mask & WINED3DSP_WRITEMASK_2) *ptr++ = swizzle_chars[(swizzle >> 4) & 0x03];
1040 if (mask & WINED3DSP_WRITEMASK_3) *ptr++ = swizzle_chars[(swizzle >> 6) & 0x03];
1043 *ptr = '\0';
1046 /* From a given parameter token, generate the corresponding GLSL string.
1047 * Also, return the actual register name and swizzle in case the
1048 * caller needs this information as well. */
1049 static void shader_glsl_add_src_param(const SHADER_OPCODE_ARG *arg, const DWORD param,
1050 const DWORD addr_token, DWORD mask, glsl_src_param_t *src_param)
1052 BOOL is_color = FALSE;
1053 char swizzle_str[6];
1055 src_param->reg_name[0] = '\0';
1056 src_param->param_str[0] = '\0';
1057 swizzle_str[0] = '\0';
1059 shader_glsl_get_register_name(param, addr_token, src_param->reg_name, &is_color, arg);
1061 shader_glsl_get_swizzle(param, is_color, mask, swizzle_str);
1062 shader_glsl_gen_modifier(param, src_param->reg_name, swizzle_str, src_param->param_str);
1065 /* From a given parameter token, generate the corresponding GLSL string.
1066 * Also, return the actual register name and swizzle in case the
1067 * caller needs this information as well. */
1068 static DWORD shader_glsl_add_dst_param(const SHADER_OPCODE_ARG* arg, const DWORD param,
1069 const DWORD addr_token, glsl_dst_param_t *dst_param)
1071 BOOL is_color = FALSE;
1073 dst_param->mask_str[0] = '\0';
1074 dst_param->reg_name[0] = '\0';
1076 shader_glsl_get_register_name(param, addr_token, dst_param->reg_name, &is_color, arg);
1077 return shader_glsl_get_write_mask(param, dst_param->mask_str);
1080 /* Append the destination part of the instruction to the buffer, return the effective write mask */
1081 static DWORD shader_glsl_append_dst_ext(SHADER_BUFFER *buffer, const SHADER_OPCODE_ARG *arg, const DWORD param)
1083 glsl_dst_param_t dst_param;
1084 DWORD mask;
1085 int shift;
1087 mask = shader_glsl_add_dst_param(arg, param, arg->dst_addr, &dst_param);
1089 if(mask) {
1090 shift = (param & WINED3DSP_DSTSHIFT_MASK) >> WINED3DSP_DSTSHIFT_SHIFT;
1091 shader_addline(buffer, "%s%s = %s(", dst_param.reg_name, dst_param.mask_str, shift_glsl_tab[shift]);
1094 return mask;
1097 /* Append the destination part of the instruction to the buffer, return the effective write mask */
1098 static DWORD shader_glsl_append_dst(SHADER_BUFFER *buffer, const SHADER_OPCODE_ARG *arg)
1100 return shader_glsl_append_dst_ext(buffer, arg, arg->dst);
1103 /** Process GLSL instruction modifiers */
1104 void shader_glsl_add_instruction_modifiers(const SHADER_OPCODE_ARG* arg)
1106 DWORD mask = arg->dst & WINED3DSP_DSTMOD_MASK;
1108 if (arg->opcode->dst_token && mask != 0) {
1109 glsl_dst_param_t dst_param;
1111 shader_glsl_add_dst_param(arg, arg->dst, 0, &dst_param);
1113 if (mask & WINED3DSPDM_SATURATE) {
1114 /* _SAT means to clamp the value of the register to between 0 and 1 */
1115 shader_addline(arg->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param.reg_name,
1116 dst_param.mask_str, dst_param.reg_name, dst_param.mask_str);
1118 if (mask & WINED3DSPDM_MSAMPCENTROID) {
1119 FIXME("_centroid modifier not handled\n");
1121 if (mask & WINED3DSPDM_PARTIALPRECISION) {
1122 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
1127 static inline const char* shader_get_comp_op(
1128 const DWORD opcode) {
1130 DWORD op = (opcode & INST_CONTROLS_MASK) >> INST_CONTROLS_SHIFT;
1131 switch (op) {
1132 case COMPARISON_GT: return ">";
1133 case COMPARISON_EQ: return "==";
1134 case COMPARISON_GE: return ">=";
1135 case COMPARISON_LT: return "<";
1136 case COMPARISON_NE: return "!=";
1137 case COMPARISON_LE: return "<=";
1138 default:
1139 FIXME("Unrecognized comparison value: %u\n", op);
1140 return "(\?\?)";
1144 static void shader_glsl_get_sample_function(DWORD sampler_type, BOOL projected, BOOL texrect, glsl_sample_function_t *sample_function) {
1145 /* Note that there's no such thing as a projected cube texture. */
1146 switch(sampler_type) {
1147 case WINED3DSTT_1D:
1148 sample_function->name = projected ? "texture1DProj" : "texture1D";
1149 sample_function->coord_mask = WINED3DSP_WRITEMASK_0;
1150 break;
1151 case WINED3DSTT_2D:
1152 if(texrect) {
1153 sample_function->name = projected ? "texture2DRectProj" : "texture2DRect";
1154 } else {
1155 sample_function->name = projected ? "texture2DProj" : "texture2D";
1157 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
1158 break;
1159 case WINED3DSTT_CUBE:
1160 sample_function->name = "textureCube";
1161 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1162 break;
1163 case WINED3DSTT_VOLUME:
1164 sample_function->name = projected ? "texture3DProj" : "texture3D";
1165 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1166 break;
1167 default:
1168 sample_function->name = "";
1169 sample_function->coord_mask = 0;
1170 FIXME("Unrecognized sampler type: %#x;\n", sampler_type);
1171 break;
1175 static void shader_glsl_append_fixup_arg(char *arguments, const char *reg_name,
1176 BOOL sign_fixup, enum fixup_channel_source channel_source)
1178 switch(channel_source)
1180 case CHANNEL_SOURCE_ZERO:
1181 strcat(arguments, "0.0");
1182 break;
1184 case CHANNEL_SOURCE_ONE:
1185 strcat(arguments, "1.0");
1186 break;
1188 case CHANNEL_SOURCE_X:
1189 strcat(arguments, reg_name);
1190 strcat(arguments, ".x");
1191 break;
1193 case CHANNEL_SOURCE_Y:
1194 strcat(arguments, reg_name);
1195 strcat(arguments, ".y");
1196 break;
1198 case CHANNEL_SOURCE_Z:
1199 strcat(arguments, reg_name);
1200 strcat(arguments, ".z");
1201 break;
1203 case CHANNEL_SOURCE_W:
1204 strcat(arguments, reg_name);
1205 strcat(arguments, ".w");
1206 break;
1208 default:
1209 FIXME("Unhandled channel source %#x\n", channel_source);
1210 strcat(arguments, "undefined");
1211 break;
1214 if (sign_fixup) strcat(arguments, " * 2.0 - 1.0");
1217 static void shader_glsl_color_correction(const struct SHADER_OPCODE_ARG *arg, struct color_fixup_desc fixup)
1219 unsigned int mask_size, remaining;
1220 glsl_dst_param_t dst_param;
1221 char arguments[256];
1222 DWORD mask;
1223 BOOL dummy;
1225 mask = 0;
1226 if (fixup.x_sign_fixup || fixup.x_source != CHANNEL_SOURCE_X) mask |= WINED3DSP_WRITEMASK_0;
1227 if (fixup.y_sign_fixup || fixup.y_source != CHANNEL_SOURCE_Y) mask |= WINED3DSP_WRITEMASK_1;
1228 if (fixup.z_sign_fixup || fixup.z_source != CHANNEL_SOURCE_Z) mask |= WINED3DSP_WRITEMASK_2;
1229 if (fixup.w_sign_fixup || fixup.w_source != CHANNEL_SOURCE_W) mask |= WINED3DSP_WRITEMASK_3;
1230 mask &= arg->dst;
1232 if (!mask) return; /* Nothing to do */
1234 if (is_yuv_fixup(fixup))
1236 enum yuv_fixup yuv_fixup = get_yuv_fixup(fixup);
1237 FIXME("YUV fixup (%#x) not supported\n", yuv_fixup);
1238 return;
1241 mask_size = shader_glsl_get_write_mask_size(mask);
1243 dst_param.mask_str[0] = '\0';
1244 shader_glsl_get_write_mask(mask, dst_param.mask_str);
1246 dst_param.reg_name[0] = '\0';
1247 shader_glsl_get_register_name(arg->dst, arg->dst_addr, dst_param.reg_name, &dummy, arg);
1249 arguments[0] = '\0';
1250 remaining = mask_size;
1251 if (mask & WINED3DSP_WRITEMASK_0)
1253 shader_glsl_append_fixup_arg(arguments, dst_param.reg_name, fixup.x_sign_fixup, fixup.x_source);
1254 if (--remaining) strcat(arguments, ", ");
1256 if (mask & WINED3DSP_WRITEMASK_1)
1258 shader_glsl_append_fixup_arg(arguments, dst_param.reg_name, fixup.y_sign_fixup, fixup.y_source);
1259 if (--remaining) strcat(arguments, ", ");
1261 if (mask & WINED3DSP_WRITEMASK_2)
1263 shader_glsl_append_fixup_arg(arguments, dst_param.reg_name, fixup.z_sign_fixup, fixup.z_source);
1264 if (--remaining) strcat(arguments, ", ");
1266 if (mask & WINED3DSP_WRITEMASK_3)
1268 shader_glsl_append_fixup_arg(arguments, dst_param.reg_name, fixup.w_sign_fixup, fixup.w_source);
1269 if (--remaining) strcat(arguments, ", ");
1272 if (mask_size > 1)
1274 shader_addline(arg->buffer, "%s%s = vec%u(%s);\n",
1275 dst_param.reg_name, dst_param.mask_str, mask_size, arguments);
1277 else
1279 shader_addline(arg->buffer, "%s%s = %s;\n", dst_param.reg_name, dst_param.mask_str, arguments);
1283 /*****************************************************************************
1285 * Begin processing individual instruction opcodes
1287 ****************************************************************************/
1289 /* Generate GLSL arithmetic functions (dst = src1 + src2) */
1290 static void shader_glsl_arith(const SHADER_OPCODE_ARG *arg)
1292 CONST SHADER_OPCODE* curOpcode = arg->opcode;
1293 SHADER_BUFFER* buffer = arg->buffer;
1294 glsl_src_param_t src0_param;
1295 glsl_src_param_t src1_param;
1296 DWORD write_mask;
1297 char op;
1299 /* Determine the GLSL operator to use based on the opcode */
1300 switch (curOpcode->opcode) {
1301 case WINED3DSIO_MUL: op = '*'; break;
1302 case WINED3DSIO_ADD: op = '+'; break;
1303 case WINED3DSIO_SUB: op = '-'; break;
1304 default:
1305 op = ' ';
1306 FIXME("Opcode %s not yet handled in GLSL\n", curOpcode->name);
1307 break;
1310 write_mask = shader_glsl_append_dst(buffer, arg);
1311 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src0_param);
1312 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1313 shader_addline(buffer, "%s %c %s);\n", src0_param.param_str, op, src1_param.param_str);
1316 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
1317 static void shader_glsl_mov(const SHADER_OPCODE_ARG *arg)
1319 IWineD3DBaseShaderImpl* shader = (IWineD3DBaseShaderImpl*) arg->shader;
1320 SHADER_BUFFER* buffer = arg->buffer;
1321 glsl_src_param_t src0_param;
1322 DWORD write_mask;
1324 write_mask = shader_glsl_append_dst(buffer, arg);
1325 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src0_param);
1327 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
1328 * shader versions WINED3DSIO_MOVA is used for this. */
1329 if ((WINED3DSHADER_VERSION_MAJOR(shader->baseShader.hex_version) == 1 &&
1330 !shader_is_pshader_version(shader->baseShader.hex_version) &&
1331 shader_get_regtype(arg->dst) == WINED3DSPR_ADDR)) {
1332 /* This is a simple floor() */
1333 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
1334 if (mask_size > 1) {
1335 shader_addline(buffer, "ivec%d(floor(%s)));\n", mask_size, src0_param.param_str);
1336 } else {
1337 shader_addline(buffer, "int(floor(%s)));\n", src0_param.param_str);
1339 } else if(arg->opcode->opcode == WINED3DSIO_MOVA) {
1340 /* We need to *round* to the nearest int here. */
1341 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
1342 if (mask_size > 1) {
1343 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);
1344 } else {
1345 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n", src0_param.param_str, src0_param.param_str);
1347 } else {
1348 shader_addline(buffer, "%s);\n", src0_param.param_str);
1352 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
1353 static void shader_glsl_dot(const SHADER_OPCODE_ARG *arg)
1355 CONST SHADER_OPCODE* curOpcode = arg->opcode;
1356 SHADER_BUFFER* buffer = arg->buffer;
1357 glsl_src_param_t src0_param;
1358 glsl_src_param_t src1_param;
1359 DWORD dst_write_mask, src_write_mask;
1360 unsigned int dst_size = 0;
1362 dst_write_mask = shader_glsl_append_dst(buffer, arg);
1363 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
1365 /* dp3 works on vec3, dp4 on vec4 */
1366 if (curOpcode->opcode == WINED3DSIO_DP4) {
1367 src_write_mask = WINED3DSP_WRITEMASK_ALL;
1368 } else {
1369 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1372 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_write_mask, &src0_param);
1373 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], src_write_mask, &src1_param);
1375 if (dst_size > 1) {
1376 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
1377 } else {
1378 shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
1382 /* Note that this instruction has some restrictions. The destination write mask
1383 * can't contain the w component, and the source swizzles have to be .xyzw */
1384 static void shader_glsl_cross(const SHADER_OPCODE_ARG *arg)
1386 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1387 glsl_src_param_t src0_param;
1388 glsl_src_param_t src1_param;
1389 char dst_mask[6];
1391 shader_glsl_get_write_mask(arg->dst, dst_mask);
1392 shader_glsl_append_dst(arg->buffer, arg);
1393 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
1394 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], src_mask, &src1_param);
1395 shader_addline(arg->buffer, "cross(%s, %s)%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
1398 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
1399 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
1400 * GLSL uses the value as-is. */
1401 static void shader_glsl_pow(const SHADER_OPCODE_ARG *arg)
1403 SHADER_BUFFER *buffer = arg->buffer;
1404 glsl_src_param_t src0_param;
1405 glsl_src_param_t src1_param;
1406 DWORD dst_write_mask;
1407 unsigned int dst_size;
1409 dst_write_mask = shader_glsl_append_dst(buffer, arg);
1410 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
1412 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1413 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0, &src1_param);
1415 if (dst_size > 1) {
1416 shader_addline(buffer, "vec%d(pow(abs(%s), %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
1417 } else {
1418 shader_addline(buffer, "pow(abs(%s), %s));\n", src0_param.param_str, src1_param.param_str);
1422 /* Process the WINED3DSIO_LOG instruction in GLSL (dst = log2(|src0|))
1423 * Src0 is a scalar. Note that D3D uses the absolute of src0, while
1424 * GLSL uses the value as-is. */
1425 static void shader_glsl_log(const SHADER_OPCODE_ARG *arg)
1427 SHADER_BUFFER *buffer = arg->buffer;
1428 glsl_src_param_t src0_param;
1429 DWORD dst_write_mask;
1430 unsigned int dst_size;
1432 dst_write_mask = shader_glsl_append_dst(buffer, arg);
1433 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
1435 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1437 if (dst_size > 1) {
1438 shader_addline(buffer, "vec%d(log2(abs(%s))));\n", dst_size, src0_param.param_str);
1439 } else {
1440 shader_addline(buffer, "log2(abs(%s)));\n", src0_param.param_str);
1444 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
1445 static void shader_glsl_map2gl(const SHADER_OPCODE_ARG *arg)
1447 CONST SHADER_OPCODE* curOpcode = arg->opcode;
1448 SHADER_BUFFER* buffer = arg->buffer;
1449 glsl_src_param_t src_param;
1450 const char *instruction;
1451 char arguments[256];
1452 DWORD write_mask;
1453 unsigned i;
1455 /* Determine the GLSL function to use based on the opcode */
1456 /* TODO: Possibly make this a table for faster lookups */
1457 switch (curOpcode->opcode) {
1458 case WINED3DSIO_MIN: instruction = "min"; break;
1459 case WINED3DSIO_MAX: instruction = "max"; break;
1460 case WINED3DSIO_ABS: instruction = "abs"; break;
1461 case WINED3DSIO_FRC: instruction = "fract"; break;
1462 case WINED3DSIO_NRM: instruction = "normalize"; break;
1463 case WINED3DSIO_EXP: instruction = "exp2"; break;
1464 case WINED3DSIO_SGN: instruction = "sign"; break;
1465 case WINED3DSIO_DSX: instruction = "dFdx"; break;
1466 case WINED3DSIO_DSY: instruction = "ycorrection.y * dFdy"; break;
1467 default: instruction = "";
1468 FIXME("Opcode %s not yet handled in GLSL\n", curOpcode->name);
1469 break;
1472 write_mask = shader_glsl_append_dst(buffer, arg);
1474 arguments[0] = '\0';
1475 if (curOpcode->num_params > 0) {
1476 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src_param);
1477 strcat(arguments, src_param.param_str);
1478 for (i = 2; i < curOpcode->num_params; ++i) {
1479 strcat(arguments, ", ");
1480 shader_glsl_add_src_param(arg, arg->src[i-1], arg->src_addr[i-1], write_mask, &src_param);
1481 strcat(arguments, src_param.param_str);
1485 shader_addline(buffer, "%s(%s));\n", instruction, arguments);
1488 /** Process the WINED3DSIO_EXPP instruction in GLSL:
1489 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
1490 * dst.x = 2^(floor(src))
1491 * dst.y = src - floor(src)
1492 * dst.z = 2^src (partial precision is allowed, but optional)
1493 * dst.w = 1.0;
1494 * For 2.0 shaders, just do this (honoring writemask and swizzle):
1495 * dst = 2^src; (partial precision is allowed, but optional)
1497 static void shader_glsl_expp(const SHADER_OPCODE_ARG *arg)
1499 IWineD3DBaseShaderImpl *shader = (IWineD3DBaseShaderImpl *)arg->shader;
1500 glsl_src_param_t src_param;
1502 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src_param);
1504 if (shader->baseShader.hex_version < WINED3DPS_VERSION(2,0)) {
1505 char dst_mask[6];
1507 shader_addline(arg->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
1508 shader_addline(arg->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
1509 shader_addline(arg->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
1510 shader_addline(arg->buffer, "tmp0.w = 1.0;\n");
1512 shader_glsl_append_dst(arg->buffer, arg);
1513 shader_glsl_get_write_mask(arg->dst, dst_mask);
1514 shader_addline(arg->buffer, "tmp0%s);\n", dst_mask);
1515 } else {
1516 DWORD write_mask;
1517 unsigned int mask_size;
1519 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1520 mask_size = shader_glsl_get_write_mask_size(write_mask);
1522 if (mask_size > 1) {
1523 shader_addline(arg->buffer, "vec%d(exp2(%s)));\n", mask_size, src_param.param_str);
1524 } else {
1525 shader_addline(arg->buffer, "exp2(%s));\n", src_param.param_str);
1530 /** Process the RCP (reciprocal or inverse) opcode in GLSL (dst = 1 / src) */
1531 static void shader_glsl_rcp(const SHADER_OPCODE_ARG *arg)
1533 glsl_src_param_t src_param;
1534 DWORD write_mask;
1535 unsigned int mask_size;
1537 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1538 mask_size = shader_glsl_get_write_mask_size(write_mask);
1539 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_3, &src_param);
1541 if (mask_size > 1) {
1542 shader_addline(arg->buffer, "vec%d(1.0 / %s));\n", mask_size, src_param.param_str);
1543 } else {
1544 shader_addline(arg->buffer, "1.0 / %s);\n", src_param.param_str);
1548 static void shader_glsl_rsq(const SHADER_OPCODE_ARG *arg)
1550 SHADER_BUFFER* buffer = arg->buffer;
1551 glsl_src_param_t src_param;
1552 DWORD write_mask;
1553 unsigned int mask_size;
1555 write_mask = shader_glsl_append_dst(buffer, arg);
1556 mask_size = shader_glsl_get_write_mask_size(write_mask);
1558 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_3, &src_param);
1560 if (mask_size > 1) {
1561 shader_addline(buffer, "vec%d(inversesqrt(%s)));\n", mask_size, src_param.param_str);
1562 } else {
1563 shader_addline(buffer, "inversesqrt(%s));\n", src_param.param_str);
1567 /** Process signed comparison opcodes in GLSL. */
1568 static void shader_glsl_compare(const SHADER_OPCODE_ARG *arg)
1570 glsl_src_param_t src0_param;
1571 glsl_src_param_t src1_param;
1572 DWORD write_mask;
1573 unsigned int mask_size;
1575 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1576 mask_size = shader_glsl_get_write_mask_size(write_mask);
1577 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src0_param);
1578 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1580 if (mask_size > 1) {
1581 const char *compare;
1583 switch(arg->opcode->opcode) {
1584 case WINED3DSIO_SLT: compare = "lessThan"; break;
1585 case WINED3DSIO_SGE: compare = "greaterThanEqual"; break;
1586 default: compare = "";
1587 FIXME("Can't handle opcode %s\n", arg->opcode->name);
1590 shader_addline(arg->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
1591 src0_param.param_str, src1_param.param_str);
1592 } else {
1593 switch(arg->opcode->opcode) {
1594 case WINED3DSIO_SLT:
1595 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
1596 * to return 0.0 but step returns 1.0 because step is not < x
1597 * An alternative is a bvec compare padded with an unused second component.
1598 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
1599 * issue. Playing with not() is not possible either because not() does not accept
1600 * a scalar.
1602 shader_addline(arg->buffer, "(%s < %s) ? 1.0 : 0.0);\n", src0_param.param_str, src1_param.param_str);
1603 break;
1604 case WINED3DSIO_SGE:
1605 /* Here we can use the step() function and safe a conditional */
1606 shader_addline(arg->buffer, "step(%s, %s));\n", src1_param.param_str, src0_param.param_str);
1607 break;
1608 default:
1609 FIXME("Can't handle opcode %s\n", arg->opcode->name);
1615 /** Process CMP instruction in GLSL (dst = src0 >= 0.0 ? src1 : src2), per channel */
1616 static void shader_glsl_cmp(const SHADER_OPCODE_ARG *arg)
1618 glsl_src_param_t src0_param;
1619 glsl_src_param_t src1_param;
1620 glsl_src_param_t src2_param;
1621 DWORD write_mask, cmp_channel = 0;
1622 unsigned int i, j;
1623 char mask_char[6];
1624 BOOL temp_destination = FALSE;
1626 if(shader_is_scalar(arg->src[0])) {
1627 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1629 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
1630 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1631 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, &src2_param);
1633 shader_addline(arg->buffer, "%s >= 0.0 ? %s : %s);\n",
1634 src0_param.param_str, src1_param.param_str, src2_param.param_str);
1635 } else {
1636 DWORD src0reg = arg->src[0] & WINED3DSP_REGNUM_MASK;
1637 DWORD src1reg = arg->src[1] & WINED3DSP_REGNUM_MASK;
1638 DWORD src2reg = arg->src[2] & WINED3DSP_REGNUM_MASK;
1639 DWORD src0regtype = shader_get_regtype(arg->src[0]);
1640 DWORD src1regtype = shader_get_regtype(arg->src[1]);
1641 DWORD src2regtype = shader_get_regtype(arg->src[2]);
1642 DWORD dstreg = arg->dst & WINED3DSP_REGNUM_MASK;
1643 DWORD dstregtype = shader_get_regtype(arg->dst);
1645 /* Cycle through all source0 channels */
1646 for (i=0; i<4; i++) {
1647 write_mask = 0;
1648 /* Find the destination channels which use the current source0 channel */
1649 for (j=0; j<4; j++) {
1650 if ( ((arg->src[0] >> (WINED3DSP_SWIZZLE_SHIFT + 2*j)) & 0x3) == i ) {
1651 write_mask |= WINED3DSP_WRITEMASK_0 << j;
1652 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
1656 /* Splitting the cmp instruction up in multiple lines imposes a problem:
1657 * The first lines may overwrite source parameters of the following lines.
1658 * Deal with that by using a temporary destination register if needed
1660 if((src0reg == dstreg && src0regtype == dstregtype) ||
1661 (src1reg == dstreg && src1regtype == dstregtype) ||
1662 (src2reg == dstreg && src2regtype == dstregtype)) {
1664 write_mask = shader_glsl_get_write_mask(arg->dst & (~WINED3DSP_SWIZZLE_MASK | write_mask), mask_char);
1665 if (!write_mask) continue;
1666 shader_addline(arg->buffer, "tmp0%s = (", mask_char);
1667 temp_destination = TRUE;
1668 } else {
1669 write_mask = shader_glsl_append_dst_ext(arg->buffer, arg, arg->dst & (~WINED3DSP_SWIZZLE_MASK | write_mask));
1670 if (!write_mask) continue;
1673 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], cmp_channel, &src0_param);
1674 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1675 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, &src2_param);
1677 shader_addline(arg->buffer, "%s >= 0.0 ? %s : %s);\n",
1678 src0_param.param_str, src1_param.param_str, src2_param.param_str);
1681 if(temp_destination) {
1682 shader_glsl_get_write_mask(arg->dst, mask_char);
1683 shader_glsl_append_dst_ext(arg->buffer, arg, arg->dst);
1684 shader_addline(arg->buffer, "tmp0%s);\n", mask_char);
1690 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
1691 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
1692 * the compare is done per component of src0. */
1693 static void shader_glsl_cnd(const SHADER_OPCODE_ARG *arg)
1695 IWineD3DBaseShaderImpl* shader = (IWineD3DBaseShaderImpl*) arg->shader;
1696 glsl_src_param_t src0_param;
1697 glsl_src_param_t src1_param;
1698 glsl_src_param_t src2_param;
1699 DWORD write_mask, cmp_channel = 0;
1700 unsigned int i, j;
1702 if (shader->baseShader.hex_version < WINED3DPS_VERSION(1, 4)) {
1703 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1704 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1705 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1706 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, &src2_param);
1708 /* Fun: The D3DSI_COISSUE flag changes the semantic of the cnd instruction for < 1.4 shaders */
1709 if(arg->opcode_token & WINED3DSI_COISSUE) {
1710 shader_addline(arg->buffer, "%s /* COISSUE! */);\n", src1_param.param_str);
1711 } else {
1712 shader_addline(arg->buffer, "%s > 0.5 ? %s : %s);\n",
1713 src0_param.param_str, src1_param.param_str, src2_param.param_str);
1715 return;
1717 /* Cycle through all source0 channels */
1718 for (i=0; i<4; i++) {
1719 write_mask = 0;
1720 /* Find the destination channels which use the current source0 channel */
1721 for (j=0; j<4; j++) {
1722 if ( ((arg->src[0] >> (WINED3DSP_SWIZZLE_SHIFT + 2*j)) & 0x3) == i ) {
1723 write_mask |= WINED3DSP_WRITEMASK_0 << j;
1724 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
1727 write_mask = shader_glsl_append_dst_ext(arg->buffer, arg, arg->dst & (~WINED3DSP_SWIZZLE_MASK | write_mask));
1728 if (!write_mask) continue;
1730 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], cmp_channel, &src0_param);
1731 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1732 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, &src2_param);
1734 shader_addline(arg->buffer, "%s > 0.5 ? %s : %s);\n",
1735 src0_param.param_str, src1_param.param_str, src2_param.param_str);
1739 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
1740 static void shader_glsl_mad(const SHADER_OPCODE_ARG *arg)
1742 glsl_src_param_t src0_param;
1743 glsl_src_param_t src1_param;
1744 glsl_src_param_t src2_param;
1745 DWORD write_mask;
1747 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1748 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src0_param);
1749 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1750 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, &src2_param);
1751 shader_addline(arg->buffer, "(%s * %s) + %s);\n",
1752 src0_param.param_str, src1_param.param_str, src2_param.param_str);
1755 /** Handles transforming all WINED3DSIO_M?x? opcodes for
1756 Vertex shaders to GLSL codes */
1757 static void shader_glsl_mnxn(const SHADER_OPCODE_ARG *arg)
1759 IWineD3DBaseShaderImpl *shader = (IWineD3DBaseShaderImpl *)arg->shader;
1760 const SHADER_OPCODE *opcode_table = shader->baseShader.shader_ins;
1761 DWORD shader_version = shader->baseShader.hex_version;
1762 int i;
1763 int nComponents = 0;
1764 SHADER_OPCODE_ARG tmpArg;
1766 memset(&tmpArg, 0, sizeof(SHADER_OPCODE_ARG));
1768 /* Set constants for the temporary argument */
1769 tmpArg.shader = arg->shader;
1770 tmpArg.buffer = arg->buffer;
1771 tmpArg.src[0] = arg->src[0];
1772 tmpArg.src_addr[0] = arg->src_addr[0];
1773 tmpArg.src_addr[1] = arg->src_addr[1];
1774 tmpArg.reg_maps = arg->reg_maps;
1776 switch(arg->opcode->opcode) {
1777 case WINED3DSIO_M4x4:
1778 nComponents = 4;
1779 tmpArg.opcode = shader_get_opcode(opcode_table, shader_version, WINED3DSIO_DP4);
1780 break;
1781 case WINED3DSIO_M4x3:
1782 nComponents = 3;
1783 tmpArg.opcode = shader_get_opcode(opcode_table, shader_version, WINED3DSIO_DP4);
1784 break;
1785 case WINED3DSIO_M3x4:
1786 nComponents = 4;
1787 tmpArg.opcode = shader_get_opcode(opcode_table, shader_version, WINED3DSIO_DP3);
1788 break;
1789 case WINED3DSIO_M3x3:
1790 nComponents = 3;
1791 tmpArg.opcode = shader_get_opcode(opcode_table, shader_version, WINED3DSIO_DP3);
1792 break;
1793 case WINED3DSIO_M3x2:
1794 nComponents = 2;
1795 tmpArg.opcode = shader_get_opcode(opcode_table, shader_version, WINED3DSIO_DP3);
1796 break;
1797 default:
1798 break;
1801 for (i = 0; i < nComponents; i++) {
1802 tmpArg.dst = ((arg->dst) & ~WINED3DSP_WRITEMASK_ALL)|(WINED3DSP_WRITEMASK_0<<i);
1803 tmpArg.src[1] = arg->src[1]+i;
1804 shader_glsl_dot(&tmpArg);
1809 The LRP instruction performs a component-wise linear interpolation
1810 between the second and third operands using the first operand as the
1811 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
1812 This is equivalent to mix(src2, src1, src0);
1814 static void shader_glsl_lrp(const SHADER_OPCODE_ARG *arg)
1816 glsl_src_param_t src0_param;
1817 glsl_src_param_t src1_param;
1818 glsl_src_param_t src2_param;
1819 DWORD write_mask;
1821 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1823 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src0_param);
1824 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1825 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, &src2_param);
1827 shader_addline(arg->buffer, "mix(%s, %s, %s));\n",
1828 src2_param.param_str, src1_param.param_str, src0_param.param_str);
1831 /** Process the WINED3DSIO_LIT instruction in GLSL:
1832 * dst.x = dst.w = 1.0
1833 * dst.y = (src0.x > 0) ? src0.x
1834 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
1835 * where src.w is clamped at +- 128
1837 static void shader_glsl_lit(const SHADER_OPCODE_ARG *arg)
1839 glsl_src_param_t src0_param;
1840 glsl_src_param_t src1_param;
1841 glsl_src_param_t src3_param;
1842 char dst_mask[6];
1844 shader_glsl_append_dst(arg->buffer, arg);
1845 shader_glsl_get_write_mask(arg->dst, dst_mask);
1847 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1848 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_1, &src1_param);
1849 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_3, &src3_param);
1851 /* The sdk specifies the instruction like this
1852 * dst.x = 1.0;
1853 * if(src.x > 0.0) dst.y = src.x
1854 * else dst.y = 0.0.
1855 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
1856 * else dst.z = 0.0;
1857 * dst.w = 1.0;
1859 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
1860 * dst.x = 1.0 ... No further explanation needed
1861 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
1862 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
1863 * dst.w = 1.0. ... Nothing fancy.
1865 * So we still have one conditional in there. So do this:
1866 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
1868 * 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),
1869 * 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.
1870 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to
1872 shader_addline(arg->buffer, "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",
1873 src0_param.param_str, src1_param.param_str, src0_param.param_str, src3_param.param_str, dst_mask);
1876 /** Process the WINED3DSIO_DST instruction in GLSL:
1877 * dst.x = 1.0
1878 * dst.y = src0.x * src0.y
1879 * dst.z = src0.z
1880 * dst.w = src1.w
1882 static void shader_glsl_dst(const SHADER_OPCODE_ARG *arg)
1884 glsl_src_param_t src0y_param;
1885 glsl_src_param_t src0z_param;
1886 glsl_src_param_t src1y_param;
1887 glsl_src_param_t src1w_param;
1888 char dst_mask[6];
1890 shader_glsl_append_dst(arg->buffer, arg);
1891 shader_glsl_get_write_mask(arg->dst, dst_mask);
1893 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_1, &src0y_param);
1894 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_2, &src0z_param);
1895 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_1, &src1y_param);
1896 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_3, &src1w_param);
1898 shader_addline(arg->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
1899 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
1902 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
1903 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
1904 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
1906 * dst.x = cos(src0.?)
1907 * dst.y = sin(src0.?)
1908 * dst.z = dst.z
1909 * dst.w = dst.w
1911 static void shader_glsl_sincos(const SHADER_OPCODE_ARG *arg)
1913 glsl_src_param_t src0_param;
1914 DWORD write_mask;
1916 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1917 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1919 switch (write_mask) {
1920 case WINED3DSP_WRITEMASK_0:
1921 shader_addline(arg->buffer, "cos(%s));\n", src0_param.param_str);
1922 break;
1924 case WINED3DSP_WRITEMASK_1:
1925 shader_addline(arg->buffer, "sin(%s));\n", src0_param.param_str);
1926 break;
1928 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
1929 shader_addline(arg->buffer, "vec2(cos(%s), sin(%s)));\n", src0_param.param_str, src0_param.param_str);
1930 break;
1932 default:
1933 ERR("Write mask should be .x, .y or .xy\n");
1934 break;
1938 /** Process the WINED3DSIO_LOOP instruction in GLSL:
1939 * Start a for() loop where src1.y is the initial value of aL,
1940 * increment aL by src1.z for a total of src1.x iterations.
1941 * Need to use a temporary variable for this operation.
1943 /* FIXME: I don't think nested loops will work correctly this way. */
1944 static void shader_glsl_loop(const SHADER_OPCODE_ARG *arg)
1946 glsl_src_param_t src1_param;
1947 IWineD3DBaseShaderImpl* shader = (IWineD3DBaseShaderImpl*) arg->shader;
1948 DWORD regtype = shader_get_regtype(arg->src[1]);
1949 DWORD reg = arg->src[1] & WINED3DSP_REGNUM_MASK;
1950 const DWORD *control_values = NULL;
1951 const local_constant *constant;
1953 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_ALL, &src1_param);
1955 /* Try to hardcode the loop control parameters if possible. Direct3D 9 class hardware doesn't support real
1956 * varying indexing, but Microsoft designed this feature for Shader model 2.x+. If the loop control is
1957 * known at compile time, the GLSL compiler can unroll the loop, and replace indirect addressing with direct
1958 * addressing.
1960 if(regtype == WINED3DSPR_CONSTINT) {
1961 LIST_FOR_EACH_ENTRY(constant, &shader->baseShader.constantsI, local_constant, entry) {
1962 if(constant->idx == reg) {
1963 control_values = constant->value;
1964 break;
1969 if(control_values) {
1970 if(control_values[2] > 0) {
1971 shader_addline(arg->buffer, "for (aL%u = %d; aL%u < (%d * %d + %d); aL%u += %d) {\n",
1972 shader->baseShader.cur_loop_depth, control_values[1],
1973 shader->baseShader.cur_loop_depth, control_values[0], control_values[2], control_values[1],
1974 shader->baseShader.cur_loop_depth, control_values[2]);
1975 } else if(control_values[2] == 0) {
1976 shader_addline(arg->buffer, "for (aL%u = %d, tmpInt%u = 0; tmpInt%u < %d; tmpInt%u++) {\n",
1977 shader->baseShader.cur_loop_depth, control_values[1], shader->baseShader.cur_loop_depth,
1978 shader->baseShader.cur_loop_depth, control_values[0],
1979 shader->baseShader.cur_loop_depth);
1980 } else {
1981 shader_addline(arg->buffer, "for (aL%u = %d; aL%u > (%d * %d + %d); aL%u += %d) {\n",
1982 shader->baseShader.cur_loop_depth, control_values[1],
1983 shader->baseShader.cur_loop_depth, control_values[0], control_values[2], control_values[1],
1984 shader->baseShader.cur_loop_depth, control_values[2]);
1986 } else {
1987 shader_addline(arg->buffer, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z) {\n",
1988 shader->baseShader.cur_loop_depth, shader->baseShader.cur_loop_regno,
1989 src1_param.reg_name, shader->baseShader.cur_loop_depth, src1_param.reg_name,
1990 shader->baseShader.cur_loop_depth, shader->baseShader.cur_loop_regno, src1_param.reg_name);
1993 shader->baseShader.cur_loop_depth++;
1994 shader->baseShader.cur_loop_regno++;
1997 static void shader_glsl_end(const SHADER_OPCODE_ARG *arg)
1999 IWineD3DBaseShaderImpl* shader = (IWineD3DBaseShaderImpl*) arg->shader;
2001 shader_addline(arg->buffer, "}\n");
2003 if(arg->opcode->opcode == WINED3DSIO_ENDLOOP) {
2004 shader->baseShader.cur_loop_depth--;
2005 shader->baseShader.cur_loop_regno--;
2007 if(arg->opcode->opcode == WINED3DSIO_ENDREP) {
2008 shader->baseShader.cur_loop_depth--;
2012 static void shader_glsl_rep(const SHADER_OPCODE_ARG *arg)
2014 IWineD3DBaseShaderImpl* shader = (IWineD3DBaseShaderImpl*) arg->shader;
2015 glsl_src_param_t src0_param;
2017 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
2018 shader_addline(arg->buffer, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
2019 shader->baseShader.cur_loop_depth, shader->baseShader.cur_loop_depth,
2020 src0_param.param_str, shader->baseShader.cur_loop_depth);
2021 shader->baseShader.cur_loop_depth++;
2024 static void shader_glsl_if(const SHADER_OPCODE_ARG *arg)
2026 glsl_src_param_t src0_param;
2028 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
2029 shader_addline(arg->buffer, "if (%s) {\n", src0_param.param_str);
2032 static void shader_glsl_ifc(const SHADER_OPCODE_ARG *arg)
2034 glsl_src_param_t src0_param;
2035 glsl_src_param_t src1_param;
2037 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
2038 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0, &src1_param);
2040 shader_addline(arg->buffer, "if (%s %s %s) {\n",
2041 src0_param.param_str, shader_get_comp_op(arg->opcode_token), src1_param.param_str);
2044 static void shader_glsl_else(const SHADER_OPCODE_ARG *arg)
2046 shader_addline(arg->buffer, "} else {\n");
2049 static void shader_glsl_break(const SHADER_OPCODE_ARG *arg)
2051 shader_addline(arg->buffer, "break;\n");
2054 /* FIXME: According to MSDN the compare is done per component. */
2055 static void shader_glsl_breakc(const SHADER_OPCODE_ARG *arg)
2057 glsl_src_param_t src0_param;
2058 glsl_src_param_t src1_param;
2060 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
2061 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0, &src1_param);
2063 shader_addline(arg->buffer, "if (%s %s %s) break;\n",
2064 src0_param.param_str, shader_get_comp_op(arg->opcode_token), src1_param.param_str);
2067 static void shader_glsl_label(const SHADER_OPCODE_ARG *arg)
2070 DWORD snum = (arg->src[0]) & WINED3DSP_REGNUM_MASK;
2071 shader_addline(arg->buffer, "}\n");
2072 shader_addline(arg->buffer, "void subroutine%u () {\n", snum);
2075 static void shader_glsl_call(const SHADER_OPCODE_ARG *arg)
2077 DWORD snum = (arg->src[0]) & WINED3DSP_REGNUM_MASK;
2078 shader_addline(arg->buffer, "subroutine%u();\n", snum);
2081 static void shader_glsl_callnz(const SHADER_OPCODE_ARG *arg)
2083 glsl_src_param_t src1_param;
2085 DWORD snum = (arg->src[0]) & WINED3DSP_REGNUM_MASK;
2086 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0, &src1_param);
2087 shader_addline(arg->buffer, "if (%s) subroutine%u();\n", src1_param.param_str, snum);
2090 /*********************************************
2091 * Pixel Shader Specific Code begins here
2092 ********************************************/
2093 static void pshader_glsl_tex(const SHADER_OPCODE_ARG *arg)
2095 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
2096 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
2097 DWORD hex_version = This->baseShader.hex_version;
2098 char dst_swizzle[6];
2099 glsl_sample_function_t sample_function;
2100 DWORD sampler_type;
2101 DWORD sampler_idx;
2102 BOOL projected, texrect = FALSE;
2103 DWORD mask = 0;
2105 /* All versions have a destination register */
2106 shader_glsl_append_dst(arg->buffer, arg);
2108 /* 1.0-1.4: Use destination register as sampler source.
2109 * 2.0+: Use provided sampler source. */
2110 if (hex_version < WINED3DPS_VERSION(2,0)) {
2111 sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2112 } else {
2113 sampler_idx = arg->src[1] & WINED3DSP_REGNUM_MASK;
2115 sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
2117 if (hex_version < WINED3DPS_VERSION(1,4)) {
2118 DWORD flags = deviceImpl->stateBlock->textureState[sampler_idx][WINED3DTSS_TEXTURETRANSFORMFLAGS];
2120 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
2121 if (flags & WINED3DTTFF_PROJECTED && sampler_type != WINED3DSTT_CUBE) {
2122 projected = TRUE;
2123 switch (flags & ~WINED3DTTFF_PROJECTED) {
2124 case WINED3DTTFF_COUNT1: FIXME("WINED3DTTFF_PROJECTED with WINED3DTTFF_COUNT1?\n"); break;
2125 case WINED3DTTFF_COUNT2: mask = WINED3DSP_WRITEMASK_1; break;
2126 case WINED3DTTFF_COUNT3: mask = WINED3DSP_WRITEMASK_2; break;
2127 case WINED3DTTFF_COUNT4:
2128 case WINED3DTTFF_DISABLE: mask = WINED3DSP_WRITEMASK_3; break;
2130 } else {
2131 projected = FALSE;
2133 } else if (hex_version < WINED3DPS_VERSION(2,0)) {
2134 DWORD src_mod = arg->src[0] & WINED3DSP_SRCMOD_MASK;
2136 if (src_mod == WINED3DSPSM_DZ) {
2137 projected = TRUE;
2138 mask = WINED3DSP_WRITEMASK_2;
2139 } else if (src_mod == WINED3DSPSM_DW) {
2140 projected = TRUE;
2141 mask = WINED3DSP_WRITEMASK_3;
2142 } else {
2143 projected = FALSE;
2145 } else {
2146 if(arg->opcode_token & WINED3DSI_TEXLD_PROJECT) {
2147 /* ps 2.0 texldp instruction always divides by the fourth component. */
2148 projected = TRUE;
2149 mask = WINED3DSP_WRITEMASK_3;
2150 } else {
2151 projected = FALSE;
2155 if(deviceImpl->stateBlock->textures[sampler_idx] &&
2156 IWineD3DBaseTexture_GetTextureDimensions(deviceImpl->stateBlock->textures[sampler_idx]) == GL_TEXTURE_RECTANGLE_ARB) {
2157 texrect = TRUE;
2160 shader_glsl_get_sample_function(sampler_type, projected, texrect, &sample_function);
2161 mask |= sample_function.coord_mask;
2163 if (hex_version < WINED3DPS_VERSION(2,0)) {
2164 shader_glsl_get_write_mask(arg->dst, dst_swizzle);
2165 } else {
2166 shader_glsl_get_swizzle(arg->src[1], FALSE, arg->dst, dst_swizzle);
2169 /* 1.0-1.3: Use destination register as coordinate source.
2170 1.4+: Use provided coordinate source register. */
2171 if (hex_version < WINED3DPS_VERSION(1,4)) {
2172 char coord_mask[6];
2173 shader_glsl_get_write_mask(mask, coord_mask);
2174 shader_addline(arg->buffer, "%s(Psampler%u, T%u%s)%s);\n",
2175 sample_function.name, sampler_idx, sampler_idx, coord_mask, dst_swizzle);
2176 } else {
2177 glsl_src_param_t coord_param;
2178 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], mask, &coord_param);
2179 if(arg->opcode_token & WINED3DSI_TEXLD_BIAS) {
2180 glsl_src_param_t bias;
2181 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_3, &bias);
2183 shader_addline(arg->buffer, "%s(Psampler%u, %s, %s)%s);\n",
2184 sample_function.name, sampler_idx, coord_param.param_str,
2185 bias.param_str, dst_swizzle);
2186 } else {
2187 shader_addline(arg->buffer, "%s(Psampler%u, %s)%s);\n",
2188 sample_function.name, sampler_idx, coord_param.param_str, dst_swizzle);
2193 static void shader_glsl_texldl(const SHADER_OPCODE_ARG *arg)
2195 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*)arg->shader;
2196 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
2197 glsl_sample_function_t sample_function;
2198 glsl_src_param_t coord_param, lod_param;
2199 char dst_swizzle[6];
2200 DWORD sampler_type;
2201 DWORD sampler_idx;
2202 BOOL texrect = FALSE;
2204 shader_glsl_append_dst(arg->buffer, arg);
2205 shader_glsl_get_swizzle(arg->src[1], FALSE, arg->dst, dst_swizzle);
2207 sampler_idx = arg->src[1] & WINED3DSP_REGNUM_MASK;
2208 sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
2209 if(deviceImpl->stateBlock->textures[sampler_idx] &&
2210 IWineD3DBaseTexture_GetTextureDimensions(deviceImpl->stateBlock->textures[sampler_idx]) == GL_TEXTURE_RECTANGLE_ARB) {
2211 texrect = TRUE;
2213 shader_glsl_get_sample_function(sampler_type, FALSE, texrect, &sample_function); shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], sample_function.coord_mask, &coord_param);
2215 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_3, &lod_param);
2217 if (shader_is_pshader_version(This->baseShader.hex_version)) {
2218 /* The GLSL spec claims the Lod sampling functions are only supported in vertex shaders.
2219 * However, they seem to work just fine in fragment shaders as well. */
2220 WARN("Using %sLod in fragment shader.\n", sample_function.name);
2221 shader_addline(arg->buffer, "%sLod(Psampler%u, %s, %s)%s);\n",
2222 sample_function.name, sampler_idx, coord_param.param_str, lod_param.param_str, dst_swizzle);
2223 } else {
2224 shader_addline(arg->buffer, "%sLod(Vsampler%u, %s, %s)%s);\n",
2225 sample_function.name, sampler_idx, coord_param.param_str, lod_param.param_str, dst_swizzle);
2229 static void pshader_glsl_texcoord(const SHADER_OPCODE_ARG *arg)
2231 /* FIXME: Make this work for more than just 2D textures */
2233 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
2234 SHADER_BUFFER* buffer = arg->buffer;
2235 DWORD hex_version = This->baseShader.hex_version;
2236 DWORD write_mask;
2237 char dst_mask[6];
2239 write_mask = shader_glsl_append_dst(arg->buffer, arg);
2240 shader_glsl_get_write_mask(write_mask, dst_mask);
2242 if (hex_version != WINED3DPS_VERSION(1,4)) {
2243 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2244 shader_addline(buffer, "clamp(gl_TexCoord[%u], 0.0, 1.0)%s);\n", reg, dst_mask);
2245 } else {
2246 DWORD reg = arg->src[0] & WINED3DSP_REGNUM_MASK;
2247 DWORD src_mod = arg->src[0] & WINED3DSP_SRCMOD_MASK;
2248 char dst_swizzle[6];
2250 shader_glsl_get_swizzle(arg->src[0], FALSE, write_mask, dst_swizzle);
2252 if (src_mod == WINED3DSPSM_DZ) {
2253 glsl_src_param_t div_param;
2254 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
2255 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_2, &div_param);
2257 if (mask_size > 1) {
2258 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
2259 } else {
2260 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
2262 } else if (src_mod == WINED3DSPSM_DW) {
2263 glsl_src_param_t div_param;
2264 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
2265 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_3, &div_param);
2267 if (mask_size > 1) {
2268 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
2269 } else {
2270 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
2272 } else {
2273 shader_addline(buffer, "gl_TexCoord[%u]%s);\n", reg, dst_swizzle);
2278 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
2279 * Take a 3-component dot product of the TexCoord[dstreg] and src,
2280 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
2281 static void pshader_glsl_texdp3tex(const SHADER_OPCODE_ARG *arg)
2283 glsl_src_param_t src0_param;
2284 char dst_mask[6];
2285 glsl_sample_function_t sample_function;
2286 DWORD sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2287 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2288 DWORD sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
2290 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2292 shader_glsl_append_dst(arg->buffer, arg);
2293 shader_glsl_get_write_mask(arg->dst, dst_mask);
2295 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
2296 * scalar, and projected sampling would require 4.
2298 * It is a dependent read - not valid with conditional NP2 textures
2300 shader_glsl_get_sample_function(sampler_type, FALSE, FALSE, &sample_function);
2302 switch(count_bits(sample_function.coord_mask)) {
2303 case 1:
2304 shader_addline(arg->buffer, "%s(Psampler%u, dot(gl_TexCoord[%u].xyz, %s))%s);\n",
2305 sample_function.name, sampler_idx, sampler_idx, src0_param.param_str, dst_mask);
2306 break;
2308 case 2:
2309 shader_addline(arg->buffer, "%s(Psampler%u, vec2(dot(gl_TexCoord[%u].xyz, %s), 0.0))%s);\n",
2310 sample_function.name, sampler_idx, sampler_idx, src0_param.param_str, dst_mask);
2311 break;
2313 case 3:
2314 shader_addline(arg->buffer, "%s(Psampler%u, vec3(dot(gl_TexCoord[%u].xyz, %s), 0.0, 0.0))%s);\n",
2315 sample_function.name, sampler_idx, sampler_idx, src0_param.param_str, dst_mask);
2316 break;
2317 default:
2318 FIXME("Unexpected mask bitcount %d\n", count_bits(sample_function.coord_mask));
2322 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
2323 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
2324 static void pshader_glsl_texdp3(const SHADER_OPCODE_ARG *arg)
2326 glsl_src_param_t src0_param;
2327 DWORD dstreg = arg->dst & WINED3DSP_REGNUM_MASK;
2328 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2329 DWORD dst_mask;
2330 unsigned int mask_size;
2332 dst_mask = shader_glsl_append_dst(arg->buffer, arg);
2333 mask_size = shader_glsl_get_write_mask_size(dst_mask);
2334 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2336 if (mask_size > 1) {
2337 shader_addline(arg->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
2338 } else {
2339 shader_addline(arg->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
2343 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
2344 * Calculate the depth as dst.x / dst.y */
2345 static void pshader_glsl_texdepth(const SHADER_OPCODE_ARG *arg)
2347 glsl_dst_param_t dst_param;
2349 shader_glsl_add_dst_param(arg, arg->dst, 0, &dst_param);
2351 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
2352 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
2353 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
2354 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
2355 * >= 1.0 or < 0.0
2357 shader_addline(arg->buffer, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n", dst_param.reg_name, dst_param.reg_name);
2360 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
2361 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
2362 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
2363 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
2365 static void pshader_glsl_texm3x2depth(const SHADER_OPCODE_ARG *arg)
2367 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2368 DWORD dstreg = arg->dst & WINED3DSP_REGNUM_MASK;
2369 glsl_src_param_t src0_param;
2371 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2373 shader_addline(arg->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
2374 shader_addline(arg->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
2377 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
2378 * Calculate the 1st of a 2-row matrix multiplication. */
2379 static void pshader_glsl_texm3x2pad(const SHADER_OPCODE_ARG *arg)
2381 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2382 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2383 SHADER_BUFFER* buffer = arg->buffer;
2384 glsl_src_param_t src0_param;
2386 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2387 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
2390 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
2391 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
2392 static void pshader_glsl_texm3x3pad(const SHADER_OPCODE_ARG* arg)
2394 IWineD3DPixelShaderImpl* shader = (IWineD3DPixelShaderImpl*) arg->shader;
2395 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2396 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2397 SHADER_BUFFER* buffer = arg->buffer;
2398 SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
2399 glsl_src_param_t src0_param;
2401 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2402 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + current_state->current_row, reg, src0_param.param_str);
2403 current_state->texcoord_w[current_state->current_row++] = reg;
2406 static void pshader_glsl_texm3x2tex(const SHADER_OPCODE_ARG *arg)
2408 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2409 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2410 SHADER_BUFFER* buffer = arg->buffer;
2411 glsl_src_param_t src0_param;
2412 char dst_mask[6];
2414 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2415 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
2417 shader_glsl_append_dst(buffer, arg);
2418 shader_glsl_get_write_mask(arg->dst, dst_mask);
2420 /* Sample the texture using the calculated coordinates */
2421 shader_addline(buffer, "texture2D(Psampler%u, tmp0.xy)%s);\n", reg, dst_mask);
2424 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
2425 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
2426 static void pshader_glsl_texm3x3tex(const SHADER_OPCODE_ARG *arg)
2428 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2429 glsl_src_param_t src0_param;
2430 char dst_mask[6];
2431 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2432 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
2433 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
2434 DWORD sampler_type = arg->reg_maps->samplers[reg] & WINED3DSP_TEXTURETYPE_MASK;
2435 glsl_sample_function_t sample_function;
2437 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2438 shader_addline(arg->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
2440 shader_glsl_append_dst(arg->buffer, arg);
2441 shader_glsl_get_write_mask(arg->dst, dst_mask);
2442 /* Dependent read, not valid with conditional NP2 */
2443 shader_glsl_get_sample_function(sampler_type, FALSE, FALSE, &sample_function);
2445 /* Sample the texture using the calculated coordinates */
2446 shader_addline(arg->buffer, "%s(Psampler%u, tmp0.xyz)%s);\n", sample_function.name, reg, dst_mask);
2448 current_state->current_row = 0;
2451 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
2452 * Perform the 3rd row of a 3x3 matrix multiply */
2453 static void pshader_glsl_texm3x3(const SHADER_OPCODE_ARG *arg)
2455 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2456 glsl_src_param_t src0_param;
2457 char dst_mask[6];
2458 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2459 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
2460 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
2462 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2464 shader_glsl_append_dst(arg->buffer, arg);
2465 shader_glsl_get_write_mask(arg->dst, dst_mask);
2466 shader_addline(arg->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
2468 current_state->current_row = 0;
2471 /** Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
2472 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
2473 static void pshader_glsl_texm3x3spec(const SHADER_OPCODE_ARG *arg)
2475 IWineD3DPixelShaderImpl* shader = (IWineD3DPixelShaderImpl*) arg->shader;
2476 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2477 glsl_src_param_t src0_param;
2478 glsl_src_param_t src1_param;
2479 char dst_mask[6];
2480 SHADER_BUFFER* buffer = arg->buffer;
2481 SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
2482 DWORD stype = arg->reg_maps->samplers[reg] & WINED3DSP_TEXTURETYPE_MASK;
2483 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2484 glsl_sample_function_t sample_function;
2486 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2487 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], src_mask, &src1_param);
2489 /* Perform the last matrix multiply operation */
2490 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
2491 /* Reflection calculation */
2492 shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
2494 shader_glsl_append_dst(buffer, arg);
2495 shader_glsl_get_write_mask(arg->dst, dst_mask);
2496 /* Dependent read, not valid with conditional NP2 */
2497 shader_glsl_get_sample_function(stype, FALSE, FALSE, &sample_function);
2499 /* Sample the texture */
2500 shader_addline(buffer, "%s(Psampler%u, tmp0.xyz)%s);\n", sample_function.name, reg, dst_mask);
2502 current_state->current_row = 0;
2505 /** Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
2506 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
2507 static void pshader_glsl_texm3x3vspec(const SHADER_OPCODE_ARG *arg)
2509 IWineD3DPixelShaderImpl* shader = (IWineD3DPixelShaderImpl*) arg->shader;
2510 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2511 SHADER_BUFFER* buffer = arg->buffer;
2512 SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
2513 glsl_src_param_t src0_param;
2514 char dst_mask[6];
2515 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2516 DWORD sampler_type = arg->reg_maps->samplers[reg] & WINED3DSP_TEXTURETYPE_MASK;
2517 glsl_sample_function_t sample_function;
2519 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2521 /* Perform the last matrix multiply operation */
2522 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
2524 /* Construct the eye-ray vector from w coordinates */
2525 shader_addline(buffer, "tmp1.xyz = normalize(vec3(gl_TexCoord[%u].w, gl_TexCoord[%u].w, gl_TexCoord[%u].w));\n",
2526 current_state->texcoord_w[0], current_state->texcoord_w[1], reg);
2527 shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
2529 shader_glsl_append_dst(buffer, arg);
2530 shader_glsl_get_write_mask(arg->dst, dst_mask);
2531 /* Dependent read, not valid with conditional NP2 */
2532 shader_glsl_get_sample_function(sampler_type, FALSE, FALSE, &sample_function);
2534 /* Sample the texture using the calculated coordinates */
2535 shader_addline(buffer, "%s(Psampler%u, tmp0.xyz)%s);\n", sample_function.name, reg, dst_mask);
2537 current_state->current_row = 0;
2540 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
2541 * Apply a fake bump map transform.
2542 * texbem is pshader <= 1.3 only, this saves a few version checks
2544 static void pshader_glsl_texbem(const SHADER_OPCODE_ARG *arg)
2546 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
2547 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
2548 char dst_swizzle[6];
2549 glsl_sample_function_t sample_function;
2550 glsl_src_param_t coord_param;
2551 DWORD sampler_type;
2552 DWORD sampler_idx;
2553 DWORD mask;
2554 DWORD flags;
2555 char coord_mask[6];
2557 sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2558 flags = deviceImpl->stateBlock->textureState[sampler_idx][WINED3DTSS_TEXTURETRANSFORMFLAGS];
2560 sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
2561 /* Dependent read, not valid with conditional NP2 */
2562 shader_glsl_get_sample_function(sampler_type, FALSE, FALSE, &sample_function);
2563 mask = sample_function.coord_mask;
2565 shader_glsl_get_write_mask(arg->dst, dst_swizzle);
2567 shader_glsl_get_write_mask(mask, coord_mask);
2569 /* with projective textures, texbem only divides the static texture coord, not the displacement,
2570 * so we can't let the GL handle this.
2572 if (flags & WINED3DTTFF_PROJECTED) {
2573 DWORD div_mask=0;
2574 char coord_div_mask[3];
2575 switch (flags & ~WINED3DTTFF_PROJECTED) {
2576 case WINED3DTTFF_COUNT1: FIXME("WINED3DTTFF_PROJECTED with WINED3DTTFF_COUNT1?\n"); break;
2577 case WINED3DTTFF_COUNT2: div_mask = WINED3DSP_WRITEMASK_1; break;
2578 case WINED3DTTFF_COUNT3: div_mask = WINED3DSP_WRITEMASK_2; break;
2579 case WINED3DTTFF_COUNT4:
2580 case WINED3DTTFF_DISABLE: div_mask = WINED3DSP_WRITEMASK_3; break;
2582 shader_glsl_get_write_mask(div_mask, coord_div_mask);
2583 shader_addline(arg->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
2586 shader_glsl_append_dst(arg->buffer, arg);
2587 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0|WINED3DSP_WRITEMASK_1, &coord_param);
2588 if(arg->opcode->opcode == WINED3DSIO_TEXBEML) {
2589 glsl_src_param_t luminance_param;
2590 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_2, &luminance_param);
2591 shader_addline(arg->buffer, "(%s(Psampler%u, T%u%s + vec4(bumpenvmat%d * %s, 0.0, 0.0)%s )*(%s * luminancescale%d + luminanceoffset%d))%s);\n",
2592 sample_function.name, sampler_idx, sampler_idx, coord_mask, sampler_idx, coord_param.param_str, coord_mask,
2593 luminance_param.param_str, sampler_idx, sampler_idx, dst_swizzle);
2594 } else {
2595 shader_addline(arg->buffer, "%s(Psampler%u, T%u%s + vec4(bumpenvmat%d * %s, 0.0, 0.0)%s )%s);\n",
2596 sample_function.name, sampler_idx, sampler_idx, coord_mask, sampler_idx, coord_param.param_str, coord_mask, dst_swizzle);
2600 static void pshader_glsl_bem(const SHADER_OPCODE_ARG *arg)
2602 glsl_src_param_t src0_param, src1_param;
2603 DWORD sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2605 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0|WINED3DSP_WRITEMASK_1, &src0_param);
2606 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0|WINED3DSP_WRITEMASK_1, &src1_param);
2608 shader_glsl_append_dst(arg->buffer, arg);
2609 shader_addline(arg->buffer, "%s + bumpenvmat%d * %s);\n",
2610 src0_param.param_str, sampler_idx, src1_param.param_str);
2613 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
2614 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
2615 static void pshader_glsl_texreg2ar(const SHADER_OPCODE_ARG *arg)
2617 glsl_src_param_t src0_param;
2618 DWORD sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2619 char dst_mask[6];
2621 shader_glsl_append_dst(arg->buffer, arg);
2622 shader_glsl_get_write_mask(arg->dst, dst_mask);
2623 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
2625 shader_addline(arg->buffer, "texture2D(Psampler%u, %s.wx)%s);\n", sampler_idx, src0_param.reg_name, dst_mask);
2628 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
2629 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
2630 static void pshader_glsl_texreg2gb(const SHADER_OPCODE_ARG *arg)
2632 glsl_src_param_t src0_param;
2633 DWORD sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2634 char dst_mask[6];
2636 shader_glsl_append_dst(arg->buffer, arg);
2637 shader_glsl_get_write_mask(arg->dst, dst_mask);
2638 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
2640 shader_addline(arg->buffer, "texture2D(Psampler%u, %s.yz)%s);\n", sampler_idx, src0_param.reg_name, dst_mask);
2643 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
2644 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
2645 static void pshader_glsl_texreg2rgb(const SHADER_OPCODE_ARG *arg)
2647 glsl_src_param_t src0_param;
2648 char dst_mask[6];
2649 DWORD sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2650 DWORD sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
2651 glsl_sample_function_t sample_function;
2653 shader_glsl_append_dst(arg->buffer, arg);
2654 shader_glsl_get_write_mask(arg->dst, dst_mask);
2655 /* Dependent read, not valid with conditional NP2 */
2656 shader_glsl_get_sample_function(sampler_type, FALSE, FALSE, &sample_function);
2657 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], sample_function.coord_mask, &src0_param);
2659 shader_addline(arg->buffer, "%s(Psampler%u, %s)%s);\n", sample_function.name, sampler_idx, src0_param.param_str, dst_mask);
2662 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
2663 * If any of the first 3 components are < 0, discard this pixel */
2664 static void pshader_glsl_texkill(const SHADER_OPCODE_ARG *arg)
2666 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
2667 DWORD hex_version = This->baseShader.hex_version;
2668 glsl_dst_param_t dst_param;
2670 /* The argument is a destination parameter, and no writemasks are allowed */
2671 shader_glsl_add_dst_param(arg, arg->dst, 0, &dst_param);
2672 if((hex_version >= WINED3DPS_VERSION(2,0))) {
2673 /* 2.0 shaders compare all 4 components in texkill */
2674 shader_addline(arg->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
2675 } else {
2676 /* 1.X shaders only compare the first 3 components, probably due to the nature of the texkill
2677 * instruction as a tex* instruction, and phase, which kills all a / w components. Even if all
2678 * 4 components are defined, only the first 3 are used
2680 shader_addline(arg->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
2684 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
2685 * dst = dot2(src0, src1) + src2 */
2686 static void pshader_glsl_dp2add(const SHADER_OPCODE_ARG *arg)
2688 glsl_src_param_t src0_param;
2689 glsl_src_param_t src1_param;
2690 glsl_src_param_t src2_param;
2691 DWORD write_mask;
2692 unsigned int mask_size;
2694 write_mask = shader_glsl_append_dst(arg->buffer, arg);
2695 mask_size = shader_glsl_get_write_mask_size(write_mask);
2697 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
2698 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
2699 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], WINED3DSP_WRITEMASK_0, &src2_param);
2701 if (mask_size > 1) {
2702 shader_addline(arg->buffer, "vec%d(dot(%s, %s) + %s));\n", mask_size, src0_param.param_str, src1_param.param_str, src2_param.param_str);
2703 } else {
2704 shader_addline(arg->buffer, "dot(%s, %s) + %s);\n", src0_param.param_str, src1_param.param_str, src2_param.param_str);
2708 static void pshader_glsl_input_pack(SHADER_BUFFER* buffer, const struct semantic* semantics_in,
2709 IWineD3DPixelShader *iface, enum vertexprocessing_mode vertexprocessing)
2711 unsigned int i;
2712 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *) iface;
2714 for (i = 0; i < MAX_REG_INPUT; i++) {
2716 DWORD usage_token = semantics_in[i].usage;
2717 DWORD register_token = semantics_in[i].reg;
2718 DWORD usage, usage_idx;
2719 char reg_mask[6];
2721 /* Uninitialized */
2722 if (!usage_token) continue;
2723 usage = (usage_token & WINED3DSP_DCL_USAGE_MASK) >> WINED3DSP_DCL_USAGE_SHIFT;
2724 usage_idx = (usage_token & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
2725 shader_glsl_get_write_mask(register_token, reg_mask);
2727 switch(usage) {
2729 case WINED3DDECLUSAGE_TEXCOORD:
2730 if(usage_idx < 8 && vertexprocessing == pretransformed) {
2731 shader_addline(buffer, "IN[%u]%s = gl_TexCoord[%u]%s;\n",
2732 This->input_reg_map[i], reg_mask, usage_idx, reg_mask);
2733 } else {
2734 shader_addline(buffer, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
2735 This->input_reg_map[i], reg_mask, reg_mask);
2737 break;
2739 case WINED3DDECLUSAGE_COLOR:
2740 if (usage_idx == 0)
2741 shader_addline(buffer, "IN[%u]%s = vec4(gl_Color)%s;\n",
2742 This->input_reg_map[i], reg_mask, reg_mask);
2743 else if (usage_idx == 1)
2744 shader_addline(buffer, "IN[%u]%s = vec4(gl_SecondaryColor)%s;\n",
2745 This->input_reg_map[i], reg_mask, reg_mask);
2746 else
2747 shader_addline(buffer, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
2748 This->input_reg_map[i], reg_mask, reg_mask);
2749 break;
2751 default:
2752 shader_addline(buffer, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
2753 This->input_reg_map[i], reg_mask, reg_mask);
2758 /*********************************************
2759 * Vertex Shader Specific Code begins here
2760 ********************************************/
2762 static void add_glsl_program_entry(struct shader_glsl_priv *priv, struct glsl_shader_prog_link *entry) {
2763 glsl_program_key_t *key;
2765 key = HeapAlloc(GetProcessHeap(), 0, sizeof(glsl_program_key_t));
2766 key->vshader = entry->vshader;
2767 key->pshader = entry->pshader;
2768 key->ps_args = entry->ps_args;
2770 hash_table_put(priv->glsl_program_lookup, key, entry);
2773 static struct glsl_shader_prog_link *get_glsl_program_entry(struct shader_glsl_priv *priv,
2774 GLhandleARB vshader, IWineD3DPixelShader *pshader, struct ps_compile_args *ps_args) {
2775 glsl_program_key_t key;
2777 key.vshader = vshader;
2778 key.pshader = pshader;
2779 key.ps_args = *ps_args;
2781 return (struct glsl_shader_prog_link *)hash_table_get(priv->glsl_program_lookup, &key);
2784 static void delete_glsl_program_entry(struct shader_glsl_priv *priv, const WineD3D_GL_Info *gl_info,
2785 struct glsl_shader_prog_link *entry)
2787 glsl_program_key_t *key;
2789 key = HeapAlloc(GetProcessHeap(), 0, sizeof(glsl_program_key_t));
2790 key->vshader = entry->vshader;
2791 key->pshader = entry->pshader;
2792 key->ps_args = entry->ps_args;
2793 hash_table_remove(priv->glsl_program_lookup, key);
2795 GL_EXTCALL(glDeleteObjectARB(entry->programId));
2796 if (entry->vshader) list_remove(&entry->vshader_entry);
2797 if (entry->pshader) list_remove(&entry->pshader_entry);
2798 HeapFree(GetProcessHeap(), 0, entry->vuniformF_locations);
2799 HeapFree(GetProcessHeap(), 0, entry->puniformF_locations);
2800 HeapFree(GetProcessHeap(), 0, entry);
2803 static void handle_ps3_input(SHADER_BUFFER *buffer, const struct semantic *semantics_in,
2804 const struct semantic *semantics_out, const WineD3D_GL_Info *gl_info, const DWORD *map)
2806 unsigned int i, j;
2807 DWORD usage_token, usage_token_out;
2808 DWORD register_token, register_token_out;
2809 DWORD usage, usage_idx, usage_out, usage_idx_out;
2810 DWORD *set;
2811 DWORD in_idx;
2812 DWORD in_count = GL_LIMITS(glsl_varyings) / 4;
2813 char reg_mask[6], reg_mask_out[6];
2814 char destination[50];
2816 set = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*set) * (in_count + 2));
2818 if (!semantics_out) {
2819 /* Save gl_FrontColor & gl_FrontSecondaryColor before overwriting them. */
2820 shader_addline(buffer, "vec4 front_color = gl_FrontColor;\n");
2821 shader_addline(buffer, "vec4 front_secondary_color = gl_FrontSecondaryColor;\n");
2824 for(i = 0; i < MAX_REG_INPUT; i++) {
2825 usage_token = semantics_in[i].usage;
2826 if (!usage_token) continue;
2828 in_idx = map[i];
2829 if (in_idx >= (in_count + 2)) {
2830 FIXME("More input varyings declared than supported, expect issues\n");
2831 continue;
2832 } else if(map[i] == -1) {
2833 /* Declared, but not read register */
2834 continue;
2837 if (in_idx == in_count) {
2838 sprintf(destination, "gl_FrontColor");
2839 } else if (in_idx == in_count + 1) {
2840 sprintf(destination, "gl_FrontSecondaryColor");
2841 } else {
2842 sprintf(destination, "IN[%u]", in_idx);
2845 register_token = semantics_in[i].reg;
2847 usage = (usage_token & WINED3DSP_DCL_USAGE_MASK) >> WINED3DSP_DCL_USAGE_SHIFT;
2848 usage_idx = (usage_token & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
2849 set[map[i]] = shader_glsl_get_write_mask(register_token, reg_mask);
2851 if(!semantics_out) {
2852 switch(usage) {
2853 case WINED3DDECLUSAGE_COLOR:
2854 if (usage_idx == 0)
2855 shader_addline(buffer, "%s%s = front_color%s;\n",
2856 destination, reg_mask, reg_mask);
2857 else if (usage_idx == 1)
2858 shader_addline(buffer, "%s%s = front_secondary_color%s;\n",
2859 destination, reg_mask, reg_mask);
2860 else
2861 shader_addline(buffer, "%s%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
2862 destination, reg_mask, reg_mask);
2863 break;
2865 case WINED3DDECLUSAGE_TEXCOORD:
2866 if (usage_idx < 8) {
2867 shader_addline(buffer, "%s%s = gl_TexCoord[%u]%s;\n",
2868 destination, reg_mask, usage_idx, reg_mask);
2869 } else {
2870 shader_addline(buffer, "%s%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
2871 destination, reg_mask, reg_mask);
2873 break;
2875 case WINED3DDECLUSAGE_FOG:
2876 shader_addline(buffer, "%s%s = vec4(gl_FogFragCoord, 0.0, 0.0, 0.0)%s;\n",
2877 destination, reg_mask, reg_mask);
2878 break;
2880 default:
2881 shader_addline(buffer, "%s%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
2882 destination, reg_mask, reg_mask);
2884 } else {
2885 BOOL found = FALSE;
2886 for(j = 0; j < MAX_REG_OUTPUT; j++) {
2887 usage_token_out = semantics_out[j].usage;
2888 if (!usage_token_out) continue;
2889 register_token_out = semantics_out[j].reg;
2891 usage_out = (usage_token_out & WINED3DSP_DCL_USAGE_MASK) >> WINED3DSP_DCL_USAGE_SHIFT;
2892 usage_idx_out = (usage_token_out & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
2893 shader_glsl_get_write_mask(register_token_out, reg_mask_out);
2895 if(usage == usage_out &&
2896 usage_idx == usage_idx_out) {
2897 shader_addline(buffer, "%s%s = OUT[%u]%s;\n",
2898 destination, reg_mask, j, reg_mask);
2899 found = TRUE;
2902 if(!found) {
2903 shader_addline(buffer, "%s%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
2904 destination, reg_mask, reg_mask);
2909 /* This is solely to make the compiler / linker happy and avoid warning about undefined
2910 * varyings. It shouldn't result in any real code executed on the GPU, since all read
2911 * input varyings are assigned above, if the optimizer works properly.
2913 for(i = 0; i < in_count + 2; i++) {
2914 if(set[i] != WINED3DSP_WRITEMASK_ALL) {
2915 unsigned int size = 0;
2916 memset(reg_mask, 0, sizeof(reg_mask));
2917 if(!(set[i] & WINED3DSP_WRITEMASK_0)) {
2918 reg_mask[size] = 'x';
2919 size++;
2921 if(!(set[i] & WINED3DSP_WRITEMASK_1)) {
2922 reg_mask[size] = 'y';
2923 size++;
2925 if(!(set[i] & WINED3DSP_WRITEMASK_2)) {
2926 reg_mask[size] = 'z';
2927 size++;
2929 if(!(set[i] & WINED3DSP_WRITEMASK_3)) {
2930 reg_mask[size] = 'w';
2931 size++;
2934 if (i == in_count) {
2935 sprintf(destination, "gl_FrontColor");
2936 } else if (i == in_count + 1) {
2937 sprintf(destination, "gl_FrontSecondaryColor");
2938 } else {
2939 sprintf(destination, "IN[%u]", i);
2942 if (size == 1) {
2943 shader_addline(buffer, "%s.%s = 0.0;\n", destination, reg_mask);
2944 } else {
2945 shader_addline(buffer, "%s.%s = vec%u(0.0);\n", destination, reg_mask, size);
2950 HeapFree(GetProcessHeap(), 0, set);
2953 static GLhandleARB generate_param_reorder_function(IWineD3DVertexShader *vertexshader,
2954 IWineD3DPixelShader *pixelshader, const WineD3D_GL_Info *gl_info)
2956 GLhandleARB ret = 0;
2957 IWineD3DVertexShaderImpl *vs = (IWineD3DVertexShaderImpl *) vertexshader;
2958 IWineD3DPixelShaderImpl *ps = (IWineD3DPixelShaderImpl *) pixelshader;
2959 IWineD3DDeviceImpl *device;
2960 DWORD vs_major = WINED3DSHADER_VERSION_MAJOR(vs->baseShader.hex_version);
2961 DWORD ps_major = ps ? WINED3DSHADER_VERSION_MAJOR(ps->baseShader.hex_version) : 0;
2962 unsigned int i;
2963 SHADER_BUFFER buffer;
2964 DWORD usage_token;
2965 DWORD register_token;
2966 DWORD usage, usage_idx, writemask;
2967 char reg_mask[6];
2968 const struct semantic *semantics_out, *semantics_in;
2970 shader_buffer_init(&buffer);
2972 shader_addline(&buffer, "#version 120\n");
2974 if(vs_major < 3 && ps_major < 3) {
2975 /* That one is easy: The vertex shader writes to the builtin varyings, the pixel shader reads from them.
2976 * Take care about the texcoord .w fixup though if we're using the fixed function fragment pipeline
2978 device = (IWineD3DDeviceImpl *) vs->baseShader.device;
2979 if((GLINFO_LOCATION).set_texcoord_w && ps_major == 0 && vs_major > 0 &&
2980 !device->frag_pipe->ffp_proj_control) {
2981 shader_addline(&buffer, "void order_ps_input() {\n");
2982 for(i = 0; i < min(8, MAX_REG_TEXCRD); i++) {
2983 if(vs->baseShader.reg_maps.texcoord_mask[i] != 0 &&
2984 vs->baseShader.reg_maps.texcoord_mask[i] != WINED3DSP_WRITEMASK_ALL) {
2985 shader_addline(&buffer, "gl_TexCoord[%u].w = 1.0;\n", i);
2988 shader_addline(&buffer, "}\n");
2989 } else {
2990 shader_addline(&buffer, "void order_ps_input() { /* do nothing */ }\n");
2992 } else if(ps_major < 3 && vs_major >= 3) {
2993 /* The vertex shader writes to its own varyings, the pixel shader needs them in the builtin ones */
2994 semantics_out = vs->semantics_out;
2996 shader_addline(&buffer, "void order_ps_input(in vec4 OUT[%u]) {\n", MAX_REG_OUTPUT);
2997 for(i = 0; i < MAX_REG_OUTPUT; i++) {
2998 usage_token = semantics_out[i].usage;
2999 if (!usage_token) continue;
3000 register_token = semantics_out[i].reg;
3002 usage = (usage_token & WINED3DSP_DCL_USAGE_MASK) >> WINED3DSP_DCL_USAGE_SHIFT;
3003 usage_idx = (usage_token & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
3004 writemask = shader_glsl_get_write_mask(register_token, reg_mask);
3006 switch(usage) {
3007 case WINED3DDECLUSAGE_COLOR:
3008 if (usage_idx == 0)
3009 shader_addline(&buffer, "gl_FrontColor%s = OUT[%u]%s;\n", reg_mask, i, reg_mask);
3010 else if (usage_idx == 1)
3011 shader_addline(&buffer, "gl_FrontSecondaryColor%s = OUT[%u]%s;\n", reg_mask, i, reg_mask);
3012 break;
3014 case WINED3DDECLUSAGE_POSITION:
3015 shader_addline(&buffer, "gl_Position%s = OUT[%u]%s;\n", reg_mask, i, reg_mask);
3016 break;
3018 case WINED3DDECLUSAGE_TEXCOORD:
3019 if (usage_idx < 8) {
3020 if(!(GLINFO_LOCATION).set_texcoord_w || ps_major > 0) writemask |= WINED3DSP_WRITEMASK_3;
3022 shader_addline(&buffer, "gl_TexCoord[%u]%s = OUT[%u]%s;\n",
3023 usage_idx, reg_mask, i, reg_mask);
3024 if(!(writemask & WINED3DSP_WRITEMASK_3)) {
3025 shader_addline(&buffer, "gl_TexCoord[%u].w = 1.0;\n", usage_idx);
3028 break;
3030 case WINED3DDECLUSAGE_PSIZE:
3031 shader_addline(&buffer, "gl_PointSize = OUT[%u].x;\n", i);
3032 break;
3034 case WINED3DDECLUSAGE_FOG:
3035 shader_addline(&buffer, "gl_FogFragCoord = OUT[%u].%c;\n", i, reg_mask[1]);
3036 break;
3038 default:
3039 break;
3042 shader_addline(&buffer, "}\n");
3044 } else if(ps_major >= 3 && vs_major >= 3) {
3045 semantics_out = vs->semantics_out;
3046 semantics_in = ps->semantics_in;
3048 /* This one is tricky: a 3.0 pixel shader reads from a 3.0 vertex shader */
3049 shader_addline(&buffer, "varying vec4 IN[%u];\n", GL_LIMITS(glsl_varyings) / 4);
3050 shader_addline(&buffer, "void order_ps_input(in vec4 OUT[%u]) {\n", MAX_REG_OUTPUT);
3052 /* First, sort out position and point size. Those are not passed to the pixel shader */
3053 for(i = 0; i < MAX_REG_OUTPUT; i++) {
3054 usage_token = semantics_out[i].usage;
3055 if (!usage_token) continue;
3056 register_token = semantics_out[i].reg;
3058 usage = (usage_token & WINED3DSP_DCL_USAGE_MASK) >> WINED3DSP_DCL_USAGE_SHIFT;
3059 usage_idx = (usage_token & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
3060 shader_glsl_get_write_mask(register_token, reg_mask);
3062 switch(usage) {
3063 case WINED3DDECLUSAGE_POSITION:
3064 shader_addline(&buffer, "gl_Position%s = OUT[%u]%s;\n", reg_mask, i, reg_mask);
3065 break;
3067 case WINED3DDECLUSAGE_PSIZE:
3068 shader_addline(&buffer, "gl_PointSize = OUT[%u].x;\n", i);
3069 break;
3071 default:
3072 break;
3076 /* Then, fix the pixel shader input */
3077 handle_ps3_input(&buffer, semantics_in, semantics_out, gl_info, ps->input_reg_map);
3079 shader_addline(&buffer, "}\n");
3080 } else if(ps_major >= 3 && vs_major < 3) {
3081 semantics_in = ps->semantics_in;
3083 shader_addline(&buffer, "varying vec4 IN[%u];\n", GL_LIMITS(glsl_varyings) / 4);
3084 shader_addline(&buffer, "void order_ps_input() {\n");
3085 /* The vertex shader wrote to the builtin varyings. There is no need to figure out position and
3086 * point size, but we depend on the optimizers kindness to find out that the pixel shader doesn't
3087 * read gl_TexCoord and gl_ColorX, otherwise we'll run out of varyings
3089 handle_ps3_input(&buffer, semantics_in, NULL, gl_info, ps->input_reg_map);
3090 shader_addline(&buffer, "}\n");
3091 } else {
3092 ERR("Unexpected vertex and pixel shader version condition: vs: %d, ps: %d\n", vs_major, ps_major);
3095 ret = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
3096 checkGLcall("glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB)");
3097 GL_EXTCALL(glShaderSourceARB(ret, 1, (const char**)&buffer.buffer, NULL));
3098 checkGLcall("glShaderSourceARB(ret, 1, (const char**)&buffer.buffer, NULL)");
3099 GL_EXTCALL(glCompileShaderARB(ret));
3100 checkGLcall("glCompileShaderARB(ret)");
3102 shader_buffer_free(&buffer);
3103 return ret;
3106 static void hardcode_local_constants(IWineD3DBaseShaderImpl *shader, const WineD3D_GL_Info *gl_info,
3107 GLhandleARB programId, char prefix)
3109 const local_constant *lconst;
3110 GLuint tmp_loc;
3111 const float *value;
3112 char glsl_name[8];
3114 LIST_FOR_EACH_ENTRY(lconst, &shader->baseShader.constantsF, local_constant, entry) {
3115 value = (const float *)lconst->value;
3116 snprintf(glsl_name, sizeof(glsl_name), "%cLC%u", prefix, lconst->idx);
3117 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
3118 GL_EXTCALL(glUniform4fvARB(tmp_loc, 1, value));
3120 checkGLcall("Hardcoding local constants\n");
3123 /** Sets the GLSL program ID for the given pixel and vertex shader combination.
3124 * It sets the programId on the current StateBlock (because it should be called
3125 * inside of the DrawPrimitive() part of the render loop).
3127 * If a program for the given combination does not exist, create one, and store
3128 * the program in the hash table. If it creates a program, it will link the
3129 * given objects, too.
3131 static void set_glsl_shader_program(IWineD3DDevice *iface, BOOL use_ps, BOOL use_vs) {
3132 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
3133 struct shader_glsl_priv *priv = (struct shader_glsl_priv *)This->shader_priv;
3134 const WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
3135 IWineD3DPixelShader *pshader = This->stateBlock->pixelShader;
3136 IWineD3DVertexShader *vshader = This->stateBlock->vertexShader;
3137 struct glsl_shader_prog_link *entry = NULL;
3138 GLhandleARB programId = 0;
3139 GLhandleARB reorder_shader_id = 0;
3140 int i;
3141 char glsl_name[8];
3142 GLhandleARB vshader_id, pshader_id;
3143 struct ps_compile_args compile_args;
3145 if(use_vs) {
3146 IWineD3DVertexShaderImpl_CompileShader(vshader);
3147 vshader_id = ((IWineD3DVertexShaderImpl*)vshader)->prgId;
3148 } else {
3149 vshader_id = 0;
3151 if(use_ps) {
3152 find_ps_compile_args((IWineD3DPixelShaderImpl*)This->stateBlock->pixelShader, This->stateBlock, &compile_args);
3153 } else {
3154 /* FIXME: Do we really have to spend CPU cycles to generate a few zeroed bytes? */
3155 memset(&compile_args, 0, sizeof(compile_args));
3157 entry = get_glsl_program_entry(priv, vshader_id, pshader, &compile_args);
3158 if (entry) {
3159 priv->glsl_program = entry;
3160 return;
3163 /* If we get to this point, then no matching program exists, so we create one */
3164 programId = GL_EXTCALL(glCreateProgramObjectARB());
3165 TRACE("Created new GLSL shader program %u\n", programId);
3167 /* Create the entry */
3168 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link));
3169 entry->programId = programId;
3170 entry->vshader = vshader_id;
3171 entry->pshader = pshader;
3172 entry->ps_args = compile_args;
3173 /* Add the hash table entry */
3174 add_glsl_program_entry(priv, entry);
3176 /* Set the current program */
3177 priv->glsl_program = entry;
3179 /* Attach GLSL vshader */
3180 if (vshader_id) {
3181 int max_attribs = 16; /* TODO: Will this always be the case? It is at the moment... */
3182 char tmp_name[10];
3184 reorder_shader_id = generate_param_reorder_function(vshader, pshader, gl_info);
3185 TRACE("Attaching GLSL shader object %u to program %u\n", reorder_shader_id, programId);
3186 GL_EXTCALL(glAttachObjectARB(programId, reorder_shader_id));
3187 checkGLcall("glAttachObjectARB");
3188 /* Flag the reorder function for deletion, then it will be freed automatically when the program
3189 * is destroyed
3191 GL_EXTCALL(glDeleteObjectARB(reorder_shader_id));
3193 TRACE("Attaching GLSL shader object %u to program %u\n", vshader_id, programId);
3194 GL_EXTCALL(glAttachObjectARB(programId, vshader_id));
3195 checkGLcall("glAttachObjectARB");
3197 /* Bind vertex attributes to a corresponding index number to match
3198 * the same index numbers as ARB_vertex_programs (makes loading
3199 * vertex attributes simpler). With this method, we can use the
3200 * exact same code to load the attributes later for both ARB and
3201 * GLSL shaders.
3203 * We have to do this here because we need to know the Program ID
3204 * in order to make the bindings work, and it has to be done prior
3205 * to linking the GLSL program. */
3206 for (i = 0; i < max_attribs; ++i) {
3207 if (((IWineD3DBaseShaderImpl*)vshader)->baseShader.reg_maps.attributes[i]) {
3208 snprintf(tmp_name, sizeof(tmp_name), "attrib%i", i);
3209 GL_EXTCALL(glBindAttribLocationARB(programId, i, tmp_name));
3212 checkGLcall("glBindAttribLocationARB");
3214 list_add_head(&((IWineD3DBaseShaderImpl *)vshader)->baseShader.linked_programs, &entry->vshader_entry);
3217 if(use_ps) {
3218 pshader_id = find_gl_pshader((IWineD3DPixelShaderImpl *) pshader, &compile_args);
3219 } else {
3220 pshader_id = 0;
3223 /* Attach GLSL pshader */
3224 if (pshader_id) {
3225 TRACE("Attaching GLSL shader object %u to program %u\n", pshader_id, programId);
3226 GL_EXTCALL(glAttachObjectARB(programId, pshader_id));
3227 checkGLcall("glAttachObjectARB");
3229 list_add_head(&((IWineD3DBaseShaderImpl *)pshader)->baseShader.linked_programs, &entry->pshader_entry);
3232 /* Link the program */
3233 TRACE("Linking GLSL shader program %u\n", programId);
3234 GL_EXTCALL(glLinkProgramARB(programId));
3235 print_glsl_info_log(&GLINFO_LOCATION, programId);
3237 entry->vuniformF_locations = HeapAlloc(GetProcessHeap(), 0, sizeof(GLhandleARB) * GL_LIMITS(vshader_constantsF));
3238 for (i = 0; i < GL_LIMITS(vshader_constantsF); ++i) {
3239 snprintf(glsl_name, sizeof(glsl_name), "VC[%i]", i);
3240 entry->vuniformF_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
3242 for (i = 0; i < MAX_CONST_I; ++i) {
3243 snprintf(glsl_name, sizeof(glsl_name), "VI[%i]", i);
3244 entry->vuniformI_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
3246 entry->puniformF_locations = HeapAlloc(GetProcessHeap(), 0, sizeof(GLhandleARB) * GL_LIMITS(pshader_constantsF));
3247 for (i = 0; i < GL_LIMITS(pshader_constantsF); ++i) {
3248 snprintf(glsl_name, sizeof(glsl_name), "PC[%i]", i);
3249 entry->puniformF_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
3251 for (i = 0; i < MAX_CONST_I; ++i) {
3252 snprintf(glsl_name, sizeof(glsl_name), "PI[%i]", i);
3253 entry->puniformI_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
3256 if(pshader) {
3257 for(i = 0; i < ((IWineD3DPixelShaderImpl*)pshader)->numbumpenvmatconsts; i++) {
3258 char name[32];
3259 sprintf(name, "bumpenvmat%d", ((IWineD3DPixelShaderImpl*)pshader)->bumpenvmatconst[i].texunit);
3260 entry->bumpenvmat_location[i] = GL_EXTCALL(glGetUniformLocationARB(programId, name));
3261 sprintf(name, "luminancescale%d", ((IWineD3DPixelShaderImpl*)pshader)->luminanceconst[i].texunit);
3262 entry->luminancescale_location[i] = GL_EXTCALL(glGetUniformLocationARB(programId, name));
3263 sprintf(name, "luminanceoffset%d", ((IWineD3DPixelShaderImpl*)pshader)->luminanceconst[i].texunit);
3264 entry->luminanceoffset_location[i] = GL_EXTCALL(glGetUniformLocationARB(programId, name));
3269 entry->posFixup_location = GL_EXTCALL(glGetUniformLocationARB(programId, "posFixup"));
3270 entry->ycorrection_location = GL_EXTCALL(glGetUniformLocationARB(programId, "ycorrection"));
3271 checkGLcall("Find glsl program uniform locations");
3273 if (pshader && WINED3DSHADER_VERSION_MAJOR(((IWineD3DPixelShaderImpl *)pshader)->baseShader.hex_version) >= 3
3274 && ((IWineD3DPixelShaderImpl *)pshader)->declared_in_count > GL_LIMITS(glsl_varyings) / 4) {
3275 TRACE("Shader %d needs vertex color clamping disabled\n", programId);
3276 entry->vertex_color_clamp = GL_FALSE;
3277 } else {
3278 entry->vertex_color_clamp = GL_FIXED_ONLY_ARB;
3281 /* Set the shader to allow uniform loading on it */
3282 GL_EXTCALL(glUseProgramObjectARB(programId));
3283 checkGLcall("glUseProgramObjectARB(programId)");
3285 /* Load the vertex and pixel samplers now. The function that finds the mappings makes sure
3286 * that it stays the same for each vertexshader-pixelshader pair(=linked glsl program). If
3287 * a pshader with fixed function pipeline is used there are no vertex samplers, and if a
3288 * vertex shader with fixed function pixel processing is used we make sure that the card
3289 * supports enough samplers to allow the max number of vertex samplers with all possible
3290 * fixed function fragment processing setups. So once the program is linked these samplers
3291 * won't change.
3293 if(vshader_id) {
3294 /* Load vertex shader samplers */
3295 shader_glsl_load_vsamplers(gl_info, (IWineD3DStateBlock*)This->stateBlock, programId);
3297 if(pshader_id) {
3298 /* Load pixel shader samplers */
3299 shader_glsl_load_psamplers(gl_info, (IWineD3DStateBlock*)This->stateBlock, programId);
3302 /* If the local constants do not have to be loaded with the environment constants,
3303 * load them now to have them hardcoded in the GLSL program. This saves some CPU cycles
3304 * later
3306 if(pshader && !((IWineD3DPixelShaderImpl*)pshader)->baseShader.load_local_constsF) {
3307 hardcode_local_constants((IWineD3DBaseShaderImpl *) pshader, gl_info, programId, 'P');
3309 if(vshader && !((IWineD3DVertexShaderImpl*)vshader)->baseShader.load_local_constsF) {
3310 hardcode_local_constants((IWineD3DBaseShaderImpl *) vshader, gl_info, programId, 'V');
3314 static GLhandleARB create_glsl_blt_shader(const WineD3D_GL_Info *gl_info, enum tex_types tex_type)
3316 GLhandleARB program_id;
3317 GLhandleARB vshader_id, pshader_id;
3318 static const char *blt_vshader[] =
3320 "#version 120\n"
3321 "void main(void)\n"
3322 "{\n"
3323 " gl_Position = gl_Vertex;\n"
3324 " gl_FrontColor = vec4(1.0);\n"
3325 " gl_TexCoord[0] = gl_MultiTexCoord0;\n"
3326 "}\n"
3329 static const char *blt_pshaders[tex_type_count] =
3331 /* tex_1d */
3332 NULL,
3333 /* tex_2d */
3334 "#version 120\n"
3335 "uniform sampler2D sampler;\n"
3336 "void main(void)\n"
3337 "{\n"
3338 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
3339 "}\n",
3340 /* tex_3d */
3341 NULL,
3342 /* tex_cube */
3343 "#version 120\n"
3344 "uniform samplerCube sampler;\n"
3345 "void main(void)\n"
3346 "{\n"
3347 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
3348 "}\n",
3349 /* tex_rect */
3350 "#version 120\n"
3351 "#extension GL_ARB_texture_rectangle : enable\n"
3352 "uniform sampler2DRect sampler;\n"
3353 "void main(void)\n"
3354 "{\n"
3355 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
3356 "}\n",
3359 if (!blt_pshaders[tex_type])
3361 FIXME("tex_type %#x not supported\n", tex_type);
3362 tex_type = tex_2d;
3365 vshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
3366 GL_EXTCALL(glShaderSourceARB(vshader_id, 1, blt_vshader, NULL));
3367 GL_EXTCALL(glCompileShaderARB(vshader_id));
3369 pshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
3370 GL_EXTCALL(glShaderSourceARB(pshader_id, 1, &blt_pshaders[tex_type], NULL));
3371 GL_EXTCALL(glCompileShaderARB(pshader_id));
3373 program_id = GL_EXTCALL(glCreateProgramObjectARB());
3374 GL_EXTCALL(glAttachObjectARB(program_id, vshader_id));
3375 GL_EXTCALL(glAttachObjectARB(program_id, pshader_id));
3376 GL_EXTCALL(glLinkProgramARB(program_id));
3378 print_glsl_info_log(&GLINFO_LOCATION, program_id);
3380 /* Once linked we can mark the shaders for deletion. They will be deleted once the program
3381 * is destroyed
3383 GL_EXTCALL(glDeleteObjectARB(vshader_id));
3384 GL_EXTCALL(glDeleteObjectARB(pshader_id));
3385 return program_id;
3388 static void shader_glsl_select(IWineD3DDevice *iface, BOOL usePS, BOOL useVS) {
3389 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
3390 struct shader_glsl_priv *priv = (struct shader_glsl_priv *)This->shader_priv;
3391 const WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
3392 GLhandleARB program_id = 0;
3393 GLenum old_vertex_color_clamp, current_vertex_color_clamp;
3395 old_vertex_color_clamp = priv->glsl_program ? priv->glsl_program->vertex_color_clamp : GL_FIXED_ONLY_ARB;
3397 if (useVS || usePS) set_glsl_shader_program(iface, usePS, useVS);
3398 else priv->glsl_program = NULL;
3400 current_vertex_color_clamp = priv->glsl_program ? priv->glsl_program->vertex_color_clamp : GL_FIXED_ONLY_ARB;
3402 if (old_vertex_color_clamp != current_vertex_color_clamp) {
3403 if (GL_SUPPORT(ARB_COLOR_BUFFER_FLOAT)) {
3404 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, current_vertex_color_clamp));
3405 checkGLcall("glClampColorARB");
3406 } else {
3407 FIXME("vertex color clamp needs to be changed, but extension not supported.\n");
3411 program_id = priv->glsl_program ? priv->glsl_program->programId : 0;
3412 if (program_id) TRACE("Using GLSL program %u\n", program_id);
3413 GL_EXTCALL(glUseProgramObjectARB(program_id));
3414 checkGLcall("glUseProgramObjectARB");
3417 static void shader_glsl_select_depth_blt(IWineD3DDevice *iface, enum tex_types tex_type) {
3418 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
3419 const WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
3420 struct shader_glsl_priv *priv = (struct shader_glsl_priv *) This->shader_priv;
3421 GLhandleARB *blt_program = &priv->depth_blt_program[tex_type];
3423 if (!*blt_program) {
3424 GLhandleARB loc;
3425 *blt_program = create_glsl_blt_shader(gl_info, tex_type);
3426 loc = GL_EXTCALL(glGetUniformLocationARB(*blt_program, "sampler"));
3427 GL_EXTCALL(glUseProgramObjectARB(*blt_program));
3428 GL_EXTCALL(glUniform1iARB(loc, 0));
3429 } else {
3430 GL_EXTCALL(glUseProgramObjectARB(*blt_program));
3434 static void shader_glsl_deselect_depth_blt(IWineD3DDevice *iface) {
3435 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
3436 const WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
3437 struct shader_glsl_priv *priv = (struct shader_glsl_priv *) This->shader_priv;
3438 GLhandleARB program_id;
3440 program_id = priv->glsl_program ? priv->glsl_program->programId : 0;
3441 if (program_id) TRACE("Using GLSL program %u\n", program_id);
3443 GL_EXTCALL(glUseProgramObjectARB(program_id));
3444 checkGLcall("glUseProgramObjectARB");
3447 static void shader_glsl_cleanup(IWineD3DDevice *iface) {
3448 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
3449 const WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
3450 GL_EXTCALL(glUseProgramObjectARB(0));
3453 static void shader_glsl_destroy(IWineD3DBaseShader *iface) {
3454 const struct list *linked_programs;
3455 IWineD3DBaseShaderImpl *This = (IWineD3DBaseShaderImpl *) iface;
3456 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *)This->baseShader.device;
3457 struct shader_glsl_priv *priv = (struct shader_glsl_priv *)device->shader_priv;
3458 const WineD3D_GL_Info *gl_info = &device->adapter->gl_info;
3459 IWineD3DPixelShaderImpl *ps = NULL;
3460 IWineD3DVertexShaderImpl *vs = NULL;
3462 /* Note: Do not use QueryInterface here to find out which shader type this is because this code
3463 * can be called from IWineD3DBaseShader::Release
3465 char pshader = shader_is_pshader_version(This->baseShader.hex_version);
3467 if(pshader) {
3468 ps = (IWineD3DPixelShaderImpl *) This;
3469 if(ps->num_gl_shaders == 0) return;
3470 } else {
3471 vs = (IWineD3DVertexShaderImpl *) This;
3472 if(vs->prgId == 0) return;
3475 linked_programs = &This->baseShader.linked_programs;
3477 TRACE("Deleting linked programs\n");
3478 if (linked_programs->next) {
3479 struct glsl_shader_prog_link *entry, *entry2;
3481 if(pshader) {
3482 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs, struct glsl_shader_prog_link, pshader_entry) {
3483 delete_glsl_program_entry(priv, gl_info, entry);
3485 } else {
3486 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs, struct glsl_shader_prog_link, vshader_entry) {
3487 delete_glsl_program_entry(priv, gl_info, entry);
3492 if(pshader) {
3493 UINT i;
3495 ENTER_GL();
3496 for(i = 0; i < ps->num_gl_shaders; i++) {
3497 TRACE("deleting pshader %u\n", ps->gl_shaders[i].prgId);
3498 GL_EXTCALL(glDeleteObjectARB(ps->gl_shaders[i].prgId));
3499 checkGLcall("glDeleteObjectARB");
3501 LEAVE_GL();
3502 HeapFree(GetProcessHeap(), 0, ps->gl_shaders);
3503 ps->gl_shaders = NULL;
3504 ps->num_gl_shaders = 0;
3505 } else {
3506 TRACE("Deleting shader object %u\n", vs->prgId);
3507 ENTER_GL();
3508 GL_EXTCALL(glDeleteObjectARB(vs->prgId));
3509 checkGLcall("glDeleteObjectARB");
3510 LEAVE_GL();
3511 vs->prgId = 0;
3512 vs->baseShader.is_compiled = FALSE;
3516 static unsigned int glsl_program_key_hash(const void *key)
3518 const glsl_program_key_t *k = (const glsl_program_key_t *)key;
3520 unsigned int hash = k->vshader | ((DWORD_PTR) k->pshader) << 16;
3521 hash += ~(hash << 15);
3522 hash ^= (hash >> 10);
3523 hash += (hash << 3);
3524 hash ^= (hash >> 6);
3525 hash += ~(hash << 11);
3526 hash ^= (hash >> 16);
3528 return hash;
3531 static BOOL glsl_program_key_compare(const void *keya, const void *keyb)
3533 const glsl_program_key_t *ka = (const glsl_program_key_t *)keya;
3534 const glsl_program_key_t *kb = (const glsl_program_key_t *)keyb;
3536 return ka->vshader == kb->vshader && ka->pshader == kb->pshader &&
3537 (memcmp(&ka->ps_args, &kb->ps_args, sizeof(kb->ps_args)) == 0);
3540 static HRESULT shader_glsl_alloc(IWineD3DDevice *iface) {
3541 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
3542 struct shader_glsl_priv *priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct shader_glsl_priv));
3543 priv->glsl_program_lookup = hash_table_create(glsl_program_key_hash, glsl_program_key_compare);
3544 This->shader_priv = priv;
3545 return WINED3D_OK;
3548 static void shader_glsl_free(IWineD3DDevice *iface) {
3549 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
3550 const WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
3551 struct shader_glsl_priv *priv = (struct shader_glsl_priv *)This->shader_priv;
3552 int i;
3554 for (i = 0; i < tex_type_count; ++i)
3556 if (priv->depth_blt_program[i])
3558 GL_EXTCALL(glDeleteObjectARB(priv->depth_blt_program[i]));
3562 hash_table_destroy(priv->glsl_program_lookup, NULL, NULL);
3564 HeapFree(GetProcessHeap(), 0, This->shader_priv);
3565 This->shader_priv = NULL;
3568 static BOOL shader_glsl_dirty_const(IWineD3DDevice *iface) {
3569 /* TODO: GL_EXT_bindable_uniform can be used to share constants across shaders */
3570 return FALSE;
3573 static GLuint shader_glsl_generate_pshader(IWineD3DPixelShader *iface, SHADER_BUFFER *buffer) {
3574 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)iface;
3575 const struct shader_reg_maps *reg_maps = &This->baseShader.reg_maps;
3576 CONST DWORD *function = This->baseShader.function;
3577 const char *fragcolor;
3578 const WineD3D_GL_Info *gl_info = &((IWineD3DDeviceImpl *)This->baseShader.device)->adapter->gl_info;
3580 /* Create the hw GLSL shader object and assign it as the shader->prgId */
3581 GLhandleARB shader_obj = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
3583 shader_addline(buffer, "#version 120\n");
3585 if (GL_SUPPORT(ARB_DRAW_BUFFERS)) {
3586 shader_addline(buffer, "#extension GL_ARB_draw_buffers : enable\n");
3588 if (GL_SUPPORT(ARB_TEXTURE_RECTANGLE)) {
3589 /* The spec says that it doesn't have to be explicitly enabled, but the nvidia
3590 * drivers write a warning if we don't do so
3592 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
3595 /* Base Declarations */
3596 shader_generate_glsl_declarations( (IWineD3DBaseShader*) This, reg_maps, buffer, &GLINFO_LOCATION);
3598 /* Pack 3.0 inputs */
3599 if (This->baseShader.hex_version >= WINED3DPS_VERSION(3,0)) {
3601 if(((IWineD3DDeviceImpl *) This->baseShader.device)->strided_streams.u.s.position_transformed) {
3602 pshader_glsl_input_pack(buffer, This->semantics_in, iface, pretransformed);
3603 } else if(!use_vs((IWineD3DDeviceImpl *) This->baseShader.device)) {
3604 pshader_glsl_input_pack(buffer, This->semantics_in, iface, fixedfunction);
3608 /* Base Shader Body */
3609 shader_generate_main( (IWineD3DBaseShader*) This, buffer, reg_maps, function);
3611 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly */
3612 if (This->baseShader.hex_version < WINED3DPS_VERSION(2,0)) {
3613 /* Some older cards like GeforceFX ones don't support multiple buffers, so also not gl_FragData */
3614 if(GL_SUPPORT(ARB_DRAW_BUFFERS))
3615 shader_addline(buffer, "gl_FragData[0] = R0;\n");
3616 else
3617 shader_addline(buffer, "gl_FragColor = R0;\n");
3620 if(GL_SUPPORT(ARB_DRAW_BUFFERS)) {
3621 fragcolor = "gl_FragData[0]";
3622 } else {
3623 fragcolor = "gl_FragColor";
3625 if(((IWineD3DDeviceImpl *)This->baseShader.device)->stateBlock->renderState[WINED3DRS_SRGBWRITEENABLE]) {
3626 shader_addline(buffer, "tmp0.xyz = pow(%s.xyz, vec3(%f, %f, %f)) * vec3(%f, %f, %f) - vec3(%f, %f, %f);\n",
3627 fragcolor, srgb_pow, srgb_pow, srgb_pow, srgb_mul_high, srgb_mul_high, srgb_mul_high,
3628 srgb_sub_high, srgb_sub_high, srgb_sub_high);
3629 shader_addline(buffer, "tmp1.xyz = %s.xyz * srgb_mul_low.xyz;\n", fragcolor);
3630 shader_addline(buffer, "%s.x = %s.x < srgb_comparison.x ? tmp1.x : tmp0.x;\n", fragcolor, fragcolor);
3631 shader_addline(buffer, "%s.y = %s.y < srgb_comparison.y ? tmp1.y : tmp0.y;\n", fragcolor, fragcolor);
3632 shader_addline(buffer, "%s.z = %s.z < srgb_comparison.z ? tmp1.z : tmp0.z;\n", fragcolor, fragcolor);
3633 shader_addline(buffer, "%s = clamp(%s, 0.0, 1.0);\n", fragcolor, fragcolor);
3635 /* Pixel shader < 3.0 do not replace the fog stage.
3636 * This implements linear fog computation and blending.
3637 * TODO: non linear fog
3638 * NOTE: gl_Fog.start and gl_Fog.end don't hold fog start s and end e but
3639 * -1/(e-s) and e/(e-s) respectively.
3641 if(This->baseShader.hex_version < WINED3DPS_VERSION(3,0)) {
3642 shader_addline(buffer, "float Fog = clamp(gl_FogFragCoord * gl_Fog.start + gl_Fog.end, 0.0, 1.0);\n");
3643 shader_addline(buffer, "%s.xyz = mix(gl_Fog.color.xyz, %s.xyz, Fog);\n", fragcolor, fragcolor);
3646 shader_addline(buffer, "}\n");
3648 TRACE("Compiling shader object %u\n", shader_obj);
3649 GL_EXTCALL(glShaderSourceARB(shader_obj, 1, (const char**)&buffer->buffer, NULL));
3650 GL_EXTCALL(glCompileShaderARB(shader_obj));
3651 print_glsl_info_log(&GLINFO_LOCATION, shader_obj);
3653 /* Store the shader object */
3654 return shader_obj;
3657 static void shader_glsl_generate_vshader(IWineD3DVertexShader *iface, SHADER_BUFFER *buffer) {
3658 IWineD3DVertexShaderImpl *This = (IWineD3DVertexShaderImpl *)iface;
3659 const struct shader_reg_maps *reg_maps = &This->baseShader.reg_maps;
3660 CONST DWORD *function = This->baseShader.function;
3661 const WineD3D_GL_Info *gl_info = &((IWineD3DDeviceImpl *)This->baseShader.device)->adapter->gl_info;
3663 /* Create the hw GLSL shader program and assign it as the shader->prgId */
3664 GLhandleARB shader_obj = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
3666 shader_addline(buffer, "#version 120\n");
3668 /* Base Declarations */
3669 shader_generate_glsl_declarations( (IWineD3DBaseShader*) This, reg_maps, buffer, &GLINFO_LOCATION);
3671 /* Base Shader Body */
3672 shader_generate_main( (IWineD3DBaseShader*) This, buffer, reg_maps, function);
3674 /* Unpack 3.0 outputs */
3675 if (This->baseShader.hex_version >= WINED3DVS_VERSION(3,0)) {
3676 shader_addline(buffer, "order_ps_input(OUT);\n");
3677 } else {
3678 shader_addline(buffer, "order_ps_input();\n");
3681 /* If this shader doesn't use fog copy the z coord to the fog coord so that we can use table fog */
3682 if (!reg_maps->fog)
3683 shader_addline(buffer, "gl_FogFragCoord = gl_Position.z;\n");
3685 /* Write the final position.
3687 * OpenGL coordinates specify the center of the pixel while d3d coords specify
3688 * the corner. The offsets are stored in z and w in posFixup. posFixup.y contains
3689 * 1.0 or -1.0 to turn the rendering upside down for offscreen rendering. PosFixup.x
3690 * contains 1.0 to allow a mad.
3692 shader_addline(buffer, "gl_Position.y = gl_Position.y * posFixup.y;\n");
3693 shader_addline(buffer, "gl_Position.xy += posFixup.zw * gl_Position.ww;\n");
3695 /* Z coord [0;1]->[-1;1] mapping, see comment in transform_projection in state.c
3697 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However, shaders are run
3698 * before the homogeneous divide, so we have to take the w into account: z = ((z / w) * 2 - 1) * w,
3699 * which is the same as z = z * 2 - w.
3701 shader_addline(buffer, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
3703 shader_addline(buffer, "}\n");
3705 TRACE("Compiling shader object %u\n", shader_obj);
3706 GL_EXTCALL(glShaderSourceARB(shader_obj, 1, (const char**)&buffer->buffer, NULL));
3707 GL_EXTCALL(glCompileShaderARB(shader_obj));
3708 print_glsl_info_log(&GLINFO_LOCATION, shader_obj);
3710 /* Store the shader object */
3711 This->prgId = shader_obj;
3714 static void shader_glsl_get_caps(WINED3DDEVTYPE devtype, const WineD3D_GL_Info *gl_info, struct shader_caps *pCaps)
3716 /* Nvidia Geforce6/7 or Ati R4xx/R5xx cards with GLSL support, support VS 3.0 but older Nvidia/Ati
3717 * models with GLSL support only support 2.0. In case of nvidia we can detect VS 2.0 support using
3718 * vs_nv_version which is based on NV_vertex_program.
3719 * For Ati cards there's no way using glsl (it abstracts the lowlevel info away) and also not
3720 * using ARB_vertex_program. It is safe to assume that when a card supports pixel shader 2.0 it
3721 * supports vertex shader 2.0 too and the way around. We can detect ps2.0 using the maximum number
3722 * of native instructions, so use that here. For more info see the pixel shader versioning code below.
3724 if((GLINFO_LOCATION.vs_nv_version == VS_VERSION_20) || (GLINFO_LOCATION.ps_arb_max_instructions <= 512))
3725 pCaps->VertexShaderVersion = WINED3DVS_VERSION(2,0);
3726 else
3727 pCaps->VertexShaderVersion = WINED3DVS_VERSION(3,0);
3728 TRACE_(d3d_caps)("Hardware vertex shader version %d.%d enabled (GLSL)\n", (pCaps->VertexShaderVersion >> 8) & 0xff, pCaps->VertexShaderVersion & 0xff);
3729 pCaps->MaxVertexShaderConst = GL_LIMITS(vshader_constantsF);
3731 /* Older DX9-class videocards (GeforceFX / Radeon >9500/X*00) only support pixel shader 2.0/2.0a/2.0b.
3732 * In OpenGL the extensions related to GLSL abstract lowlevel GL info away which is needed
3733 * to distinguish between 2.0 and 3.0 (and 2.0a/2.0b). In case of Nvidia we use their fragment
3734 * program extensions. On other hardware including ATI GL_ARB_fragment_program offers the info
3735 * in max native instructions. Intel and others also offer the info in this extension but they
3736 * don't support GLSL (at least on Windows).
3738 * PS2.0 requires at least 96 instructions, 2.0a/2.0b go up to 512. Assume that if the number
3739 * of instructions is 512 or less we have to do with ps2.0 hardware.
3740 * 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.
3742 if((GLINFO_LOCATION.ps_nv_version == PS_VERSION_20) || (GLINFO_LOCATION.ps_arb_max_instructions <= 512))
3743 pCaps->PixelShaderVersion = WINED3DPS_VERSION(2,0);
3744 else
3745 pCaps->PixelShaderVersion = WINED3DPS_VERSION(3,0);
3747 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
3748 * Direct3D minimum requirement.
3750 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
3751 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
3753 * The problem is that the refrast clamps temporary results in the shader to
3754 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
3755 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
3756 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
3757 * offer a way to query this.
3759 pCaps->PixelShader1xMaxValue = 8.0;
3760 TRACE_(d3d_caps)("Hardware pixel shader version %d.%d enabled (GLSL)\n", (pCaps->PixelShaderVersion >> 8) & 0xff, pCaps->PixelShaderVersion & 0xff);
3763 static BOOL shader_glsl_color_fixup_supported(struct color_fixup_desc fixup)
3765 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
3767 TRACE("Checking support for fixup:\n");
3768 dump_color_fixup_desc(fixup);
3771 /* We support everything except YUV conversions. */
3772 if (!is_yuv_fixup(fixup))
3774 TRACE("[OK]\n");
3775 return TRUE;
3778 TRACE("[FAILED]\n");
3779 return FALSE;
3782 static const SHADER_HANDLER shader_glsl_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
3784 /* WINED3DSIH_ABS */ shader_glsl_map2gl,
3785 /* WINED3DSIH_ADD */ shader_glsl_arith,
3786 /* WINED3DSIH_BEM */ pshader_glsl_bem,
3787 /* WINED3DSIH_BREAK */ shader_glsl_break,
3788 /* WINED3DSIH_BREAKC */ shader_glsl_breakc,
3789 /* WINED3DSIH_BREAKP */ NULL,
3790 /* WINED3DSIH_CALL */ shader_glsl_call,
3791 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz,
3792 /* WINED3DSIH_CMP */ shader_glsl_cmp,
3793 /* WINED3DSIH_CND */ shader_glsl_cnd,
3794 /* WINED3DSIH_CRS */ shader_glsl_cross,
3795 /* WINED3DSIH_DCL */ NULL,
3796 /* WINED3DSIH_DEF */ NULL,
3797 /* WINED3DSIH_DEFB */ NULL,
3798 /* WINED3DSIH_DEFI */ NULL,
3799 /* WINED3DSIH_DP2ADD */ pshader_glsl_dp2add,
3800 /* WINED3DSIH_DP3 */ shader_glsl_dot,
3801 /* WINED3DSIH_DP4 */ shader_glsl_dot,
3802 /* WINED3DSIH_DST */ shader_glsl_dst,
3803 /* WINED3DSIH_DSX */ shader_glsl_map2gl,
3804 /* WINED3DSIH_DSY */ shader_glsl_map2gl,
3805 /* WINED3DSIH_ELSE */ shader_glsl_else,
3806 /* WINED3DSIH_ENDIF */ shader_glsl_end,
3807 /* WINED3DSIH_ENDLOOP */ shader_glsl_end,
3808 /* WINED3DSIH_ENDREP */ shader_glsl_end,
3809 /* WINED3DSIH_EXP */ shader_glsl_map2gl,
3810 /* WINED3DSIH_EXPP */ shader_glsl_expp,
3811 /* WINED3DSIH_FRC */ shader_glsl_map2gl,
3812 /* WINED3DSIH_IF */ shader_glsl_if,
3813 /* WINED3DSIH_IFC */ shader_glsl_ifc,
3814 /* WINED3DSIH_LABEL */ shader_glsl_label,
3815 /* WINED3DSIH_LIT */ shader_glsl_lit,
3816 /* WINED3DSIH_LOG */ shader_glsl_log,
3817 /* WINED3DSIH_LOGP */ shader_glsl_log,
3818 /* WINED3DSIH_LOOP */ shader_glsl_loop,
3819 /* WINED3DSIH_LRP */ shader_glsl_lrp,
3820 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn,
3821 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn,
3822 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn,
3823 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn,
3824 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn,
3825 /* WINED3DSIH_MAD */ shader_glsl_mad,
3826 /* WINED3DSIH_MAX */ shader_glsl_map2gl,
3827 /* WINED3DSIH_MIN */ shader_glsl_map2gl,
3828 /* WINED3DSIH_MOV */ shader_glsl_mov,
3829 /* WINED3DSIH_MOVA */ shader_glsl_mov,
3830 /* WINED3DSIH_MUL */ shader_glsl_arith,
3831 /* WINED3DSIH_NOP */ NULL,
3832 /* WINED3DSIH_NRM */ shader_glsl_map2gl,
3833 /* WINED3DSIH_PHASE */ NULL,
3834 /* WINED3DSIH_POW */ shader_glsl_pow,
3835 /* WINED3DSIH_RCP */ shader_glsl_rcp,
3836 /* WINED3DSIH_REP */ shader_glsl_rep,
3837 /* WINED3DSIH_RET */ NULL,
3838 /* WINED3DSIH_RSQ */ shader_glsl_rsq,
3839 /* WINED3DSIH_SETP */ NULL,
3840 /* WINED3DSIH_SGE */ shader_glsl_compare,
3841 /* WINED3DSIH_SGN */ shader_glsl_map2gl,
3842 /* WINED3DSIH_SINCOS */ shader_glsl_sincos,
3843 /* WINED3DSIH_SLT */ shader_glsl_compare,
3844 /* WINED3DSIH_SUB */ shader_glsl_arith,
3845 /* WINED3DSIH_TEX */ pshader_glsl_tex,
3846 /* WINED3DSIH_TEXBEM */ pshader_glsl_texbem,
3847 /* WINED3DSIH_TEXBEML */ pshader_glsl_texbem,
3848 /* WINED3DSIH_TEXCOORD */ pshader_glsl_texcoord,
3849 /* WINED3DSIH_TEXDEPTH */ pshader_glsl_texdepth,
3850 /* WINED3DSIH_TEXDP3 */ pshader_glsl_texdp3,
3851 /* WINED3DSIH_TEXDP3TEX */ pshader_glsl_texdp3tex,
3852 /* WINED3DSIH_TEXKILL */ pshader_glsl_texkill,
3853 /* WINED3DSIH_TEXLDD */ NULL,
3854 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl,
3855 /* WINED3DSIH_TEXM3x2DEPTH */ pshader_glsl_texm3x2depth,
3856 /* WINED3DSIH_TEXM3x2PAD */ pshader_glsl_texm3x2pad,
3857 /* WINED3DSIH_TEXM3x2TEX */ pshader_glsl_texm3x2tex,
3858 /* WINED3DSIH_TEXM3x3 */ pshader_glsl_texm3x3,
3859 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
3860 /* WINED3DSIH_TEXM3x3PAD */ pshader_glsl_texm3x3pad,
3861 /* WINED3DSIH_TEXM3x3SPEC */ pshader_glsl_texm3x3spec,
3862 /* WINED3DSIH_TEXM3x3TEX */ pshader_glsl_texm3x3tex,
3863 /* WINED3DSIH_TEXM3x3VSPEC */ pshader_glsl_texm3x3vspec,
3864 /* WINED3DSIH_TEXREG2AR */ pshader_glsl_texreg2ar,
3865 /* WINED3DSIH_TEXREG2GB */ pshader_glsl_texreg2gb,
3866 /* WINED3DSIH_TEXREG2RGB */ pshader_glsl_texreg2rgb,
3869 const shader_backend_t glsl_shader_backend = {
3870 shader_glsl_instruction_handler_table,
3871 shader_glsl_select,
3872 shader_glsl_select_depth_blt,
3873 shader_glsl_deselect_depth_blt,
3874 shader_glsl_load_constants,
3875 shader_glsl_cleanup,
3876 shader_glsl_color_correction,
3877 shader_glsl_destroy,
3878 shader_glsl_alloc,
3879 shader_glsl_free,
3880 shader_glsl_dirty_const,
3881 shader_glsl_generate_pshader,
3882 shader_glsl_generate_vshader,
3883 shader_glsl_get_caps,
3884 shader_glsl_color_fixup_supported,