2 * GLSL pixel and vertex shader implementation
4 * Copyright 2006 Jason Green
5 * Copyright 2006-2007 Henri Verbeet
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 * D3D shader asm has swizzles on source parameters, and write masks for
24 * destination parameters. GLSL uses swizzles for both. The result of this is
25 * that for example "mov dst.xw, src.zyxw" becomes "dst.xw = src.zw" in GLSL.
26 * Ie, to generate a proper GLSL source swizzle, we need to take the D3D write
27 * mask for the destination parameter into account.
32 #include "wined3d_private.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
35 WINE_DECLARE_DEBUG_CHANNEL(d3d_constants);
37 #define GLINFO_LOCATION (*gl_info)
52 } glsl_sample_function_t;
54 /** Prints the GLSL info log which will contain error messages if they exist */
55 void print_glsl_info_log(WineD3D_GL_Info *gl_info, GLhandleARB obj) {
57 int infologLength = 0;
60 GL_EXTCALL(glGetObjectParameterivARB(obj,
61 GL_OBJECT_INFO_LOG_LENGTH_ARB,
64 /* A size of 1 is just a null-terminated string, so the log should be bigger than
65 * that if there are errors. */
66 if (infologLength > 1)
68 infoLog = HeapAlloc(GetProcessHeap(), 0, infologLength);
69 GL_EXTCALL(glGetInfoLogARB(obj, infologLength, NULL, infoLog));
70 FIXME("Error received from GLSL shader #%u: %s\n", obj, debugstr_a(infoLog));
71 HeapFree(GetProcessHeap(), 0, infoLog);
76 * Loads (pixel shader) samplers
78 static void shader_glsl_load_psamplers(
79 WineD3D_GL_Info *gl_info,
80 IWineD3DStateBlock* iface) {
82 IWineD3DStateBlockImpl* stateBlock = (IWineD3DStateBlockImpl*) iface;
83 GLhandleARB programId = stateBlock->glsl_program->programId;
86 char sampler_name[20];
88 for (i = 0; i < MAX_FRAGMENT_SAMPLERS; ++i) {
89 snprintf(sampler_name, sizeof(sampler_name), "Psampler%d", i);
90 name_loc = GL_EXTCALL(glGetUniformLocationARB(programId, sampler_name));
92 int mapped_unit = stateBlock->wineD3DDevice->texUnitMap[i];
93 if (mapped_unit != -1 && mapped_unit < GL_LIMITS(fragment_samplers)) {
94 TRACE("Loading %s for texture %d\n", sampler_name, mapped_unit);
95 GL_EXTCALL(glUniform1iARB(name_loc, mapped_unit));
96 checkGLcall("glUniform1iARB");
98 ERR("Trying to load sampler %s on unsupported unit %d\n", sampler_name, mapped_unit);
104 static void shader_glsl_load_vsamplers(WineD3D_GL_Info *gl_info, IWineD3DStateBlock* iface) {
105 IWineD3DStateBlockImpl* stateBlock = (IWineD3DStateBlockImpl*) iface;
106 GLhandleARB programId = stateBlock->glsl_program->programId;
107 GLhandleARB name_loc;
108 char sampler_name[20];
111 for (i = 0; i < MAX_VERTEX_SAMPLERS; ++i) {
112 snprintf(sampler_name, sizeof(sampler_name), "Vsampler%d", i);
113 name_loc = GL_EXTCALL(glGetUniformLocationARB(programId, sampler_name));
114 if (name_loc != -1) {
115 int mapped_unit = stateBlock->wineD3DDevice->texUnitMap[MAX_FRAGMENT_SAMPLERS + i];
116 if (mapped_unit != -1 && mapped_unit < GL_LIMITS(combined_samplers)) {
117 TRACE("Loading %s for texture %d\n", sampler_name, mapped_unit);
118 GL_EXTCALL(glUniform1iARB(name_loc, mapped_unit));
119 checkGLcall("glUniform1iARB");
121 ERR("Trying to load sampler %s on unsupported unit %d\n", sampler_name, mapped_unit);
128 * Loads floating point constants (aka uniforms) into the currently set GLSL program.
129 * When constant_list == NULL, it will load all the constants.
131 static void shader_glsl_load_constantsF(IWineD3DBaseShaderImpl* This, WineD3D_GL_Info *gl_info,
132 unsigned int max_constants, float* constants, GLhandleARB *constant_locations,
133 struct list *constant_list) {
134 constants_entry *constant;
135 local_constant* lconst;
140 if (TRACE_ON(d3d_shader)) {
141 LIST_FOR_EACH_ENTRY(constant, constant_list, constants_entry, entry) {
146 tmp_loc = constant_locations[i];
148 TRACE_(d3d_constants)("Loading constants %i: %f, %f, %f, %f\n", i,
149 constants[i * 4 + 0], constants[i * 4 + 1],
150 constants[i * 4 + 2], constants[i * 4 + 3]);
156 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
157 if(WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) == 1 &&
158 shader_is_pshader_version(This->baseShader.hex_version)) {
161 LIST_FOR_EACH_ENTRY(constant, constant_list, constants_entry, entry) {
166 tmp_loc = constant_locations[i];
168 /* We found this uniform name in the program - go ahead and send the data */
170 if(constants[k + 0] < -1.0) lcl_const[0] = -1.0;
171 else if(constants[k + 0] > 1.0) lcl_const[0] = 1.0;
172 else lcl_const[0] = constants[k + 0];
173 if(constants[k + 1] < -1.0) lcl_const[1] = -1.0;
174 else if(constants[k + 1] > 1.0) lcl_const[1] = 1.0;
175 else lcl_const[1] = constants[k + 1];
176 if(constants[k + 2] < -1.0) lcl_const[2] = -1.0;
177 else if(constants[k + 2] > 1.0) lcl_const[2] = 1.0;
178 else lcl_const[2] = constants[k + 2];
179 if(constants[k + 3] < -1.0) lcl_const[3] = -1.0;
180 else if(constants[k + 3] > 1.0) lcl_const[3] = 1.0;
181 else lcl_const[3] = constants[k + 3];
183 GL_EXTCALL(glUniform4fvARB(tmp_loc, 1, lcl_const));
188 LIST_FOR_EACH_ENTRY(constant, constant_list, constants_entry, entry) {
193 tmp_loc = constant_locations[i];
195 /* We found this uniform name in the program - go ahead and send the data */
196 GL_EXTCALL(glUniform4fvARB(tmp_loc, 1, constants + (i * 4)));
201 checkGLcall("glUniform4fvARB()");
203 /* Load immediate constants */
204 if (TRACE_ON(d3d_shader)) {
205 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
206 tmp_loc = constant_locations[lconst->idx];
208 GLfloat* values = (GLfloat*)lconst->value;
209 TRACE_(d3d_constants)("Loading local constants %i: %f, %f, %f, %f\n", lconst->idx,
210 values[0], values[1], values[2], values[3]);
214 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
215 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
216 tmp_loc = constant_locations[lconst->idx];
218 /* We found this uniform name in the program - go ahead and send the data */
219 GL_EXTCALL(glUniform4fvARB(tmp_loc, 1, (GLfloat*)lconst->value));
222 checkGLcall("glUniform4fvARB()");
226 * Loads integer constants (aka uniforms) into the currently set GLSL program.
227 * When @constants_set == NULL, it will load all the constants.
229 static void shader_glsl_load_constantsI(
230 IWineD3DBaseShaderImpl* This,
231 WineD3D_GL_Info *gl_info,
232 GLhandleARB programId,
233 unsigned max_constants,
235 BOOL* constants_set) {
240 char is_pshader = shader_is_pshader_version(This->baseShader.hex_version);
241 const char* prefix = is_pshader? "PI":"VI";
244 for (i=0; i<max_constants; ++i) {
245 if (NULL == constants_set || constants_set[i]) {
247 TRACE_(d3d_constants)("Loading constants %i: %i, %i, %i, %i\n",
248 i, constants[i*4], constants[i*4+1], constants[i*4+2], constants[i*4+3]);
250 /* TODO: Benchmark and see if it would be beneficial to store the
251 * locations of the constants to avoid looking up each time */
252 snprintf(tmp_name, sizeof(tmp_name), "%s[%i]", prefix, i);
253 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
255 /* We found this uniform name in the program - go ahead and send the data */
256 GL_EXTCALL(glUniform4ivARB(tmp_loc, 1, &constants[i*4]));
257 checkGLcall("glUniform4ivARB");
262 /* Load immediate constants */
263 ptr = list_head(&This->baseShader.constantsI);
265 local_constant* lconst = LIST_ENTRY(ptr, struct local_constant, entry);
266 unsigned int idx = lconst->idx;
267 GLint* values = (GLint*) lconst->value;
269 TRACE_(d3d_constants)("Loading local constants %i: %i, %i, %i, %i\n", idx,
270 values[0], values[1], values[2], values[3]);
272 snprintf(tmp_name, sizeof(tmp_name), "%s[%i]", prefix, idx);
273 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
275 /* We found this uniform name in the program - go ahead and send the data */
276 GL_EXTCALL(glUniform4ivARB(tmp_loc, 1, values));
277 checkGLcall("glUniform4ivARB");
279 ptr = list_next(&This->baseShader.constantsI, ptr);
284 * Loads boolean constants (aka uniforms) into the currently set GLSL program.
285 * When @constants_set == NULL, it will load all the constants.
287 static void shader_glsl_load_constantsB(
288 IWineD3DBaseShaderImpl* This,
289 WineD3D_GL_Info *gl_info,
290 GLhandleARB programId,
291 unsigned max_constants,
293 BOOL* constants_set) {
298 char is_pshader = shader_is_pshader_version(This->baseShader.hex_version);
299 const char* prefix = is_pshader? "PB":"VB";
302 for (i=0; i<max_constants; ++i) {
303 if (NULL == constants_set || constants_set[i]) {
305 TRACE_(d3d_constants)("Loading constants %i: %i;\n", i, constants[i*4]);
307 /* TODO: Benchmark and see if it would be beneficial to store the
308 * locations of the constants to avoid looking up each time */
309 snprintf(tmp_name, sizeof(tmp_name), "%s[%i]", prefix, i);
310 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
312 /* We found this uniform name in the program - go ahead and send the data */
313 GL_EXTCALL(glUniform1ivARB(tmp_loc, 1, &constants[i*4]));
314 checkGLcall("glUniform1ivARB");
319 /* Load immediate constants */
320 ptr = list_head(&This->baseShader.constantsB);
322 local_constant* lconst = LIST_ENTRY(ptr, struct local_constant, entry);
323 unsigned int idx = lconst->idx;
324 GLint* values = (GLint*) lconst->value;
326 TRACE_(d3d_constants)("Loading local constants %i: %i\n", idx, values[0]);
328 snprintf(tmp_name, sizeof(tmp_name), "%s[%i]", prefix, idx);
329 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
331 /* We found this uniform name in the program - go ahead and send the data */
332 GL_EXTCALL(glUniform1ivARB(tmp_loc, 1, values));
333 checkGLcall("glUniform1ivARB");
335 ptr = list_next(&This->baseShader.constantsB, ptr);
342 * Loads the app-supplied constants into the currently set GLSL program.
344 void shader_glsl_load_constants(
345 IWineD3DDevice* device,
347 char useVertexShader) {
349 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) device;
350 IWineD3DStateBlockImpl* stateBlock = deviceImpl->stateBlock;
351 WineD3D_GL_Info *gl_info = &deviceImpl->adapter->gl_info;
353 GLhandleARB *constant_locations;
354 struct list *constant_list;
355 GLhandleARB programId;
358 if (!stateBlock->glsl_program) {
359 /* No GLSL program set - nothing to do. */
362 programId = stateBlock->glsl_program->programId;
364 if (useVertexShader) {
365 IWineD3DBaseShaderImpl* vshader = (IWineD3DBaseShaderImpl*) stateBlock->vertexShader;
368 constant_locations = stateBlock->glsl_program->vuniformF_locations;
369 constant_list = &stateBlock->set_vconstantsF;
371 /* Load vertex shader samplers */
372 shader_glsl_load_vsamplers(gl_info, (IWineD3DStateBlock*)stateBlock);
374 /* Load DirectX 9 float constants/uniforms for vertex shader */
375 shader_glsl_load_constantsF(vshader, gl_info, GL_LIMITS(vshader_constantsF),
376 stateBlock->vertexShaderConstantF, constant_locations, constant_list);
378 /* Load DirectX 9 integer constants/uniforms for vertex shader */
379 shader_glsl_load_constantsI(vshader, gl_info, programId, MAX_CONST_I,
380 stateBlock->vertexShaderConstantI,
381 stateBlock->changed.vertexShaderConstantsI);
383 /* Load DirectX 9 boolean constants/uniforms for vertex shader */
384 shader_glsl_load_constantsB(vshader, gl_info, programId, MAX_CONST_B,
385 stateBlock->vertexShaderConstantB,
386 stateBlock->changed.vertexShaderConstantsB);
388 /* Upload the position fixup params */
389 pos = GL_EXTCALL(glGetUniformLocationARB(programId, "posFixup"));
390 checkGLcall("glGetUniformLocationARB");
391 GL_EXTCALL(glUniform4fvARB(pos, 1, &deviceImpl->posFixup[0]));
392 checkGLcall("glUniform4fvARB");
395 if (usePixelShader) {
397 IWineD3DBaseShaderImpl* pshader = (IWineD3DBaseShaderImpl*) stateBlock->pixelShader;
399 constant_locations = stateBlock->glsl_program->puniformF_locations;
400 constant_list = &stateBlock->set_pconstantsF;
402 /* Load pixel shader samplers */
403 shader_glsl_load_psamplers(gl_info, (IWineD3DStateBlock*) stateBlock);
405 /* Load DirectX 9 float constants/uniforms for pixel shader */
406 shader_glsl_load_constantsF(pshader, gl_info, GL_LIMITS(pshader_constantsF),
407 stateBlock->pixelShaderConstantF, constant_locations, constant_list);
409 /* Load DirectX 9 integer constants/uniforms for pixel shader */
410 shader_glsl_load_constantsI(pshader, gl_info, programId, MAX_CONST_I,
411 stateBlock->pixelShaderConstantI,
412 stateBlock->changed.pixelShaderConstantsI);
414 /* Load DirectX 9 boolean constants/uniforms for pixel shader */
415 shader_glsl_load_constantsB(pshader, gl_info, programId, MAX_CONST_B,
416 stateBlock->pixelShaderConstantB,
417 stateBlock->changed.pixelShaderConstantsB);
419 /* Upload the environment bump map matrix if needed. The needsbumpmat member specifies the texture stage to load the matrix from.
420 * It can't be 0 for a valid texbem instruction.
422 if(((IWineD3DPixelShaderImpl *) pshader)->needsbumpmat != -1) {
423 float *data = (float *) &stateBlock->textureState[(int) ((IWineD3DPixelShaderImpl *) pshader)->needsbumpmat][WINED3DTSS_BUMPENVMAT00];
424 pos = GL_EXTCALL(glGetUniformLocationARB(programId, "bumpenvmat"));
425 checkGLcall("glGetUniformLocationARB");
426 GL_EXTCALL(glUniformMatrix2fvARB(pos, 1, 0, data));
427 checkGLcall("glUniformMatrix2fvARB");
429 /* texbeml needs the luminance scale and offset too. If texbeml is used, needsbumpmat
430 * is set too, so we can check that in the needsbumpmat check
432 if(((IWineD3DPixelShaderImpl *) pshader)->baseShader.reg_maps.luminanceparams != -1) {
433 int stage = ((IWineD3DPixelShaderImpl *) pshader)->baseShader.reg_maps.luminanceparams;
434 GLfloat *scale = (GLfloat *) &stateBlock->textureState[stage][WINED3DTSS_BUMPENVLSCALE];
435 GLfloat *offset = (GLfloat *) &stateBlock->textureState[stage][WINED3DTSS_BUMPENVLOFFSET];
437 pos = GL_EXTCALL(glGetUniformLocationARB(programId, "luminancescale"));
438 checkGLcall("glGetUniformLocationARB");
439 GL_EXTCALL(glUniform1fvARB(pos, 1, scale));
440 checkGLcall("glUniform1fvARB");
441 pos = GL_EXTCALL(glGetUniformLocationARB(programId, "luminanceoffset"));
442 checkGLcall("glGetUniformLocationARB");
443 GL_EXTCALL(glUniform1fvARB(pos, 1, offset));
444 checkGLcall("glUniform1fvARB");
446 } else if(((IWineD3DPixelShaderImpl *) pshader)->srgb_enabled &&
447 !((IWineD3DPixelShaderImpl *) pshader)->srgb_mode_hardcoded) {
451 if(stateBlock->renderState[WINED3DRS_SRGBWRITEENABLE]) {
452 comparison[0] = srgb_cmp; comparison[1] = srgb_cmp;
453 comparison[2] = srgb_cmp; comparison[3] = srgb_cmp;
455 mul_low[0] = srgb_mul_low; mul_low[1] = srgb_mul_low;
456 mul_low[2] = srgb_mul_low; mul_low[3] = srgb_mul_low;
458 comparison[0] = 1.0 / 0.0; comparison[1] = 1.0 / 0.0;
459 comparison[2] = 1.0 / 0.0; comparison[3] = 1.0 / 0.0;
461 mul_low[0] = 1.0; mul_low[1] = 1.0;
462 mul_low[2] = 1.0; mul_low[3] = 1.0;
465 pos = GL_EXTCALL(glGetUniformLocationARB(programId, "srgb_comparison"));
466 checkGLcall("glGetUniformLocationARB");
467 GL_EXTCALL(glUniform4fvARB(pos, 1, comparison));
468 pos = GL_EXTCALL(glGetUniformLocationARB(programId, "srgb_mul_low"));
469 checkGLcall("glGetUniformLocationARB");
470 GL_EXTCALL(glUniform4fvARB(pos, 1, mul_low));
472 if(((IWineD3DPixelShaderImpl *) pshader)->vpos_uniform) {
473 float correction_params[4];
474 pos = GL_EXTCALL(glGetUniformLocationARB(programId, "ycorrection"));
475 checkGLcall("glGetUniformLocationARB");
476 if(deviceImpl->render_offscreen) {
477 correction_params[0] = 0.0;
478 correction_params[1] = 1.0;
480 /* position is window relative, not viewport relative */
481 correction_params[0] = ((IWineD3DSurfaceImpl *) deviceImpl->render_targets[0])->currentDesc.Height;
482 correction_params[1] = -1.0;
484 GL_EXTCALL(glUniform4fvARB(pos, 1, correction_params));
489 /** Generate the variable & register declarations for the GLSL output target */
490 void shader_generate_glsl_declarations(
491 IWineD3DBaseShader *iface,
492 shader_reg_maps* reg_maps,
493 SHADER_BUFFER* buffer,
494 WineD3D_GL_Info* gl_info) {
496 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
497 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) This->baseShader.device;
499 unsigned int extra_constants_needed = 0;
501 /* There are some minor differences between pixel and vertex shaders */
502 char pshader = shader_is_pshader_version(This->baseShader.hex_version);
503 char prefix = pshader ? 'P' : 'V';
505 /* Prototype the subroutines */
506 for (i = 0; i < This->baseShader.limits.label; i++) {
507 if (reg_maps->labels[i])
508 shader_addline(buffer, "void subroutine%lu();\n", i);
511 /* Declare the constants (aka uniforms) */
512 if (This->baseShader.limits.constant_float > 0) {
513 unsigned max_constantsF = min(This->baseShader.limits.constant_float,
514 (pshader ? GL_LIMITS(pshader_constantsF) : GL_LIMITS(vshader_constantsF)));
515 shader_addline(buffer, "uniform vec4 %cC[%u];\n", prefix, max_constantsF);
518 if (This->baseShader.limits.constant_int > 0)
519 shader_addline(buffer, "uniform ivec4 %cI[%u];\n", prefix, This->baseShader.limits.constant_int);
521 if (This->baseShader.limits.constant_bool > 0)
522 shader_addline(buffer, "uniform bool %cB[%u];\n", prefix, This->baseShader.limits.constant_bool);
525 shader_addline(buffer, "uniform vec4 posFixup;\n");
527 IWineD3DPixelShaderImpl *ps_impl = (IWineD3DPixelShaderImpl *) This;
529 if(reg_maps->bumpmat != -1) {
530 shader_addline(buffer, "uniform mat2 bumpenvmat;\n");
531 if(reg_maps->luminanceparams) {
532 shader_addline(buffer, "uniform float luminancescale;\n");
533 shader_addline(buffer, "uniform float luminanceoffset;\n");
534 extra_constants_needed++;
536 extra_constants_needed++;
539 if(device->stateBlock->renderState[WINED3DRS_SRGBWRITEENABLE]) {
540 ps_impl->srgb_enabled = 1;
541 if(This->baseShader.limits.constant_float + extra_constants_needed + 1 < GL_LIMITS(pshader_constantsF)) {
542 shader_addline(buffer, "uniform vec4 srgb_mul_low;\n");
543 shader_addline(buffer, "uniform vec4 srgb_comparison;\n");
544 ps_impl->srgb_mode_hardcoded = 0;
545 extra_constants_needed++;
547 ps_impl->srgb_mode_hardcoded = 1;
548 shader_addline(buffer, "const vec4 srgb_mul_low = {%f, %f, %f, %f};\n",
549 srgb_mul_low, srgb_mul_low, srgb_mul_low, srgb_mul_low);
550 shader_addline(buffer, "const vec4 srgb_comparison = {%f, %f, %f, %f};\n",
551 srgb_cmp, srgb_cmp, srgb_cmp, srgb_cmp);
554 IWineD3DPixelShaderImpl *ps_impl = (IWineD3DPixelShaderImpl *) This;
556 /* Do not write any srgb fixup into the shader to save shader size and processing time.
557 * As a consequence, we can't toggle srgb write on without recompilation
559 ps_impl->srgb_enabled = 0;
560 ps_impl->srgb_mode_hardcoded = 1;
563 if(This->baseShader.limits.constant_float + extra_constants_needed + 1 < GL_LIMITS(pshader_constantsF)) {
564 shader_addline(buffer, "uniform vec4 ycorrection;\n");
565 ((IWineD3DPixelShaderImpl *) This)->vpos_uniform = 1;
566 extra_constants_needed++;
568 /* This happens because we do not have proper tracking of the constant registers that are
569 * actually used, only the max limit of the shader version
571 FIXME("Cannot find a free uniform for vpos correction params\n");
572 shader_addline(buffer, "const vec4 ycorrection = {%f, %f, 0.0, 0.0};\n",
573 device->render_offscreen ? 0.0 : ((IWineD3DSurfaceImpl *) device->render_targets[0])->currentDesc.Height,
574 device->render_offscreen ? 1.0 : -1.0);
576 shader_addline(buffer, "vec4 vpos;\n");
580 /* Declare texture samplers */
581 for (i = 0; i < This->baseShader.limits.sampler; i++) {
582 if (reg_maps->samplers[i]) {
584 DWORD stype = reg_maps->samplers[i] & WINED3DSP_TEXTURETYPE_MASK;
588 shader_addline(buffer, "uniform sampler1D %csampler%lu;\n", prefix, i);
591 shader_addline(buffer, "uniform sampler2D %csampler%lu;\n", prefix, i);
593 case WINED3DSTT_CUBE:
594 shader_addline(buffer, "uniform samplerCube %csampler%lu;\n", prefix, i);
596 case WINED3DSTT_VOLUME:
597 shader_addline(buffer, "uniform sampler3D %csampler%lu;\n", prefix, i);
600 shader_addline(buffer, "uniform unsupported_sampler %csampler%lu;\n", prefix, i);
601 FIXME("Unrecognized sampler type: %#x\n", stype);
607 /* Declare address variables */
608 for (i = 0; i < This->baseShader.limits.address; i++) {
609 if (reg_maps->address[i])
610 shader_addline(buffer, "ivec4 A%d;\n", i);
613 /* Declare texture coordinate temporaries and initialize them */
614 for (i = 0; i < This->baseShader.limits.texcoord; i++) {
615 if (reg_maps->texcoord[i])
616 shader_addline(buffer, "vec4 T%lu = gl_TexCoord[%lu];\n", i, i);
619 /* Declare input register temporaries */
620 for (i=0; i < This->baseShader.limits.packed_input; i++) {
621 if (reg_maps->packed_input[i])
622 shader_addline(buffer, "vec4 IN%lu;\n", i);
625 /* Declare output register temporaries */
626 for (i = 0; i < This->baseShader.limits.packed_output; i++) {
627 if (reg_maps->packed_output[i])
628 shader_addline(buffer, "vec4 OUT%lu;\n", i);
631 /* Declare temporary variables */
632 for(i = 0; i < This->baseShader.limits.temporary; i++) {
633 if (reg_maps->temporary[i])
634 shader_addline(buffer, "vec4 R%lu;\n", i);
637 /* Declare attributes */
638 for (i = 0; i < This->baseShader.limits.attributes; i++) {
639 if (reg_maps->attributes[i])
640 shader_addline(buffer, "attribute vec4 attrib%i;\n", i);
643 /* Declare loop register aL */
644 if (reg_maps->loop) {
645 shader_addline(buffer, "int aL;\n");
646 shader_addline(buffer, "int tmpInt;\n");
649 /* Temporary variables for matrix operations */
650 shader_addline(buffer, "vec4 tmp0;\n");
651 shader_addline(buffer, "vec4 tmp1;\n");
653 /* Start the main program */
654 shader_addline(buffer, "void main() {\n");
655 if(pshader && reg_maps->vpos) {
656 shader_addline(buffer, "vpos = vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1) - 0.5;\n");
660 /*****************************************************************************
661 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
663 * For more information, see http://wiki.winehq.org/DirectX-Shaders
664 ****************************************************************************/
667 static void shader_glsl_add_src_param(SHADER_OPCODE_ARG* arg, const DWORD param,
668 const DWORD addr_token, DWORD mask, glsl_src_param_t *src_param);
670 /** Used for opcode modifiers - They multiply the result by the specified amount */
671 static const char * const shift_glsl_tab[] = {
673 "2.0 * ", /* 1 (x2) */
674 "4.0 * ", /* 2 (x4) */
675 "8.0 * ", /* 3 (x8) */
676 "16.0 * ", /* 4 (x16) */
677 "32.0 * ", /* 5 (x32) */
684 "0.0625 * ", /* 12 (d16) */
685 "0.125 * ", /* 13 (d8) */
686 "0.25 * ", /* 14 (d4) */
687 "0.5 * " /* 15 (d2) */
690 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
691 static void shader_glsl_gen_modifier (
694 const char *in_regswizzle,
699 if (instr == WINED3DSIO_TEXKILL)
702 switch (instr & WINED3DSP_SRCMOD_MASK) {
703 case WINED3DSPSM_DZ: /* Need to handle this in the instructions itself (texld & texcrd). */
705 case WINED3DSPSM_NONE:
706 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
708 case WINED3DSPSM_NEG:
709 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
711 case WINED3DSPSM_NOT:
712 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
714 case WINED3DSPSM_BIAS:
715 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
717 case WINED3DSPSM_BIASNEG:
718 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
720 case WINED3DSPSM_SIGN:
721 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
723 case WINED3DSPSM_SIGNNEG:
724 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
726 case WINED3DSPSM_COMP:
727 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
730 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
732 case WINED3DSPSM_X2NEG:
733 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
735 case WINED3DSPSM_ABS:
736 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
738 case WINED3DSPSM_ABSNEG:
739 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
742 FIXME("Unhandled modifier %u\n", (instr & WINED3DSP_SRCMOD_MASK));
743 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
747 /** Writes the GLSL variable name that corresponds to the register that the
748 * DX opcode parameter is trying to access */
749 static void shader_glsl_get_register_name(
751 const DWORD addr_token,
754 SHADER_OPCODE_ARG* arg) {
756 /* oPos, oFog and oPts in D3D */
757 static const char * const hwrastout_reg_names[] = { "gl_Position", "gl_FogFragCoord", "gl_PointSize" };
759 DWORD reg = param & WINED3DSP_REGNUM_MASK;
760 DWORD regtype = shader_get_regtype(param);
761 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) arg->shader;
762 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
763 WineD3D_GL_Info* gl_info = &deviceImpl->adapter->gl_info;
765 char pshader = shader_is_pshader_version(This->baseShader.hex_version);
771 case WINED3DSPR_TEMP:
772 sprintf(tmpStr, "R%u", reg);
774 case WINED3DSPR_INPUT:
776 /* Pixel shaders >= 3.0 */
777 if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 3)
778 sprintf(tmpStr, "IN%u", reg);
781 strcpy(tmpStr, "gl_Color");
783 strcpy(tmpStr, "gl_SecondaryColor");
786 if (vshader_input_is_color((IWineD3DVertexShader*) This, reg))
788 sprintf(tmpStr, "attrib%u", reg);
791 case WINED3DSPR_CONST:
793 const char* prefix = pshader? "PC":"VC";
795 /* Relative addressing */
796 if (param & WINED3DSHADER_ADDRMODE_RELATIVE) {
798 /* Relative addressing on shaders 2.0+ have a relative address token,
799 * prior to that, it was hard-coded as "A0.x" because there's only 1 register */
800 if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 2) {
801 glsl_src_param_t rel_param;
802 shader_glsl_add_src_param(arg, addr_token, 0, WINED3DSP_WRITEMASK_0, &rel_param);
803 sprintf(tmpStr, "%s[%s + %u]", prefix, rel_param.param_str, reg);
805 sprintf(tmpStr, "%s[A0.x + %u]", prefix, reg);
808 sprintf(tmpStr, "%s[%u]", prefix, reg);
812 case WINED3DSPR_CONSTINT:
814 sprintf(tmpStr, "PI[%u]", reg);
816 sprintf(tmpStr, "VI[%u]", reg);
818 case WINED3DSPR_CONSTBOOL:
820 sprintf(tmpStr, "PB[%u]", reg);
822 sprintf(tmpStr, "VB[%u]", reg);
824 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
826 sprintf(tmpStr, "T%u", reg);
828 sprintf(tmpStr, "A%u", reg);
831 case WINED3DSPR_LOOP:
832 sprintf(tmpStr, "aL");
834 case WINED3DSPR_SAMPLER:
836 sprintf(tmpStr, "Psampler%u", reg);
838 sprintf(tmpStr, "Vsampler%u", reg);
840 case WINED3DSPR_COLOROUT:
841 if (reg >= GL_LIMITS(buffers)) {
842 WARN("Write to render target %u, only %d supported\n", reg, 4);
844 if (GL_SUPPORT(ARB_DRAW_BUFFERS)) {
845 sprintf(tmpStr, "gl_FragData[%u]", reg);
846 } else { /* On older cards with GLSL support like the GeforceFX there's only one buffer. */
847 sprintf(tmpStr, "gl_FragColor");
850 case WINED3DSPR_RASTOUT:
851 sprintf(tmpStr, "%s", hwrastout_reg_names[reg]);
853 case WINED3DSPR_DEPTHOUT:
854 sprintf(tmpStr, "gl_FragDepth");
856 case WINED3DSPR_ATTROUT:
858 sprintf(tmpStr, "gl_FrontColor");
860 sprintf(tmpStr, "gl_FrontSecondaryColor");
863 case WINED3DSPR_TEXCRDOUT:
864 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
865 if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 3)
866 sprintf(tmpStr, "OUT%u", reg);
868 sprintf(tmpStr, "gl_TexCoord[%u]", reg);
870 case WINED3DSPR_MISCTYPE:
873 sprintf(tmpStr, "vpos");
875 /* gl_FrontFacing could be used for vFace, but note that
876 * gl_FrontFacing is a bool, while vFace is a float for
877 * which the sign determines front/back */
878 FIXME("Unhandled misctype register %d\n", reg);
879 sprintf(tmpStr, "unrecognized_register");
883 FIXME("Unhandled register name Type(%d)\n", regtype);
884 sprintf(tmpStr, "unrecognized_register");
888 strcat(regstr, tmpStr);
891 /* Get the GLSL write mask for the destination register */
892 static DWORD shader_glsl_get_write_mask(const DWORD param, char *write_mask) {
893 char *ptr = write_mask;
894 DWORD mask = param & WINED3DSP_WRITEMASK_ALL;
896 if (shader_is_scalar(param)) {
897 mask = WINED3DSP_WRITEMASK_0;
900 if (param & WINED3DSP_WRITEMASK_0) *ptr++ = 'x';
901 if (param & WINED3DSP_WRITEMASK_1) *ptr++ = 'y';
902 if (param & WINED3DSP_WRITEMASK_2) *ptr++ = 'z';
903 if (param & WINED3DSP_WRITEMASK_3) *ptr++ = 'w';
911 static size_t shader_glsl_get_write_mask_size(DWORD write_mask) {
914 if (write_mask & WINED3DSP_WRITEMASK_0) ++size;
915 if (write_mask & WINED3DSP_WRITEMASK_1) ++size;
916 if (write_mask & WINED3DSP_WRITEMASK_2) ++size;
917 if (write_mask & WINED3DSP_WRITEMASK_3) ++size;
922 static void shader_glsl_get_swizzle(const DWORD param, BOOL fixup, DWORD mask, char *swizzle_str) {
923 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
924 * but addressed as "rgba". To fix this we need to swap the register's x
925 * and z components. */
926 DWORD swizzle = (param & WINED3DSP_SWIZZLE_MASK) >> WINED3DSP_SWIZZLE_SHIFT;
927 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
928 char *ptr = swizzle_str;
930 if (!shader_is_scalar(param)) {
932 /* swizzle bits fields: wwzzyyxx */
933 if (mask & WINED3DSP_WRITEMASK_0) *ptr++ = swizzle_chars[swizzle & 0x03];
934 if (mask & WINED3DSP_WRITEMASK_1) *ptr++ = swizzle_chars[(swizzle >> 2) & 0x03];
935 if (mask & WINED3DSP_WRITEMASK_2) *ptr++ = swizzle_chars[(swizzle >> 4) & 0x03];
936 if (mask & WINED3DSP_WRITEMASK_3) *ptr++ = swizzle_chars[(swizzle >> 6) & 0x03];
942 /* From a given parameter token, generate the corresponding GLSL string.
943 * Also, return the actual register name and swizzle in case the
944 * caller needs this information as well. */
945 static void shader_glsl_add_src_param(SHADER_OPCODE_ARG* arg, const DWORD param,
946 const DWORD addr_token, DWORD mask, glsl_src_param_t *src_param) {
947 BOOL is_color = FALSE;
950 src_param->reg_name[0] = '\0';
951 src_param->param_str[0] = '\0';
952 swizzle_str[0] = '\0';
954 shader_glsl_get_register_name(param, addr_token, src_param->reg_name, &is_color, arg);
956 shader_glsl_get_swizzle(param, is_color, mask, swizzle_str);
957 shader_glsl_gen_modifier(param, src_param->reg_name, swizzle_str, src_param->param_str);
960 /* From a given parameter token, generate the corresponding GLSL string.
961 * Also, return the actual register name and swizzle in case the
962 * caller needs this information as well. */
963 static DWORD shader_glsl_add_dst_param(SHADER_OPCODE_ARG* arg, const DWORD param,
964 const DWORD addr_token, glsl_dst_param_t *dst_param) {
965 BOOL is_color = FALSE;
967 dst_param->mask_str[0] = '\0';
968 dst_param->reg_name[0] = '\0';
970 shader_glsl_get_register_name(param, addr_token, dst_param->reg_name, &is_color, arg);
971 return shader_glsl_get_write_mask(param, dst_param->mask_str);
974 /* Append the destination part of the instruction to the buffer, return the effective write mask */
975 static DWORD shader_glsl_append_dst_ext(SHADER_BUFFER *buffer, SHADER_OPCODE_ARG *arg, const DWORD param) {
976 glsl_dst_param_t dst_param;
980 mask = shader_glsl_add_dst_param(arg, param, arg->dst_addr, &dst_param);
983 shift = (param & WINED3DSP_DSTSHIFT_MASK) >> WINED3DSP_DSTSHIFT_SHIFT;
984 shader_addline(buffer, "%s%s = %s(", dst_param.reg_name, dst_param.mask_str, shift_glsl_tab[shift]);
990 /* Append the destination part of the instruction to the buffer, return the effective write mask */
991 static DWORD shader_glsl_append_dst(SHADER_BUFFER *buffer, SHADER_OPCODE_ARG *arg) {
992 return shader_glsl_append_dst_ext(buffer, arg, arg->dst);
995 /** Process GLSL instruction modifiers */
996 void shader_glsl_add_instruction_modifiers(SHADER_OPCODE_ARG* arg) {
998 DWORD mask = arg->dst & WINED3DSP_DSTMOD_MASK;
1000 if (arg->opcode->dst_token && mask != 0) {
1001 glsl_dst_param_t dst_param;
1003 shader_glsl_add_dst_param(arg, arg->dst, 0, &dst_param);
1005 if (mask & WINED3DSPDM_SATURATE) {
1006 /* _SAT means to clamp the value of the register to between 0 and 1 */
1007 shader_addline(arg->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param.reg_name,
1008 dst_param.mask_str, dst_param.reg_name, dst_param.mask_str);
1010 if (mask & WINED3DSPDM_MSAMPCENTROID) {
1011 FIXME("_centroid modifier not handled\n");
1013 if (mask & WINED3DSPDM_PARTIALPRECISION) {
1014 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
1019 static inline const char* shader_get_comp_op(
1020 const DWORD opcode) {
1022 DWORD op = (opcode & INST_CONTROLS_MASK) >> INST_CONTROLS_SHIFT;
1024 case COMPARISON_GT: return ">";
1025 case COMPARISON_EQ: return "==";
1026 case COMPARISON_GE: return ">=";
1027 case COMPARISON_LT: return "<";
1028 case COMPARISON_NE: return "!=";
1029 case COMPARISON_LE: return "<=";
1031 FIXME("Unrecognized comparison value: %u\n", op);
1036 static void shader_glsl_get_sample_function(DWORD sampler_type, BOOL projected, glsl_sample_function_t *sample_function) {
1037 /* Note that there's no such thing as a projected cube texture. */
1038 switch(sampler_type) {
1040 sample_function->name = projected ? "texture1DProj" : "texture1D";
1041 sample_function->coord_mask = WINED3DSP_WRITEMASK_0;
1044 sample_function->name = projected ? "texture2DProj" : "texture2D";
1045 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
1047 case WINED3DSTT_CUBE:
1048 sample_function->name = "textureCube";
1049 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1051 case WINED3DSTT_VOLUME:
1052 sample_function->name = projected ? "texture3DProj" : "texture3D";
1053 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1056 sample_function->name = "";
1057 FIXME("Unrecognized sampler type: %#x;\n", sampler_type);
1062 static void shader_glsl_color_correction(SHADER_OPCODE_ARG* arg) {
1063 IWineD3DBaseShaderImpl* shader = (IWineD3DBaseShaderImpl*) arg->shader;
1064 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) shader->baseShader.device;
1065 WineD3D_GL_Info *gl_info = &deviceImpl->adapter->gl_info;
1066 glsl_dst_param_t dst_param;
1067 glsl_dst_param_t dst_param2;
1069 WINED3DFORMAT conversion_group;
1070 IWineD3DBaseTextureImpl *texture;
1071 DWORD mask, mask_size;
1073 BOOL recorded = FALSE;
1075 DWORD hex_version = shader->baseShader.hex_version;
1077 switch(arg->opcode->opcode) {
1078 case WINED3DSIO_TEX:
1079 if (hex_version < WINED3DPS_VERSION(2,0)) {
1080 sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
1082 sampler_idx = arg->src[1] & WINED3DSP_REGNUM_MASK;
1086 case WINED3DSIO_TEXLDL:
1087 FIXME("Add color fixup for vertex texture WINED3DSIO_TEXLDL\n");
1090 case WINED3DSIO_TEXDP3TEX:
1091 case WINED3DSIO_TEXM3x3TEX:
1092 case WINED3DSIO_TEXM3x3SPEC:
1093 case WINED3DSIO_TEXM3x3VSPEC:
1094 case WINED3DSIO_TEXBEM:
1095 case WINED3DSIO_TEXREG2AR:
1096 case WINED3DSIO_TEXREG2GB:
1097 case WINED3DSIO_TEXREG2RGB:
1098 sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
1102 /* Not a texture sampling instruction, nothing to do */
1106 texture = (IWineD3DBaseTextureImpl *) deviceImpl->stateBlock->textures[sampler_idx];
1108 fmt = texture->resource.format;
1109 conversion_group = texture->baseTexture.shader_conversion_group;
1111 fmt = WINED3DFMT_UNKNOWN;
1112 conversion_group = WINED3DFMT_UNKNOWN;
1115 /* before doing anything, record the sampler with the format in the format conversion list,
1116 * but check if it's not there already
1118 for(i = 0; i < shader->baseShader.num_sampled_samplers; i++) {
1119 if(shader->baseShader.sampled_samplers[i] == sampler_idx) {
1125 shader->baseShader.sampled_samplers[shader->baseShader.num_sampled_samplers] = sampler_idx;
1126 shader->baseShader.num_sampled_samplers++;
1127 shader->baseShader.sampled_format[sampler_idx] = conversion_group;
1131 case WINED3DFMT_V8U8:
1132 case WINED3DFMT_V16U16:
1133 if(GL_SUPPORT(NV_TEXTURE_SHADER) ||
1134 (GL_SUPPORT(ATI_ENVMAP_BUMPMAP) && fmt == WINED3DFMT_V8U8)) {
1135 /* The 3rd channel returns 1.0 in d3d, but 0.0 in gl. Fix this while we're at it :-) */
1136 mask = shader_glsl_add_dst_param(arg, arg->dst, WINED3DSP_WRITEMASK_2, &dst_param);
1137 mask_size = shader_glsl_get_write_mask_size(mask);
1138 if(mask_size >= 3) {
1139 shader_addline(arg->buffer, "%s.%c = 1.0;\n", dst_param.reg_name, dst_param.mask_str[3]);
1142 /* Correct the sign, but leave the blue as it is - it was loaded correctly already */
1143 mask = shader_glsl_add_dst_param(arg, arg->dst,
1144 WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1,
1146 mask_size = shader_glsl_get_write_mask_size(mask);
1147 if(mask_size >= 2) {
1148 shader_addline(arg->buffer, "%s.%c%c = %s.%c%c * 2.0 - 1.0;\n",
1149 dst_param.reg_name, dst_param.mask_str[1], dst_param.mask_str[2],
1150 dst_param.reg_name, dst_param.mask_str[1], dst_param.mask_str[2]);
1151 } else if(mask_size == 1) {
1152 shader_addline(arg->buffer, "%s.%c = %s.%c * 2.0 - 1.0;\n", dst_param.reg_name, dst_param.mask_str[1],
1153 dst_param.reg_name, dst_param.mask_str[1]);
1158 case WINED3DFMT_X8L8V8U8:
1159 if(!GL_SUPPORT(NV_TEXTURE_SHADER)) {
1160 /* Red and blue are the signed channels, fix them up; Blue(=L) is correct already,
1161 * and a(X) is always 1.0
1163 mask = shader_glsl_add_dst_param(arg, arg->dst, WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &dst_param);
1164 mask_size = shader_glsl_get_write_mask_size(mask);
1165 if(mask_size >= 2) {
1166 shader_addline(arg->buffer, "%s.%c%c = %s.%c%c * 2.0 - 1.0;\n",
1167 dst_param.reg_name, dst_param.mask_str[1], dst_param.mask_str[2],
1168 dst_param.reg_name, dst_param.mask_str[1], dst_param.mask_str[2]);
1169 } else if(mask_size == 1) {
1170 shader_addline(arg->buffer, "%s.%c = %s.%c * 2.0 - 1.0;\n",
1171 dst_param.reg_name, dst_param.mask_str[1],
1172 dst_param.reg_name, dst_param.mask_str[1]);
1177 case WINED3DFMT_L6V5U5:
1178 if(!GL_SUPPORT(NV_TEXTURE_SHADER)) {
1179 mask = shader_glsl_add_dst_param(arg, arg->dst, WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &dst_param);
1180 mask_size = shader_glsl_get_write_mask_size(mask);
1181 shader_glsl_add_dst_param(arg, arg->dst, WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_2, &dst_param2);
1182 if(mask_size >= 3) {
1183 /* Swap y and z (U and L), and do a sign conversion on x and the new y(V and U) */
1184 shader_addline(arg->buffer, "tmp0.g = %s.%c;\n",
1185 dst_param.reg_name, dst_param.mask_str[2]);
1186 shader_addline(arg->buffer, "%s.%c%c = %s.%c%c * 2.0 - 1.0;\n",
1187 dst_param.reg_name, dst_param.mask_str[2], dst_param.mask_str[1],
1188 dst_param2.reg_name, dst_param.mask_str[1], dst_param.mask_str[3]);
1189 shader_addline(arg->buffer, "%s.%c = tmp0.g;\n", dst_param.reg_name,
1190 dst_param.mask_str[3]);
1191 } else if(mask_size == 2) {
1192 /* This is bad: We have VL, but we need VU */
1193 FIXME("2 components sampled from a converted L6V5U5 texture\n");
1195 shader_addline(arg->buffer, "%s.%c = %s.%c * 2.0 - 1.0;\n",
1196 dst_param.reg_name, dst_param.mask_str[1],
1197 dst_param2.reg_name, dst_param.mask_str[1]);
1202 case WINED3DFMT_Q8W8V8U8:
1203 if(!GL_SUPPORT(NV_TEXTURE_SHADER)) {
1204 /* Correct the sign in all channels. The writemask just applies as-is, no
1205 * need for checking the mask size
1207 shader_glsl_add_dst_param(arg, arg->dst,
1208 WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 |
1209 WINED3DSP_WRITEMASK_2 | WINED3DSP_WRITEMASK_3,
1211 shader_addline(arg->buffer, "%s%s = %s%s * 2.0 - 1.0;\n", dst_param.reg_name, dst_param.mask_str,
1212 dst_param.reg_name, dst_param.mask_str);
1216 /* stupid compiler */
1222 /*****************************************************************************
1224 * Begin processing individual instruction opcodes
1226 ****************************************************************************/
1228 /* Generate GLSL arithmetic functions (dst = src1 + src2) */
1229 void shader_glsl_arith(SHADER_OPCODE_ARG* arg) {
1230 CONST SHADER_OPCODE* curOpcode = arg->opcode;
1231 SHADER_BUFFER* buffer = arg->buffer;
1232 glsl_src_param_t src0_param;
1233 glsl_src_param_t src1_param;
1237 /* Determine the GLSL operator to use based on the opcode */
1238 switch (curOpcode->opcode) {
1239 case WINED3DSIO_MUL: op = '*'; break;
1240 case WINED3DSIO_ADD: op = '+'; break;
1241 case WINED3DSIO_SUB: op = '-'; break;
1244 FIXME("Opcode %s not yet handled in GLSL\n", curOpcode->name);
1248 write_mask = shader_glsl_append_dst(buffer, arg);
1249 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src0_param);
1250 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1251 shader_addline(buffer, "%s %c %s);\n", src0_param.param_str, op, src1_param.param_str);
1254 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
1255 void shader_glsl_mov(SHADER_OPCODE_ARG* arg) {
1256 IWineD3DBaseShaderImpl* shader = (IWineD3DBaseShaderImpl*) arg->shader;
1257 SHADER_BUFFER* buffer = arg->buffer;
1258 glsl_src_param_t src0_param;
1261 write_mask = shader_glsl_append_dst(buffer, arg);
1262 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src0_param);
1264 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
1265 * shader versions WINED3DSIO_MOVA is used for this. */
1266 if ((WINED3DSHADER_VERSION_MAJOR(shader->baseShader.hex_version) == 1 &&
1267 !shader_is_pshader_version(shader->baseShader.hex_version) &&
1268 shader_get_regtype(arg->dst) == WINED3DSPR_ADDR) ||
1269 arg->opcode->opcode == WINED3DSIO_MOVA) {
1270 /* We need to *round* to the nearest int here. */
1271 size_t mask_size = shader_glsl_get_write_mask_size(write_mask);
1272 if (mask_size > 1) {
1273 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);
1275 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n", src0_param.param_str, src0_param.param_str);
1278 shader_addline(buffer, "%s);\n", src0_param.param_str);
1282 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
1283 void shader_glsl_dot(SHADER_OPCODE_ARG* arg) {
1284 CONST SHADER_OPCODE* curOpcode = arg->opcode;
1285 SHADER_BUFFER* buffer = arg->buffer;
1286 glsl_src_param_t src0_param;
1287 glsl_src_param_t src1_param;
1288 DWORD dst_write_mask, src_write_mask;
1289 size_t dst_size = 0;
1291 dst_write_mask = shader_glsl_append_dst(buffer, arg);
1292 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
1294 /* dp3 works on vec3, dp4 on vec4 */
1295 if (curOpcode->opcode == WINED3DSIO_DP4) {
1296 src_write_mask = WINED3DSP_WRITEMASK_ALL;
1298 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1301 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_write_mask, &src0_param);
1302 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], src_write_mask, &src1_param);
1305 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
1307 shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
1311 /* Note that this instruction has some restrictions. The destination write mask
1312 * can't contain the w component, and the source swizzles have to be .xyzw */
1313 void shader_glsl_cross(SHADER_OPCODE_ARG *arg) {
1314 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1315 glsl_src_param_t src0_param;
1316 glsl_src_param_t src1_param;
1319 shader_glsl_get_write_mask(arg->dst, dst_mask);
1320 shader_glsl_append_dst(arg->buffer, arg);
1321 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
1322 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], src_mask, &src1_param);
1323 shader_addline(arg->buffer, "cross(%s, %s)%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
1326 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
1327 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
1328 * GLSL uses the value as-is. */
1329 void shader_glsl_pow(SHADER_OPCODE_ARG *arg) {
1330 SHADER_BUFFER *buffer = arg->buffer;
1331 glsl_src_param_t src0_param;
1332 glsl_src_param_t src1_param;
1333 DWORD dst_write_mask;
1336 dst_write_mask = shader_glsl_append_dst(buffer, arg);
1337 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
1339 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1340 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0, &src1_param);
1343 shader_addline(buffer, "vec%d(pow(abs(%s), %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
1345 shader_addline(buffer, "pow(abs(%s), %s));\n", src0_param.param_str, src1_param.param_str);
1349 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
1350 void shader_glsl_map2gl(SHADER_OPCODE_ARG* arg) {
1351 CONST SHADER_OPCODE* curOpcode = arg->opcode;
1352 SHADER_BUFFER* buffer = arg->buffer;
1353 glsl_src_param_t src_param;
1354 const char *instruction;
1355 char arguments[256];
1359 /* Determine the GLSL function to use based on the opcode */
1360 /* TODO: Possibly make this a table for faster lookups */
1361 switch (curOpcode->opcode) {
1362 case WINED3DSIO_MIN: instruction = "min"; break;
1363 case WINED3DSIO_MAX: instruction = "max"; break;
1364 case WINED3DSIO_ABS: instruction = "abs"; break;
1365 case WINED3DSIO_FRC: instruction = "fract"; break;
1366 case WINED3DSIO_NRM: instruction = "normalize"; break;
1367 case WINED3DSIO_LOGP:
1368 case WINED3DSIO_LOG: instruction = "log2"; break;
1369 case WINED3DSIO_EXP: instruction = "exp2"; break;
1370 case WINED3DSIO_SGN: instruction = "sign"; break;
1371 case WINED3DSIO_DSX: instruction = "dFdx"; break;
1372 case WINED3DSIO_DSY: instruction = "dFdy"; break;
1373 default: instruction = "";
1374 FIXME("Opcode %s not yet handled in GLSL\n", curOpcode->name);
1378 write_mask = shader_glsl_append_dst(buffer, arg);
1380 arguments[0] = '\0';
1381 if (curOpcode->num_params > 0) {
1382 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src_param);
1383 strcat(arguments, src_param.param_str);
1384 for (i = 2; i < curOpcode->num_params; ++i) {
1385 strcat(arguments, ", ");
1386 shader_glsl_add_src_param(arg, arg->src[i-1], arg->src_addr[i-1], write_mask, &src_param);
1387 strcat(arguments, src_param.param_str);
1391 shader_addline(buffer, "%s(%s));\n", instruction, arguments);
1394 /** Process the WINED3DSIO_EXPP instruction in GLSL:
1395 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
1396 * dst.x = 2^(floor(src))
1397 * dst.y = src - floor(src)
1398 * dst.z = 2^src (partial precision is allowed, but optional)
1400 * For 2.0 shaders, just do this (honoring writemask and swizzle):
1401 * dst = 2^src; (partial precision is allowed, but optional)
1403 void shader_glsl_expp(SHADER_OPCODE_ARG* arg) {
1404 IWineD3DBaseShaderImpl *shader = (IWineD3DBaseShaderImpl *)arg->shader;
1405 glsl_src_param_t src_param;
1407 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src_param);
1409 if (shader->baseShader.hex_version < WINED3DPS_VERSION(2,0)) {
1412 shader_addline(arg->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
1413 shader_addline(arg->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
1414 shader_addline(arg->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
1415 shader_addline(arg->buffer, "tmp0.w = 1.0;\n");
1417 shader_glsl_append_dst(arg->buffer, arg);
1418 shader_glsl_get_write_mask(arg->dst, dst_mask);
1419 shader_addline(arg->buffer, "tmp0%s);\n", dst_mask);
1424 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1425 mask_size = shader_glsl_get_write_mask_size(write_mask);
1427 if (mask_size > 1) {
1428 shader_addline(arg->buffer, "vec%d(exp2(%s)));\n", mask_size, src_param.param_str);
1430 shader_addline(arg->buffer, "exp2(%s));\n", src_param.param_str);
1435 /** Process the RCP (reciprocal or inverse) opcode in GLSL (dst = 1 / src) */
1436 void shader_glsl_rcp(SHADER_OPCODE_ARG* arg) {
1437 glsl_src_param_t src_param;
1441 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1442 mask_size = shader_glsl_get_write_mask_size(write_mask);
1443 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_3, &src_param);
1445 if (mask_size > 1) {
1446 shader_addline(arg->buffer, "vec%d(1.0 / %s));\n", mask_size, src_param.param_str);
1448 shader_addline(arg->buffer, "1.0 / %s);\n", src_param.param_str);
1452 void shader_glsl_rsq(SHADER_OPCODE_ARG* arg) {
1453 SHADER_BUFFER* buffer = arg->buffer;
1454 glsl_src_param_t src_param;
1458 write_mask = shader_glsl_append_dst(buffer, arg);
1459 mask_size = shader_glsl_get_write_mask_size(write_mask);
1461 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_3, &src_param);
1463 if (mask_size > 1) {
1464 shader_addline(buffer, "vec%d(inversesqrt(%s)));\n", mask_size, src_param.param_str);
1466 shader_addline(buffer, "inversesqrt(%s));\n", src_param.param_str);
1470 /** Process signed comparison opcodes in GLSL. */
1471 void shader_glsl_compare(SHADER_OPCODE_ARG* arg) {
1472 glsl_src_param_t src0_param;
1473 glsl_src_param_t src1_param;
1477 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1478 mask_size = shader_glsl_get_write_mask_size(write_mask);
1479 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src0_param);
1480 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1482 if (mask_size > 1) {
1483 const char *compare;
1485 switch(arg->opcode->opcode) {
1486 case WINED3DSIO_SLT: compare = "lessThan"; break;
1487 case WINED3DSIO_SGE: compare = "greaterThanEqual"; break;
1488 default: compare = "";
1489 FIXME("Can't handle opcode %s\n", arg->opcode->name);
1492 shader_addline(arg->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
1493 src0_param.param_str, src1_param.param_str);
1495 const char *compare;
1497 switch(arg->opcode->opcode) {
1498 case WINED3DSIO_SLT: compare = "<"; break;
1499 case WINED3DSIO_SGE: compare = ">="; break;
1500 default: compare = "";
1501 FIXME("Can't handle opcode %s\n", arg->opcode->name);
1504 shader_addline(arg->buffer, "(%s %s %s) ? 1.0 : 0.0);\n",
1505 src0_param.param_str, compare, src1_param.param_str);
1509 /** Process CMP instruction in GLSL (dst = src0 >= 0.0 ? src1 : src2), per channel */
1510 void shader_glsl_cmp(SHADER_OPCODE_ARG* arg) {
1511 glsl_src_param_t src0_param;
1512 glsl_src_param_t src1_param;
1513 glsl_src_param_t src2_param;
1514 DWORD write_mask, cmp_channel = 0;
1517 BOOL temp_destination = FALSE;
1519 DWORD src0reg = arg->src[0] & WINED3DSP_REGNUM_MASK;
1520 DWORD src1reg = arg->src[1] & WINED3DSP_REGNUM_MASK;
1521 DWORD src2reg = arg->src[2] & WINED3DSP_REGNUM_MASK;
1522 DWORD src0regtype = shader_get_regtype(arg->src[0]);
1523 DWORD src1regtype = shader_get_regtype(arg->src[1]);
1524 DWORD src2regtype = shader_get_regtype(arg->src[2]);
1525 DWORD dstreg = arg->dst & WINED3DSP_REGNUM_MASK;
1526 DWORD dstregtype = shader_get_regtype(arg->dst);
1528 /* Cycle through all source0 channels */
1529 for (i=0; i<4; i++) {
1531 /* Find the destination channels which use the current source0 channel */
1532 for (j=0; j<4; j++) {
1533 if ( ((arg->src[0] >> (WINED3DSP_SWIZZLE_SHIFT + 2*j)) & 0x3) == i ) {
1534 write_mask |= WINED3DSP_WRITEMASK_0 << j;
1535 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
1539 /* Splitting the cmp instruction up in multiple lines imposes a problem:
1540 * The first lines may overwrite source parameters of the following lines.
1541 * Deal with that by using a temporary destination register if needed
1543 if((src0reg == dstreg && src0regtype == dstregtype) ||
1544 (src1reg == dstreg && src1regtype == dstregtype) ||
1545 (src2reg == dstreg && src2regtype == dstregtype)) {
1547 write_mask = shader_glsl_get_write_mask(arg->dst & (~WINED3DSP_SWIZZLE_MASK | write_mask), mask_char);
1548 if (!write_mask) continue;
1549 shader_addline(arg->buffer, "tmp0%s = (", mask_char);
1550 temp_destination = TRUE;
1552 write_mask = shader_glsl_append_dst_ext(arg->buffer, arg, arg->dst & (~WINED3DSP_SWIZZLE_MASK | write_mask));
1553 if (!write_mask) continue;
1556 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], cmp_channel, &src0_param);
1557 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1558 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, &src2_param);
1560 shader_addline(arg->buffer, "%s >= 0.0 ? %s : %s);\n",
1561 src0_param.param_str, src1_param.param_str, src2_param.param_str);
1564 if(temp_destination) {
1565 shader_glsl_get_write_mask(arg->dst, mask_char);
1566 shader_glsl_append_dst_ext(arg->buffer, arg, arg->dst);
1567 shader_addline(arg->buffer, "tmp0%s);\n", mask_char);
1572 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
1573 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
1574 * the compare is done per component of src0. */
1575 void shader_glsl_cnd(SHADER_OPCODE_ARG* arg) {
1576 IWineD3DBaseShaderImpl* shader = (IWineD3DBaseShaderImpl*) arg->shader;
1577 glsl_src_param_t src0_param;
1578 glsl_src_param_t src1_param;
1579 glsl_src_param_t src2_param;
1580 DWORD write_mask, cmp_channel = 0;
1583 if (shader->baseShader.hex_version < WINED3DPS_VERSION(1, 4)) {
1584 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1585 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1586 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1587 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, &src2_param);
1589 /* Fun: The D3DSI_COISSUE flag changes the semantic of the cnd instruction for < 1.4 shaders */
1590 if(arg->opcode_token & WINED3DSI_COISSUE) {
1591 shader_addline(arg->buffer, "%s /* COISSUE! */);\n", src1_param.param_str);
1593 shader_addline(arg->buffer, "%s > 0.5 ? %s : %s);\n",
1594 src0_param.param_str, src1_param.param_str, src2_param.param_str);
1598 /* Cycle through all source0 channels */
1599 for (i=0; i<4; i++) {
1601 /* Find the destination channels which use the current source0 channel */
1602 for (j=0; j<4; j++) {
1603 if ( ((arg->src[0] >> (WINED3DSP_SWIZZLE_SHIFT + 2*j)) & 0x3) == i ) {
1604 write_mask |= WINED3DSP_WRITEMASK_0 << j;
1605 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
1608 write_mask = shader_glsl_append_dst_ext(arg->buffer, arg, arg->dst & (~WINED3DSP_SWIZZLE_MASK | write_mask));
1609 if (!write_mask) continue;
1611 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], cmp_channel, &src0_param);
1612 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1613 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, &src2_param);
1615 shader_addline(arg->buffer, "%s > 0.5 ? %s : %s);\n",
1616 src0_param.param_str, src1_param.param_str, src2_param.param_str);
1620 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
1621 void shader_glsl_mad(SHADER_OPCODE_ARG* arg) {
1622 glsl_src_param_t src0_param;
1623 glsl_src_param_t src1_param;
1624 glsl_src_param_t src2_param;
1627 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1628 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src0_param);
1629 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1630 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, &src2_param);
1631 shader_addline(arg->buffer, "(%s * %s) + %s);\n",
1632 src0_param.param_str, src1_param.param_str, src2_param.param_str);
1635 /** Handles transforming all WINED3DSIO_M?x? opcodes for
1636 Vertex shaders to GLSL codes */
1637 void shader_glsl_mnxn(SHADER_OPCODE_ARG* arg) {
1639 int nComponents = 0;
1640 SHADER_OPCODE_ARG tmpArg;
1642 memset(&tmpArg, 0, sizeof(SHADER_OPCODE_ARG));
1644 /* Set constants for the temporary argument */
1645 tmpArg.shader = arg->shader;
1646 tmpArg.buffer = arg->buffer;
1647 tmpArg.src[0] = arg->src[0];
1648 tmpArg.src_addr[0] = arg->src_addr[0];
1649 tmpArg.src_addr[1] = arg->src_addr[1];
1650 tmpArg.reg_maps = arg->reg_maps;
1652 switch(arg->opcode->opcode) {
1653 case WINED3DSIO_M4x4:
1655 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP4);
1657 case WINED3DSIO_M4x3:
1659 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP4);
1661 case WINED3DSIO_M3x4:
1663 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP3);
1665 case WINED3DSIO_M3x3:
1667 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP3);
1669 case WINED3DSIO_M3x2:
1671 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP3);
1677 for (i = 0; i < nComponents; i++) {
1678 tmpArg.dst = ((arg->dst) & ~WINED3DSP_WRITEMASK_ALL)|(WINED3DSP_WRITEMASK_0<<i);
1679 tmpArg.src[1] = arg->src[1]+i;
1680 shader_glsl_dot(&tmpArg);
1685 The LRP instruction performs a component-wise linear interpolation
1686 between the second and third operands using the first operand as the
1687 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
1688 This is equivalent to mix(src2, src1, src0);
1690 void shader_glsl_lrp(SHADER_OPCODE_ARG* arg) {
1691 glsl_src_param_t src0_param;
1692 glsl_src_param_t src1_param;
1693 glsl_src_param_t src2_param;
1696 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1698 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src0_param);
1699 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1700 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, &src2_param);
1702 shader_addline(arg->buffer, "mix(%s, %s, %s));\n",
1703 src2_param.param_str, src1_param.param_str, src0_param.param_str);
1706 /** Process the WINED3DSIO_LIT instruction in GLSL:
1707 * dst.x = dst.w = 1.0
1708 * dst.y = (src0.x > 0) ? src0.x
1709 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
1710 * where src.w is clamped at +- 128
1712 void shader_glsl_lit(SHADER_OPCODE_ARG* arg) {
1713 glsl_src_param_t src0_param;
1714 glsl_src_param_t src1_param;
1715 glsl_src_param_t src3_param;
1718 shader_glsl_append_dst(arg->buffer, arg);
1719 shader_glsl_get_write_mask(arg->dst, dst_mask);
1721 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1722 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_1, &src1_param);
1723 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_3, &src3_param);
1725 shader_addline(arg->buffer, "vec4(1.0, (%s > 0.0 ? %s : 0.0), (%s > 0.0 ? ((%s > 0.0) ? pow(%s, clamp(%s, -128.0, 128.0)) : 0.0) : 0.0), 1.0)%s);\n",
1726 src0_param.param_str, src0_param.param_str, src0_param.param_str, src1_param.param_str, src1_param.param_str, src3_param.param_str, dst_mask);
1729 /** Process the WINED3DSIO_DST instruction in GLSL:
1731 * dst.y = src0.x * src0.y
1735 void shader_glsl_dst(SHADER_OPCODE_ARG* arg) {
1736 glsl_src_param_t src0y_param;
1737 glsl_src_param_t src0z_param;
1738 glsl_src_param_t src1y_param;
1739 glsl_src_param_t src1w_param;
1742 shader_glsl_append_dst(arg->buffer, arg);
1743 shader_glsl_get_write_mask(arg->dst, dst_mask);
1745 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_1, &src0y_param);
1746 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_2, &src0z_param);
1747 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_1, &src1y_param);
1748 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_3, &src1w_param);
1750 shader_addline(arg->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
1751 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
1754 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
1755 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
1756 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
1758 * dst.x = cos(src0.?)
1759 * dst.y = sin(src0.?)
1763 void shader_glsl_sincos(SHADER_OPCODE_ARG* arg) {
1764 glsl_src_param_t src0_param;
1767 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1768 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1770 switch (write_mask) {
1771 case WINED3DSP_WRITEMASK_0:
1772 shader_addline(arg->buffer, "cos(%s));\n", src0_param.param_str);
1775 case WINED3DSP_WRITEMASK_1:
1776 shader_addline(arg->buffer, "sin(%s));\n", src0_param.param_str);
1779 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
1780 shader_addline(arg->buffer, "vec2(cos(%s), sin(%s)));\n", src0_param.param_str, src0_param.param_str);
1784 ERR("Write mask should be .x, .y or .xy\n");
1789 /** Process the WINED3DSIO_LOOP instruction in GLSL:
1790 * Start a for() loop where src1.y is the initial value of aL,
1791 * increment aL by src1.z for a total of src1.x iterations.
1792 * Need to use a temporary variable for this operation.
1794 /* FIXME: I don't think nested loops will work correctly this way. */
1795 void shader_glsl_loop(SHADER_OPCODE_ARG* arg) {
1796 glsl_src_param_t src1_param;
1798 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_ALL, &src1_param);
1800 shader_addline(arg->buffer, "for (tmpInt = 0, aL = %s.y; tmpInt < %s.x; tmpInt++, aL += %s.z) {\n",
1801 src1_param.reg_name, src1_param.reg_name, src1_param.reg_name);
1804 void shader_glsl_end(SHADER_OPCODE_ARG* arg) {
1805 shader_addline(arg->buffer, "}\n");
1808 void shader_glsl_rep(SHADER_OPCODE_ARG* arg) {
1809 glsl_src_param_t src0_param;
1811 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1812 shader_addline(arg->buffer, "for (tmpInt = 0; tmpInt < %s; tmpInt++) {\n", src0_param.param_str);
1815 void shader_glsl_if(SHADER_OPCODE_ARG* arg) {
1816 glsl_src_param_t src0_param;
1818 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1819 shader_addline(arg->buffer, "if (%s) {\n", src0_param.param_str);
1822 void shader_glsl_ifc(SHADER_OPCODE_ARG* arg) {
1823 glsl_src_param_t src0_param;
1824 glsl_src_param_t src1_param;
1826 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1827 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0, &src1_param);
1829 shader_addline(arg->buffer, "if (%s %s %s) {\n",
1830 src0_param.param_str, shader_get_comp_op(arg->opcode_token), src1_param.param_str);
1833 void shader_glsl_else(SHADER_OPCODE_ARG* arg) {
1834 shader_addline(arg->buffer, "} else {\n");
1837 void shader_glsl_break(SHADER_OPCODE_ARG* arg) {
1838 shader_addline(arg->buffer, "break;\n");
1841 /* FIXME: According to MSDN the compare is done per component. */
1842 void shader_glsl_breakc(SHADER_OPCODE_ARG* arg) {
1843 glsl_src_param_t src0_param;
1844 glsl_src_param_t src1_param;
1846 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1847 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0, &src1_param);
1849 shader_addline(arg->buffer, "if (%s %s %s) break;\n",
1850 src0_param.param_str, shader_get_comp_op(arg->opcode_token), src1_param.param_str);
1853 void shader_glsl_label(SHADER_OPCODE_ARG* arg) {
1855 DWORD snum = (arg->src[0]) & WINED3DSP_REGNUM_MASK;
1856 shader_addline(arg->buffer, "}\n");
1857 shader_addline(arg->buffer, "void subroutine%lu () {\n", snum);
1860 void shader_glsl_call(SHADER_OPCODE_ARG* arg) {
1861 DWORD snum = (arg->src[0]) & WINED3DSP_REGNUM_MASK;
1862 shader_addline(arg->buffer, "subroutine%lu();\n", snum);
1865 void shader_glsl_callnz(SHADER_OPCODE_ARG* arg) {
1866 glsl_src_param_t src1_param;
1868 DWORD snum = (arg->src[0]) & WINED3DSP_REGNUM_MASK;
1869 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0, &src1_param);
1870 shader_addline(arg->buffer, "if (%s) subroutine%lu();\n", src1_param.param_str, snum);
1873 /*********************************************
1874 * Pixel Shader Specific Code begins here
1875 ********************************************/
1876 void pshader_glsl_tex(SHADER_OPCODE_ARG* arg) {
1877 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1878 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
1879 DWORD hex_version = This->baseShader.hex_version;
1880 char dst_swizzle[6];
1881 glsl_sample_function_t sample_function;
1887 /* All versions have a destination register */
1888 shader_glsl_append_dst(arg->buffer, arg);
1890 /* 1.0-1.4: Use destination register as sampler source.
1891 * 2.0+: Use provided sampler source. */
1892 if (hex_version < WINED3DPS_VERSION(1,4)) {
1895 sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
1896 flags = deviceImpl->stateBlock->textureState[sampler_idx][WINED3DTSS_TEXTURETRANSFORMFLAGS];
1898 if (flags & WINED3DTTFF_PROJECTED) {
1900 switch (flags & ~WINED3DTTFF_PROJECTED) {
1901 case WINED3DTTFF_COUNT1: FIXME("WINED3DTTFF_PROJECTED with WINED3DTTFF_COUNT1?\n"); break;
1902 case WINED3DTTFF_COUNT2: mask = WINED3DSP_WRITEMASK_1; break;
1903 case WINED3DTTFF_COUNT3: mask = WINED3DSP_WRITEMASK_2; break;
1904 case WINED3DTTFF_COUNT4:
1905 case WINED3DTTFF_DISABLE: mask = WINED3DSP_WRITEMASK_3; break;
1910 } else if (hex_version < WINED3DPS_VERSION(2,0)) {
1911 DWORD src_mod = arg->src[0] & WINED3DSP_SRCMOD_MASK;
1912 sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
1914 if (src_mod == WINED3DSPSM_DZ) {
1916 mask = WINED3DSP_WRITEMASK_2;
1917 } else if (src_mod == WINED3DSPSM_DW) {
1919 mask = WINED3DSP_WRITEMASK_3;
1924 sampler_idx = arg->src[1] & WINED3DSP_REGNUM_MASK;
1925 if(arg->opcode_token & WINED3DSI_TEXLD_PROJECT) {
1926 /* ps 2.0 texldp instruction always divides by the fourth component. */
1928 mask = WINED3DSP_WRITEMASK_3;
1934 sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
1935 shader_glsl_get_sample_function(sampler_type, projected, &sample_function);
1936 mask |= sample_function.coord_mask;
1938 if (hex_version < WINED3DPS_VERSION(2,0)) {
1939 shader_glsl_get_write_mask(arg->dst, dst_swizzle);
1941 shader_glsl_get_swizzle(arg->src[1], FALSE, arg->dst, dst_swizzle);
1944 /* 1.0-1.3: Use destination register as coordinate source.
1945 1.4+: Use provided coordinate source register. */
1946 if (hex_version < WINED3DPS_VERSION(1,4)) {
1948 shader_glsl_get_write_mask(mask, coord_mask);
1949 shader_addline(arg->buffer, "%s(Psampler%u, T%u%s)%s);\n",
1950 sample_function.name, sampler_idx, sampler_idx, coord_mask, dst_swizzle);
1952 glsl_src_param_t coord_param;
1953 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], mask, &coord_param);
1954 if(arg->opcode_token & WINED3DSI_TEXLD_BIAS) {
1955 glsl_src_param_t bias;
1956 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_3, &bias);
1958 shader_addline(arg->buffer, "%s(Psampler%u, %s, %s)%s);\n",
1959 sample_function.name, sampler_idx, coord_param.param_str,
1960 bias.param_str, dst_swizzle);
1962 shader_addline(arg->buffer, "%s(Psampler%u, %s)%s);\n",
1963 sample_function.name, sampler_idx, coord_param.param_str, dst_swizzle);
1968 void shader_glsl_texldl(SHADER_OPCODE_ARG* arg) {
1969 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*)arg->shader;
1970 glsl_sample_function_t sample_function;
1971 glsl_src_param_t coord_param, lod_param;
1972 char dst_swizzle[6];
1976 shader_glsl_append_dst(arg->buffer, arg);
1977 shader_glsl_get_swizzle(arg->src[1], FALSE, arg->dst, dst_swizzle);
1979 sampler_idx = arg->src[1] & WINED3DSP_REGNUM_MASK;
1980 sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
1981 shader_glsl_get_sample_function(sampler_type, FALSE, &sample_function);
1982 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], sample_function.coord_mask, &coord_param);
1984 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_3, &lod_param);
1986 if (shader_is_pshader_version(This->baseShader.hex_version)) {
1987 /* The GLSL spec claims the Lod sampling functions are only supported in vertex shaders.
1988 * However, they seem to work just fine in fragment shaders as well. */
1989 WARN("Using %sLod in fragment shader.\n", sample_function.name);
1990 shader_addline(arg->buffer, "%sLod(Psampler%u, %s, %s)%s);\n",
1991 sample_function.name, sampler_idx, coord_param.param_str, lod_param.param_str, dst_swizzle);
1993 shader_addline(arg->buffer, "%sLod(Vsampler%u, %s, %s)%s);\n",
1994 sample_function.name, sampler_idx, coord_param.param_str, lod_param.param_str, dst_swizzle);
1998 void pshader_glsl_texcoord(SHADER_OPCODE_ARG* arg) {
2000 /* FIXME: Make this work for more than just 2D textures */
2002 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
2003 SHADER_BUFFER* buffer = arg->buffer;
2004 DWORD hex_version = This->baseShader.hex_version;
2008 write_mask = shader_glsl_append_dst(arg->buffer, arg);
2009 shader_glsl_get_write_mask(write_mask, dst_mask);
2011 if (hex_version != WINED3DPS_VERSION(1,4)) {
2012 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2013 shader_addline(buffer, "clamp(gl_TexCoord[%u], 0.0, 1.0)%s);\n", reg, dst_mask);
2015 DWORD reg = arg->src[0] & WINED3DSP_REGNUM_MASK;
2016 DWORD src_mod = arg->src[0] & WINED3DSP_SRCMOD_MASK;
2017 char dst_swizzle[6];
2019 shader_glsl_get_swizzle(arg->src[0], FALSE, write_mask, dst_swizzle);
2021 if (src_mod == WINED3DSPSM_DZ) {
2022 glsl_src_param_t div_param;
2023 size_t mask_size = shader_glsl_get_write_mask_size(write_mask);
2024 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_2, &div_param);
2026 if (mask_size > 1) {
2027 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
2029 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
2031 } else if (src_mod == WINED3DSPSM_DW) {
2032 glsl_src_param_t div_param;
2033 size_t mask_size = shader_glsl_get_write_mask_size(write_mask);
2034 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_3, &div_param);
2036 if (mask_size > 1) {
2037 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
2039 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
2042 shader_addline(buffer, "gl_TexCoord[%u]%s);\n", reg, dst_swizzle);
2047 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
2048 * Take a 3-component dot product of the TexCoord[dstreg] and src,
2049 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
2050 void pshader_glsl_texdp3tex(SHADER_OPCODE_ARG* arg) {
2051 glsl_src_param_t src0_param;
2053 glsl_sample_function_t sample_function;
2054 DWORD sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2055 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2056 DWORD sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
2058 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2060 shader_glsl_append_dst(arg->buffer, arg);
2061 shader_glsl_get_write_mask(arg->dst, dst_mask);
2063 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
2064 * scalar, and projected sampling would require 4
2066 shader_glsl_get_sample_function(sampler_type, FALSE, &sample_function);
2068 switch(count_bits(sample_function.coord_mask)) {
2070 shader_addline(arg->buffer, "%s(Psampler%u, dot(gl_TexCoord[%u].xyz, %s))%s);\n",
2071 sample_function.name, sampler_idx, sampler_idx, src0_param.param_str, dst_mask);
2075 shader_addline(arg->buffer, "%s(Psampler%u, vec2(dot(gl_TexCoord[%u].xyz, %s), 0.0))%s);\n",
2076 sample_function.name, sampler_idx, sampler_idx, src0_param.param_str, dst_mask);
2080 shader_addline(arg->buffer, "%s(Psampler%u, vec3(dot(gl_TexCoord[%u].xyz, %s), 0.0, 0.0))%s);\n",
2081 sample_function.name, sampler_idx, sampler_idx, src0_param.param_str, dst_mask);
2084 FIXME("Unexpected mask bitcount %d\n", count_bits(sample_function.coord_mask));
2088 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
2089 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
2090 void pshader_glsl_texdp3(SHADER_OPCODE_ARG* arg) {
2091 glsl_src_param_t src0_param;
2092 DWORD dstreg = arg->dst & WINED3DSP_REGNUM_MASK;
2093 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2097 dst_mask = shader_glsl_append_dst(arg->buffer, arg);
2098 mask_size = shader_glsl_get_write_mask_size(dst_mask);
2099 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2101 if (mask_size > 1) {
2102 shader_addline(arg->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
2104 shader_addline(arg->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
2108 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
2109 * Calculate the depth as dst.x / dst.y */
2110 void pshader_glsl_texdepth(SHADER_OPCODE_ARG* arg) {
2111 glsl_dst_param_t dst_param;
2113 shader_glsl_add_dst_param(arg, arg->dst, 0, &dst_param);
2115 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
2116 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
2117 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
2118 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
2121 shader_addline(arg->buffer, "gl_FragDepth = (%s.y == 0.0) ? 1.0 : clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n", dst_param.reg_name, dst_param.reg_name, dst_param.reg_name);
2124 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
2125 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
2126 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
2127 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
2129 void pshader_glsl_texm3x2depth(SHADER_OPCODE_ARG* arg) {
2130 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2131 DWORD dstreg = arg->dst & WINED3DSP_REGNUM_MASK;
2132 glsl_src_param_t src0_param;
2134 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2136 shader_addline(arg->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
2137 shader_addline(arg->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
2140 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
2141 * Calculate the 1st of a 2-row matrix multiplication. */
2142 void pshader_glsl_texm3x2pad(SHADER_OPCODE_ARG* arg) {
2143 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2144 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2145 SHADER_BUFFER* buffer = arg->buffer;
2146 glsl_src_param_t src0_param;
2148 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2149 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
2152 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
2153 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
2154 void pshader_glsl_texm3x3pad(SHADER_OPCODE_ARG* arg) {
2156 IWineD3DPixelShaderImpl* shader = (IWineD3DPixelShaderImpl*) arg->shader;
2157 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2158 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2159 SHADER_BUFFER* buffer = arg->buffer;
2160 SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
2161 glsl_src_param_t src0_param;
2163 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2164 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + current_state->current_row, reg, src0_param.param_str);
2165 current_state->texcoord_w[current_state->current_row++] = reg;
2168 void pshader_glsl_texm3x2tex(SHADER_OPCODE_ARG* arg) {
2169 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2170 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2171 SHADER_BUFFER* buffer = arg->buffer;
2172 glsl_src_param_t src0_param;
2175 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2176 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
2178 shader_glsl_append_dst(buffer, arg);
2179 shader_glsl_get_write_mask(arg->dst, dst_mask);
2181 /* Sample the texture using the calculated coordinates */
2182 shader_addline(buffer, "texture2D(Psampler%u, tmp0.xy)%s);\n", reg, dst_mask);
2185 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
2186 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
2187 void pshader_glsl_texm3x3tex(SHADER_OPCODE_ARG* arg) {
2188 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2189 glsl_src_param_t src0_param;
2191 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2192 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
2193 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
2194 DWORD sampler_type = arg->reg_maps->samplers[reg] & WINED3DSP_TEXTURETYPE_MASK;
2195 glsl_sample_function_t sample_function;
2197 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2198 shader_addline(arg->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
2200 shader_glsl_append_dst(arg->buffer, arg);
2201 shader_glsl_get_write_mask(arg->dst, dst_mask);
2202 shader_glsl_get_sample_function(sampler_type, FALSE, &sample_function);
2204 /* Sample the texture using the calculated coordinates */
2205 shader_addline(arg->buffer, "%s(Psampler%u, tmp0.xyz)%s);\n", sample_function.name, reg, dst_mask);
2207 current_state->current_row = 0;
2210 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
2211 * Perform the 3rd row of a 3x3 matrix multiply */
2212 void pshader_glsl_texm3x3(SHADER_OPCODE_ARG* arg) {
2213 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2214 glsl_src_param_t src0_param;
2216 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2217 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
2218 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
2220 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2222 shader_glsl_append_dst(arg->buffer, arg);
2223 shader_glsl_get_write_mask(arg->dst, dst_mask);
2224 shader_addline(arg->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
2226 current_state->current_row = 0;
2229 /** Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
2230 * Peform the final texture lookup based on the previous 2 3x3 matrix multiplies */
2231 void pshader_glsl_texm3x3spec(SHADER_OPCODE_ARG* arg) {
2233 IWineD3DPixelShaderImpl* shader = (IWineD3DPixelShaderImpl*) arg->shader;
2234 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2235 glsl_src_param_t src0_param;
2236 glsl_src_param_t src1_param;
2238 SHADER_BUFFER* buffer = arg->buffer;
2239 SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
2240 DWORD stype = arg->reg_maps->samplers[reg] & WINED3DSP_TEXTURETYPE_MASK;
2241 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2242 glsl_sample_function_t sample_function;
2244 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2245 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], src_mask, &src1_param);
2247 /* Perform the last matrix multiply operation */
2248 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
2249 /* Reflection calculation */
2250 shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
2252 shader_glsl_append_dst(buffer, arg);
2253 shader_glsl_get_write_mask(arg->dst, dst_mask);
2254 shader_glsl_get_sample_function(stype, FALSE, &sample_function);
2256 /* Sample the texture */
2257 shader_addline(buffer, "%s(Psampler%u, tmp0.xyz)%s);\n", sample_function.name, reg, dst_mask);
2259 current_state->current_row = 0;
2262 /** Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
2263 * Peform the final texture lookup based on the previous 2 3x3 matrix multiplies */
2264 void pshader_glsl_texm3x3vspec(SHADER_OPCODE_ARG* arg) {
2266 IWineD3DPixelShaderImpl* shader = (IWineD3DPixelShaderImpl*) arg->shader;
2267 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2268 SHADER_BUFFER* buffer = arg->buffer;
2269 SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
2270 glsl_src_param_t src0_param;
2272 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2273 DWORD sampler_type = arg->reg_maps->samplers[reg] & WINED3DSP_TEXTURETYPE_MASK;
2274 glsl_sample_function_t sample_function;
2276 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2278 /* Perform the last matrix multiply operation */
2279 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
2281 /* Construct the eye-ray vector from w coordinates */
2282 shader_addline(buffer, "tmp1.xyz = normalize(vec3(gl_TexCoord[%u].w, gl_TexCoord[%u].w, gl_TexCoord[%u].w));\n",
2283 current_state->texcoord_w[0], current_state->texcoord_w[1], reg);
2284 shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
2286 shader_glsl_append_dst(buffer, arg);
2287 shader_glsl_get_write_mask(arg->dst, dst_mask);
2288 shader_glsl_get_sample_function(sampler_type, FALSE, &sample_function);
2290 /* Sample the texture using the calculated coordinates */
2291 shader_addline(buffer, "%s(Psampler%u, tmp0.xyz)%s);\n", sample_function.name, reg, dst_mask);
2293 current_state->current_row = 0;
2296 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
2297 * Apply a fake bump map transform.
2298 * texbem is pshader <= 1.3 only, this saves a few version checks
2300 void pshader_glsl_texbem(SHADER_OPCODE_ARG* arg) {
2301 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
2302 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
2303 char dst_swizzle[6];
2304 glsl_sample_function_t sample_function;
2305 glsl_src_param_t coord_param;
2312 sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2313 flags = deviceImpl->stateBlock->textureState[sampler_idx][WINED3DTSS_TEXTURETRANSFORMFLAGS];
2315 sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
2316 shader_glsl_get_sample_function(sampler_type, FALSE, &sample_function);
2317 mask = sample_function.coord_mask;
2319 shader_glsl_get_write_mask(arg->dst, dst_swizzle);
2321 shader_glsl_get_write_mask(mask, coord_mask);
2323 /* with projective textures, texbem only divides the static texture coord, not the displacement,
2324 * so we can't let the GL handle this.
2326 if (flags & WINED3DTTFF_PROJECTED) {
2328 char coord_div_mask[3];
2329 switch (flags & ~WINED3DTTFF_PROJECTED) {
2330 case WINED3DTTFF_COUNT1: FIXME("WINED3DTTFF_PROJECTED with WINED3DTTFF_COUNT1?\n"); break;
2331 case WINED3DTTFF_COUNT2: div_mask = WINED3DSP_WRITEMASK_1; break;
2332 case WINED3DTTFF_COUNT3: div_mask = WINED3DSP_WRITEMASK_2; break;
2333 case WINED3DTTFF_COUNT4:
2334 case WINED3DTTFF_DISABLE: div_mask = WINED3DSP_WRITEMASK_3; break;
2336 shader_glsl_get_write_mask(div_mask, coord_div_mask);
2337 shader_addline(arg->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
2340 shader_glsl_append_dst(arg->buffer, arg);
2341 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0|WINED3DSP_WRITEMASK_1, &coord_param);
2342 if(arg->opcode->opcode == WINED3DSIO_TEXBEML) {
2343 glsl_src_param_t luminance_param;
2344 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_2, &luminance_param);
2345 shader_addline(arg->buffer, "(%s(Psampler%u, T%u%s + vec4(bumpenvmat * %s, 0.0, 0.0)%s )*(%s * luminancescale + luminanceoffset))%s);\n",
2346 sample_function.name, sampler_idx, sampler_idx, coord_mask, coord_param.param_str, coord_mask,
2347 luminance_param.param_str, dst_swizzle);
2349 shader_addline(arg->buffer, "%s(Psampler%u, T%u%s + vec4(bumpenvmat * %s, 0.0, 0.0)%s )%s);\n",
2350 sample_function.name, sampler_idx, sampler_idx, coord_mask, coord_param.param_str, coord_mask, dst_swizzle);
2354 void pshader_glsl_bem(SHADER_OPCODE_ARG* arg) {
2355 glsl_src_param_t src0_param, src1_param;
2357 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0|WINED3DSP_WRITEMASK_1, &src0_param);
2358 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0|WINED3DSP_WRITEMASK_1, &src1_param);
2360 shader_glsl_append_dst(arg->buffer, arg);
2361 shader_addline(arg->buffer, "%s + bumpenvmat * %s);\n",
2362 src0_param.param_str, src1_param.param_str);
2365 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
2366 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
2367 void pshader_glsl_texreg2ar(SHADER_OPCODE_ARG* arg) {
2369 glsl_src_param_t src0_param;
2370 DWORD sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2373 shader_glsl_append_dst(arg->buffer, arg);
2374 shader_glsl_get_write_mask(arg->dst, dst_mask);
2375 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
2377 shader_addline(arg->buffer, "texture2D(Psampler%u, %s.wx)%s);\n", sampler_idx, src0_param.reg_name, dst_mask);
2380 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
2381 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
2382 void pshader_glsl_texreg2gb(SHADER_OPCODE_ARG* arg) {
2383 glsl_src_param_t src0_param;
2384 DWORD sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2387 shader_glsl_append_dst(arg->buffer, arg);
2388 shader_glsl_get_write_mask(arg->dst, dst_mask);
2389 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
2391 shader_addline(arg->buffer, "texture2D(Psampler%u, %s.yz)%s);\n", sampler_idx, src0_param.reg_name, dst_mask);
2394 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
2395 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
2396 void pshader_glsl_texreg2rgb(SHADER_OPCODE_ARG* arg) {
2397 glsl_src_param_t src0_param;
2399 DWORD sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2400 DWORD sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
2401 glsl_sample_function_t sample_function;
2403 shader_glsl_append_dst(arg->buffer, arg);
2404 shader_glsl_get_write_mask(arg->dst, dst_mask);
2405 shader_glsl_get_sample_function(sampler_type, FALSE, &sample_function);
2406 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], sample_function.coord_mask, &src0_param);
2408 shader_addline(arg->buffer, "%s(Psampler%u, %s)%s);\n", sample_function.name, sampler_idx, src0_param.param_str, dst_mask);
2411 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
2412 * If any of the first 3 components are < 0, discard this pixel */
2413 void pshader_glsl_texkill(SHADER_OPCODE_ARG* arg) {
2414 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
2415 DWORD hex_version = This->baseShader.hex_version;
2416 glsl_dst_param_t dst_param;
2418 /* The argument is a destination parameter, and no writemasks are allowed */
2419 shader_glsl_add_dst_param(arg, arg->dst, 0, &dst_param);
2420 if((hex_version >= WINED3DPS_VERSION(2,0))) {
2421 /* 2.0 shaders compare all 4 components in texkill */
2422 shader_addline(arg->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
2424 /* 1.X shaders only compare the first 3 components, propably due to the nature of the texkill
2425 * instruction as a tex* instruction, and phase, which kills all a / w components. Even if all
2426 * 4 components are defined, only the first 3 are used
2428 shader_addline(arg->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
2432 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
2433 * dst = dot2(src0, src1) + src2 */
2434 void pshader_glsl_dp2add(SHADER_OPCODE_ARG* arg) {
2435 glsl_src_param_t src0_param;
2436 glsl_src_param_t src1_param;
2437 glsl_src_param_t src2_param;
2441 write_mask = shader_glsl_append_dst(arg->buffer, arg);
2442 mask_size = shader_glsl_get_write_mask_size(write_mask);
2444 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
2445 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
2446 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], WINED3DSP_WRITEMASK_0, &src2_param);
2448 shader_addline(arg->buffer, "dot(%s, %s) + %s);\n", src0_param.param_str, src1_param.param_str, src2_param.param_str);
2451 void pshader_glsl_input_pack(
2452 SHADER_BUFFER* buffer,
2453 semantic* semantics_in) {
2457 for (i = 0; i < MAX_REG_INPUT; i++) {
2459 DWORD usage_token = semantics_in[i].usage;
2460 DWORD register_token = semantics_in[i].reg;
2461 DWORD usage, usage_idx;
2465 if (!usage_token) continue;
2466 usage = (usage_token & WINED3DSP_DCL_USAGE_MASK) >> WINED3DSP_DCL_USAGE_SHIFT;
2467 usage_idx = (usage_token & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
2468 shader_glsl_get_write_mask(register_token, reg_mask);
2472 case WINED3DDECLUSAGE_COLOR:
2474 shader_addline(buffer, "IN%u%s = vec4(gl_Color)%s;\n",
2475 i, reg_mask, reg_mask);
2476 else if (usage_idx == 1)
2477 shader_addline(buffer, "IN%u%s = vec4(gl_SecondaryColor)%s;\n",
2478 i, reg_mask, reg_mask);
2480 shader_addline(buffer, "IN%u%s = vec4(unsupported_color_input)%s;\n",
2481 i, reg_mask, reg_mask);
2484 case WINED3DDECLUSAGE_TEXCOORD:
2485 shader_addline(buffer, "IN%u%s = vec4(gl_TexCoord[%u])%s;\n",
2486 i, reg_mask, usage_idx, reg_mask );
2489 case WINED3DDECLUSAGE_FOG:
2490 shader_addline(buffer, "IN%u%s = vec4(gl_FogFragCoord)%s;\n",
2491 i, reg_mask, reg_mask);
2495 shader_addline(buffer, "IN%u%s = vec4(unsupported_input)%s;\n",
2496 i, reg_mask, reg_mask);
2501 /*********************************************
2502 * Vertex Shader Specific Code begins here
2503 ********************************************/
2505 void vshader_glsl_output_unpack(
2506 SHADER_BUFFER* buffer,
2507 semantic* semantics_out) {
2511 for (i = 0; i < MAX_REG_OUTPUT; i++) {
2513 DWORD usage_token = semantics_out[i].usage;
2514 DWORD register_token = semantics_out[i].reg;
2515 DWORD usage, usage_idx;
2519 if (!usage_token) continue;
2521 usage = (usage_token & WINED3DSP_DCL_USAGE_MASK) >> WINED3DSP_DCL_USAGE_SHIFT;
2522 usage_idx = (usage_token & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
2523 shader_glsl_get_write_mask(register_token, reg_mask);
2527 case WINED3DDECLUSAGE_COLOR:
2529 shader_addline(buffer, "gl_FrontColor%s = OUT%u%s;\n", reg_mask, i, reg_mask);
2530 else if (usage_idx == 1)
2531 shader_addline(buffer, "gl_FrontSecondaryColor%s = OUT%u%s;\n", reg_mask, i, reg_mask);
2533 shader_addline(buffer, "unsupported_color_output%s = OUT%u%s;\n", reg_mask, i, reg_mask);
2536 case WINED3DDECLUSAGE_POSITION:
2537 shader_addline(buffer, "gl_Position%s = OUT%u%s;\n", reg_mask, i, reg_mask);
2540 case WINED3DDECLUSAGE_TEXCOORD:
2541 shader_addline(buffer, "gl_TexCoord[%u]%s = OUT%u%s;\n",
2542 usage_idx, reg_mask, i, reg_mask);
2545 case WINED3DDECLUSAGE_PSIZE:
2546 shader_addline(buffer, "gl_PointSize = OUT%u.x;\n", i);
2549 case WINED3DDECLUSAGE_FOG:
2550 shader_addline(buffer, "gl_FogFragCoord = OUT%u%s;\n", i, reg_mask);
2554 shader_addline(buffer, "unsupported_output%s = OUT%u%s;\n", reg_mask, i, reg_mask);
2559 static void add_glsl_program_entry(IWineD3DDeviceImpl *device, struct glsl_shader_prog_link *entry) {
2560 glsl_program_key_t *key;
2562 key = HeapAlloc(GetProcessHeap(), 0, sizeof(glsl_program_key_t));
2563 key->vshader = entry->vshader;
2564 key->pshader = entry->pshader;
2566 hash_table_put(device->glsl_program_lookup, key, entry);
2569 static struct glsl_shader_prog_link *get_glsl_program_entry(IWineD3DDeviceImpl *device,
2570 GLhandleARB vshader, GLhandleARB pshader) {
2571 glsl_program_key_t key;
2573 key.vshader = vshader;
2574 key.pshader = pshader;
2576 return (struct glsl_shader_prog_link *)hash_table_get(device->glsl_program_lookup, &key);
2579 void delete_glsl_program_entry(IWineD3DDevice *iface, struct glsl_shader_prog_link *entry) {
2580 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
2581 WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
2582 glsl_program_key_t *key;
2584 key = HeapAlloc(GetProcessHeap(), 0, sizeof(glsl_program_key_t));
2585 key->vshader = entry->vshader;
2586 key->pshader = entry->pshader;
2587 hash_table_remove(This->glsl_program_lookup, key);
2589 GL_EXTCALL(glDeleteObjectARB(entry->programId));
2590 if (entry->vshader) list_remove(&entry->vshader_entry);
2591 if (entry->pshader) list_remove(&entry->pshader_entry);
2592 HeapFree(GetProcessHeap(), 0, entry->vuniformF_locations);
2593 HeapFree(GetProcessHeap(), 0, entry->puniformF_locations);
2594 HeapFree(GetProcessHeap(), 0, entry);
2597 /** Sets the GLSL program ID for the given pixel and vertex shader combination.
2598 * It sets the programId on the current StateBlock (because it should be called
2599 * inside of the DrawPrimitive() part of the render loop).
2601 * If a program for the given combination does not exist, create one, and store
2602 * the program in the hash table. If it creates a program, it will link the
2603 * given objects, too.
2605 static void set_glsl_shader_program(IWineD3DDevice *iface, BOOL use_ps, BOOL use_vs) {
2606 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
2607 WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
2608 IWineD3DPixelShader *pshader = This->stateBlock->pixelShader;
2609 IWineD3DVertexShader *vshader = This->stateBlock->vertexShader;
2610 struct glsl_shader_prog_link *entry = NULL;
2611 GLhandleARB programId = 0;
2615 GLhandleARB vshader_id = use_vs ? ((IWineD3DBaseShaderImpl*)vshader)->baseShader.prgId : 0;
2616 GLhandleARB pshader_id = use_ps ? ((IWineD3DBaseShaderImpl*)pshader)->baseShader.prgId : 0;
2617 entry = get_glsl_program_entry(This, vshader_id, pshader_id);
2619 This->stateBlock->glsl_program = entry;
2623 /* If we get to this point, then no matching program exists, so we create one */
2624 programId = GL_EXTCALL(glCreateProgramObjectARB());
2625 TRACE("Created new GLSL shader program %u\n", programId);
2627 /* Create the entry */
2628 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link));
2629 entry->programId = programId;
2630 entry->vshader = vshader_id;
2631 entry->pshader = pshader_id;
2632 /* Add the hash table entry */
2633 add_glsl_program_entry(This, entry);
2635 /* Set the current program */
2636 This->stateBlock->glsl_program = entry;
2638 /* Attach GLSL vshader */
2640 int max_attribs = 16; /* TODO: Will this always be the case? It is at the moment... */
2643 TRACE("Attaching GLSL shader object %u to program %u\n", vshader_id, programId);
2644 GL_EXTCALL(glAttachObjectARB(programId, vshader_id));
2645 checkGLcall("glAttachObjectARB");
2647 /* Bind vertex attributes to a corresponding index number to match
2648 * the same index numbers as ARB_vertex_programs (makes loading
2649 * vertex attributes simpler). With this method, we can use the
2650 * exact same code to load the attributes later for both ARB and
2653 * We have to do this here because we need to know the Program ID
2654 * in order to make the bindings work, and it has to be done prior
2655 * to linking the GLSL program. */
2656 for (i = 0; i < max_attribs; ++i) {
2657 snprintf(tmp_name, sizeof(tmp_name), "attrib%i", i);
2658 GL_EXTCALL(glBindAttribLocationARB(programId, i, tmp_name));
2660 checkGLcall("glBindAttribLocationARB");
2662 list_add_head(&((IWineD3DBaseShaderImpl *)vshader)->baseShader.linked_programs, &entry->vshader_entry);
2665 /* Attach GLSL pshader */
2667 TRACE("Attaching GLSL shader object %u to program %u\n", pshader_id, programId);
2668 GL_EXTCALL(glAttachObjectARB(programId, pshader_id));
2669 checkGLcall("glAttachObjectARB");
2671 list_add_head(&((IWineD3DBaseShaderImpl *)pshader)->baseShader.linked_programs, &entry->pshader_entry);
2674 /* Link the program */
2675 TRACE("Linking GLSL shader program %u\n", programId);
2676 GL_EXTCALL(glLinkProgramARB(programId));
2677 print_glsl_info_log(&GLINFO_LOCATION, programId);
2679 entry->vuniformF_locations = HeapAlloc(GetProcessHeap(), 0, sizeof(GLhandleARB) * GL_LIMITS(vshader_constantsF));
2680 for (i = 0; i < GL_LIMITS(vshader_constantsF); ++i) {
2681 snprintf(glsl_name, sizeof(glsl_name), "VC[%i]", i);
2682 entry->vuniformF_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
2684 entry->puniformF_locations = HeapAlloc(GetProcessHeap(), 0, sizeof(GLhandleARB) * GL_LIMITS(pshader_constantsF));
2685 for (i = 0; i < GL_LIMITS(pshader_constantsF); ++i) {
2686 snprintf(glsl_name, sizeof(glsl_name), "PC[%i]", i);
2687 entry->puniformF_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
2691 static GLhandleARB create_glsl_blt_shader(WineD3D_GL_Info *gl_info) {
2692 GLhandleARB program_id;
2693 GLhandleARB vshader_id, pshader_id;
2694 const char *blt_vshader[] = {
2697 " gl_Position = gl_Vertex;\n"
2698 " gl_FrontColor = vec4(1.0);\n"
2699 " gl_TexCoord[0].x = (gl_Vertex.x * 0.5) + 0.5;\n"
2700 " gl_TexCoord[0].y = (-gl_Vertex.y * 0.5) + 0.5;\n"
2704 const char *blt_pshader[] = {
2705 "uniform sampler2D sampler;\n"
2708 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
2712 vshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
2713 GL_EXTCALL(glShaderSourceARB(vshader_id, 1, blt_vshader, NULL));
2714 GL_EXTCALL(glCompileShaderARB(vshader_id));
2716 pshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
2717 GL_EXTCALL(glShaderSourceARB(pshader_id, 1, blt_pshader, NULL));
2718 GL_EXTCALL(glCompileShaderARB(pshader_id));
2720 program_id = GL_EXTCALL(glCreateProgramObjectARB());
2721 GL_EXTCALL(glAttachObjectARB(program_id, vshader_id));
2722 GL_EXTCALL(glAttachObjectARB(program_id, pshader_id));
2723 GL_EXTCALL(glLinkProgramARB(program_id));
2725 print_glsl_info_log(&GLINFO_LOCATION, program_id);
2730 static void shader_glsl_select(IWineD3DDevice *iface, BOOL usePS, BOOL useVS) {
2731 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
2732 WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
2733 GLhandleARB program_id = 0;
2735 if (useVS || usePS) set_glsl_shader_program(iface, usePS, useVS);
2736 else This->stateBlock->glsl_program = NULL;
2738 program_id = This->stateBlock->glsl_program ? This->stateBlock->glsl_program->programId : 0;
2739 if (program_id) TRACE("Using GLSL program %u\n", program_id);
2740 GL_EXTCALL(glUseProgramObjectARB(program_id));
2741 checkGLcall("glUseProgramObjectARB");
2744 static void shader_glsl_select_depth_blt(IWineD3DDevice *iface) {
2745 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
2746 WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
2747 static GLhandleARB program_id = 0;
2748 static GLhandleARB loc = -1;
2751 program_id = create_glsl_blt_shader(gl_info);
2752 loc = GL_EXTCALL(glGetUniformLocationARB(program_id, "sampler"));
2755 GL_EXTCALL(glUseProgramObjectARB(program_id));
2756 GL_EXTCALL(glUniform1iARB(loc, 0));
2759 static void shader_glsl_cleanup(IWineD3DDevice *iface) {
2760 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
2761 WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
2762 GL_EXTCALL(glUseProgramObjectARB(0));
2765 const shader_backend_t glsl_shader_backend = {
2766 &shader_glsl_select,
2767 &shader_glsl_select_depth_blt,
2768 &shader_glsl_load_constants,
2769 &shader_glsl_cleanup,
2770 &shader_glsl_color_correction