wined3d: Sign-compare warnings fix.
[wine/wine64.git] / dlls / wined3d / glsl_shader.c
blob8f9ec1658c6395b27fc14834c8acbc4f50c24bb3
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);
39 #define GLINFO_LOCATION (*gl_info)
41 typedef struct {
42 char reg_name[150];
43 char mask_str[6];
44 } glsl_dst_param_t;
46 typedef struct {
47 char reg_name[150];
48 char param_str[100];
49 } glsl_src_param_t;
51 typedef struct {
52 const char *name;
53 DWORD coord_mask;
54 } glsl_sample_function_t;
56 /* GLSL shader private data */
57 struct shader_glsl_priv {
58 struct hash_table_t *glsl_program_lookup;
59 const struct glsl_shader_prog_link *glsl_program;
60 GLhandleARB depth_blt_program[tex_type_count];
63 /* Struct to maintain data about a linked GLSL program */
64 struct glsl_shader_prog_link {
65 struct list vshader_entry;
66 struct list pshader_entry;
67 GLhandleARB programId;
68 GLhandleARB *vuniformF_locations;
69 GLhandleARB *puniformF_locations;
70 GLhandleARB vuniformI_locations[MAX_CONST_I];
71 GLhandleARB puniformI_locations[MAX_CONST_I];
72 GLhandleARB posFixup_location;
73 GLhandleARB bumpenvmat_location[MAX_TEXTURES];
74 GLhandleARB luminancescale_location[MAX_TEXTURES];
75 GLhandleARB luminanceoffset_location[MAX_TEXTURES];
76 GLhandleARB ycorrection_location;
77 GLenum vertex_color_clamp;
78 GLhandleARB vshader;
79 IWineD3DPixelShader *pshader;
80 struct ps_compile_args ps_args;
83 typedef struct {
84 GLhandleARB vshader;
85 IWineD3DPixelShader *pshader;
86 struct ps_compile_args ps_args;
87 } glsl_program_key_t;
90 /** Prints the GLSL info log which will contain error messages if they exist */
91 static void print_glsl_info_log(const WineD3D_GL_Info *gl_info, GLhandleARB obj)
93 int infologLength = 0;
94 char *infoLog;
95 unsigned int i;
96 BOOL is_spam;
98 const char *spam[] = {
99 "Vertex shader was successfully compiled to run on hardware.\n", /* fglrx */
100 "Fragment shader was successfully compiled to run on hardware.\n", /* fglrx */
101 "Fragment shader(s) linked, vertex shader(s) linked. \n ", /* fglrx, with \n */
102 "Fragment shader(s) linked, vertex shader(s) linked.", /* fglrx, no \n */
103 "Vertex shader(s) linked, no fragment shader(s) defined. \n ", /* fglrx, with \n */
104 "Vertex shader(s) linked, no fragment shader(s) defined.", /* fglrx, no \n */
105 "Fragment shader was successfully compiled to run on hardware.\nWARNING: 0:1: extension 'GL_ARB_draw_buffers' is not supported",
106 "Fragment shader(s) linked, no vertex shader(s) defined.", /* fglrx, no \n */
107 "Fragment shader(s) linked, no vertex shader(s) defined. \n ", /* fglrx, with \n */
108 "WARNING: 0:2: extension 'GL_ARB_draw_buffers' is not supported\n" /* MacOS ati */
111 GL_EXTCALL(glGetObjectParameterivARB(obj,
112 GL_OBJECT_INFO_LOG_LENGTH_ARB,
113 &infologLength));
115 /* A size of 1 is just a null-terminated string, so the log should be bigger than
116 * that if there are errors. */
117 if (infologLength > 1)
119 /* Fglrx doesn't terminate the string properly, but it tells us the proper length.
120 * So use HEAP_ZERO_MEMORY to avoid uninitialized bytes
122 infoLog = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, infologLength);
123 GL_EXTCALL(glGetInfoLogARB(obj, infologLength, NULL, infoLog));
124 is_spam = FALSE;
126 for(i = 0; i < sizeof(spam) / sizeof(spam[0]); i++) {
127 if(strcmp(infoLog, spam[i]) == 0) {
128 is_spam = TRUE;
129 break;
132 if(is_spam) {
133 TRACE("Spam received from GLSL shader #%u: %s\n", obj, debugstr_a(infoLog));
134 } else {
135 FIXME("Error received from GLSL shader #%u: %s\n", obj, debugstr_a(infoLog));
137 HeapFree(GetProcessHeap(), 0, infoLog);
142 * Loads (pixel shader) samplers
144 static void shader_glsl_load_psamplers(const WineD3D_GL_Info *gl_info, IWineD3DStateBlock *iface, GLhandleARB programId)
146 IWineD3DStateBlockImpl* stateBlock = (IWineD3DStateBlockImpl*) iface;
147 GLhandleARB name_loc;
148 int i;
149 char sampler_name[20];
151 for (i = 0; i < MAX_FRAGMENT_SAMPLERS; ++i) {
152 snprintf(sampler_name, sizeof(sampler_name), "Psampler%d", i);
153 name_loc = GL_EXTCALL(glGetUniformLocationARB(programId, sampler_name));
154 if (name_loc != -1) {
155 int mapped_unit = stateBlock->wineD3DDevice->texUnitMap[i];
156 if (mapped_unit != -1 && mapped_unit < GL_LIMITS(fragment_samplers)) {
157 TRACE("Loading %s for texture %d\n", sampler_name, mapped_unit);
158 GL_EXTCALL(glUniform1iARB(name_loc, mapped_unit));
159 checkGLcall("glUniform1iARB");
160 } else {
161 ERR("Trying to load sampler %s on unsupported unit %d\n", sampler_name, mapped_unit);
167 static void shader_glsl_load_vsamplers(const WineD3D_GL_Info *gl_info, IWineD3DStateBlock *iface, GLhandleARB programId)
169 IWineD3DStateBlockImpl* stateBlock = (IWineD3DStateBlockImpl*) iface;
170 GLhandleARB name_loc;
171 char sampler_name[20];
172 int i;
174 for (i = 0; i < MAX_VERTEX_SAMPLERS; ++i) {
175 snprintf(sampler_name, sizeof(sampler_name), "Vsampler%d", i);
176 name_loc = GL_EXTCALL(glGetUniformLocationARB(programId, sampler_name));
177 if (name_loc != -1) {
178 int mapped_unit = stateBlock->wineD3DDevice->texUnitMap[MAX_FRAGMENT_SAMPLERS + i];
179 if (mapped_unit != -1 && mapped_unit < GL_LIMITS(combined_samplers)) {
180 TRACE("Loading %s for texture %d\n", sampler_name, mapped_unit);
181 GL_EXTCALL(glUniform1iARB(name_loc, mapped_unit));
182 checkGLcall("glUniform1iARB");
183 } else {
184 ERR("Trying to load sampler %s on unsupported unit %d\n", sampler_name, mapped_unit);
190 /**
191 * Loads floating point constants (aka uniforms) into the currently set GLSL program.
192 * When constant_list == NULL, it will load all the constants.
194 static void shader_glsl_load_constantsF(IWineD3DBaseShaderImpl *This, const WineD3D_GL_Info *gl_info,
195 unsigned int max_constants, const float *constants, const GLhandleARB *constant_locations,
196 const struct list *constant_list)
198 const constants_entry *constant;
199 const local_constant *lconst;
200 GLhandleARB tmp_loc;
201 DWORD i, j, k;
202 const DWORD *idx;
204 if (TRACE_ON(d3d_shader)) {
205 LIST_FOR_EACH_ENTRY(constant, constant_list, constants_entry, entry) {
206 idx = constant->idx;
207 j = constant->count;
208 while (j--) {
209 i = *idx++;
210 tmp_loc = constant_locations[i];
211 if (tmp_loc != -1) {
212 TRACE_(d3d_constants)("Loading constants %i: %f, %f, %f, %f\n", i,
213 constants[i * 4 + 0], constants[i * 4 + 1],
214 constants[i * 4 + 2], constants[i * 4 + 3]);
220 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
221 if(WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) == 1 &&
222 shader_is_pshader_version(This->baseShader.hex_version)) {
223 float lcl_const[4];
225 LIST_FOR_EACH_ENTRY(constant, constant_list, constants_entry, entry) {
226 idx = constant->idx;
227 j = constant->count;
228 while (j--) {
229 i = *idx++;
230 tmp_loc = constant_locations[i];
231 if (tmp_loc != -1) {
232 /* We found this uniform name in the program - go ahead and send the data */
233 k = i * 4;
234 if(constants[k + 0] < -1.0) lcl_const[0] = -1.0;
235 else if(constants[k + 0] > 1.0) lcl_const[0] = 1.0;
236 else lcl_const[0] = constants[k + 0];
237 if(constants[k + 1] < -1.0) lcl_const[1] = -1.0;
238 else if(constants[k + 1] > 1.0) lcl_const[1] = 1.0;
239 else lcl_const[1] = constants[k + 1];
240 if(constants[k + 2] < -1.0) lcl_const[2] = -1.0;
241 else if(constants[k + 2] > 1.0) lcl_const[2] = 1.0;
242 else lcl_const[2] = constants[k + 2];
243 if(constants[k + 3] < -1.0) lcl_const[3] = -1.0;
244 else if(constants[k + 3] > 1.0) lcl_const[3] = 1.0;
245 else lcl_const[3] = constants[k + 3];
247 GL_EXTCALL(glUniform4fvARB(tmp_loc, 1, lcl_const));
251 } else {
252 LIST_FOR_EACH_ENTRY(constant, constant_list, constants_entry, entry) {
253 idx = constant->idx;
254 j = constant->count;
255 while (j--) {
256 i = *idx++;
257 tmp_loc = constant_locations[i];
258 if (tmp_loc != -1) {
259 /* We found this uniform name in the program - go ahead and send the data */
260 GL_EXTCALL(glUniform4fvARB(tmp_loc, 1, constants + (i * 4)));
265 checkGLcall("glUniform4fvARB()");
267 if(!This->baseShader.load_local_constsF) {
268 TRACE("No need to load local float constants for this shader\n");
269 return;
272 /* Load immediate constants */
273 if (TRACE_ON(d3d_shader)) {
274 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
275 tmp_loc = constant_locations[lconst->idx];
276 if (tmp_loc != -1) {
277 const GLfloat *values = (const GLfloat *)lconst->value;
278 TRACE_(d3d_constants)("Loading local constants %i: %f, %f, %f, %f\n", lconst->idx,
279 values[0], values[1], values[2], values[3]);
283 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
284 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
285 tmp_loc = constant_locations[lconst->idx];
286 if (tmp_loc != -1) {
287 /* We found this uniform name in the program - go ahead and send the data */
288 GL_EXTCALL(glUniform4fvARB(tmp_loc, 1, (const GLfloat *)lconst->value));
291 checkGLcall("glUniform4fvARB()");
294 /**
295 * Loads integer constants (aka uniforms) into the currently set GLSL program.
296 * When @constants_set == NULL, it will load all the constants.
298 static void shader_glsl_load_constantsI(IWineD3DBaseShaderImpl *This, const WineD3D_GL_Info *gl_info,
299 GLhandleARB programId, const GLhandleARB locations[MAX_CONST_I], unsigned int max_constants,
300 const int *constants, const BOOL *constants_set)
302 unsigned int i;
303 struct list* ptr;
305 for (i=0; i<max_constants; ++i) {
306 if (NULL == constants_set || constants_set[i]) {
308 TRACE_(d3d_constants)("Loading constants %u: %i, %i, %i, %i\n",
309 i, constants[i*4], constants[i*4+1], constants[i*4+2], constants[i*4+3]);
311 /* We found this uniform name in the program - go ahead and send the data */
312 GL_EXTCALL(glUniform4ivARB(locations[i], 1, &constants[i*4]));
313 checkGLcall("glUniform4ivARB");
317 /* Load immediate constants */
318 ptr = list_head(&This->baseShader.constantsI);
319 while (ptr) {
320 const struct local_constant *lconst = LIST_ENTRY(ptr, const struct local_constant, entry);
321 unsigned int idx = lconst->idx;
322 const GLint *values = (const GLint *)lconst->value;
324 TRACE_(d3d_constants)("Loading local constants %i: %i, %i, %i, %i\n", idx,
325 values[0], values[1], values[2], values[3]);
327 /* We found this uniform name in the program - go ahead and send the data */
328 GL_EXTCALL(glUniform4ivARB(locations[idx], 1, values));
329 checkGLcall("glUniform4ivARB");
330 ptr = list_next(&This->baseShader.constantsI, ptr);
334 /**
335 * Loads boolean constants (aka uniforms) into the currently set GLSL program.
336 * When @constants_set == NULL, it will load all the constants.
338 static void shader_glsl_load_constantsB(IWineD3DBaseShaderImpl *This, const WineD3D_GL_Info *gl_info,
339 GLhandleARB programId, unsigned int max_constants, const BOOL *constants, const BOOL *constants_set)
341 GLhandleARB tmp_loc;
342 unsigned int i;
343 char tmp_name[8];
344 char is_pshader = shader_is_pshader_version(This->baseShader.hex_version);
345 const char* prefix = is_pshader? "PB":"VB";
346 struct list* ptr;
348 for (i=0; i<max_constants; ++i) {
349 if (NULL == constants_set || constants_set[i]) {
351 TRACE_(d3d_constants)("Loading constants %u: %i;\n", i, constants[i]);
353 /* TODO: Benchmark and see if it would be beneficial to store the
354 * locations of the constants to avoid looking up each time */
355 snprintf(tmp_name, sizeof(tmp_name), "%s[%u]", prefix, i);
356 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
357 if (tmp_loc != -1) {
358 /* We found this uniform name in the program - go ahead and send the data */
359 GL_EXTCALL(glUniform1ivARB(tmp_loc, 1, &constants[i]));
360 checkGLcall("glUniform1ivARB");
365 /* Load immediate constants */
366 ptr = list_head(&This->baseShader.constantsB);
367 while (ptr) {
368 const struct local_constant *lconst = LIST_ENTRY(ptr, const struct local_constant, entry);
369 unsigned int idx = lconst->idx;
370 const GLint *values = (const GLint *)lconst->value;
372 TRACE_(d3d_constants)("Loading local constants %i: %i\n", idx, values[0]);
374 snprintf(tmp_name, sizeof(tmp_name), "%s[%i]", prefix, idx);
375 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
376 if (tmp_loc != -1) {
377 /* We found this uniform name in the program - go ahead and send the data */
378 GL_EXTCALL(glUniform1ivARB(tmp_loc, 1, values));
379 checkGLcall("glUniform1ivARB");
381 ptr = list_next(&This->baseShader.constantsB, ptr);
388 * Loads the app-supplied constants into the currently set GLSL program.
390 static void shader_glsl_load_constants(
391 IWineD3DDevice* device,
392 char usePixelShader,
393 char useVertexShader) {
395 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) device;
396 const struct shader_glsl_priv *priv = (struct shader_glsl_priv *)deviceImpl->shader_priv;
397 IWineD3DStateBlockImpl* stateBlock = deviceImpl->stateBlock;
398 const WineD3D_GL_Info *gl_info = &deviceImpl->adapter->gl_info;
400 const GLhandleARB *constant_locations;
401 const struct list *constant_list;
402 GLhandleARB programId;
403 const struct glsl_shader_prog_link *prog = priv->glsl_program;
404 int i;
406 if (!prog) {
407 /* No GLSL program set - nothing to do. */
408 return;
410 programId = prog->programId;
412 if (useVertexShader) {
413 IWineD3DBaseShaderImpl* vshader = (IWineD3DBaseShaderImpl*) stateBlock->vertexShader;
415 constant_locations = prog->vuniformF_locations;
416 constant_list = &stateBlock->set_vconstantsF;
418 /* Load DirectX 9 float constants/uniforms for vertex shader */
419 shader_glsl_load_constantsF(vshader, gl_info, GL_LIMITS(vshader_constantsF),
420 stateBlock->vertexShaderConstantF, constant_locations, constant_list);
422 /* Load DirectX 9 integer constants/uniforms for vertex shader */
423 shader_glsl_load_constantsI(vshader, gl_info, programId,
424 prog->vuniformI_locations, MAX_CONST_I,
425 stateBlock->vertexShaderConstantI,
426 stateBlock->changed.vertexShaderConstantsI);
428 /* Load DirectX 9 boolean constants/uniforms for vertex shader */
429 shader_glsl_load_constantsB(vshader, gl_info, programId, MAX_CONST_B,
430 stateBlock->vertexShaderConstantB,
431 stateBlock->changed.vertexShaderConstantsB);
433 /* Upload the position fixup params */
434 GL_EXTCALL(glUniform4fvARB(prog->posFixup_location, 1, &deviceImpl->posFixup[0]));
435 checkGLcall("glUniform4fvARB");
438 if (usePixelShader) {
440 IWineD3DBaseShaderImpl* pshader = (IWineD3DBaseShaderImpl*) stateBlock->pixelShader;
442 constant_locations = prog->puniformF_locations;
443 constant_list = &stateBlock->set_pconstantsF;
445 /* Load DirectX 9 float constants/uniforms for pixel shader */
446 shader_glsl_load_constantsF(pshader, gl_info, GL_LIMITS(pshader_constantsF),
447 stateBlock->pixelShaderConstantF, constant_locations, constant_list);
449 /* Load DirectX 9 integer constants/uniforms for pixel shader */
450 shader_glsl_load_constantsI(pshader, gl_info, programId,
451 prog->puniformI_locations, MAX_CONST_I,
452 stateBlock->pixelShaderConstantI,
453 stateBlock->changed.pixelShaderConstantsI);
455 /* Load DirectX 9 boolean constants/uniforms for pixel shader */
456 shader_glsl_load_constantsB(pshader, gl_info, programId, MAX_CONST_B,
457 stateBlock->pixelShaderConstantB,
458 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_color_correction(const SHADER_OPCODE_ARG *arg)
1177 IWineD3DBaseShaderImpl* shader = (IWineD3DBaseShaderImpl*) arg->shader;
1178 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) shader->baseShader.device;
1179 const WineD3D_GL_Info *gl_info = &deviceImpl->adapter->gl_info;
1180 glsl_dst_param_t dst_param;
1181 glsl_dst_param_t dst_param2;
1182 WINED3DFORMAT fmt;
1183 WINED3DFORMAT conversion_group;
1184 IWineD3DBaseTextureImpl *texture;
1185 DWORD mask, mask_size;
1186 UINT i;
1187 BOOL recorded = FALSE;
1188 DWORD sampler_idx;
1189 DWORD hex_version = shader->baseShader.hex_version;
1191 switch(arg->opcode->opcode) {
1192 case WINED3DSIO_TEX:
1193 if (hex_version < WINED3DPS_VERSION(2,0)) {
1194 sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
1195 } else {
1196 sampler_idx = arg->src[1] & WINED3DSP_REGNUM_MASK;
1198 break;
1200 case WINED3DSIO_TEXLDL:
1201 FIXME("Add color fixup for vertex texture WINED3DSIO_TEXLDL\n");
1202 return;
1204 case WINED3DSIO_TEXDP3TEX:
1205 case WINED3DSIO_TEXM3x3TEX:
1206 case WINED3DSIO_TEXM3x3SPEC:
1207 case WINED3DSIO_TEXM3x3VSPEC:
1208 case WINED3DSIO_TEXBEM:
1209 case WINED3DSIO_TEXREG2AR:
1210 case WINED3DSIO_TEXREG2GB:
1211 case WINED3DSIO_TEXREG2RGB:
1212 sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
1213 break;
1215 default:
1216 /* Not a texture sampling instruction, nothing to do */
1217 return;
1220 texture = (IWineD3DBaseTextureImpl *) deviceImpl->stateBlock->textures[sampler_idx];
1221 if(texture) {
1222 fmt = texture->resource.format;
1223 conversion_group = texture->baseTexture.shader_conversion_group;
1224 } else {
1225 fmt = WINED3DFMT_UNKNOWN;
1226 conversion_group = WINED3DFMT_UNKNOWN;
1229 /* before doing anything, record the sampler with the format in the format conversion list,
1230 * but check if it's not there already
1232 for(i = 0; i < shader->baseShader.num_sampled_samplers; i++) {
1233 if(shader->baseShader.sampled_samplers[i] == sampler_idx) {
1234 recorded = TRUE;
1235 break;
1238 if(!recorded) {
1239 shader->baseShader.sampled_samplers[shader->baseShader.num_sampled_samplers] = sampler_idx;
1240 shader->baseShader.num_sampled_samplers++;
1241 shader->baseShader.sampled_format[sampler_idx] = conversion_group;
1244 switch(fmt) {
1245 case WINED3DFMT_V8U8:
1246 case WINED3DFMT_V16U16:
1247 if(GL_SUPPORT(NV_TEXTURE_SHADER) && fmt == WINED3DFMT_V8U8) {
1248 /* The 3rd channel returns 1.0 in d3d, but 0.0 in gl. Fix this while we're at it :-) */
1249 mask = shader_glsl_add_dst_param(arg, arg->dst, WINED3DSP_WRITEMASK_2, &dst_param);
1250 mask_size = shader_glsl_get_write_mask_size(mask);
1251 if(mask_size >= 3) {
1252 shader_addline(arg->buffer, "%s.%c = 1.0;\n", dst_param.reg_name, dst_param.mask_str[3]);
1254 } else {
1255 /* Correct the sign, but leave the blue as it is - it was loaded correctly already */
1256 mask = shader_glsl_add_dst_param(arg, arg->dst,
1257 WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1,
1258 &dst_param);
1259 mask_size = shader_glsl_get_write_mask_size(mask);
1260 if(mask_size >= 2) {
1261 shader_addline(arg->buffer, "%s.%c%c = %s.%c%c * 2.0 - 1.0;\n",
1262 dst_param.reg_name, dst_param.mask_str[1], dst_param.mask_str[2],
1263 dst_param.reg_name, dst_param.mask_str[1], dst_param.mask_str[2]);
1264 } else if(mask_size == 1) {
1265 shader_addline(arg->buffer, "%s.%c = %s.%c * 2.0 - 1.0;\n", dst_param.reg_name, dst_param.mask_str[1],
1266 dst_param.reg_name, dst_param.mask_str[1]);
1269 break;
1271 case WINED3DFMT_X8L8V8U8:
1272 if(!GL_SUPPORT(NV_TEXTURE_SHADER)) {
1273 /* Red and blue are the signed channels, fix them up; Blue(=L) is correct already,
1274 * and a(X) is always 1.0
1276 mask = shader_glsl_add_dst_param(arg, arg->dst, WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &dst_param);
1277 mask_size = shader_glsl_get_write_mask_size(mask);
1278 if(mask_size >= 2) {
1279 shader_addline(arg->buffer, "%s.%c%c = %s.%c%c * 2.0 - 1.0;\n",
1280 dst_param.reg_name, dst_param.mask_str[1], dst_param.mask_str[2],
1281 dst_param.reg_name, dst_param.mask_str[1], dst_param.mask_str[2]);
1282 } else if(mask_size == 1) {
1283 shader_addline(arg->buffer, "%s.%c = %s.%c * 2.0 - 1.0;\n",
1284 dst_param.reg_name, dst_param.mask_str[1],
1285 dst_param.reg_name, dst_param.mask_str[1]);
1288 break;
1290 case WINED3DFMT_L6V5U5:
1291 if(!GL_SUPPORT(NV_TEXTURE_SHADER)) {
1292 mask = shader_glsl_add_dst_param(arg, arg->dst, WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &dst_param);
1293 mask_size = shader_glsl_get_write_mask_size(mask);
1294 shader_glsl_add_dst_param(arg, arg->dst, WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_2, &dst_param2);
1295 if(mask_size >= 3) {
1296 /* Swap y and z (U and L), and do a sign conversion on x and the new y(V and U) */
1297 shader_addline(arg->buffer, "tmp0.g = %s.%c;\n",
1298 dst_param.reg_name, dst_param.mask_str[2]);
1299 shader_addline(arg->buffer, "%s.%c%c = %s.%c%c * 2.0 - 1.0;\n",
1300 dst_param.reg_name, dst_param.mask_str[2], dst_param.mask_str[1],
1301 dst_param2.reg_name, dst_param.mask_str[1], dst_param.mask_str[3]);
1302 shader_addline(arg->buffer, "%s.%c = tmp0.g;\n", dst_param.reg_name,
1303 dst_param.mask_str[3]);
1304 } else if(mask_size == 2) {
1305 /* This is bad: We have VL, but we need VU */
1306 FIXME("2 components sampled from a converted L6V5U5 texture\n");
1307 } else {
1308 shader_addline(arg->buffer, "%s.%c = %s.%c * 2.0 - 1.0;\n",
1309 dst_param.reg_name, dst_param.mask_str[1],
1310 dst_param2.reg_name, dst_param.mask_str[1]);
1313 break;
1315 case WINED3DFMT_Q8W8V8U8:
1316 if(!GL_SUPPORT(NV_TEXTURE_SHADER)) {
1317 /* Correct the sign in all channels. The writemask just applies as-is, no
1318 * need for checking the mask size
1320 shader_glsl_add_dst_param(arg, arg->dst,
1321 WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 |
1322 WINED3DSP_WRITEMASK_2 | WINED3DSP_WRITEMASK_3,
1323 &dst_param);
1324 shader_addline(arg->buffer, "%s%s = %s%s * 2.0 - 1.0;\n", dst_param.reg_name, dst_param.mask_str,
1325 dst_param.reg_name, dst_param.mask_str);
1327 break;
1329 case WINED3DFMT_ATI2N:
1330 /* GL_ATI_texture_compression_3dc returns the two channels as luminance-alpha,
1331 * which means the first one is replicated across .rgb, and the 2nd one is in
1332 * .a. We need the 2nd in .g
1334 * GL_EXT_texture_compression_rgtc returns the values in .rg, however, they
1335 * are swapped compared to d3d. So swap red and green.
1337 mask = shader_glsl_add_dst_param(arg, arg->dst, WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &dst_param);
1338 mask_size = shader_glsl_get_write_mask_size(mask);
1339 if(GL_SUPPORT(EXT_TEXTURE_COMPRESSION_RGTC)) {
1340 if(mask_size >= 2) {
1341 shader_addline(arg->buffer, "%s.%c%c = %s.%c%c;\n",
1342 dst_param.reg_name, dst_param.mask_str[1],
1343 dst_param.mask_str[2],
1344 dst_param.reg_name, dst_param.mask_str[2],
1345 dst_param.mask_str[1]);
1346 } else {
1347 FIXME("%u components sampled from a converted ATI2N texture\n", mask_size);
1349 } else {
1350 if(mask_size == 4) {
1351 /* Swap y and z (U and L), and do a sign conversion on x and the new y(V and U) */
1352 shader_addline(arg->buffer, "%s.%c = %s.%c;\n",
1353 dst_param.reg_name, dst_param.mask_str[2],
1354 dst_param.reg_name, dst_param.mask_str[4]);
1355 } else if(mask_size == 1) {
1356 /* Nothing to do */
1357 } else {
1358 FIXME("%u components sampled from a converted ATI2N texture\n", mask_size);
1359 /* This is bad: We have .r[gb], but we need .ra */
1362 break;
1364 /* stupid compiler */
1365 default:
1366 break;
1370 /*****************************************************************************
1372 * Begin processing individual instruction opcodes
1374 ****************************************************************************/
1376 /* Generate GLSL arithmetic functions (dst = src1 + src2) */
1377 static void shader_glsl_arith(const SHADER_OPCODE_ARG *arg)
1379 CONST SHADER_OPCODE* curOpcode = arg->opcode;
1380 SHADER_BUFFER* buffer = arg->buffer;
1381 glsl_src_param_t src0_param;
1382 glsl_src_param_t src1_param;
1383 DWORD write_mask;
1384 char op;
1386 /* Determine the GLSL operator to use based on the opcode */
1387 switch (curOpcode->opcode) {
1388 case WINED3DSIO_MUL: op = '*'; break;
1389 case WINED3DSIO_ADD: op = '+'; break;
1390 case WINED3DSIO_SUB: op = '-'; break;
1391 default:
1392 op = ' ';
1393 FIXME("Opcode %s not yet handled in GLSL\n", curOpcode->name);
1394 break;
1397 write_mask = shader_glsl_append_dst(buffer, arg);
1398 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src0_param);
1399 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1400 shader_addline(buffer, "%s %c %s);\n", src0_param.param_str, op, src1_param.param_str);
1403 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
1404 static void shader_glsl_mov(const SHADER_OPCODE_ARG *arg)
1406 IWineD3DBaseShaderImpl* shader = (IWineD3DBaseShaderImpl*) arg->shader;
1407 SHADER_BUFFER* buffer = arg->buffer;
1408 glsl_src_param_t src0_param;
1409 DWORD write_mask;
1411 write_mask = shader_glsl_append_dst(buffer, arg);
1412 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src0_param);
1414 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
1415 * shader versions WINED3DSIO_MOVA is used for this. */
1416 if ((WINED3DSHADER_VERSION_MAJOR(shader->baseShader.hex_version) == 1 &&
1417 !shader_is_pshader_version(shader->baseShader.hex_version) &&
1418 shader_get_regtype(arg->dst) == WINED3DSPR_ADDR)) {
1419 /* This is a simple floor() */
1420 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
1421 if (mask_size > 1) {
1422 shader_addline(buffer, "ivec%d(floor(%s)));\n", mask_size, src0_param.param_str);
1423 } else {
1424 shader_addline(buffer, "int(floor(%s)));\n", src0_param.param_str);
1426 } else if(arg->opcode->opcode == WINED3DSIO_MOVA) {
1427 /* We need to *round* to the nearest int here. */
1428 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
1429 if (mask_size > 1) {
1430 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);
1431 } else {
1432 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n", src0_param.param_str, src0_param.param_str);
1434 } else {
1435 shader_addline(buffer, "%s);\n", src0_param.param_str);
1439 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
1440 static void shader_glsl_dot(const SHADER_OPCODE_ARG *arg)
1442 CONST SHADER_OPCODE* curOpcode = arg->opcode;
1443 SHADER_BUFFER* buffer = arg->buffer;
1444 glsl_src_param_t src0_param;
1445 glsl_src_param_t src1_param;
1446 DWORD dst_write_mask, src_write_mask;
1447 unsigned int dst_size = 0;
1449 dst_write_mask = shader_glsl_append_dst(buffer, arg);
1450 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
1452 /* dp3 works on vec3, dp4 on vec4 */
1453 if (curOpcode->opcode == WINED3DSIO_DP4) {
1454 src_write_mask = WINED3DSP_WRITEMASK_ALL;
1455 } else {
1456 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1459 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_write_mask, &src0_param);
1460 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], src_write_mask, &src1_param);
1462 if (dst_size > 1) {
1463 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
1464 } else {
1465 shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
1469 /* Note that this instruction has some restrictions. The destination write mask
1470 * can't contain the w component, and the source swizzles have to be .xyzw */
1471 static void shader_glsl_cross(const SHADER_OPCODE_ARG *arg)
1473 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1474 glsl_src_param_t src0_param;
1475 glsl_src_param_t src1_param;
1476 char dst_mask[6];
1478 shader_glsl_get_write_mask(arg->dst, dst_mask);
1479 shader_glsl_append_dst(arg->buffer, arg);
1480 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
1481 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], src_mask, &src1_param);
1482 shader_addline(arg->buffer, "cross(%s, %s)%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
1485 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
1486 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
1487 * GLSL uses the value as-is. */
1488 static void shader_glsl_pow(const SHADER_OPCODE_ARG *arg)
1490 SHADER_BUFFER *buffer = arg->buffer;
1491 glsl_src_param_t src0_param;
1492 glsl_src_param_t src1_param;
1493 DWORD dst_write_mask;
1494 unsigned int dst_size;
1496 dst_write_mask = shader_glsl_append_dst(buffer, arg);
1497 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
1499 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1500 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0, &src1_param);
1502 if (dst_size > 1) {
1503 shader_addline(buffer, "vec%d(pow(abs(%s), %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
1504 } else {
1505 shader_addline(buffer, "pow(abs(%s), %s));\n", src0_param.param_str, src1_param.param_str);
1509 /* Process the WINED3DSIO_LOG instruction in GLSL (dst = log2(|src0|))
1510 * Src0 is a scalar. Note that D3D uses the absolute of src0, while
1511 * GLSL uses the value as-is. */
1512 static void shader_glsl_log(const SHADER_OPCODE_ARG *arg)
1514 SHADER_BUFFER *buffer = arg->buffer;
1515 glsl_src_param_t src0_param;
1516 DWORD dst_write_mask;
1517 unsigned int dst_size;
1519 dst_write_mask = shader_glsl_append_dst(buffer, arg);
1520 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
1522 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1524 if (dst_size > 1) {
1525 shader_addline(buffer, "vec%d(log2(abs(%s))));\n", dst_size, src0_param.param_str);
1526 } else {
1527 shader_addline(buffer, "log2(abs(%s)));\n", src0_param.param_str);
1531 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
1532 static void shader_glsl_map2gl(const SHADER_OPCODE_ARG *arg)
1534 CONST SHADER_OPCODE* curOpcode = arg->opcode;
1535 SHADER_BUFFER* buffer = arg->buffer;
1536 glsl_src_param_t src_param;
1537 const char *instruction;
1538 char arguments[256];
1539 DWORD write_mask;
1540 unsigned i;
1542 /* Determine the GLSL function to use based on the opcode */
1543 /* TODO: Possibly make this a table for faster lookups */
1544 switch (curOpcode->opcode) {
1545 case WINED3DSIO_MIN: instruction = "min"; break;
1546 case WINED3DSIO_MAX: instruction = "max"; break;
1547 case WINED3DSIO_ABS: instruction = "abs"; break;
1548 case WINED3DSIO_FRC: instruction = "fract"; break;
1549 case WINED3DSIO_NRM: instruction = "normalize"; break;
1550 case WINED3DSIO_EXP: instruction = "exp2"; break;
1551 case WINED3DSIO_SGN: instruction = "sign"; break;
1552 case WINED3DSIO_DSX: instruction = "dFdx"; break;
1553 case WINED3DSIO_DSY: instruction = "ycorrection.y * dFdy"; break;
1554 default: instruction = "";
1555 FIXME("Opcode %s not yet handled in GLSL\n", curOpcode->name);
1556 break;
1559 write_mask = shader_glsl_append_dst(buffer, arg);
1561 arguments[0] = '\0';
1562 if (curOpcode->num_params > 0) {
1563 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src_param);
1564 strcat(arguments, src_param.param_str);
1565 for (i = 2; i < curOpcode->num_params; ++i) {
1566 strcat(arguments, ", ");
1567 shader_glsl_add_src_param(arg, arg->src[i-1], arg->src_addr[i-1], write_mask, &src_param);
1568 strcat(arguments, src_param.param_str);
1572 shader_addline(buffer, "%s(%s));\n", instruction, arguments);
1575 /** Process the WINED3DSIO_EXPP instruction in GLSL:
1576 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
1577 * dst.x = 2^(floor(src))
1578 * dst.y = src - floor(src)
1579 * dst.z = 2^src (partial precision is allowed, but optional)
1580 * dst.w = 1.0;
1581 * For 2.0 shaders, just do this (honoring writemask and swizzle):
1582 * dst = 2^src; (partial precision is allowed, but optional)
1584 static void shader_glsl_expp(const SHADER_OPCODE_ARG *arg)
1586 IWineD3DBaseShaderImpl *shader = (IWineD3DBaseShaderImpl *)arg->shader;
1587 glsl_src_param_t src_param;
1589 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src_param);
1591 if (shader->baseShader.hex_version < WINED3DPS_VERSION(2,0)) {
1592 char dst_mask[6];
1594 shader_addline(arg->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
1595 shader_addline(arg->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
1596 shader_addline(arg->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
1597 shader_addline(arg->buffer, "tmp0.w = 1.0;\n");
1599 shader_glsl_append_dst(arg->buffer, arg);
1600 shader_glsl_get_write_mask(arg->dst, dst_mask);
1601 shader_addline(arg->buffer, "tmp0%s);\n", dst_mask);
1602 } else {
1603 DWORD write_mask;
1604 unsigned int mask_size;
1606 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1607 mask_size = shader_glsl_get_write_mask_size(write_mask);
1609 if (mask_size > 1) {
1610 shader_addline(arg->buffer, "vec%d(exp2(%s)));\n", mask_size, src_param.param_str);
1611 } else {
1612 shader_addline(arg->buffer, "exp2(%s));\n", src_param.param_str);
1617 /** Process the RCP (reciprocal or inverse) opcode in GLSL (dst = 1 / src) */
1618 static void shader_glsl_rcp(const SHADER_OPCODE_ARG *arg)
1620 glsl_src_param_t src_param;
1621 DWORD write_mask;
1622 unsigned int mask_size;
1624 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1625 mask_size = shader_glsl_get_write_mask_size(write_mask);
1626 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_3, &src_param);
1628 if (mask_size > 1) {
1629 shader_addline(arg->buffer, "vec%d(1.0 / %s));\n", mask_size, src_param.param_str);
1630 } else {
1631 shader_addline(arg->buffer, "1.0 / %s);\n", src_param.param_str);
1635 static void shader_glsl_rsq(const SHADER_OPCODE_ARG *arg)
1637 SHADER_BUFFER* buffer = arg->buffer;
1638 glsl_src_param_t src_param;
1639 DWORD write_mask;
1640 unsigned int mask_size;
1642 write_mask = shader_glsl_append_dst(buffer, arg);
1643 mask_size = shader_glsl_get_write_mask_size(write_mask);
1645 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_3, &src_param);
1647 if (mask_size > 1) {
1648 shader_addline(buffer, "vec%d(inversesqrt(%s)));\n", mask_size, src_param.param_str);
1649 } else {
1650 shader_addline(buffer, "inversesqrt(%s));\n", src_param.param_str);
1654 /** Process signed comparison opcodes in GLSL. */
1655 static void shader_glsl_compare(const SHADER_OPCODE_ARG *arg)
1657 glsl_src_param_t src0_param;
1658 glsl_src_param_t src1_param;
1659 DWORD write_mask;
1660 unsigned int mask_size;
1662 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1663 mask_size = shader_glsl_get_write_mask_size(write_mask);
1664 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src0_param);
1665 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1667 if (mask_size > 1) {
1668 const char *compare;
1670 switch(arg->opcode->opcode) {
1671 case WINED3DSIO_SLT: compare = "lessThan"; break;
1672 case WINED3DSIO_SGE: compare = "greaterThanEqual"; break;
1673 default: compare = "";
1674 FIXME("Can't handle opcode %s\n", arg->opcode->name);
1677 shader_addline(arg->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
1678 src0_param.param_str, src1_param.param_str);
1679 } else {
1680 switch(arg->opcode->opcode) {
1681 case WINED3DSIO_SLT:
1682 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
1683 * to return 0.0 but step returns 1.0 because step is not < x
1684 * An alternative is a bvec compare padded with an unused second component.
1685 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
1686 * issue. Playing with not() is not possible either because not() does not accept
1687 * a scalar.
1689 shader_addline(arg->buffer, "(%s < %s) ? 1.0 : 0.0);\n", src0_param.param_str, src1_param.param_str);
1690 break;
1691 case WINED3DSIO_SGE:
1692 /* Here we can use the step() function and safe a conditional */
1693 shader_addline(arg->buffer, "step(%s, %s));\n", src1_param.param_str, src0_param.param_str);
1694 break;
1695 default:
1696 FIXME("Can't handle opcode %s\n", arg->opcode->name);
1702 /** Process CMP instruction in GLSL (dst = src0 >= 0.0 ? src1 : src2), per channel */
1703 static void shader_glsl_cmp(const SHADER_OPCODE_ARG *arg)
1705 glsl_src_param_t src0_param;
1706 glsl_src_param_t src1_param;
1707 glsl_src_param_t src2_param;
1708 DWORD write_mask, cmp_channel = 0;
1709 unsigned int i, j;
1710 char mask_char[6];
1711 BOOL temp_destination = FALSE;
1713 if(shader_is_scalar(arg->src[0])) {
1714 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1716 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
1717 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1718 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, &src2_param);
1720 shader_addline(arg->buffer, "%s >= 0.0 ? %s : %s);\n",
1721 src0_param.param_str, src1_param.param_str, src2_param.param_str);
1722 } else {
1723 DWORD src0reg = arg->src[0] & WINED3DSP_REGNUM_MASK;
1724 DWORD src1reg = arg->src[1] & WINED3DSP_REGNUM_MASK;
1725 DWORD src2reg = arg->src[2] & WINED3DSP_REGNUM_MASK;
1726 DWORD src0regtype = shader_get_regtype(arg->src[0]);
1727 DWORD src1regtype = shader_get_regtype(arg->src[1]);
1728 DWORD src2regtype = shader_get_regtype(arg->src[2]);
1729 DWORD dstreg = arg->dst & WINED3DSP_REGNUM_MASK;
1730 DWORD dstregtype = shader_get_regtype(arg->dst);
1732 /* Cycle through all source0 channels */
1733 for (i=0; i<4; i++) {
1734 write_mask = 0;
1735 /* Find the destination channels which use the current source0 channel */
1736 for (j=0; j<4; j++) {
1737 if ( ((arg->src[0] >> (WINED3DSP_SWIZZLE_SHIFT + 2*j)) & 0x3) == i ) {
1738 write_mask |= WINED3DSP_WRITEMASK_0 << j;
1739 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
1743 /* Splitting the cmp instruction up in multiple lines imposes a problem:
1744 * The first lines may overwrite source parameters of the following lines.
1745 * Deal with that by using a temporary destination register if needed
1747 if((src0reg == dstreg && src0regtype == dstregtype) ||
1748 (src1reg == dstreg && src1regtype == dstregtype) ||
1749 (src2reg == dstreg && src2regtype == dstregtype)) {
1751 write_mask = shader_glsl_get_write_mask(arg->dst & (~WINED3DSP_SWIZZLE_MASK | write_mask), mask_char);
1752 if (!write_mask) continue;
1753 shader_addline(arg->buffer, "tmp0%s = (", mask_char);
1754 temp_destination = TRUE;
1755 } else {
1756 write_mask = shader_glsl_append_dst_ext(arg->buffer, arg, arg->dst & (~WINED3DSP_SWIZZLE_MASK | write_mask));
1757 if (!write_mask) continue;
1760 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], cmp_channel, &src0_param);
1761 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1762 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, &src2_param);
1764 shader_addline(arg->buffer, "%s >= 0.0 ? %s : %s);\n",
1765 src0_param.param_str, src1_param.param_str, src2_param.param_str);
1768 if(temp_destination) {
1769 shader_glsl_get_write_mask(arg->dst, mask_char);
1770 shader_glsl_append_dst_ext(arg->buffer, arg, arg->dst);
1771 shader_addline(arg->buffer, "tmp0%s);\n", mask_char);
1777 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
1778 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
1779 * the compare is done per component of src0. */
1780 static void shader_glsl_cnd(const SHADER_OPCODE_ARG *arg)
1782 IWineD3DBaseShaderImpl* shader = (IWineD3DBaseShaderImpl*) arg->shader;
1783 glsl_src_param_t src0_param;
1784 glsl_src_param_t src1_param;
1785 glsl_src_param_t src2_param;
1786 DWORD write_mask, cmp_channel = 0;
1787 unsigned int i, j;
1789 if (shader->baseShader.hex_version < WINED3DPS_VERSION(1, 4)) {
1790 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1791 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1792 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1793 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, &src2_param);
1795 /* Fun: The D3DSI_COISSUE flag changes the semantic of the cnd instruction for < 1.4 shaders */
1796 if(arg->opcode_token & WINED3DSI_COISSUE) {
1797 shader_addline(arg->buffer, "%s /* COISSUE! */);\n", src1_param.param_str);
1798 } else {
1799 shader_addline(arg->buffer, "%s > 0.5 ? %s : %s);\n",
1800 src0_param.param_str, src1_param.param_str, src2_param.param_str);
1802 return;
1804 /* Cycle through all source0 channels */
1805 for (i=0; i<4; i++) {
1806 write_mask = 0;
1807 /* Find the destination channels which use the current source0 channel */
1808 for (j=0; j<4; j++) {
1809 if ( ((arg->src[0] >> (WINED3DSP_SWIZZLE_SHIFT + 2*j)) & 0x3) == i ) {
1810 write_mask |= WINED3DSP_WRITEMASK_0 << j;
1811 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
1814 write_mask = shader_glsl_append_dst_ext(arg->buffer, arg, arg->dst & (~WINED3DSP_SWIZZLE_MASK | write_mask));
1815 if (!write_mask) continue;
1817 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], cmp_channel, &src0_param);
1818 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1819 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, &src2_param);
1821 shader_addline(arg->buffer, "%s > 0.5 ? %s : %s);\n",
1822 src0_param.param_str, src1_param.param_str, src2_param.param_str);
1826 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
1827 static void shader_glsl_mad(const SHADER_OPCODE_ARG *arg)
1829 glsl_src_param_t src0_param;
1830 glsl_src_param_t src1_param;
1831 glsl_src_param_t src2_param;
1832 DWORD write_mask;
1834 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1835 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src0_param);
1836 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1837 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, &src2_param);
1838 shader_addline(arg->buffer, "(%s * %s) + %s);\n",
1839 src0_param.param_str, src1_param.param_str, src2_param.param_str);
1842 /** Handles transforming all WINED3DSIO_M?x? opcodes for
1843 Vertex shaders to GLSL codes */
1844 static void shader_glsl_mnxn(const SHADER_OPCODE_ARG *arg)
1846 int i;
1847 int nComponents = 0;
1848 SHADER_OPCODE_ARG tmpArg;
1850 memset(&tmpArg, 0, sizeof(SHADER_OPCODE_ARG));
1852 /* Set constants for the temporary argument */
1853 tmpArg.shader = arg->shader;
1854 tmpArg.buffer = arg->buffer;
1855 tmpArg.src[0] = arg->src[0];
1856 tmpArg.src_addr[0] = arg->src_addr[0];
1857 tmpArg.src_addr[1] = arg->src_addr[1];
1858 tmpArg.reg_maps = arg->reg_maps;
1860 switch(arg->opcode->opcode) {
1861 case WINED3DSIO_M4x4:
1862 nComponents = 4;
1863 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP4);
1864 break;
1865 case WINED3DSIO_M4x3:
1866 nComponents = 3;
1867 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP4);
1868 break;
1869 case WINED3DSIO_M3x4:
1870 nComponents = 4;
1871 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP3);
1872 break;
1873 case WINED3DSIO_M3x3:
1874 nComponents = 3;
1875 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP3);
1876 break;
1877 case WINED3DSIO_M3x2:
1878 nComponents = 2;
1879 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP3);
1880 break;
1881 default:
1882 break;
1885 for (i = 0; i < nComponents; i++) {
1886 tmpArg.dst = ((arg->dst) & ~WINED3DSP_WRITEMASK_ALL)|(WINED3DSP_WRITEMASK_0<<i);
1887 tmpArg.src[1] = arg->src[1]+i;
1888 shader_glsl_dot(&tmpArg);
1893 The LRP instruction performs a component-wise linear interpolation
1894 between the second and third operands using the first operand as the
1895 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
1896 This is equivalent to mix(src2, src1, src0);
1898 static void shader_glsl_lrp(const SHADER_OPCODE_ARG *arg)
1900 glsl_src_param_t src0_param;
1901 glsl_src_param_t src1_param;
1902 glsl_src_param_t src2_param;
1903 DWORD write_mask;
1905 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1907 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src0_param);
1908 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1909 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, &src2_param);
1911 shader_addline(arg->buffer, "mix(%s, %s, %s));\n",
1912 src2_param.param_str, src1_param.param_str, src0_param.param_str);
1915 /** Process the WINED3DSIO_LIT instruction in GLSL:
1916 * dst.x = dst.w = 1.0
1917 * dst.y = (src0.x > 0) ? src0.x
1918 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
1919 * where src.w is clamped at +- 128
1921 static void shader_glsl_lit(const SHADER_OPCODE_ARG *arg)
1923 glsl_src_param_t src0_param;
1924 glsl_src_param_t src1_param;
1925 glsl_src_param_t src3_param;
1926 char dst_mask[6];
1928 shader_glsl_append_dst(arg->buffer, arg);
1929 shader_glsl_get_write_mask(arg->dst, dst_mask);
1931 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1932 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_1, &src1_param);
1933 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_3, &src3_param);
1935 /* The sdk specifies the instruction like this
1936 * dst.x = 1.0;
1937 * if(src.x > 0.0) dst.y = src.x
1938 * else dst.y = 0.0.
1939 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
1940 * else dst.z = 0.0;
1941 * dst.w = 1.0;
1943 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
1944 * dst.x = 1.0 ... No further explanation needed
1945 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
1946 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
1947 * dst.w = 1.0. ... Nothing fancy.
1949 * So we still have one conditional in there. So do this:
1950 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
1952 * 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),
1953 * 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.
1954 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to
1956 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",
1957 src0_param.param_str, src1_param.param_str, src0_param.param_str, src3_param.param_str, dst_mask);
1960 /** Process the WINED3DSIO_DST instruction in GLSL:
1961 * dst.x = 1.0
1962 * dst.y = src0.x * src0.y
1963 * dst.z = src0.z
1964 * dst.w = src1.w
1966 static void shader_glsl_dst(const SHADER_OPCODE_ARG *arg)
1968 glsl_src_param_t src0y_param;
1969 glsl_src_param_t src0z_param;
1970 glsl_src_param_t src1y_param;
1971 glsl_src_param_t src1w_param;
1972 char dst_mask[6];
1974 shader_glsl_append_dst(arg->buffer, arg);
1975 shader_glsl_get_write_mask(arg->dst, dst_mask);
1977 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_1, &src0y_param);
1978 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_2, &src0z_param);
1979 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_1, &src1y_param);
1980 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_3, &src1w_param);
1982 shader_addline(arg->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
1983 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
1986 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
1987 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
1988 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
1990 * dst.x = cos(src0.?)
1991 * dst.y = sin(src0.?)
1992 * dst.z = dst.z
1993 * dst.w = dst.w
1995 static void shader_glsl_sincos(const SHADER_OPCODE_ARG *arg)
1997 glsl_src_param_t src0_param;
1998 DWORD write_mask;
2000 write_mask = shader_glsl_append_dst(arg->buffer, arg);
2001 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
2003 switch (write_mask) {
2004 case WINED3DSP_WRITEMASK_0:
2005 shader_addline(arg->buffer, "cos(%s));\n", src0_param.param_str);
2006 break;
2008 case WINED3DSP_WRITEMASK_1:
2009 shader_addline(arg->buffer, "sin(%s));\n", src0_param.param_str);
2010 break;
2012 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
2013 shader_addline(arg->buffer, "vec2(cos(%s), sin(%s)));\n", src0_param.param_str, src0_param.param_str);
2014 break;
2016 default:
2017 ERR("Write mask should be .x, .y or .xy\n");
2018 break;
2022 /** Process the WINED3DSIO_LOOP instruction in GLSL:
2023 * Start a for() loop where src1.y is the initial value of aL,
2024 * increment aL by src1.z for a total of src1.x iterations.
2025 * Need to use a temporary variable for this operation.
2027 /* FIXME: I don't think nested loops will work correctly this way. */
2028 static void shader_glsl_loop(const SHADER_OPCODE_ARG *arg)
2030 glsl_src_param_t src1_param;
2031 IWineD3DBaseShaderImpl* shader = (IWineD3DBaseShaderImpl*) arg->shader;
2032 DWORD regtype = shader_get_regtype(arg->src[1]);
2033 DWORD reg = arg->src[1] & WINED3DSP_REGNUM_MASK;
2034 const DWORD *control_values = NULL;
2035 const local_constant *constant;
2037 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_ALL, &src1_param);
2039 /* Try to hardcode the loop control parameters if possible. Direct3D 9 class hardware doesn't support real
2040 * varying indexing, but Microsoft designed this feature for Shader model 2.x+. If the loop control is
2041 * known at compile time, the GLSL compiler can unroll the loop, and replace indirect addressing with direct
2042 * addressing.
2044 if(regtype == WINED3DSPR_CONSTINT) {
2045 LIST_FOR_EACH_ENTRY(constant, &shader->baseShader.constantsI, local_constant, entry) {
2046 if(constant->idx == reg) {
2047 control_values = constant->value;
2048 break;
2053 if(control_values) {
2054 if(control_values[2] > 0) {
2055 shader_addline(arg->buffer, "for (aL%u = %d; aL%u < (%d * %d + %d); aL%u += %d) {\n",
2056 shader->baseShader.cur_loop_depth, control_values[1],
2057 shader->baseShader.cur_loop_depth, control_values[0], control_values[2], control_values[1],
2058 shader->baseShader.cur_loop_depth, control_values[2]);
2059 } else if(control_values[2] == 0) {
2060 shader_addline(arg->buffer, "for (aL%u = %d, tmpInt%u = 0; tmpInt%u < %d; tmpInt%u++) {\n",
2061 shader->baseShader.cur_loop_depth, control_values[1], shader->baseShader.cur_loop_depth,
2062 shader->baseShader.cur_loop_depth, control_values[0],
2063 shader->baseShader.cur_loop_depth);
2064 } else {
2065 shader_addline(arg->buffer, "for (aL%u = %d; aL%u > (%d * %d + %d); aL%u += %d) {\n",
2066 shader->baseShader.cur_loop_depth, control_values[1],
2067 shader->baseShader.cur_loop_depth, control_values[0], control_values[2], control_values[1],
2068 shader->baseShader.cur_loop_depth, control_values[2]);
2070 } else {
2071 shader_addline(arg->buffer, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z) {\n",
2072 shader->baseShader.cur_loop_depth, shader->baseShader.cur_loop_regno,
2073 src1_param.reg_name, shader->baseShader.cur_loop_depth, src1_param.reg_name,
2074 shader->baseShader.cur_loop_depth, shader->baseShader.cur_loop_regno, src1_param.reg_name);
2077 shader->baseShader.cur_loop_depth++;
2078 shader->baseShader.cur_loop_regno++;
2081 static void shader_glsl_end(const SHADER_OPCODE_ARG *arg)
2083 IWineD3DBaseShaderImpl* shader = (IWineD3DBaseShaderImpl*) arg->shader;
2085 shader_addline(arg->buffer, "}\n");
2087 if(arg->opcode->opcode == WINED3DSIO_ENDLOOP) {
2088 shader->baseShader.cur_loop_depth--;
2089 shader->baseShader.cur_loop_regno--;
2091 if(arg->opcode->opcode == WINED3DSIO_ENDREP) {
2092 shader->baseShader.cur_loop_depth--;
2096 static void shader_glsl_rep(const SHADER_OPCODE_ARG *arg)
2098 IWineD3DBaseShaderImpl* shader = (IWineD3DBaseShaderImpl*) arg->shader;
2099 glsl_src_param_t src0_param;
2101 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
2102 shader_addline(arg->buffer, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
2103 shader->baseShader.cur_loop_depth, shader->baseShader.cur_loop_depth,
2104 src0_param.param_str, shader->baseShader.cur_loop_depth);
2105 shader->baseShader.cur_loop_depth++;
2108 static void shader_glsl_if(const SHADER_OPCODE_ARG *arg)
2110 glsl_src_param_t src0_param;
2112 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
2113 shader_addline(arg->buffer, "if (%s) {\n", src0_param.param_str);
2116 static void shader_glsl_ifc(const SHADER_OPCODE_ARG *arg)
2118 glsl_src_param_t src0_param;
2119 glsl_src_param_t src1_param;
2121 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
2122 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0, &src1_param);
2124 shader_addline(arg->buffer, "if (%s %s %s) {\n",
2125 src0_param.param_str, shader_get_comp_op(arg->opcode_token), src1_param.param_str);
2128 static void shader_glsl_else(const SHADER_OPCODE_ARG *arg)
2130 shader_addline(arg->buffer, "} else {\n");
2133 static void shader_glsl_break(const SHADER_OPCODE_ARG *arg)
2135 shader_addline(arg->buffer, "break;\n");
2138 /* FIXME: According to MSDN the compare is done per component. */
2139 static void shader_glsl_breakc(const SHADER_OPCODE_ARG *arg)
2141 glsl_src_param_t src0_param;
2142 glsl_src_param_t src1_param;
2144 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
2145 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0, &src1_param);
2147 shader_addline(arg->buffer, "if (%s %s %s) break;\n",
2148 src0_param.param_str, shader_get_comp_op(arg->opcode_token), src1_param.param_str);
2151 static void shader_glsl_label(const SHADER_OPCODE_ARG *arg)
2154 DWORD snum = (arg->src[0]) & WINED3DSP_REGNUM_MASK;
2155 shader_addline(arg->buffer, "}\n");
2156 shader_addline(arg->buffer, "void subroutine%u () {\n", snum);
2159 static void shader_glsl_call(const SHADER_OPCODE_ARG *arg)
2161 DWORD snum = (arg->src[0]) & WINED3DSP_REGNUM_MASK;
2162 shader_addline(arg->buffer, "subroutine%u();\n", snum);
2165 static void shader_glsl_callnz(const SHADER_OPCODE_ARG *arg)
2167 glsl_src_param_t src1_param;
2169 DWORD snum = (arg->src[0]) & WINED3DSP_REGNUM_MASK;
2170 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0, &src1_param);
2171 shader_addline(arg->buffer, "if (%s) subroutine%u();\n", src1_param.param_str, snum);
2174 /*********************************************
2175 * Pixel Shader Specific Code begins here
2176 ********************************************/
2177 static void pshader_glsl_tex(const SHADER_OPCODE_ARG *arg)
2179 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
2180 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
2181 DWORD hex_version = This->baseShader.hex_version;
2182 char dst_swizzle[6];
2183 glsl_sample_function_t sample_function;
2184 DWORD sampler_type;
2185 DWORD sampler_idx;
2186 BOOL projected, texrect = FALSE;
2187 DWORD mask = 0;
2189 /* All versions have a destination register */
2190 shader_glsl_append_dst(arg->buffer, arg);
2192 /* 1.0-1.4: Use destination register as sampler source.
2193 * 2.0+: Use provided sampler source. */
2194 if (hex_version < WINED3DPS_VERSION(2,0)) {
2195 sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2196 } else {
2197 sampler_idx = arg->src[1] & WINED3DSP_REGNUM_MASK;
2199 sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
2201 if (hex_version < WINED3DPS_VERSION(1,4)) {
2202 DWORD flags = deviceImpl->stateBlock->textureState[sampler_idx][WINED3DTSS_TEXTURETRANSFORMFLAGS];
2204 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
2205 if (flags & WINED3DTTFF_PROJECTED && sampler_type != WINED3DSTT_CUBE) {
2206 projected = TRUE;
2207 switch (flags & ~WINED3DTTFF_PROJECTED) {
2208 case WINED3DTTFF_COUNT1: FIXME("WINED3DTTFF_PROJECTED with WINED3DTTFF_COUNT1?\n"); break;
2209 case WINED3DTTFF_COUNT2: mask = WINED3DSP_WRITEMASK_1; break;
2210 case WINED3DTTFF_COUNT3: mask = WINED3DSP_WRITEMASK_2; break;
2211 case WINED3DTTFF_COUNT4:
2212 case WINED3DTTFF_DISABLE: mask = WINED3DSP_WRITEMASK_3; break;
2214 } else {
2215 projected = FALSE;
2217 } else if (hex_version < WINED3DPS_VERSION(2,0)) {
2218 DWORD src_mod = arg->src[0] & WINED3DSP_SRCMOD_MASK;
2220 if (src_mod == WINED3DSPSM_DZ) {
2221 projected = TRUE;
2222 mask = WINED3DSP_WRITEMASK_2;
2223 } else if (src_mod == WINED3DSPSM_DW) {
2224 projected = TRUE;
2225 mask = WINED3DSP_WRITEMASK_3;
2226 } else {
2227 projected = FALSE;
2229 } else {
2230 if(arg->opcode_token & WINED3DSI_TEXLD_PROJECT) {
2231 /* ps 2.0 texldp instruction always divides by the fourth component. */
2232 projected = TRUE;
2233 mask = WINED3DSP_WRITEMASK_3;
2234 } else {
2235 projected = FALSE;
2239 if(deviceImpl->stateBlock->textures[sampler_idx] &&
2240 IWineD3DBaseTexture_GetTextureDimensions(deviceImpl->stateBlock->textures[sampler_idx]) == GL_TEXTURE_RECTANGLE_ARB) {
2241 texrect = TRUE;
2244 shader_glsl_get_sample_function(sampler_type, projected, texrect, &sample_function);
2245 mask |= sample_function.coord_mask;
2247 if (hex_version < WINED3DPS_VERSION(2,0)) {
2248 shader_glsl_get_write_mask(arg->dst, dst_swizzle);
2249 } else {
2250 shader_glsl_get_swizzle(arg->src[1], FALSE, arg->dst, dst_swizzle);
2253 /* 1.0-1.3: Use destination register as coordinate source.
2254 1.4+: Use provided coordinate source register. */
2255 if (hex_version < WINED3DPS_VERSION(1,4)) {
2256 char coord_mask[6];
2257 shader_glsl_get_write_mask(mask, coord_mask);
2258 shader_addline(arg->buffer, "%s(Psampler%u, T%u%s)%s);\n",
2259 sample_function.name, sampler_idx, sampler_idx, coord_mask, dst_swizzle);
2260 } else {
2261 glsl_src_param_t coord_param;
2262 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], mask, &coord_param);
2263 if(arg->opcode_token & WINED3DSI_TEXLD_BIAS) {
2264 glsl_src_param_t bias;
2265 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_3, &bias);
2267 shader_addline(arg->buffer, "%s(Psampler%u, %s, %s)%s);\n",
2268 sample_function.name, sampler_idx, coord_param.param_str,
2269 bias.param_str, dst_swizzle);
2270 } else {
2271 shader_addline(arg->buffer, "%s(Psampler%u, %s)%s);\n",
2272 sample_function.name, sampler_idx, coord_param.param_str, dst_swizzle);
2277 static void shader_glsl_texldl(const SHADER_OPCODE_ARG *arg)
2279 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*)arg->shader;
2280 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
2281 glsl_sample_function_t sample_function;
2282 glsl_src_param_t coord_param, lod_param;
2283 char dst_swizzle[6];
2284 DWORD sampler_type;
2285 DWORD sampler_idx;
2286 BOOL texrect = FALSE;
2288 shader_glsl_append_dst(arg->buffer, arg);
2289 shader_glsl_get_swizzle(arg->src[1], FALSE, arg->dst, dst_swizzle);
2291 sampler_idx = arg->src[1] & WINED3DSP_REGNUM_MASK;
2292 sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
2293 if(deviceImpl->stateBlock->textures[sampler_idx] &&
2294 IWineD3DBaseTexture_GetTextureDimensions(deviceImpl->stateBlock->textures[sampler_idx]) == GL_TEXTURE_RECTANGLE_ARB) {
2295 texrect = TRUE;
2297 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);
2299 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_3, &lod_param);
2301 if (shader_is_pshader_version(This->baseShader.hex_version)) {
2302 /* The GLSL spec claims the Lod sampling functions are only supported in vertex shaders.
2303 * However, they seem to work just fine in fragment shaders as well. */
2304 WARN("Using %sLod in fragment shader.\n", sample_function.name);
2305 shader_addline(arg->buffer, "%sLod(Psampler%u, %s, %s)%s);\n",
2306 sample_function.name, sampler_idx, coord_param.param_str, lod_param.param_str, dst_swizzle);
2307 } else {
2308 shader_addline(arg->buffer, "%sLod(Vsampler%u, %s, %s)%s);\n",
2309 sample_function.name, sampler_idx, coord_param.param_str, lod_param.param_str, dst_swizzle);
2313 static void pshader_glsl_texcoord(const SHADER_OPCODE_ARG *arg)
2315 /* FIXME: Make this work for more than just 2D textures */
2317 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
2318 SHADER_BUFFER* buffer = arg->buffer;
2319 DWORD hex_version = This->baseShader.hex_version;
2320 DWORD write_mask;
2321 char dst_mask[6];
2323 write_mask = shader_glsl_append_dst(arg->buffer, arg);
2324 shader_glsl_get_write_mask(write_mask, dst_mask);
2326 if (hex_version != WINED3DPS_VERSION(1,4)) {
2327 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2328 shader_addline(buffer, "clamp(gl_TexCoord[%u], 0.0, 1.0)%s);\n", reg, dst_mask);
2329 } else {
2330 DWORD reg = arg->src[0] & WINED3DSP_REGNUM_MASK;
2331 DWORD src_mod = arg->src[0] & WINED3DSP_SRCMOD_MASK;
2332 char dst_swizzle[6];
2334 shader_glsl_get_swizzle(arg->src[0], FALSE, write_mask, dst_swizzle);
2336 if (src_mod == WINED3DSPSM_DZ) {
2337 glsl_src_param_t div_param;
2338 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
2339 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_2, &div_param);
2341 if (mask_size > 1) {
2342 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
2343 } else {
2344 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
2346 } else if (src_mod == WINED3DSPSM_DW) {
2347 glsl_src_param_t div_param;
2348 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
2349 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_3, &div_param);
2351 if (mask_size > 1) {
2352 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
2353 } else {
2354 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
2356 } else {
2357 shader_addline(buffer, "gl_TexCoord[%u]%s);\n", reg, dst_swizzle);
2362 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
2363 * Take a 3-component dot product of the TexCoord[dstreg] and src,
2364 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
2365 static void pshader_glsl_texdp3tex(const SHADER_OPCODE_ARG *arg)
2367 glsl_src_param_t src0_param;
2368 char dst_mask[6];
2369 glsl_sample_function_t sample_function;
2370 DWORD sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2371 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2372 DWORD sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
2374 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2376 shader_glsl_append_dst(arg->buffer, arg);
2377 shader_glsl_get_write_mask(arg->dst, dst_mask);
2379 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
2380 * scalar, and projected sampling would require 4.
2382 * It is a dependent read - not valid with conditional NP2 textures
2384 shader_glsl_get_sample_function(sampler_type, FALSE, FALSE, &sample_function);
2386 switch(count_bits(sample_function.coord_mask)) {
2387 case 1:
2388 shader_addline(arg->buffer, "%s(Psampler%u, dot(gl_TexCoord[%u].xyz, %s))%s);\n",
2389 sample_function.name, sampler_idx, sampler_idx, src0_param.param_str, dst_mask);
2390 break;
2392 case 2:
2393 shader_addline(arg->buffer, "%s(Psampler%u, vec2(dot(gl_TexCoord[%u].xyz, %s), 0.0))%s);\n",
2394 sample_function.name, sampler_idx, sampler_idx, src0_param.param_str, dst_mask);
2395 break;
2397 case 3:
2398 shader_addline(arg->buffer, "%s(Psampler%u, vec3(dot(gl_TexCoord[%u].xyz, %s), 0.0, 0.0))%s);\n",
2399 sample_function.name, sampler_idx, sampler_idx, src0_param.param_str, dst_mask);
2400 break;
2401 default:
2402 FIXME("Unexpected mask bitcount %d\n", count_bits(sample_function.coord_mask));
2406 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
2407 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
2408 static void pshader_glsl_texdp3(const SHADER_OPCODE_ARG *arg)
2410 glsl_src_param_t src0_param;
2411 DWORD dstreg = arg->dst & WINED3DSP_REGNUM_MASK;
2412 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2413 DWORD dst_mask;
2414 unsigned int mask_size;
2416 dst_mask = shader_glsl_append_dst(arg->buffer, arg);
2417 mask_size = shader_glsl_get_write_mask_size(dst_mask);
2418 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2420 if (mask_size > 1) {
2421 shader_addline(arg->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
2422 } else {
2423 shader_addline(arg->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
2427 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
2428 * Calculate the depth as dst.x / dst.y */
2429 static void pshader_glsl_texdepth(const SHADER_OPCODE_ARG *arg)
2431 glsl_dst_param_t dst_param;
2433 shader_glsl_add_dst_param(arg, arg->dst, 0, &dst_param);
2435 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
2436 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
2437 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
2438 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
2439 * >= 1.0 or < 0.0
2441 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);
2444 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
2445 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
2446 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
2447 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
2449 static void pshader_glsl_texm3x2depth(const SHADER_OPCODE_ARG *arg)
2451 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2452 DWORD dstreg = arg->dst & WINED3DSP_REGNUM_MASK;
2453 glsl_src_param_t src0_param;
2455 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2457 shader_addline(arg->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
2458 shader_addline(arg->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
2461 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
2462 * Calculate the 1st of a 2-row matrix multiplication. */
2463 static void pshader_glsl_texm3x2pad(const SHADER_OPCODE_ARG *arg)
2465 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2466 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2467 SHADER_BUFFER* buffer = arg->buffer;
2468 glsl_src_param_t src0_param;
2470 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2471 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
2474 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
2475 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
2476 static void pshader_glsl_texm3x3pad(const SHADER_OPCODE_ARG* arg)
2478 IWineD3DPixelShaderImpl* shader = (IWineD3DPixelShaderImpl*) arg->shader;
2479 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2480 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2481 SHADER_BUFFER* buffer = arg->buffer;
2482 SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
2483 glsl_src_param_t src0_param;
2485 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2486 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + current_state->current_row, reg, src0_param.param_str);
2487 current_state->texcoord_w[current_state->current_row++] = reg;
2490 static void pshader_glsl_texm3x2tex(const SHADER_OPCODE_ARG *arg)
2492 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2493 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2494 SHADER_BUFFER* buffer = arg->buffer;
2495 glsl_src_param_t src0_param;
2496 char dst_mask[6];
2498 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2499 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
2501 shader_glsl_append_dst(buffer, arg);
2502 shader_glsl_get_write_mask(arg->dst, dst_mask);
2504 /* Sample the texture using the calculated coordinates */
2505 shader_addline(buffer, "texture2D(Psampler%u, tmp0.xy)%s);\n", reg, dst_mask);
2508 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
2509 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
2510 static void pshader_glsl_texm3x3tex(const SHADER_OPCODE_ARG *arg)
2512 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2513 glsl_src_param_t src0_param;
2514 char dst_mask[6];
2515 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2516 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
2517 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
2518 DWORD sampler_type = arg->reg_maps->samplers[reg] & WINED3DSP_TEXTURETYPE_MASK;
2519 glsl_sample_function_t sample_function;
2521 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2522 shader_addline(arg->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
2524 shader_glsl_append_dst(arg->buffer, arg);
2525 shader_glsl_get_write_mask(arg->dst, dst_mask);
2526 /* Dependent read, not valid with conditional NP2 */
2527 shader_glsl_get_sample_function(sampler_type, FALSE, FALSE, &sample_function);
2529 /* Sample the texture using the calculated coordinates */
2530 shader_addline(arg->buffer, "%s(Psampler%u, tmp0.xyz)%s);\n", sample_function.name, reg, dst_mask);
2532 current_state->current_row = 0;
2535 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
2536 * Perform the 3rd row of a 3x3 matrix multiply */
2537 static void pshader_glsl_texm3x3(const SHADER_OPCODE_ARG *arg)
2539 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2540 glsl_src_param_t src0_param;
2541 char dst_mask[6];
2542 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2543 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
2544 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
2546 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2548 shader_glsl_append_dst(arg->buffer, arg);
2549 shader_glsl_get_write_mask(arg->dst, dst_mask);
2550 shader_addline(arg->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
2552 current_state->current_row = 0;
2555 /** Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
2556 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
2557 static void pshader_glsl_texm3x3spec(const SHADER_OPCODE_ARG *arg)
2559 IWineD3DPixelShaderImpl* shader = (IWineD3DPixelShaderImpl*) arg->shader;
2560 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2561 glsl_src_param_t src0_param;
2562 glsl_src_param_t src1_param;
2563 char dst_mask[6];
2564 SHADER_BUFFER* buffer = arg->buffer;
2565 SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
2566 DWORD stype = arg->reg_maps->samplers[reg] & WINED3DSP_TEXTURETYPE_MASK;
2567 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2568 glsl_sample_function_t sample_function;
2570 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2571 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], src_mask, &src1_param);
2573 /* Perform the last matrix multiply operation */
2574 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
2575 /* Reflection calculation */
2576 shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
2578 shader_glsl_append_dst(buffer, arg);
2579 shader_glsl_get_write_mask(arg->dst, dst_mask);
2580 /* Dependent read, not valid with conditional NP2 */
2581 shader_glsl_get_sample_function(stype, FALSE, FALSE, &sample_function);
2583 /* Sample the texture */
2584 shader_addline(buffer, "%s(Psampler%u, tmp0.xyz)%s);\n", sample_function.name, reg, dst_mask);
2586 current_state->current_row = 0;
2589 /** Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
2590 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
2591 static void pshader_glsl_texm3x3vspec(const SHADER_OPCODE_ARG *arg)
2593 IWineD3DPixelShaderImpl* shader = (IWineD3DPixelShaderImpl*) arg->shader;
2594 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2595 SHADER_BUFFER* buffer = arg->buffer;
2596 SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
2597 glsl_src_param_t src0_param;
2598 char dst_mask[6];
2599 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2600 DWORD sampler_type = arg->reg_maps->samplers[reg] & WINED3DSP_TEXTURETYPE_MASK;
2601 glsl_sample_function_t sample_function;
2603 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2605 /* Perform the last matrix multiply operation */
2606 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
2608 /* Construct the eye-ray vector from w coordinates */
2609 shader_addline(buffer, "tmp1.xyz = normalize(vec3(gl_TexCoord[%u].w, gl_TexCoord[%u].w, gl_TexCoord[%u].w));\n",
2610 current_state->texcoord_w[0], current_state->texcoord_w[1], reg);
2611 shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
2613 shader_glsl_append_dst(buffer, arg);
2614 shader_glsl_get_write_mask(arg->dst, dst_mask);
2615 /* Dependent read, not valid with conditional NP2 */
2616 shader_glsl_get_sample_function(sampler_type, FALSE, FALSE, &sample_function);
2618 /* Sample the texture using the calculated coordinates */
2619 shader_addline(buffer, "%s(Psampler%u, tmp0.xyz)%s);\n", sample_function.name, reg, dst_mask);
2621 current_state->current_row = 0;
2624 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
2625 * Apply a fake bump map transform.
2626 * texbem is pshader <= 1.3 only, this saves a few version checks
2628 static void pshader_glsl_texbem(const SHADER_OPCODE_ARG *arg)
2630 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
2631 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
2632 char dst_swizzle[6];
2633 glsl_sample_function_t sample_function;
2634 glsl_src_param_t coord_param;
2635 DWORD sampler_type;
2636 DWORD sampler_idx;
2637 DWORD mask;
2638 DWORD flags;
2639 char coord_mask[6];
2641 sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2642 flags = deviceImpl->stateBlock->textureState[sampler_idx][WINED3DTSS_TEXTURETRANSFORMFLAGS];
2644 sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
2645 /* Dependent read, not valid with conditional NP2 */
2646 shader_glsl_get_sample_function(sampler_type, FALSE, FALSE, &sample_function);
2647 mask = sample_function.coord_mask;
2649 shader_glsl_get_write_mask(arg->dst, dst_swizzle);
2651 shader_glsl_get_write_mask(mask, coord_mask);
2653 /* with projective textures, texbem only divides the static texture coord, not the displacement,
2654 * so we can't let the GL handle this.
2656 if (flags & WINED3DTTFF_PROJECTED) {
2657 DWORD div_mask=0;
2658 char coord_div_mask[3];
2659 switch (flags & ~WINED3DTTFF_PROJECTED) {
2660 case WINED3DTTFF_COUNT1: FIXME("WINED3DTTFF_PROJECTED with WINED3DTTFF_COUNT1?\n"); break;
2661 case WINED3DTTFF_COUNT2: div_mask = WINED3DSP_WRITEMASK_1; break;
2662 case WINED3DTTFF_COUNT3: div_mask = WINED3DSP_WRITEMASK_2; break;
2663 case WINED3DTTFF_COUNT4:
2664 case WINED3DTTFF_DISABLE: div_mask = WINED3DSP_WRITEMASK_3; break;
2666 shader_glsl_get_write_mask(div_mask, coord_div_mask);
2667 shader_addline(arg->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
2670 shader_glsl_append_dst(arg->buffer, arg);
2671 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0|WINED3DSP_WRITEMASK_1, &coord_param);
2672 if(arg->opcode->opcode == WINED3DSIO_TEXBEML) {
2673 glsl_src_param_t luminance_param;
2674 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_2, &luminance_param);
2675 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",
2676 sample_function.name, sampler_idx, sampler_idx, coord_mask, sampler_idx, coord_param.param_str, coord_mask,
2677 luminance_param.param_str, sampler_idx, sampler_idx, dst_swizzle);
2678 } else {
2679 shader_addline(arg->buffer, "%s(Psampler%u, T%u%s + vec4(bumpenvmat%d * %s, 0.0, 0.0)%s )%s);\n",
2680 sample_function.name, sampler_idx, sampler_idx, coord_mask, sampler_idx, coord_param.param_str, coord_mask, dst_swizzle);
2684 static void pshader_glsl_bem(const SHADER_OPCODE_ARG *arg)
2686 glsl_src_param_t src0_param, src1_param;
2687 DWORD sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2689 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0|WINED3DSP_WRITEMASK_1, &src0_param);
2690 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0|WINED3DSP_WRITEMASK_1, &src1_param);
2692 shader_glsl_append_dst(arg->buffer, arg);
2693 shader_addline(arg->buffer, "%s + bumpenvmat%d * %s);\n",
2694 src0_param.param_str, sampler_idx, src1_param.param_str);
2697 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
2698 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
2699 static void pshader_glsl_texreg2ar(const SHADER_OPCODE_ARG *arg)
2701 glsl_src_param_t src0_param;
2702 DWORD sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2703 char dst_mask[6];
2705 shader_glsl_append_dst(arg->buffer, arg);
2706 shader_glsl_get_write_mask(arg->dst, dst_mask);
2707 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
2709 shader_addline(arg->buffer, "texture2D(Psampler%u, %s.wx)%s);\n", sampler_idx, src0_param.reg_name, dst_mask);
2712 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
2713 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
2714 static void pshader_glsl_texreg2gb(const SHADER_OPCODE_ARG *arg)
2716 glsl_src_param_t src0_param;
2717 DWORD sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2718 char dst_mask[6];
2720 shader_glsl_append_dst(arg->buffer, arg);
2721 shader_glsl_get_write_mask(arg->dst, dst_mask);
2722 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
2724 shader_addline(arg->buffer, "texture2D(Psampler%u, %s.yz)%s);\n", sampler_idx, src0_param.reg_name, dst_mask);
2727 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
2728 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
2729 static void pshader_glsl_texreg2rgb(const SHADER_OPCODE_ARG *arg)
2731 glsl_src_param_t src0_param;
2732 char dst_mask[6];
2733 DWORD sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2734 DWORD sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
2735 glsl_sample_function_t sample_function;
2737 shader_glsl_append_dst(arg->buffer, arg);
2738 shader_glsl_get_write_mask(arg->dst, dst_mask);
2739 /* Dependent read, not valid with conditional NP2 */
2740 shader_glsl_get_sample_function(sampler_type, FALSE, FALSE, &sample_function);
2741 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], sample_function.coord_mask, &src0_param);
2743 shader_addline(arg->buffer, "%s(Psampler%u, %s)%s);\n", sample_function.name, sampler_idx, src0_param.param_str, dst_mask);
2746 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
2747 * If any of the first 3 components are < 0, discard this pixel */
2748 static void pshader_glsl_texkill(const SHADER_OPCODE_ARG *arg)
2750 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
2751 DWORD hex_version = This->baseShader.hex_version;
2752 glsl_dst_param_t dst_param;
2754 /* The argument is a destination parameter, and no writemasks are allowed */
2755 shader_glsl_add_dst_param(arg, arg->dst, 0, &dst_param);
2756 if((hex_version >= WINED3DPS_VERSION(2,0))) {
2757 /* 2.0 shaders compare all 4 components in texkill */
2758 shader_addline(arg->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
2759 } else {
2760 /* 1.X shaders only compare the first 3 components, probably due to the nature of the texkill
2761 * instruction as a tex* instruction, and phase, which kills all a / w components. Even if all
2762 * 4 components are defined, only the first 3 are used
2764 shader_addline(arg->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
2768 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
2769 * dst = dot2(src0, src1) + src2 */
2770 static void pshader_glsl_dp2add(const SHADER_OPCODE_ARG *arg)
2772 glsl_src_param_t src0_param;
2773 glsl_src_param_t src1_param;
2774 glsl_src_param_t src2_param;
2775 DWORD write_mask;
2776 unsigned int mask_size;
2778 write_mask = shader_glsl_append_dst(arg->buffer, arg);
2779 mask_size = shader_glsl_get_write_mask_size(write_mask);
2781 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
2782 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
2783 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], WINED3DSP_WRITEMASK_0, &src2_param);
2785 if (mask_size > 1) {
2786 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);
2787 } else {
2788 shader_addline(arg->buffer, "dot(%s, %s) + %s);\n", src0_param.param_str, src1_param.param_str, src2_param.param_str);
2792 static void pshader_glsl_input_pack(SHADER_BUFFER* buffer, const struct semantic* semantics_in,
2793 IWineD3DPixelShader *iface, enum vertexprocessing_mode vertexprocessing)
2795 unsigned int i;
2796 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *) iface;
2798 for (i = 0; i < MAX_REG_INPUT; i++) {
2800 DWORD usage_token = semantics_in[i].usage;
2801 DWORD register_token = semantics_in[i].reg;
2802 DWORD usage, usage_idx;
2803 char reg_mask[6];
2805 /* Uninitialized */
2806 if (!usage_token) continue;
2807 usage = (usage_token & WINED3DSP_DCL_USAGE_MASK) >> WINED3DSP_DCL_USAGE_SHIFT;
2808 usage_idx = (usage_token & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
2809 shader_glsl_get_write_mask(register_token, reg_mask);
2811 switch(usage) {
2813 case WINED3DDECLUSAGE_TEXCOORD:
2814 if(usage_idx < 8 && vertexprocessing == pretransformed) {
2815 shader_addline(buffer, "IN[%u]%s = gl_TexCoord[%u]%s;\n",
2816 This->input_reg_map[i], reg_mask, usage_idx, reg_mask);
2817 } else {
2818 shader_addline(buffer, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
2819 This->input_reg_map[i], reg_mask, reg_mask);
2821 break;
2823 case WINED3DDECLUSAGE_COLOR:
2824 if (usage_idx == 0)
2825 shader_addline(buffer, "IN[%u]%s = vec4(gl_Color)%s;\n",
2826 This->input_reg_map[i], reg_mask, reg_mask);
2827 else if (usage_idx == 1)
2828 shader_addline(buffer, "IN[%u]%s = vec4(gl_SecondaryColor)%s;\n",
2829 This->input_reg_map[i], reg_mask, reg_mask);
2830 else
2831 shader_addline(buffer, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
2832 This->input_reg_map[i], reg_mask, reg_mask);
2833 break;
2835 default:
2836 shader_addline(buffer, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
2837 This->input_reg_map[i], reg_mask, reg_mask);
2842 /*********************************************
2843 * Vertex Shader Specific Code begins here
2844 ********************************************/
2846 static void add_glsl_program_entry(struct shader_glsl_priv *priv, struct glsl_shader_prog_link *entry) {
2847 glsl_program_key_t *key;
2849 key = HeapAlloc(GetProcessHeap(), 0, sizeof(glsl_program_key_t));
2850 key->vshader = entry->vshader;
2851 key->pshader = entry->pshader;
2852 key->ps_args = entry->ps_args;
2854 hash_table_put(priv->glsl_program_lookup, key, entry);
2857 static struct glsl_shader_prog_link *get_glsl_program_entry(struct shader_glsl_priv *priv,
2858 GLhandleARB vshader, IWineD3DPixelShader *pshader, struct ps_compile_args *ps_args) {
2859 glsl_program_key_t key;
2861 key.vshader = vshader;
2862 key.pshader = pshader;
2863 key.ps_args = *ps_args;
2865 return (struct glsl_shader_prog_link *)hash_table_get(priv->glsl_program_lookup, &key);
2868 static void delete_glsl_program_entry(struct shader_glsl_priv *priv, const WineD3D_GL_Info *gl_info,
2869 struct glsl_shader_prog_link *entry)
2871 glsl_program_key_t *key;
2873 key = HeapAlloc(GetProcessHeap(), 0, sizeof(glsl_program_key_t));
2874 key->vshader = entry->vshader;
2875 key->pshader = entry->pshader;
2876 key->ps_args = entry->ps_args;
2877 hash_table_remove(priv->glsl_program_lookup, key);
2879 GL_EXTCALL(glDeleteObjectARB(entry->programId));
2880 if (entry->vshader) list_remove(&entry->vshader_entry);
2881 if (entry->pshader) list_remove(&entry->pshader_entry);
2882 HeapFree(GetProcessHeap(), 0, entry->vuniformF_locations);
2883 HeapFree(GetProcessHeap(), 0, entry->puniformF_locations);
2884 HeapFree(GetProcessHeap(), 0, entry);
2887 static void handle_ps3_input(SHADER_BUFFER *buffer, const struct semantic *semantics_in,
2888 const struct semantic *semantics_out, const WineD3D_GL_Info *gl_info, const DWORD *map)
2890 unsigned int i, j;
2891 DWORD usage_token, usage_token_out;
2892 DWORD register_token, register_token_out;
2893 DWORD usage, usage_idx, usage_out, usage_idx_out;
2894 DWORD *set;
2895 DWORD in_idx;
2896 DWORD in_count = GL_LIMITS(glsl_varyings) / 4;
2897 char reg_mask[6], reg_mask_out[6];
2898 char destination[50];
2900 set = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*set) * (in_count + 2));
2902 if (!semantics_out) {
2903 /* Save gl_FrontColor & gl_FrontSecondaryColor before overwriting them. */
2904 shader_addline(buffer, "vec4 front_color = gl_FrontColor;\n");
2905 shader_addline(buffer, "vec4 front_secondary_color = gl_FrontSecondaryColor;\n");
2908 for(i = 0; i < MAX_REG_INPUT; i++) {
2909 usage_token = semantics_in[i].usage;
2910 if (!usage_token) continue;
2912 in_idx = map[i];
2913 if (in_idx >= (in_count + 2)) {
2914 FIXME("More input varyings declared than supported, expect issues\n");
2915 continue;
2916 } else if(map[i] == -1) {
2917 /* Declared, but not read register */
2918 continue;
2921 if (in_idx == in_count) {
2922 sprintf(destination, "gl_FrontColor");
2923 } else if (in_idx == in_count + 1) {
2924 sprintf(destination, "gl_FrontSecondaryColor");
2925 } else {
2926 sprintf(destination, "IN[%u]", in_idx);
2929 register_token = semantics_in[i].reg;
2931 usage = (usage_token & WINED3DSP_DCL_USAGE_MASK) >> WINED3DSP_DCL_USAGE_SHIFT;
2932 usage_idx = (usage_token & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
2933 set[map[i]] = shader_glsl_get_write_mask(register_token, reg_mask);
2935 if(!semantics_out) {
2936 switch(usage) {
2937 case WINED3DDECLUSAGE_COLOR:
2938 if (usage_idx == 0)
2939 shader_addline(buffer, "%s%s = front_color%s;\n",
2940 destination, reg_mask, reg_mask);
2941 else if (usage_idx == 1)
2942 shader_addline(buffer, "%s%s = front_secondary_color%s;\n",
2943 destination, reg_mask, reg_mask);
2944 else
2945 shader_addline(buffer, "%s%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
2946 destination, reg_mask, reg_mask);
2947 break;
2949 case WINED3DDECLUSAGE_TEXCOORD:
2950 if (usage_idx < 8) {
2951 shader_addline(buffer, "%s%s = gl_TexCoord[%u]%s;\n",
2952 destination, reg_mask, usage_idx, reg_mask);
2953 } else {
2954 shader_addline(buffer, "%s%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
2955 destination, reg_mask, reg_mask);
2957 break;
2959 case WINED3DDECLUSAGE_FOG:
2960 shader_addline(buffer, "%s%s = vec4(gl_FogFragCoord, 0.0, 0.0, 0.0)%s;\n",
2961 destination, reg_mask, reg_mask);
2962 break;
2964 default:
2965 shader_addline(buffer, "%s%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
2966 destination, reg_mask, reg_mask);
2968 } else {
2969 BOOL found = FALSE;
2970 for(j = 0; j < MAX_REG_OUTPUT; j++) {
2971 usage_token_out = semantics_out[j].usage;
2972 if (!usage_token_out) continue;
2973 register_token_out = semantics_out[j].reg;
2975 usage_out = (usage_token_out & WINED3DSP_DCL_USAGE_MASK) >> WINED3DSP_DCL_USAGE_SHIFT;
2976 usage_idx_out = (usage_token_out & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
2977 shader_glsl_get_write_mask(register_token_out, reg_mask_out);
2979 if(usage == usage_out &&
2980 usage_idx == usage_idx_out) {
2981 shader_addline(buffer, "%s%s = OUT[%u]%s;\n",
2982 destination, reg_mask, j, reg_mask);
2983 found = TRUE;
2986 if(!found) {
2987 shader_addline(buffer, "%s%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
2988 destination, reg_mask, reg_mask);
2993 /* This is solely to make the compiler / linker happy and avoid warning about undefined
2994 * varyings. It shouldn't result in any real code executed on the GPU, since all read
2995 * input varyings are assigned above, if the optimizer works properly.
2997 for(i = 0; i < in_count + 2; i++) {
2998 if(set[i] != WINED3DSP_WRITEMASK_ALL) {
2999 unsigned int size = 0;
3000 memset(reg_mask, 0, sizeof(reg_mask));
3001 if(!(set[i] & WINED3DSP_WRITEMASK_0)) {
3002 reg_mask[size] = 'x';
3003 size++;
3005 if(!(set[i] & WINED3DSP_WRITEMASK_1)) {
3006 reg_mask[size] = 'y';
3007 size++;
3009 if(!(set[i] & WINED3DSP_WRITEMASK_2)) {
3010 reg_mask[size] = 'z';
3011 size++;
3013 if(!(set[i] & WINED3DSP_WRITEMASK_3)) {
3014 reg_mask[size] = 'w';
3015 size++;
3018 if (i == in_count) {
3019 sprintf(destination, "gl_FrontColor");
3020 } else if (i == in_count + 1) {
3021 sprintf(destination, "gl_FrontSecondaryColor");
3022 } else {
3023 sprintf(destination, "IN[%u]", i);
3026 if (size == 1) {
3027 shader_addline(buffer, "%s.%s = 0.0;\n", destination, reg_mask);
3028 } else {
3029 shader_addline(buffer, "%s.%s = vec%u(0.0);\n", destination, reg_mask, size);
3034 HeapFree(GetProcessHeap(), 0, set);
3037 static GLhandleARB generate_param_reorder_function(IWineD3DVertexShader *vertexshader,
3038 IWineD3DPixelShader *pixelshader, const WineD3D_GL_Info *gl_info)
3040 GLhandleARB ret = 0;
3041 IWineD3DVertexShaderImpl *vs = (IWineD3DVertexShaderImpl *) vertexshader;
3042 IWineD3DPixelShaderImpl *ps = (IWineD3DPixelShaderImpl *) pixelshader;
3043 IWineD3DDeviceImpl *device;
3044 DWORD vs_major = WINED3DSHADER_VERSION_MAJOR(vs->baseShader.hex_version);
3045 DWORD ps_major = ps ? WINED3DSHADER_VERSION_MAJOR(ps->baseShader.hex_version) : 0;
3046 unsigned int i;
3047 SHADER_BUFFER buffer;
3048 DWORD usage_token;
3049 DWORD register_token;
3050 DWORD usage, usage_idx, writemask;
3051 char reg_mask[6];
3052 const struct semantic *semantics_out, *semantics_in;
3054 buffer.buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, SHADER_PGMSIZE);
3055 buffer.bsize = 0;
3056 buffer.lineNo = 0;
3057 buffer.newline = TRUE;
3059 shader_addline(&buffer, "#version 120\n");
3061 if(vs_major < 3 && ps_major < 3) {
3062 /* That one is easy: The vertex shader writes to the builtin varyings, the pixel shader reads from them.
3063 * Take care about the texcoord .w fixup though if we're using the fixed function fragment pipeline
3065 device = (IWineD3DDeviceImpl *) vs->baseShader.device;
3066 if((GLINFO_LOCATION).set_texcoord_w && ps_major == 0 && vs_major > 0 &&
3067 !device->frag_pipe->ffp_proj_control) {
3068 shader_addline(&buffer, "void order_ps_input() {\n");
3069 for(i = 0; i < min(8, MAX_REG_TEXCRD); i++) {
3070 if(vs->baseShader.reg_maps.texcoord_mask[i] != 0 &&
3071 vs->baseShader.reg_maps.texcoord_mask[i] != WINED3DSP_WRITEMASK_ALL) {
3072 shader_addline(&buffer, "gl_TexCoord[%u].w = 1.0;\n", i);
3075 shader_addline(&buffer, "}\n");
3076 } else {
3077 shader_addline(&buffer, "void order_ps_input() { /* do nothing */ }\n");
3079 } else if(ps_major < 3 && vs_major >= 3) {
3080 /* The vertex shader writes to its own varyings, the pixel shader needs them in the builtin ones */
3081 semantics_out = vs->semantics_out;
3083 shader_addline(&buffer, "void order_ps_input(in vec4 OUT[%u]) {\n", MAX_REG_OUTPUT);
3084 for(i = 0; i < MAX_REG_OUTPUT; i++) {
3085 usage_token = semantics_out[i].usage;
3086 if (!usage_token) continue;
3087 register_token = semantics_out[i].reg;
3089 usage = (usage_token & WINED3DSP_DCL_USAGE_MASK) >> WINED3DSP_DCL_USAGE_SHIFT;
3090 usage_idx = (usage_token & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
3091 writemask = shader_glsl_get_write_mask(register_token, reg_mask);
3093 switch(usage) {
3094 case WINED3DDECLUSAGE_COLOR:
3095 if (usage_idx == 0)
3096 shader_addline(&buffer, "gl_FrontColor%s = OUT[%u]%s;\n", reg_mask, i, reg_mask);
3097 else if (usage_idx == 1)
3098 shader_addline(&buffer, "gl_FrontSecondaryColor%s = OUT[%u]%s;\n", reg_mask, i, reg_mask);
3099 break;
3101 case WINED3DDECLUSAGE_POSITION:
3102 shader_addline(&buffer, "gl_Position%s = OUT[%u]%s;\n", reg_mask, i, reg_mask);
3103 break;
3105 case WINED3DDECLUSAGE_TEXCOORD:
3106 if (usage_idx < 8) {
3107 if(!(GLINFO_LOCATION).set_texcoord_w || ps_major > 0) writemask |= WINED3DSP_WRITEMASK_3;
3109 shader_addline(&buffer, "gl_TexCoord[%u]%s = OUT[%u]%s;\n",
3110 usage_idx, reg_mask, i, reg_mask);
3111 if(!(writemask & WINED3DSP_WRITEMASK_3)) {
3112 shader_addline(&buffer, "gl_TexCoord[%u].w = 1.0;\n", usage_idx);
3115 break;
3117 case WINED3DDECLUSAGE_PSIZE:
3118 shader_addline(&buffer, "gl_PointSize = OUT[%u].x;\n", i);
3119 break;
3121 case WINED3DDECLUSAGE_FOG:
3122 shader_addline(&buffer, "gl_FogFragCoord = OUT[%u].%c;\n", i, reg_mask[1]);
3123 break;
3125 default:
3126 break;
3129 shader_addline(&buffer, "}\n");
3131 } else if(ps_major >= 3 && vs_major >= 3) {
3132 semantics_out = vs->semantics_out;
3133 semantics_in = ps->semantics_in;
3135 /* This one is tricky: a 3.0 pixel shader reads from a 3.0 vertex shader */
3136 shader_addline(&buffer, "varying vec4 IN[%u];\n", GL_LIMITS(glsl_varyings) / 4);
3137 shader_addline(&buffer, "void order_ps_input(in vec4 OUT[%u]) {\n", MAX_REG_OUTPUT);
3139 /* First, sort out position and point size. Those are not passed to the pixel shader */
3140 for(i = 0; i < MAX_REG_OUTPUT; i++) {
3141 usage_token = semantics_out[i].usage;
3142 if (!usage_token) continue;
3143 register_token = semantics_out[i].reg;
3145 usage = (usage_token & WINED3DSP_DCL_USAGE_MASK) >> WINED3DSP_DCL_USAGE_SHIFT;
3146 usage_idx = (usage_token & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
3147 shader_glsl_get_write_mask(register_token, reg_mask);
3149 switch(usage) {
3150 case WINED3DDECLUSAGE_POSITION:
3151 shader_addline(&buffer, "gl_Position%s = OUT[%u]%s;\n", reg_mask, i, reg_mask);
3152 break;
3154 case WINED3DDECLUSAGE_PSIZE:
3155 shader_addline(&buffer, "gl_PointSize = OUT[%u].x;\n", i);
3156 break;
3158 default:
3159 break;
3163 /* Then, fix the pixel shader input */
3164 handle_ps3_input(&buffer, semantics_in, semantics_out, gl_info, ps->input_reg_map);
3166 shader_addline(&buffer, "}\n");
3167 } else if(ps_major >= 3 && vs_major < 3) {
3168 semantics_in = ps->semantics_in;
3170 shader_addline(&buffer, "varying vec4 IN[%u];\n", GL_LIMITS(glsl_varyings) / 4);
3171 shader_addline(&buffer, "void order_ps_input() {\n");
3172 /* The vertex shader wrote to the builtin varyings. There is no need to figure out position and
3173 * point size, but we depend on the optimizers kindness to find out that the pixel shader doesn't
3174 * read gl_TexCoord and gl_ColorX, otherwise we'll run out of varyings
3176 handle_ps3_input(&buffer, semantics_in, NULL, gl_info, ps->input_reg_map);
3177 shader_addline(&buffer, "}\n");
3178 } else {
3179 ERR("Unexpected vertex and pixel shader version condition: vs: %d, ps: %d\n", vs_major, ps_major);
3182 ret = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
3183 checkGLcall("glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB)");
3184 GL_EXTCALL(glShaderSourceARB(ret, 1, (const char**)&buffer.buffer, NULL));
3185 checkGLcall("glShaderSourceARB(ret, 1, (const char**)&buffer.buffer, NULL)");
3186 GL_EXTCALL(glCompileShaderARB(ret));
3187 checkGLcall("glCompileShaderARB(ret)");
3189 HeapFree(GetProcessHeap(), 0, buffer.buffer);
3190 return ret;
3193 static void hardcode_local_constants(IWineD3DBaseShaderImpl *shader, const WineD3D_GL_Info *gl_info,
3194 GLhandleARB programId, char prefix)
3196 const local_constant *lconst;
3197 GLuint tmp_loc;
3198 const float *value;
3199 char glsl_name[8];
3201 LIST_FOR_EACH_ENTRY(lconst, &shader->baseShader.constantsF, local_constant, entry) {
3202 value = (const float *)lconst->value;
3203 snprintf(glsl_name, sizeof(glsl_name), "%cLC%u", prefix, lconst->idx);
3204 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
3205 GL_EXTCALL(glUniform4fvARB(tmp_loc, 1, value));
3207 checkGLcall("Hardcoding local constants\n");
3210 /** Sets the GLSL program ID for the given pixel and vertex shader combination.
3211 * It sets the programId on the current StateBlock (because it should be called
3212 * inside of the DrawPrimitive() part of the render loop).
3214 * If a program for the given combination does not exist, create one, and store
3215 * the program in the hash table. If it creates a program, it will link the
3216 * given objects, too.
3218 static void set_glsl_shader_program(IWineD3DDevice *iface, BOOL use_ps, BOOL use_vs) {
3219 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
3220 struct shader_glsl_priv *priv = (struct shader_glsl_priv *)This->shader_priv;
3221 const WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
3222 IWineD3DPixelShader *pshader = This->stateBlock->pixelShader;
3223 IWineD3DVertexShader *vshader = This->stateBlock->vertexShader;
3224 struct glsl_shader_prog_link *entry = NULL;
3225 GLhandleARB programId = 0;
3226 GLhandleARB reorder_shader_id = 0;
3227 int i;
3228 char glsl_name[8];
3229 GLhandleARB vshader_id, pshader_id;
3230 struct ps_compile_args compile_args;
3232 if(use_vs) {
3233 IWineD3DVertexShaderImpl_CompileShader(vshader);
3234 vshader_id = ((IWineD3DVertexShaderImpl*)vshader)->prgId;
3235 } else {
3236 vshader_id = 0;
3238 if(use_ps) {
3239 find_ps_compile_args((IWineD3DPixelShaderImpl*)This->stateBlock->pixelShader, This->stateBlock, &compile_args);
3240 } else {
3241 /* FIXME: Do we really have to spend CPU cycles to generate a few zeroed bytes? */
3242 memset(&compile_args, 0, sizeof(compile_args));
3244 entry = get_glsl_program_entry(priv, vshader_id, pshader, &compile_args);
3245 if (entry) {
3246 priv->glsl_program = entry;
3247 return;
3250 /* If we get to this point, then no matching program exists, so we create one */
3251 programId = GL_EXTCALL(glCreateProgramObjectARB());
3252 TRACE("Created new GLSL shader program %u\n", programId);
3254 /* Create the entry */
3255 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link));
3256 entry->programId = programId;
3257 entry->vshader = vshader_id;
3258 entry->pshader = pshader;
3259 entry->ps_args = compile_args;
3260 /* Add the hash table entry */
3261 add_glsl_program_entry(priv, entry);
3263 /* Set the current program */
3264 priv->glsl_program = entry;
3266 /* Attach GLSL vshader */
3267 if (vshader_id) {
3268 int max_attribs = 16; /* TODO: Will this always be the case? It is at the moment... */
3269 char tmp_name[10];
3271 reorder_shader_id = generate_param_reorder_function(vshader, pshader, gl_info);
3272 TRACE("Attaching GLSL shader object %u to program %u\n", reorder_shader_id, programId);
3273 GL_EXTCALL(glAttachObjectARB(programId, reorder_shader_id));
3274 checkGLcall("glAttachObjectARB");
3275 /* Flag the reorder function for deletion, then it will be freed automatically when the program
3276 * is destroyed
3278 GL_EXTCALL(glDeleteObjectARB(reorder_shader_id));
3280 TRACE("Attaching GLSL shader object %u to program %u\n", vshader_id, programId);
3281 GL_EXTCALL(glAttachObjectARB(programId, vshader_id));
3282 checkGLcall("glAttachObjectARB");
3284 /* Bind vertex attributes to a corresponding index number to match
3285 * the same index numbers as ARB_vertex_programs (makes loading
3286 * vertex attributes simpler). With this method, we can use the
3287 * exact same code to load the attributes later for both ARB and
3288 * GLSL shaders.
3290 * We have to do this here because we need to know the Program ID
3291 * in order to make the bindings work, and it has to be done prior
3292 * to linking the GLSL program. */
3293 for (i = 0; i < max_attribs; ++i) {
3294 if (((IWineD3DBaseShaderImpl*)vshader)->baseShader.reg_maps.attributes[i]) {
3295 snprintf(tmp_name, sizeof(tmp_name), "attrib%i", i);
3296 GL_EXTCALL(glBindAttribLocationARB(programId, i, tmp_name));
3299 checkGLcall("glBindAttribLocationARB");
3301 list_add_head(&((IWineD3DBaseShaderImpl *)vshader)->baseShader.linked_programs, &entry->vshader_entry);
3304 if(use_ps) {
3305 pshader_id = find_gl_pshader((IWineD3DPixelShaderImpl *) pshader, &compile_args);
3306 } else {
3307 pshader_id = 0;
3310 /* Attach GLSL pshader */
3311 if (pshader_id) {
3312 TRACE("Attaching GLSL shader object %u to program %u\n", pshader_id, programId);
3313 GL_EXTCALL(glAttachObjectARB(programId, pshader_id));
3314 checkGLcall("glAttachObjectARB");
3316 list_add_head(&((IWineD3DBaseShaderImpl *)pshader)->baseShader.linked_programs, &entry->pshader_entry);
3319 /* Link the program */
3320 TRACE("Linking GLSL shader program %u\n", programId);
3321 GL_EXTCALL(glLinkProgramARB(programId));
3322 print_glsl_info_log(&GLINFO_LOCATION, programId);
3324 entry->vuniformF_locations = HeapAlloc(GetProcessHeap(), 0, sizeof(GLhandleARB) * GL_LIMITS(vshader_constantsF));
3325 for (i = 0; i < GL_LIMITS(vshader_constantsF); ++i) {
3326 snprintf(glsl_name, sizeof(glsl_name), "VC[%i]", i);
3327 entry->vuniformF_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
3329 for (i = 0; i < MAX_CONST_I; ++i) {
3330 snprintf(glsl_name, sizeof(glsl_name), "VI[%i]", i);
3331 entry->vuniformI_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
3333 entry->puniformF_locations = HeapAlloc(GetProcessHeap(), 0, sizeof(GLhandleARB) * GL_LIMITS(pshader_constantsF));
3334 for (i = 0; i < GL_LIMITS(pshader_constantsF); ++i) {
3335 snprintf(glsl_name, sizeof(glsl_name), "PC[%i]", i);
3336 entry->puniformF_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
3338 for (i = 0; i < MAX_CONST_I; ++i) {
3339 snprintf(glsl_name, sizeof(glsl_name), "PI[%i]", i);
3340 entry->puniformI_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
3343 if(pshader) {
3344 for(i = 0; i < ((IWineD3DPixelShaderImpl*)pshader)->numbumpenvmatconsts; i++) {
3345 char name[32];
3346 sprintf(name, "bumpenvmat%d", ((IWineD3DPixelShaderImpl*)pshader)->bumpenvmatconst[i].texunit);
3347 entry->bumpenvmat_location[i] = GL_EXTCALL(glGetUniformLocationARB(programId, name));
3348 sprintf(name, "luminancescale%d", ((IWineD3DPixelShaderImpl*)pshader)->luminanceconst[i].texunit);
3349 entry->luminancescale_location[i] = GL_EXTCALL(glGetUniformLocationARB(programId, name));
3350 sprintf(name, "luminanceoffset%d", ((IWineD3DPixelShaderImpl*)pshader)->luminanceconst[i].texunit);
3351 entry->luminanceoffset_location[i] = GL_EXTCALL(glGetUniformLocationARB(programId, name));
3356 entry->posFixup_location = GL_EXTCALL(glGetUniformLocationARB(programId, "posFixup"));
3357 entry->ycorrection_location = GL_EXTCALL(glGetUniformLocationARB(programId, "ycorrection"));
3358 checkGLcall("Find glsl program uniform locations");
3360 if (pshader && WINED3DSHADER_VERSION_MAJOR(((IWineD3DPixelShaderImpl *)pshader)->baseShader.hex_version) >= 3
3361 && ((IWineD3DPixelShaderImpl *)pshader)->declared_in_count > GL_LIMITS(glsl_varyings) / 4) {
3362 TRACE("Shader %d needs vertex color clamping disabled\n", programId);
3363 entry->vertex_color_clamp = GL_FALSE;
3364 } else {
3365 entry->vertex_color_clamp = GL_FIXED_ONLY_ARB;
3368 /* Set the shader to allow uniform loading on it */
3369 GL_EXTCALL(glUseProgramObjectARB(programId));
3370 checkGLcall("glUseProgramObjectARB(programId)");
3372 /* Load the vertex and pixel samplers now. The function that finds the mappings makes sure
3373 * that it stays the same for each vertexshader-pixelshader pair(=linked glsl program). If
3374 * a pshader with fixed function pipeline is used there are no vertex samplers, and if a
3375 * vertex shader with fixed function pixel processing is used we make sure that the card
3376 * supports enough samplers to allow the max number of vertex samplers with all possible
3377 * fixed function fragment processing setups. So once the program is linked these samplers
3378 * won't change.
3380 if(vshader_id) {
3381 /* Load vertex shader samplers */
3382 shader_glsl_load_vsamplers(gl_info, (IWineD3DStateBlock*)This->stateBlock, programId);
3384 if(pshader_id) {
3385 /* Load pixel shader samplers */
3386 shader_glsl_load_psamplers(gl_info, (IWineD3DStateBlock*)This->stateBlock, programId);
3389 /* If the local constants do not have to be loaded with the environment constants,
3390 * load them now to have them hardcoded in the GLSL program. This saves some CPU cycles
3391 * later
3393 if(pshader && !((IWineD3DPixelShaderImpl*)pshader)->baseShader.load_local_constsF) {
3394 hardcode_local_constants((IWineD3DBaseShaderImpl *) pshader, gl_info, programId, 'P');
3396 if(vshader && !((IWineD3DVertexShaderImpl*)vshader)->baseShader.load_local_constsF) {
3397 hardcode_local_constants((IWineD3DBaseShaderImpl *) vshader, gl_info, programId, 'V');
3401 static GLhandleARB create_glsl_blt_shader(const WineD3D_GL_Info *gl_info, enum tex_types tex_type)
3403 GLhandleARB program_id;
3404 GLhandleARB vshader_id, pshader_id;
3405 const char *blt_vshader[] = {
3406 "#version 120\n"
3407 "void main(void)\n"
3408 "{\n"
3409 " gl_Position = gl_Vertex;\n"
3410 " gl_FrontColor = vec4(1.0);\n"
3411 " gl_TexCoord[0] = gl_MultiTexCoord0;\n"
3412 "}\n"
3415 const char *blt_pshaders[tex_type_count] = {
3416 /* tex_1d */
3417 NULL,
3418 /* tex_2d */
3419 "#version 120\n"
3420 "uniform sampler2D sampler;\n"
3421 "void main(void)\n"
3422 "{\n"
3423 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
3424 "}\n",
3425 /* tex_3d */
3426 NULL,
3427 /* tex_cube */
3428 "#version 120\n"
3429 "uniform samplerCube sampler;\n"
3430 "void main(void)\n"
3431 "{\n"
3432 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
3433 "}\n",
3434 /* tex_rect */
3435 "#version 120\n"
3436 "#extension GL_ARB_texture_rectangle : enable\n"
3437 "uniform sampler2DRect sampler;\n"
3438 "void main(void)\n"
3439 "{\n"
3440 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
3441 "}\n",
3444 if (!blt_pshaders[tex_type])
3446 FIXME("tex_type %#x not supported\n", tex_type);
3447 tex_type = tex_2d;
3450 vshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
3451 GL_EXTCALL(glShaderSourceARB(vshader_id, 1, blt_vshader, NULL));
3452 GL_EXTCALL(glCompileShaderARB(vshader_id));
3454 pshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
3455 GL_EXTCALL(glShaderSourceARB(pshader_id, 1, &blt_pshaders[tex_type], NULL));
3456 GL_EXTCALL(glCompileShaderARB(pshader_id));
3458 program_id = GL_EXTCALL(glCreateProgramObjectARB());
3459 GL_EXTCALL(glAttachObjectARB(program_id, vshader_id));
3460 GL_EXTCALL(glAttachObjectARB(program_id, pshader_id));
3461 GL_EXTCALL(glLinkProgramARB(program_id));
3463 print_glsl_info_log(&GLINFO_LOCATION, program_id);
3465 /* Once linked we can mark the shaders for deletion. They will be deleted once the program
3466 * is destroyed
3468 GL_EXTCALL(glDeleteObjectARB(vshader_id));
3469 GL_EXTCALL(glDeleteObjectARB(pshader_id));
3470 return program_id;
3473 static void shader_glsl_select(IWineD3DDevice *iface, BOOL usePS, BOOL useVS) {
3474 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
3475 struct shader_glsl_priv *priv = (struct shader_glsl_priv *)This->shader_priv;
3476 const WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
3477 GLhandleARB program_id = 0;
3478 GLenum old_vertex_color_clamp, current_vertex_color_clamp;
3480 old_vertex_color_clamp = priv->glsl_program ? priv->glsl_program->vertex_color_clamp : GL_FIXED_ONLY_ARB;
3482 if (useVS || usePS) set_glsl_shader_program(iface, usePS, useVS);
3483 else priv->glsl_program = NULL;
3485 current_vertex_color_clamp = priv->glsl_program ? priv->glsl_program->vertex_color_clamp : GL_FIXED_ONLY_ARB;
3487 if (old_vertex_color_clamp != current_vertex_color_clamp) {
3488 if (GL_SUPPORT(ARB_COLOR_BUFFER_FLOAT)) {
3489 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, current_vertex_color_clamp));
3490 checkGLcall("glClampColorARB");
3491 } else {
3492 FIXME("vertex color clamp needs to be changed, but extension not supported.\n");
3496 program_id = priv->glsl_program ? priv->glsl_program->programId : 0;
3497 if (program_id) TRACE("Using GLSL program %u\n", program_id);
3498 GL_EXTCALL(glUseProgramObjectARB(program_id));
3499 checkGLcall("glUseProgramObjectARB");
3502 static void shader_glsl_select_depth_blt(IWineD3DDevice *iface, enum tex_types tex_type) {
3503 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
3504 const WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
3505 struct shader_glsl_priv *priv = (struct shader_glsl_priv *) This->shader_priv;
3506 GLhandleARB *blt_program = &priv->depth_blt_program[tex_type];
3508 if (!*blt_program) {
3509 GLhandleARB loc;
3510 *blt_program = create_glsl_blt_shader(gl_info, tex_type);
3511 loc = GL_EXTCALL(glGetUniformLocationARB(*blt_program, "sampler"));
3512 GL_EXTCALL(glUseProgramObjectARB(*blt_program));
3513 GL_EXTCALL(glUniform1iARB(loc, 0));
3514 } else {
3515 GL_EXTCALL(glUseProgramObjectARB(*blt_program));
3519 static void shader_glsl_deselect_depth_blt(IWineD3DDevice *iface) {
3520 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
3521 const WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
3522 struct shader_glsl_priv *priv = (struct shader_glsl_priv *) This->shader_priv;
3523 GLhandleARB program_id;
3525 program_id = priv->glsl_program ? priv->glsl_program->programId : 0;
3526 if (program_id) TRACE("Using GLSL program %u\n", program_id);
3528 GL_EXTCALL(glUseProgramObjectARB(program_id));
3529 checkGLcall("glUseProgramObjectARB");
3532 static void shader_glsl_cleanup(IWineD3DDevice *iface) {
3533 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
3534 const WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
3535 GL_EXTCALL(glUseProgramObjectARB(0));
3538 static void shader_glsl_destroy(IWineD3DBaseShader *iface) {
3539 const struct list *linked_programs;
3540 IWineD3DBaseShaderImpl *This = (IWineD3DBaseShaderImpl *) iface;
3541 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *)This->baseShader.device;
3542 struct shader_glsl_priv *priv = (struct shader_glsl_priv *)device->shader_priv;
3543 const WineD3D_GL_Info *gl_info = &device->adapter->gl_info;
3544 IWineD3DPixelShaderImpl *ps = NULL;
3545 IWineD3DVertexShaderImpl *vs = NULL;
3547 /* Note: Do not use QueryInterface here to find out which shader type this is because this code
3548 * can be called from IWineD3DBaseShader::Release
3550 char pshader = shader_is_pshader_version(This->baseShader.hex_version);
3552 if(pshader) {
3553 ps = (IWineD3DPixelShaderImpl *) This;
3554 if(ps->num_gl_shaders == 0) return;
3555 } else {
3556 vs = (IWineD3DVertexShaderImpl *) This;
3557 if(vs->prgId == 0) return;
3560 linked_programs = &This->baseShader.linked_programs;
3562 TRACE("Deleting linked programs\n");
3563 if (linked_programs->next) {
3564 struct glsl_shader_prog_link *entry, *entry2;
3566 if(pshader) {
3567 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs, struct glsl_shader_prog_link, pshader_entry) {
3568 delete_glsl_program_entry(priv, gl_info, entry);
3570 } else {
3571 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs, struct glsl_shader_prog_link, vshader_entry) {
3572 delete_glsl_program_entry(priv, gl_info, entry);
3577 if(pshader) {
3578 UINT i;
3580 ENTER_GL();
3581 for(i = 0; i < ps->num_gl_shaders; i++) {
3582 TRACE("deleting pshader %u\n", ps->gl_shaders[i].prgId);
3583 GL_EXTCALL(glDeleteObjectARB(ps->gl_shaders[i].prgId));
3584 checkGLcall("glDeleteObjectARB");
3586 LEAVE_GL();
3587 HeapFree(GetProcessHeap(), 0, ps->gl_shaders);
3588 ps->gl_shaders = NULL;
3589 ps->num_gl_shaders = 0;
3590 } else {
3591 TRACE("Deleting shader object %u\n", vs->prgId);
3592 ENTER_GL();
3593 GL_EXTCALL(glDeleteObjectARB(vs->prgId));
3594 checkGLcall("glDeleteObjectARB");
3595 LEAVE_GL();
3596 vs->prgId = 0;
3597 vs->baseShader.is_compiled = FALSE;
3601 static unsigned int glsl_program_key_hash(const void *key)
3603 const glsl_program_key_t *k = (const glsl_program_key_t *)key;
3605 unsigned int hash = k->vshader | ((DWORD_PTR) k->pshader) << 16;
3606 hash += ~(hash << 15);
3607 hash ^= (hash >> 10);
3608 hash += (hash << 3);
3609 hash ^= (hash >> 6);
3610 hash += ~(hash << 11);
3611 hash ^= (hash >> 16);
3613 return hash;
3616 static BOOL glsl_program_key_compare(const void *keya, const void *keyb)
3618 const glsl_program_key_t *ka = (const glsl_program_key_t *)keya;
3619 const glsl_program_key_t *kb = (const glsl_program_key_t *)keyb;
3621 return ka->vshader == kb->vshader && ka->pshader == kb->pshader &&
3622 (memcmp(&ka->ps_args, &kb->ps_args, sizeof(kb->ps_args)) == 0);
3625 static HRESULT shader_glsl_alloc(IWineD3DDevice *iface) {
3626 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
3627 struct shader_glsl_priv *priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct shader_glsl_priv));
3628 priv->glsl_program_lookup = hash_table_create(glsl_program_key_hash, glsl_program_key_compare);
3629 This->shader_priv = priv;
3630 return WINED3D_OK;
3633 static void shader_glsl_free(IWineD3DDevice *iface) {
3634 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
3635 const WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
3636 struct shader_glsl_priv *priv = (struct shader_glsl_priv *)This->shader_priv;
3637 int i;
3639 for (i = 0; i < tex_type_count; ++i)
3641 if (priv->depth_blt_program[i])
3643 GL_EXTCALL(glDeleteObjectARB(priv->depth_blt_program[i]));
3647 hash_table_destroy(priv->glsl_program_lookup, NULL, NULL);
3649 HeapFree(GetProcessHeap(), 0, This->shader_priv);
3650 This->shader_priv = NULL;
3653 static BOOL shader_glsl_dirty_const(IWineD3DDevice *iface) {
3654 /* TODO: GL_EXT_bindable_uniform can be used to share constants across shaders */
3655 return FALSE;
3658 static GLuint shader_glsl_generate_pshader(IWineD3DPixelShader *iface, SHADER_BUFFER *buffer) {
3659 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)iface;
3660 const struct shader_reg_maps *reg_maps = &This->baseShader.reg_maps;
3661 CONST DWORD *function = This->baseShader.function;
3662 const char *fragcolor;
3663 const WineD3D_GL_Info *gl_info = &((IWineD3DDeviceImpl *)This->baseShader.device)->adapter->gl_info;
3665 /* Create the hw GLSL shader object and assign it as the shader->prgId */
3666 GLhandleARB shader_obj = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
3668 shader_addline(buffer, "#version 120\n");
3670 if (GL_SUPPORT(ARB_DRAW_BUFFERS)) {
3671 shader_addline(buffer, "#extension GL_ARB_draw_buffers : enable\n");
3673 if (GL_SUPPORT(ARB_TEXTURE_RECTANGLE)) {
3674 /* The spec says that it doesn't have to be explicitly enabled, but the nvidia
3675 * drivers write a warning if we don't do so
3677 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
3680 /* Base Declarations */
3681 shader_generate_glsl_declarations( (IWineD3DBaseShader*) This, reg_maps, buffer, &GLINFO_LOCATION);
3683 /* Pack 3.0 inputs */
3684 if (This->baseShader.hex_version >= WINED3DPS_VERSION(3,0)) {
3686 if(((IWineD3DDeviceImpl *) This->baseShader.device)->strided_streams.u.s.position_transformed) {
3687 pshader_glsl_input_pack(buffer, This->semantics_in, iface, pretransformed);
3688 } else if(!use_vs((IWineD3DDeviceImpl *) This->baseShader.device)) {
3689 pshader_glsl_input_pack(buffer, This->semantics_in, iface, fixedfunction);
3693 /* Base Shader Body */
3694 shader_generate_main( (IWineD3DBaseShader*) This, buffer, reg_maps, function);
3696 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly */
3697 if (This->baseShader.hex_version < WINED3DPS_VERSION(2,0)) {
3698 /* Some older cards like GeforceFX ones don't support multiple buffers, so also not gl_FragData */
3699 if(GL_SUPPORT(ARB_DRAW_BUFFERS))
3700 shader_addline(buffer, "gl_FragData[0] = R0;\n");
3701 else
3702 shader_addline(buffer, "gl_FragColor = R0;\n");
3705 if(GL_SUPPORT(ARB_DRAW_BUFFERS)) {
3706 fragcolor = "gl_FragData[0]";
3707 } else {
3708 fragcolor = "gl_FragColor";
3710 if(((IWineD3DDeviceImpl *)This->baseShader.device)->stateBlock->renderState[WINED3DRS_SRGBWRITEENABLE]) {
3711 shader_addline(buffer, "tmp0.xyz = pow(%s.xyz, vec3(%f, %f, %f)) * vec3(%f, %f, %f) - vec3(%f, %f, %f);\n",
3712 fragcolor, srgb_pow, srgb_pow, srgb_pow, srgb_mul_high, srgb_mul_high, srgb_mul_high,
3713 srgb_sub_high, srgb_sub_high, srgb_sub_high);
3714 shader_addline(buffer, "tmp1.xyz = %s.xyz * srgb_mul_low.xyz;\n", fragcolor);
3715 shader_addline(buffer, "%s.x = %s.x < srgb_comparison.x ? tmp1.x : tmp0.x;\n", fragcolor, fragcolor);
3716 shader_addline(buffer, "%s.y = %s.y < srgb_comparison.y ? tmp1.y : tmp0.y;\n", fragcolor, fragcolor);
3717 shader_addline(buffer, "%s.z = %s.z < srgb_comparison.z ? tmp1.z : tmp0.z;\n", fragcolor, fragcolor);
3718 shader_addline(buffer, "%s = clamp(%s, 0.0, 1.0);\n", fragcolor, fragcolor);
3720 /* Pixel shader < 3.0 do not replace the fog stage.
3721 * This implements linear fog computation and blending.
3722 * TODO: non linear fog
3723 * NOTE: gl_Fog.start and gl_Fog.end don't hold fog start s and end e but
3724 * -1/(e-s) and e/(e-s) respectively.
3726 if(This->baseShader.hex_version < WINED3DPS_VERSION(3,0)) {
3727 shader_addline(buffer, "float Fog = clamp(gl_FogFragCoord * gl_Fog.start + gl_Fog.end, 0.0, 1.0);\n");
3728 shader_addline(buffer, "%s.xyz = mix(gl_Fog.color.xyz, %s.xyz, Fog);\n", fragcolor, fragcolor);
3731 shader_addline(buffer, "}\n");
3733 TRACE("Compiling shader object %u\n", shader_obj);
3734 GL_EXTCALL(glShaderSourceARB(shader_obj, 1, (const char**)&buffer->buffer, NULL));
3735 GL_EXTCALL(glCompileShaderARB(shader_obj));
3736 print_glsl_info_log(&GLINFO_LOCATION, shader_obj);
3738 /* Store the shader object */
3739 return shader_obj;
3742 static void shader_glsl_generate_vshader(IWineD3DVertexShader *iface, SHADER_BUFFER *buffer) {
3743 IWineD3DVertexShaderImpl *This = (IWineD3DVertexShaderImpl *)iface;
3744 const struct shader_reg_maps *reg_maps = &This->baseShader.reg_maps;
3745 CONST DWORD *function = This->baseShader.function;
3746 const WineD3D_GL_Info *gl_info = &((IWineD3DDeviceImpl *)This->baseShader.device)->adapter->gl_info;
3748 /* Create the hw GLSL shader program and assign it as the shader->prgId */
3749 GLhandleARB shader_obj = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
3751 shader_addline(buffer, "#version 120\n");
3753 /* Base Declarations */
3754 shader_generate_glsl_declarations( (IWineD3DBaseShader*) This, reg_maps, buffer, &GLINFO_LOCATION);
3756 /* Base Shader Body */
3757 shader_generate_main( (IWineD3DBaseShader*) This, buffer, reg_maps, function);
3759 /* Unpack 3.0 outputs */
3760 if (This->baseShader.hex_version >= WINED3DVS_VERSION(3,0)) {
3761 shader_addline(buffer, "order_ps_input(OUT);\n");
3762 } else {
3763 shader_addline(buffer, "order_ps_input();\n");
3766 /* If this shader doesn't use fog copy the z coord to the fog coord so that we can use table fog */
3767 if (!reg_maps->fog)
3768 shader_addline(buffer, "gl_FogFragCoord = gl_Position.z;\n");
3770 /* Write the final position.
3772 * OpenGL coordinates specify the center of the pixel while d3d coords specify
3773 * the corner. The offsets are stored in z and w in posFixup. posFixup.y contains
3774 * 1.0 or -1.0 to turn the rendering upside down for offscreen rendering. PosFixup.x
3775 * contains 1.0 to allow a mad.
3777 shader_addline(buffer, "gl_Position.y = gl_Position.y * posFixup.y;\n");
3778 shader_addline(buffer, "gl_Position.xy += posFixup.zw * gl_Position.ww;\n");
3780 /* Z coord [0;1]->[-1;1] mapping, see comment in transform_projection in state.c
3782 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However, shaders are run
3783 * before the homogeneous divide, so we have to take the w into account: z = ((z / w) * 2 - 1) * w,
3784 * which is the same as z = z * 2 - w.
3786 shader_addline(buffer, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
3788 shader_addline(buffer, "}\n");
3790 TRACE("Compiling shader object %u\n", shader_obj);
3791 GL_EXTCALL(glShaderSourceARB(shader_obj, 1, (const char**)&buffer->buffer, NULL));
3792 GL_EXTCALL(glCompileShaderARB(shader_obj));
3793 print_glsl_info_log(&GLINFO_LOCATION, shader_obj);
3795 /* Store the shader object */
3796 This->prgId = shader_obj;
3799 static void shader_glsl_get_caps(WINED3DDEVTYPE devtype, const WineD3D_GL_Info *gl_info, struct shader_caps *pCaps)
3801 /* Nvidia Geforce6/7 or Ati R4xx/R5xx cards with GLSL support, support VS 3.0 but older Nvidia/Ati
3802 * models with GLSL support only support 2.0. In case of nvidia we can detect VS 2.0 support using
3803 * vs_nv_version which is based on NV_vertex_program.
3804 * For Ati cards there's no way using glsl (it abstracts the lowlevel info away) and also not
3805 * using ARB_vertex_program. It is safe to assume that when a card supports pixel shader 2.0 it
3806 * supports vertex shader 2.0 too and the way around. We can detect ps2.0 using the maximum number
3807 * of native instructions, so use that here. For more info see the pixel shader versioning code below.
3809 if((GLINFO_LOCATION.vs_nv_version == VS_VERSION_20) || (GLINFO_LOCATION.ps_arb_max_instructions <= 512))
3810 pCaps->VertexShaderVersion = WINED3DVS_VERSION(2,0);
3811 else
3812 pCaps->VertexShaderVersion = WINED3DVS_VERSION(3,0);
3813 TRACE_(d3d_caps)("Hardware vertex shader version %d.%d enabled (GLSL)\n", (pCaps->VertexShaderVersion >> 8) & 0xff, pCaps->VertexShaderVersion & 0xff);
3814 pCaps->MaxVertexShaderConst = GL_LIMITS(vshader_constantsF);
3816 /* Older DX9-class videocards (GeforceFX / Radeon >9500/X*00) only support pixel shader 2.0/2.0a/2.0b.
3817 * In OpenGL the extensions related to GLSL abstract lowlevel GL info away which is needed
3818 * to distinguish between 2.0 and 3.0 (and 2.0a/2.0b). In case of Nvidia we use their fragment
3819 * program extensions. On other hardware including ATI GL_ARB_fragment_program offers the info
3820 * in max native instructions. Intel and others also offer the info in this extension but they
3821 * don't support GLSL (at least on Windows).
3823 * PS2.0 requires at least 96 instructions, 2.0a/2.0b go up to 512. Assume that if the number
3824 * of instructions is 512 or less we have to do with ps2.0 hardware.
3825 * 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.
3827 if((GLINFO_LOCATION.ps_nv_version == PS_VERSION_20) || (GLINFO_LOCATION.ps_arb_max_instructions <= 512))
3828 pCaps->PixelShaderVersion = WINED3DPS_VERSION(2,0);
3829 else
3830 pCaps->PixelShaderVersion = WINED3DPS_VERSION(3,0);
3832 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
3833 * Direct3D minimum requirement.
3835 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
3836 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
3838 * The problem is that the refrast clamps temporary results in the shader to
3839 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
3840 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
3841 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
3842 * offer a way to query this.
3844 pCaps->PixelShader1xMaxValue = 8.0;
3845 TRACE_(d3d_caps)("Hardware pixel shader version %d.%d enabled (GLSL)\n", (pCaps->PixelShaderVersion >> 8) & 0xff, pCaps->PixelShaderVersion & 0xff);
3848 static BOOL shader_glsl_conv_supported(WINED3DFORMAT fmt) {
3849 TRACE("Checking shader format support for format %s:", debug_d3dformat(fmt));
3850 switch(fmt) {
3851 case WINED3DFMT_V8U8:
3852 case WINED3DFMT_V16U16:
3853 case WINED3DFMT_X8L8V8U8:
3854 case WINED3DFMT_L6V5U5:
3855 case WINED3DFMT_Q8W8V8U8:
3856 case WINED3DFMT_ATI2N:
3857 TRACE("[OK]\n");
3858 return TRUE;
3859 default:
3860 TRACE("[FAILED\n");
3861 return FALSE;
3865 static const SHADER_HANDLER shader_glsl_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
3867 /* WINED3DSIH_ABS */ shader_glsl_map2gl,
3868 /* WINED3DSIH_ADD */ shader_glsl_arith,
3869 /* WINED3DSIH_BEM */ pshader_glsl_bem,
3870 /* WINED3DSIH_BREAK */ shader_glsl_break,
3871 /* WINED3DSIH_BREAKC */ shader_glsl_breakc,
3872 /* WINED3DSIH_BREAKP */ NULL,
3873 /* WINED3DSIH_CALL */ shader_glsl_call,
3874 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz,
3875 /* WINED3DSIH_CMP */ shader_glsl_cmp,
3876 /* WINED3DSIH_CND */ shader_glsl_cnd,
3877 /* WINED3DSIH_CRS */ shader_glsl_cross,
3878 /* WINED3DSIH_DCL */ NULL,
3879 /* WINED3DSIH_DEF */ NULL,
3880 /* WINED3DSIH_DEFB */ NULL,
3881 /* WINED3DSIH_DEFI */ NULL,
3882 /* WINED3DSIH_DP2ADD */ pshader_glsl_dp2add,
3883 /* WINED3DSIH_DP3 */ shader_glsl_dot,
3884 /* WINED3DSIH_DP4 */ shader_glsl_dot,
3885 /* WINED3DSIH_DST */ shader_glsl_dst,
3886 /* WINED3DSIH_DSX */ shader_glsl_map2gl,
3887 /* WINED3DSIH_DSY */ shader_glsl_map2gl,
3888 /* WINED3DSIH_ELSE */ shader_glsl_else,
3889 /* WINED3DSIH_ENDIF */ shader_glsl_end,
3890 /* WINED3DSIH_ENDLOOP */ shader_glsl_end,
3891 /* WINED3DSIH_ENDREP */ shader_glsl_end,
3892 /* WINED3DSIH_EXP */ shader_glsl_map2gl,
3893 /* WINED3DSIH_EXPP */ shader_glsl_expp,
3894 /* WINED3DSIH_FRC */ shader_glsl_map2gl,
3895 /* WINED3DSIH_IF */ shader_glsl_if,
3896 /* WINED3DSIH_IFC */ shader_glsl_ifc,
3897 /* WINED3DSIH_LABEL */ shader_glsl_label,
3898 /* WINED3DSIH_LIT */ shader_glsl_lit,
3899 /* WINED3DSIH_LOG */ shader_glsl_log,
3900 /* WINED3DSIH_LOGP */ shader_glsl_log,
3901 /* WINED3DSIH_LOOP */ shader_glsl_loop,
3902 /* WINED3DSIH_LRP */ shader_glsl_lrp,
3903 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn,
3904 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn,
3905 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn,
3906 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn,
3907 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn,
3908 /* WINED3DSIH_MAD */ shader_glsl_mad,
3909 /* WINED3DSIH_MAX */ shader_glsl_map2gl,
3910 /* WINED3DSIH_MIN */ shader_glsl_map2gl,
3911 /* WINED3DSIH_MOV */ shader_glsl_mov,
3912 /* WINED3DSIH_MOVA */ shader_glsl_mov,
3913 /* WINED3DSIH_MUL */ shader_glsl_arith,
3914 /* WINED3DSIH_NOP */ NULL,
3915 /* WINED3DSIH_NRM */ shader_glsl_map2gl,
3916 /* WINED3DSIH_PHASE */ NULL,
3917 /* WINED3DSIH_POW */ shader_glsl_pow,
3918 /* WINED3DSIH_RCP */ shader_glsl_rcp,
3919 /* WINED3DSIH_REP */ shader_glsl_rep,
3920 /* WINED3DSIH_RET */ NULL,
3921 /* WINED3DSIH_RSQ */ shader_glsl_rsq,
3922 /* WINED3DSIH_SETP */ NULL,
3923 /* WINED3DSIH_SGE */ shader_glsl_compare,
3924 /* WINED3DSIH_SGN */ shader_glsl_map2gl,
3925 /* WINED3DSIH_SINCOS */ shader_glsl_sincos,
3926 /* WINED3DSIH_SLT */ shader_glsl_compare,
3927 /* WINED3DSIH_SUB */ shader_glsl_arith,
3928 /* WINED3DSIH_TEX */ pshader_glsl_tex,
3929 /* WINED3DSIH_TEXBEM */ pshader_glsl_texbem,
3930 /* WINED3DSIH_TEXBEML */ pshader_glsl_texbem,
3931 /* WINED3DSIH_TEXCOORD */ pshader_glsl_texcoord,
3932 /* WINED3DSIH_TEXDEPTH */ pshader_glsl_texdepth,
3933 /* WINED3DSIH_TEXDP3 */ pshader_glsl_texdp3,
3934 /* WINED3DSIH_TEXDP3TEX */ pshader_glsl_texdp3tex,
3935 /* WINED3DSIH_TEXKILL */ pshader_glsl_texkill,
3936 /* WINED3DSIH_TEXLDD */ NULL,
3937 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl,
3938 /* WINED3DSIH_TEXM3x2DEPTH */ pshader_glsl_texm3x2depth,
3939 /* WINED3DSIH_TEXM3x2PAD */ pshader_glsl_texm3x2pad,
3940 /* WINED3DSIH_TEXM3x2TEX */ pshader_glsl_texm3x2tex,
3941 /* WINED3DSIH_TEXM3x3 */ pshader_glsl_texm3x3,
3942 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
3943 /* WINED3DSIH_TEXM3x3PAD */ pshader_glsl_texm3x3pad,
3944 /* WINED3DSIH_TEXM3x3SPEC */ pshader_glsl_texm3x3spec,
3945 /* WINED3DSIH_TEXM3x3TEX */ pshader_glsl_texm3x3tex,
3946 /* WINED3DSIH_TEXM3x3VSPEC */ pshader_glsl_texm3x3vspec,
3947 /* WINED3DSIH_TEXREG2AR */ pshader_glsl_texreg2ar,
3948 /* WINED3DSIH_TEXREG2GB */ pshader_glsl_texreg2gb,
3949 /* WINED3DSIH_TEXREG2RGB */ pshader_glsl_texreg2rgb,
3952 const shader_backend_t glsl_shader_backend = {
3953 shader_glsl_instruction_handler_table,
3954 shader_glsl_select,
3955 shader_glsl_select_depth_blt,
3956 shader_glsl_deselect_depth_blt,
3957 shader_glsl_load_constants,
3958 shader_glsl_cleanup,
3959 shader_glsl_color_correction,
3960 shader_glsl_destroy,
3961 shader_glsl_alloc,
3962 shader_glsl_free,
3963 shader_glsl_dirty_const,
3964 shader_glsl_generate_pshader,
3965 shader_glsl_generate_vshader,
3966 shader_glsl_get_caps,
3967 shader_glsl_conv_supported,