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 */
74 "WARNING: 0:2: extension 'GL_ARB_draw_buffers' is not supported\n" /* MacOS ati */
77 GL_EXTCALL(glGetObjectParameterivARB(obj,
78 GL_OBJECT_INFO_LOG_LENGTH_ARB,
81 /* A size of 1 is just a null-terminated string, so the log should be bigger than
82 * that if there are errors. */
83 if (infologLength > 1)
85 /* Fglrx doesn't terminate the string properly, but it tells us the proper length.
86 * So use HEAP_ZERO_MEMORY to avoid uninitialized bytes
88 infoLog = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, infologLength);
89 GL_EXTCALL(glGetInfoLogARB(obj, infologLength, NULL, infoLog));
92 for(i = 0; i < sizeof(spam) / sizeof(spam[0]); i++) {
93 if(strcmp(infoLog, spam[i]) == 0) {
99 TRACE("Spam received from GLSL shader #%u: %s\n", obj, debugstr_a(infoLog));
101 FIXME("Error received from GLSL shader #%u: %s\n", obj, debugstr_a(infoLog));
103 HeapFree(GetProcessHeap(), 0, infoLog);
108 * Loads (pixel shader) samplers
110 static void shader_glsl_load_psamplers(
111 WineD3D_GL_Info *gl_info,
112 IWineD3DStateBlock* iface,
113 GLhandleARB programId) {
115 IWineD3DStateBlockImpl* stateBlock = (IWineD3DStateBlockImpl*) iface;
116 GLhandleARB name_loc;
118 char sampler_name[20];
120 for (i = 0; i < MAX_FRAGMENT_SAMPLERS; ++i) {
121 snprintf(sampler_name, sizeof(sampler_name), "Psampler%d", i);
122 name_loc = GL_EXTCALL(glGetUniformLocationARB(programId, sampler_name));
123 if (name_loc != -1) {
124 int mapped_unit = stateBlock->wineD3DDevice->texUnitMap[i];
125 if (mapped_unit != -1 && mapped_unit < GL_LIMITS(fragment_samplers)) {
126 TRACE("Loading %s for texture %d\n", sampler_name, mapped_unit);
127 GL_EXTCALL(glUniform1iARB(name_loc, mapped_unit));
128 checkGLcall("glUniform1iARB");
130 ERR("Trying to load sampler %s on unsupported unit %d\n", sampler_name, mapped_unit);
136 static void shader_glsl_load_vsamplers(WineD3D_GL_Info *gl_info, IWineD3DStateBlock* iface, GLhandleARB programId) {
137 IWineD3DStateBlockImpl* stateBlock = (IWineD3DStateBlockImpl*) iface;
138 GLhandleARB name_loc;
139 char sampler_name[20];
142 for (i = 0; i < MAX_VERTEX_SAMPLERS; ++i) {
143 snprintf(sampler_name, sizeof(sampler_name), "Vsampler%d", i);
144 name_loc = GL_EXTCALL(glGetUniformLocationARB(programId, sampler_name));
145 if (name_loc != -1) {
146 int mapped_unit = stateBlock->wineD3DDevice->texUnitMap[MAX_FRAGMENT_SAMPLERS + i];
147 if (mapped_unit != -1 && mapped_unit < GL_LIMITS(combined_samplers)) {
148 TRACE("Loading %s for texture %d\n", sampler_name, mapped_unit);
149 GL_EXTCALL(glUniform1iARB(name_loc, mapped_unit));
150 checkGLcall("glUniform1iARB");
152 ERR("Trying to load sampler %s on unsupported unit %d\n", sampler_name, mapped_unit);
159 * Loads floating point constants (aka uniforms) into the currently set GLSL program.
160 * When constant_list == NULL, it will load all the constants.
162 static void shader_glsl_load_constantsF(IWineD3DBaseShaderImpl* This, WineD3D_GL_Info *gl_info,
163 unsigned int max_constants, float* constants, GLhandleARB *constant_locations,
164 struct list *constant_list) {
165 constants_entry *constant;
166 local_constant* lconst;
171 if (TRACE_ON(d3d_shader)) {
172 LIST_FOR_EACH_ENTRY(constant, constant_list, constants_entry, entry) {
177 tmp_loc = constant_locations[i];
179 TRACE_(d3d_constants)("Loading constants %i: %f, %f, %f, %f\n", i,
180 constants[i * 4 + 0], constants[i * 4 + 1],
181 constants[i * 4 + 2], constants[i * 4 + 3]);
187 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
188 if(WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) == 1 &&
189 shader_is_pshader_version(This->baseShader.hex_version)) {
192 LIST_FOR_EACH_ENTRY(constant, constant_list, constants_entry, entry) {
197 tmp_loc = constant_locations[i];
199 /* We found this uniform name in the program - go ahead and send the data */
201 if(constants[k + 0] < -1.0) lcl_const[0] = -1.0;
202 else if(constants[k + 0] > 1.0) lcl_const[0] = 1.0;
203 else lcl_const[0] = constants[k + 0];
204 if(constants[k + 1] < -1.0) lcl_const[1] = -1.0;
205 else if(constants[k + 1] > 1.0) lcl_const[1] = 1.0;
206 else lcl_const[1] = constants[k + 1];
207 if(constants[k + 2] < -1.0) lcl_const[2] = -1.0;
208 else if(constants[k + 2] > 1.0) lcl_const[2] = 1.0;
209 else lcl_const[2] = constants[k + 2];
210 if(constants[k + 3] < -1.0) lcl_const[3] = -1.0;
211 else if(constants[k + 3] > 1.0) lcl_const[3] = 1.0;
212 else lcl_const[3] = constants[k + 3];
214 GL_EXTCALL(glUniform4fvARB(tmp_loc, 1, lcl_const));
219 LIST_FOR_EACH_ENTRY(constant, constant_list, constants_entry, entry) {
224 tmp_loc = constant_locations[i];
226 /* We found this uniform name in the program - go ahead and send the data */
227 GL_EXTCALL(glUniform4fvARB(tmp_loc, 1, constants + (i * 4)));
232 checkGLcall("glUniform4fvARB()");
234 if(!This->baseShader.load_local_constsF) {
235 TRACE("No need to load local float constants for this shader\n");
239 /* Load immediate constants */
240 if (TRACE_ON(d3d_shader)) {
241 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
242 tmp_loc = constant_locations[lconst->idx];
244 GLfloat* values = (GLfloat*)lconst->value;
245 TRACE_(d3d_constants)("Loading local constants %i: %f, %f, %f, %f\n", lconst->idx,
246 values[0], values[1], values[2], values[3]);
250 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
251 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
252 tmp_loc = constant_locations[lconst->idx];
254 /* We found this uniform name in the program - go ahead and send the data */
255 GL_EXTCALL(glUniform4fvARB(tmp_loc, 1, (GLfloat*)lconst->value));
258 checkGLcall("glUniform4fvARB()");
262 * Loads integer constants (aka uniforms) into the currently set GLSL program.
263 * When @constants_set == NULL, it will load all the constants.
265 static void shader_glsl_load_constantsI(
266 IWineD3DBaseShaderImpl* This,
267 WineD3D_GL_Info *gl_info,
268 GLhandleARB programId,
269 GLhandleARB locations[MAX_CONST_I],
270 unsigned max_constants,
272 BOOL* constants_set) {
277 for (i=0; i<max_constants; ++i) {
278 if (NULL == constants_set || constants_set[i]) {
280 TRACE_(d3d_constants)("Loading constants %i: %i, %i, %i, %i\n",
281 i, constants[i*4], constants[i*4+1], constants[i*4+2], constants[i*4+3]);
283 /* We found this uniform name in the program - go ahead and send the data */
284 GL_EXTCALL(glUniform4ivARB(locations[i], 1, &constants[i*4]));
285 checkGLcall("glUniform4ivARB");
289 /* Load immediate constants */
290 ptr = list_head(&This->baseShader.constantsI);
292 local_constant* lconst = LIST_ENTRY(ptr, struct local_constant, entry);
293 unsigned int idx = lconst->idx;
294 GLint* values = (GLint*) lconst->value;
296 TRACE_(d3d_constants)("Loading local constants %i: %i, %i, %i, %i\n", idx,
297 values[0], values[1], values[2], values[3]);
299 /* We found this uniform name in the program - go ahead and send the data */
300 GL_EXTCALL(glUniform4ivARB(locations[idx], 1, values));
301 checkGLcall("glUniform4ivARB");
302 ptr = list_next(&This->baseShader.constantsI, ptr);
307 * Loads boolean constants (aka uniforms) into the currently set GLSL program.
308 * When @constants_set == NULL, it will load all the constants.
310 static void shader_glsl_load_constantsB(
311 IWineD3DBaseShaderImpl* This,
312 WineD3D_GL_Info *gl_info,
313 GLhandleARB programId,
314 unsigned max_constants,
316 BOOL* constants_set) {
321 char is_pshader = shader_is_pshader_version(This->baseShader.hex_version);
322 const char* prefix = is_pshader? "PB":"VB";
325 for (i=0; i<max_constants; ++i) {
326 if (NULL == constants_set || constants_set[i]) {
328 TRACE_(d3d_constants)("Loading constants %i: %i;\n", i, constants[i]);
330 /* TODO: Benchmark and see if it would be beneficial to store the
331 * locations of the constants to avoid looking up each time */
332 snprintf(tmp_name, sizeof(tmp_name), "%s[%i]", prefix, i);
333 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
335 /* We found this uniform name in the program - go ahead and send the data */
336 GL_EXTCALL(glUniform1ivARB(tmp_loc, 1, &constants[i]));
337 checkGLcall("glUniform1ivARB");
342 /* Load immediate constants */
343 ptr = list_head(&This->baseShader.constantsB);
345 local_constant* lconst = LIST_ENTRY(ptr, struct local_constant, entry);
346 unsigned int idx = lconst->idx;
347 GLint* values = (GLint*) lconst->value;
349 TRACE_(d3d_constants)("Loading local constants %i: %i\n", idx, values[0]);
351 snprintf(tmp_name, sizeof(tmp_name), "%s[%i]", prefix, idx);
352 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
354 /* We found this uniform name in the program - go ahead and send the data */
355 GL_EXTCALL(glUniform1ivARB(tmp_loc, 1, values));
356 checkGLcall("glUniform1ivARB");
358 ptr = list_next(&This->baseShader.constantsB, ptr);
365 * Loads the app-supplied constants into the currently set GLSL program.
367 void shader_glsl_load_constants(
368 IWineD3DDevice* device,
370 char useVertexShader) {
372 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) device;
373 struct shader_glsl_priv *priv = (struct shader_glsl_priv *)deviceImpl->shader_priv;
374 IWineD3DStateBlockImpl* stateBlock = deviceImpl->stateBlock;
375 WineD3D_GL_Info *gl_info = &deviceImpl->adapter->gl_info;
377 GLhandleARB *constant_locations;
378 struct list *constant_list;
379 GLhandleARB programId;
380 struct glsl_shader_prog_link *prog = priv->glsl_program;
384 /* No GLSL program set - nothing to do. */
387 programId = prog->programId;
389 if (useVertexShader) {
390 IWineD3DBaseShaderImpl* vshader = (IWineD3DBaseShaderImpl*) stateBlock->vertexShader;
392 constant_locations = prog->vuniformF_locations;
393 constant_list = &stateBlock->set_vconstantsF;
395 /* Load DirectX 9 float constants/uniforms for vertex shader */
396 shader_glsl_load_constantsF(vshader, gl_info, GL_LIMITS(vshader_constantsF),
397 stateBlock->vertexShaderConstantF, constant_locations, constant_list);
399 /* Load DirectX 9 integer constants/uniforms for vertex shader */
400 shader_glsl_load_constantsI(vshader, gl_info, programId,
401 prog->vuniformI_locations, MAX_CONST_I,
402 stateBlock->vertexShaderConstantI,
403 stateBlock->changed.vertexShaderConstantsI);
405 /* Load DirectX 9 boolean constants/uniforms for vertex shader */
406 shader_glsl_load_constantsB(vshader, gl_info, programId, MAX_CONST_B,
407 stateBlock->vertexShaderConstantB,
408 stateBlock->changed.vertexShaderConstantsB);
410 /* Upload the position fixup params */
411 GL_EXTCALL(glUniform4fvARB(prog->posFixup_location, 1, &deviceImpl->posFixup[0]));
412 checkGLcall("glUniform4fvARB");
415 if (usePixelShader) {
417 IWineD3DBaseShaderImpl* pshader = (IWineD3DBaseShaderImpl*) stateBlock->pixelShader;
419 constant_locations = prog->puniformF_locations;
420 constant_list = &stateBlock->set_pconstantsF;
422 /* Load DirectX 9 float constants/uniforms for pixel shader */
423 shader_glsl_load_constantsF(pshader, gl_info, GL_LIMITS(pshader_constantsF),
424 stateBlock->pixelShaderConstantF, constant_locations, constant_list);
426 /* Load DirectX 9 integer constants/uniforms for pixel shader */
427 shader_glsl_load_constantsI(pshader, gl_info, programId,
428 prog->puniformI_locations, MAX_CONST_I,
429 stateBlock->pixelShaderConstantI,
430 stateBlock->changed.pixelShaderConstantsI);
432 /* Load DirectX 9 boolean constants/uniforms for pixel shader */
433 shader_glsl_load_constantsB(pshader, gl_info, programId, MAX_CONST_B,
434 stateBlock->pixelShaderConstantB,
435 stateBlock->changed.pixelShaderConstantsB);
437 /* Upload the environment bump map matrix if needed. The needsbumpmat member specifies the texture stage to load the matrix from.
438 * It can't be 0 for a valid texbem instruction.
440 for(i = 0; i < ((IWineD3DPixelShaderImpl *) pshader)->numbumpenvmatconsts; i++) {
441 IWineD3DPixelShaderImpl *ps = (IWineD3DPixelShaderImpl *) pshader;
442 int stage = ps->luminanceconst[i].texunit;
444 float *data = (float *) &stateBlock->textureState[(int) ps->bumpenvmatconst[i].texunit][WINED3DTSS_BUMPENVMAT00];
445 GL_EXTCALL(glUniformMatrix2fvARB(prog->bumpenvmat_location[i], 1, 0, data));
446 checkGLcall("glUniformMatrix2fvARB");
448 /* texbeml needs the luminance scale and offset too. If texbeml is used, needsbumpmat
449 * is set too, so we can check that in the needsbumpmat check
451 if(ps->baseShader.reg_maps.luminanceparams[stage]) {
452 GLfloat *scale = (GLfloat *) &stateBlock->textureState[stage][WINED3DTSS_BUMPENVLSCALE];
453 GLfloat *offset = (GLfloat *) &stateBlock->textureState[stage][WINED3DTSS_BUMPENVLOFFSET];
455 GL_EXTCALL(glUniform1fvARB(prog->luminancescale_location[i], 1, scale));
456 checkGLcall("glUniform1fvARB");
457 GL_EXTCALL(glUniform1fvARB(prog->luminanceoffset_location[i], 1, offset));
458 checkGLcall("glUniform1fvARB");
462 if(((IWineD3DPixelShaderImpl *) pshader)->srgb_enabled &&
463 !((IWineD3DPixelShaderImpl *) pshader)->srgb_mode_hardcoded) {
467 if(stateBlock->renderState[WINED3DRS_SRGBWRITEENABLE]) {
468 comparison[0] = srgb_cmp; comparison[1] = srgb_cmp;
469 comparison[2] = srgb_cmp; comparison[3] = srgb_cmp;
471 mul_low[0] = srgb_mul_low; mul_low[1] = srgb_mul_low;
472 mul_low[2] = srgb_mul_low; mul_low[3] = srgb_mul_low;
474 comparison[0] = 1.0 / 0.0; comparison[1] = 1.0 / 0.0;
475 comparison[2] = 1.0 / 0.0; comparison[3] = 1.0 / 0.0;
477 mul_low[0] = 1.0; mul_low[1] = 1.0;
478 mul_low[2] = 1.0; mul_low[3] = 1.0;
481 GL_EXTCALL(glUniform4fvARB(prog->srgb_comparison_location, 1, comparison));
482 GL_EXTCALL(glUniform4fvARB(prog->srgb_mul_low_location, 1, mul_low));
484 if(((IWineD3DPixelShaderImpl *) pshader)->vpos_uniform) {
485 float correction_params[4];
486 if(deviceImpl->render_offscreen) {
487 correction_params[0] = 0.0;
488 correction_params[1] = 1.0;
490 /* position is window relative, not viewport relative */
491 correction_params[0] = ((IWineD3DSurfaceImpl *) deviceImpl->render_targets[0])->currentDesc.Height;
492 correction_params[1] = -1.0;
494 GL_EXTCALL(glUniform4fvARB(prog->ycorrection_location, 1, correction_params));
499 /** Generate the variable & register declarations for the GLSL output target */
500 void shader_generate_glsl_declarations(
501 IWineD3DBaseShader *iface,
502 shader_reg_maps* reg_maps,
503 SHADER_BUFFER* buffer,
504 WineD3D_GL_Info* gl_info) {
506 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
507 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) This->baseShader.device;
509 unsigned int extra_constants_needed = 0;
510 local_constant* lconst;
512 /* There are some minor differences between pixel and vertex shaders */
513 char pshader = shader_is_pshader_version(This->baseShader.hex_version);
514 char prefix = pshader ? 'P' : 'V';
516 /* Prototype the subroutines */
517 for (i = 0; i < This->baseShader.limits.label; i++) {
518 if (reg_maps->labels[i])
519 shader_addline(buffer, "void subroutine%u();\n", i);
522 /* Declare the constants (aka uniforms) */
523 if (This->baseShader.limits.constant_float > 0) {
524 unsigned max_constantsF = min(This->baseShader.limits.constant_float,
525 (pshader ? GL_LIMITS(pshader_constantsF) : GL_LIMITS(vshader_constantsF)));
526 shader_addline(buffer, "uniform vec4 %cC[%u];\n", prefix, max_constantsF);
529 if (This->baseShader.limits.constant_int > 0)
530 shader_addline(buffer, "uniform ivec4 %cI[%u];\n", prefix, This->baseShader.limits.constant_int);
532 if (This->baseShader.limits.constant_bool > 0)
533 shader_addline(buffer, "uniform bool %cB[%u];\n", prefix, This->baseShader.limits.constant_bool);
536 shader_addline(buffer, "uniform vec4 posFixup;\n");
537 /* Predeclaration; This function is added at link time based on the pixel shader.
538 * VS 3.0 shaders have an array OUT[] the shader writes to, earlier versions don't have
539 * that. We know the input to the reorder function at vertex shader compile time, so
540 * we can deal with that. The reorder function for a 1.x and 2.x vertex shader can just
541 * read gl_FrontColor. The output depends on the pixel shader. The reorder function for a
542 * 1.x and 2.x pshader or for fixed function will write gl_FrontColor, and for a 3.0 shader
543 * it will write to the varying array. Here we depend on the shader optimizer on sorting that
544 * out. The nvidia driver only does that if the parameter is inout instead of out, hence the
547 if(This->baseShader.hex_version >= WINED3DVS_VERSION(3, 0)) {
548 shader_addline(buffer, "void order_ps_input(in vec4[%u]);\n", MAX_REG_OUTPUT);
550 shader_addline(buffer, "void order_ps_input();\n");
553 IWineD3DPixelShaderImpl *ps_impl = (IWineD3DPixelShaderImpl *) This;
555 ps_impl->numbumpenvmatconsts = 0;
556 for(i = 0; i < (sizeof(reg_maps->bumpmat) / sizeof(reg_maps->bumpmat[0])); i++) {
557 if(!reg_maps->bumpmat[i]) {
561 ps_impl->bumpenvmatconst[(int) ps_impl->numbumpenvmatconsts].texunit = i;
562 shader_addline(buffer, "uniform mat2 bumpenvmat%d;\n", i);
564 if(reg_maps->luminanceparams) {
565 ps_impl->luminanceconst[(int) ps_impl->numbumpenvmatconsts].texunit = i;
566 shader_addline(buffer, "uniform float luminancescale%d;\n", i);
567 shader_addline(buffer, "uniform float luminanceoffset%d;\n", i);
568 extra_constants_needed++;
570 ps_impl->luminanceconst[(int) ps_impl->numbumpenvmatconsts].texunit = -1;
573 extra_constants_needed++;
574 ps_impl->numbumpenvmatconsts++;
577 if(device->stateBlock->renderState[WINED3DRS_SRGBWRITEENABLE]) {
578 ps_impl->srgb_enabled = 1;
579 if(This->baseShader.limits.constant_float + extra_constants_needed + 1 < GL_LIMITS(pshader_constantsF)) {
580 shader_addline(buffer, "uniform vec4 srgb_mul_low;\n");
581 shader_addline(buffer, "uniform vec4 srgb_comparison;\n");
582 ps_impl->srgb_mode_hardcoded = 0;
583 extra_constants_needed++;
585 ps_impl->srgb_mode_hardcoded = 1;
586 shader_addline(buffer, "const vec4 srgb_mul_low = vec4(%f, %f, %f, %f);\n",
587 srgb_mul_low, srgb_mul_low, srgb_mul_low, srgb_mul_low);
588 shader_addline(buffer, "const vec4 srgb_comparison = vec4(%f, %f, %f, %f);\n",
589 srgb_cmp, srgb_cmp, srgb_cmp, srgb_cmp);
592 IWineD3DPixelShaderImpl *ps_impl = (IWineD3DPixelShaderImpl *) This;
594 /* Do not write any srgb fixup into the shader to save shader size and processing time.
595 * As a consequence, we can't toggle srgb write on without recompilation
597 ps_impl->srgb_enabled = 0;
598 ps_impl->srgb_mode_hardcoded = 1;
600 if(reg_maps->vpos || reg_maps->usesdsy) {
601 if(This->baseShader.limits.constant_float + extra_constants_needed + 1 < GL_LIMITS(pshader_constantsF)) {
602 shader_addline(buffer, "uniform vec4 ycorrection;\n");
603 ((IWineD3DPixelShaderImpl *) This)->vpos_uniform = 1;
604 extra_constants_needed++;
606 /* This happens because we do not have proper tracking of the constant registers that are
607 * actually used, only the max limit of the shader version
609 FIXME("Cannot find a free uniform for vpos correction params\n");
610 shader_addline(buffer, "const vec4 ycorrection = vec4(%f, %f, 0.0, 0.0);\n",
611 device->render_offscreen ? 0.0 : ((IWineD3DSurfaceImpl *) device->render_targets[0])->currentDesc.Height,
612 device->render_offscreen ? 1.0 : -1.0);
614 shader_addline(buffer, "vec4 vpos;\n");
618 /* Declare texture samplers */
619 for (i = 0; i < This->baseShader.limits.sampler; i++) {
620 if (reg_maps->samplers[i]) {
622 DWORD stype = reg_maps->samplers[i] & WINED3DSP_TEXTURETYPE_MASK;
626 shader_addline(buffer, "uniform sampler1D %csampler%u;\n", prefix, i);
629 if(device->stateBlock->textures[i] &&
630 IWineD3DBaseTexture_GetTextureDimensions(device->stateBlock->textures[i]) == GL_TEXTURE_RECTANGLE_ARB) {
631 shader_addline(buffer, "uniform sampler2DRect %csampler%u;\n", prefix, i);
633 shader_addline(buffer, "uniform sampler2D %csampler%u;\n", prefix, i);
636 case WINED3DSTT_CUBE:
637 shader_addline(buffer, "uniform samplerCube %csampler%u;\n", prefix, i);
639 case WINED3DSTT_VOLUME:
640 shader_addline(buffer, "uniform sampler3D %csampler%u;\n", prefix, i);
643 shader_addline(buffer, "uniform unsupported_sampler %csampler%u;\n", prefix, i);
644 FIXME("Unrecognized sampler type: %#x\n", stype);
650 /* Declare address variables */
651 for (i = 0; i < This->baseShader.limits.address; i++) {
652 if (reg_maps->address[i])
653 shader_addline(buffer, "ivec4 A%d;\n", i);
656 /* Declare texture coordinate temporaries and initialize them */
657 for (i = 0; i < This->baseShader.limits.texcoord; i++) {
658 if (reg_maps->texcoord[i])
659 shader_addline(buffer, "vec4 T%u = gl_TexCoord[%u];\n", i, i);
662 /* Declare input register varyings. Only pixel shader, vertex shaders have that declared in the
663 * helper function shader that is linked in at link time
665 if(pshader && This->baseShader.hex_version >= WINED3DPS_VERSION(3, 0)) {
667 shader_addline(buffer, "varying vec4 IN[%u];\n", GL_LIMITS(glsl_varyings) / 4);
669 /* TODO: Write a replacement shader for the fixed function vertex pipeline, so this isn't needed.
670 * For fixed function vertex processing + 3.0 pixel shader we need a separate function in the
671 * pixel shader that reads the fixed function color into the packed input registers.
673 shader_addline(buffer, "vec4 IN[%u];\n", GL_LIMITS(glsl_varyings) / 4);
677 /* Declare output register temporaries */
678 if(This->baseShader.limits.packed_output) {
679 shader_addline(buffer, "vec4 OUT[%u];\n", This->baseShader.limits.packed_output);
682 /* Declare temporary variables */
683 for(i = 0; i < This->baseShader.limits.temporary; i++) {
684 if (reg_maps->temporary[i])
685 shader_addline(buffer, "vec4 R%u;\n", i);
688 /* Declare attributes */
689 for (i = 0; i < This->baseShader.limits.attributes; i++) {
690 if (reg_maps->attributes[i])
691 shader_addline(buffer, "attribute vec4 attrib%i;\n", i);
694 /* Declare loop registers aLx */
695 for (i = 0; i < reg_maps->loop_depth; i++) {
696 shader_addline(buffer, "int aL%u;\n", i);
697 shader_addline(buffer, "int tmpInt%u;\n", i);
700 /* Temporary variables for matrix operations */
701 shader_addline(buffer, "vec4 tmp0;\n");
702 shader_addline(buffer, "vec4 tmp1;\n");
704 /* Local constants use a different name so they can be loaded once at shader link time
705 * They can't be hardcoded into the shader text via LC = {x, y, z, w}; because the
706 * float -> string conversion can cause precision loss.
708 if(!This->baseShader.load_local_constsF) {
709 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
710 shader_addline(buffer, "uniform vec4 %cLC%u;\n", prefix, lconst->idx);
714 /* Start the main program */
715 shader_addline(buffer, "void main() {\n");
716 if(pshader && reg_maps->vpos) {
717 /* DirectX apps expect integer values, while OpenGL drivers add approximately 0.5. This causes
718 * off-by-one problems as spotted by the vPos d3d9 visual test. Unfortunately the ATI cards do
719 * not add exactly 0.5, but rather something like 0.49999999 or 0.50000001, which still causes
720 * precision troubles when we just substract 0.5.
722 * To deal with that just floor() the position. This will eliminate the fraction on all cards.
724 * TODO: Test how that behaves with multisampling once we can enable multisampling in winex11.
726 * An advantage of floor is that it works even if the driver doesn't add 1/2. It is somewhat
727 * questionable if 1.5, 2.5, ... are the proper values to return in gl_FragCoord, even though
728 * coordinates specify the pixel centers instead of the pixel corners. This code will behave
729 * correctly on drivers that returns integer values.
731 shader_addline(buffer, "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
735 /*****************************************************************************
736 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
738 * For more information, see http://wiki.winehq.org/DirectX-Shaders
739 ****************************************************************************/
742 static void shader_glsl_add_src_param(SHADER_OPCODE_ARG* arg, const DWORD param,
743 const DWORD addr_token, DWORD mask, glsl_src_param_t *src_param);
745 /** Used for opcode modifiers - They multiply the result by the specified amount */
746 static const char * const shift_glsl_tab[] = {
748 "2.0 * ", /* 1 (x2) */
749 "4.0 * ", /* 2 (x4) */
750 "8.0 * ", /* 3 (x8) */
751 "16.0 * ", /* 4 (x16) */
752 "32.0 * ", /* 5 (x32) */
759 "0.0625 * ", /* 12 (d16) */
760 "0.125 * ", /* 13 (d8) */
761 "0.25 * ", /* 14 (d4) */
762 "0.5 * " /* 15 (d2) */
765 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
766 static void shader_glsl_gen_modifier (
769 const char *in_regswizzle,
774 if (instr == WINED3DSIO_TEXKILL)
777 switch (instr & WINED3DSP_SRCMOD_MASK) {
778 case WINED3DSPSM_DZ: /* Need to handle this in the instructions itself (texld & texcrd). */
780 case WINED3DSPSM_NONE:
781 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
783 case WINED3DSPSM_NEG:
784 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
786 case WINED3DSPSM_NOT:
787 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
789 case WINED3DSPSM_BIAS:
790 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
792 case WINED3DSPSM_BIASNEG:
793 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
795 case WINED3DSPSM_SIGN:
796 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
798 case WINED3DSPSM_SIGNNEG:
799 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
801 case WINED3DSPSM_COMP:
802 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
805 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
807 case WINED3DSPSM_X2NEG:
808 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
810 case WINED3DSPSM_ABS:
811 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
813 case WINED3DSPSM_ABSNEG:
814 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
817 FIXME("Unhandled modifier %u\n", (instr & WINED3DSP_SRCMOD_MASK));
818 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
822 /** Writes the GLSL variable name that corresponds to the register that the
823 * DX opcode parameter is trying to access */
824 static void shader_glsl_get_register_name(
826 const DWORD addr_token,
829 SHADER_OPCODE_ARG* arg) {
831 /* oPos, oFog and oPts in D3D */
832 static const char * const hwrastout_reg_names[] = { "gl_Position", "gl_FogFragCoord", "gl_PointSize" };
834 DWORD reg = param & WINED3DSP_REGNUM_MASK;
835 DWORD regtype = shader_get_regtype(param);
836 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) arg->shader;
837 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
838 WineD3D_GL_Info* gl_info = &deviceImpl->adapter->gl_info;
840 char pshader = shader_is_pshader_version(This->baseShader.hex_version);
846 case WINED3DSPR_TEMP:
847 sprintf(tmpStr, "R%u", reg);
849 case WINED3DSPR_INPUT:
851 /* Pixel shaders >= 3.0 */
852 if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 3) {
853 DWORD in_count = GL_LIMITS(glsl_varyings) / 4;
855 if (param & WINED3DSHADER_ADDRMODE_RELATIVE) {
856 glsl_src_param_t rel_param;
857 shader_glsl_add_src_param(arg, addr_token, 0, WINED3DSP_WRITEMASK_0, &rel_param);
859 /* Removing a + 0 would be an obvious optimization, but macos doesn't see the NOP
862 if(((IWineD3DPixelShaderImpl *) This)->input_reg_map[reg]) {
863 if (((IWineD3DPixelShaderImpl *)This)->declared_in_count > in_count) {
864 sprintf(tmpStr, "((%s + %u) > %d ? (%s + %u) > %d ? gl_SecondaryColor : gl_Color : IN[%s + %u])",
865 rel_param.param_str, ((IWineD3DPixelShaderImpl *)This)->input_reg_map[reg], in_count - 1,
866 rel_param.param_str, ((IWineD3DPixelShaderImpl *)This)->input_reg_map[reg], in_count,
867 rel_param.param_str, ((IWineD3DPixelShaderImpl *)This)->input_reg_map[reg]);
869 sprintf(tmpStr, "IN[%s + %u]", rel_param.param_str, ((IWineD3DPixelShaderImpl *)This)->input_reg_map[reg]);
872 if (((IWineD3DPixelShaderImpl *)This)->declared_in_count > in_count) {
873 sprintf(tmpStr, "((%s) > %d ? (%s) > %d ? gl_SecondaryColor : gl_Color : IN[%s])",
874 rel_param.param_str, in_count - 1,
875 rel_param.param_str, in_count,
876 rel_param.param_str);
878 sprintf(tmpStr, "IN[%s]", rel_param.param_str);
882 DWORD idx = ((IWineD3DPixelShaderImpl *) This)->input_reg_map[reg];
883 if (idx == in_count) {
884 sprintf(tmpStr, "gl_Color");
885 } else if (idx == in_count + 1) {
886 sprintf(tmpStr, "gl_SecondaryColor");
888 sprintf(tmpStr, "IN[%u]", idx);
893 strcpy(tmpStr, "gl_Color");
895 strcpy(tmpStr, "gl_SecondaryColor");
898 if (vshader_input_is_color((IWineD3DVertexShader*) This, reg))
900 sprintf(tmpStr, "attrib%u", reg);
903 case WINED3DSPR_CONST:
905 const char prefix = pshader? 'P':'V';
907 /* Relative addressing */
908 if (param & WINED3DSHADER_ADDRMODE_RELATIVE) {
910 /* Relative addressing on shaders 2.0+ have a relative address token,
911 * prior to that, it was hard-coded as "A0.x" because there's only 1 register */
912 if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 2) {
913 glsl_src_param_t rel_param;
914 shader_glsl_add_src_param(arg, addr_token, 0, WINED3DSP_WRITEMASK_0, &rel_param);
916 sprintf(tmpStr, "%cC[%s + %u]", prefix, rel_param.param_str, reg);
918 sprintf(tmpStr, "%cC[%s]", prefix, rel_param.param_str);
922 sprintf(tmpStr, "%cC[A0.x + %u]", prefix, reg);
924 sprintf(tmpStr, "%cC[A0.x]", prefix);
929 if(shader_constant_is_local(This, reg)) {
930 sprintf(tmpStr, "%cLC%u", prefix, reg);
932 sprintf(tmpStr, "%cC[%u]", prefix, reg);
938 case WINED3DSPR_CONSTINT:
940 sprintf(tmpStr, "PI[%u]", reg);
942 sprintf(tmpStr, "VI[%u]", reg);
944 case WINED3DSPR_CONSTBOOL:
946 sprintf(tmpStr, "PB[%u]", reg);
948 sprintf(tmpStr, "VB[%u]", reg);
950 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
952 sprintf(tmpStr, "T%u", reg);
954 sprintf(tmpStr, "A%u", reg);
957 case WINED3DSPR_LOOP:
958 sprintf(tmpStr, "aL%u", This->baseShader.cur_loop_regno - 1);
960 case WINED3DSPR_SAMPLER:
962 sprintf(tmpStr, "Psampler%u", reg);
964 sprintf(tmpStr, "Vsampler%u", reg);
966 case WINED3DSPR_COLOROUT:
967 if (reg >= GL_LIMITS(buffers)) {
968 WARN("Write to render target %u, only %d supported\n", reg, 4);
970 if (GL_SUPPORT(ARB_DRAW_BUFFERS)) {
971 sprintf(tmpStr, "gl_FragData[%u]", reg);
972 } else { /* On older cards with GLSL support like the GeforceFX there's only one buffer. */
973 sprintf(tmpStr, "gl_FragColor");
976 case WINED3DSPR_RASTOUT:
977 sprintf(tmpStr, "%s", hwrastout_reg_names[reg]);
979 case WINED3DSPR_DEPTHOUT:
980 sprintf(tmpStr, "gl_FragDepth");
982 case WINED3DSPR_ATTROUT:
984 sprintf(tmpStr, "gl_FrontColor");
986 sprintf(tmpStr, "gl_FrontSecondaryColor");
989 case WINED3DSPR_TEXCRDOUT:
990 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
991 if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 3)
992 sprintf(tmpStr, "OUT[%u]", reg);
994 sprintf(tmpStr, "gl_TexCoord[%u]", reg);
996 case WINED3DSPR_MISCTYPE:
999 sprintf(tmpStr, "vpos");
1000 } else if (reg == 1){
1001 /* Note that gl_FrontFacing is a bool, while vFace is
1002 * a float for which the sign determines front/back
1004 sprintf(tmpStr, "(gl_FrontFacing ? 1.0 : -1.0)");
1006 FIXME("Unhandled misctype register %d\n", reg);
1007 sprintf(tmpStr, "unrecognized_register");
1011 FIXME("Unhandled register name Type(%d)\n", regtype);
1012 sprintf(tmpStr, "unrecognized_register");
1016 strcat(regstr, tmpStr);
1019 /* Get the GLSL write mask for the destination register */
1020 static DWORD shader_glsl_get_write_mask(const DWORD param, char *write_mask) {
1021 char *ptr = write_mask;
1022 DWORD mask = param & WINED3DSP_WRITEMASK_ALL;
1024 if (shader_is_scalar(param)) {
1025 mask = WINED3DSP_WRITEMASK_0;
1028 if (param & WINED3DSP_WRITEMASK_0) *ptr++ = 'x';
1029 if (param & WINED3DSP_WRITEMASK_1) *ptr++ = 'y';
1030 if (param & WINED3DSP_WRITEMASK_2) *ptr++ = 'z';
1031 if (param & WINED3DSP_WRITEMASK_3) *ptr++ = 'w';
1039 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask) {
1040 unsigned int size = 0;
1042 if (write_mask & WINED3DSP_WRITEMASK_0) ++size;
1043 if (write_mask & WINED3DSP_WRITEMASK_1) ++size;
1044 if (write_mask & WINED3DSP_WRITEMASK_2) ++size;
1045 if (write_mask & WINED3DSP_WRITEMASK_3) ++size;
1050 static void shader_glsl_get_swizzle(const DWORD param, BOOL fixup, DWORD mask, char *swizzle_str) {
1051 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
1052 * but addressed as "rgba". To fix this we need to swap the register's x
1053 * and z components. */
1054 DWORD swizzle = (param & WINED3DSP_SWIZZLE_MASK) >> WINED3DSP_SWIZZLE_SHIFT;
1055 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
1056 char *ptr = swizzle_str;
1058 if (!shader_is_scalar(param)) {
1060 /* swizzle bits fields: wwzzyyxx */
1061 if (mask & WINED3DSP_WRITEMASK_0) *ptr++ = swizzle_chars[swizzle & 0x03];
1062 if (mask & WINED3DSP_WRITEMASK_1) *ptr++ = swizzle_chars[(swizzle >> 2) & 0x03];
1063 if (mask & WINED3DSP_WRITEMASK_2) *ptr++ = swizzle_chars[(swizzle >> 4) & 0x03];
1064 if (mask & WINED3DSP_WRITEMASK_3) *ptr++ = swizzle_chars[(swizzle >> 6) & 0x03];
1070 /* From a given parameter token, generate the corresponding GLSL string.
1071 * Also, return the actual register name and swizzle in case the
1072 * caller needs this information as well. */
1073 static void shader_glsl_add_src_param(SHADER_OPCODE_ARG* arg, const DWORD param,
1074 const DWORD addr_token, DWORD mask, glsl_src_param_t *src_param) {
1075 BOOL is_color = FALSE;
1076 char swizzle_str[6];
1078 src_param->reg_name[0] = '\0';
1079 src_param->param_str[0] = '\0';
1080 swizzle_str[0] = '\0';
1082 shader_glsl_get_register_name(param, addr_token, src_param->reg_name, &is_color, arg);
1084 shader_glsl_get_swizzle(param, is_color, mask, swizzle_str);
1085 shader_glsl_gen_modifier(param, src_param->reg_name, swizzle_str, src_param->param_str);
1088 /* From a given parameter token, generate the corresponding GLSL string.
1089 * Also, return the actual register name and swizzle in case the
1090 * caller needs this information as well. */
1091 static DWORD shader_glsl_add_dst_param(SHADER_OPCODE_ARG* arg, const DWORD param,
1092 const DWORD addr_token, glsl_dst_param_t *dst_param) {
1093 BOOL is_color = FALSE;
1095 dst_param->mask_str[0] = '\0';
1096 dst_param->reg_name[0] = '\0';
1098 shader_glsl_get_register_name(param, addr_token, dst_param->reg_name, &is_color, arg);
1099 return shader_glsl_get_write_mask(param, dst_param->mask_str);
1102 /* Append the destination part of the instruction to the buffer, return the effective write mask */
1103 static DWORD shader_glsl_append_dst_ext(SHADER_BUFFER *buffer, SHADER_OPCODE_ARG *arg, const DWORD param) {
1104 glsl_dst_param_t dst_param;
1108 mask = shader_glsl_add_dst_param(arg, param, arg->dst_addr, &dst_param);
1111 shift = (param & WINED3DSP_DSTSHIFT_MASK) >> WINED3DSP_DSTSHIFT_SHIFT;
1112 shader_addline(buffer, "%s%s = %s(", dst_param.reg_name, dst_param.mask_str, shift_glsl_tab[shift]);
1118 /* Append the destination part of the instruction to the buffer, return the effective write mask */
1119 static DWORD shader_glsl_append_dst(SHADER_BUFFER *buffer, SHADER_OPCODE_ARG *arg) {
1120 return shader_glsl_append_dst_ext(buffer, arg, arg->dst);
1123 /** Process GLSL instruction modifiers */
1124 void shader_glsl_add_instruction_modifiers(SHADER_OPCODE_ARG* arg) {
1126 DWORD mask = arg->dst & WINED3DSP_DSTMOD_MASK;
1128 if (arg->opcode->dst_token && mask != 0) {
1129 glsl_dst_param_t dst_param;
1131 shader_glsl_add_dst_param(arg, arg->dst, 0, &dst_param);
1133 if (mask & WINED3DSPDM_SATURATE) {
1134 /* _SAT means to clamp the value of the register to between 0 and 1 */
1135 shader_addline(arg->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param.reg_name,
1136 dst_param.mask_str, dst_param.reg_name, dst_param.mask_str);
1138 if (mask & WINED3DSPDM_MSAMPCENTROID) {
1139 FIXME("_centroid modifier not handled\n");
1141 if (mask & WINED3DSPDM_PARTIALPRECISION) {
1142 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
1147 static inline const char* shader_get_comp_op(
1148 const DWORD opcode) {
1150 DWORD op = (opcode & INST_CONTROLS_MASK) >> INST_CONTROLS_SHIFT;
1152 case COMPARISON_GT: return ">";
1153 case COMPARISON_EQ: return "==";
1154 case COMPARISON_GE: return ">=";
1155 case COMPARISON_LT: return "<";
1156 case COMPARISON_NE: return "!=";
1157 case COMPARISON_LE: return "<=";
1159 FIXME("Unrecognized comparison value: %u\n", op);
1164 static void shader_glsl_get_sample_function(DWORD sampler_type, BOOL projected, BOOL texrect, glsl_sample_function_t *sample_function) {
1165 /* Note that there's no such thing as a projected cube texture. */
1166 switch(sampler_type) {
1168 sample_function->name = projected ? "texture1DProj" : "texture1D";
1169 sample_function->coord_mask = WINED3DSP_WRITEMASK_0;
1173 sample_function->name = projected ? "texture2DRectProj" : "texture2DRect";
1175 sample_function->name = projected ? "texture2DProj" : "texture2D";
1177 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
1179 case WINED3DSTT_CUBE:
1180 sample_function->name = "textureCube";
1181 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1183 case WINED3DSTT_VOLUME:
1184 sample_function->name = projected ? "texture3DProj" : "texture3D";
1185 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1188 sample_function->name = "";
1189 FIXME("Unrecognized sampler type: %#x;\n", sampler_type);
1194 static void shader_glsl_color_correction(SHADER_OPCODE_ARG* arg) {
1195 IWineD3DBaseShaderImpl* shader = (IWineD3DBaseShaderImpl*) arg->shader;
1196 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) shader->baseShader.device;
1197 WineD3D_GL_Info *gl_info = &deviceImpl->adapter->gl_info;
1198 glsl_dst_param_t dst_param;
1199 glsl_dst_param_t dst_param2;
1201 WINED3DFORMAT conversion_group;
1202 IWineD3DBaseTextureImpl *texture;
1203 DWORD mask, mask_size;
1205 BOOL recorded = FALSE;
1207 DWORD hex_version = shader->baseShader.hex_version;
1209 switch(arg->opcode->opcode) {
1210 case WINED3DSIO_TEX:
1211 if (hex_version < WINED3DPS_VERSION(2,0)) {
1212 sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
1214 sampler_idx = arg->src[1] & WINED3DSP_REGNUM_MASK;
1218 case WINED3DSIO_TEXLDL:
1219 FIXME("Add color fixup for vertex texture WINED3DSIO_TEXLDL\n");
1222 case WINED3DSIO_TEXDP3TEX:
1223 case WINED3DSIO_TEXM3x3TEX:
1224 case WINED3DSIO_TEXM3x3SPEC:
1225 case WINED3DSIO_TEXM3x3VSPEC:
1226 case WINED3DSIO_TEXBEM:
1227 case WINED3DSIO_TEXREG2AR:
1228 case WINED3DSIO_TEXREG2GB:
1229 case WINED3DSIO_TEXREG2RGB:
1230 sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
1234 /* Not a texture sampling instruction, nothing to do */
1238 texture = (IWineD3DBaseTextureImpl *) deviceImpl->stateBlock->textures[sampler_idx];
1240 fmt = texture->resource.format;
1241 conversion_group = texture->baseTexture.shader_conversion_group;
1243 fmt = WINED3DFMT_UNKNOWN;
1244 conversion_group = WINED3DFMT_UNKNOWN;
1247 /* before doing anything, record the sampler with the format in the format conversion list,
1248 * but check if it's not there already
1250 for(i = 0; i < shader->baseShader.num_sampled_samplers; i++) {
1251 if(shader->baseShader.sampled_samplers[i] == sampler_idx) {
1257 shader->baseShader.sampled_samplers[shader->baseShader.num_sampled_samplers] = sampler_idx;
1258 shader->baseShader.num_sampled_samplers++;
1259 shader->baseShader.sampled_format[sampler_idx] = conversion_group;
1263 case WINED3DFMT_V8U8:
1264 case WINED3DFMT_V16U16:
1265 if(GL_SUPPORT(NV_TEXTURE_SHADER) ||
1266 (GL_SUPPORT(ATI_ENVMAP_BUMPMAP) && fmt == WINED3DFMT_V8U8)) {
1267 /* The 3rd channel returns 1.0 in d3d, but 0.0 in gl. Fix this while we're at it :-) */
1268 mask = shader_glsl_add_dst_param(arg, arg->dst, WINED3DSP_WRITEMASK_2, &dst_param);
1269 mask_size = shader_glsl_get_write_mask_size(mask);
1270 if(mask_size >= 3) {
1271 shader_addline(arg->buffer, "%s.%c = 1.0;\n", dst_param.reg_name, dst_param.mask_str[3]);
1274 /* Correct the sign, but leave the blue as it is - it was loaded correctly already */
1275 mask = shader_glsl_add_dst_param(arg, arg->dst,
1276 WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1,
1278 mask_size = shader_glsl_get_write_mask_size(mask);
1279 if(mask_size >= 2) {
1280 shader_addline(arg->buffer, "%s.%c%c = %s.%c%c * 2.0 - 1.0;\n",
1281 dst_param.reg_name, dst_param.mask_str[1], dst_param.mask_str[2],
1282 dst_param.reg_name, dst_param.mask_str[1], dst_param.mask_str[2]);
1283 } else if(mask_size == 1) {
1284 shader_addline(arg->buffer, "%s.%c = %s.%c * 2.0 - 1.0;\n", dst_param.reg_name, dst_param.mask_str[1],
1285 dst_param.reg_name, dst_param.mask_str[1]);
1290 case WINED3DFMT_X8L8V8U8:
1291 if(!GL_SUPPORT(NV_TEXTURE_SHADER)) {
1292 /* Red and blue are the signed channels, fix them up; Blue(=L) is correct already,
1293 * and a(X) is always 1.0
1295 mask = shader_glsl_add_dst_param(arg, arg->dst, WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &dst_param);
1296 mask_size = shader_glsl_get_write_mask_size(mask);
1297 if(mask_size >= 2) {
1298 shader_addline(arg->buffer, "%s.%c%c = %s.%c%c * 2.0 - 1.0;\n",
1299 dst_param.reg_name, dst_param.mask_str[1], dst_param.mask_str[2],
1300 dst_param.reg_name, dst_param.mask_str[1], dst_param.mask_str[2]);
1301 } else if(mask_size == 1) {
1302 shader_addline(arg->buffer, "%s.%c = %s.%c * 2.0 - 1.0;\n",
1303 dst_param.reg_name, dst_param.mask_str[1],
1304 dst_param.reg_name, dst_param.mask_str[1]);
1309 case WINED3DFMT_L6V5U5:
1310 if(!GL_SUPPORT(NV_TEXTURE_SHADER)) {
1311 mask = shader_glsl_add_dst_param(arg, arg->dst, WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &dst_param);
1312 mask_size = shader_glsl_get_write_mask_size(mask);
1313 shader_glsl_add_dst_param(arg, arg->dst, WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_2, &dst_param2);
1314 if(mask_size >= 3) {
1315 /* Swap y and z (U and L), and do a sign conversion on x and the new y(V and U) */
1316 shader_addline(arg->buffer, "tmp0.g = %s.%c;\n",
1317 dst_param.reg_name, dst_param.mask_str[2]);
1318 shader_addline(arg->buffer, "%s.%c%c = %s.%c%c * 2.0 - 1.0;\n",
1319 dst_param.reg_name, dst_param.mask_str[2], dst_param.mask_str[1],
1320 dst_param2.reg_name, dst_param.mask_str[1], dst_param.mask_str[3]);
1321 shader_addline(arg->buffer, "%s.%c = tmp0.g;\n", dst_param.reg_name,
1322 dst_param.mask_str[3]);
1323 } else if(mask_size == 2) {
1324 /* This is bad: We have VL, but we need VU */
1325 FIXME("2 components sampled from a converted L6V5U5 texture\n");
1327 shader_addline(arg->buffer, "%s.%c = %s.%c * 2.0 - 1.0;\n",
1328 dst_param.reg_name, dst_param.mask_str[1],
1329 dst_param2.reg_name, dst_param.mask_str[1]);
1334 case WINED3DFMT_Q8W8V8U8:
1335 if(!GL_SUPPORT(NV_TEXTURE_SHADER)) {
1336 /* Correct the sign in all channels. The writemask just applies as-is, no
1337 * need for checking the mask size
1339 shader_glsl_add_dst_param(arg, arg->dst,
1340 WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 |
1341 WINED3DSP_WRITEMASK_2 | WINED3DSP_WRITEMASK_3,
1343 shader_addline(arg->buffer, "%s%s = %s%s * 2.0 - 1.0;\n", dst_param.reg_name, dst_param.mask_str,
1344 dst_param.reg_name, dst_param.mask_str);
1348 case WINED3DFMT_ATI2N:
1349 /* GL_ATI_texture_compression_3dc returns the two channels as luminance-alpha,
1350 * which means the first one is replicated across .rgb, and the 2nd one is in
1351 * .a. We need the 2nd in .g
1353 * GL_EXT_texture_compression_rgtc returns the values in .rg, however, they
1354 * are swapped compared to d3d. So swap red and green.
1356 mask = shader_glsl_add_dst_param(arg, arg->dst, WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &dst_param);
1357 mask_size = shader_glsl_get_write_mask_size(mask);
1358 if(GL_SUPPORT(EXT_TEXTURE_COMPRESSION_RGTC)) {
1359 if(mask_size >= 2) {
1360 shader_addline(arg->buffer, "%s.%c%c = %s.%c%c;\n",
1361 dst_param.reg_name, dst_param.mask_str[1],
1362 dst_param.mask_str[2],
1363 dst_param.reg_name, dst_param.mask_str[2],
1364 dst_param.mask_str[1]);
1366 FIXME("%u components sampled from a converted ATI2N texture\n", mask_size);
1369 if(mask_size == 4) {
1370 /* Swap y and z (U and L), and do a sign conversion on x and the new y(V and U) */
1371 shader_addline(arg->buffer, "%s.%c = %s.%c;\n",
1372 dst_param.reg_name, dst_param.mask_str[2],
1373 dst_param.reg_name, dst_param.mask_str[4]);
1374 } else if(mask_size == 1) {
1377 FIXME("%u components sampled from a converted ATI2N texture\n", mask_size);
1378 /* This is bad: We have .r[gb], but we need .ra */
1383 /* stupid compiler */
1389 /*****************************************************************************
1391 * Begin processing individual instruction opcodes
1393 ****************************************************************************/
1395 /* Generate GLSL arithmetic functions (dst = src1 + src2) */
1396 void shader_glsl_arith(SHADER_OPCODE_ARG* arg) {
1397 CONST SHADER_OPCODE* curOpcode = arg->opcode;
1398 SHADER_BUFFER* buffer = arg->buffer;
1399 glsl_src_param_t src0_param;
1400 glsl_src_param_t src1_param;
1404 /* Determine the GLSL operator to use based on the opcode */
1405 switch (curOpcode->opcode) {
1406 case WINED3DSIO_MUL: op = '*'; break;
1407 case WINED3DSIO_ADD: op = '+'; break;
1408 case WINED3DSIO_SUB: op = '-'; break;
1411 FIXME("Opcode %s not yet handled in GLSL\n", curOpcode->name);
1415 write_mask = shader_glsl_append_dst(buffer, arg);
1416 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src0_param);
1417 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1418 shader_addline(buffer, "%s %c %s);\n", src0_param.param_str, op, src1_param.param_str);
1421 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
1422 void shader_glsl_mov(SHADER_OPCODE_ARG* arg) {
1423 IWineD3DBaseShaderImpl* shader = (IWineD3DBaseShaderImpl*) arg->shader;
1424 SHADER_BUFFER* buffer = arg->buffer;
1425 glsl_src_param_t src0_param;
1428 write_mask = shader_glsl_append_dst(buffer, arg);
1429 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src0_param);
1431 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
1432 * shader versions WINED3DSIO_MOVA is used for this. */
1433 if ((WINED3DSHADER_VERSION_MAJOR(shader->baseShader.hex_version) == 1 &&
1434 !shader_is_pshader_version(shader->baseShader.hex_version) &&
1435 shader_get_regtype(arg->dst) == WINED3DSPR_ADDR)) {
1436 /* This is a simple floor() */
1437 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
1438 if (mask_size > 1) {
1439 shader_addline(buffer, "ivec%d(floor(%s)));\n", mask_size, src0_param.param_str);
1441 shader_addline(buffer, "int(floor(%s)));\n", src0_param.param_str);
1443 } else if(arg->opcode->opcode == WINED3DSIO_MOVA) {
1444 /* We need to *round* to the nearest int here. */
1445 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
1446 if (mask_size > 1) {
1447 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);
1449 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n", src0_param.param_str, src0_param.param_str);
1452 shader_addline(buffer, "%s);\n", src0_param.param_str);
1456 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
1457 void shader_glsl_dot(SHADER_OPCODE_ARG* arg) {
1458 CONST SHADER_OPCODE* curOpcode = arg->opcode;
1459 SHADER_BUFFER* buffer = arg->buffer;
1460 glsl_src_param_t src0_param;
1461 glsl_src_param_t src1_param;
1462 DWORD dst_write_mask, src_write_mask;
1463 unsigned int dst_size = 0;
1465 dst_write_mask = shader_glsl_append_dst(buffer, arg);
1466 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
1468 /* dp3 works on vec3, dp4 on vec4 */
1469 if (curOpcode->opcode == WINED3DSIO_DP4) {
1470 src_write_mask = WINED3DSP_WRITEMASK_ALL;
1472 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1475 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_write_mask, &src0_param);
1476 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], src_write_mask, &src1_param);
1479 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
1481 shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
1485 /* Note that this instruction has some restrictions. The destination write mask
1486 * can't contain the w component, and the source swizzles have to be .xyzw */
1487 void shader_glsl_cross(SHADER_OPCODE_ARG *arg) {
1488 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1489 glsl_src_param_t src0_param;
1490 glsl_src_param_t src1_param;
1493 shader_glsl_get_write_mask(arg->dst, dst_mask);
1494 shader_glsl_append_dst(arg->buffer, arg);
1495 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
1496 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], src_mask, &src1_param);
1497 shader_addline(arg->buffer, "cross(%s, %s)%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
1500 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
1501 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
1502 * GLSL uses the value as-is. */
1503 void shader_glsl_pow(SHADER_OPCODE_ARG *arg) {
1504 SHADER_BUFFER *buffer = arg->buffer;
1505 glsl_src_param_t src0_param;
1506 glsl_src_param_t src1_param;
1507 DWORD dst_write_mask;
1508 unsigned int dst_size;
1510 dst_write_mask = shader_glsl_append_dst(buffer, arg);
1511 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
1513 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1514 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0, &src1_param);
1517 shader_addline(buffer, "vec%d(pow(abs(%s), %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
1519 shader_addline(buffer, "pow(abs(%s), %s));\n", src0_param.param_str, src1_param.param_str);
1523 /* Process the WINED3DSIO_LOG instruction in GLSL (dst = log2(|src0|))
1524 * Src0 is a scalar. Note that D3D uses the absolute of src0, while
1525 * GLSL uses the value as-is. */
1526 void shader_glsl_log(SHADER_OPCODE_ARG *arg) {
1527 SHADER_BUFFER *buffer = arg->buffer;
1528 glsl_src_param_t src0_param;
1529 DWORD dst_write_mask;
1530 unsigned int dst_size;
1532 dst_write_mask = shader_glsl_append_dst(buffer, arg);
1533 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
1535 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1538 shader_addline(buffer, "vec%d(log2(abs(%s))));\n", dst_size, src0_param.param_str);
1540 shader_addline(buffer, "log2(abs(%s)));\n", src0_param.param_str);
1544 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
1545 void shader_glsl_map2gl(SHADER_OPCODE_ARG* arg) {
1546 CONST SHADER_OPCODE* curOpcode = arg->opcode;
1547 SHADER_BUFFER* buffer = arg->buffer;
1548 glsl_src_param_t src_param;
1549 const char *instruction;
1550 char arguments[256];
1554 /* Determine the GLSL function to use based on the opcode */
1555 /* TODO: Possibly make this a table for faster lookups */
1556 switch (curOpcode->opcode) {
1557 case WINED3DSIO_MIN: instruction = "min"; break;
1558 case WINED3DSIO_MAX: instruction = "max"; break;
1559 case WINED3DSIO_ABS: instruction = "abs"; break;
1560 case WINED3DSIO_FRC: instruction = "fract"; break;
1561 case WINED3DSIO_NRM: instruction = "normalize"; break;
1562 case WINED3DSIO_LOGP:
1563 case WINED3DSIO_LOG: instruction = "log2"; break;
1564 case WINED3DSIO_EXP: instruction = "exp2"; break;
1565 case WINED3DSIO_SGN: instruction = "sign"; break;
1566 case WINED3DSIO_DSX: instruction = "dFdx"; break;
1567 case WINED3DSIO_DSY: instruction = "ycorrection.y * dFdy"; break;
1568 default: instruction = "";
1569 FIXME("Opcode %s not yet handled in GLSL\n", curOpcode->name);
1573 write_mask = shader_glsl_append_dst(buffer, arg);
1575 arguments[0] = '\0';
1576 if (curOpcode->num_params > 0) {
1577 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src_param);
1578 strcat(arguments, src_param.param_str);
1579 for (i = 2; i < curOpcode->num_params; ++i) {
1580 strcat(arguments, ", ");
1581 shader_glsl_add_src_param(arg, arg->src[i-1], arg->src_addr[i-1], write_mask, &src_param);
1582 strcat(arguments, src_param.param_str);
1586 shader_addline(buffer, "%s(%s));\n", instruction, arguments);
1589 /** Process the WINED3DSIO_EXPP instruction in GLSL:
1590 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
1591 * dst.x = 2^(floor(src))
1592 * dst.y = src - floor(src)
1593 * dst.z = 2^src (partial precision is allowed, but optional)
1595 * For 2.0 shaders, just do this (honoring writemask and swizzle):
1596 * dst = 2^src; (partial precision is allowed, but optional)
1598 void shader_glsl_expp(SHADER_OPCODE_ARG* arg) {
1599 IWineD3DBaseShaderImpl *shader = (IWineD3DBaseShaderImpl *)arg->shader;
1600 glsl_src_param_t src_param;
1602 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src_param);
1604 if (shader->baseShader.hex_version < WINED3DPS_VERSION(2,0)) {
1607 shader_addline(arg->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
1608 shader_addline(arg->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
1609 shader_addline(arg->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
1610 shader_addline(arg->buffer, "tmp0.w = 1.0;\n");
1612 shader_glsl_append_dst(arg->buffer, arg);
1613 shader_glsl_get_write_mask(arg->dst, dst_mask);
1614 shader_addline(arg->buffer, "tmp0%s);\n", dst_mask);
1617 unsigned int mask_size;
1619 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1620 mask_size = shader_glsl_get_write_mask_size(write_mask);
1622 if (mask_size > 1) {
1623 shader_addline(arg->buffer, "vec%d(exp2(%s)));\n", mask_size, src_param.param_str);
1625 shader_addline(arg->buffer, "exp2(%s));\n", src_param.param_str);
1630 /** Process the RCP (reciprocal or inverse) opcode in GLSL (dst = 1 / src) */
1631 void shader_glsl_rcp(SHADER_OPCODE_ARG* arg) {
1632 glsl_src_param_t src_param;
1634 unsigned int mask_size;
1636 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1637 mask_size = shader_glsl_get_write_mask_size(write_mask);
1638 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_3, &src_param);
1640 if (mask_size > 1) {
1641 shader_addline(arg->buffer, "vec%d(1.0 / %s));\n", mask_size, src_param.param_str);
1643 shader_addline(arg->buffer, "1.0 / %s);\n", src_param.param_str);
1647 void shader_glsl_rsq(SHADER_OPCODE_ARG* arg) {
1648 SHADER_BUFFER* buffer = arg->buffer;
1649 glsl_src_param_t src_param;
1651 unsigned int mask_size;
1653 write_mask = shader_glsl_append_dst(buffer, arg);
1654 mask_size = shader_glsl_get_write_mask_size(write_mask);
1656 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_3, &src_param);
1658 if (mask_size > 1) {
1659 shader_addline(buffer, "vec%d(inversesqrt(%s)));\n", mask_size, src_param.param_str);
1661 shader_addline(buffer, "inversesqrt(%s));\n", src_param.param_str);
1665 /** Process signed comparison opcodes in GLSL. */
1666 void shader_glsl_compare(SHADER_OPCODE_ARG* arg) {
1667 glsl_src_param_t src0_param;
1668 glsl_src_param_t src1_param;
1670 unsigned int mask_size;
1672 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1673 mask_size = shader_glsl_get_write_mask_size(write_mask);
1674 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src0_param);
1675 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1677 if (mask_size > 1) {
1678 const char *compare;
1680 switch(arg->opcode->opcode) {
1681 case WINED3DSIO_SLT: compare = "lessThan"; break;
1682 case WINED3DSIO_SGE: compare = "greaterThanEqual"; break;
1683 default: compare = "";
1684 FIXME("Can't handle opcode %s\n", arg->opcode->name);
1687 shader_addline(arg->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
1688 src0_param.param_str, src1_param.param_str);
1690 switch(arg->opcode->opcode) {
1691 case WINED3DSIO_SLT:
1692 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
1693 * to return 0.0 but step returns 1.0 because step is not < x
1694 * An alternative is a bvec compare padded with an unused second component.
1695 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
1696 * issue. Playing with not() is not possible either because not() does not accept
1699 shader_addline(arg->buffer, "(%s < %s) ? 1.0 : 0.0);\n", src0_param.param_str, src1_param.param_str);
1701 case WINED3DSIO_SGE:
1702 /* Here we can use the step() function and safe a conditional */
1703 shader_addline(arg->buffer, "step(%s, %s));\n", src1_param.param_str, src0_param.param_str);
1706 FIXME("Can't handle opcode %s\n", arg->opcode->name);
1712 /** Process CMP instruction in GLSL (dst = src0 >= 0.0 ? src1 : src2), per channel */
1713 void shader_glsl_cmp(SHADER_OPCODE_ARG* arg) {
1714 glsl_src_param_t src0_param;
1715 glsl_src_param_t src1_param;
1716 glsl_src_param_t src2_param;
1717 DWORD write_mask, cmp_channel = 0;
1720 BOOL temp_destination = FALSE;
1722 if(shader_is_scalar(arg->src[0])) {
1723 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1725 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
1726 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1727 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, &src2_param);
1729 shader_addline(arg->buffer, "%s >= 0.0 ? %s : %s);\n",
1730 src0_param.param_str, src1_param.param_str, src2_param.param_str);
1732 DWORD src0reg = arg->src[0] & WINED3DSP_REGNUM_MASK;
1733 DWORD src1reg = arg->src[1] & WINED3DSP_REGNUM_MASK;
1734 DWORD src2reg = arg->src[2] & WINED3DSP_REGNUM_MASK;
1735 DWORD src0regtype = shader_get_regtype(arg->src[0]);
1736 DWORD src1regtype = shader_get_regtype(arg->src[1]);
1737 DWORD src2regtype = shader_get_regtype(arg->src[2]);
1738 DWORD dstreg = arg->dst & WINED3DSP_REGNUM_MASK;
1739 DWORD dstregtype = shader_get_regtype(arg->dst);
1741 /* Cycle through all source0 channels */
1742 for (i=0; i<4; i++) {
1744 /* Find the destination channels which use the current source0 channel */
1745 for (j=0; j<4; j++) {
1746 if ( ((arg->src[0] >> (WINED3DSP_SWIZZLE_SHIFT + 2*j)) & 0x3) == i ) {
1747 write_mask |= WINED3DSP_WRITEMASK_0 << j;
1748 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
1752 /* Splitting the cmp instruction up in multiple lines imposes a problem:
1753 * The first lines may overwrite source parameters of the following lines.
1754 * Deal with that by using a temporary destination register if needed
1756 if((src0reg == dstreg && src0regtype == dstregtype) ||
1757 (src1reg == dstreg && src1regtype == dstregtype) ||
1758 (src2reg == dstreg && src2regtype == dstregtype)) {
1760 write_mask = shader_glsl_get_write_mask(arg->dst & (~WINED3DSP_SWIZZLE_MASK | write_mask), mask_char);
1761 if (!write_mask) continue;
1762 shader_addline(arg->buffer, "tmp0%s = (", mask_char);
1763 temp_destination = TRUE;
1765 write_mask = shader_glsl_append_dst_ext(arg->buffer, arg, arg->dst & (~WINED3DSP_SWIZZLE_MASK | write_mask));
1766 if (!write_mask) continue;
1769 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], cmp_channel, &src0_param);
1770 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1771 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, &src2_param);
1773 shader_addline(arg->buffer, "%s >= 0.0 ? %s : %s);\n",
1774 src0_param.param_str, src1_param.param_str, src2_param.param_str);
1777 if(temp_destination) {
1778 shader_glsl_get_write_mask(arg->dst, mask_char);
1779 shader_glsl_append_dst_ext(arg->buffer, arg, arg->dst);
1780 shader_addline(arg->buffer, "tmp0%s);\n", mask_char);
1786 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
1787 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
1788 * the compare is done per component of src0. */
1789 void shader_glsl_cnd(SHADER_OPCODE_ARG* arg) {
1790 IWineD3DBaseShaderImpl* shader = (IWineD3DBaseShaderImpl*) arg->shader;
1791 glsl_src_param_t src0_param;
1792 glsl_src_param_t src1_param;
1793 glsl_src_param_t src2_param;
1794 DWORD write_mask, cmp_channel = 0;
1797 if (shader->baseShader.hex_version < WINED3DPS_VERSION(1, 4)) {
1798 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1799 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1800 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1801 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, &src2_param);
1803 /* Fun: The D3DSI_COISSUE flag changes the semantic of the cnd instruction for < 1.4 shaders */
1804 if(arg->opcode_token & WINED3DSI_COISSUE) {
1805 shader_addline(arg->buffer, "%s /* COISSUE! */);\n", src1_param.param_str);
1807 shader_addline(arg->buffer, "%s > 0.5 ? %s : %s);\n",
1808 src0_param.param_str, src1_param.param_str, src2_param.param_str);
1812 /* Cycle through all source0 channels */
1813 for (i=0; i<4; i++) {
1815 /* Find the destination channels which use the current source0 channel */
1816 for (j=0; j<4; j++) {
1817 if ( ((arg->src[0] >> (WINED3DSP_SWIZZLE_SHIFT + 2*j)) & 0x3) == i ) {
1818 write_mask |= WINED3DSP_WRITEMASK_0 << j;
1819 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
1822 write_mask = shader_glsl_append_dst_ext(arg->buffer, arg, arg->dst & (~WINED3DSP_SWIZZLE_MASK | write_mask));
1823 if (!write_mask) continue;
1825 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], cmp_channel, &src0_param);
1826 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1827 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, &src2_param);
1829 shader_addline(arg->buffer, "%s > 0.5 ? %s : %s);\n",
1830 src0_param.param_str, src1_param.param_str, src2_param.param_str);
1834 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
1835 void shader_glsl_mad(SHADER_OPCODE_ARG* arg) {
1836 glsl_src_param_t src0_param;
1837 glsl_src_param_t src1_param;
1838 glsl_src_param_t src2_param;
1841 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1842 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src0_param);
1843 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1844 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, &src2_param);
1845 shader_addline(arg->buffer, "(%s * %s) + %s);\n",
1846 src0_param.param_str, src1_param.param_str, src2_param.param_str);
1849 /** Handles transforming all WINED3DSIO_M?x? opcodes for
1850 Vertex shaders to GLSL codes */
1851 void shader_glsl_mnxn(SHADER_OPCODE_ARG* arg) {
1853 int nComponents = 0;
1854 SHADER_OPCODE_ARG tmpArg;
1856 memset(&tmpArg, 0, sizeof(SHADER_OPCODE_ARG));
1858 /* Set constants for the temporary argument */
1859 tmpArg.shader = arg->shader;
1860 tmpArg.buffer = arg->buffer;
1861 tmpArg.src[0] = arg->src[0];
1862 tmpArg.src_addr[0] = arg->src_addr[0];
1863 tmpArg.src_addr[1] = arg->src_addr[1];
1864 tmpArg.reg_maps = arg->reg_maps;
1866 switch(arg->opcode->opcode) {
1867 case WINED3DSIO_M4x4:
1869 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP4);
1871 case WINED3DSIO_M4x3:
1873 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP4);
1875 case WINED3DSIO_M3x4:
1877 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP3);
1879 case WINED3DSIO_M3x3:
1881 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP3);
1883 case WINED3DSIO_M3x2:
1885 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP3);
1891 for (i = 0; i < nComponents; i++) {
1892 tmpArg.dst = ((arg->dst) & ~WINED3DSP_WRITEMASK_ALL)|(WINED3DSP_WRITEMASK_0<<i);
1893 tmpArg.src[1] = arg->src[1]+i;
1894 shader_glsl_dot(&tmpArg);
1899 The LRP instruction performs a component-wise linear interpolation
1900 between the second and third operands using the first operand as the
1901 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
1902 This is equivalent to mix(src2, src1, src0);
1904 void shader_glsl_lrp(SHADER_OPCODE_ARG* arg) {
1905 glsl_src_param_t src0_param;
1906 glsl_src_param_t src1_param;
1907 glsl_src_param_t src2_param;
1910 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1912 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src0_param);
1913 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1914 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, &src2_param);
1916 shader_addline(arg->buffer, "mix(%s, %s, %s));\n",
1917 src2_param.param_str, src1_param.param_str, src0_param.param_str);
1920 /** Process the WINED3DSIO_LIT instruction in GLSL:
1921 * dst.x = dst.w = 1.0
1922 * dst.y = (src0.x > 0) ? src0.x
1923 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
1924 * where src.w is clamped at +- 128
1926 void shader_glsl_lit(SHADER_OPCODE_ARG* arg) {
1927 glsl_src_param_t src0_param;
1928 glsl_src_param_t src1_param;
1929 glsl_src_param_t src3_param;
1932 shader_glsl_append_dst(arg->buffer, arg);
1933 shader_glsl_get_write_mask(arg->dst, dst_mask);
1935 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1936 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_1, &src1_param);
1937 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_3, &src3_param);
1939 /* The sdk specifies the instruction like this
1941 * if(src.x > 0.0) dst.y = src.x
1943 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
1947 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
1948 * dst.x = 1.0 ... No further explanation needed
1949 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
1950 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
1951 * dst.w = 1.0. ... Nothing fancy.
1953 * So we still have one conditional in there. So do this:
1954 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
1956 * 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),
1957 * 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.
1958 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to
1960 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",
1961 src0_param.param_str, src1_param.param_str, src0_param.param_str, src3_param.param_str, dst_mask);
1964 /** Process the WINED3DSIO_DST instruction in GLSL:
1966 * dst.y = src0.x * src0.y
1970 void shader_glsl_dst(SHADER_OPCODE_ARG* arg) {
1971 glsl_src_param_t src0y_param;
1972 glsl_src_param_t src0z_param;
1973 glsl_src_param_t src1y_param;
1974 glsl_src_param_t src1w_param;
1977 shader_glsl_append_dst(arg->buffer, arg);
1978 shader_glsl_get_write_mask(arg->dst, dst_mask);
1980 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_1, &src0y_param);
1981 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_2, &src0z_param);
1982 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_1, &src1y_param);
1983 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_3, &src1w_param);
1985 shader_addline(arg->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
1986 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
1989 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
1990 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
1991 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
1993 * dst.x = cos(src0.?)
1994 * dst.y = sin(src0.?)
1998 void shader_glsl_sincos(SHADER_OPCODE_ARG* arg) {
1999 glsl_src_param_t src0_param;
2002 write_mask = shader_glsl_append_dst(arg->buffer, arg);
2003 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
2005 switch (write_mask) {
2006 case WINED3DSP_WRITEMASK_0:
2007 shader_addline(arg->buffer, "cos(%s));\n", src0_param.param_str);
2010 case WINED3DSP_WRITEMASK_1:
2011 shader_addline(arg->buffer, "sin(%s));\n", src0_param.param_str);
2014 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
2015 shader_addline(arg->buffer, "vec2(cos(%s), sin(%s)));\n", src0_param.param_str, src0_param.param_str);
2019 ERR("Write mask should be .x, .y or .xy\n");
2024 /** Process the WINED3DSIO_LOOP instruction in GLSL:
2025 * Start a for() loop where src1.y is the initial value of aL,
2026 * increment aL by src1.z for a total of src1.x iterations.
2027 * Need to use a temporary variable for this operation.
2029 /* FIXME: I don't think nested loops will work correctly this way. */
2030 void shader_glsl_loop(SHADER_OPCODE_ARG* arg) {
2031 glsl_src_param_t src1_param;
2032 IWineD3DBaseShaderImpl* shader = (IWineD3DBaseShaderImpl*) arg->shader;
2033 DWORD regtype = shader_get_regtype(arg->src[1]);
2034 DWORD reg = arg->src[1] & WINED3DSP_REGNUM_MASK;
2035 const DWORD *control_values = NULL;
2036 local_constant *constant;
2038 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_ALL, &src1_param);
2040 /* Try to hardcode the loop control parameters if possible. Direct3D 9 class hardware doesn't support real
2041 * varying indexing, but Microsoft designed this feature for Shader model 2.x+. If the loop control is
2042 * known at compile time, the GLSL compiler can unroll the loop, and replace indirect addressing with direct
2045 if(regtype == WINED3DSPR_CONSTINT) {
2046 LIST_FOR_EACH_ENTRY(constant, &shader->baseShader.constantsI, local_constant, entry) {
2047 if(constant->idx == reg) {
2048 control_values = constant->value;
2054 if(control_values) {
2055 if(control_values[2] > 0) {
2056 shader_addline(arg->buffer, "for (aL%u = %d; aL%u < (%d * %d + %d); aL%u += %d) {\n",
2057 shader->baseShader.cur_loop_depth, control_values[1],
2058 shader->baseShader.cur_loop_depth, control_values[0], control_values[2], control_values[1],
2059 shader->baseShader.cur_loop_depth, control_values[2]);
2060 } else if(control_values[2] == 0) {
2061 shader_addline(arg->buffer, "for (aL%u = %d, tmpInt%u = 0; tmpInt%u < %d; tmpInt%u++) {\n",
2062 shader->baseShader.cur_loop_depth, control_values[1], shader->baseShader.cur_loop_depth,
2063 shader->baseShader.cur_loop_depth, control_values[0],
2064 shader->baseShader.cur_loop_depth);
2066 shader_addline(arg->buffer, "for (aL%u = %d; aL%u > (%d * %d + %d); aL%u += %d) {\n",
2067 shader->baseShader.cur_loop_depth, control_values[1],
2068 shader->baseShader.cur_loop_depth, control_values[0], control_values[2], control_values[1],
2069 shader->baseShader.cur_loop_depth, control_values[2]);
2072 shader_addline(arg->buffer, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z) {\n",
2073 shader->baseShader.cur_loop_depth, shader->baseShader.cur_loop_regno,
2074 src1_param.reg_name, shader->baseShader.cur_loop_depth, src1_param.reg_name,
2075 shader->baseShader.cur_loop_depth, shader->baseShader.cur_loop_regno, src1_param.reg_name);
2078 shader->baseShader.cur_loop_depth++;
2079 shader->baseShader.cur_loop_regno++;
2082 void shader_glsl_end(SHADER_OPCODE_ARG* arg) {
2083 IWineD3DBaseShaderImpl* shader = (IWineD3DBaseShaderImpl*) arg->shader;
2085 shader_addline(arg->buffer, "}\n");
2087 if(arg->opcode->opcode == WINED3DSIO_ENDLOOP) {
2088 shader->baseShader.cur_loop_depth--;
2089 shader->baseShader.cur_loop_regno--;
2091 if(arg->opcode->opcode == WINED3DSIO_ENDREP) {
2092 shader->baseShader.cur_loop_depth--;
2096 void shader_glsl_rep(SHADER_OPCODE_ARG* arg) {
2097 IWineD3DBaseShaderImpl* shader = (IWineD3DBaseShaderImpl*) arg->shader;
2098 glsl_src_param_t src0_param;
2100 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
2101 shader_addline(arg->buffer, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
2102 shader->baseShader.cur_loop_depth, shader->baseShader.cur_loop_depth,
2103 src0_param.param_str, shader->baseShader.cur_loop_depth);
2104 shader->baseShader.cur_loop_depth++;
2107 void shader_glsl_if(SHADER_OPCODE_ARG* arg) {
2108 glsl_src_param_t src0_param;
2110 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
2111 shader_addline(arg->buffer, "if (%s) {\n", src0_param.param_str);
2114 void shader_glsl_ifc(SHADER_OPCODE_ARG* arg) {
2115 glsl_src_param_t src0_param;
2116 glsl_src_param_t src1_param;
2118 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
2119 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0, &src1_param);
2121 shader_addline(arg->buffer, "if (%s %s %s) {\n",
2122 src0_param.param_str, shader_get_comp_op(arg->opcode_token), src1_param.param_str);
2125 void shader_glsl_else(SHADER_OPCODE_ARG* arg) {
2126 shader_addline(arg->buffer, "} else {\n");
2129 void shader_glsl_break(SHADER_OPCODE_ARG* arg) {
2130 shader_addline(arg->buffer, "break;\n");
2133 /* FIXME: According to MSDN the compare is done per component. */
2134 void shader_glsl_breakc(SHADER_OPCODE_ARG* arg) {
2135 glsl_src_param_t src0_param;
2136 glsl_src_param_t src1_param;
2138 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
2139 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0, &src1_param);
2141 shader_addline(arg->buffer, "if (%s %s %s) break;\n",
2142 src0_param.param_str, shader_get_comp_op(arg->opcode_token), src1_param.param_str);
2145 void shader_glsl_label(SHADER_OPCODE_ARG* arg) {
2147 DWORD snum = (arg->src[0]) & WINED3DSP_REGNUM_MASK;
2148 shader_addline(arg->buffer, "}\n");
2149 shader_addline(arg->buffer, "void subroutine%u () {\n", snum);
2152 void shader_glsl_call(SHADER_OPCODE_ARG* arg) {
2153 DWORD snum = (arg->src[0]) & WINED3DSP_REGNUM_MASK;
2154 shader_addline(arg->buffer, "subroutine%u();\n", snum);
2157 void shader_glsl_callnz(SHADER_OPCODE_ARG* arg) {
2158 glsl_src_param_t src1_param;
2160 DWORD snum = (arg->src[0]) & WINED3DSP_REGNUM_MASK;
2161 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0, &src1_param);
2162 shader_addline(arg->buffer, "if (%s) subroutine%u();\n", src1_param.param_str, snum);
2165 /*********************************************
2166 * Pixel Shader Specific Code begins here
2167 ********************************************/
2168 void pshader_glsl_tex(SHADER_OPCODE_ARG* arg) {
2169 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
2170 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
2171 DWORD hex_version = This->baseShader.hex_version;
2172 char dst_swizzle[6];
2173 glsl_sample_function_t sample_function;
2176 BOOL projected, texrect = FALSE;
2179 /* All versions have a destination register */
2180 shader_glsl_append_dst(arg->buffer, arg);
2182 /* 1.0-1.4: Use destination register as sampler source.
2183 * 2.0+: Use provided sampler source. */
2184 if (hex_version < WINED3DPS_VERSION(1,4)) {
2187 sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2188 flags = deviceImpl->stateBlock->textureState[sampler_idx][WINED3DTSS_TEXTURETRANSFORMFLAGS];
2190 if (flags & WINED3DTTFF_PROJECTED) {
2192 switch (flags & ~WINED3DTTFF_PROJECTED) {
2193 case WINED3DTTFF_COUNT1: FIXME("WINED3DTTFF_PROJECTED with WINED3DTTFF_COUNT1?\n"); break;
2194 case WINED3DTTFF_COUNT2: mask = WINED3DSP_WRITEMASK_1; break;
2195 case WINED3DTTFF_COUNT3: mask = WINED3DSP_WRITEMASK_2; break;
2196 case WINED3DTTFF_COUNT4:
2197 case WINED3DTTFF_DISABLE: mask = WINED3DSP_WRITEMASK_3; break;
2202 } else if (hex_version < WINED3DPS_VERSION(2,0)) {
2203 DWORD src_mod = arg->src[0] & WINED3DSP_SRCMOD_MASK;
2204 sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2206 if (src_mod == WINED3DSPSM_DZ) {
2208 mask = WINED3DSP_WRITEMASK_2;
2209 } else if (src_mod == WINED3DSPSM_DW) {
2211 mask = WINED3DSP_WRITEMASK_3;
2216 sampler_idx = arg->src[1] & WINED3DSP_REGNUM_MASK;
2217 if(arg->opcode_token & WINED3DSI_TEXLD_PROJECT) {
2218 /* ps 2.0 texldp instruction always divides by the fourth component. */
2220 mask = WINED3DSP_WRITEMASK_3;
2226 if(deviceImpl->stateBlock->textures[sampler_idx] &&
2227 IWineD3DBaseTexture_GetTextureDimensions(deviceImpl->stateBlock->textures[sampler_idx]) == GL_TEXTURE_RECTANGLE_ARB) {
2231 sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
2232 shader_glsl_get_sample_function(sampler_type, projected, texrect, &sample_function);
2233 mask |= sample_function.coord_mask;
2235 if (hex_version < WINED3DPS_VERSION(2,0)) {
2236 shader_glsl_get_write_mask(arg->dst, dst_swizzle);
2238 shader_glsl_get_swizzle(arg->src[1], FALSE, arg->dst, dst_swizzle);
2241 /* 1.0-1.3: Use destination register as coordinate source.
2242 1.4+: Use provided coordinate source register. */
2243 if (hex_version < WINED3DPS_VERSION(1,4)) {
2245 shader_glsl_get_write_mask(mask, coord_mask);
2246 shader_addline(arg->buffer, "%s(Psampler%u, T%u%s)%s);\n",
2247 sample_function.name, sampler_idx, sampler_idx, coord_mask, dst_swizzle);
2249 glsl_src_param_t coord_param;
2250 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], mask, &coord_param);
2251 if(arg->opcode_token & WINED3DSI_TEXLD_BIAS) {
2252 glsl_src_param_t bias;
2253 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_3, &bias);
2255 shader_addline(arg->buffer, "%s(Psampler%u, %s, %s)%s);\n",
2256 sample_function.name, sampler_idx, coord_param.param_str,
2257 bias.param_str, dst_swizzle);
2259 shader_addline(arg->buffer, "%s(Psampler%u, %s)%s);\n",
2260 sample_function.name, sampler_idx, coord_param.param_str, dst_swizzle);
2265 void shader_glsl_texldl(SHADER_OPCODE_ARG* arg) {
2266 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*)arg->shader;
2267 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
2268 glsl_sample_function_t sample_function;
2269 glsl_src_param_t coord_param, lod_param;
2270 char dst_swizzle[6];
2273 BOOL texrect = FALSE;
2275 shader_glsl_append_dst(arg->buffer, arg);
2276 shader_glsl_get_swizzle(arg->src[1], FALSE, arg->dst, dst_swizzle);
2278 sampler_idx = arg->src[1] & WINED3DSP_REGNUM_MASK;
2279 sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
2280 if(deviceImpl->stateBlock->textures[sampler_idx] &&
2281 IWineD3DBaseTexture_GetTextureDimensions(deviceImpl->stateBlock->textures[sampler_idx]) == GL_TEXTURE_RECTANGLE_ARB) {
2284 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);
2286 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_3, &lod_param);
2288 if (shader_is_pshader_version(This->baseShader.hex_version)) {
2289 /* The GLSL spec claims the Lod sampling functions are only supported in vertex shaders.
2290 * However, they seem to work just fine in fragment shaders as well. */
2291 WARN("Using %sLod in fragment shader.\n", sample_function.name);
2292 shader_addline(arg->buffer, "%sLod(Psampler%u, %s, %s)%s);\n",
2293 sample_function.name, sampler_idx, coord_param.param_str, lod_param.param_str, dst_swizzle);
2295 shader_addline(arg->buffer, "%sLod(Vsampler%u, %s, %s)%s);\n",
2296 sample_function.name, sampler_idx, coord_param.param_str, lod_param.param_str, dst_swizzle);
2300 void pshader_glsl_texcoord(SHADER_OPCODE_ARG* arg) {
2302 /* FIXME: Make this work for more than just 2D textures */
2304 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
2305 SHADER_BUFFER* buffer = arg->buffer;
2306 DWORD hex_version = This->baseShader.hex_version;
2310 write_mask = shader_glsl_append_dst(arg->buffer, arg);
2311 shader_glsl_get_write_mask(write_mask, dst_mask);
2313 if (hex_version != WINED3DPS_VERSION(1,4)) {
2314 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2315 shader_addline(buffer, "clamp(gl_TexCoord[%u], 0.0, 1.0)%s);\n", reg, dst_mask);
2317 DWORD reg = arg->src[0] & WINED3DSP_REGNUM_MASK;
2318 DWORD src_mod = arg->src[0] & WINED3DSP_SRCMOD_MASK;
2319 char dst_swizzle[6];
2321 shader_glsl_get_swizzle(arg->src[0], FALSE, write_mask, dst_swizzle);
2323 if (src_mod == WINED3DSPSM_DZ) {
2324 glsl_src_param_t div_param;
2325 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
2326 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_2, &div_param);
2328 if (mask_size > 1) {
2329 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
2331 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
2333 } else if (src_mod == WINED3DSPSM_DW) {
2334 glsl_src_param_t div_param;
2335 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
2336 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_3, &div_param);
2338 if (mask_size > 1) {
2339 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
2341 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
2344 shader_addline(buffer, "gl_TexCoord[%u]%s);\n", reg, dst_swizzle);
2349 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
2350 * Take a 3-component dot product of the TexCoord[dstreg] and src,
2351 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
2352 void pshader_glsl_texdp3tex(SHADER_OPCODE_ARG* arg) {
2353 glsl_src_param_t src0_param;
2355 glsl_sample_function_t sample_function;
2356 DWORD sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2357 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2358 DWORD sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
2360 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2362 shader_glsl_append_dst(arg->buffer, arg);
2363 shader_glsl_get_write_mask(arg->dst, dst_mask);
2365 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
2366 * scalar, and projected sampling would require 4.
2368 * It is a dependent read - not valid with conditional NP2 textures
2370 shader_glsl_get_sample_function(sampler_type, FALSE, FALSE, &sample_function);
2372 switch(count_bits(sample_function.coord_mask)) {
2374 shader_addline(arg->buffer, "%s(Psampler%u, dot(gl_TexCoord[%u].xyz, %s))%s);\n",
2375 sample_function.name, sampler_idx, sampler_idx, src0_param.param_str, dst_mask);
2379 shader_addline(arg->buffer, "%s(Psampler%u, vec2(dot(gl_TexCoord[%u].xyz, %s), 0.0))%s);\n",
2380 sample_function.name, sampler_idx, sampler_idx, src0_param.param_str, dst_mask);
2384 shader_addline(arg->buffer, "%s(Psampler%u, vec3(dot(gl_TexCoord[%u].xyz, %s), 0.0, 0.0))%s);\n",
2385 sample_function.name, sampler_idx, sampler_idx, src0_param.param_str, dst_mask);
2388 FIXME("Unexpected mask bitcount %d\n", count_bits(sample_function.coord_mask));
2392 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
2393 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
2394 void pshader_glsl_texdp3(SHADER_OPCODE_ARG* arg) {
2395 glsl_src_param_t src0_param;
2396 DWORD dstreg = arg->dst & WINED3DSP_REGNUM_MASK;
2397 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2399 unsigned int mask_size;
2401 dst_mask = shader_glsl_append_dst(arg->buffer, arg);
2402 mask_size = shader_glsl_get_write_mask_size(dst_mask);
2403 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2405 if (mask_size > 1) {
2406 shader_addline(arg->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
2408 shader_addline(arg->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
2412 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
2413 * Calculate the depth as dst.x / dst.y */
2414 void pshader_glsl_texdepth(SHADER_OPCODE_ARG* arg) {
2415 glsl_dst_param_t dst_param;
2417 shader_glsl_add_dst_param(arg, arg->dst, 0, &dst_param);
2419 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
2420 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
2421 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
2422 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
2425 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);
2428 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
2429 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
2430 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
2431 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
2433 void pshader_glsl_texm3x2depth(SHADER_OPCODE_ARG* arg) {
2434 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2435 DWORD dstreg = arg->dst & WINED3DSP_REGNUM_MASK;
2436 glsl_src_param_t src0_param;
2438 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2440 shader_addline(arg->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
2441 shader_addline(arg->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
2444 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
2445 * Calculate the 1st of a 2-row matrix multiplication. */
2446 void pshader_glsl_texm3x2pad(SHADER_OPCODE_ARG* arg) {
2447 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2448 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2449 SHADER_BUFFER* buffer = arg->buffer;
2450 glsl_src_param_t src0_param;
2452 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2453 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
2456 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
2457 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
2458 void pshader_glsl_texm3x3pad(SHADER_OPCODE_ARG* arg) {
2460 IWineD3DPixelShaderImpl* shader = (IWineD3DPixelShaderImpl*) arg->shader;
2461 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2462 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2463 SHADER_BUFFER* buffer = arg->buffer;
2464 SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
2465 glsl_src_param_t src0_param;
2467 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2468 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + current_state->current_row, reg, src0_param.param_str);
2469 current_state->texcoord_w[current_state->current_row++] = reg;
2472 void pshader_glsl_texm3x2tex(SHADER_OPCODE_ARG* arg) {
2473 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2474 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2475 SHADER_BUFFER* buffer = arg->buffer;
2476 glsl_src_param_t src0_param;
2479 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2480 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
2482 shader_glsl_append_dst(buffer, arg);
2483 shader_glsl_get_write_mask(arg->dst, dst_mask);
2485 /* Sample the texture using the calculated coordinates */
2486 shader_addline(buffer, "texture2D(Psampler%u, tmp0.xy)%s);\n", reg, dst_mask);
2489 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
2490 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
2491 void pshader_glsl_texm3x3tex(SHADER_OPCODE_ARG* arg) {
2492 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2493 glsl_src_param_t src0_param;
2495 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2496 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
2497 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
2498 DWORD sampler_type = arg->reg_maps->samplers[reg] & WINED3DSP_TEXTURETYPE_MASK;
2499 glsl_sample_function_t sample_function;
2501 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2502 shader_addline(arg->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
2504 shader_glsl_append_dst(arg->buffer, arg);
2505 shader_glsl_get_write_mask(arg->dst, dst_mask);
2506 /* Dependent read, not valid with conditional NP2 */
2507 shader_glsl_get_sample_function(sampler_type, FALSE, FALSE, &sample_function);
2509 /* Sample the texture using the calculated coordinates */
2510 shader_addline(arg->buffer, "%s(Psampler%u, tmp0.xyz)%s);\n", sample_function.name, reg, dst_mask);
2512 current_state->current_row = 0;
2515 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
2516 * Perform the 3rd row of a 3x3 matrix multiply */
2517 void pshader_glsl_texm3x3(SHADER_OPCODE_ARG* arg) {
2518 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2519 glsl_src_param_t src0_param;
2521 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2522 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
2523 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
2525 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2527 shader_glsl_append_dst(arg->buffer, arg);
2528 shader_glsl_get_write_mask(arg->dst, dst_mask);
2529 shader_addline(arg->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
2531 current_state->current_row = 0;
2534 /** Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
2535 * Peform the final texture lookup based on the previous 2 3x3 matrix multiplies */
2536 void pshader_glsl_texm3x3spec(SHADER_OPCODE_ARG* arg) {
2538 IWineD3DPixelShaderImpl* shader = (IWineD3DPixelShaderImpl*) arg->shader;
2539 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2540 glsl_src_param_t src0_param;
2541 glsl_src_param_t src1_param;
2543 SHADER_BUFFER* buffer = arg->buffer;
2544 SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
2545 DWORD stype = arg->reg_maps->samplers[reg] & WINED3DSP_TEXTURETYPE_MASK;
2546 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2547 glsl_sample_function_t sample_function;
2549 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2550 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], src_mask, &src1_param);
2552 /* Perform the last matrix multiply operation */
2553 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
2554 /* Reflection calculation */
2555 shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
2557 shader_glsl_append_dst(buffer, arg);
2558 shader_glsl_get_write_mask(arg->dst, dst_mask);
2559 /* Dependent read, not valid with conditional NP2 */
2560 shader_glsl_get_sample_function(stype, FALSE, FALSE, &sample_function);
2562 /* Sample the texture */
2563 shader_addline(buffer, "%s(Psampler%u, tmp0.xyz)%s);\n", sample_function.name, reg, dst_mask);
2565 current_state->current_row = 0;
2568 /** Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
2569 * Peform the final texture lookup based on the previous 2 3x3 matrix multiplies */
2570 void pshader_glsl_texm3x3vspec(SHADER_OPCODE_ARG* arg) {
2572 IWineD3DPixelShaderImpl* shader = (IWineD3DPixelShaderImpl*) arg->shader;
2573 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2574 SHADER_BUFFER* buffer = arg->buffer;
2575 SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
2576 glsl_src_param_t src0_param;
2578 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2579 DWORD sampler_type = arg->reg_maps->samplers[reg] & WINED3DSP_TEXTURETYPE_MASK;
2580 glsl_sample_function_t sample_function;
2582 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2584 /* Perform the last matrix multiply operation */
2585 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
2587 /* Construct the eye-ray vector from w coordinates */
2588 shader_addline(buffer, "tmp1.xyz = normalize(vec3(gl_TexCoord[%u].w, gl_TexCoord[%u].w, gl_TexCoord[%u].w));\n",
2589 current_state->texcoord_w[0], current_state->texcoord_w[1], reg);
2590 shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
2592 shader_glsl_append_dst(buffer, arg);
2593 shader_glsl_get_write_mask(arg->dst, dst_mask);
2594 /* Dependent read, not valid with conditional NP2 */
2595 shader_glsl_get_sample_function(sampler_type, FALSE, FALSE, &sample_function);
2597 /* Sample the texture using the calculated coordinates */
2598 shader_addline(buffer, "%s(Psampler%u, tmp0.xyz)%s);\n", sample_function.name, reg, dst_mask);
2600 current_state->current_row = 0;
2603 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
2604 * Apply a fake bump map transform.
2605 * texbem is pshader <= 1.3 only, this saves a few version checks
2607 void pshader_glsl_texbem(SHADER_OPCODE_ARG* arg) {
2608 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
2609 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
2610 char dst_swizzle[6];
2611 glsl_sample_function_t sample_function;
2612 glsl_src_param_t coord_param;
2619 sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2620 flags = deviceImpl->stateBlock->textureState[sampler_idx][WINED3DTSS_TEXTURETRANSFORMFLAGS];
2622 sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
2623 /* Dependent read, not valid with conditional NP2 */
2624 shader_glsl_get_sample_function(sampler_type, FALSE, FALSE, &sample_function);
2625 mask = sample_function.coord_mask;
2627 shader_glsl_get_write_mask(arg->dst, dst_swizzle);
2629 shader_glsl_get_write_mask(mask, coord_mask);
2631 /* with projective textures, texbem only divides the static texture coord, not the displacement,
2632 * so we can't let the GL handle this.
2634 if (flags & WINED3DTTFF_PROJECTED) {
2636 char coord_div_mask[3];
2637 switch (flags & ~WINED3DTTFF_PROJECTED) {
2638 case WINED3DTTFF_COUNT1: FIXME("WINED3DTTFF_PROJECTED with WINED3DTTFF_COUNT1?\n"); break;
2639 case WINED3DTTFF_COUNT2: div_mask = WINED3DSP_WRITEMASK_1; break;
2640 case WINED3DTTFF_COUNT3: div_mask = WINED3DSP_WRITEMASK_2; break;
2641 case WINED3DTTFF_COUNT4:
2642 case WINED3DTTFF_DISABLE: div_mask = WINED3DSP_WRITEMASK_3; break;
2644 shader_glsl_get_write_mask(div_mask, coord_div_mask);
2645 shader_addline(arg->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
2648 shader_glsl_append_dst(arg->buffer, arg);
2649 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0|WINED3DSP_WRITEMASK_1, &coord_param);
2650 if(arg->opcode->opcode == WINED3DSIO_TEXBEML) {
2651 glsl_src_param_t luminance_param;
2652 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_2, &luminance_param);
2653 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",
2654 sample_function.name, sampler_idx, sampler_idx, coord_mask, sampler_idx, coord_param.param_str, coord_mask,
2655 luminance_param.param_str, sampler_idx, sampler_idx, dst_swizzle);
2657 shader_addline(arg->buffer, "%s(Psampler%u, T%u%s + vec4(bumpenvmat%d * %s, 0.0, 0.0)%s )%s);\n",
2658 sample_function.name, sampler_idx, sampler_idx, coord_mask, sampler_idx, coord_param.param_str, coord_mask, dst_swizzle);
2662 void pshader_glsl_bem(SHADER_OPCODE_ARG* arg) {
2663 glsl_src_param_t src0_param, src1_param;
2664 DWORD sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2666 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0|WINED3DSP_WRITEMASK_1, &src0_param);
2667 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0|WINED3DSP_WRITEMASK_1, &src1_param);
2669 shader_glsl_append_dst(arg->buffer, arg);
2670 shader_addline(arg->buffer, "%s + bumpenvmat%d * %s);\n",
2671 src0_param.param_str, sampler_idx, src1_param.param_str);
2674 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
2675 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
2676 void pshader_glsl_texreg2ar(SHADER_OPCODE_ARG* arg) {
2678 glsl_src_param_t src0_param;
2679 DWORD sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2682 shader_glsl_append_dst(arg->buffer, arg);
2683 shader_glsl_get_write_mask(arg->dst, dst_mask);
2684 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
2686 shader_addline(arg->buffer, "texture2D(Psampler%u, %s.wx)%s);\n", sampler_idx, src0_param.reg_name, dst_mask);
2689 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
2690 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
2691 void pshader_glsl_texreg2gb(SHADER_OPCODE_ARG* arg) {
2692 glsl_src_param_t src0_param;
2693 DWORD sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2696 shader_glsl_append_dst(arg->buffer, arg);
2697 shader_glsl_get_write_mask(arg->dst, dst_mask);
2698 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
2700 shader_addline(arg->buffer, "texture2D(Psampler%u, %s.yz)%s);\n", sampler_idx, src0_param.reg_name, dst_mask);
2703 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
2704 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
2705 void pshader_glsl_texreg2rgb(SHADER_OPCODE_ARG* arg) {
2706 glsl_src_param_t src0_param;
2708 DWORD sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2709 DWORD sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
2710 glsl_sample_function_t sample_function;
2712 shader_glsl_append_dst(arg->buffer, arg);
2713 shader_glsl_get_write_mask(arg->dst, dst_mask);
2714 /* Dependent read, not valid with conditional NP2 */
2715 shader_glsl_get_sample_function(sampler_type, FALSE, FALSE, &sample_function);
2716 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], sample_function.coord_mask, &src0_param);
2718 shader_addline(arg->buffer, "%s(Psampler%u, %s)%s);\n", sample_function.name, sampler_idx, src0_param.param_str, dst_mask);
2721 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
2722 * If any of the first 3 components are < 0, discard this pixel */
2723 void pshader_glsl_texkill(SHADER_OPCODE_ARG* arg) {
2724 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
2725 DWORD hex_version = This->baseShader.hex_version;
2726 glsl_dst_param_t dst_param;
2728 /* The argument is a destination parameter, and no writemasks are allowed */
2729 shader_glsl_add_dst_param(arg, arg->dst, 0, &dst_param);
2730 if((hex_version >= WINED3DPS_VERSION(2,0))) {
2731 /* 2.0 shaders compare all 4 components in texkill */
2732 shader_addline(arg->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
2734 /* 1.X shaders only compare the first 3 components, probably due to the nature of the texkill
2735 * instruction as a tex* instruction, and phase, which kills all a / w components. Even if all
2736 * 4 components are defined, only the first 3 are used
2738 shader_addline(arg->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
2742 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
2743 * dst = dot2(src0, src1) + src2 */
2744 void pshader_glsl_dp2add(SHADER_OPCODE_ARG* arg) {
2745 glsl_src_param_t src0_param;
2746 glsl_src_param_t src1_param;
2747 glsl_src_param_t src2_param;
2749 unsigned int mask_size;
2751 write_mask = shader_glsl_append_dst(arg->buffer, arg);
2752 mask_size = shader_glsl_get_write_mask_size(write_mask);
2754 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
2755 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
2756 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], WINED3DSP_WRITEMASK_0, &src2_param);
2758 if (mask_size > 1) {
2759 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);
2761 shader_addline(arg->buffer, "dot(%s, %s) + %s);\n", src0_param.param_str, src1_param.param_str, src2_param.param_str);
2765 void pshader_glsl_input_pack(
2766 SHADER_BUFFER* buffer,
2767 semantic* semantics_in,
2768 IWineD3DPixelShader *iface) {
2771 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *) iface;
2773 for (i = 0; i < MAX_REG_INPUT; i++) {
2775 DWORD usage_token = semantics_in[i].usage;
2776 DWORD register_token = semantics_in[i].reg;
2777 DWORD usage, usage_idx;
2781 if (!usage_token) continue;
2782 usage = (usage_token & WINED3DSP_DCL_USAGE_MASK) >> WINED3DSP_DCL_USAGE_SHIFT;
2783 usage_idx = (usage_token & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
2784 shader_glsl_get_write_mask(register_token, reg_mask);
2788 case WINED3DDECLUSAGE_TEXCOORD:
2789 if(usage_idx < 8 && This->vertexprocessing == pretransformed) {
2790 shader_addline(buffer, "IN[%u]%s = gl_TexCoord[%u]%s;\n",
2791 This->input_reg_map[i], reg_mask, usage_idx, reg_mask);
2793 shader_addline(buffer, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
2794 This->input_reg_map[i], reg_mask, reg_mask);
2798 case WINED3DDECLUSAGE_COLOR:
2800 shader_addline(buffer, "IN[%u]%s = vec4(gl_Color)%s;\n",
2801 This->input_reg_map[i], reg_mask, reg_mask);
2802 else if (usage_idx == 1)
2803 shader_addline(buffer, "IN[%u]%s = vec4(gl_SecondaryColor)%s;\n",
2804 This->input_reg_map[i], reg_mask, reg_mask);
2806 shader_addline(buffer, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
2807 This->input_reg_map[i], reg_mask, reg_mask);
2811 shader_addline(buffer, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
2812 This->input_reg_map[i], reg_mask, reg_mask);
2817 /*********************************************
2818 * Vertex Shader Specific Code begins here
2819 ********************************************/
2821 static void add_glsl_program_entry(struct shader_glsl_priv *priv, struct glsl_shader_prog_link *entry) {
2822 glsl_program_key_t *key;
2824 key = HeapAlloc(GetProcessHeap(), 0, sizeof(glsl_program_key_t));
2825 key->vshader = entry->vshader;
2826 key->pshader = entry->pshader;
2828 hash_table_put(priv->glsl_program_lookup, key, entry);
2831 static struct glsl_shader_prog_link *get_glsl_program_entry(struct shader_glsl_priv *priv,
2832 GLhandleARB vshader, GLhandleARB pshader) {
2833 glsl_program_key_t key;
2835 key.vshader = vshader;
2836 key.pshader = pshader;
2838 return (struct glsl_shader_prog_link *)hash_table_get(priv->glsl_program_lookup, &key);
2841 void delete_glsl_program_entry(struct shader_glsl_priv *priv, WineD3D_GL_Info *gl_info, struct glsl_shader_prog_link *entry) {
2842 glsl_program_key_t *key;
2844 key = HeapAlloc(GetProcessHeap(), 0, sizeof(glsl_program_key_t));
2845 key->vshader = entry->vshader;
2846 key->pshader = entry->pshader;
2847 hash_table_remove(priv->glsl_program_lookup, key);
2849 GL_EXTCALL(glDeleteObjectARB(entry->programId));
2850 if (entry->vshader) list_remove(&entry->vshader_entry);
2851 if (entry->pshader) list_remove(&entry->pshader_entry);
2852 HeapFree(GetProcessHeap(), 0, entry->vuniformF_locations);
2853 HeapFree(GetProcessHeap(), 0, entry->puniformF_locations);
2854 HeapFree(GetProcessHeap(), 0, entry);
2857 static void handle_ps3_input(SHADER_BUFFER *buffer, semantic *semantics_in, semantic *semantics_out, WineD3D_GL_Info *gl_info, DWORD *map) {
2859 DWORD usage_token, usage_token_out;
2860 DWORD register_token, register_token_out;
2861 DWORD usage, usage_idx, usage_out, usage_idx_out;
2864 DWORD in_count = GL_LIMITS(glsl_varyings) / 4;
2865 char reg_mask[6], reg_mask_out[6];
2866 char destination[50];
2868 set = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*set) * (in_count + 2));
2870 if (!semantics_out) {
2871 /* Save gl_FrontColor & gl_FrontSecondaryColor before overwriting them. */
2872 shader_addline(buffer, "vec4 front_color = gl_FrontColor;\n");
2873 shader_addline(buffer, "vec4 front_secondary_color = gl_FrontSecondaryColor;\n");
2876 for(i = 0; i < MAX_REG_INPUT; i++) {
2877 usage_token = semantics_in[i].usage;
2878 if (!usage_token) continue;
2881 if (in_idx >= (in_count + 2)) {
2882 FIXME("More input varyings declared than supported, expect issues\n");
2884 } else if(map[i] == -1) {
2885 /* Declared, but not read register */
2889 if (in_idx == in_count) {
2890 sprintf(destination, "gl_FrontColor");
2891 } else if (in_idx == in_count + 1) {
2892 sprintf(destination, "gl_FrontSecondaryColor");
2894 sprintf(destination, "IN[%u]", in_idx);
2897 register_token = semantics_in[i].reg;
2899 usage = (usage_token & WINED3DSP_DCL_USAGE_MASK) >> WINED3DSP_DCL_USAGE_SHIFT;
2900 usage_idx = (usage_token & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
2901 set[map[i]] = shader_glsl_get_write_mask(register_token, reg_mask);
2903 if(!semantics_out) {
2905 case WINED3DDECLUSAGE_COLOR:
2907 shader_addline(buffer, "%s%s = front_color%s;\n",
2908 destination, reg_mask, reg_mask);
2909 else if (usage_idx == 1)
2910 shader_addline(buffer, "%s%s = front_secondary_color%s;\n",
2911 destination, reg_mask, reg_mask);
2913 shader_addline(buffer, "%s%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
2914 destination, reg_mask, reg_mask);
2917 case WINED3DDECLUSAGE_TEXCOORD:
2918 if (usage_idx < 8) {
2919 shader_addline(buffer, "%s%s = gl_TexCoord[%u]%s;\n",
2920 destination, reg_mask, usage_idx, reg_mask);
2922 shader_addline(buffer, "%s%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
2923 destination, reg_mask, reg_mask);
2927 case WINED3DDECLUSAGE_FOG:
2928 shader_addline(buffer, "%s%s = vec4(gl_FogFragCoord, 0.0, 0.0, 0.0)%s;\n",
2929 destination, reg_mask, reg_mask);
2933 shader_addline(buffer, "%s%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
2934 destination, reg_mask, reg_mask);
2938 for(j = 0; j < MAX_REG_OUTPUT; j++) {
2939 usage_token_out = semantics_out[j].usage;
2940 if (!usage_token_out) continue;
2941 register_token_out = semantics_out[j].reg;
2943 usage_out = (usage_token_out & WINED3DSP_DCL_USAGE_MASK) >> WINED3DSP_DCL_USAGE_SHIFT;
2944 usage_idx_out = (usage_token_out & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
2945 shader_glsl_get_write_mask(register_token_out, reg_mask_out);
2947 if(usage == usage_out &&
2948 usage_idx == usage_idx_out) {
2949 shader_addline(buffer, "%s%s = OUT[%u]%s;\n",
2950 destination, reg_mask, j, reg_mask);
2955 shader_addline(buffer, "%s%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
2956 destination, reg_mask, reg_mask);
2961 /* This is solely to make the compiler / linker happy and avoid warning about undefined
2962 * varyings. It shouldn't result in any real code executed on the GPU, since all read
2963 * input varyings are assigned above, if the optimizer works properly.
2965 for(i = 0; i < in_count + 2; i++) {
2966 if(set[i] != WINED3DSP_WRITEMASK_ALL) {
2967 unsigned int size = 0;
2968 memset(reg_mask, 0, sizeof(reg_mask));
2969 if(!(set[i] & WINED3DSP_WRITEMASK_0)) {
2970 reg_mask[size] = 'x';
2973 if(!(set[i] & WINED3DSP_WRITEMASK_1)) {
2974 reg_mask[size] = 'y';
2977 if(!(set[i] & WINED3DSP_WRITEMASK_2)) {
2978 reg_mask[size] = 'z';
2981 if(!(set[i] & WINED3DSP_WRITEMASK_3)) {
2982 reg_mask[size] = 'w';
2986 if (i == in_count) {
2987 sprintf(destination, "gl_FrontColor");
2988 } else if (i == in_count + 1) {
2989 sprintf(destination, "gl_FrontSecondaryColor");
2991 sprintf(destination, "IN[%u]", i);
2995 shader_addline(buffer, "%s.%s = 0.0;\n", destination, reg_mask);
2997 shader_addline(buffer, "%s.%s = vec%u(0.0);\n", destination, reg_mask, size);
3002 HeapFree(GetProcessHeap(), 0, set);
3005 static GLhandleARB generate_param_reorder_function(IWineD3DVertexShader *vertexshader,
3006 IWineD3DPixelShader *pixelshader,
3007 WineD3D_GL_Info *gl_info) {
3008 GLhandleARB ret = 0;
3009 IWineD3DVertexShaderImpl *vs = (IWineD3DVertexShaderImpl *) vertexshader;
3010 IWineD3DPixelShaderImpl *ps = (IWineD3DPixelShaderImpl *) pixelshader;
3011 IWineD3DDeviceImpl *device;
3012 DWORD vs_major = vs ? WINED3DSHADER_VERSION_MAJOR(vs->baseShader.hex_version) : 0;
3013 DWORD ps_major = ps ? WINED3DSHADER_VERSION_MAJOR(ps->baseShader.hex_version) : 0;
3015 SHADER_BUFFER buffer;
3017 DWORD register_token;
3018 DWORD usage, usage_idx, writemask;
3020 semantic *semantics_out, *semantics_in;
3022 buffer.buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, SHADER_PGMSIZE);
3025 buffer.newline = TRUE;
3027 shader_addline(&buffer, "#version 120\n");
3029 if(vs_major < 3 && ps_major < 3) {
3030 /* That one is easy: The vertex shader writes to the builtin varyings, the pixel shader reads from them.
3031 * Take care about the texcoord .w fixup though if we're using the fixed function fragment pipeline
3033 device = (IWineD3DDeviceImpl *) vs->baseShader.device;
3034 if((GLINFO_LOCATION).set_texcoord_w && ps_major == 0 && vs_major > 0 &&
3035 !device->frag_pipe->ffp_proj_control) {
3036 shader_addline(&buffer, "void order_ps_input() {\n");
3037 for(i = 0; i < min(8, MAX_REG_TEXCRD); i++) {
3038 if(vs->baseShader.reg_maps.texcoord_mask[i] != 0 &&
3039 vs->baseShader.reg_maps.texcoord_mask[i] != WINED3DSP_WRITEMASK_ALL) {
3040 shader_addline(&buffer, "gl_TexCoord[%u].w = 1.0;\n", i);
3043 shader_addline(&buffer, "}\n");
3045 shader_addline(&buffer, "void order_ps_input() { /* do nothing */ }\n");
3047 } else if(ps_major < 3 && vs_major >= 3) {
3048 /* The vertex shader writes to its own varyings, the pixel shader needs them in the builtin ones */
3049 semantics_out = vs->semantics_out;
3051 shader_addline(&buffer, "void order_ps_input(in vec4 OUT[%u]) {\n", MAX_REG_OUTPUT);
3052 for(i = 0; i < MAX_REG_OUTPUT; i++) {
3053 usage_token = semantics_out[i].usage;
3054 if (!usage_token) continue;
3055 register_token = semantics_out[i].reg;
3057 usage = (usage_token & WINED3DSP_DCL_USAGE_MASK) >> WINED3DSP_DCL_USAGE_SHIFT;
3058 usage_idx = (usage_token & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
3059 writemask = shader_glsl_get_write_mask(register_token, reg_mask);
3062 case WINED3DDECLUSAGE_COLOR:
3064 shader_addline(&buffer, "gl_FrontColor%s = OUT[%u]%s;\n", reg_mask, i, reg_mask);
3065 else if (usage_idx == 1)
3066 shader_addline(&buffer, "gl_FrontSecondaryColor%s = OUT[%u]%s;\n", reg_mask, i, reg_mask);
3069 case WINED3DDECLUSAGE_POSITION:
3070 shader_addline(&buffer, "gl_Position%s = OUT[%u]%s;\n", reg_mask, i, reg_mask);
3073 case WINED3DDECLUSAGE_TEXCOORD:
3074 if (usage_idx < 8) {
3075 if(!(GLINFO_LOCATION).set_texcoord_w || ps_major > 0) writemask |= WINED3DSP_WRITEMASK_3;
3077 shader_addline(&buffer, "gl_TexCoord[%u]%s = OUT[%u]%s;\n",
3078 usage_idx, reg_mask, i, reg_mask);
3079 if(!(writemask & WINED3DSP_WRITEMASK_3)) {
3080 shader_addline(&buffer, "gl_TexCoord[%u].w = 1.0;\n", usage_idx);
3085 case WINED3DDECLUSAGE_PSIZE:
3086 shader_addline(&buffer, "gl_PointSize = OUT[%u].x;\n", i);
3089 case WINED3DDECLUSAGE_FOG:
3090 shader_addline(&buffer, "gl_FogFragCoord = OUT[%u].%c;\n", i, reg_mask[1]);
3097 shader_addline(&buffer, "}\n");
3099 } else if(ps_major >= 3 && vs_major >= 3) {
3100 semantics_out = vs->semantics_out;
3101 semantics_in = ps->semantics_in;
3103 /* This one is tricky: a 3.0 pixel shader reads from a 3.0 vertex shader */
3104 shader_addline(&buffer, "varying vec4 IN[%u];\n", GL_LIMITS(glsl_varyings) / 4);
3105 shader_addline(&buffer, "void order_ps_input(in vec4 OUT[%u]) {\n", MAX_REG_OUTPUT);
3107 /* First, sort out position and point size. Those are not passed to the pixel shader */
3108 for(i = 0; i < MAX_REG_OUTPUT; i++) {
3109 usage_token = semantics_out[i].usage;
3110 if (!usage_token) continue;
3111 register_token = semantics_out[i].reg;
3113 usage = (usage_token & WINED3DSP_DCL_USAGE_MASK) >> WINED3DSP_DCL_USAGE_SHIFT;
3114 usage_idx = (usage_token & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
3115 shader_glsl_get_write_mask(register_token, reg_mask);
3118 case WINED3DDECLUSAGE_POSITION:
3119 shader_addline(&buffer, "gl_Position%s = OUT[%u]%s;\n", reg_mask, i, reg_mask);
3122 case WINED3DDECLUSAGE_PSIZE:
3123 shader_addline(&buffer, "gl_PointSize = OUT[%u].x;\n", i);
3131 /* Then, fix the pixel shader input */
3132 handle_ps3_input(&buffer, semantics_in, semantics_out, gl_info, ps->input_reg_map);
3134 shader_addline(&buffer, "}\n");
3135 } else if(ps_major >= 3 && vs_major < 3) {
3136 semantics_in = ps->semantics_in;
3138 shader_addline(&buffer, "varying vec4 IN[%u];\n", GL_LIMITS(glsl_varyings) / 4);
3139 shader_addline(&buffer, "void order_ps_input() {\n");
3140 /* The vertex shader wrote to the builtin varyings. There is no need to figure out position and
3141 * point size, but we depend on the optimizers kindness to find out that the pixel shader doesn't
3142 * read gl_TexCoord and gl_ColorX, otherwise we'll run out of varyings
3144 handle_ps3_input(&buffer, semantics_in, NULL, gl_info, ps->input_reg_map);
3145 shader_addline(&buffer, "}\n");
3147 ERR("Unexpected vertex and pixel shader version condition: vs: %d, ps: %d\n", vs_major, ps_major);
3150 ret = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
3151 checkGLcall("glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB)");
3152 GL_EXTCALL(glShaderSourceARB(ret, 1, (const char**)&buffer.buffer, NULL));
3153 checkGLcall("glShaderSourceARB(ret, 1, (const char**)&buffer.buffer, NULL)");
3154 GL_EXTCALL(glCompileShaderARB(ret));
3155 checkGLcall("glCompileShaderARB(ret)");
3157 HeapFree(GetProcessHeap(), 0, buffer.buffer);
3161 static void hardcode_local_constants(IWineD3DBaseShaderImpl *shader, WineD3D_GL_Info *gl_info, GLhandleARB programId, char prefix) {
3162 local_constant* lconst;
3167 LIST_FOR_EACH_ENTRY(lconst, &shader->baseShader.constantsF, local_constant, entry) {
3168 value = (float *) lconst->value;
3169 snprintf(glsl_name, sizeof(glsl_name), "%cLC%u", prefix, lconst->idx);
3170 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
3171 GL_EXTCALL(glUniform4fvARB(tmp_loc, 1, value));
3173 checkGLcall("Hardcoding local constants\n");
3176 /** Sets the GLSL program ID for the given pixel and vertex shader combination.
3177 * It sets the programId on the current StateBlock (because it should be called
3178 * inside of the DrawPrimitive() part of the render loop).
3180 * If a program for the given combination does not exist, create one, and store
3181 * the program in the hash table. If it creates a program, it will link the
3182 * given objects, too.
3184 static void set_glsl_shader_program(IWineD3DDevice *iface, BOOL use_ps, BOOL use_vs) {
3185 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
3186 struct shader_glsl_priv *priv = (struct shader_glsl_priv *)This->shader_priv;
3187 WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
3188 IWineD3DPixelShader *pshader = This->stateBlock->pixelShader;
3189 IWineD3DVertexShader *vshader = This->stateBlock->vertexShader;
3190 struct glsl_shader_prog_link *entry = NULL;
3191 GLhandleARB programId = 0;
3192 GLhandleARB reorder_shader_id = 0;
3196 GLhandleARB vshader_id = use_vs ? ((IWineD3DBaseShaderImpl*)vshader)->baseShader.prgId : 0;
3197 GLhandleARB pshader_id = use_ps ? ((IWineD3DBaseShaderImpl*)pshader)->baseShader.prgId : 0;
3198 entry = get_glsl_program_entry(priv, vshader_id, pshader_id);
3200 priv->glsl_program = entry;
3204 /* If we get to this point, then no matching program exists, so we create one */
3205 programId = GL_EXTCALL(glCreateProgramObjectARB());
3206 TRACE("Created new GLSL shader program %u\n", programId);
3208 /* Create the entry */
3209 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link));
3210 entry->programId = programId;
3211 entry->vshader = vshader_id;
3212 entry->pshader = pshader_id;
3213 /* Add the hash table entry */
3214 add_glsl_program_entry(priv, entry);
3216 /* Set the current program */
3217 priv->glsl_program = entry;
3219 /* Attach GLSL vshader */
3221 int max_attribs = 16; /* TODO: Will this always be the case? It is at the moment... */
3224 reorder_shader_id = generate_param_reorder_function(vshader, pshader, gl_info);
3225 TRACE("Attaching GLSL shader object %u to program %u\n", reorder_shader_id, programId);
3226 GL_EXTCALL(glAttachObjectARB(programId, reorder_shader_id));
3227 checkGLcall("glAttachObjectARB");
3228 /* Flag the reorder function for deletion, then it will be freed automatically when the program
3231 GL_EXTCALL(glDeleteObjectARB(reorder_shader_id));
3233 TRACE("Attaching GLSL shader object %u to program %u\n", vshader_id, programId);
3234 GL_EXTCALL(glAttachObjectARB(programId, vshader_id));
3235 checkGLcall("glAttachObjectARB");
3237 /* Bind vertex attributes to a corresponding index number to match
3238 * the same index numbers as ARB_vertex_programs (makes loading
3239 * vertex attributes simpler). With this method, we can use the
3240 * exact same code to load the attributes later for both ARB and
3243 * We have to do this here because we need to know the Program ID
3244 * in order to make the bindings work, and it has to be done prior
3245 * to linking the GLSL program. */
3246 for (i = 0; i < max_attribs; ++i) {
3247 if (((IWineD3DBaseShaderImpl*)vshader)->baseShader.reg_maps.attributes[i]) {
3248 snprintf(tmp_name, sizeof(tmp_name), "attrib%i", i);
3249 GL_EXTCALL(glBindAttribLocationARB(programId, i, tmp_name));
3252 checkGLcall("glBindAttribLocationARB");
3254 list_add_head(&((IWineD3DBaseShaderImpl *)vshader)->baseShader.linked_programs, &entry->vshader_entry);
3257 /* Attach GLSL pshader */
3259 TRACE("Attaching GLSL shader object %u to program %u\n", pshader_id, programId);
3260 GL_EXTCALL(glAttachObjectARB(programId, pshader_id));
3261 checkGLcall("glAttachObjectARB");
3263 list_add_head(&((IWineD3DBaseShaderImpl *)pshader)->baseShader.linked_programs, &entry->pshader_entry);
3266 /* Link the program */
3267 TRACE("Linking GLSL shader program %u\n", programId);
3268 GL_EXTCALL(glLinkProgramARB(programId));
3269 print_glsl_info_log(&GLINFO_LOCATION, programId);
3271 entry->vuniformF_locations = HeapAlloc(GetProcessHeap(), 0, sizeof(GLhandleARB) * GL_LIMITS(vshader_constantsF));
3272 for (i = 0; i < GL_LIMITS(vshader_constantsF); ++i) {
3273 snprintf(glsl_name, sizeof(glsl_name), "VC[%i]", i);
3274 entry->vuniformF_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
3276 for (i = 0; i < MAX_CONST_I; ++i) {
3277 snprintf(glsl_name, sizeof(glsl_name), "VI[%i]", i);
3278 entry->vuniformI_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
3280 entry->puniformF_locations = HeapAlloc(GetProcessHeap(), 0, sizeof(GLhandleARB) * GL_LIMITS(pshader_constantsF));
3281 for (i = 0; i < GL_LIMITS(pshader_constantsF); ++i) {
3282 snprintf(glsl_name, sizeof(glsl_name), "PC[%i]", i);
3283 entry->puniformF_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
3285 for (i = 0; i < MAX_CONST_I; ++i) {
3286 snprintf(glsl_name, sizeof(glsl_name), "PI[%i]", i);
3287 entry->puniformI_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
3291 for(i = 0; i < ((IWineD3DPixelShaderImpl*)pshader)->numbumpenvmatconsts; i++) {
3293 sprintf(name, "bumpenvmat%d", ((IWineD3DPixelShaderImpl*)pshader)->bumpenvmatconst[i].texunit);
3294 entry->bumpenvmat_location[i] = GL_EXTCALL(glGetUniformLocationARB(programId, name));
3295 sprintf(name, "luminancescale%d", ((IWineD3DPixelShaderImpl*)pshader)->luminanceconst[i].texunit);
3296 entry->luminancescale_location[i] = GL_EXTCALL(glGetUniformLocationARB(programId, name));
3297 sprintf(name, "luminanceoffset%d", ((IWineD3DPixelShaderImpl*)pshader)->luminanceconst[i].texunit);
3298 entry->luminanceoffset_location[i] = GL_EXTCALL(glGetUniformLocationARB(programId, name));
3303 entry->posFixup_location = GL_EXTCALL(glGetUniformLocationARB(programId, "posFixup"));
3304 entry->srgb_comparison_location = GL_EXTCALL(glGetUniformLocationARB(programId, "srgb_comparison"));
3305 entry->srgb_mul_low_location = GL_EXTCALL(glGetUniformLocationARB(programId, "srgb_mul_low"));
3306 entry->ycorrection_location = GL_EXTCALL(glGetUniformLocationARB(programId, "ycorrection"));
3307 checkGLcall("Find glsl program uniform locations");
3309 if (pshader && WINED3DSHADER_VERSION_MAJOR(((IWineD3DPixelShaderImpl *)pshader)->baseShader.hex_version) >= 3
3310 && ((IWineD3DPixelShaderImpl *)pshader)->declared_in_count > GL_LIMITS(glsl_varyings) / 4) {
3311 TRACE("Shader %d needs vertex color clamping disabled\n", programId);
3312 entry->vertex_color_clamp = GL_FALSE;
3314 entry->vertex_color_clamp = GL_FIXED_ONLY_ARB;
3317 /* Set the shader to allow uniform loading on it */
3318 GL_EXTCALL(glUseProgramObjectARB(programId));
3319 checkGLcall("glUseProgramObjectARB(programId)");
3321 /* Load the vertex and pixel samplers now. The function that finds the mappings makes sure
3322 * that it stays the same for each vertexshader-pixelshader pair(=linked glsl program). If
3323 * a pshader with fixed function pipeline is used there are no vertex samplers, and if a
3324 * vertex shader with fixed function pixel processing is used we make sure that the card
3325 * supports enough samplers to allow the max number of vertex samplers with all possible
3326 * fixed function fragment processing setups. So once the program is linked these samplers
3330 /* Load vertex shader samplers */
3331 shader_glsl_load_vsamplers(gl_info, (IWineD3DStateBlock*)This->stateBlock, programId);
3334 /* Load pixel shader samplers */
3335 shader_glsl_load_psamplers(gl_info, (IWineD3DStateBlock*)This->stateBlock, programId);
3338 /* If the local constants do not have to be loaded with the environment constants,
3339 * load them now to have them hardcoded in the GLSL program. This saves some CPU cycles
3342 if(pshader && !((IWineD3DPixelShaderImpl*)pshader)->baseShader.load_local_constsF) {
3343 hardcode_local_constants((IWineD3DBaseShaderImpl *) pshader, gl_info, programId, 'P');
3345 if(vshader && !((IWineD3DVertexShaderImpl*)vshader)->baseShader.load_local_constsF) {
3346 hardcode_local_constants((IWineD3DBaseShaderImpl *) vshader, gl_info, programId, 'V');
3350 static GLhandleARB create_glsl_blt_shader(WineD3D_GL_Info *gl_info) {
3351 GLhandleARB program_id;
3352 GLhandleARB vshader_id, pshader_id;
3353 const char *blt_vshader[] = {
3357 " gl_Position = gl_Vertex;\n"
3358 " gl_FrontColor = vec4(1.0);\n"
3359 " gl_TexCoord[0].x = (gl_Vertex.x * 0.5) + 0.5;\n"
3360 " gl_TexCoord[0].y = (-gl_Vertex.y * 0.5) + 0.5;\n"
3364 const char *blt_pshader[] = {
3366 "uniform sampler2D sampler;\n"
3369 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
3373 vshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
3374 GL_EXTCALL(glShaderSourceARB(vshader_id, 1, blt_vshader, NULL));
3375 GL_EXTCALL(glCompileShaderARB(vshader_id));
3377 pshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
3378 GL_EXTCALL(glShaderSourceARB(pshader_id, 1, blt_pshader, NULL));
3379 GL_EXTCALL(glCompileShaderARB(pshader_id));
3381 program_id = GL_EXTCALL(glCreateProgramObjectARB());
3382 GL_EXTCALL(glAttachObjectARB(program_id, vshader_id));
3383 GL_EXTCALL(glAttachObjectARB(program_id, pshader_id));
3384 GL_EXTCALL(glLinkProgramARB(program_id));
3386 print_glsl_info_log(&GLINFO_LOCATION, program_id);
3388 /* Once linked we can mark the shaders for deletion. They will be deleted once the program
3391 GL_EXTCALL(glDeleteObjectARB(vshader_id));
3392 GL_EXTCALL(glDeleteObjectARB(pshader_id));
3396 static void shader_glsl_select(IWineD3DDevice *iface, BOOL usePS, BOOL useVS) {
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;
3400 GLhandleARB program_id = 0;
3401 GLenum old_vertex_color_clamp, current_vertex_color_clamp;
3403 old_vertex_color_clamp = priv->glsl_program ? priv->glsl_program->vertex_color_clamp : GL_FIXED_ONLY_ARB;
3405 if (useVS || usePS) set_glsl_shader_program(iface, usePS, useVS);
3406 else priv->glsl_program = NULL;
3408 current_vertex_color_clamp = priv->glsl_program ? priv->glsl_program->vertex_color_clamp : GL_FIXED_ONLY_ARB;
3410 if (old_vertex_color_clamp != current_vertex_color_clamp) {
3411 if (GL_SUPPORT(ARB_COLOR_BUFFER_FLOAT)) {
3412 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, current_vertex_color_clamp));
3413 checkGLcall("glClampColorARB");
3415 FIXME("vertex color clamp needs to be changed, but extension not supported.\n");
3419 program_id = priv->glsl_program ? priv->glsl_program->programId : 0;
3420 if (program_id) TRACE("Using GLSL program %u\n", program_id);
3421 GL_EXTCALL(glUseProgramObjectARB(program_id));
3422 checkGLcall("glUseProgramObjectARB");
3425 static void shader_glsl_select_depth_blt(IWineD3DDevice *iface) {
3426 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
3427 WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
3428 struct shader_glsl_priv *priv = (struct shader_glsl_priv *) This->shader_priv;
3429 static GLhandleARB loc = -1;
3431 if (!priv->depth_blt_glsl_program_id) {
3432 priv->depth_blt_glsl_program_id = create_glsl_blt_shader(gl_info);
3433 loc = GL_EXTCALL(glGetUniformLocationARB(priv->depth_blt_glsl_program_id, "sampler"));
3436 GL_EXTCALL(glUseProgramObjectARB(priv->depth_blt_glsl_program_id));
3437 GL_EXTCALL(glUniform1iARB(loc, 0));
3440 static void shader_glsl_deselect_depth_blt(IWineD3DDevice *iface) {
3441 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
3442 WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
3443 struct shader_glsl_priv *priv = (struct shader_glsl_priv *) This->shader_priv;
3444 GLhandleARB program_id;
3446 program_id = priv->glsl_program ? priv->glsl_program->programId : 0;
3447 if (program_id) TRACE("Using GLSL program %u\n", program_id);
3449 GL_EXTCALL(glUseProgramObjectARB(program_id));
3450 checkGLcall("glUseProgramObjectARB");
3453 static void shader_glsl_cleanup(IWineD3DDevice *iface) {
3454 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
3455 WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
3456 GL_EXTCALL(glUseProgramObjectARB(0));
3459 static void shader_glsl_destroy(IWineD3DBaseShader *iface) {
3460 struct list *linked_programs;
3461 IWineD3DBaseShaderImpl *This = (IWineD3DBaseShaderImpl *) iface;
3462 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *)This->baseShader.device;
3463 struct shader_glsl_priv *priv = (struct shader_glsl_priv *)device->shader_priv;
3464 WineD3D_GL_Info *gl_info = &device->adapter->gl_info;
3466 /* Note: Do not use QueryInterface here to find out which shader type this is because this code
3467 * can be called from IWineD3DBaseShader::Release
3469 char pshader = shader_is_pshader_version(This->baseShader.hex_version);
3471 if(This->baseShader.prgId == 0) return;
3472 linked_programs = &This->baseShader.linked_programs;
3474 TRACE("Deleting linked programs\n");
3475 if (linked_programs->next) {
3476 struct glsl_shader_prog_link *entry, *entry2;
3479 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs, struct glsl_shader_prog_link, pshader_entry) {
3480 delete_glsl_program_entry(priv, gl_info, entry);
3483 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs, struct glsl_shader_prog_link, vshader_entry) {
3484 delete_glsl_program_entry(priv, gl_info, entry);
3489 TRACE("Deleting shader object %u\n", This->baseShader.prgId);
3490 GL_EXTCALL(glDeleteObjectARB(This->baseShader.prgId));
3491 checkGLcall("glDeleteObjectARB");
3492 This->baseShader.prgId = 0;
3493 This->baseShader.is_compiled = FALSE;
3496 static unsigned int glsl_program_key_hash(void *key) {
3497 glsl_program_key_t *k = (glsl_program_key_t *)key;
3499 unsigned int hash = k->vshader | k->pshader << 16;
3500 hash += ~(hash << 15);
3501 hash ^= (hash >> 10);
3502 hash += (hash << 3);
3503 hash ^= (hash >> 6);
3504 hash += ~(hash << 11);
3505 hash ^= (hash >> 16);
3510 static BOOL glsl_program_key_compare(void *keya, void *keyb) {
3511 glsl_program_key_t *ka = (glsl_program_key_t *)keya;
3512 glsl_program_key_t *kb = (glsl_program_key_t *)keyb;
3514 return ka->vshader == kb->vshader && ka->pshader == kb->pshader;
3517 static HRESULT shader_glsl_alloc(IWineD3DDevice *iface) {
3518 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
3519 struct shader_glsl_priv *priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct shader_glsl_priv));
3520 priv->glsl_program_lookup = hash_table_create(glsl_program_key_hash, glsl_program_key_compare);
3521 This->shader_priv = priv;
3525 static void shader_glsl_free(IWineD3DDevice *iface) {
3526 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
3527 WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
3528 struct shader_glsl_priv *priv = (struct shader_glsl_priv *)This->shader_priv;
3530 if(priv->depth_blt_glsl_program_id) {
3531 GL_EXTCALL(glDeleteObjectARB(priv->depth_blt_glsl_program_id));
3534 hash_table_destroy(priv->glsl_program_lookup, NULL, NULL);
3536 HeapFree(GetProcessHeap(), 0, This->shader_priv);
3537 This->shader_priv = NULL;
3540 static BOOL shader_glsl_dirty_const(IWineD3DDevice *iface) {
3541 /* TODO: GL_EXT_bindable_uniform can be used to share constants across shaders */
3545 static void shader_glsl_generate_pshader(IWineD3DPixelShader *iface, SHADER_BUFFER *buffer) {
3546 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)iface;
3547 shader_reg_maps* reg_maps = &This->baseShader.reg_maps;
3548 CONST DWORD *function = This->baseShader.function;
3549 const char *fragcolor;
3550 WineD3D_GL_Info *gl_info = &((IWineD3DDeviceImpl *)This->baseShader.device)->adapter->gl_info;
3552 /* Create the hw GLSL shader object and assign it as the baseShader.prgId */
3553 GLhandleARB shader_obj = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
3555 shader_addline(buffer, "#version 120\n");
3557 if (GL_SUPPORT(ARB_DRAW_BUFFERS)) {
3558 shader_addline(buffer, "#extension GL_ARB_draw_buffers : enable\n");
3560 if (GL_SUPPORT(ARB_TEXTURE_RECTANGLE)) {
3561 /* The spec says that it doesn't have to be explicitly enabled, but the nvidia
3562 * drivers write a warning if we don't do so
3564 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
3567 /* Base Declarations */
3568 shader_generate_glsl_declarations( (IWineD3DBaseShader*) This, reg_maps, buffer, &GLINFO_LOCATION);
3570 /* Pack 3.0 inputs */
3571 if (This->baseShader.hex_version >= WINED3DPS_VERSION(3,0)) {
3573 if(((IWineD3DDeviceImpl *) This->baseShader.device)->strided_streams.u.s.position_transformed) {
3574 This->vertexprocessing = pretransformed;
3575 pshader_glsl_input_pack(buffer, This->semantics_in, iface);
3576 } else if(!use_vs((IWineD3DDeviceImpl *) This->baseShader.device)) {
3577 This->vertexprocessing = fixedfunction;
3578 pshader_glsl_input_pack(buffer, This->semantics_in, iface);
3580 This->vertexprocessing = vertexshader;
3584 /* Base Shader Body */
3585 shader_generate_main( (IWineD3DBaseShader*) This, buffer, reg_maps, function);
3587 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly */
3588 if (This->baseShader.hex_version < WINED3DPS_VERSION(2,0)) {
3589 /* Some older cards like GeforceFX ones don't support multiple buffers, so also not gl_FragData */
3590 if(GL_SUPPORT(ARB_DRAW_BUFFERS))
3591 shader_addline(buffer, "gl_FragData[0] = R0;\n");
3593 shader_addline(buffer, "gl_FragColor = R0;\n");
3596 if(GL_SUPPORT(ARB_DRAW_BUFFERS)) {
3597 fragcolor = "gl_FragData[0]";
3599 fragcolor = "gl_FragColor";
3601 if(This->srgb_enabled) {
3602 shader_addline(buffer, "tmp0.xyz = pow(%s.xyz, vec3(%f, %f, %f)) * vec3(%f, %f, %f) - vec3(%f, %f, %f);\n",
3603 fragcolor, srgb_pow, srgb_pow, srgb_pow, srgb_mul_high, srgb_mul_high, srgb_mul_high,
3604 srgb_sub_high, srgb_sub_high, srgb_sub_high);
3605 shader_addline(buffer, "tmp1.xyz = %s.xyz * srgb_mul_low.xyz;\n", fragcolor);
3606 shader_addline(buffer, "%s.x = %s.x < srgb_comparison.x ? tmp1.x : tmp0.x;\n", fragcolor, fragcolor);
3607 shader_addline(buffer, "%s.y = %s.y < srgb_comparison.y ? tmp1.y : tmp0.y;\n", fragcolor, fragcolor);
3608 shader_addline(buffer, "%s.z = %s.z < srgb_comparison.z ? tmp1.z : tmp0.z;\n", fragcolor, fragcolor);
3609 shader_addline(buffer, "%s = clamp(%s, 0.0, 1.0);\n", fragcolor, fragcolor);
3611 /* Pixel shader < 3.0 do not replace the fog stage.
3612 * This implements linear fog computation and blending.
3613 * TODO: non linear fog
3614 * NOTE: gl_Fog.start and gl_Fog.end don't hold fog start s and end e but
3615 * -1/(e-s) and e/(e-s) respectively.
3617 if(This->baseShader.hex_version < WINED3DPS_VERSION(3,0)) {
3618 shader_addline(buffer, "float Fog = clamp(gl_FogFragCoord * gl_Fog.start + gl_Fog.end, 0.0, 1.0);\n");
3619 shader_addline(buffer, "%s.xyz = mix(gl_Fog.color.xyz, %s.xyz, Fog);\n", fragcolor, fragcolor);
3622 shader_addline(buffer, "}\n");
3624 TRACE("Compiling shader object %u\n", shader_obj);
3625 GL_EXTCALL(glShaderSourceARB(shader_obj, 1, (const char**)&buffer->buffer, NULL));
3626 GL_EXTCALL(glCompileShaderARB(shader_obj));
3627 print_glsl_info_log(&GLINFO_LOCATION, shader_obj);
3629 /* Store the shader object */
3630 This->baseShader.prgId = shader_obj;
3633 static void shader_glsl_generate_vshader(IWineD3DVertexShader *iface, SHADER_BUFFER *buffer) {
3634 IWineD3DVertexShaderImpl *This = (IWineD3DVertexShaderImpl *)iface;
3635 shader_reg_maps* reg_maps = &This->baseShader.reg_maps;
3636 CONST DWORD *function = This->baseShader.function;
3637 WineD3D_GL_Info *gl_info = &((IWineD3DDeviceImpl *)This->baseShader.device)->adapter->gl_info;
3639 /* Create the hw GLSL shader program and assign it as the baseShader.prgId */
3640 GLhandleARB shader_obj = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
3642 shader_addline(buffer, "#version 120\n");
3644 /* Base Declarations */
3645 shader_generate_glsl_declarations( (IWineD3DBaseShader*) This, reg_maps, buffer, &GLINFO_LOCATION);
3647 /* Base Shader Body */
3648 shader_generate_main( (IWineD3DBaseShader*) This, buffer, reg_maps, function);
3650 /* Unpack 3.0 outputs */
3651 if (This->baseShader.hex_version >= WINED3DVS_VERSION(3,0)) {
3652 shader_addline(buffer, "order_ps_input(OUT);\n");
3654 shader_addline(buffer, "order_ps_input();\n");
3657 /* If this shader doesn't use fog copy the z coord to the fog coord so that we can use table fog */
3659 shader_addline(buffer, "gl_FogFragCoord = gl_Position.z;\n");
3661 /* Write the final position.
3663 * OpenGL coordinates specify the center of the pixel while d3d coords specify
3664 * the corner. The offsets are stored in z and w in posFixup. posFixup.y contains
3665 * 1.0 or -1.0 to turn the rendering upside down for offscreen rendering. PosFixup.x
3666 * contains 1.0 to allow a mad.
3668 shader_addline(buffer, "gl_Position.y = gl_Position.y * posFixup.y;\n");
3669 shader_addline(buffer, "gl_Position.xy += posFixup.zw * gl_Position.ww;\n");
3671 /* Z coord [0;1]->[-1;1] mapping, see comment in transform_projection in state.c
3673 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However, shaders are run
3674 * before the homogeneous divide, so we have to take the w into account: z = ((z / w) * 2 - 1) * w,
3675 * which is the same as z = z / 2 - w.
3677 shader_addline(buffer, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
3679 shader_addline(buffer, "}\n");
3681 TRACE("Compiling shader object %u\n", shader_obj);
3682 GL_EXTCALL(glShaderSourceARB(shader_obj, 1, (const char**)&buffer->buffer, NULL));
3683 GL_EXTCALL(glCompileShaderARB(shader_obj));
3684 print_glsl_info_log(&GLINFO_LOCATION, shader_obj);
3686 /* Store the shader object */
3687 This->baseShader.prgId = shader_obj;
3690 static void shader_glsl_get_caps(WINED3DDEVTYPE devtype, WineD3D_GL_Info *gl_info, struct shader_caps *pCaps) {
3691 /* Nvidia Geforce6/7 or Ati R4xx/R5xx cards with GLSL support, support VS 3.0 but older Nvidia/Ati
3692 * models with GLSL support only support 2.0. In case of nvidia we can detect VS 2.0 support using
3693 * vs_nv_version which is based on NV_vertex_program.
3694 * For Ati cards there's no way using glsl (it abstracts the lowlevel info away) and also not
3695 * using ARB_vertex_program. It is safe to assume that when a card supports pixel shader 2.0 it
3696 * supports vertex shader 2.0 too and the way around. We can detect ps2.0 using the maximum number
3697 * of native instructions, so use that here. For more info see the pixel shader versioning code below.
3699 if((GLINFO_LOCATION.vs_nv_version == VS_VERSION_20) || (GLINFO_LOCATION.ps_arb_max_instructions <= 512))
3700 pCaps->VertexShaderVersion = WINED3DVS_VERSION(2,0);
3702 pCaps->VertexShaderVersion = WINED3DVS_VERSION(3,0);
3703 TRACE_(d3d_caps)("Hardware vertex shader version %d.%d enabled (GLSL)\n", (pCaps->VertexShaderVersion >> 8) & 0xff, pCaps->VertexShaderVersion & 0xff);
3704 pCaps->MaxVertexShaderConst = GL_LIMITS(vshader_constantsF);
3706 /* Older DX9-class videocards (GeforceFX / Radeon >9500/X*00) only support pixel shader 2.0/2.0a/2.0b.
3707 * In OpenGL the extensions related to GLSL abstract lowlevel GL info away which is needed
3708 * to distinguish between 2.0 and 3.0 (and 2.0a/2.0b). In case of Nvidia we use their fragment
3709 * program extensions. On other hardware including ATI GL_ARB_fragment_program offers the info
3710 * in max native instructions. Intel and others also offer the info in this extension but they
3711 * don't support GLSL (at least on Windows).
3713 * PS2.0 requires at least 96 instructions, 2.0a/2.0b go up to 512. Assume that if the number
3714 * of instructions is 512 or less we have to do with ps2.0 hardware.
3715 * 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.
3717 if((GLINFO_LOCATION.ps_nv_version == PS_VERSION_20) || (GLINFO_LOCATION.ps_arb_max_instructions <= 512))
3718 pCaps->PixelShaderVersion = WINED3DPS_VERSION(2,0);
3720 pCaps->PixelShaderVersion = WINED3DPS_VERSION(3,0);
3722 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
3723 * Direct3D minimum requirement.
3725 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
3726 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
3728 * The problem is that the refrast clamps temporary results in the shader to
3729 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
3730 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
3731 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
3732 * offer a way to query this.
3734 pCaps->PixelShader1xMaxValue = 8.0;
3735 TRACE_(d3d_caps)("Hardware pixel shader version %d.%d enabled (GLSL)\n", (pCaps->PixelShaderVersion >> 8) & 0xff, pCaps->PixelShaderVersion & 0xff);
3738 static BOOL shader_glsl_conv_supported(WINED3DFORMAT fmt) {
3739 TRACE("Checking shader format support for format %s:", debug_d3dformat(fmt));
3741 case WINED3DFMT_V8U8:
3742 case WINED3DFMT_V16U16:
3743 case WINED3DFMT_X8L8V8U8:
3744 case WINED3DFMT_L6V5U5:
3745 case WINED3DFMT_Q8W8V8U8:
3746 case WINED3DFMT_ATI2N:
3755 const shader_backend_t glsl_shader_backend = {
3757 shader_glsl_select_depth_blt,
3758 shader_glsl_deselect_depth_blt,
3759 shader_glsl_load_constants,
3760 shader_glsl_cleanup,
3761 shader_glsl_color_correction,
3762 shader_glsl_destroy,
3765 shader_glsl_dirty_const,
3766 shader_glsl_generate_pshader,
3767 shader_glsl_generate_vshader,
3768 shader_glsl_get_caps,
3769 shader_glsl_conv_supported,