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.
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)
54 } glsl_sample_function_t;
56 /** Prints the GLSL info log which will contain error messages if they exist */
57 void print_glsl_info_log(WineD3D_GL_Info *gl_info, GLhandleARB obj) {
59 int infologLength = 0;
64 const char *spam[] = {
65 "Vertex shader was successfully compiled to run on hardware.\n", /* fglrx */
66 "Fragment shader was successfully compiled to run on hardware.\n", /* fglrx */
67 "Fragment shader(s) linked, vertex shader(s) linked. \n ", /* fglrx, with \n */
68 "Fragment shader(s) linked, vertex shader(s) linked.", /* fglrx, no \n */
69 "Vertex shader(s) linked, no fragment shader(s) defined. \n ", /* fglrx, with \n */
70 "Vertex shader(s) linked, no fragment shader(s) defined.", /* fglrx, no \n */
71 "Fragment shader was successfully compiled to run on hardware.\nWARNING: 0:1: extension 'GL_ARB_draw_buffers' is not supported",
72 "Fragment shader(s) linked, no vertex shader(s) defined.", /* fglrx, no \n */
73 "Fragment shader(s) linked, no vertex shader(s) defined. \n " /* fglrx, with \n */
76 GL_EXTCALL(glGetObjectParameterivARB(obj,
77 GL_OBJECT_INFO_LOG_LENGTH_ARB,
80 /* A size of 1 is just a null-terminated string, so the log should be bigger than
81 * that if there are errors. */
82 if (infologLength > 1)
84 /* Fglrx doesn't terminate the string properly, but it tells us the proper length.
85 * So use HEAP_ZERO_MEMORY to avoid uninitialized bytes
87 infoLog = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, infologLength);
88 GL_EXTCALL(glGetInfoLogARB(obj, infologLength, NULL, infoLog));
91 for(i = 0; i < sizeof(spam) / sizeof(spam[0]); i++) {
92 if(strcmp(infoLog, spam[i]) == 0) {
98 TRACE("Spam received from GLSL shader #%u: %s\n", obj, debugstr_a(infoLog));
100 FIXME("Error received from GLSL shader #%u: %s\n", obj, debugstr_a(infoLog));
102 HeapFree(GetProcessHeap(), 0, infoLog);
107 * Loads (pixel shader) samplers
109 static void shader_glsl_load_psamplers(
110 WineD3D_GL_Info *gl_info,
111 IWineD3DStateBlock* iface,
112 GLhandleARB programId) {
114 IWineD3DStateBlockImpl* stateBlock = (IWineD3DStateBlockImpl*) iface;
115 GLhandleARB name_loc;
117 char sampler_name[20];
119 for (i = 0; i < MAX_FRAGMENT_SAMPLERS; ++i) {
120 snprintf(sampler_name, sizeof(sampler_name), "Psampler%d", i);
121 name_loc = GL_EXTCALL(glGetUniformLocationARB(programId, sampler_name));
122 if (name_loc != -1) {
123 int mapped_unit = stateBlock->wineD3DDevice->texUnitMap[i];
124 if (mapped_unit != -1 && mapped_unit < GL_LIMITS(fragment_samplers)) {
125 TRACE("Loading %s for texture %d\n", sampler_name, mapped_unit);
126 GL_EXTCALL(glUniform1iARB(name_loc, mapped_unit));
127 checkGLcall("glUniform1iARB");
129 ERR("Trying to load sampler %s on unsupported unit %d\n", sampler_name, mapped_unit);
135 static void shader_glsl_load_vsamplers(WineD3D_GL_Info *gl_info, IWineD3DStateBlock* iface, GLhandleARB programId) {
136 IWineD3DStateBlockImpl* stateBlock = (IWineD3DStateBlockImpl*) iface;
137 GLhandleARB name_loc;
138 char sampler_name[20];
141 for (i = 0; i < MAX_VERTEX_SAMPLERS; ++i) {
142 snprintf(sampler_name, sizeof(sampler_name), "Vsampler%d", i);
143 name_loc = GL_EXTCALL(glGetUniformLocationARB(programId, sampler_name));
144 if (name_loc != -1) {
145 int mapped_unit = stateBlock->wineD3DDevice->texUnitMap[MAX_FRAGMENT_SAMPLERS + i];
146 if (mapped_unit != -1 && mapped_unit < GL_LIMITS(combined_samplers)) {
147 TRACE("Loading %s for texture %d\n", sampler_name, mapped_unit);
148 GL_EXTCALL(glUniform1iARB(name_loc, mapped_unit));
149 checkGLcall("glUniform1iARB");
151 ERR("Trying to load sampler %s on unsupported unit %d\n", sampler_name, mapped_unit);
158 * Loads floating point constants (aka uniforms) into the currently set GLSL program.
159 * When constant_list == NULL, it will load all the constants.
161 static void shader_glsl_load_constantsF(IWineD3DBaseShaderImpl* This, WineD3D_GL_Info *gl_info,
162 unsigned int max_constants, float* constants, GLhandleARB *constant_locations,
163 struct list *constant_list) {
164 constants_entry *constant;
165 local_constant* lconst;
170 if (TRACE_ON(d3d_shader)) {
171 LIST_FOR_EACH_ENTRY(constant, constant_list, constants_entry, entry) {
176 tmp_loc = constant_locations[i];
178 TRACE_(d3d_constants)("Loading constants %i: %f, %f, %f, %f\n", i,
179 constants[i * 4 + 0], constants[i * 4 + 1],
180 constants[i * 4 + 2], constants[i * 4 + 3]);
186 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
187 if(WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) == 1 &&
188 shader_is_pshader_version(This->baseShader.hex_version)) {
191 LIST_FOR_EACH_ENTRY(constant, constant_list, constants_entry, entry) {
196 tmp_loc = constant_locations[i];
198 /* We found this uniform name in the program - go ahead and send the data */
200 if(constants[k + 0] < -1.0) lcl_const[0] = -1.0;
201 else if(constants[k + 0] > 1.0) lcl_const[0] = 1.0;
202 else lcl_const[0] = constants[k + 0];
203 if(constants[k + 1] < -1.0) lcl_const[1] = -1.0;
204 else if(constants[k + 1] > 1.0) lcl_const[1] = 1.0;
205 else lcl_const[1] = constants[k + 1];
206 if(constants[k + 2] < -1.0) lcl_const[2] = -1.0;
207 else if(constants[k + 2] > 1.0) lcl_const[2] = 1.0;
208 else lcl_const[2] = constants[k + 2];
209 if(constants[k + 3] < -1.0) lcl_const[3] = -1.0;
210 else if(constants[k + 3] > 1.0) lcl_const[3] = 1.0;
211 else lcl_const[3] = constants[k + 3];
213 GL_EXTCALL(glUniform4fvARB(tmp_loc, 1, lcl_const));
218 LIST_FOR_EACH_ENTRY(constant, constant_list, constants_entry, entry) {
223 tmp_loc = constant_locations[i];
225 /* We found this uniform name in the program - go ahead and send the data */
226 GL_EXTCALL(glUniform4fvARB(tmp_loc, 1, constants + (i * 4)));
231 checkGLcall("glUniform4fvARB()");
233 if(!This->baseShader.load_local_constsF) {
234 TRACE("No need to load local float constants for this shader\n");
238 /* Load immediate constants */
239 if (TRACE_ON(d3d_shader)) {
240 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
241 tmp_loc = constant_locations[lconst->idx];
243 GLfloat* values = (GLfloat*)lconst->value;
244 TRACE_(d3d_constants)("Loading local constants %i: %f, %f, %f, %f\n", lconst->idx,
245 values[0], values[1], values[2], values[3]);
249 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
250 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
251 tmp_loc = constant_locations[lconst->idx];
253 /* We found this uniform name in the program - go ahead and send the data */
254 GL_EXTCALL(glUniform4fvARB(tmp_loc, 1, (GLfloat*)lconst->value));
257 checkGLcall("glUniform4fvARB()");
261 * Loads integer constants (aka uniforms) into the currently set GLSL program.
262 * When @constants_set == NULL, it will load all the constants.
264 static void shader_glsl_load_constantsI(
265 IWineD3DBaseShaderImpl* This,
266 WineD3D_GL_Info *gl_info,
267 GLhandleARB programId,
268 GLhandleARB locations[MAX_CONST_I],
269 unsigned max_constants,
271 BOOL* constants_set) {
276 for (i=0; i<max_constants; ++i) {
277 if (NULL == constants_set || constants_set[i]) {
279 TRACE_(d3d_constants)("Loading constants %i: %i, %i, %i, %i\n",
280 i, constants[i*4], constants[i*4+1], constants[i*4+2], constants[i*4+3]);
282 /* We found this uniform name in the program - go ahead and send the data */
283 GL_EXTCALL(glUniform4ivARB(locations[i], 1, &constants[i*4]));
284 checkGLcall("glUniform4ivARB");
288 /* Load immediate constants */
289 ptr = list_head(&This->baseShader.constantsI);
291 local_constant* lconst = LIST_ENTRY(ptr, struct local_constant, entry);
292 unsigned int idx = lconst->idx;
293 GLint* values = (GLint*) lconst->value;
295 TRACE_(d3d_constants)("Loading local constants %i: %i, %i, %i, %i\n", idx,
296 values[0], values[1], values[2], values[3]);
298 /* We found this uniform name in the program - go ahead and send the data */
299 GL_EXTCALL(glUniform4ivARB(locations[idx], 1, values));
300 checkGLcall("glUniform4ivARB");
301 ptr = list_next(&This->baseShader.constantsI, ptr);
306 * Loads boolean constants (aka uniforms) into the currently set GLSL program.
307 * When @constants_set == NULL, it will load all the constants.
309 static void shader_glsl_load_constantsB(
310 IWineD3DBaseShaderImpl* This,
311 WineD3D_GL_Info *gl_info,
312 GLhandleARB programId,
313 unsigned max_constants,
315 BOOL* constants_set) {
320 char is_pshader = shader_is_pshader_version(This->baseShader.hex_version);
321 const char* prefix = is_pshader? "PB":"VB";
324 for (i=0; i<max_constants; ++i) {
325 if (NULL == constants_set || constants_set[i]) {
327 TRACE_(d3d_constants)("Loading constants %i: %i;\n", i, constants[i]);
329 /* TODO: Benchmark and see if it would be beneficial to store the
330 * locations of the constants to avoid looking up each time */
331 snprintf(tmp_name, sizeof(tmp_name), "%s[%i]", prefix, i);
332 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
334 /* We found this uniform name in the program - go ahead and send the data */
335 GL_EXTCALL(glUniform1ivARB(tmp_loc, 1, &constants[i]));
336 checkGLcall("glUniform1ivARB");
341 /* Load immediate constants */
342 ptr = list_head(&This->baseShader.constantsB);
344 local_constant* lconst = LIST_ENTRY(ptr, struct local_constant, entry);
345 unsigned int idx = lconst->idx;
346 GLint* values = (GLint*) lconst->value;
348 TRACE_(d3d_constants)("Loading local constants %i: %i\n", idx, values[0]);
350 snprintf(tmp_name, sizeof(tmp_name), "%s[%i]", prefix, idx);
351 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
353 /* We found this uniform name in the program - go ahead and send the data */
354 GL_EXTCALL(glUniform1ivARB(tmp_loc, 1, values));
355 checkGLcall("glUniform1ivARB");
357 ptr = list_next(&This->baseShader.constantsB, ptr);
364 * Loads the app-supplied constants into the currently set GLSL program.
366 void shader_glsl_load_constants(
367 IWineD3DDevice* device,
369 char useVertexShader) {
371 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) device;
372 IWineD3DStateBlockImpl* stateBlock = deviceImpl->stateBlock;
373 WineD3D_GL_Info *gl_info = &deviceImpl->adapter->gl_info;
375 GLhandleARB *constant_locations;
376 struct list *constant_list;
377 GLhandleARB programId;
378 struct glsl_shader_prog_link *prog = stateBlock->glsl_program;
382 /* No GLSL program set - nothing to do. */
385 programId = prog->programId;
387 if (useVertexShader) {
388 IWineD3DBaseShaderImpl* vshader = (IWineD3DBaseShaderImpl*) stateBlock->vertexShader;
390 constant_locations = prog->vuniformF_locations;
391 constant_list = &stateBlock->set_vconstantsF;
393 /* Load DirectX 9 float constants/uniforms for vertex shader */
394 shader_glsl_load_constantsF(vshader, gl_info, GL_LIMITS(vshader_constantsF),
395 stateBlock->vertexShaderConstantF, constant_locations, constant_list);
397 /* Load DirectX 9 integer constants/uniforms for vertex shader */
398 shader_glsl_load_constantsI(vshader, gl_info, programId,
399 prog->vuniformI_locations, MAX_CONST_I,
400 stateBlock->vertexShaderConstantI,
401 stateBlock->changed.vertexShaderConstantsI);
403 /* Load DirectX 9 boolean constants/uniforms for vertex shader */
404 shader_glsl_load_constantsB(vshader, gl_info, programId, MAX_CONST_B,
405 stateBlock->vertexShaderConstantB,
406 stateBlock->changed.vertexShaderConstantsB);
408 /* Upload the position fixup params */
409 GL_EXTCALL(glUniform4fvARB(prog->posFixup_location, 1, &deviceImpl->posFixup[0]));
410 checkGLcall("glUniform4fvARB");
413 if (usePixelShader) {
415 IWineD3DBaseShaderImpl* pshader = (IWineD3DBaseShaderImpl*) stateBlock->pixelShader;
417 constant_locations = prog->puniformF_locations;
418 constant_list = &stateBlock->set_pconstantsF;
420 /* Load DirectX 9 float constants/uniforms for pixel shader */
421 shader_glsl_load_constantsF(pshader, gl_info, GL_LIMITS(pshader_constantsF),
422 stateBlock->pixelShaderConstantF, constant_locations, constant_list);
424 /* Load DirectX 9 integer constants/uniforms for pixel shader */
425 shader_glsl_load_constantsI(pshader, gl_info, programId,
426 prog->puniformI_locations, MAX_CONST_I,
427 stateBlock->pixelShaderConstantI,
428 stateBlock->changed.pixelShaderConstantsI);
430 /* Load DirectX 9 boolean constants/uniforms for pixel shader */
431 shader_glsl_load_constantsB(pshader, gl_info, programId, MAX_CONST_B,
432 stateBlock->pixelShaderConstantB,
433 stateBlock->changed.pixelShaderConstantsB);
435 /* Upload the environment bump map matrix if needed. The needsbumpmat member specifies the texture stage to load the matrix from.
436 * It can't be 0 for a valid texbem instruction.
438 for(i = 0; i < ((IWineD3DPixelShaderImpl *) pshader)->numbumpenvmatconsts; i++) {
439 IWineD3DPixelShaderImpl *ps = (IWineD3DPixelShaderImpl *) pshader;
440 int stage = ps->luminanceconst[i].texunit;
442 float *data = (float *) &stateBlock->textureState[(int) ps->bumpenvmatconst[i].texunit][WINED3DTSS_BUMPENVMAT00];
443 GL_EXTCALL(glUniformMatrix2fvARB(prog->bumpenvmat_location[i], 1, 0, data));
444 checkGLcall("glUniformMatrix2fvARB");
446 /* texbeml needs the luminance scale and offset too. If texbeml is used, needsbumpmat
447 * is set too, so we can check that in the needsbumpmat check
449 if(ps->baseShader.reg_maps.luminanceparams[stage]) {
450 GLfloat *scale = (GLfloat *) &stateBlock->textureState[stage][WINED3DTSS_BUMPENVLSCALE];
451 GLfloat *offset = (GLfloat *) &stateBlock->textureState[stage][WINED3DTSS_BUMPENVLOFFSET];
453 GL_EXTCALL(glUniform1fvARB(prog->luminancescale_location[i], 1, scale));
454 checkGLcall("glUniform1fvARB");
455 GL_EXTCALL(glUniform1fvARB(prog->luminanceoffset_location[i], 1, offset));
456 checkGLcall("glUniform1fvARB");
460 if(((IWineD3DPixelShaderImpl *) pshader)->srgb_enabled &&
461 !((IWineD3DPixelShaderImpl *) pshader)->srgb_mode_hardcoded) {
465 if(stateBlock->renderState[WINED3DRS_SRGBWRITEENABLE]) {
466 comparison[0] = srgb_cmp; comparison[1] = srgb_cmp;
467 comparison[2] = srgb_cmp; comparison[3] = srgb_cmp;
469 mul_low[0] = srgb_mul_low; mul_low[1] = srgb_mul_low;
470 mul_low[2] = srgb_mul_low; mul_low[3] = srgb_mul_low;
472 comparison[0] = 1.0 / 0.0; comparison[1] = 1.0 / 0.0;
473 comparison[2] = 1.0 / 0.0; comparison[3] = 1.0 / 0.0;
475 mul_low[0] = 1.0; mul_low[1] = 1.0;
476 mul_low[2] = 1.0; mul_low[3] = 1.0;
479 GL_EXTCALL(glUniform4fvARB(prog->srgb_comparison_location, 1, comparison));
480 GL_EXTCALL(glUniform4fvARB(prog->srgb_mul_low_location, 1, mul_low));
482 if(((IWineD3DPixelShaderImpl *) pshader)->vpos_uniform) {
483 float correction_params[4];
484 if(deviceImpl->render_offscreen) {
485 correction_params[0] = 0.0;
486 correction_params[1] = 1.0;
488 /* position is window relative, not viewport relative */
489 correction_params[0] = ((IWineD3DSurfaceImpl *) deviceImpl->render_targets[0])->currentDesc.Height;
490 correction_params[1] = -1.0;
492 GL_EXTCALL(glUniform4fvARB(prog->ycorrection_location, 1, correction_params));
497 /** Generate the variable & register declarations for the GLSL output target */
498 void shader_generate_glsl_declarations(
499 IWineD3DBaseShader *iface,
500 shader_reg_maps* reg_maps,
501 SHADER_BUFFER* buffer,
502 WineD3D_GL_Info* gl_info) {
504 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
505 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) This->baseShader.device;
507 unsigned int extra_constants_needed = 0;
508 local_constant* lconst;
510 /* There are some minor differences between pixel and vertex shaders */
511 char pshader = shader_is_pshader_version(This->baseShader.hex_version);
512 char prefix = pshader ? 'P' : 'V';
514 /* Prototype the subroutines */
515 for (i = 0; i < This->baseShader.limits.label; i++) {
516 if (reg_maps->labels[i])
517 shader_addline(buffer, "void subroutine%u();\n", i);
520 /* Declare the constants (aka uniforms) */
521 if (This->baseShader.limits.constant_float > 0) {
522 unsigned max_constantsF = min(This->baseShader.limits.constant_float,
523 (pshader ? GL_LIMITS(pshader_constantsF) : GL_LIMITS(vshader_constantsF)));
524 shader_addline(buffer, "uniform vec4 %cC[%u];\n", prefix, max_constantsF);
527 if (This->baseShader.limits.constant_int > 0)
528 shader_addline(buffer, "uniform ivec4 %cI[%u];\n", prefix, This->baseShader.limits.constant_int);
530 if (This->baseShader.limits.constant_bool > 0)
531 shader_addline(buffer, "uniform bool %cB[%u];\n", prefix, This->baseShader.limits.constant_bool);
534 shader_addline(buffer, "uniform vec4 posFixup;\n");
535 /* Predeclaration; This function is added at link time based on the pixel shader.
536 * VS 3.0 shaders have an array OUT[] the shader writes to, earlier versions don't have
537 * that. We know the input to the reorder function at vertex shader compile time, so
538 * we can deal with that. The reorder function for a 1.x and 2.x vertex shader can just
539 * read gl_FrontColor. The output depends on the pixel shader. The reorder function for a
540 * 1.x and 2.x pshader or for fixed function will write gl_FrontColor, and for a 3.0 shader
541 * it will write to the varying array. Here we depend on the shader optimizer on sorting that
542 * out. The nvidia driver only does that if the parameter is inout instead of out, hence the
545 if(This->baseShader.hex_version >= WINED3DVS_VERSION(3, 0)) {
546 shader_addline(buffer, "void order_ps_input(in vec4[%u]);\n", MAX_REG_OUTPUT);
548 shader_addline(buffer, "void order_ps_input();\n");
551 IWineD3DPixelShaderImpl *ps_impl = (IWineD3DPixelShaderImpl *) This;
553 ps_impl->numbumpenvmatconsts = 0;
554 for(i = 0; i < (sizeof(reg_maps->bumpmat) / sizeof(reg_maps->bumpmat[0])); i++) {
555 if(!reg_maps->bumpmat[i]) {
559 ps_impl->bumpenvmatconst[(int) ps_impl->numbumpenvmatconsts].texunit = i;
560 shader_addline(buffer, "uniform mat2 bumpenvmat%d;\n", i);
562 if(reg_maps->luminanceparams) {
563 ps_impl->luminanceconst[(int) ps_impl->numbumpenvmatconsts].texunit = i;
564 shader_addline(buffer, "uniform float luminancescale%d;\n", i);
565 shader_addline(buffer, "uniform float luminanceoffset%d;\n", i);
566 extra_constants_needed++;
568 ps_impl->luminanceconst[(int) ps_impl->numbumpenvmatconsts].texunit = -1;
571 extra_constants_needed++;
572 ps_impl->numbumpenvmatconsts++;
575 if(device->stateBlock->renderState[WINED3DRS_SRGBWRITEENABLE]) {
576 ps_impl->srgb_enabled = 1;
577 if(This->baseShader.limits.constant_float + extra_constants_needed + 1 < GL_LIMITS(pshader_constantsF)) {
578 shader_addline(buffer, "uniform vec4 srgb_mul_low;\n");
579 shader_addline(buffer, "uniform vec4 srgb_comparison;\n");
580 ps_impl->srgb_mode_hardcoded = 0;
581 extra_constants_needed++;
583 ps_impl->srgb_mode_hardcoded = 1;
584 shader_addline(buffer, "const vec4 srgb_mul_low = vec4(%f, %f, %f, %f);\n",
585 srgb_mul_low, srgb_mul_low, srgb_mul_low, srgb_mul_low);
586 shader_addline(buffer, "const vec4 srgb_comparison = vec4(%f, %f, %f, %f);\n",
587 srgb_cmp, srgb_cmp, srgb_cmp, srgb_cmp);
590 IWineD3DPixelShaderImpl *ps_impl = (IWineD3DPixelShaderImpl *) This;
592 /* Do not write any srgb fixup into the shader to save shader size and processing time.
593 * As a consequence, we can't toggle srgb write on without recompilation
595 ps_impl->srgb_enabled = 0;
596 ps_impl->srgb_mode_hardcoded = 1;
598 if(reg_maps->vpos || reg_maps->usesdsy) {
599 if(This->baseShader.limits.constant_float + extra_constants_needed + 1 < GL_LIMITS(pshader_constantsF)) {
600 shader_addline(buffer, "uniform vec4 ycorrection;\n");
601 ((IWineD3DPixelShaderImpl *) This)->vpos_uniform = 1;
602 extra_constants_needed++;
604 /* This happens because we do not have proper tracking of the constant registers that are
605 * actually used, only the max limit of the shader version
607 FIXME("Cannot find a free uniform for vpos correction params\n");
608 shader_addline(buffer, "const vec4 ycorrection = vec4(%f, %f, 0.0, 0.0);\n",
609 device->render_offscreen ? 0.0 : ((IWineD3DSurfaceImpl *) device->render_targets[0])->currentDesc.Height,
610 device->render_offscreen ? 1.0 : -1.0);
612 shader_addline(buffer, "vec4 vpos;\n");
616 /* Declare texture samplers */
617 for (i = 0; i < This->baseShader.limits.sampler; i++) {
618 if (reg_maps->samplers[i]) {
620 DWORD stype = reg_maps->samplers[i] & WINED3DSP_TEXTURETYPE_MASK;
624 shader_addline(buffer, "uniform sampler1D %csampler%u;\n", prefix, i);
627 if(device->stateBlock->textures[i] &&
628 IWineD3DBaseTexture_GetTextureDimensions(device->stateBlock->textures[i]) == GL_TEXTURE_RECTANGLE_ARB) {
629 shader_addline(buffer, "uniform sampler2DRect %csampler%u;\n", prefix, i);
631 shader_addline(buffer, "uniform sampler2D %csampler%u;\n", prefix, i);
634 case WINED3DSTT_CUBE:
635 shader_addline(buffer, "uniform samplerCube %csampler%u;\n", prefix, i);
637 case WINED3DSTT_VOLUME:
638 shader_addline(buffer, "uniform sampler3D %csampler%u;\n", prefix, i);
641 shader_addline(buffer, "uniform unsupported_sampler %csampler%u;\n", prefix, i);
642 FIXME("Unrecognized sampler type: %#x\n", stype);
648 /* Declare address variables */
649 for (i = 0; i < This->baseShader.limits.address; i++) {
650 if (reg_maps->address[i])
651 shader_addline(buffer, "ivec4 A%d;\n", i);
654 /* Declare texture coordinate temporaries and initialize them */
655 for (i = 0; i < This->baseShader.limits.texcoord; i++) {
656 if (reg_maps->texcoord[i])
657 shader_addline(buffer, "vec4 T%u = gl_TexCoord[%u];\n", i, i);
660 /* Declare input register varyings. Only pixel shader, vertex shaders have that declared in the
661 * helper function shader that is linked in at link time
663 if(pshader && This->baseShader.hex_version >= WINED3DPS_VERSION(3, 0)) {
665 shader_addline(buffer, "varying vec4 IN[%u];\n", GL_LIMITS(glsl_varyings) / 4);
667 /* TODO: Write a replacement shader for the fixed function vertex pipeline, so this isn't needed.
668 * For fixed function vertex processing + 3.0 pixel shader we need a separate function in the
669 * pixel shader that reads the fixed function color into the packed input registers.
671 shader_addline(buffer, "vec4 IN[%u];\n", GL_LIMITS(glsl_varyings) / 4);
675 /* Declare output register temporaries */
676 if(This->baseShader.limits.packed_output) {
677 shader_addline(buffer, "vec4 OUT[%u];\n", This->baseShader.limits.packed_output);
680 /* Declare temporary variables */
681 for(i = 0; i < This->baseShader.limits.temporary; i++) {
682 if (reg_maps->temporary[i])
683 shader_addline(buffer, "vec4 R%u;\n", i);
686 /* Declare attributes */
687 for (i = 0; i < This->baseShader.limits.attributes; i++) {
688 if (reg_maps->attributes[i])
689 shader_addline(buffer, "attribute vec4 attrib%i;\n", i);
692 /* Declare loop registers aLx */
693 for (i = 0; i < reg_maps->loop_depth; i++) {
694 shader_addline(buffer, "int aL%u;\n", i);
695 shader_addline(buffer, "int tmpInt%u;\n", i);
698 /* Temporary variables for matrix operations */
699 shader_addline(buffer, "vec4 tmp0;\n");
700 shader_addline(buffer, "vec4 tmp1;\n");
702 /* Local constants use a different name so they can be loaded once at shader link time
703 * They can't be hardcoded into the shader text via LC = {x, y, z, w}; because the
704 * float -> string conversion can cause precision loss.
706 if(!This->baseShader.load_local_constsF) {
707 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
708 shader_addline(buffer, "uniform vec4 %cLC%u;\n", prefix, lconst->idx);
712 /* Start the main program */
713 shader_addline(buffer, "void main() {\n");
714 if(pshader && reg_maps->vpos) {
715 /* DirectX apps expect integer values, while OpenGL drivers add approximately 0.5. This causes
716 * off-by-one problems as spotted by the vPos d3d9 visual test. Unfortunately the ATI cards do
717 * not add exactly 0.5, but rather something like 0.49999999 or 0.50000001, which still causes
718 * precision troubles when we just substract 0.5.
720 * To deal with that just floor() the position. This will eliminate the fraction on all cards.
722 * TODO: Test how that behaves with multisampling once we can enable multisampling in winex11.
724 * An advantage of floor is that it works even if the driver doesn't add 1/2. It is somewhat
725 * questionable if 1.5, 2.5, ... are the proper values to return in gl_FragCoord, even though
726 * coordinates specify the pixel centers instead of the pixel corners. This code will behave
727 * correctly on drivers that returns integer values.
729 shader_addline(buffer, "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
733 /*****************************************************************************
734 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
736 * For more information, see http://wiki.winehq.org/DirectX-Shaders
737 ****************************************************************************/
740 static void shader_glsl_add_src_param(SHADER_OPCODE_ARG* arg, const DWORD param,
741 const DWORD addr_token, DWORD mask, glsl_src_param_t *src_param);
743 /** Used for opcode modifiers - They multiply the result by the specified amount */
744 static const char * const shift_glsl_tab[] = {
746 "2.0 * ", /* 1 (x2) */
747 "4.0 * ", /* 2 (x4) */
748 "8.0 * ", /* 3 (x8) */
749 "16.0 * ", /* 4 (x16) */
750 "32.0 * ", /* 5 (x32) */
757 "0.0625 * ", /* 12 (d16) */
758 "0.125 * ", /* 13 (d8) */
759 "0.25 * ", /* 14 (d4) */
760 "0.5 * " /* 15 (d2) */
763 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
764 static void shader_glsl_gen_modifier (
767 const char *in_regswizzle,
772 if (instr == WINED3DSIO_TEXKILL)
775 switch (instr & WINED3DSP_SRCMOD_MASK) {
776 case WINED3DSPSM_DZ: /* Need to handle this in the instructions itself (texld & texcrd). */
778 case WINED3DSPSM_NONE:
779 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
781 case WINED3DSPSM_NEG:
782 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
784 case WINED3DSPSM_NOT:
785 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
787 case WINED3DSPSM_BIAS:
788 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
790 case WINED3DSPSM_BIASNEG:
791 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
793 case WINED3DSPSM_SIGN:
794 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
796 case WINED3DSPSM_SIGNNEG:
797 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
799 case WINED3DSPSM_COMP:
800 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
803 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
805 case WINED3DSPSM_X2NEG:
806 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
808 case WINED3DSPSM_ABS:
809 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
811 case WINED3DSPSM_ABSNEG:
812 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
815 FIXME("Unhandled modifier %u\n", (instr & WINED3DSP_SRCMOD_MASK));
816 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
820 /** Writes the GLSL variable name that corresponds to the register that the
821 * DX opcode parameter is trying to access */
822 static void shader_glsl_get_register_name(
824 const DWORD addr_token,
827 SHADER_OPCODE_ARG* arg) {
829 /* oPos, oFog and oPts in D3D */
830 static const char * const hwrastout_reg_names[] = { "gl_Position", "gl_FogFragCoord", "gl_PointSize" };
832 DWORD reg = param & WINED3DSP_REGNUM_MASK;
833 DWORD regtype = shader_get_regtype(param);
834 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) arg->shader;
835 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
836 WineD3D_GL_Info* gl_info = &deviceImpl->adapter->gl_info;
838 char pshader = shader_is_pshader_version(This->baseShader.hex_version);
844 case WINED3DSPR_TEMP:
845 sprintf(tmpStr, "R%u", reg);
847 case WINED3DSPR_INPUT:
849 /* Pixel shaders >= 3.0 */
850 if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 3) {
851 DWORD in_count = GL_LIMITS(glsl_varyings) / 4;
853 if (param & WINED3DSHADER_ADDRMODE_RELATIVE) {
854 glsl_src_param_t rel_param;
855 shader_glsl_add_src_param(arg, addr_token, 0, WINED3DSP_WRITEMASK_0, &rel_param);
857 /* Removing a + 0 would be an obvious optimization, but macos doesn't see the NOP
860 if(((IWineD3DPixelShaderImpl *) This)->input_reg_map[reg]) {
861 if (((IWineD3DPixelShaderImpl *)This)->declared_in_count > in_count) {
862 sprintf(tmpStr, "((%s + %u) > %d ? (%s + %u) > %d ? gl_SecondaryColor : gl_Color : IN[%s + %u])",
863 rel_param.param_str, ((IWineD3DPixelShaderImpl *)This)->input_reg_map[reg], in_count - 1,
864 rel_param.param_str, ((IWineD3DPixelShaderImpl *)This)->input_reg_map[reg], in_count,
865 rel_param.param_str, ((IWineD3DPixelShaderImpl *)This)->input_reg_map[reg]);
867 sprintf(tmpStr, "IN[%s + %u]", rel_param.param_str, ((IWineD3DPixelShaderImpl *)This)->input_reg_map[reg]);
870 if (((IWineD3DPixelShaderImpl *)This)->declared_in_count > in_count) {
871 sprintf(tmpStr, "((%s) > %d ? (%s) > %d ? gl_SecondaryColor : gl_Color : IN[%s])",
872 rel_param.param_str, in_count - 1,
873 rel_param.param_str, in_count,
874 rel_param.param_str);
876 sprintf(tmpStr, "IN[%s]", rel_param.param_str);
880 DWORD idx = ((IWineD3DPixelShaderImpl *) This)->input_reg_map[reg];
881 if (idx == in_count) {
882 sprintf(tmpStr, "gl_Color");
883 } else if (idx == in_count + 1) {
884 sprintf(tmpStr, "gl_SecondaryColor");
886 sprintf(tmpStr, "IN[%u]", idx);
891 strcpy(tmpStr, "gl_Color");
893 strcpy(tmpStr, "gl_SecondaryColor");
896 if (vshader_input_is_color((IWineD3DVertexShader*) This, reg))
898 sprintf(tmpStr, "attrib%u", reg);
901 case WINED3DSPR_CONST:
903 const char prefix = pshader? 'P':'V';
905 /* Relative addressing */
906 if (param & WINED3DSHADER_ADDRMODE_RELATIVE) {
908 /* Relative addressing on shaders 2.0+ have a relative address token,
909 * prior to that, it was hard-coded as "A0.x" because there's only 1 register */
910 if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 2) {
911 glsl_src_param_t rel_param;
912 shader_glsl_add_src_param(arg, addr_token, 0, WINED3DSP_WRITEMASK_0, &rel_param);
914 sprintf(tmpStr, "%cC[%s + %u]", prefix, rel_param.param_str, reg);
916 sprintf(tmpStr, "%cC[%s]", prefix, rel_param.param_str);
920 sprintf(tmpStr, "%cC[A0.x + %u]", prefix, reg);
922 sprintf(tmpStr, "%cC[A0.x]", prefix);
927 if(shader_constant_is_local(This, reg)) {
928 sprintf(tmpStr, "%cLC%u", prefix, reg);
930 sprintf(tmpStr, "%cC[%u]", prefix, reg);
936 case WINED3DSPR_CONSTINT:
938 sprintf(tmpStr, "PI[%u]", reg);
940 sprintf(tmpStr, "VI[%u]", reg);
942 case WINED3DSPR_CONSTBOOL:
944 sprintf(tmpStr, "PB[%u]", reg);
946 sprintf(tmpStr, "VB[%u]", reg);
948 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
950 sprintf(tmpStr, "T%u", reg);
952 sprintf(tmpStr, "A%u", reg);
955 case WINED3DSPR_LOOP:
956 sprintf(tmpStr, "aL%u", This->baseShader.cur_loop_regno - 1);
958 case WINED3DSPR_SAMPLER:
960 sprintf(tmpStr, "Psampler%u", reg);
962 sprintf(tmpStr, "Vsampler%u", reg);
964 case WINED3DSPR_COLOROUT:
965 if (reg >= GL_LIMITS(buffers)) {
966 WARN("Write to render target %u, only %d supported\n", reg, 4);
968 if (GL_SUPPORT(ARB_DRAW_BUFFERS)) {
969 sprintf(tmpStr, "gl_FragData[%u]", reg);
970 } else { /* On older cards with GLSL support like the GeforceFX there's only one buffer. */
971 sprintf(tmpStr, "gl_FragColor");
974 case WINED3DSPR_RASTOUT:
975 sprintf(tmpStr, "%s", hwrastout_reg_names[reg]);
977 case WINED3DSPR_DEPTHOUT:
978 sprintf(tmpStr, "gl_FragDepth");
980 case WINED3DSPR_ATTROUT:
982 sprintf(tmpStr, "gl_FrontColor");
984 sprintf(tmpStr, "gl_FrontSecondaryColor");
987 case WINED3DSPR_TEXCRDOUT:
988 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
989 if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 3)
990 sprintf(tmpStr, "OUT[%u]", reg);
992 sprintf(tmpStr, "gl_TexCoord[%u]", reg);
994 case WINED3DSPR_MISCTYPE:
997 sprintf(tmpStr, "vpos");
998 } else if (reg == 1){
999 /* Note that gl_FrontFacing is a bool, while vFace is
1000 * a float for which the sign determines front/back
1002 sprintf(tmpStr, "(gl_FrontFacing ? 1.0 : -1.0)");
1004 FIXME("Unhandled misctype register %d\n", reg);
1005 sprintf(tmpStr, "unrecognized_register");
1009 FIXME("Unhandled register name Type(%d)\n", regtype);
1010 sprintf(tmpStr, "unrecognized_register");
1014 strcat(regstr, tmpStr);
1017 /* Get the GLSL write mask for the destination register */
1018 static DWORD shader_glsl_get_write_mask(const DWORD param, char *write_mask) {
1019 char *ptr = write_mask;
1020 DWORD mask = param & WINED3DSP_WRITEMASK_ALL;
1022 if (shader_is_scalar(param)) {
1023 mask = WINED3DSP_WRITEMASK_0;
1026 if (param & WINED3DSP_WRITEMASK_0) *ptr++ = 'x';
1027 if (param & WINED3DSP_WRITEMASK_1) *ptr++ = 'y';
1028 if (param & WINED3DSP_WRITEMASK_2) *ptr++ = 'z';
1029 if (param & WINED3DSP_WRITEMASK_3) *ptr++ = 'w';
1037 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask) {
1038 unsigned int size = 0;
1040 if (write_mask & WINED3DSP_WRITEMASK_0) ++size;
1041 if (write_mask & WINED3DSP_WRITEMASK_1) ++size;
1042 if (write_mask & WINED3DSP_WRITEMASK_2) ++size;
1043 if (write_mask & WINED3DSP_WRITEMASK_3) ++size;
1048 static void shader_glsl_get_swizzle(const DWORD param, BOOL fixup, DWORD mask, char *swizzle_str) {
1049 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
1050 * but addressed as "rgba". To fix this we need to swap the register's x
1051 * and z components. */
1052 DWORD swizzle = (param & WINED3DSP_SWIZZLE_MASK) >> WINED3DSP_SWIZZLE_SHIFT;
1053 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
1054 char *ptr = swizzle_str;
1056 if (!shader_is_scalar(param)) {
1058 /* swizzle bits fields: wwzzyyxx */
1059 if (mask & WINED3DSP_WRITEMASK_0) *ptr++ = swizzle_chars[swizzle & 0x03];
1060 if (mask & WINED3DSP_WRITEMASK_1) *ptr++ = swizzle_chars[(swizzle >> 2) & 0x03];
1061 if (mask & WINED3DSP_WRITEMASK_2) *ptr++ = swizzle_chars[(swizzle >> 4) & 0x03];
1062 if (mask & WINED3DSP_WRITEMASK_3) *ptr++ = swizzle_chars[(swizzle >> 6) & 0x03];
1068 /* From a given parameter token, generate the corresponding GLSL string.
1069 * Also, return the actual register name and swizzle in case the
1070 * caller needs this information as well. */
1071 static void shader_glsl_add_src_param(SHADER_OPCODE_ARG* arg, const DWORD param,
1072 const DWORD addr_token, DWORD mask, glsl_src_param_t *src_param) {
1073 BOOL is_color = FALSE;
1074 char swizzle_str[6];
1076 src_param->reg_name[0] = '\0';
1077 src_param->param_str[0] = '\0';
1078 swizzle_str[0] = '\0';
1080 shader_glsl_get_register_name(param, addr_token, src_param->reg_name, &is_color, arg);
1082 shader_glsl_get_swizzle(param, is_color, mask, swizzle_str);
1083 shader_glsl_gen_modifier(param, src_param->reg_name, swizzle_str, src_param->param_str);
1086 /* From a given parameter token, generate the corresponding GLSL string.
1087 * Also, return the actual register name and swizzle in case the
1088 * caller needs this information as well. */
1089 static DWORD shader_glsl_add_dst_param(SHADER_OPCODE_ARG* arg, const DWORD param,
1090 const DWORD addr_token, glsl_dst_param_t *dst_param) {
1091 BOOL is_color = FALSE;
1093 dst_param->mask_str[0] = '\0';
1094 dst_param->reg_name[0] = '\0';
1096 shader_glsl_get_register_name(param, addr_token, dst_param->reg_name, &is_color, arg);
1097 return shader_glsl_get_write_mask(param, dst_param->mask_str);
1100 /* Append the destination part of the instruction to the buffer, return the effective write mask */
1101 static DWORD shader_glsl_append_dst_ext(SHADER_BUFFER *buffer, SHADER_OPCODE_ARG *arg, const DWORD param) {
1102 glsl_dst_param_t dst_param;
1106 mask = shader_glsl_add_dst_param(arg, param, arg->dst_addr, &dst_param);
1109 shift = (param & WINED3DSP_DSTSHIFT_MASK) >> WINED3DSP_DSTSHIFT_SHIFT;
1110 shader_addline(buffer, "%s%s = %s(", dst_param.reg_name, dst_param.mask_str, shift_glsl_tab[shift]);
1116 /* Append the destination part of the instruction to the buffer, return the effective write mask */
1117 static DWORD shader_glsl_append_dst(SHADER_BUFFER *buffer, SHADER_OPCODE_ARG *arg) {
1118 return shader_glsl_append_dst_ext(buffer, arg, arg->dst);
1121 /** Process GLSL instruction modifiers */
1122 void shader_glsl_add_instruction_modifiers(SHADER_OPCODE_ARG* arg) {
1124 DWORD mask = arg->dst & WINED3DSP_DSTMOD_MASK;
1126 if (arg->opcode->dst_token && mask != 0) {
1127 glsl_dst_param_t dst_param;
1129 shader_glsl_add_dst_param(arg, arg->dst, 0, &dst_param);
1131 if (mask & WINED3DSPDM_SATURATE) {
1132 /* _SAT means to clamp the value of the register to between 0 and 1 */
1133 shader_addline(arg->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param.reg_name,
1134 dst_param.mask_str, dst_param.reg_name, dst_param.mask_str);
1136 if (mask & WINED3DSPDM_MSAMPCENTROID) {
1137 FIXME("_centroid modifier not handled\n");
1139 if (mask & WINED3DSPDM_PARTIALPRECISION) {
1140 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
1145 static inline const char* shader_get_comp_op(
1146 const DWORD opcode) {
1148 DWORD op = (opcode & INST_CONTROLS_MASK) >> INST_CONTROLS_SHIFT;
1150 case COMPARISON_GT: return ">";
1151 case COMPARISON_EQ: return "==";
1152 case COMPARISON_GE: return ">=";
1153 case COMPARISON_LT: return "<";
1154 case COMPARISON_NE: return "!=";
1155 case COMPARISON_LE: return "<=";
1157 FIXME("Unrecognized comparison value: %u\n", op);
1162 static void shader_glsl_get_sample_function(DWORD sampler_type, BOOL projected, BOOL texrect, glsl_sample_function_t *sample_function) {
1163 /* Note that there's no such thing as a projected cube texture. */
1164 switch(sampler_type) {
1166 sample_function->name = projected ? "texture1DProj" : "texture1D";
1167 sample_function->coord_mask = WINED3DSP_WRITEMASK_0;
1171 sample_function->name = projected ? "texture2DRectProj" : "texture2DRect";
1173 sample_function->name = projected ? "texture2DProj" : "texture2D";
1175 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
1177 case WINED3DSTT_CUBE:
1178 sample_function->name = "textureCube";
1179 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1181 case WINED3DSTT_VOLUME:
1182 sample_function->name = projected ? "texture3DProj" : "texture3D";
1183 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1186 sample_function->name = "";
1187 FIXME("Unrecognized sampler type: %#x;\n", sampler_type);
1192 static void shader_glsl_color_correction(SHADER_OPCODE_ARG* arg) {
1193 IWineD3DBaseShaderImpl* shader = (IWineD3DBaseShaderImpl*) arg->shader;
1194 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) shader->baseShader.device;
1195 WineD3D_GL_Info *gl_info = &deviceImpl->adapter->gl_info;
1196 glsl_dst_param_t dst_param;
1197 glsl_dst_param_t dst_param2;
1199 WINED3DFORMAT conversion_group;
1200 IWineD3DBaseTextureImpl *texture;
1201 DWORD mask, mask_size;
1203 BOOL recorded = FALSE;
1205 DWORD hex_version = shader->baseShader.hex_version;
1207 switch(arg->opcode->opcode) {
1208 case WINED3DSIO_TEX:
1209 if (hex_version < WINED3DPS_VERSION(2,0)) {
1210 sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
1212 sampler_idx = arg->src[1] & WINED3DSP_REGNUM_MASK;
1216 case WINED3DSIO_TEXLDL:
1217 FIXME("Add color fixup for vertex texture WINED3DSIO_TEXLDL\n");
1220 case WINED3DSIO_TEXDP3TEX:
1221 case WINED3DSIO_TEXM3x3TEX:
1222 case WINED3DSIO_TEXM3x3SPEC:
1223 case WINED3DSIO_TEXM3x3VSPEC:
1224 case WINED3DSIO_TEXBEM:
1225 case WINED3DSIO_TEXREG2AR:
1226 case WINED3DSIO_TEXREG2GB:
1227 case WINED3DSIO_TEXREG2RGB:
1228 sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
1232 /* Not a texture sampling instruction, nothing to do */
1236 texture = (IWineD3DBaseTextureImpl *) deviceImpl->stateBlock->textures[sampler_idx];
1238 fmt = texture->resource.format;
1239 conversion_group = texture->baseTexture.shader_conversion_group;
1241 fmt = WINED3DFMT_UNKNOWN;
1242 conversion_group = WINED3DFMT_UNKNOWN;
1245 /* before doing anything, record the sampler with the format in the format conversion list,
1246 * but check if it's not there already
1248 for(i = 0; i < shader->baseShader.num_sampled_samplers; i++) {
1249 if(shader->baseShader.sampled_samplers[i] == sampler_idx) {
1255 shader->baseShader.sampled_samplers[shader->baseShader.num_sampled_samplers] = sampler_idx;
1256 shader->baseShader.num_sampled_samplers++;
1257 shader->baseShader.sampled_format[sampler_idx] = conversion_group;
1261 case WINED3DFMT_V8U8:
1262 case WINED3DFMT_V16U16:
1263 if(GL_SUPPORT(NV_TEXTURE_SHADER) ||
1264 (GL_SUPPORT(ATI_ENVMAP_BUMPMAP) && fmt == WINED3DFMT_V8U8)) {
1265 /* The 3rd channel returns 1.0 in d3d, but 0.0 in gl. Fix this while we're at it :-) */
1266 mask = shader_glsl_add_dst_param(arg, arg->dst, WINED3DSP_WRITEMASK_2, &dst_param);
1267 mask_size = shader_glsl_get_write_mask_size(mask);
1268 if(mask_size >= 3) {
1269 shader_addline(arg->buffer, "%s.%c = 1.0;\n", dst_param.reg_name, dst_param.mask_str[3]);
1272 /* Correct the sign, but leave the blue as it is - it was loaded correctly already */
1273 mask = shader_glsl_add_dst_param(arg, arg->dst,
1274 WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1,
1276 mask_size = shader_glsl_get_write_mask_size(mask);
1277 if(mask_size >= 2) {
1278 shader_addline(arg->buffer, "%s.%c%c = %s.%c%c * 2.0 - 1.0;\n",
1279 dst_param.reg_name, dst_param.mask_str[1], dst_param.mask_str[2],
1280 dst_param.reg_name, dst_param.mask_str[1], dst_param.mask_str[2]);
1281 } else if(mask_size == 1) {
1282 shader_addline(arg->buffer, "%s.%c = %s.%c * 2.0 - 1.0;\n", dst_param.reg_name, dst_param.mask_str[1],
1283 dst_param.reg_name, dst_param.mask_str[1]);
1288 case WINED3DFMT_X8L8V8U8:
1289 if(!GL_SUPPORT(NV_TEXTURE_SHADER)) {
1290 /* Red and blue are the signed channels, fix them up; Blue(=L) is correct already,
1291 * and a(X) is always 1.0
1293 mask = shader_glsl_add_dst_param(arg, arg->dst, WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &dst_param);
1294 mask_size = shader_glsl_get_write_mask_size(mask);
1295 if(mask_size >= 2) {
1296 shader_addline(arg->buffer, "%s.%c%c = %s.%c%c * 2.0 - 1.0;\n",
1297 dst_param.reg_name, dst_param.mask_str[1], dst_param.mask_str[2],
1298 dst_param.reg_name, dst_param.mask_str[1], dst_param.mask_str[2]);
1299 } else if(mask_size == 1) {
1300 shader_addline(arg->buffer, "%s.%c = %s.%c * 2.0 - 1.0;\n",
1301 dst_param.reg_name, dst_param.mask_str[1],
1302 dst_param.reg_name, dst_param.mask_str[1]);
1307 case WINED3DFMT_L6V5U5:
1308 if(!GL_SUPPORT(NV_TEXTURE_SHADER)) {
1309 mask = shader_glsl_add_dst_param(arg, arg->dst, WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &dst_param);
1310 mask_size = shader_glsl_get_write_mask_size(mask);
1311 shader_glsl_add_dst_param(arg, arg->dst, WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_2, &dst_param2);
1312 if(mask_size >= 3) {
1313 /* Swap y and z (U and L), and do a sign conversion on x and the new y(V and U) */
1314 shader_addline(arg->buffer, "tmp0.g = %s.%c;\n",
1315 dst_param.reg_name, dst_param.mask_str[2]);
1316 shader_addline(arg->buffer, "%s.%c%c = %s.%c%c * 2.0 - 1.0;\n",
1317 dst_param.reg_name, dst_param.mask_str[2], dst_param.mask_str[1],
1318 dst_param2.reg_name, dst_param.mask_str[1], dst_param.mask_str[3]);
1319 shader_addline(arg->buffer, "%s.%c = tmp0.g;\n", dst_param.reg_name,
1320 dst_param.mask_str[3]);
1321 } else if(mask_size == 2) {
1322 /* This is bad: We have VL, but we need VU */
1323 FIXME("2 components sampled from a converted L6V5U5 texture\n");
1325 shader_addline(arg->buffer, "%s.%c = %s.%c * 2.0 - 1.0;\n",
1326 dst_param.reg_name, dst_param.mask_str[1],
1327 dst_param2.reg_name, dst_param.mask_str[1]);
1332 case WINED3DFMT_Q8W8V8U8:
1333 if(!GL_SUPPORT(NV_TEXTURE_SHADER)) {
1334 /* Correct the sign in all channels. The writemask just applies as-is, no
1335 * need for checking the mask size
1337 shader_glsl_add_dst_param(arg, arg->dst,
1338 WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 |
1339 WINED3DSP_WRITEMASK_2 | WINED3DSP_WRITEMASK_3,
1341 shader_addline(arg->buffer, "%s%s = %s%s * 2.0 - 1.0;\n", dst_param.reg_name, dst_param.mask_str,
1342 dst_param.reg_name, dst_param.mask_str);
1346 /* stupid compiler */
1352 /*****************************************************************************
1354 * Begin processing individual instruction opcodes
1356 ****************************************************************************/
1358 /* Generate GLSL arithmetic functions (dst = src1 + src2) */
1359 void shader_glsl_arith(SHADER_OPCODE_ARG* arg) {
1360 CONST SHADER_OPCODE* curOpcode = arg->opcode;
1361 SHADER_BUFFER* buffer = arg->buffer;
1362 glsl_src_param_t src0_param;
1363 glsl_src_param_t src1_param;
1367 /* Determine the GLSL operator to use based on the opcode */
1368 switch (curOpcode->opcode) {
1369 case WINED3DSIO_MUL: op = '*'; break;
1370 case WINED3DSIO_ADD: op = '+'; break;
1371 case WINED3DSIO_SUB: op = '-'; break;
1374 FIXME("Opcode %s not yet handled in GLSL\n", curOpcode->name);
1378 write_mask = shader_glsl_append_dst(buffer, arg);
1379 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src0_param);
1380 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1381 shader_addline(buffer, "%s %c %s);\n", src0_param.param_str, op, src1_param.param_str);
1384 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
1385 void shader_glsl_mov(SHADER_OPCODE_ARG* arg) {
1386 IWineD3DBaseShaderImpl* shader = (IWineD3DBaseShaderImpl*) arg->shader;
1387 SHADER_BUFFER* buffer = arg->buffer;
1388 glsl_src_param_t src0_param;
1391 write_mask = shader_glsl_append_dst(buffer, arg);
1392 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src0_param);
1394 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
1395 * shader versions WINED3DSIO_MOVA is used for this. */
1396 if ((WINED3DSHADER_VERSION_MAJOR(shader->baseShader.hex_version) == 1 &&
1397 !shader_is_pshader_version(shader->baseShader.hex_version) &&
1398 shader_get_regtype(arg->dst) == WINED3DSPR_ADDR)) {
1399 /* This is a simple floor() */
1400 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
1401 if (mask_size > 1) {
1402 shader_addline(buffer, "ivec%d(floor(%s)));\n", mask_size, src0_param.param_str);
1404 shader_addline(buffer, "int(floor(%s)));\n", src0_param.param_str);
1406 } else if(arg->opcode->opcode == WINED3DSIO_MOVA) {
1407 /* We need to *round* to the nearest int here. */
1408 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
1409 if (mask_size > 1) {
1410 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);
1412 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n", src0_param.param_str, src0_param.param_str);
1415 shader_addline(buffer, "%s);\n", src0_param.param_str);
1419 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
1420 void shader_glsl_dot(SHADER_OPCODE_ARG* arg) {
1421 CONST SHADER_OPCODE* curOpcode = arg->opcode;
1422 SHADER_BUFFER* buffer = arg->buffer;
1423 glsl_src_param_t src0_param;
1424 glsl_src_param_t src1_param;
1425 DWORD dst_write_mask, src_write_mask;
1426 unsigned int dst_size = 0;
1428 dst_write_mask = shader_glsl_append_dst(buffer, arg);
1429 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
1431 /* dp3 works on vec3, dp4 on vec4 */
1432 if (curOpcode->opcode == WINED3DSIO_DP4) {
1433 src_write_mask = WINED3DSP_WRITEMASK_ALL;
1435 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1438 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_write_mask, &src0_param);
1439 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], src_write_mask, &src1_param);
1442 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
1444 shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
1448 /* Note that this instruction has some restrictions. The destination write mask
1449 * can't contain the w component, and the source swizzles have to be .xyzw */
1450 void shader_glsl_cross(SHADER_OPCODE_ARG *arg) {
1451 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1452 glsl_src_param_t src0_param;
1453 glsl_src_param_t src1_param;
1456 shader_glsl_get_write_mask(arg->dst, dst_mask);
1457 shader_glsl_append_dst(arg->buffer, arg);
1458 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
1459 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], src_mask, &src1_param);
1460 shader_addline(arg->buffer, "cross(%s, %s)%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
1463 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
1464 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
1465 * GLSL uses the value as-is. */
1466 void shader_glsl_pow(SHADER_OPCODE_ARG *arg) {
1467 SHADER_BUFFER *buffer = arg->buffer;
1468 glsl_src_param_t src0_param;
1469 glsl_src_param_t src1_param;
1470 DWORD dst_write_mask;
1471 unsigned int dst_size;
1473 dst_write_mask = shader_glsl_append_dst(buffer, arg);
1474 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
1476 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1477 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0, &src1_param);
1480 shader_addline(buffer, "vec%d(pow(abs(%s), %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
1482 shader_addline(buffer, "pow(abs(%s), %s));\n", src0_param.param_str, src1_param.param_str);
1486 /* Process the WINED3DSIO_LOG instruction in GLSL (dst = log2(|src0|))
1487 * Src0 is a scalar. Note that D3D uses the absolute of src0, while
1488 * GLSL uses the value as-is. */
1489 void shader_glsl_log(SHADER_OPCODE_ARG *arg) {
1490 SHADER_BUFFER *buffer = arg->buffer;
1491 glsl_src_param_t src0_param;
1492 DWORD dst_write_mask;
1493 unsigned int dst_size;
1495 dst_write_mask = shader_glsl_append_dst(buffer, arg);
1496 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
1498 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1501 shader_addline(buffer, "vec%d(log2(abs(%s))));\n", dst_size, src0_param.param_str);
1503 shader_addline(buffer, "log2(abs(%s)));\n", src0_param.param_str);
1507 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
1508 void shader_glsl_map2gl(SHADER_OPCODE_ARG* arg) {
1509 CONST SHADER_OPCODE* curOpcode = arg->opcode;
1510 SHADER_BUFFER* buffer = arg->buffer;
1511 glsl_src_param_t src_param;
1512 const char *instruction;
1513 char arguments[256];
1517 /* Determine the GLSL function to use based on the opcode */
1518 /* TODO: Possibly make this a table for faster lookups */
1519 switch (curOpcode->opcode) {
1520 case WINED3DSIO_MIN: instruction = "min"; break;
1521 case WINED3DSIO_MAX: instruction = "max"; break;
1522 case WINED3DSIO_ABS: instruction = "abs"; break;
1523 case WINED3DSIO_FRC: instruction = "fract"; break;
1524 case WINED3DSIO_NRM: instruction = "normalize"; break;
1525 case WINED3DSIO_LOGP:
1526 case WINED3DSIO_LOG: instruction = "log2"; break;
1527 case WINED3DSIO_EXP: instruction = "exp2"; break;
1528 case WINED3DSIO_SGN: instruction = "sign"; break;
1529 case WINED3DSIO_DSX: instruction = "dFdx"; break;
1530 case WINED3DSIO_DSY: instruction = "ycorrection.y * dFdy"; break;
1531 default: instruction = "";
1532 FIXME("Opcode %s not yet handled in GLSL\n", curOpcode->name);
1536 write_mask = shader_glsl_append_dst(buffer, arg);
1538 arguments[0] = '\0';
1539 if (curOpcode->num_params > 0) {
1540 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src_param);
1541 strcat(arguments, src_param.param_str);
1542 for (i = 2; i < curOpcode->num_params; ++i) {
1543 strcat(arguments, ", ");
1544 shader_glsl_add_src_param(arg, arg->src[i-1], arg->src_addr[i-1], write_mask, &src_param);
1545 strcat(arguments, src_param.param_str);
1549 shader_addline(buffer, "%s(%s));\n", instruction, arguments);
1552 /** Process the WINED3DSIO_EXPP instruction in GLSL:
1553 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
1554 * dst.x = 2^(floor(src))
1555 * dst.y = src - floor(src)
1556 * dst.z = 2^src (partial precision is allowed, but optional)
1558 * For 2.0 shaders, just do this (honoring writemask and swizzle):
1559 * dst = 2^src; (partial precision is allowed, but optional)
1561 void shader_glsl_expp(SHADER_OPCODE_ARG* arg) {
1562 IWineD3DBaseShaderImpl *shader = (IWineD3DBaseShaderImpl *)arg->shader;
1563 glsl_src_param_t src_param;
1565 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src_param);
1567 if (shader->baseShader.hex_version < WINED3DPS_VERSION(2,0)) {
1570 shader_addline(arg->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
1571 shader_addline(arg->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
1572 shader_addline(arg->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
1573 shader_addline(arg->buffer, "tmp0.w = 1.0;\n");
1575 shader_glsl_append_dst(arg->buffer, arg);
1576 shader_glsl_get_write_mask(arg->dst, dst_mask);
1577 shader_addline(arg->buffer, "tmp0%s);\n", dst_mask);
1580 unsigned int mask_size;
1582 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1583 mask_size = shader_glsl_get_write_mask_size(write_mask);
1585 if (mask_size > 1) {
1586 shader_addline(arg->buffer, "vec%d(exp2(%s)));\n", mask_size, src_param.param_str);
1588 shader_addline(arg->buffer, "exp2(%s));\n", src_param.param_str);
1593 /** Process the RCP (reciprocal or inverse) opcode in GLSL (dst = 1 / src) */
1594 void shader_glsl_rcp(SHADER_OPCODE_ARG* arg) {
1595 glsl_src_param_t src_param;
1597 unsigned int mask_size;
1599 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1600 mask_size = shader_glsl_get_write_mask_size(write_mask);
1601 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_3, &src_param);
1603 if (mask_size > 1) {
1604 shader_addline(arg->buffer, "vec%d(1.0 / %s));\n", mask_size, src_param.param_str);
1606 shader_addline(arg->buffer, "1.0 / %s);\n", src_param.param_str);
1610 void shader_glsl_rsq(SHADER_OPCODE_ARG* arg) {
1611 SHADER_BUFFER* buffer = arg->buffer;
1612 glsl_src_param_t src_param;
1614 unsigned int mask_size;
1616 write_mask = shader_glsl_append_dst(buffer, arg);
1617 mask_size = shader_glsl_get_write_mask_size(write_mask);
1619 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_3, &src_param);
1621 if (mask_size > 1) {
1622 shader_addline(buffer, "vec%d(inversesqrt(%s)));\n", mask_size, src_param.param_str);
1624 shader_addline(buffer, "inversesqrt(%s));\n", src_param.param_str);
1628 /** Process signed comparison opcodes in GLSL. */
1629 void shader_glsl_compare(SHADER_OPCODE_ARG* arg) {
1630 glsl_src_param_t src0_param;
1631 glsl_src_param_t src1_param;
1633 unsigned int mask_size;
1635 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1636 mask_size = shader_glsl_get_write_mask_size(write_mask);
1637 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src0_param);
1638 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1640 if (mask_size > 1) {
1641 const char *compare;
1643 switch(arg->opcode->opcode) {
1644 case WINED3DSIO_SLT: compare = "lessThan"; break;
1645 case WINED3DSIO_SGE: compare = "greaterThanEqual"; break;
1646 default: compare = "";
1647 FIXME("Can't handle opcode %s\n", arg->opcode->name);
1650 shader_addline(arg->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
1651 src0_param.param_str, src1_param.param_str);
1653 switch(arg->opcode->opcode) {
1654 case WINED3DSIO_SLT:
1655 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
1656 * to return 0.0 but step returns 1.0 because step is not < x
1657 * An alternative is a bvec compare padded with an unused second component.
1658 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
1659 * issue. Playing with not() is not possible either because not() does not accept
1662 shader_addline(arg->buffer, "(%s < %s) ? 1.0 : 0.0);\n", src0_param.param_str, src1_param.param_str);
1664 case WINED3DSIO_SGE:
1665 /* Here we can use the step() function and safe a conditional */
1666 shader_addline(arg->buffer, "step(%s, %s));\n", src1_param.param_str, src0_param.param_str);
1669 FIXME("Can't handle opcode %s\n", arg->opcode->name);
1675 /** Process CMP instruction in GLSL (dst = src0 >= 0.0 ? src1 : src2), per channel */
1676 void shader_glsl_cmp(SHADER_OPCODE_ARG* arg) {
1677 glsl_src_param_t src0_param;
1678 glsl_src_param_t src1_param;
1679 glsl_src_param_t src2_param;
1680 DWORD write_mask, cmp_channel = 0;
1683 BOOL temp_destination = FALSE;
1685 if(shader_is_scalar(arg->src[0])) {
1686 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1688 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
1689 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1690 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, &src2_param);
1692 shader_addline(arg->buffer, "%s >= 0.0 ? %s : %s);\n",
1693 src0_param.param_str, src1_param.param_str, src2_param.param_str);
1695 DWORD src0reg = arg->src[0] & WINED3DSP_REGNUM_MASK;
1696 DWORD src1reg = arg->src[1] & WINED3DSP_REGNUM_MASK;
1697 DWORD src2reg = arg->src[2] & WINED3DSP_REGNUM_MASK;
1698 DWORD src0regtype = shader_get_regtype(arg->src[0]);
1699 DWORD src1regtype = shader_get_regtype(arg->src[1]);
1700 DWORD src2regtype = shader_get_regtype(arg->src[2]);
1701 DWORD dstreg = arg->dst & WINED3DSP_REGNUM_MASK;
1702 DWORD dstregtype = shader_get_regtype(arg->dst);
1704 /* Cycle through all source0 channels */
1705 for (i=0; i<4; i++) {
1707 /* Find the destination channels which use the current source0 channel */
1708 for (j=0; j<4; j++) {
1709 if ( ((arg->src[0] >> (WINED3DSP_SWIZZLE_SHIFT + 2*j)) & 0x3) == i ) {
1710 write_mask |= WINED3DSP_WRITEMASK_0 << j;
1711 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
1715 /* Splitting the cmp instruction up in multiple lines imposes a problem:
1716 * The first lines may overwrite source parameters of the following lines.
1717 * Deal with that by using a temporary destination register if needed
1719 if((src0reg == dstreg && src0regtype == dstregtype) ||
1720 (src1reg == dstreg && src1regtype == dstregtype) ||
1721 (src2reg == dstreg && src2regtype == dstregtype)) {
1723 write_mask = shader_glsl_get_write_mask(arg->dst & (~WINED3DSP_SWIZZLE_MASK | write_mask), mask_char);
1724 if (!write_mask) continue;
1725 shader_addline(arg->buffer, "tmp0%s = (", mask_char);
1726 temp_destination = TRUE;
1728 write_mask = shader_glsl_append_dst_ext(arg->buffer, arg, arg->dst & (~WINED3DSP_SWIZZLE_MASK | write_mask));
1729 if (!write_mask) continue;
1732 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], cmp_channel, &src0_param);
1733 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1734 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, &src2_param);
1736 shader_addline(arg->buffer, "%s >= 0.0 ? %s : %s);\n",
1737 src0_param.param_str, src1_param.param_str, src2_param.param_str);
1740 if(temp_destination) {
1741 shader_glsl_get_write_mask(arg->dst, mask_char);
1742 shader_glsl_append_dst_ext(arg->buffer, arg, arg->dst);
1743 shader_addline(arg->buffer, "tmp0%s);\n", mask_char);
1749 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
1750 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
1751 * the compare is done per component of src0. */
1752 void shader_glsl_cnd(SHADER_OPCODE_ARG* arg) {
1753 IWineD3DBaseShaderImpl* shader = (IWineD3DBaseShaderImpl*) arg->shader;
1754 glsl_src_param_t src0_param;
1755 glsl_src_param_t src1_param;
1756 glsl_src_param_t src2_param;
1757 DWORD write_mask, cmp_channel = 0;
1760 if (shader->baseShader.hex_version < WINED3DPS_VERSION(1, 4)) {
1761 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1762 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1763 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1764 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, &src2_param);
1766 /* Fun: The D3DSI_COISSUE flag changes the semantic of the cnd instruction for < 1.4 shaders */
1767 if(arg->opcode_token & WINED3DSI_COISSUE) {
1768 shader_addline(arg->buffer, "%s /* COISSUE! */);\n", src1_param.param_str);
1770 shader_addline(arg->buffer, "%s > 0.5 ? %s : %s);\n",
1771 src0_param.param_str, src1_param.param_str, src2_param.param_str);
1775 /* Cycle through all source0 channels */
1776 for (i=0; i<4; i++) {
1778 /* Find the destination channels which use the current source0 channel */
1779 for (j=0; j<4; j++) {
1780 if ( ((arg->src[0] >> (WINED3DSP_SWIZZLE_SHIFT + 2*j)) & 0x3) == i ) {
1781 write_mask |= WINED3DSP_WRITEMASK_0 << j;
1782 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
1785 write_mask = shader_glsl_append_dst_ext(arg->buffer, arg, arg->dst & (~WINED3DSP_SWIZZLE_MASK | write_mask));
1786 if (!write_mask) continue;
1788 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], cmp_channel, &src0_param);
1789 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1790 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, &src2_param);
1792 shader_addline(arg->buffer, "%s > 0.5 ? %s : %s);\n",
1793 src0_param.param_str, src1_param.param_str, src2_param.param_str);
1797 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
1798 void shader_glsl_mad(SHADER_OPCODE_ARG* arg) {
1799 glsl_src_param_t src0_param;
1800 glsl_src_param_t src1_param;
1801 glsl_src_param_t src2_param;
1804 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1805 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src0_param);
1806 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1807 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, &src2_param);
1808 shader_addline(arg->buffer, "(%s * %s) + %s);\n",
1809 src0_param.param_str, src1_param.param_str, src2_param.param_str);
1812 /** Handles transforming all WINED3DSIO_M?x? opcodes for
1813 Vertex shaders to GLSL codes */
1814 void shader_glsl_mnxn(SHADER_OPCODE_ARG* arg) {
1816 int nComponents = 0;
1817 SHADER_OPCODE_ARG tmpArg;
1819 memset(&tmpArg, 0, sizeof(SHADER_OPCODE_ARG));
1821 /* Set constants for the temporary argument */
1822 tmpArg.shader = arg->shader;
1823 tmpArg.buffer = arg->buffer;
1824 tmpArg.src[0] = arg->src[0];
1825 tmpArg.src_addr[0] = arg->src_addr[0];
1826 tmpArg.src_addr[1] = arg->src_addr[1];
1827 tmpArg.reg_maps = arg->reg_maps;
1829 switch(arg->opcode->opcode) {
1830 case WINED3DSIO_M4x4:
1832 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP4);
1834 case WINED3DSIO_M4x3:
1836 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP4);
1838 case WINED3DSIO_M3x4:
1840 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP3);
1842 case WINED3DSIO_M3x3:
1844 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP3);
1846 case WINED3DSIO_M3x2:
1848 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP3);
1854 for (i = 0; i < nComponents; i++) {
1855 tmpArg.dst = ((arg->dst) & ~WINED3DSP_WRITEMASK_ALL)|(WINED3DSP_WRITEMASK_0<<i);
1856 tmpArg.src[1] = arg->src[1]+i;
1857 shader_glsl_dot(&tmpArg);
1862 The LRP instruction performs a component-wise linear interpolation
1863 between the second and third operands using the first operand as the
1864 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
1865 This is equivalent to mix(src2, src1, src0);
1867 void shader_glsl_lrp(SHADER_OPCODE_ARG* arg) {
1868 glsl_src_param_t src0_param;
1869 glsl_src_param_t src1_param;
1870 glsl_src_param_t src2_param;
1873 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1875 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src0_param);
1876 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1877 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, &src2_param);
1879 shader_addline(arg->buffer, "mix(%s, %s, %s));\n",
1880 src2_param.param_str, src1_param.param_str, src0_param.param_str);
1883 /** Process the WINED3DSIO_LIT instruction in GLSL:
1884 * dst.x = dst.w = 1.0
1885 * dst.y = (src0.x > 0) ? src0.x
1886 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
1887 * where src.w is clamped at +- 128
1889 void shader_glsl_lit(SHADER_OPCODE_ARG* arg) {
1890 glsl_src_param_t src0_param;
1891 glsl_src_param_t src1_param;
1892 glsl_src_param_t src3_param;
1895 shader_glsl_append_dst(arg->buffer, arg);
1896 shader_glsl_get_write_mask(arg->dst, dst_mask);
1898 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1899 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_1, &src1_param);
1900 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_3, &src3_param);
1902 /* The sdk specifies the instruction like this
1904 * if(src.x > 0.0) dst.y = src.x
1906 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
1910 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
1911 * dst.x = 1.0 ... No further explanation needed
1912 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
1913 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
1914 * dst.w = 1.0. ... Nothing fancy.
1916 * So we still have one conditional in there. So do this:
1917 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
1919 * 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),
1920 * 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.
1921 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to
1923 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",
1924 src0_param.param_str, src1_param.param_str, src0_param.param_str, src3_param.param_str, dst_mask);
1927 /** Process the WINED3DSIO_DST instruction in GLSL:
1929 * dst.y = src0.x * src0.y
1933 void shader_glsl_dst(SHADER_OPCODE_ARG* arg) {
1934 glsl_src_param_t src0y_param;
1935 glsl_src_param_t src0z_param;
1936 glsl_src_param_t src1y_param;
1937 glsl_src_param_t src1w_param;
1940 shader_glsl_append_dst(arg->buffer, arg);
1941 shader_glsl_get_write_mask(arg->dst, dst_mask);
1943 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_1, &src0y_param);
1944 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_2, &src0z_param);
1945 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_1, &src1y_param);
1946 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_3, &src1w_param);
1948 shader_addline(arg->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
1949 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
1952 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
1953 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
1954 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
1956 * dst.x = cos(src0.?)
1957 * dst.y = sin(src0.?)
1961 void shader_glsl_sincos(SHADER_OPCODE_ARG* arg) {
1962 glsl_src_param_t src0_param;
1965 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1966 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1968 switch (write_mask) {
1969 case WINED3DSP_WRITEMASK_0:
1970 shader_addline(arg->buffer, "cos(%s));\n", src0_param.param_str);
1973 case WINED3DSP_WRITEMASK_1:
1974 shader_addline(arg->buffer, "sin(%s));\n", src0_param.param_str);
1977 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
1978 shader_addline(arg->buffer, "vec2(cos(%s), sin(%s)));\n", src0_param.param_str, src0_param.param_str);
1982 ERR("Write mask should be .x, .y or .xy\n");
1987 /** Process the WINED3DSIO_LOOP instruction in GLSL:
1988 * Start a for() loop where src1.y is the initial value of aL,
1989 * increment aL by src1.z for a total of src1.x iterations.
1990 * Need to use a temporary variable for this operation.
1992 /* FIXME: I don't think nested loops will work correctly this way. */
1993 void shader_glsl_loop(SHADER_OPCODE_ARG* arg) {
1994 glsl_src_param_t src1_param;
1995 IWineD3DBaseShaderImpl* shader = (IWineD3DBaseShaderImpl*) arg->shader;
1996 DWORD regtype = shader_get_regtype(arg->src[1]);
1997 DWORD reg = arg->src[1] & WINED3DSP_REGNUM_MASK;
1998 const DWORD *control_values = NULL;
1999 local_constant *constant;
2001 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_ALL, &src1_param);
2003 /* Try to hardcode the loop control parameters if possible. Direct3D 9 class hardware doesn't support real
2004 * varying indexing, but Microsoft designed this feature for Shader model 2.x+. If the loop control is
2005 * known at compile time, the GLSL compiler can unroll the loop, and replace indirect addressing with direct
2008 if(regtype == WINED3DSPR_CONSTINT) {
2009 LIST_FOR_EACH_ENTRY(constant, &shader->baseShader.constantsI, local_constant, entry) {
2010 if(constant->idx == reg) {
2011 control_values = constant->value;
2017 if(control_values) {
2018 if(control_values[2] > 0) {
2019 shader_addline(arg->buffer, "for (aL%u = %d; aL%u < (%d * %d + %d); aL%u += %d) {\n",
2020 shader->baseShader.cur_loop_depth, control_values[1],
2021 shader->baseShader.cur_loop_depth, control_values[0], control_values[2], control_values[1],
2022 shader->baseShader.cur_loop_depth, control_values[2]);
2023 } else if(control_values[2] == 0) {
2024 shader_addline(arg->buffer, "for (aL%u = %d, tmpInt%u = 0; tmpInt%u < %d; tmpInt%u++) {\n",
2025 shader->baseShader.cur_loop_depth, control_values[1], shader->baseShader.cur_loop_depth,
2026 shader->baseShader.cur_loop_depth, control_values[0],
2027 shader->baseShader.cur_loop_depth);
2029 shader_addline(arg->buffer, "for (aL%u = %d; aL%u > (%d * %d + %d); aL%u += %d) {\n",
2030 shader->baseShader.cur_loop_depth, control_values[1],
2031 shader->baseShader.cur_loop_depth, control_values[0], control_values[2], control_values[1],
2032 shader->baseShader.cur_loop_depth, control_values[2]);
2035 shader_addline(arg->buffer, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z) {\n",
2036 shader->baseShader.cur_loop_depth, shader->baseShader.cur_loop_regno,
2037 src1_param.reg_name, shader->baseShader.cur_loop_depth, src1_param.reg_name,
2038 shader->baseShader.cur_loop_depth, shader->baseShader.cur_loop_regno, src1_param.reg_name);
2041 shader->baseShader.cur_loop_depth++;
2042 shader->baseShader.cur_loop_regno++;
2045 void shader_glsl_end(SHADER_OPCODE_ARG* arg) {
2046 IWineD3DBaseShaderImpl* shader = (IWineD3DBaseShaderImpl*) arg->shader;
2048 shader_addline(arg->buffer, "}\n");
2050 if(arg->opcode->opcode == WINED3DSIO_ENDLOOP) {
2051 shader->baseShader.cur_loop_depth--;
2052 shader->baseShader.cur_loop_regno--;
2054 if(arg->opcode->opcode == WINED3DSIO_ENDREP) {
2055 shader->baseShader.cur_loop_depth--;
2059 void shader_glsl_rep(SHADER_OPCODE_ARG* arg) {
2060 IWineD3DBaseShaderImpl* shader = (IWineD3DBaseShaderImpl*) arg->shader;
2061 glsl_src_param_t src0_param;
2063 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
2064 shader_addline(arg->buffer, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
2065 shader->baseShader.cur_loop_depth, shader->baseShader.cur_loop_depth,
2066 src0_param.param_str, shader->baseShader.cur_loop_depth);
2067 shader->baseShader.cur_loop_depth++;
2070 void shader_glsl_if(SHADER_OPCODE_ARG* arg) {
2071 glsl_src_param_t src0_param;
2073 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
2074 shader_addline(arg->buffer, "if (%s) {\n", src0_param.param_str);
2077 void shader_glsl_ifc(SHADER_OPCODE_ARG* arg) {
2078 glsl_src_param_t src0_param;
2079 glsl_src_param_t src1_param;
2081 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
2082 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0, &src1_param);
2084 shader_addline(arg->buffer, "if (%s %s %s) {\n",
2085 src0_param.param_str, shader_get_comp_op(arg->opcode_token), src1_param.param_str);
2088 void shader_glsl_else(SHADER_OPCODE_ARG* arg) {
2089 shader_addline(arg->buffer, "} else {\n");
2092 void shader_glsl_break(SHADER_OPCODE_ARG* arg) {
2093 shader_addline(arg->buffer, "break;\n");
2096 /* FIXME: According to MSDN the compare is done per component. */
2097 void shader_glsl_breakc(SHADER_OPCODE_ARG* arg) {
2098 glsl_src_param_t src0_param;
2099 glsl_src_param_t src1_param;
2101 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
2102 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0, &src1_param);
2104 shader_addline(arg->buffer, "if (%s %s %s) break;\n",
2105 src0_param.param_str, shader_get_comp_op(arg->opcode_token), src1_param.param_str);
2108 void shader_glsl_label(SHADER_OPCODE_ARG* arg) {
2110 DWORD snum = (arg->src[0]) & WINED3DSP_REGNUM_MASK;
2111 shader_addline(arg->buffer, "}\n");
2112 shader_addline(arg->buffer, "void subroutine%u () {\n", snum);
2115 void shader_glsl_call(SHADER_OPCODE_ARG* arg) {
2116 DWORD snum = (arg->src[0]) & WINED3DSP_REGNUM_MASK;
2117 shader_addline(arg->buffer, "subroutine%u();\n", snum);
2120 void shader_glsl_callnz(SHADER_OPCODE_ARG* arg) {
2121 glsl_src_param_t src1_param;
2123 DWORD snum = (arg->src[0]) & WINED3DSP_REGNUM_MASK;
2124 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0, &src1_param);
2125 shader_addline(arg->buffer, "if (%s) subroutine%u();\n", src1_param.param_str, snum);
2128 /*********************************************
2129 * Pixel Shader Specific Code begins here
2130 ********************************************/
2131 void pshader_glsl_tex(SHADER_OPCODE_ARG* arg) {
2132 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
2133 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
2134 DWORD hex_version = This->baseShader.hex_version;
2135 char dst_swizzle[6];
2136 glsl_sample_function_t sample_function;
2139 BOOL projected, texrect = FALSE;
2142 /* All versions have a destination register */
2143 shader_glsl_append_dst(arg->buffer, arg);
2145 /* 1.0-1.4: Use destination register as sampler source.
2146 * 2.0+: Use provided sampler source. */
2147 if (hex_version < WINED3DPS_VERSION(1,4)) {
2150 sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2151 flags = deviceImpl->stateBlock->textureState[sampler_idx][WINED3DTSS_TEXTURETRANSFORMFLAGS];
2153 if (flags & WINED3DTTFF_PROJECTED) {
2155 switch (flags & ~WINED3DTTFF_PROJECTED) {
2156 case WINED3DTTFF_COUNT1: FIXME("WINED3DTTFF_PROJECTED with WINED3DTTFF_COUNT1?\n"); break;
2157 case WINED3DTTFF_COUNT2: mask = WINED3DSP_WRITEMASK_1; break;
2158 case WINED3DTTFF_COUNT3: mask = WINED3DSP_WRITEMASK_2; break;
2159 case WINED3DTTFF_COUNT4:
2160 case WINED3DTTFF_DISABLE: mask = WINED3DSP_WRITEMASK_3; break;
2165 } else if (hex_version < WINED3DPS_VERSION(2,0)) {
2166 DWORD src_mod = arg->src[0] & WINED3DSP_SRCMOD_MASK;
2167 sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2169 if (src_mod == WINED3DSPSM_DZ) {
2171 mask = WINED3DSP_WRITEMASK_2;
2172 } else if (src_mod == WINED3DSPSM_DW) {
2174 mask = WINED3DSP_WRITEMASK_3;
2179 sampler_idx = arg->src[1] & WINED3DSP_REGNUM_MASK;
2180 if(arg->opcode_token & WINED3DSI_TEXLD_PROJECT) {
2181 /* ps 2.0 texldp instruction always divides by the fourth component. */
2183 mask = WINED3DSP_WRITEMASK_3;
2189 if(deviceImpl->stateBlock->textures[sampler_idx] &&
2190 IWineD3DBaseTexture_GetTextureDimensions(deviceImpl->stateBlock->textures[sampler_idx]) == GL_TEXTURE_RECTANGLE_ARB) {
2194 sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
2195 shader_glsl_get_sample_function(sampler_type, projected, texrect, &sample_function);
2196 mask |= sample_function.coord_mask;
2198 if (hex_version < WINED3DPS_VERSION(2,0)) {
2199 shader_glsl_get_write_mask(arg->dst, dst_swizzle);
2201 shader_glsl_get_swizzle(arg->src[1], FALSE, arg->dst, dst_swizzle);
2204 /* 1.0-1.3: Use destination register as coordinate source.
2205 1.4+: Use provided coordinate source register. */
2206 if (hex_version < WINED3DPS_VERSION(1,4)) {
2208 shader_glsl_get_write_mask(mask, coord_mask);
2209 shader_addline(arg->buffer, "%s(Psampler%u, T%u%s)%s);\n",
2210 sample_function.name, sampler_idx, sampler_idx, coord_mask, dst_swizzle);
2212 glsl_src_param_t coord_param;
2213 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], mask, &coord_param);
2214 if(arg->opcode_token & WINED3DSI_TEXLD_BIAS) {
2215 glsl_src_param_t bias;
2216 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_3, &bias);
2218 shader_addline(arg->buffer, "%s(Psampler%u, %s, %s)%s);\n",
2219 sample_function.name, sampler_idx, coord_param.param_str,
2220 bias.param_str, dst_swizzle);
2222 shader_addline(arg->buffer, "%s(Psampler%u, %s)%s);\n",
2223 sample_function.name, sampler_idx, coord_param.param_str, dst_swizzle);
2228 void shader_glsl_texldl(SHADER_OPCODE_ARG* arg) {
2229 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*)arg->shader;
2230 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
2231 glsl_sample_function_t sample_function;
2232 glsl_src_param_t coord_param, lod_param;
2233 char dst_swizzle[6];
2236 BOOL texrect = FALSE;
2238 shader_glsl_append_dst(arg->buffer, arg);
2239 shader_glsl_get_swizzle(arg->src[1], FALSE, arg->dst, dst_swizzle);
2241 sampler_idx = arg->src[1] & WINED3DSP_REGNUM_MASK;
2242 sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
2243 if(deviceImpl->stateBlock->textures[sampler_idx] &&
2244 IWineD3DBaseTexture_GetTextureDimensions(deviceImpl->stateBlock->textures[sampler_idx]) == GL_TEXTURE_RECTANGLE_ARB) {
2247 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);
2249 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_3, &lod_param);
2251 if (shader_is_pshader_version(This->baseShader.hex_version)) {
2252 /* The GLSL spec claims the Lod sampling functions are only supported in vertex shaders.
2253 * However, they seem to work just fine in fragment shaders as well. */
2254 WARN("Using %sLod in fragment shader.\n", sample_function.name);
2255 shader_addline(arg->buffer, "%sLod(Psampler%u, %s, %s)%s);\n",
2256 sample_function.name, sampler_idx, coord_param.param_str, lod_param.param_str, dst_swizzle);
2258 shader_addline(arg->buffer, "%sLod(Vsampler%u, %s, %s)%s);\n",
2259 sample_function.name, sampler_idx, coord_param.param_str, lod_param.param_str, dst_swizzle);
2263 void pshader_glsl_texcoord(SHADER_OPCODE_ARG* arg) {
2265 /* FIXME: Make this work for more than just 2D textures */
2267 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
2268 SHADER_BUFFER* buffer = arg->buffer;
2269 DWORD hex_version = This->baseShader.hex_version;
2273 write_mask = shader_glsl_append_dst(arg->buffer, arg);
2274 shader_glsl_get_write_mask(write_mask, dst_mask);
2276 if (hex_version != WINED3DPS_VERSION(1,4)) {
2277 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2278 shader_addline(buffer, "clamp(gl_TexCoord[%u], 0.0, 1.0)%s);\n", reg, dst_mask);
2280 DWORD reg = arg->src[0] & WINED3DSP_REGNUM_MASK;
2281 DWORD src_mod = arg->src[0] & WINED3DSP_SRCMOD_MASK;
2282 char dst_swizzle[6];
2284 shader_glsl_get_swizzle(arg->src[0], FALSE, write_mask, dst_swizzle);
2286 if (src_mod == WINED3DSPSM_DZ) {
2287 glsl_src_param_t div_param;
2288 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
2289 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_2, &div_param);
2291 if (mask_size > 1) {
2292 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
2294 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
2296 } else if (src_mod == WINED3DSPSM_DW) {
2297 glsl_src_param_t div_param;
2298 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
2299 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_3, &div_param);
2301 if (mask_size > 1) {
2302 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
2304 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
2307 shader_addline(buffer, "gl_TexCoord[%u]%s);\n", reg, dst_swizzle);
2312 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
2313 * Take a 3-component dot product of the TexCoord[dstreg] and src,
2314 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
2315 void pshader_glsl_texdp3tex(SHADER_OPCODE_ARG* arg) {
2316 glsl_src_param_t src0_param;
2318 glsl_sample_function_t sample_function;
2319 DWORD sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2320 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2321 DWORD sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
2323 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2325 shader_glsl_append_dst(arg->buffer, arg);
2326 shader_glsl_get_write_mask(arg->dst, dst_mask);
2328 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
2329 * scalar, and projected sampling would require 4.
2331 * It is a dependent read - not valid with conditional NP2 textures
2333 shader_glsl_get_sample_function(sampler_type, FALSE, FALSE, &sample_function);
2335 switch(count_bits(sample_function.coord_mask)) {
2337 shader_addline(arg->buffer, "%s(Psampler%u, dot(gl_TexCoord[%u].xyz, %s))%s);\n",
2338 sample_function.name, sampler_idx, sampler_idx, src0_param.param_str, dst_mask);
2342 shader_addline(arg->buffer, "%s(Psampler%u, vec2(dot(gl_TexCoord[%u].xyz, %s), 0.0))%s);\n",
2343 sample_function.name, sampler_idx, sampler_idx, src0_param.param_str, dst_mask);
2347 shader_addline(arg->buffer, "%s(Psampler%u, vec3(dot(gl_TexCoord[%u].xyz, %s), 0.0, 0.0))%s);\n",
2348 sample_function.name, sampler_idx, sampler_idx, src0_param.param_str, dst_mask);
2351 FIXME("Unexpected mask bitcount %d\n", count_bits(sample_function.coord_mask));
2355 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
2356 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
2357 void pshader_glsl_texdp3(SHADER_OPCODE_ARG* arg) {
2358 glsl_src_param_t src0_param;
2359 DWORD dstreg = arg->dst & WINED3DSP_REGNUM_MASK;
2360 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2362 unsigned int mask_size;
2364 dst_mask = shader_glsl_append_dst(arg->buffer, arg);
2365 mask_size = shader_glsl_get_write_mask_size(dst_mask);
2366 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2368 if (mask_size > 1) {
2369 shader_addline(arg->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
2371 shader_addline(arg->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
2375 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
2376 * Calculate the depth as dst.x / dst.y */
2377 void pshader_glsl_texdepth(SHADER_OPCODE_ARG* arg) {
2378 glsl_dst_param_t dst_param;
2380 shader_glsl_add_dst_param(arg, arg->dst, 0, &dst_param);
2382 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
2383 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
2384 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
2385 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
2388 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);
2391 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
2392 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
2393 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
2394 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
2396 void pshader_glsl_texm3x2depth(SHADER_OPCODE_ARG* arg) {
2397 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2398 DWORD dstreg = arg->dst & WINED3DSP_REGNUM_MASK;
2399 glsl_src_param_t src0_param;
2401 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2403 shader_addline(arg->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
2404 shader_addline(arg->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
2407 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
2408 * Calculate the 1st of a 2-row matrix multiplication. */
2409 void pshader_glsl_texm3x2pad(SHADER_OPCODE_ARG* arg) {
2410 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2411 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2412 SHADER_BUFFER* buffer = arg->buffer;
2413 glsl_src_param_t src0_param;
2415 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2416 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
2419 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
2420 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
2421 void pshader_glsl_texm3x3pad(SHADER_OPCODE_ARG* arg) {
2423 IWineD3DPixelShaderImpl* shader = (IWineD3DPixelShaderImpl*) arg->shader;
2424 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2425 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2426 SHADER_BUFFER* buffer = arg->buffer;
2427 SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
2428 glsl_src_param_t src0_param;
2430 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2431 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + current_state->current_row, reg, src0_param.param_str);
2432 current_state->texcoord_w[current_state->current_row++] = reg;
2435 void pshader_glsl_texm3x2tex(SHADER_OPCODE_ARG* arg) {
2436 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2437 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2438 SHADER_BUFFER* buffer = arg->buffer;
2439 glsl_src_param_t src0_param;
2442 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2443 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
2445 shader_glsl_append_dst(buffer, arg);
2446 shader_glsl_get_write_mask(arg->dst, dst_mask);
2448 /* Sample the texture using the calculated coordinates */
2449 shader_addline(buffer, "texture2D(Psampler%u, tmp0.xy)%s);\n", reg, dst_mask);
2452 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
2453 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
2454 void pshader_glsl_texm3x3tex(SHADER_OPCODE_ARG* arg) {
2455 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2456 glsl_src_param_t src0_param;
2458 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2459 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
2460 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
2461 DWORD sampler_type = arg->reg_maps->samplers[reg] & WINED3DSP_TEXTURETYPE_MASK;
2462 glsl_sample_function_t sample_function;
2464 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2465 shader_addline(arg->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
2467 shader_glsl_append_dst(arg->buffer, arg);
2468 shader_glsl_get_write_mask(arg->dst, dst_mask);
2469 /* Dependent read, not valid with conditional NP2 */
2470 shader_glsl_get_sample_function(sampler_type, FALSE, FALSE, &sample_function);
2472 /* Sample the texture using the calculated coordinates */
2473 shader_addline(arg->buffer, "%s(Psampler%u, tmp0.xyz)%s);\n", sample_function.name, reg, dst_mask);
2475 current_state->current_row = 0;
2478 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
2479 * Perform the 3rd row of a 3x3 matrix multiply */
2480 void pshader_glsl_texm3x3(SHADER_OPCODE_ARG* arg) {
2481 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2482 glsl_src_param_t src0_param;
2484 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2485 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
2486 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
2488 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2490 shader_glsl_append_dst(arg->buffer, arg);
2491 shader_glsl_get_write_mask(arg->dst, dst_mask);
2492 shader_addline(arg->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
2494 current_state->current_row = 0;
2497 /** Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
2498 * Peform the final texture lookup based on the previous 2 3x3 matrix multiplies */
2499 void pshader_glsl_texm3x3spec(SHADER_OPCODE_ARG* arg) {
2501 IWineD3DPixelShaderImpl* shader = (IWineD3DPixelShaderImpl*) arg->shader;
2502 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2503 glsl_src_param_t src0_param;
2504 glsl_src_param_t src1_param;
2506 SHADER_BUFFER* buffer = arg->buffer;
2507 SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
2508 DWORD stype = arg->reg_maps->samplers[reg] & WINED3DSP_TEXTURETYPE_MASK;
2509 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2510 glsl_sample_function_t sample_function;
2512 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2513 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], src_mask, &src1_param);
2515 /* Perform the last matrix multiply operation */
2516 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
2517 /* Reflection calculation */
2518 shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
2520 shader_glsl_append_dst(buffer, arg);
2521 shader_glsl_get_write_mask(arg->dst, dst_mask);
2522 /* Dependent read, not valid with conditional NP2 */
2523 shader_glsl_get_sample_function(stype, FALSE, FALSE, &sample_function);
2525 /* Sample the texture */
2526 shader_addline(buffer, "%s(Psampler%u, tmp0.xyz)%s);\n", sample_function.name, reg, dst_mask);
2528 current_state->current_row = 0;
2531 /** Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
2532 * Peform the final texture lookup based on the previous 2 3x3 matrix multiplies */
2533 void pshader_glsl_texm3x3vspec(SHADER_OPCODE_ARG* arg) {
2535 IWineD3DPixelShaderImpl* shader = (IWineD3DPixelShaderImpl*) arg->shader;
2536 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2537 SHADER_BUFFER* buffer = arg->buffer;
2538 SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
2539 glsl_src_param_t src0_param;
2541 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2542 DWORD sampler_type = arg->reg_maps->samplers[reg] & WINED3DSP_TEXTURETYPE_MASK;
2543 glsl_sample_function_t sample_function;
2545 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2547 /* Perform the last matrix multiply operation */
2548 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
2550 /* Construct the eye-ray vector from w coordinates */
2551 shader_addline(buffer, "tmp1.xyz = normalize(vec3(gl_TexCoord[%u].w, gl_TexCoord[%u].w, gl_TexCoord[%u].w));\n",
2552 current_state->texcoord_w[0], current_state->texcoord_w[1], reg);
2553 shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
2555 shader_glsl_append_dst(buffer, arg);
2556 shader_glsl_get_write_mask(arg->dst, dst_mask);
2557 /* Dependent read, not valid with conditional NP2 */
2558 shader_glsl_get_sample_function(sampler_type, FALSE, FALSE, &sample_function);
2560 /* Sample the texture using the calculated coordinates */
2561 shader_addline(buffer, "%s(Psampler%u, tmp0.xyz)%s);\n", sample_function.name, reg, dst_mask);
2563 current_state->current_row = 0;
2566 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
2567 * Apply a fake bump map transform.
2568 * texbem is pshader <= 1.3 only, this saves a few version checks
2570 void pshader_glsl_texbem(SHADER_OPCODE_ARG* arg) {
2571 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
2572 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
2573 char dst_swizzle[6];
2574 glsl_sample_function_t sample_function;
2575 glsl_src_param_t coord_param;
2582 sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2583 flags = deviceImpl->stateBlock->textureState[sampler_idx][WINED3DTSS_TEXTURETRANSFORMFLAGS];
2585 sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
2586 /* Dependent read, not valid with conditional NP2 */
2587 shader_glsl_get_sample_function(sampler_type, FALSE, FALSE, &sample_function);
2588 mask = sample_function.coord_mask;
2590 shader_glsl_get_write_mask(arg->dst, dst_swizzle);
2592 shader_glsl_get_write_mask(mask, coord_mask);
2594 /* with projective textures, texbem only divides the static texture coord, not the displacement,
2595 * so we can't let the GL handle this.
2597 if (flags & WINED3DTTFF_PROJECTED) {
2599 char coord_div_mask[3];
2600 switch (flags & ~WINED3DTTFF_PROJECTED) {
2601 case WINED3DTTFF_COUNT1: FIXME("WINED3DTTFF_PROJECTED with WINED3DTTFF_COUNT1?\n"); break;
2602 case WINED3DTTFF_COUNT2: div_mask = WINED3DSP_WRITEMASK_1; break;
2603 case WINED3DTTFF_COUNT3: div_mask = WINED3DSP_WRITEMASK_2; break;
2604 case WINED3DTTFF_COUNT4:
2605 case WINED3DTTFF_DISABLE: div_mask = WINED3DSP_WRITEMASK_3; break;
2607 shader_glsl_get_write_mask(div_mask, coord_div_mask);
2608 shader_addline(arg->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
2611 shader_glsl_append_dst(arg->buffer, arg);
2612 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0|WINED3DSP_WRITEMASK_1, &coord_param);
2613 if(arg->opcode->opcode == WINED3DSIO_TEXBEML) {
2614 glsl_src_param_t luminance_param;
2615 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_2, &luminance_param);
2616 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",
2617 sample_function.name, sampler_idx, sampler_idx, coord_mask, sampler_idx, coord_param.param_str, coord_mask,
2618 luminance_param.param_str, sampler_idx, sampler_idx, dst_swizzle);
2620 shader_addline(arg->buffer, "%s(Psampler%u, T%u%s + vec4(bumpenvmat%d * %s, 0.0, 0.0)%s )%s);\n",
2621 sample_function.name, sampler_idx, sampler_idx, coord_mask, sampler_idx, coord_param.param_str, coord_mask, dst_swizzle);
2625 void pshader_glsl_bem(SHADER_OPCODE_ARG* arg) {
2626 glsl_src_param_t src0_param, src1_param;
2627 DWORD sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2629 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0|WINED3DSP_WRITEMASK_1, &src0_param);
2630 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0|WINED3DSP_WRITEMASK_1, &src1_param);
2632 shader_glsl_append_dst(arg->buffer, arg);
2633 shader_addline(arg->buffer, "%s + bumpenvmat%d * %s);\n",
2634 src0_param.param_str, sampler_idx, src1_param.param_str);
2637 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
2638 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
2639 void pshader_glsl_texreg2ar(SHADER_OPCODE_ARG* arg) {
2641 glsl_src_param_t src0_param;
2642 DWORD sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2645 shader_glsl_append_dst(arg->buffer, arg);
2646 shader_glsl_get_write_mask(arg->dst, dst_mask);
2647 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
2649 shader_addline(arg->buffer, "texture2D(Psampler%u, %s.wx)%s);\n", sampler_idx, src0_param.reg_name, dst_mask);
2652 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
2653 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
2654 void pshader_glsl_texreg2gb(SHADER_OPCODE_ARG* arg) {
2655 glsl_src_param_t src0_param;
2656 DWORD sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2659 shader_glsl_append_dst(arg->buffer, arg);
2660 shader_glsl_get_write_mask(arg->dst, dst_mask);
2661 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
2663 shader_addline(arg->buffer, "texture2D(Psampler%u, %s.yz)%s);\n", sampler_idx, src0_param.reg_name, dst_mask);
2666 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
2667 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
2668 void pshader_glsl_texreg2rgb(SHADER_OPCODE_ARG* arg) {
2669 glsl_src_param_t src0_param;
2671 DWORD sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2672 DWORD sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
2673 glsl_sample_function_t sample_function;
2675 shader_glsl_append_dst(arg->buffer, arg);
2676 shader_glsl_get_write_mask(arg->dst, dst_mask);
2677 /* Dependent read, not valid with conditional NP2 */
2678 shader_glsl_get_sample_function(sampler_type, FALSE, FALSE, &sample_function);
2679 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], sample_function.coord_mask, &src0_param);
2681 shader_addline(arg->buffer, "%s(Psampler%u, %s)%s);\n", sample_function.name, sampler_idx, src0_param.param_str, dst_mask);
2684 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
2685 * If any of the first 3 components are < 0, discard this pixel */
2686 void pshader_glsl_texkill(SHADER_OPCODE_ARG* arg) {
2687 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
2688 DWORD hex_version = This->baseShader.hex_version;
2689 glsl_dst_param_t dst_param;
2691 /* The argument is a destination parameter, and no writemasks are allowed */
2692 shader_glsl_add_dst_param(arg, arg->dst, 0, &dst_param);
2693 if((hex_version >= WINED3DPS_VERSION(2,0))) {
2694 /* 2.0 shaders compare all 4 components in texkill */
2695 shader_addline(arg->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
2697 /* 1.X shaders only compare the first 3 components, probably due to the nature of the texkill
2698 * instruction as a tex* instruction, and phase, which kills all a / w components. Even if all
2699 * 4 components are defined, only the first 3 are used
2701 shader_addline(arg->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
2705 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
2706 * dst = dot2(src0, src1) + src2 */
2707 void pshader_glsl_dp2add(SHADER_OPCODE_ARG* arg) {
2708 glsl_src_param_t src0_param;
2709 glsl_src_param_t src1_param;
2710 glsl_src_param_t src2_param;
2712 unsigned int mask_size;
2714 write_mask = shader_glsl_append_dst(arg->buffer, arg);
2715 mask_size = shader_glsl_get_write_mask_size(write_mask);
2717 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
2718 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
2719 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], WINED3DSP_WRITEMASK_0, &src2_param);
2721 if (mask_size > 1) {
2722 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);
2724 shader_addline(arg->buffer, "dot(%s, %s) + %s);\n", src0_param.param_str, src1_param.param_str, src2_param.param_str);
2728 void pshader_glsl_input_pack(
2729 SHADER_BUFFER* buffer,
2730 semantic* semantics_in,
2731 IWineD3DPixelShader *iface) {
2734 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *) iface;
2736 for (i = 0; i < MAX_REG_INPUT; i++) {
2738 DWORD usage_token = semantics_in[i].usage;
2739 DWORD register_token = semantics_in[i].reg;
2740 DWORD usage, usage_idx;
2744 if (!usage_token) continue;
2745 usage = (usage_token & WINED3DSP_DCL_USAGE_MASK) >> WINED3DSP_DCL_USAGE_SHIFT;
2746 usage_idx = (usage_token & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
2747 shader_glsl_get_write_mask(register_token, reg_mask);
2751 case WINED3DDECLUSAGE_TEXCOORD:
2752 if(usage_idx < 8 && This->vertexprocessing == pretransformed) {
2753 shader_addline(buffer, "IN[%u]%s = gl_TexCoord[%u]%s;\n",
2754 This->input_reg_map[i], reg_mask, usage_idx, reg_mask);
2756 shader_addline(buffer, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
2757 This->input_reg_map[i], reg_mask, reg_mask);
2761 case WINED3DDECLUSAGE_COLOR:
2763 shader_addline(buffer, "IN[%u]%s = vec4(gl_Color)%s;\n",
2764 This->input_reg_map[i], reg_mask, reg_mask);
2765 else if (usage_idx == 1)
2766 shader_addline(buffer, "IN[%u]%s = vec4(gl_SecondaryColor)%s;\n",
2767 This->input_reg_map[i], reg_mask, reg_mask);
2769 shader_addline(buffer, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
2770 This->input_reg_map[i], reg_mask, reg_mask);
2774 shader_addline(buffer, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
2775 This->input_reg_map[i], reg_mask, reg_mask);
2780 /*********************************************
2781 * Vertex Shader Specific Code begins here
2782 ********************************************/
2784 static void add_glsl_program_entry(IWineD3DDeviceImpl *device, struct glsl_shader_prog_link *entry) {
2785 glsl_program_key_t *key;
2787 key = HeapAlloc(GetProcessHeap(), 0, sizeof(glsl_program_key_t));
2788 key->vshader = entry->vshader;
2789 key->pshader = entry->pshader;
2791 hash_table_put(device->glsl_program_lookup, key, entry);
2794 static struct glsl_shader_prog_link *get_glsl_program_entry(IWineD3DDeviceImpl *device,
2795 GLhandleARB vshader, GLhandleARB pshader) {
2796 glsl_program_key_t key;
2798 key.vshader = vshader;
2799 key.pshader = pshader;
2801 return (struct glsl_shader_prog_link *)hash_table_get(device->glsl_program_lookup, &key);
2804 void delete_glsl_program_entry(IWineD3DDevice *iface, struct glsl_shader_prog_link *entry) {
2805 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
2806 WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
2807 glsl_program_key_t *key;
2809 key = HeapAlloc(GetProcessHeap(), 0, sizeof(glsl_program_key_t));
2810 key->vshader = entry->vshader;
2811 key->pshader = entry->pshader;
2812 hash_table_remove(This->glsl_program_lookup, key);
2814 GL_EXTCALL(glDeleteObjectARB(entry->programId));
2815 if (entry->vshader) list_remove(&entry->vshader_entry);
2816 if (entry->pshader) list_remove(&entry->pshader_entry);
2817 HeapFree(GetProcessHeap(), 0, entry->vuniformF_locations);
2818 HeapFree(GetProcessHeap(), 0, entry->puniformF_locations);
2819 HeapFree(GetProcessHeap(), 0, entry);
2822 static void handle_ps3_input(SHADER_BUFFER *buffer, semantic *semantics_in, semantic *semantics_out, WineD3D_GL_Info *gl_info, DWORD *map) {
2824 DWORD usage_token, usage_token_out;
2825 DWORD register_token, register_token_out;
2826 DWORD usage, usage_idx, usage_out, usage_idx_out;
2829 DWORD in_count = GL_LIMITS(glsl_varyings) / 4;
2830 char reg_mask[6], reg_mask_out[6];
2831 char destination[50];
2833 set = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*set) * (in_count + 2));
2835 if (!semantics_out) {
2836 /* Save gl_FrontColor & gl_FrontSecondaryColor before overwriting them. */
2837 shader_addline(buffer, "vec4 front_color = gl_FrontColor;");
2838 shader_addline(buffer, "vec4 front_secondary_color = gl_FrontSecondaryColor;");
2841 for(i = 0; i < MAX_REG_INPUT; i++) {
2842 usage_token = semantics_in[i].usage;
2843 if (!usage_token) continue;
2846 if (in_idx >= (in_count + 2)) {
2847 FIXME("More input varyings declared than supported, expect issues\n");
2849 } else if(map[i] == -1) {
2850 /* Declared, but not read register */
2854 if (in_idx == in_count) {
2855 sprintf(destination, "gl_FrontColor");
2856 } else if (in_idx == in_count + 1) {
2857 sprintf(destination, "gl_FrontSecondaryColor");
2859 sprintf(destination, "IN[%u]", in_idx);
2862 register_token = semantics_in[i].reg;
2864 usage = (usage_token & WINED3DSP_DCL_USAGE_MASK) >> WINED3DSP_DCL_USAGE_SHIFT;
2865 usage_idx = (usage_token & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
2866 set[map[i]] = shader_glsl_get_write_mask(register_token, reg_mask);
2868 if(!semantics_out) {
2870 case WINED3DDECLUSAGE_COLOR:
2872 shader_addline(buffer, "%s%s = front_color%s;\n",
2873 destination, reg_mask, reg_mask);
2874 else if (usage_idx == 1)
2875 shader_addline(buffer, "%s%s = front_secondary_color%s;\n",
2876 destination, reg_mask, reg_mask);
2878 shader_addline(buffer, "%s%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
2879 destination, reg_mask, reg_mask);
2882 case WINED3DDECLUSAGE_TEXCOORD:
2883 if (usage_idx < 8) {
2884 shader_addline(buffer, "%s%s = gl_TexCoord[%u]%s;\n",
2885 destination, reg_mask, usage_idx, reg_mask);
2887 shader_addline(buffer, "%s%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
2888 destination, reg_mask, reg_mask);
2892 case WINED3DDECLUSAGE_FOG:
2893 shader_addline(buffer, "%s%s = vec4(gl_FogFragCoord, 0.0, 0.0, 0.0)%s;\n",
2894 destination, reg_mask, reg_mask);
2898 shader_addline(buffer, "%s%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
2899 destination, reg_mask, reg_mask);
2903 for(j = 0; j < MAX_REG_OUTPUT; j++) {
2904 usage_token_out = semantics_out[j].usage;
2905 if (!usage_token_out) continue;
2906 register_token_out = semantics_out[j].reg;
2908 usage_out = (usage_token_out & WINED3DSP_DCL_USAGE_MASK) >> WINED3DSP_DCL_USAGE_SHIFT;
2909 usage_idx_out = (usage_token_out & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
2910 shader_glsl_get_write_mask(register_token_out, reg_mask_out);
2912 if(usage == usage_out &&
2913 usage_idx == usage_idx_out) {
2914 shader_addline(buffer, "%s%s = OUT[%u]%s;\n",
2915 destination, reg_mask, j, reg_mask);
2920 shader_addline(buffer, "%s%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
2921 destination, reg_mask, reg_mask);
2926 /* This is solely to make the compiler / linker happy and avoid warning about undefined
2927 * varyings. It shouldn't result in any real code executed on the GPU, since all read
2928 * input varyings are assigned above, if the optimizer works properly.
2930 for(i = 0; i < in_count + 2; i++) {
2931 if(set[i] != WINED3DSP_WRITEMASK_ALL) {
2932 unsigned int size = 0;
2933 memset(reg_mask, 0, sizeof(reg_mask));
2934 if(!(set[i] & WINED3DSP_WRITEMASK_0)) {
2935 reg_mask[size] = 'x';
2938 if(!(set[i] & WINED3DSP_WRITEMASK_1)) {
2939 reg_mask[size] = 'y';
2942 if(!(set[i] & WINED3DSP_WRITEMASK_2)) {
2943 reg_mask[size] = 'z';
2946 if(!(set[i] & WINED3DSP_WRITEMASK_3)) {
2947 reg_mask[size] = 'w';
2951 if (i == in_count) {
2952 sprintf(destination, "gl_FrontColor");
2953 } else if (i == in_count + 1) {
2954 sprintf(destination, "gl_FrontSecondaryColor");
2956 sprintf(destination, "IN[%u]", i);
2960 shader_addline(buffer, "%s.%s = 0.0;\n", destination, reg_mask);
2962 shader_addline(buffer, "%s.%s = vec%u(0.0);\n", destination, reg_mask, size);
2967 HeapFree(GetProcessHeap(), 0, set);
2970 static GLhandleARB generate_param_reorder_function(IWineD3DVertexShader *vertexshader,
2971 IWineD3DPixelShader *pixelshader,
2972 WineD3D_GL_Info *gl_info) {
2973 GLhandleARB ret = 0;
2974 IWineD3DVertexShaderImpl *vs = (IWineD3DVertexShaderImpl *) vertexshader;
2975 IWineD3DPixelShaderImpl *ps = (IWineD3DPixelShaderImpl *) pixelshader;
2976 DWORD vs_major = vs ? WINED3DSHADER_VERSION_MAJOR(vs->baseShader.hex_version) : 0;
2977 DWORD ps_major = ps ? WINED3DSHADER_VERSION_MAJOR(ps->baseShader.hex_version) : 0;
2979 SHADER_BUFFER buffer;
2981 DWORD register_token;
2982 DWORD usage, usage_idx, writemask;
2984 semantic *semantics_out, *semantics_in;
2986 buffer.buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, SHADER_PGMSIZE);
2989 buffer.newline = TRUE;
2991 if(vs_major < 3 && ps_major < 3) {
2992 /* That one is easy: The vertex shader writes to the builtin varyings, the pixel shader reads from them.
2993 * Take care about the texcoord .w fixup though if we're using the fixed function fragment pipeline
2995 if((GLINFO_LOCATION).set_texcoord_w && ps_major == 0 && vs_major > 0) {
2996 shader_addline(&buffer, "void order_ps_input() {\n");
2997 for(i = 0; i < min(8, MAX_REG_TEXCRD); i++) {
2998 if(vs->baseShader.reg_maps.texcoord_mask[i] != 0 &&
2999 vs->baseShader.reg_maps.texcoord_mask[i] != WINED3DSP_WRITEMASK_ALL) {
3000 shader_addline(&buffer, "gl_TexCoord[%u].w = 1.0;\n", i);
3003 shader_addline(&buffer, "}\n");
3005 shader_addline(&buffer, "void order_ps_input() { /* do nothing */ }\n");
3007 } else if(ps_major < 3 && vs_major >= 3) {
3008 /* The vertex shader writes to its own varyings, the pixel shader needs them in the builtin ones */
3009 semantics_out = vs->semantics_out;
3011 shader_addline(&buffer, "void order_ps_input(in vec4 OUT[%u]) {\n", MAX_REG_OUTPUT);
3012 for(i = 0; i < MAX_REG_OUTPUT; i++) {
3013 usage_token = semantics_out[i].usage;
3014 if (!usage_token) continue;
3015 register_token = semantics_out[i].reg;
3017 usage = (usage_token & WINED3DSP_DCL_USAGE_MASK) >> WINED3DSP_DCL_USAGE_SHIFT;
3018 usage_idx = (usage_token & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
3019 writemask = shader_glsl_get_write_mask(register_token, reg_mask);
3022 case WINED3DDECLUSAGE_COLOR:
3024 shader_addline(&buffer, "gl_FrontColor%s = OUT[%u]%s;\n", reg_mask, i, reg_mask);
3025 else if (usage_idx == 1)
3026 shader_addline(&buffer, "gl_FrontSecondaryColor%s = OUT[%u]%s;\n", reg_mask, i, reg_mask);
3029 case WINED3DDECLUSAGE_POSITION:
3030 shader_addline(&buffer, "gl_Position%s = OUT[%u]%s;\n", reg_mask, i, reg_mask);
3033 case WINED3DDECLUSAGE_TEXCOORD:
3034 if (usage_idx < 8) {
3035 if(!(GLINFO_LOCATION).set_texcoord_w || ps_major > 0) writemask |= WINED3DSP_WRITEMASK_3;
3037 shader_addline(&buffer, "gl_TexCoord[%u]%s = OUT[%u]%s;\n",
3038 usage_idx, reg_mask, i, reg_mask);
3039 if(!(writemask & WINED3DSP_WRITEMASK_3)) {
3040 shader_addline(&buffer, "gl_TexCoord[%u].w = 1.0;\n", usage_idx);
3045 case WINED3DDECLUSAGE_PSIZE:
3046 shader_addline(&buffer, "gl_PointSize = OUT[%u].x;\n", i);
3049 case WINED3DDECLUSAGE_FOG:
3050 shader_addline(&buffer, "gl_FogFragCoord = OUT[%u].%c;\n", i, reg_mask[1]);
3057 shader_addline(&buffer, "}\n");
3059 } else if(ps_major >= 3 && vs_major >= 3) {
3060 semantics_out = vs->semantics_out;
3061 semantics_in = ps->semantics_in;
3063 /* This one is tricky: a 3.0 pixel shader reads from a 3.0 vertex shader */
3064 shader_addline(&buffer, "varying vec4 IN[%u];\n", GL_LIMITS(glsl_varyings) / 4);
3065 shader_addline(&buffer, "void order_ps_input(in vec4 OUT[%u]) {\n", MAX_REG_OUTPUT);
3067 /* First, sort out position and point size. Those are not passed to the pixel shader */
3068 for(i = 0; i < MAX_REG_OUTPUT; i++) {
3069 usage_token = semantics_out[i].usage;
3070 if (!usage_token) continue;
3071 register_token = semantics_out[i].reg;
3073 usage = (usage_token & WINED3DSP_DCL_USAGE_MASK) >> WINED3DSP_DCL_USAGE_SHIFT;
3074 usage_idx = (usage_token & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
3075 shader_glsl_get_write_mask(register_token, reg_mask);
3078 case WINED3DDECLUSAGE_POSITION:
3079 shader_addline(&buffer, "gl_Position%s = OUT[%u]%s;\n", reg_mask, i, reg_mask);
3082 case WINED3DDECLUSAGE_PSIZE:
3083 shader_addline(&buffer, "gl_PointSize = OUT[%u].x;\n", i);
3091 /* Then, fix the pixel shader input */
3092 handle_ps3_input(&buffer, semantics_in, semantics_out, gl_info, ps->input_reg_map);
3094 shader_addline(&buffer, "}\n");
3095 } else if(ps_major >= 3 && vs_major < 3) {
3096 semantics_in = ps->semantics_in;
3098 shader_addline(&buffer, "varying vec4 IN[%u];\n", GL_LIMITS(glsl_varyings) / 4);
3099 shader_addline(&buffer, "void order_ps_input() {\n");
3100 /* The vertex shader wrote to the builtin varyings. There is no need to figure out position and
3101 * point size, but we depend on the optimizers kindness to find out that the pixel shader doesn't
3102 * read gl_TexCoord and gl_ColorX, otherwise we'll run out of varyings
3104 handle_ps3_input(&buffer, semantics_in, NULL, gl_info, ps->input_reg_map);
3105 shader_addline(&buffer, "}\n");
3107 ERR("Unexpected vertex and pixel shader version condition: vs: %d, ps: %d\n", vs_major, ps_major);
3110 ret = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
3111 checkGLcall("glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB)");
3112 GL_EXTCALL(glShaderSourceARB(ret, 1, (const char**)&buffer.buffer, NULL));
3113 checkGLcall("glShaderSourceARB(ret, 1, (const char**)&buffer.buffer, NULL)");
3114 GL_EXTCALL(glCompileShaderARB(ret));
3115 checkGLcall("glCompileShaderARB(ret)");
3117 HeapFree(GetProcessHeap(), 0, buffer.buffer);
3121 static void hardcode_local_constants(IWineD3DBaseShaderImpl *shader, WineD3D_GL_Info *gl_info, GLhandleARB programId, char prefix) {
3122 local_constant* lconst;
3127 LIST_FOR_EACH_ENTRY(lconst, &shader->baseShader.constantsF, local_constant, entry) {
3128 value = (float *) lconst->value;
3129 snprintf(glsl_name, sizeof(glsl_name), "%cLC%u", prefix, lconst->idx);
3130 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
3131 GL_EXTCALL(glUniform4fvARB(tmp_loc, 1, value));
3133 checkGLcall("Hardcoding local constants\n");
3136 /** Sets the GLSL program ID for the given pixel and vertex shader combination.
3137 * It sets the programId on the current StateBlock (because it should be called
3138 * inside of the DrawPrimitive() part of the render loop).
3140 * If a program for the given combination does not exist, create one, and store
3141 * the program in the hash table. If it creates a program, it will link the
3142 * given objects, too.
3144 static void set_glsl_shader_program(IWineD3DDevice *iface, BOOL use_ps, BOOL use_vs) {
3145 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
3146 WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
3147 IWineD3DPixelShader *pshader = This->stateBlock->pixelShader;
3148 IWineD3DVertexShader *vshader = This->stateBlock->vertexShader;
3149 struct glsl_shader_prog_link *entry = NULL;
3150 GLhandleARB programId = 0;
3151 GLhandleARB reorder_shader_id = 0;
3155 GLhandleARB vshader_id = use_vs ? ((IWineD3DBaseShaderImpl*)vshader)->baseShader.prgId : 0;
3156 GLhandleARB pshader_id = use_ps ? ((IWineD3DBaseShaderImpl*)pshader)->baseShader.prgId : 0;
3157 entry = get_glsl_program_entry(This, vshader_id, pshader_id);
3159 This->stateBlock->glsl_program = entry;
3163 /* If we get to this point, then no matching program exists, so we create one */
3164 programId = GL_EXTCALL(glCreateProgramObjectARB());
3165 TRACE("Created new GLSL shader program %u\n", programId);
3167 /* Create the entry */
3168 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link));
3169 entry->programId = programId;
3170 entry->vshader = vshader_id;
3171 entry->pshader = pshader_id;
3172 /* Add the hash table entry */
3173 add_glsl_program_entry(This, entry);
3175 /* Set the current program */
3176 This->stateBlock->glsl_program = entry;
3178 /* Attach GLSL vshader */
3180 int max_attribs = 16; /* TODO: Will this always be the case? It is at the moment... */
3183 reorder_shader_id = generate_param_reorder_function(vshader, pshader, gl_info);
3184 TRACE("Attaching GLSL shader object %u to program %u\n", reorder_shader_id, programId);
3185 GL_EXTCALL(glAttachObjectARB(programId, reorder_shader_id));
3186 checkGLcall("glAttachObjectARB");
3187 /* Flag the reorder function for deletion, then it will be freed automatically when the program
3190 GL_EXTCALL(glDeleteObjectARB(reorder_shader_id));
3192 TRACE("Attaching GLSL shader object %u to program %u\n", vshader_id, programId);
3193 GL_EXTCALL(glAttachObjectARB(programId, vshader_id));
3194 checkGLcall("glAttachObjectARB");
3196 /* Bind vertex attributes to a corresponding index number to match
3197 * the same index numbers as ARB_vertex_programs (makes loading
3198 * vertex attributes simpler). With this method, we can use the
3199 * exact same code to load the attributes later for both ARB and
3202 * We have to do this here because we need to know the Program ID
3203 * in order to make the bindings work, and it has to be done prior
3204 * to linking the GLSL program. */
3205 for (i = 0; i < max_attribs; ++i) {
3206 if (((IWineD3DBaseShaderImpl*)vshader)->baseShader.reg_maps.attributes[i]) {
3207 snprintf(tmp_name, sizeof(tmp_name), "attrib%i", i);
3208 GL_EXTCALL(glBindAttribLocationARB(programId, i, tmp_name));
3211 checkGLcall("glBindAttribLocationARB");
3213 list_add_head(&((IWineD3DBaseShaderImpl *)vshader)->baseShader.linked_programs, &entry->vshader_entry);
3216 /* Attach GLSL pshader */
3218 TRACE("Attaching GLSL shader object %u to program %u\n", pshader_id, programId);
3219 GL_EXTCALL(glAttachObjectARB(programId, pshader_id));
3220 checkGLcall("glAttachObjectARB");
3222 list_add_head(&((IWineD3DBaseShaderImpl *)pshader)->baseShader.linked_programs, &entry->pshader_entry);
3225 /* Link the program */
3226 TRACE("Linking GLSL shader program %u\n", programId);
3227 GL_EXTCALL(glLinkProgramARB(programId));
3228 print_glsl_info_log(&GLINFO_LOCATION, programId);
3230 entry->vuniformF_locations = HeapAlloc(GetProcessHeap(), 0, sizeof(GLhandleARB) * GL_LIMITS(vshader_constantsF));
3231 for (i = 0; i < GL_LIMITS(vshader_constantsF); ++i) {
3232 snprintf(glsl_name, sizeof(glsl_name), "VC[%i]", i);
3233 entry->vuniformF_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
3235 for (i = 0; i < MAX_CONST_I; ++i) {
3236 snprintf(glsl_name, sizeof(glsl_name), "VI[%i]", i);
3237 entry->vuniformI_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
3239 entry->puniformF_locations = HeapAlloc(GetProcessHeap(), 0, sizeof(GLhandleARB) * GL_LIMITS(pshader_constantsF));
3240 for (i = 0; i < GL_LIMITS(pshader_constantsF); ++i) {
3241 snprintf(glsl_name, sizeof(glsl_name), "PC[%i]", i);
3242 entry->puniformF_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
3244 for (i = 0; i < MAX_CONST_I; ++i) {
3245 snprintf(glsl_name, sizeof(glsl_name), "PI[%i]", i);
3246 entry->puniformI_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
3250 for(i = 0; i < ((IWineD3DPixelShaderImpl*)pshader)->numbumpenvmatconsts; i++) {
3252 sprintf(name, "bumpenvmat%d", ((IWineD3DPixelShaderImpl*)pshader)->bumpenvmatconst[i].texunit);
3253 entry->bumpenvmat_location[i] = GL_EXTCALL(glGetUniformLocationARB(programId, name));
3254 sprintf(name, "luminancescale%d", ((IWineD3DPixelShaderImpl*)pshader)->luminanceconst[i].texunit);
3255 entry->luminancescale_location[i] = GL_EXTCALL(glGetUniformLocationARB(programId, name));
3256 sprintf(name, "luminanceoffset%d", ((IWineD3DPixelShaderImpl*)pshader)->luminanceconst[i].texunit);
3257 entry->luminanceoffset_location[i] = GL_EXTCALL(glGetUniformLocationARB(programId, name));
3262 entry->posFixup_location = GL_EXTCALL(glGetUniformLocationARB(programId, "posFixup"));
3263 entry->srgb_comparison_location = GL_EXTCALL(glGetUniformLocationARB(programId, "srgb_comparison"));
3264 entry->srgb_mul_low_location = GL_EXTCALL(glGetUniformLocationARB(programId, "srgb_mul_low"));
3265 entry->ycorrection_location = GL_EXTCALL(glGetUniformLocationARB(programId, "ycorrection"));
3266 checkGLcall("Find glsl program uniform locations");
3268 if (pshader && WINED3DSHADER_VERSION_MAJOR(((IWineD3DPixelShaderImpl *)pshader)->baseShader.hex_version) >= 3
3269 && ((IWineD3DPixelShaderImpl *)pshader)->declared_in_count > GL_LIMITS(glsl_varyings) / 4) {
3270 TRACE("Shader %d needs vertex color clamping disabled\n", programId);
3271 entry->vertex_color_clamp = GL_FALSE;
3273 entry->vertex_color_clamp = GL_FIXED_ONLY_ARB;
3276 /* Set the shader to allow uniform loading on it */
3277 GL_EXTCALL(glUseProgramObjectARB(programId));
3278 checkGLcall("glUseProgramObjectARB(programId)");
3280 /* Load the vertex and pixel samplers now. The function that finds the mappings makes sure
3281 * that it stays the same for each vertexshader-pixelshader pair(=linked glsl program). If
3282 * a pshader with fixed function pipeline is used there are no vertex samplers, and if a
3283 * vertex shader with fixed function pixel processing is used we make sure that the card
3284 * supports enough samplers to allow the max number of vertex samplers with all possible
3285 * fixed function fragment processing setups. So once the program is linked these samplers
3289 /* Load vertex shader samplers */
3290 shader_glsl_load_vsamplers(gl_info, (IWineD3DStateBlock*)This->stateBlock, programId);
3293 /* Load pixel shader samplers */
3294 shader_glsl_load_psamplers(gl_info, (IWineD3DStateBlock*)This->stateBlock, programId);
3297 /* If the local constants do not have to be loaded with the environment constants,
3298 * load them now to have them hardcoded in the GLSL program. This saves some CPU cycles
3301 if(pshader && !((IWineD3DPixelShaderImpl*)pshader)->baseShader.load_local_constsF) {
3302 hardcode_local_constants((IWineD3DBaseShaderImpl *) pshader, gl_info, programId, 'P');
3304 if(vshader && !((IWineD3DVertexShaderImpl*)vshader)->baseShader.load_local_constsF) {
3305 hardcode_local_constants((IWineD3DBaseShaderImpl *) vshader, gl_info, programId, 'V');
3309 static GLhandleARB create_glsl_blt_shader(WineD3D_GL_Info *gl_info) {
3310 GLhandleARB program_id;
3311 GLhandleARB vshader_id, pshader_id;
3312 const char *blt_vshader[] = {
3315 " gl_Position = gl_Vertex;\n"
3316 " gl_FrontColor = vec4(1.0);\n"
3317 " gl_TexCoord[0].x = (gl_Vertex.x * 0.5) + 0.5;\n"
3318 " gl_TexCoord[0].y = (-gl_Vertex.y * 0.5) + 0.5;\n"
3322 const char *blt_pshader[] = {
3323 "uniform sampler2D sampler;\n"
3326 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
3330 vshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
3331 GL_EXTCALL(glShaderSourceARB(vshader_id, 1, blt_vshader, NULL));
3332 GL_EXTCALL(glCompileShaderARB(vshader_id));
3334 pshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
3335 GL_EXTCALL(glShaderSourceARB(pshader_id, 1, blt_pshader, NULL));
3336 GL_EXTCALL(glCompileShaderARB(pshader_id));
3338 program_id = GL_EXTCALL(glCreateProgramObjectARB());
3339 GL_EXTCALL(glAttachObjectARB(program_id, vshader_id));
3340 GL_EXTCALL(glAttachObjectARB(program_id, pshader_id));
3341 GL_EXTCALL(glLinkProgramARB(program_id));
3343 print_glsl_info_log(&GLINFO_LOCATION, program_id);
3345 /* Once linked we can mark the shaders for deletion. They will be deleted once the program
3348 GL_EXTCALL(glDeleteObjectARB(vshader_id));
3349 GL_EXTCALL(glDeleteObjectARB(pshader_id));
3353 static void shader_glsl_select(IWineD3DDevice *iface, BOOL usePS, BOOL useVS) {
3354 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
3355 WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
3356 GLhandleARB program_id = 0;
3357 GLenum old_vertex_color_clamp, current_vertex_color_clamp;
3359 old_vertex_color_clamp = This->stateBlock->glsl_program ? This->stateBlock->glsl_program->vertex_color_clamp : GL_FIXED_ONLY_ARB;
3361 if (useVS || usePS) set_glsl_shader_program(iface, usePS, useVS);
3362 else This->stateBlock->glsl_program = NULL;
3364 current_vertex_color_clamp = This->stateBlock->glsl_program ? This->stateBlock->glsl_program->vertex_color_clamp : GL_FIXED_ONLY_ARB;
3366 if (old_vertex_color_clamp != current_vertex_color_clamp) {
3367 if (GL_SUPPORT(ARB_COLOR_BUFFER_FLOAT)) {
3368 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, current_vertex_color_clamp));
3369 checkGLcall("glClampColorARB");
3371 FIXME("vertex color clamp needs to be changed, but extension not supported.\n");
3375 program_id = This->stateBlock->glsl_program ? This->stateBlock->glsl_program->programId : 0;
3376 if (program_id) TRACE("Using GLSL program %u\n", program_id);
3377 GL_EXTCALL(glUseProgramObjectARB(program_id));
3378 checkGLcall("glUseProgramObjectARB");
3381 static void shader_glsl_select_depth_blt(IWineD3DDevice *iface) {
3382 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
3383 WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
3384 struct shader_glsl_priv *priv = (struct shader_glsl_priv *) This->shader_priv;
3385 static GLhandleARB loc = -1;
3387 if (!priv->depth_blt_glsl_program_id) {
3388 priv->depth_blt_glsl_program_id = create_glsl_blt_shader(gl_info);
3389 loc = GL_EXTCALL(glGetUniformLocationARB(priv->depth_blt_glsl_program_id, "sampler"));
3392 GL_EXTCALL(glUseProgramObjectARB(priv->depth_blt_glsl_program_id));
3393 GL_EXTCALL(glUniform1iARB(loc, 0));
3396 static void shader_glsl_destroy_depth_blt(IWineD3DDevice *iface) {
3397 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
3398 struct shader_glsl_priv *priv = (struct shader_glsl_priv *) This->shader_priv;
3399 WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
3401 if(priv->depth_blt_glsl_program_id) {
3402 GL_EXTCALL(glDeleteObjectARB(priv->depth_blt_glsl_program_id));
3403 priv->depth_blt_glsl_program_id = 0;
3407 static void shader_glsl_cleanup(IWineD3DDevice *iface) {
3408 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
3409 WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
3410 GL_EXTCALL(glUseProgramObjectARB(0));
3413 static void shader_glsl_destroy(IWineD3DBaseShader *iface) {
3414 struct list *linked_programs;
3415 IWineD3DBaseShaderImpl *This = (IWineD3DBaseShaderImpl *) iface;
3416 WineD3D_GL_Info *gl_info = &((IWineD3DDeviceImpl *) This->baseShader.device)->adapter->gl_info;
3418 /* Note: Do not use QueryInterface here to find out which shader type this is because this code
3419 * can be called from IWineD3DBaseShader::Release
3421 char pshader = shader_is_pshader_version(This->baseShader.hex_version);
3423 if(This->baseShader.prgId == 0) return;
3424 linked_programs = &This->baseShader.linked_programs;
3426 TRACE("Deleting linked programs\n");
3427 if (linked_programs->next) {
3428 struct glsl_shader_prog_link *entry, *entry2;
3431 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs, struct glsl_shader_prog_link, pshader_entry) {
3432 delete_glsl_program_entry(This->baseShader.device, entry);
3435 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs, struct glsl_shader_prog_link, vshader_entry) {
3436 delete_glsl_program_entry(This->baseShader.device, entry);
3441 TRACE("Deleting shader object %u\n", This->baseShader.prgId);
3442 GL_EXTCALL(glDeleteObjectARB(This->baseShader.prgId));
3443 checkGLcall("glDeleteObjectARB");
3444 This->baseShader.prgId = 0;
3445 This->baseShader.is_compiled = FALSE;
3448 static unsigned int glsl_program_key_hash(void *key) {
3449 glsl_program_key_t *k = (glsl_program_key_t *)key;
3451 unsigned int hash = k->vshader | k->pshader << 16;
3452 hash += ~(hash << 15);
3453 hash ^= (hash >> 10);
3454 hash += (hash << 3);
3455 hash ^= (hash >> 6);
3456 hash += ~(hash << 11);
3457 hash ^= (hash >> 16);
3462 static BOOL glsl_program_key_compare(void *keya, void *keyb) {
3463 glsl_program_key_t *ka = (glsl_program_key_t *)keya;
3464 glsl_program_key_t *kb = (glsl_program_key_t *)keyb;
3466 return ka->vshader == kb->vshader && ka->pshader == kb->pshader;
3469 static HRESULT shader_glsl_alloc(IWineD3DDevice *iface) {
3470 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
3471 This->shader_priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct shader_glsl_priv));
3472 This->glsl_program_lookup = hash_table_create(&glsl_program_key_hash, &glsl_program_key_compare);
3476 static void shader_glsl_free(IWineD3DDevice *iface) {
3477 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
3478 HeapFree(GetProcessHeap(), 0, This->shader_priv);
3479 This->shader_priv = NULL;
3482 static BOOL shader_glsl_dirty_const(IWineD3DDevice *iface) {
3483 /* TODO: GL_EXT_bindable_uniform can be used to share constants across shaders */
3487 static void shader_glsl_generate_pshader(IWineD3DPixelShader *iface, SHADER_BUFFER *buffer) {
3488 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)iface;
3489 shader_reg_maps* reg_maps = &This->baseShader.reg_maps;
3490 CONST DWORD *function = This->baseShader.function;
3491 const char *fragcolor;
3492 WineD3D_GL_Info *gl_info = &((IWineD3DDeviceImpl *)This->baseShader.device)->adapter->gl_info;
3494 /* Create the hw GLSL shader object and assign it as the baseShader.prgId */
3495 GLhandleARB shader_obj = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
3497 if (GL_SUPPORT(ARB_DRAW_BUFFERS)) {
3498 shader_addline(buffer, "#extension GL_ARB_draw_buffers : enable\n");
3500 if (GL_SUPPORT(ARB_TEXTURE_RECTANGLE)) {
3501 /* The spec says that it doesn't have to be explicitly enabled, but the nvidia
3502 * drivers write a warning if we don't do so
3504 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
3507 /* Base Declarations */
3508 shader_generate_glsl_declarations( (IWineD3DBaseShader*) This, reg_maps, buffer, &GLINFO_LOCATION);
3510 /* Pack 3.0 inputs */
3511 if (This->baseShader.hex_version >= WINED3DPS_VERSION(3,0)) {
3513 if(((IWineD3DDeviceImpl *) This->baseShader.device)->strided_streams.u.s.position_transformed) {
3514 This->vertexprocessing = pretransformed;
3515 pshader_glsl_input_pack(buffer, This->semantics_in, iface);
3516 } else if(!use_vs((IWineD3DDeviceImpl *) This->baseShader.device)) {
3517 This->vertexprocessing = fixedfunction;
3518 pshader_glsl_input_pack(buffer, This->semantics_in, iface);
3520 This->vertexprocessing = vertexshader;
3524 /* Base Shader Body */
3525 shader_generate_main( (IWineD3DBaseShader*) This, buffer, reg_maps, function);
3527 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly */
3528 if (This->baseShader.hex_version < WINED3DPS_VERSION(2,0)) {
3529 /* Some older cards like GeforceFX ones don't support multiple buffers, so also not gl_FragData */
3530 if(GL_SUPPORT(ARB_DRAW_BUFFERS))
3531 shader_addline(buffer, "gl_FragData[0] = R0;\n");
3533 shader_addline(buffer, "gl_FragColor = R0;\n");
3536 if(GL_SUPPORT(ARB_DRAW_BUFFERS)) {
3537 fragcolor = "gl_FragData[0]";
3539 fragcolor = "gl_FragColor";
3541 if(This->srgb_enabled) {
3542 shader_addline(buffer, "tmp0.xyz = pow(%s.xyz, vec3(%f, %f, %f)) * vec3(%f, %f, %f) - vec3(%f, %f, %f);\n",
3543 fragcolor, srgb_pow, srgb_pow, srgb_pow, srgb_mul_high, srgb_mul_high, srgb_mul_high,
3544 srgb_sub_high, srgb_sub_high, srgb_sub_high);
3545 shader_addline(buffer, "tmp1.xyz = %s.xyz * srgb_mul_low.xyz;\n", fragcolor);
3546 shader_addline(buffer, "%s.x = %s.x < srgb_comparison.x ? tmp1.x : tmp0.x;\n", fragcolor, fragcolor);
3547 shader_addline(buffer, "%s.y = %s.y < srgb_comparison.y ? tmp1.y : tmp0.y;\n", fragcolor, fragcolor);
3548 shader_addline(buffer, "%s.z = %s.z < srgb_comparison.z ? tmp1.z : tmp0.z;\n", fragcolor, fragcolor);
3549 shader_addline(buffer, "%s = clamp(%s, 0.0, 1.0);\n", fragcolor, fragcolor);
3551 /* Pixel shader < 3.0 do not replace the fog stage.
3552 * This implements linear fog computation and blending.
3553 * TODO: non linear fog
3554 * NOTE: gl_Fog.start and gl_Fog.end don't hold fog start s and end e but
3555 * -1/(e-s) and e/(e-s) respectively.
3557 if(This->baseShader.hex_version < WINED3DPS_VERSION(3,0)) {
3558 shader_addline(buffer, "float Fog = clamp(gl_FogFragCoord * gl_Fog.start + gl_Fog.end, 0.0, 1.0);\n");
3559 shader_addline(buffer, "%s.xyz = mix(gl_Fog.color.xyz, %s.xyz, Fog);\n", fragcolor, fragcolor);
3562 shader_addline(buffer, "}\n");
3564 TRACE("Compiling shader object %u\n", shader_obj);
3565 GL_EXTCALL(glShaderSourceARB(shader_obj, 1, (const char**)&buffer->buffer, NULL));
3566 GL_EXTCALL(glCompileShaderARB(shader_obj));
3567 print_glsl_info_log(&GLINFO_LOCATION, shader_obj);
3569 /* Store the shader object */
3570 This->baseShader.prgId = shader_obj;
3573 static void shader_glsl_generate_vshader(IWineD3DVertexShader *iface, SHADER_BUFFER *buffer) {
3574 IWineD3DVertexShaderImpl *This = (IWineD3DVertexShaderImpl *)iface;
3575 shader_reg_maps* reg_maps = &This->baseShader.reg_maps;
3576 CONST DWORD *function = This->baseShader.function;
3577 WineD3D_GL_Info *gl_info = &((IWineD3DDeviceImpl *)This->baseShader.device)->adapter->gl_info;
3579 /* Create the hw GLSL shader program and assign it as the baseShader.prgId */
3580 GLhandleARB shader_obj = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
3582 /* Base Declarations */
3583 shader_generate_glsl_declarations( (IWineD3DBaseShader*) This, reg_maps, buffer, &GLINFO_LOCATION);
3585 /* Base Shader Body */
3586 shader_generate_main( (IWineD3DBaseShader*) This, buffer, reg_maps, function);
3588 /* Unpack 3.0 outputs */
3589 if (This->baseShader.hex_version >= WINED3DVS_VERSION(3,0)) {
3590 shader_addline(buffer, "order_ps_input(OUT);\n");
3592 shader_addline(buffer, "order_ps_input();\n");
3595 /* If this shader doesn't use fog copy the z coord to the fog coord so that we can use table fog */
3597 shader_addline(buffer, "gl_FogFragCoord = gl_Position.z;\n");
3599 /* Write the final position.
3601 * OpenGL coordinates specify the center of the pixel while d3d coords specify
3602 * the corner. The offsets are stored in z and w in posFixup. posFixup.y contains
3603 * 1.0 or -1.0 to turn the rendering upside down for offscreen rendering. PosFixup.x
3604 * contains 1.0 to allow a mad.
3606 shader_addline(buffer, "gl_Position.y = gl_Position.y * posFixup.y;\n");
3607 shader_addline(buffer, "gl_Position.xy += posFixup.zw * gl_Position.ww;\n");
3609 /* Z coord [0;1]->[-1;1] mapping, see comment in transform_projection in state.c
3611 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However, shaders are run
3612 * before the homogeneous divide, so we have to take the w into account: z = ((z / w) * 2 - 1) * w,
3613 * which is the same as z = z / 2 - w.
3615 shader_addline(buffer, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
3617 shader_addline(buffer, "}\n");
3619 TRACE("Compiling shader object %u\n", shader_obj);
3620 GL_EXTCALL(glShaderSourceARB(shader_obj, 1, (const char**)&buffer->buffer, NULL));
3621 GL_EXTCALL(glCompileShaderARB(shader_obj));
3622 print_glsl_info_log(&GLINFO_LOCATION, shader_obj);
3624 /* Store the shader object */
3625 This->baseShader.prgId = shader_obj;
3628 static void shader_glsl_get_caps(WINED3DDEVTYPE devtype, WineD3D_GL_Info *gl_info, struct shader_caps *pCaps) {
3629 /* We don't have a GLSL fixed function pipeline yet, so let the none backend set its caps,
3630 * then overwrite the shader specific ones
3632 none_shader_backend.shader_get_caps(devtype, gl_info, pCaps);
3634 /* Nvidia Geforce6/7 or Ati R4xx/R5xx cards with GLSL support, support VS 3.0 but older Nvidia/Ati
3635 * models with GLSL support only support 2.0. In case of nvidia we can detect VS 2.0 support using
3636 * vs_nv_version which is based on NV_vertex_program.
3637 * For Ati cards there's no way using glsl (it abstracts the lowlevel info away) and also not
3638 * using ARB_vertex_program. It is safe to assume that when a card supports pixel shader 2.0 it
3639 * supports vertex shader 2.0 too and the way around. We can detect ps2.0 using the maximum number
3640 * of native instructions, so use that here. For more info see the pixel shader versioning code below.
3642 if((GLINFO_LOCATION.vs_nv_version == VS_VERSION_20) || (GLINFO_LOCATION.ps_arb_max_instructions <= 512))
3643 pCaps->VertexShaderVersion = WINED3DVS_VERSION(2,0);
3645 pCaps->VertexShaderVersion = WINED3DVS_VERSION(3,0);
3646 TRACE_(d3d_caps)("Hardware vertex shader version %d.%d enabled (GLSL)\n", (pCaps->VertexShaderVersion >> 8) & 0xff, pCaps->VertexShaderVersion & 0xff);
3647 pCaps->MaxVertexShaderConst = GL_LIMITS(vshader_constantsF);
3649 /* Older DX9-class videocards (GeforceFX / Radeon >9500/X*00) only support pixel shader 2.0/2.0a/2.0b.
3650 * In OpenGL the extensions related to GLSL abstract lowlevel GL info away which is needed
3651 * to distinguish between 2.0 and 3.0 (and 2.0a/2.0b). In case of Nvidia we use their fragment
3652 * program extensions. On other hardware including ATI GL_ARB_fragment_program offers the info
3653 * in max native instructions. Intel and others also offer the info in this extension but they
3654 * don't support GLSL (at least on Windows).
3656 * PS2.0 requires at least 96 instructions, 2.0a/2.0b go up to 512. Assume that if the number
3657 * of instructions is 512 or less we have to do with ps2.0 hardware.
3658 * 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.
3660 if((GLINFO_LOCATION.ps_nv_version == PS_VERSION_20) || (GLINFO_LOCATION.ps_arb_max_instructions <= 512))
3661 pCaps->PixelShaderVersion = WINED3DPS_VERSION(2,0);
3663 pCaps->PixelShaderVersion = WINED3DPS_VERSION(3,0);
3665 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
3666 * Direct3D minimum requirement.
3668 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
3669 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
3671 * The problem is that the refrast clamps temporary results in the shader to
3672 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
3673 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
3674 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
3675 * offer a way to query this.
3677 pCaps->PixelShader1xMaxValue = 8.0;
3678 TRACE_(d3d_caps)("Hardware pixel shader version %d.%d enabled (GLSL)\n", (pCaps->PixelShaderVersion >> 8) & 0xff, pCaps->PixelShaderVersion & 0xff);
3681 static void shader_glsl_load_init(void) {}
3683 static void shader_glsl_fragment_enable(IWineD3DDevice *iface, BOOL enable) {
3684 none_shader_backend.shader_fragment_enable(iface, enable);
3687 const shader_backend_t glsl_shader_backend = {
3688 &shader_glsl_select,
3689 &shader_glsl_select_depth_blt,
3690 &shader_glsl_destroy_depth_blt,
3691 &shader_glsl_load_constants,
3692 &shader_glsl_cleanup,
3693 &shader_glsl_color_correction,
3694 &shader_glsl_destroy,
3697 &shader_glsl_dirty_const,
3698 &shader_glsl_generate_pshader,
3699 &shader_glsl_generate_vshader,
3700 &shader_glsl_get_caps,
3701 &shader_glsl_load_init,
3702 &shader_glsl_fragment_enable,