2 * Pixel and vertex shaders implementation using ARB_vertex_program
3 * and ARB_fragment_program GL extensions.
5 * Copyright 2002-2003 Jason Edmeades
6 * Copyright 2002-2003 Raphael Junqueira
7 * Copyright 2004 Christian Costa
8 * Copyright 2005 Oliver Stieber
9 * Copyright 2006 Ivan Gyurdiev
10 * Copyright 2006 Jason Green
11 * Copyright 2006 Henri Verbeet
13 * This library is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Lesser General Public
15 * License as published by the Free Software Foundation; either
16 * version 2.1 of the License, or (at your option) any later version.
18 * This library is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Lesser General Public License for more details.
23 * You should have received a copy of the GNU Lesser General Public
24 * License along with this library; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
33 #include "wined3d_private.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
36 WINE_DECLARE_DEBUG_CHANNEL(d3d_constants);
37 WINE_DECLARE_DEBUG_CHANNEL(d3d_caps);
39 #define GLINFO_LOCATION (*gl_info)
41 /********************************************************
42 * ARB_[vertex/fragment]_program helper functions follow
43 ********************************************************/
46 * Loads floating point constants into the currently set ARB_vertex/fragment_program.
47 * When constant_list == NULL, it will load all the constants.
49 * @target_type should be either GL_VERTEX_PROGRAM_ARB (for vertex shaders)
50 * or GL_FRAGMENT_PROGRAM_ARB (for pixel shaders)
52 static unsigned int shader_arb_load_constantsF(IWineD3DBaseShaderImpl* This, WineD3D_GL_Info *gl_info, GLuint target_type,
53 unsigned int max_constants, float* constants, char *dirty_consts) {
54 local_constant* lconst;
58 if (TRACE_ON(d3d_shader)) {
59 for(i = 0; i < max_constants; i++) {
60 if(!dirty_consts[i]) continue;
61 TRACE_(d3d_constants)("Loading constants %i: %f, %f, %f, %f\n", i,
62 constants[i * 4 + 0], constants[i * 4 + 1],
63 constants[i * 4 + 2], constants[i * 4 + 3]);
66 /* In 1.X pixel shaders constants are implicitly clamped in the range [-1;1] */
67 if(target_type == GL_FRAGMENT_PROGRAM_ARB &&
68 WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) == 1) {
70 for(i = 0; i < max_constants; i++) {
71 if(!dirty_consts[i]) continue;
75 if(constants[j + 0] > 1.0) lcl_const[0] = 1.0;
76 else if(constants[j + 0] < -1.0) lcl_const[0] = -1.0;
77 else lcl_const[0] = constants[j + 0];
79 if(constants[j + 1] > 1.0) lcl_const[1] = 1.0;
80 else if(constants[j + 1] < -1.0) lcl_const[1] = -1.0;
81 else lcl_const[1] = constants[j + 1];
83 if(constants[j + 2] > 1.0) lcl_const[2] = 1.0;
84 else if(constants[j + 2] < -1.0) lcl_const[2] = -1.0;
85 else lcl_const[2] = constants[j + 2];
87 if(constants[j + 3] > 1.0) lcl_const[3] = 1.0;
88 else if(constants[j + 3] < -1.0) lcl_const[3] = -1.0;
89 else lcl_const[3] = constants[j + 3];
91 GL_EXTCALL(glProgramEnvParameter4fvARB(target_type, i, lcl_const));
94 if(GL_SUPPORT(EXT_GPU_PROGRAM_PARAMETERS)) {
95 /* TODO: Benchmark if we're better of with finding the dirty constants ourselves,
96 * or just reloading *all* constants at once
98 GL_EXTCALL(glProgramEnvParameters4fvEXT(target_type, 0, max_constants, constants));
100 for(i = 0; i < max_constants; i++) {
101 if(!dirty_consts[i]) continue;
103 /* Find the next block of dirty constants */
106 for(i++; (i < max_constants) && dirty_consts[i]; i++) {
110 GL_EXTCALL(glProgramEnvParameters4fvEXT(target_type, j, i - j, constants + (j * 4)));
113 for(i = 0; i < max_constants; i++) {
114 if(dirty_consts[i]) {
116 GL_EXTCALL(glProgramEnvParameter4fvARB(target_type, i, constants + (i * 4)));
121 checkGLcall("glProgramEnvParameter4fvARB()");
123 /* Load immediate constants */
124 if(This->baseShader.load_local_constsF) {
125 if (TRACE_ON(d3d_shader)) {
126 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
127 GLfloat* values = (GLfloat*)lconst->value;
128 TRACE_(d3d_constants)("Loading local constants %i: %f, %f, %f, %f\n", lconst->idx,
129 values[0], values[1], values[2], values[3]);
132 /* Immediate constants are clamped for 1.X shaders at loading times */
134 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
135 dirty_consts[lconst->idx] = 1; /* Dirtify so the non-immediate constant overwrites it next time */
136 ret = max(ret, lconst->idx);
137 GL_EXTCALL(glProgramEnvParameter4fvARB(target_type, lconst->idx, (GLfloat*)lconst->value));
139 checkGLcall("glProgramEnvParameter4fvARB()");
140 return ret; /* The loaded immediate constants need reloading for the next shader */
142 return 0; /* No constants are dirty now */
147 * Loads the app-supplied constants into the currently set ARB_[vertex/fragment]_programs.
149 * We only support float constants in ARB at the moment, so don't
150 * worry about the Integers or Booleans
152 void shader_arb_load_constants(
153 IWineD3DDevice* device,
155 char useVertexShader) {
157 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) device;
158 IWineD3DStateBlockImpl* stateBlock = deviceImpl->stateBlock;
159 WineD3D_GL_Info *gl_info = &deviceImpl->adapter->gl_info;
162 if (useVertexShader) {
163 IWineD3DBaseShaderImpl* vshader = (IWineD3DBaseShaderImpl*) stateBlock->vertexShader;
165 /* Load DirectX 9 float constants for vertex shader */
166 deviceImpl->highest_dirty_vs_const = shader_arb_load_constantsF(
167 vshader, gl_info, GL_VERTEX_PROGRAM_ARB,
168 deviceImpl->highest_dirty_vs_const,
169 stateBlock->vertexShaderConstantF,
170 deviceImpl->activeContext->vshader_const_dirty);
172 /* Upload the position fixup */
173 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_VERTEX_PROGRAM_ARB, ARB_SHADER_PRIVCONST_POS, deviceImpl->posFixup));
176 if (usePixelShader) {
178 IWineD3DBaseShaderImpl* pshader = (IWineD3DBaseShaderImpl*) stateBlock->pixelShader;
179 IWineD3DPixelShaderImpl *psi = (IWineD3DPixelShaderImpl *) pshader;
181 /* Load DirectX 9 float constants for pixel shader */
182 deviceImpl->highest_dirty_ps_const = shader_arb_load_constantsF(
183 pshader, gl_info, GL_FRAGMENT_PROGRAM_ARB,
184 deviceImpl->highest_dirty_ps_const,
185 stateBlock->pixelShaderConstantF,
186 deviceImpl->activeContext->pshader_const_dirty);
188 for(i = 0; i < psi->numbumpenvmatconsts; i++) {
189 /* The state manager takes care that this function is always called if the bump env matrix changes
191 float *data = (float *) &stateBlock->textureState[(int) psi->bumpenvmatconst[i].texunit][WINED3DTSS_BUMPENVMAT00];
192 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, psi->bumpenvmatconst[i].const_num, data));
193 deviceImpl->activeContext->pshader_const_dirty[psi->bumpenvmatconst[i].const_num] = 1;
195 if(psi->luminanceconst[i].const_num != -1) {
196 /* WINED3DTSS_BUMPENVLSCALE and WINED3DTSS_BUMPENVLOFFSET are next to each other.
197 * point gl to the scale, and load 4 floats. x = scale, y = offset, z and w are junk, we
198 * don't care about them. The pointers are valid for sure because the stateblock is bigger.
199 * (they're WINED3DTSS_TEXTURETRANSFORMFLAGS and WINED3DTSS_ADDRESSW, so most likely 0 or NaN
201 float *scale = (float *) &stateBlock->textureState[(int) psi->luminanceconst[i].texunit][WINED3DTSS_BUMPENVLSCALE];
202 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, psi->luminanceconst[i].const_num, scale));
203 deviceImpl->activeContext->pshader_const_dirty[psi->luminanceconst[i].const_num] = 1;
207 if(((IWineD3DPixelShaderImpl *) pshader)->srgb_enabled &&
208 !((IWineD3DPixelShaderImpl *) pshader)->srgb_mode_hardcoded) {
212 if(stateBlock->renderState[WINED3DRS_SRGBWRITEENABLE]) {
213 comparison[0] = srgb_cmp; comparison[1] = srgb_cmp;
214 comparison[2] = srgb_cmp; comparison[3] = srgb_cmp;
216 mul_low[0] = srgb_mul_low; mul_low[1] = srgb_mul_low;
217 mul_low[2] = srgb_mul_low; mul_low[3] = srgb_mul_low;
219 comparison[0] = 1.0 / 0.0; comparison[1] = 1.0 / 0.0;
220 comparison[2] = 1.0 / 0.0; comparison[3] = 1.0 / 0.0;
222 mul_low[0] = 1.0; mul_low[1] = 1.0;
223 mul_low[2] = 1.0; mul_low[3] = 1.0;
225 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, psi->srgb_cmp_const, comparison));
226 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, psi->srgb_low_const, mul_low));
227 checkGLcall("Load sRGB correction constants\n");
228 deviceImpl->activeContext->pshader_const_dirty[psi->srgb_low_const] = 1;
229 deviceImpl->activeContext->pshader_const_dirty[psi->srgb_cmp_const] = 1;
235 /* Generate the variable & register declarations for the ARB_vertex_program output target */
236 void shader_generate_arb_declarations(
237 IWineD3DBaseShader *iface,
238 shader_reg_maps* reg_maps,
239 SHADER_BUFFER* buffer,
240 WineD3D_GL_Info* gl_info) {
242 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
243 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) This->baseShader.device;
245 char pshader = shader_is_pshader_version(This->baseShader.hex_version);
246 unsigned max_constantsF = min(This->baseShader.limits.constant_float,
247 (pshader ? GL_LIMITS(pshader_constantsF) : GL_LIMITS(vshader_constantsF)));
248 UINT extra_constants_needed = 0;
249 local_constant* lconst;
251 /* Temporary Output register */
252 shader_addline(buffer, "TEMP TMP_OUT;\n");
254 for(i = 0; i < This->baseShader.limits.temporary; i++) {
255 if (reg_maps->temporary[i])
256 shader_addline(buffer, "TEMP R%u;\n", i);
259 for (i = 0; i < This->baseShader.limits.address; i++) {
260 if (reg_maps->address[i])
261 shader_addline(buffer, "ADDRESS A%d;\n", i);
264 for(i = 0; i < This->baseShader.limits.texcoord; i++) {
265 if (reg_maps->texcoord[i])
266 shader_addline(buffer,"TEMP T%u;\n", i);
269 /* Texture coordinate registers must be pre-loaded */
270 for (i = 0; i < This->baseShader.limits.texcoord; i++) {
271 if (reg_maps->texcoord[i])
272 shader_addline(buffer, "MOV T%u, fragment.texcoord[%u];\n", i, i);
275 for(i = 0; i < (sizeof(reg_maps->bumpmat) / sizeof(reg_maps->bumpmat[0])); i++) {
276 IWineD3DPixelShaderImpl *ps = (IWineD3DPixelShaderImpl *) This;
277 if(!reg_maps->bumpmat[i]) continue;
279 cur = ps->numbumpenvmatconsts;
280 ps->bumpenvmatconst[cur].const_num = -1;
281 ps->bumpenvmatconst[cur].texunit = i;
282 ps->luminanceconst[cur].const_num = -1;
283 ps->luminanceconst[cur].texunit = i;
285 /* If the shader does not use all available constants, use the next free constant to load the bump mapping environment matrix from
286 * the stateblock into the shader. If no constant is available don't load, texbem will then just sample the texture without applying
289 if(max_constantsF + extra_constants_needed < GL_LIMITS(pshader_constantsF)) {
290 ps->bumpenvmatconst[cur].const_num = max_constantsF + extra_constants_needed;
291 shader_addline(buffer, "PARAM bumpenvmat%d = program.env[%d];\n",
292 i, ps->bumpenvmatconst[cur].const_num);
293 extra_constants_needed++;
295 if(reg_maps->luminanceparams && max_constantsF + extra_constants_needed < GL_LIMITS(pshader_constantsF)) {
296 ((IWineD3DPixelShaderImpl *)This)->luminanceconst[cur].const_num = max_constantsF + extra_constants_needed;
297 shader_addline(buffer, "PARAM luminance%d = program.env[%d];\n",
298 i, ps->luminanceconst[cur].const_num);
299 extra_constants_needed++;
300 } else if(reg_maps->luminanceparams) {
301 FIXME("No free constant to load the luminance parameters\n");
304 FIXME("No free constant found to load environemnt bump mapping matrix into the shader. texbem instruction will not apply bump mapping\n");
307 ps->numbumpenvmatconsts = cur + 1;
310 if(device->stateBlock->renderState[WINED3DRS_SRGBWRITEENABLE] && pshader) {
311 IWineD3DPixelShaderImpl *ps_impl = (IWineD3DPixelShaderImpl *) This;
312 /* If there are 2 constants left to use, use them to pass the sRGB correction values in. This way
313 * srgb write correction can be turned on and off dynamically without recompilation. Otherwise
314 * hardcode them. The drawback of hardcoding is that the shader needs recompilation to turn sRGB
317 if(max_constantsF + extra_constants_needed + 1 < GL_LIMITS(pshader_constantsF) && FALSE) {
318 /* The idea is that if srgb is enabled, then disabled, the constant loading code
319 * can effectively disable sRGB correction by passing 1.0 and INF as the multiplication
320 * and comparison constants. If it disables it that way, the shader won't be recompiled
321 * and the code will stay in, so sRGB writing can be turned on again by setting the
322 * constants from the spec
324 ps_impl->srgb_mode_hardcoded = 0;
325 ps_impl->srgb_low_const = GL_LIMITS(pshader_constantsF) - extra_constants_needed;
326 ps_impl->srgb_cmp_const = GL_LIMITS(pshader_constantsF) - extra_constants_needed - 1;
327 shader_addline(buffer, "PARAM srgb_mul_low = program.env[%d];\n", ps_impl->srgb_low_const);
328 shader_addline(buffer, "PARAM srgb_comparison = program.env[%d];\n", ps_impl->srgb_cmp_const);
330 shader_addline(buffer, "PARAM srgb_mul_low = {%f, %f, %f, 1.0};\n",
331 srgb_mul_low, srgb_mul_low, srgb_mul_low);
332 shader_addline(buffer, "PARAM srgb_comparison = {%f, %f, %f, %f};\n",
333 srgb_cmp, srgb_cmp, srgb_cmp, srgb_cmp);
334 ps_impl->srgb_mode_hardcoded = 1;
336 /* These can be hardcoded, they do not cause any harm because no fragment will enter the high
337 * path if the comparison value is set to INF
339 shader_addline(buffer, "PARAM srgb_pow = {%f, %f, %f, 1.0};\n",
340 srgb_pow, srgb_pow, srgb_pow);
341 shader_addline(buffer, "PARAM srgb_mul_hi = {%f, %f, %f, 1.0};\n",
342 srgb_mul_high, srgb_mul_high, srgb_mul_high);
343 shader_addline(buffer, "PARAM srgb_sub_hi = {%f, %f, %f, 0.0};\n",
344 srgb_sub_high, srgb_sub_high, srgb_sub_high);
345 ps_impl->srgb_enabled = 1;
347 IWineD3DPixelShaderImpl *ps_impl = (IWineD3DPixelShaderImpl *) This;
349 /* Do not write any srgb fixup into the shader to save shader size and processing time.
350 * As a consequence, we can't toggle srgb write on without recompilation
352 ps_impl->srgb_enabled = 0;
353 ps_impl->srgb_mode_hardcoded = 1;
356 /* Hardcodable local constants */
357 if(!This->baseShader.load_local_constsF) {
358 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
359 float *value = (float *) lconst->value;
360 shader_addline(buffer, "PARAM C%u = {%f, %f, %f, %f};\n", lconst->idx,
361 value[0], value[1], value[2], value[3]);
365 /* we use the array-based constants array if the local constants are marked for loading,
366 * because then we use indirect addressing, or when the local constant list is empty,
367 * because then we don't know if we're using indirect addressing or not. If we're hardcoding
368 * local constants do not declare the loaded constants as an array because ARB compilers usually
369 * do not optimize unused constants away
371 if(This->baseShader.load_local_constsF || list_empty(&This->baseShader.constantsF)) {
372 /* Need to PARAM the environment parameters (constants) so we can use relative addressing */
373 shader_addline(buffer, "PARAM C[%d] = { program.env[0..%d] };\n",
374 max_constantsF, max_constantsF - 1);
376 for(i = 0; i < max_constantsF; i++) {
377 if(!shader_constant_is_local(This, i)) {
378 shader_addline(buffer, "PARAM C%d = program.env[%d];\n",i, i);
384 static const char * const shift_tab[] = {
385 "dummy", /* 0 (none) */
386 "coefmul.x", /* 1 (x2) */
387 "coefmul.y", /* 2 (x4) */
388 "coefmul.z", /* 3 (x8) */
389 "coefmul.w", /* 4 (x16) */
390 "dummy", /* 5 (x32) */
391 "dummy", /* 6 (x64) */
392 "dummy", /* 7 (x128) */
393 "dummy", /* 8 (d256) */
394 "dummy", /* 9 (d128) */
395 "dummy", /* 10 (d64) */
396 "dummy", /* 11 (d32) */
397 "coefdiv.w", /* 12 (d16) */
398 "coefdiv.z", /* 13 (d8) */
399 "coefdiv.y", /* 14 (d4) */
400 "coefdiv.x" /* 15 (d2) */
403 static void shader_arb_get_write_mask(SHADER_OPCODE_ARG* arg, const DWORD param, char *write_mask) {
404 IWineD3DBaseShaderImpl *This = (IWineD3DBaseShaderImpl *) arg->shader;
405 char *ptr = write_mask;
406 char vshader = shader_is_vshader_version(This->baseShader.hex_version);
408 if(vshader && shader_get_regtype(param) == WINED3DSPR_ADDR) {
411 } else if ((param & WINED3DSP_WRITEMASK_ALL) != WINED3DSP_WRITEMASK_ALL) {
413 if (param & WINED3DSP_WRITEMASK_0) *ptr++ = 'x';
414 if (param & WINED3DSP_WRITEMASK_1) *ptr++ = 'y';
415 if (param & WINED3DSP_WRITEMASK_2) *ptr++ = 'z';
416 if (param & WINED3DSP_WRITEMASK_3) *ptr++ = 'w';
422 static void shader_arb_get_swizzle(const DWORD param, BOOL fixup, char *swizzle_str) {
423 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
424 * but addressed as "rgba". To fix this we need to swap the register's x
425 * and z components. */
426 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
427 char *ptr = swizzle_str;
429 /* swizzle bits fields: wwzzyyxx */
430 DWORD swizzle = (param & WINED3DSP_SWIZZLE_MASK) >> WINED3DSP_SWIZZLE_SHIFT;
431 DWORD swizzle_x = swizzle & 0x03;
432 DWORD swizzle_y = (swizzle >> 2) & 0x03;
433 DWORD swizzle_z = (swizzle >> 4) & 0x03;
434 DWORD swizzle_w = (swizzle >> 6) & 0x03;
436 /* If the swizzle is the default swizzle (ie, "xyzw"), we don't need to
437 * generate a swizzle string. Unless we need to our own swizzling. */
438 if ((WINED3DSP_NOSWIZZLE >> WINED3DSP_SWIZZLE_SHIFT) != swizzle || fixup) {
440 if (swizzle_x == swizzle_y && swizzle_x == swizzle_z && swizzle_x == swizzle_w) {
441 *ptr++ = swizzle_chars[swizzle_x];
443 *ptr++ = swizzle_chars[swizzle_x];
444 *ptr++ = swizzle_chars[swizzle_y];
445 *ptr++ = swizzle_chars[swizzle_z];
446 *ptr++ = swizzle_chars[swizzle_w];
453 static void pshader_get_register_name(IWineD3DBaseShader* iface,
454 const DWORD param, char* regstr) {
456 DWORD reg = param & WINED3DSP_REGNUM_MASK;
457 DWORD regtype = shader_get_regtype(param);
458 IWineD3DBaseShaderImpl *This = (IWineD3DBaseShaderImpl *) iface;
461 case WINED3DSPR_TEMP:
462 sprintf(regstr, "R%u", reg);
464 case WINED3DSPR_INPUT:
466 strcpy(regstr, "fragment.color.primary");
468 strcpy(regstr, "fragment.color.secondary");
471 case WINED3DSPR_CONST:
472 if(This->baseShader.load_local_constsF || list_empty(&This->baseShader.constantsF)) {
473 sprintf(regstr, "C[%u]", reg);
475 sprintf(regstr, "C%u", reg);
478 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
479 sprintf(regstr,"T%u", reg);
481 case WINED3DSPR_COLOROUT:
483 sprintf(regstr, "TMP_COLOR");
485 /* TODO: See GL_ARB_draw_buffers */
486 FIXME("Unsupported write to render target %u\n", reg);
487 sprintf(regstr, "unsupported_register");
490 case WINED3DSPR_DEPTHOUT:
491 sprintf(regstr, "result.depth");
493 case WINED3DSPR_ATTROUT:
494 sprintf(regstr, "oD[%u]", reg);
496 case WINED3DSPR_TEXCRDOUT:
497 sprintf(regstr, "oT[%u]", reg);
500 FIXME("Unhandled register name Type(%d)\n", regtype);
501 sprintf(regstr, "unrecognized_register");
506 /* TODO: merge with pixel shader */
507 static void vshader_program_add_param(SHADER_OPCODE_ARG *arg, const DWORD param, BOOL is_input, char *hwLine) {
509 IWineD3DVertexShaderImpl* This = (IWineD3DVertexShaderImpl*) arg->shader;
511 /* oPos, oFog and oPts in D3D */
512 static const char * const hwrastout_reg_names[] = { "TMP_OUT", "result.fogcoord", "result.pointsize" };
514 DWORD reg = param & WINED3DSP_REGNUM_MASK;
515 DWORD regtype = shader_get_regtype(param);
517 BOOL is_color = FALSE;
519 if ((param & WINED3DSP_SRCMOD_MASK) == WINED3DSPSM_NEG) {
520 strcat(hwLine, " -");
526 case WINED3DSPR_TEMP:
527 sprintf(tmpReg, "R%u", reg);
528 strcat(hwLine, tmpReg);
530 case WINED3DSPR_INPUT:
532 if (vshader_input_is_color((IWineD3DVertexShader*) This, reg))
535 sprintf(tmpReg, "vertex.attrib[%u]", reg);
536 strcat(hwLine, tmpReg);
538 case WINED3DSPR_CONST:
539 if(param & WINED3DSHADER_ADDRMODE_RELATIVE) {
540 if(reg >= This->rel_offset) {
541 sprintf(tmpReg, "C[A0.x + %u]", reg - This->rel_offset);
543 sprintf(tmpReg, "C[A0.x - %u]", -reg + This->rel_offset);
546 if(This->baseShader.load_local_constsF || list_empty(&This->baseShader.constantsF)) {
547 sprintf(tmpReg, "C[%u]", reg);
549 sprintf(tmpReg, "C%u", reg);
552 strcat(hwLine, tmpReg);
554 case WINED3DSPR_ADDR: /*case D3DSPR_TEXTURE:*/
555 sprintf(tmpReg, "A%u", reg);
556 strcat(hwLine, tmpReg);
558 case WINED3DSPR_RASTOUT:
559 sprintf(tmpReg, "%s", hwrastout_reg_names[reg]);
560 strcat(hwLine, tmpReg);
562 case WINED3DSPR_ATTROUT:
564 strcat(hwLine, "result.color.primary");
566 strcat(hwLine, "result.color.secondary");
569 case WINED3DSPR_TEXCRDOUT:
570 sprintf(tmpReg, "result.texcoord[%u]", reg);
571 strcat(hwLine, tmpReg);
574 FIXME("Unknown reg type %d %d\n", regtype, reg);
575 strcat(hwLine, "unrecognized_register");
581 shader_arb_get_write_mask(arg, param, write_mask);
582 strcat(hwLine, write_mask);
585 shader_arb_get_swizzle(param, is_color, swizzle);
586 strcat(hwLine, swizzle);
590 static void shader_hw_sample(SHADER_OPCODE_ARG* arg, DWORD sampler_idx, const char *dst_str, const char *coord_reg, BOOL projected, BOOL bias) {
591 SHADER_BUFFER* buffer = arg->buffer;
592 DWORD sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
593 const char *tex_type;
595 switch(sampler_type) {
602 IWineD3DBaseShaderImpl *This = (IWineD3DBaseShaderImpl *) arg->shader;
603 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) This->baseShader.device;
604 if(device->stateBlock->textures[sampler_idx] &&
605 IWineD3DBaseTexture_GetTextureDimensions(device->stateBlock->textures[sampler_idx]) == GL_TEXTURE_RECTANGLE_ARB) {
613 case WINED3DSTT_VOLUME:
617 case WINED3DSTT_CUBE:
622 ERR("Unexpected texture type %d\n", sampler_type);
627 /* Shouldn't be possible, but let's check for it */
628 if(projected) FIXME("Biased and Projected texture sampling\n");
629 /* TXB takes the 4th component of the source vector automatically, as d3d. Nothing more to do */
630 shader_addline(buffer, "TXB %s, %s, texture[%u], %s;\n", dst_str, coord_reg, sampler_idx, tex_type);
631 } else if (projected) {
632 shader_addline(buffer, "TXP %s, %s, texture[%u], %s;\n", dst_str, coord_reg, sampler_idx, tex_type);
634 shader_addline(buffer, "TEX %s, %s, texture[%u], %s;\n", dst_str, coord_reg, sampler_idx, tex_type);
638 static void shader_arb_color_correction(SHADER_OPCODE_ARG* arg) {
639 IWineD3DBaseShaderImpl* shader = (IWineD3DBaseShaderImpl*) arg->shader;
640 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) shader->baseShader.device;
641 WineD3D_GL_Info *gl_info = &deviceImpl->adapter->gl_info;
643 WINED3DFORMAT conversion_group;
644 IWineD3DBaseTextureImpl *texture;
646 BOOL recorded = FALSE;
648 DWORD hex_version = shader->baseShader.hex_version;
652 switch(arg->opcode->opcode) {
654 if (hex_version < WINED3DPS_VERSION(2,0)) {
655 sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
657 sampler_idx = arg->src[1] & WINED3DSP_REGNUM_MASK;
661 case WINED3DSIO_TEXLDL:
662 FIXME("Add color fixup for vertex texture WINED3DSIO_TEXLDL\n");
665 case WINED3DSIO_TEXDP3TEX:
666 case WINED3DSIO_TEXM3x3TEX:
667 case WINED3DSIO_TEXM3x3SPEC:
668 case WINED3DSIO_TEXM3x3VSPEC:
669 case WINED3DSIO_TEXBEM:
670 case WINED3DSIO_TEXREG2AR:
671 case WINED3DSIO_TEXREG2GB:
672 case WINED3DSIO_TEXREG2RGB:
673 sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
677 /* Not a texture sampling instruction, nothing to do */
681 texture = (IWineD3DBaseTextureImpl *) deviceImpl->stateBlock->textures[sampler_idx];
683 fmt = texture->resource.format;
684 conversion_group = texture->baseTexture.shader_conversion_group;
686 fmt = WINED3DFMT_UNKNOWN;
687 conversion_group = WINED3DFMT_UNKNOWN;
690 /* before doing anything, record the sampler with the format in the format conversion list,
691 * but check if it's not there already
693 for(i = 0; i < shader->baseShader.num_sampled_samplers; i++) {
694 if(shader->baseShader.sampled_samplers[i] == sampler_idx) {
699 shader->baseShader.sampled_samplers[shader->baseShader.num_sampled_samplers] = sampler_idx;
700 shader->baseShader.num_sampled_samplers++;
701 shader->baseShader.sampled_format[sampler_idx] = conversion_group;
704 pshader_get_register_name(arg->shader, arg->dst, reg);
705 shader_arb_get_write_mask(arg, arg->dst, writemask);
706 if(strlen(writemask) == 0) strcpy(writemask, ".xyzw");
709 case WINED3DFMT_V8U8:
710 case WINED3DFMT_V16U16:
711 if(GL_SUPPORT(NV_TEXTURE_SHADER) ||
712 (GL_SUPPORT(ATI_ENVMAP_BUMPMAP) && fmt == WINED3DFMT_V8U8)) {
714 /* The 3rd channel returns 1.0 in d3d, but 0.0 in gl. Fix this while we're at it :-)
715 * disabled until an application that needs it is found because it causes unneeded
716 * shader recompilation in some game
718 if(strlen(writemask) >= 4) {
719 shader_addline(arg->buffer, "MOV %s.%c, one.z;\n", reg, writemask[3]);
723 /* Correct the sign, but leave the blue as it is - it was loaded correctly already
724 * ARB shaders are a bit picky wrt writemasks and swizzles. If we're free to scale
725 * all registers, do so, this saves an instruction.
727 if(strlen(writemask) >= 5) {
728 shader_addline(arg->buffer, "MAD %s, %s, coefmul.x, -one;\n", reg, reg);
729 } else if(strlen(writemask) >= 3) {
730 shader_addline(arg->buffer, "MAD %s.%c, %s.%c, coefmul.x, -one;\n",
733 shader_addline(arg->buffer, "MAD %s.%c, %s.%c, coefmul.x, -one;\n",
736 } else if(strlen(writemask) == 2) {
737 shader_addline(arg->buffer, "MAD %s.%c, %s.%c, coefmul.x, -one;\n", reg, writemask[1],
743 case WINED3DFMT_X8L8V8U8:
744 if(!GL_SUPPORT(NV_TEXTURE_SHADER)) {
745 /* Red and blue are the signed channels, fix them up; Blue(=L) is correct already,
746 * and a(X) is always 1.0. Cannot do a full conversion due to L(blue)
748 if(strlen(writemask) >= 3) {
749 shader_addline(arg->buffer, "MAD %s.%c, %s.%c, coefmul.x, -one;\n",
752 shader_addline(arg->buffer, "MAD %s.%c, %s.%c, coefmul.x, -one;\n",
755 } else if(strlen(writemask) == 2) {
756 shader_addline(arg->buffer, "MAD %s.%c, %s.%c, coefmul.x, -one;\n",
763 case WINED3DFMT_L6V5U5:
764 if(!GL_SUPPORT(NV_TEXTURE_SHADER)) {
765 if(strlen(writemask) >= 4) {
766 /* Swap y and z (U and L), and do a sign conversion on x and the new y(V and U) */
767 shader_addline(arg->buffer, "MOV TMP.g, %s.%c;\n",
769 shader_addline(arg->buffer, "MAD %s.%c%c, %s.%c%c, coefmul.x, -one;\n",
770 reg, writemask[1], writemask[1],
771 reg, writemask[1], writemask[3]);
772 shader_addline(arg->buffer, "MOV %s.%c, TMP.g;\n", reg,
774 } else if(strlen(writemask) == 3) {
775 /* This is bad: We have VL, but we need VU */
776 FIXME("2 components sampled from a converted L6V5U5 texture\n");
778 shader_addline(arg->buffer, "MAD %s.%c, %s.%c, coefmul.x, -one;\n",
785 case WINED3DFMT_Q8W8V8U8:
786 if(!GL_SUPPORT(NV_TEXTURE_SHADER)) {
787 /* Correct the sign in all channels */
788 switch(strlen(writemask)) {
790 shader_addline(arg->buffer, "MAD %s.%c, %s.%c, coefmul.x, -one;\n",
795 shader_addline(arg->buffer, "MAD %s.%c, %s.%c, coefmul.x, -one;\n",
800 shader_addline(arg->buffer, "MAD %s.%c, %s.%c, coefmul.x, -one;\n",
805 /* Should not occur, since it's at minimum '.' and a letter */
807 ERR("Unexpected writemask: \"%s\"\n", writemask);
812 shader_addline(arg->buffer, "MAD %s, %s, coefmul.x, -one;\n", reg, reg);
817 /* stupid compiler */
824 static void pshader_gen_input_modifier_line (
825 IWineD3DBaseShader *iface,
826 SHADER_BUFFER* buffer,
831 /* Generate a line that does the input modifier computation and return the input register to use */
836 /* Assume a new line will be added */
839 /* Get register name */
840 pshader_get_register_name(iface, instr, regstr);
841 shader_arb_get_swizzle(instr, FALSE, swzstr);
843 switch (instr & WINED3DSP_SRCMOD_MASK) {
844 case WINED3DSPSM_NONE:
845 sprintf(outregstr, "%s%s", regstr, swzstr);
848 case WINED3DSPSM_NEG:
849 sprintf(outregstr, "-%s%s", regstr, swzstr);
852 case WINED3DSPSM_BIAS:
853 shader_addline(buffer, "ADD T%c, %s, -coefdiv.x;\n", 'A' + tmpreg, regstr);
855 case WINED3DSPSM_BIASNEG:
856 shader_addline(buffer, "ADD T%c, -%s, coefdiv.x;\n", 'A' + tmpreg, regstr);
858 case WINED3DSPSM_SIGN:
859 shader_addline(buffer, "MAD T%c, %s, coefmul.x, -one.x;\n", 'A' + tmpreg, regstr);
861 case WINED3DSPSM_SIGNNEG:
862 shader_addline(buffer, "MAD T%c, %s, -coefmul.x, one.x;\n", 'A' + tmpreg, regstr);
864 case WINED3DSPSM_COMP:
865 shader_addline(buffer, "SUB T%c, one.x, %s;\n", 'A' + tmpreg, regstr);
868 shader_addline(buffer, "ADD T%c, %s, %s;\n", 'A' + tmpreg, regstr, regstr);
870 case WINED3DSPSM_X2NEG:
871 shader_addline(buffer, "ADD T%c, -%s, -%s;\n", 'A' + tmpreg, regstr, regstr);
874 shader_addline(buffer, "RCP T%c, %s.z;\n", 'A' + tmpreg, regstr);
875 shader_addline(buffer, "MUL T%c, %s, T%c;\n", 'A' + tmpreg, regstr, 'A' + tmpreg);
878 shader_addline(buffer, "RCP T%c, %s.w;\n", 'A' + tmpreg, regstr);
879 shader_addline(buffer, "MUL T%c, %s, T%c;\n", 'A' + tmpreg, regstr, 'A' + tmpreg);
882 sprintf(outregstr, "%s%s", regstr, swzstr);
886 /* Return modified or original register, with swizzle */
888 sprintf(outregstr, "T%c%s", 'A' + tmpreg, swzstr);
891 static inline void pshader_gen_output_modifier_line(
892 SHADER_BUFFER* buffer,
898 /* Generate a line that does the output modifier computation */
899 shader_addline(buffer, "MUL%s %s%s, %s, %s;\n", saturate ? "_SAT" : "",
900 regstr, write_mask, regstr, shift_tab[shift]);
903 void pshader_hw_bem(SHADER_OPCODE_ARG* arg) {
904 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
906 SHADER_BUFFER* buffer = arg->buffer;
908 char src_name[2][50];
910 DWORD sampler_code = arg->dst & WINED3DSP_REGNUM_MASK;
911 BOOL has_bumpmat = FALSE;
914 for(i = 0; i < This->numbumpenvmatconsts; i++) {
915 if(This->bumpenvmatconst[i].const_num != -1 && This->bumpenvmatconst[i].texunit == sampler_code) {
921 pshader_get_register_name(arg->shader, arg->dst, dst_name);
922 shader_arb_get_write_mask(arg, arg->dst, dst_wmask);
923 strcat(dst_name, dst_wmask);
925 pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[0], 0, src_name[0]);
926 pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[1], 1, src_name[1]);
929 /* Sampling the perturbation map in Tsrc was done already, including the signedness correction if needed */
930 shader_addline(buffer, "SWZ TMP2, bumpenvmat%d, x, z, 0, 0;\n", sampler_code);
931 shader_addline(buffer, "DP3 TMP.r, TMP2, %s;\n", src_name[1]);
932 shader_addline(buffer, "SWZ TMP2, bumpenvmat%d, y, w, 0, 0;\n", sampler_code);
933 shader_addline(buffer, "DP3 TMP.g, TMP2, %s;\n", src_name[1]);
935 shader_addline(buffer, "ADD %s, %s, TMP;\n", dst_name, src_name[0]);
937 shader_addline(buffer, "MOV %s, %s;\n", dst_name, src_name[0]);
941 void pshader_hw_cnd(SHADER_OPCODE_ARG* arg) {
943 IWineD3DBaseShaderImpl* shader = (IWineD3DBaseShaderImpl*) arg->shader;
944 SHADER_BUFFER* buffer = arg->buffer;
947 char src_name[3][50];
948 BOOL sat = (arg->dst & WINED3DSP_DSTMOD_MASK) & WINED3DSPDM_SATURATE;
949 DWORD shift = (arg->dst & WINED3DSP_DSTSHIFT_MASK) >> WINED3DSP_DSTSHIFT_SHIFT;
951 /* FIXME: support output modifiers */
953 /* Handle output register */
954 pshader_get_register_name(arg->shader, arg->dst, dst_name);
955 shader_arb_get_write_mask(arg, arg->dst, dst_wmask);
957 /* Generate input register names (with modifiers) */
958 pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[0], 0, src_name[0]);
959 pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[1], 1, src_name[1]);
960 pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[2], 2, src_name[2]);
962 /* The coissue flag changes the semantic of the cnd instruction in <= 1.3 shaders */
963 if (shader->baseShader.hex_version <= WINED3DPS_VERSION(1, 3) &&
964 arg->opcode_token & WINED3DSI_COISSUE) {
965 shader_addline(buffer, "MOV%s %s%s, %s;\n", sat ? "_SAT" : "", dst_name, dst_wmask, src_name[1]);
967 shader_addline(buffer, "ADD TMP, -%s, coefdiv.x;\n", src_name[0]);
968 shader_addline(buffer, "CMP%s %s%s, TMP, %s, %s;\n",
969 sat ? "_SAT" : "", dst_name, dst_wmask, src_name[1], src_name[2]);
972 pshader_gen_output_modifier_line(buffer, FALSE, dst_wmask, shift, dst_name);
975 void pshader_hw_cmp(SHADER_OPCODE_ARG* arg) {
977 SHADER_BUFFER* buffer = arg->buffer;
980 char src_name[3][50];
981 DWORD shift = (arg->dst & WINED3DSP_DSTSHIFT_MASK) >> WINED3DSP_DSTSHIFT_SHIFT;
982 BOOL sat = (arg->dst & WINED3DSP_DSTMOD_MASK) & WINED3DSPDM_SATURATE;
984 /* FIXME: support output modifiers */
986 /* Handle output register */
987 pshader_get_register_name(arg->shader, arg->dst, dst_name);
988 shader_arb_get_write_mask(arg, arg->dst, dst_wmask);
990 /* Generate input register names (with modifiers) */
991 pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[0], 0, src_name[0]);
992 pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[1], 1, src_name[1]);
993 pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[2], 2, src_name[2]);
995 shader_addline(buffer, "CMP%s %s%s, %s, %s, %s;\n", sat ? "_SAT" : "", dst_name, dst_wmask,
996 src_name[0], src_name[2], src_name[1]);
999 pshader_gen_output_modifier_line(buffer, FALSE, dst_wmask, shift, dst_name);
1002 /** Process the WINED3DSIO_DP2ADD instruction in ARB.
1003 * dst = dot2(src0, src1) + src2 */
1004 void pshader_hw_dp2add(SHADER_OPCODE_ARG* arg) {
1005 SHADER_BUFFER* buffer = arg->buffer;
1008 char src_name[3][50];
1009 DWORD shift = (arg->dst & WINED3DSP_DSTSHIFT_MASK) >> WINED3DSP_DSTSHIFT_SHIFT;
1010 BOOL sat = (arg->dst & WINED3DSP_DSTMOD_MASK) & WINED3DSPDM_SATURATE;
1012 pshader_get_register_name(arg->shader, arg->dst, dst_name);
1013 shader_arb_get_write_mask(arg, arg->dst, dst_wmask);
1015 pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[0], 0, src_name[0]);
1016 pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[1], 1, src_name[1]);
1017 pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[2], 2, src_name[2]);
1019 /* Emulate a DP2 with a DP3 and 0.0 */
1020 shader_addline(buffer, "MOV TMP, %s;\n", src_name[0]);
1021 shader_addline(buffer, "MOV TMP.z, 0.0;\n");
1022 shader_addline(buffer, "DP3 TMP2, TMP, %s;\n", src_name[1]);
1023 shader_addline(buffer, "ADD%s %s%s, TMP2, %s;\n", sat ? "_SAT" : "", dst_name, dst_wmask, src_name[2]);
1026 pshader_gen_output_modifier_line(buffer, FALSE, dst_wmask, shift, dst_name);
1029 /* Map the opcode 1-to-1 to the GL code */
1030 void pshader_hw_map2gl(SHADER_OPCODE_ARG* arg) {
1032 CONST SHADER_OPCODE* curOpcode = arg->opcode;
1033 SHADER_BUFFER* buffer = arg->buffer;
1034 DWORD dst = arg->dst;
1035 DWORD* src = arg->src;
1040 /* Output token related */
1041 char output_rname[256];
1042 char output_wmask[20];
1043 BOOL saturate = FALSE;
1044 BOOL centroid = FALSE;
1045 BOOL partialprecision = FALSE;
1048 strcpy(tmpLine, curOpcode->glname);
1050 /* Process modifiers */
1051 if (0 != (dst & WINED3DSP_DSTMOD_MASK)) {
1052 DWORD mask = dst & WINED3DSP_DSTMOD_MASK;
1054 saturate = mask & WINED3DSPDM_SATURATE;
1055 centroid = mask & WINED3DSPDM_MSAMPCENTROID;
1056 partialprecision = mask & WINED3DSPDM_PARTIALPRECISION;
1057 mask &= ~(WINED3DSPDM_MSAMPCENTROID | WINED3DSPDM_PARTIALPRECISION | WINED3DSPDM_SATURATE);
1059 FIXME("Unrecognized modifier(%#x)\n", mask >> WINED3DSP_DSTMOD_SHIFT);
1062 FIXME("Unhandled modifier(%#x)\n", mask >> WINED3DSP_DSTMOD_SHIFT);
1064 shift = (dst & WINED3DSP_DSTSHIFT_MASK) >> WINED3DSP_DSTSHIFT_SHIFT;
1066 /* Generate input and output registers */
1067 if (curOpcode->num_params > 0) {
1068 char operands[4][100];
1070 /* Generate input register names (with modifiers) */
1071 for (i = 1; i < curOpcode->num_params; ++i)
1072 pshader_gen_input_modifier_line(arg->shader, buffer, src[i-1], i-1, operands[i]);
1074 /* Handle output register */
1075 pshader_get_register_name(arg->shader, dst, output_rname);
1076 strcpy(operands[0], output_rname);
1077 shader_arb_get_write_mask(arg, dst, output_wmask);
1078 strcat(operands[0], output_wmask);
1080 if (saturate && (shift == 0))
1081 strcat(tmpLine, "_SAT");
1082 strcat(tmpLine, " ");
1083 strcat(tmpLine, operands[0]);
1084 for (i = 1; i < curOpcode->num_params; i++) {
1085 strcat(tmpLine, ", ");
1086 strcat(tmpLine, operands[i]);
1088 strcat(tmpLine,";\n");
1089 shader_addline(buffer, tmpLine);
1091 /* A shift requires another line. */
1093 pshader_gen_output_modifier_line(buffer, saturate, output_wmask, shift, output_rname);
1097 void pshader_hw_texkill(SHADER_OPCODE_ARG* arg) {
1098 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1099 DWORD hex_version = This->baseShader.hex_version;
1100 SHADER_BUFFER* buffer = arg->buffer;
1103 /* No swizzles are allowed in d3d's texkill. PS 1.x ignores the 4th component as documented,
1104 * but >= 2.0 honors it(undocumented, but tested by the d3d9 testsuit)
1106 pshader_get_register_name(arg->shader, arg->dst, reg_dest);
1108 if(hex_version >= WINED3DPS_VERSION(2,0)) {
1109 /* The arb backend doesn't claim ps 2.0 support, but try to eat what the app feeds to us */
1110 shader_addline(buffer, "KIL %s;\n", reg_dest);
1112 /* ARB fp doesn't like swizzles on the parameter of the KIL instruction. To mask the 4th component,
1113 * copy the register into our general purpose TMP variable, overwrite .w and pass TMP to KIL
1115 shader_addline(buffer, "MOV TMP, %s;\n", reg_dest);
1116 shader_addline(buffer, "MOV TMP.w, one.w;\n");
1117 shader_addline(buffer, "KIL TMP;\n");
1121 void pshader_hw_tex(SHADER_OPCODE_ARG* arg) {
1122 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1123 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
1125 DWORD dst = arg->dst;
1126 DWORD* src = arg->src;
1127 SHADER_BUFFER* buffer = arg->buffer;
1128 DWORD hex_version = This->baseShader.hex_version;
1129 BOOL projected = FALSE, bias = FALSE;
1133 DWORD reg_dest_code;
1134 DWORD reg_sampler_code;
1136 /* All versions have a destination register */
1137 reg_dest_code = dst & WINED3DSP_REGNUM_MASK;
1138 pshader_get_register_name(arg->shader, dst, reg_dest);
1140 /* 1.0-1.3: Use destination register as coordinate source.
1141 1.4+: Use provided coordinate source register. */
1142 if (hex_version < WINED3DPS_VERSION(1,4))
1143 strcpy(reg_coord, reg_dest);
1145 pshader_gen_input_modifier_line(arg->shader, buffer, src[0], 0, reg_coord);
1147 /* 1.0-1.4: Use destination register number as texture code.
1148 2.0+: Use provided sampler number as texure code. */
1149 if (hex_version < WINED3DPS_VERSION(2,0))
1150 reg_sampler_code = reg_dest_code;
1152 reg_sampler_code = src[1] & WINED3DSP_REGNUM_MASK;
1155 * 1.1, 1.2, 1.3: Use WINED3DTSS_TEXTURETRANSFORMFLAGS
1156 * 1.4: Use WINED3DSPSM_DZ or WINED3DSPSM_DW on src[0]
1157 * 2.0+: Use WINED3DSI_TEXLD_PROJECT on the opcode
1159 if(hex_version < WINED3DPS_VERSION(1,4)) {
1161 if(reg_sampler_code < MAX_TEXTURES) {
1162 flags = deviceImpl->stateBlock->textureState[reg_sampler_code][WINED3DTSS_TEXTURETRANSFORMFLAGS];
1164 if (flags & WINED3DTTFF_PROJECTED) {
1167 } else if(hex_version < WINED3DPS_VERSION(2,0)) {
1168 DWORD src_mod = arg->src[0] & WINED3DSP_SRCMOD_MASK;
1169 if (src_mod == WINED3DSPSM_DZ) {
1171 } else if(src_mod == WINED3DSPSM_DW) {
1175 if(arg->opcode_token & WINED3DSI_TEXLD_PROJECT) {
1178 if(arg->opcode_token & WINED3DSI_TEXLD_BIAS) {
1182 shader_hw_sample(arg, reg_sampler_code, reg_dest, reg_coord, projected, bias);
1185 void pshader_hw_texcoord(SHADER_OPCODE_ARG* arg) {
1187 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1188 DWORD dst = arg->dst;
1189 SHADER_BUFFER* buffer = arg->buffer;
1190 DWORD hex_version = This->baseShader.hex_version;
1193 shader_arb_get_write_mask(arg, dst, tmp);
1194 if (hex_version != WINED3DPS_VERSION(1,4)) {
1195 DWORD reg = dst & WINED3DSP_REGNUM_MASK;
1196 shader_addline(buffer, "MOV_SAT T%u%s, fragment.texcoord[%u];\n", reg, tmp, reg);
1198 DWORD reg1 = dst & WINED3DSP_REGNUM_MASK;
1201 pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[0], 0, reg_src);
1202 shader_addline(buffer, "MOV R%u%s, %s;\n", reg1, tmp, reg_src);
1206 void pshader_hw_texreg2ar(SHADER_OPCODE_ARG* arg) {
1208 SHADER_BUFFER* buffer = arg->buffer;
1209 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1210 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
1213 DWORD reg1 = arg->dst & WINED3DSP_REGNUM_MASK;
1217 sprintf(dst_str, "T%u", reg1);
1218 pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[0], 0, src_str);
1219 shader_addline(buffer, "MOV TMP.r, %s.a;\n", src_str);
1220 shader_addline(buffer, "MOV TMP.g, %s.r;\n", src_str);
1221 flags = reg1 < MAX_TEXTURES ? deviceImpl->stateBlock->textureState[reg1][WINED3DTSS_TEXTURETRANSFORMFLAGS] : 0;
1222 shader_hw_sample(arg, reg1, dst_str, "TMP", flags & WINED3DTTFF_PROJECTED, FALSE);
1225 void pshader_hw_texreg2gb(SHADER_OPCODE_ARG* arg) {
1227 SHADER_BUFFER* buffer = arg->buffer;
1228 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1229 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
1232 DWORD reg1 = arg->dst & WINED3DSP_REGNUM_MASK;
1236 sprintf(dst_str, "T%u", reg1);
1237 pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[0], 0, src_str);
1238 shader_addline(buffer, "MOV TMP.r, %s.g;\n", src_str);
1239 shader_addline(buffer, "MOV TMP.g, %s.b;\n", src_str);
1240 flags = reg1 < MAX_TEXTURES ? deviceImpl->stateBlock->textureState[reg1][WINED3DTSS_TEXTURETRANSFORMFLAGS] : 0;
1241 shader_hw_sample(arg, reg1, dst_str, "TMP", FALSE, FALSE);
1244 void pshader_hw_texreg2rgb(SHADER_OPCODE_ARG* arg) {
1246 SHADER_BUFFER* buffer = arg->buffer;
1247 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1248 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
1250 DWORD reg1 = arg->dst & WINED3DSP_REGNUM_MASK;
1254 sprintf(dst_str, "T%u", reg1);
1255 pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[0], 0, src_str);
1256 flags = reg1 < MAX_TEXTURES ? deviceImpl->stateBlock->textureState[reg1][WINED3DTSS_TEXTURETRANSFORMFLAGS] : 0;
1257 shader_hw_sample(arg, reg1, dst_str, src_str, FALSE, FALSE);
1260 void pshader_hw_texbem(SHADER_OPCODE_ARG* arg) {
1261 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1262 BOOL has_bumpmat = FALSE;
1263 BOOL has_luminance = FALSE;
1266 DWORD dst = arg->dst;
1267 DWORD src = arg->src[0] & WINED3DSP_REGNUM_MASK;
1268 SHADER_BUFFER* buffer = arg->buffer;
1271 DWORD reg_dest_code;
1273 /* All versions have a destination register */
1274 reg_dest_code = dst & WINED3DSP_REGNUM_MASK;
1275 /* Can directly use the name because texbem is only valid for <= 1.3 shaders */
1276 pshader_get_register_name(arg->shader, dst, reg_coord);
1278 for(i = 0; i < This->numbumpenvmatconsts; i++) {
1279 if(This->bumpenvmatconst[i].const_num != -1 && reg_dest_code == This->bumpenvmatconst[i].texunit) {
1284 for(i = 0; i < This->numbumpenvmatconsts; i++) {
1285 if(This->luminanceconst[i].const_num != -1 && reg_dest_code == This->luminanceconst[i].texunit) {
1286 has_luminance = TRUE;
1292 /* Sampling the perturbation map in Tsrc was done already, including the signedness correction if needed */
1294 shader_addline(buffer, "SWZ TMP2, bumpenvmat%d, x, z, 0, 0;\n", reg_dest_code);
1295 shader_addline(buffer, "DP3 TMP.r, TMP2, T%u;\n", src);
1296 shader_addline(buffer, "SWZ TMP2, bumpenvmat%d, y, w, 0, 0;\n", reg_dest_code);
1297 shader_addline(buffer, "DP3 TMP.g, TMP2, T%u;\n", src);
1299 /* with projective textures, texbem only divides the static texture coord, not the displacement,
1300 * so we can't let the GL handle this.
1302 if (((IWineD3DDeviceImpl*) This->baseShader.device)->stateBlock->textureState[reg_dest_code][WINED3DTSS_TEXTURETRANSFORMFLAGS]
1303 & WINED3DTTFF_PROJECTED) {
1304 shader_addline(buffer, "RCP TMP2.a, %s.a;\n", reg_coord);
1305 shader_addline(buffer, "MUL TMP2.rg, %s, TMP2.a;\n", reg_coord);
1306 shader_addline(buffer, "ADD TMP.rg, TMP, TMP2;\n");
1308 shader_addline(buffer, "ADD TMP.rg, TMP, %s;\n", reg_coord);
1311 shader_hw_sample(arg, reg_dest_code, reg_coord, "TMP", FALSE, FALSE);
1313 if(arg->opcode->opcode == WINED3DSIO_TEXBEML && has_luminance) {
1314 shader_addline(buffer, "MAD TMP, T%u.z, luminance%d.x, luminance%d.y;\n",
1315 src, reg_dest_code, reg_dest_code);
1316 shader_addline(buffer, "MUL %s, %s, TMP;\n", reg_coord, reg_coord);
1321 if(reg_dest_code < MAX_TEXTURES) {
1322 tf = ((IWineD3DDeviceImpl*) This->baseShader.device)->stateBlock->textureState[reg_dest_code][WINED3DTSS_TEXTURETRANSFORMFLAGS];
1326 /* Without a bump matrix loaded, just sample with the unmodified coordinates */
1327 shader_hw_sample(arg, reg_dest_code, reg_coord, reg_coord, tf & WINED3DTTFF_PROJECTED, FALSE);
1331 void pshader_hw_texm3x2pad(SHADER_OPCODE_ARG* arg) {
1333 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1334 SHADER_BUFFER* buffer = arg->buffer;
1337 pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[0], 0, src0_name);
1338 shader_addline(buffer, "DP3 TMP.x, T%u, %s;\n", reg, src0_name);
1341 void pshader_hw_texm3x2tex(SHADER_OPCODE_ARG* arg) {
1343 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1344 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
1346 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1347 SHADER_BUFFER* buffer = arg->buffer;
1351 sprintf(dst_str, "T%u", reg);
1352 pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[0], 0, src0_name);
1353 shader_addline(buffer, "DP3 TMP.y, T%u, %s;\n", reg, src0_name);
1354 flags = reg < MAX_TEXTURES ? deviceImpl->stateBlock->textureState[reg][WINED3DTSS_TEXTURETRANSFORMFLAGS] : 0;
1355 shader_hw_sample(arg, reg, dst_str, "TMP", flags & WINED3DTTFF_PROJECTED, FALSE);
1358 void pshader_hw_texm3x3pad(SHADER_OPCODE_ARG* arg) {
1360 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1361 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1362 SHADER_BUFFER* buffer = arg->buffer;
1363 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
1366 pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[0], 0, src0_name);
1367 shader_addline(buffer, "DP3 TMP.%c, T%u, %s;\n", 'x' + current_state->current_row, reg, src0_name);
1368 current_state->texcoord_w[current_state->current_row++] = reg;
1371 void pshader_hw_texm3x3tex(SHADER_OPCODE_ARG* arg) {
1373 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1374 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
1376 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1377 SHADER_BUFFER* buffer = arg->buffer;
1378 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
1382 pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[0], 0, src0_name);
1383 shader_addline(buffer, "DP3 TMP.z, T%u, %s;\n", reg, src0_name);
1385 /* Sample the texture using the calculated coordinates */
1386 sprintf(dst_str, "T%u", reg);
1387 flags = reg < MAX_TEXTURES ? deviceImpl->stateBlock->textureState[reg][WINED3DTSS_TEXTURETRANSFORMFLAGS] : 0;
1388 shader_hw_sample(arg, reg, dst_str, "TMP", flags & WINED3DTTFF_PROJECTED, FALSE);
1389 current_state->current_row = 0;
1392 void pshader_hw_texm3x3vspec(SHADER_OPCODE_ARG* arg) {
1394 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1395 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
1397 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1398 SHADER_BUFFER* buffer = arg->buffer;
1399 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
1403 pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[0], 0, src0_name);
1404 shader_addline(buffer, "DP3 TMP.z, T%u, %s;\n", reg, src0_name);
1406 /* Construct the eye-ray vector from w coordinates */
1407 shader_addline(buffer, "MOV TMP2.x, fragment.texcoord[%u].w;\n", current_state->texcoord_w[0]);
1408 shader_addline(buffer, "MOV TMP2.y, fragment.texcoord[%u].w;\n", current_state->texcoord_w[1]);
1409 shader_addline(buffer, "MOV TMP2.z, fragment.texcoord[%u].w;\n", reg);
1411 /* Calculate reflection vector
1413 shader_addline(buffer, "DP3 TMP.w, TMP, TMP2;\n");
1414 /* The .w is ignored when sampling, so I can use TMP2.w to calculate dot(N, N) */
1415 shader_addline(buffer, "DP3 TMP2.w, TMP, TMP;\n");
1416 shader_addline(buffer, "RCP TMP2.w, TMP2.w;\n");
1417 shader_addline(buffer, "MUL TMP.w, TMP.w, TMP2.w;\n");
1418 shader_addline(buffer, "MUL TMP, TMP.w, TMP;\n");
1419 shader_addline(buffer, "MAD TMP, coefmul.x, TMP, -TMP2;\n");
1421 /* Sample the texture using the calculated coordinates */
1422 sprintf(dst_str, "T%u", reg);
1423 flags = reg < MAX_TEXTURES ? deviceImpl->stateBlock->textureState[reg][WINED3DTSS_TEXTURETRANSFORMFLAGS] : 0;
1424 shader_hw_sample(arg, reg, dst_str, "TMP", flags & WINED3DTTFF_PROJECTED, FALSE);
1425 current_state->current_row = 0;
1428 void pshader_hw_texm3x3spec(SHADER_OPCODE_ARG* arg) {
1430 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1431 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
1433 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1434 DWORD reg3 = arg->src[1] & WINED3DSP_REGNUM_MASK;
1435 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
1436 SHADER_BUFFER* buffer = arg->buffer;
1440 pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[0], 0, src0_name);
1441 shader_addline(buffer, "DP3 TMP.z, T%u, %s;\n", reg, src0_name);
1443 /* Calculate reflection vector.
1446 * TMP.xyz = 2 * --------- * N - E
1449 * Which normalizes the normal vector
1451 shader_addline(buffer, "DP3 TMP.w, TMP, C[%u];\n", reg3);
1452 shader_addline(buffer, "DP3 TMP2.w, TMP, TMP;\n");
1453 shader_addline(buffer, "RCP TMP2.w, TMP2.w;\n");
1454 shader_addline(buffer, "MUL TMP.w, TMP.w, TMP2.w;\n");
1455 shader_addline(buffer, "MUL TMP, TMP.w, TMP;\n");
1456 shader_addline(buffer, "MAD TMP, coefmul.x, TMP, -C[%u];\n", reg3);
1458 /* Sample the texture using the calculated coordinates */
1459 sprintf(dst_str, "T%u", reg);
1460 flags = reg < MAX_TEXTURES ? deviceImpl->stateBlock->textureState[reg][WINED3DTSS_TEXTURETRANSFORMFLAGS] : 0;
1461 shader_hw_sample(arg, reg, dst_str, "TMP", flags & WINED3DTTFF_PROJECTED, FALSE);
1462 current_state->current_row = 0;
1465 void pshader_hw_texdepth(SHADER_OPCODE_ARG* arg) {
1466 SHADER_BUFFER* buffer = arg->buffer;
1469 /* texdepth has an implicit destination, the fragment depth value. It's only parameter,
1470 * which is essentially an input, is the destination register because it is the first
1471 * parameter. According to the msdn, this must be register r5, but let's keep it more flexible
1474 pshader_get_register_name(arg->shader, arg->dst, dst_name);
1476 /* According to the msdn, the source register(must be r5) is unusable after
1477 * the texdepth instruction, so we're free to modify it
1479 shader_addline(buffer, "MIN %s.g, %s.g, one.g;\n", dst_name, dst_name);
1481 /* How to deal with the special case dst_name.g == 0? if r != 0, then
1482 * the r * (1 / 0) will give infinity, which is clamped to 1.0, the correct
1483 * result. But if r = 0.0, then 0 * inf = 0, which is incorrect.
1485 shader_addline(buffer, "RCP %s.g, %s.g;\n", dst_name, dst_name);
1486 shader_addline(buffer, "MUL TMP.x, %s.r, %s.g;\n", dst_name, dst_name);
1487 shader_addline(buffer, "MIN TMP.x, TMP.x, one.r;\n");
1488 shader_addline(buffer, "MAX result.depth, TMP.x, 0.0;\n");
1491 /** Process the WINED3DSIO_TEXDP3TEX instruction in ARB:
1492 * Take a 3-component dot product of the TexCoord[dstreg] and src,
1493 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
1494 void pshader_hw_texdp3tex(SHADER_OPCODE_ARG* arg) {
1495 SHADER_BUFFER* buffer = arg->buffer;
1496 DWORD sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
1500 pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[0], 0, src0);
1501 shader_addline(buffer, "MOV TMP, 0.0;\n");
1502 shader_addline(buffer, "DP3 TMP.x, T%u, %s;\n", sampler_idx, src0);
1504 sprintf(dst_str, "T%u", sampler_idx);
1505 shader_hw_sample(arg, sampler_idx, dst_str, "TMP", FALSE /* Only one coord, can't be projected */, FALSE);
1508 /** Process the WINED3DSIO_TEXDP3 instruction in ARB:
1509 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
1510 void pshader_hw_texdp3(SHADER_OPCODE_ARG* arg) {
1514 DWORD dstreg = arg->dst & WINED3DSP_REGNUM_MASK;
1515 SHADER_BUFFER* buffer = arg->buffer;
1517 /* Handle output register */
1518 pshader_get_register_name(arg->shader, arg->dst, dst_str);
1519 shader_arb_get_write_mask(arg, arg->dst, dst_mask);
1521 pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[0], 0, src0);
1522 shader_addline(buffer, "DP3 %s%s, T%u, %s;\n", dst_str, dst_mask, dstreg, src0);
1524 /* TODO: Handle output modifiers */
1527 /** Process the WINED3DSIO_TEXM3X3 instruction in ARB
1528 * Perform the 3rd row of a 3x3 matrix multiply */
1529 void pshader_hw_texm3x3(SHADER_OPCODE_ARG* arg) {
1530 SHADER_BUFFER* buffer = arg->buffer;
1534 DWORD dst_reg = arg->dst & WINED3DSP_REGNUM_MASK;
1536 pshader_get_register_name(arg->shader, arg->dst, dst_str);
1537 shader_arb_get_write_mask(arg, arg->dst, dst_mask);
1539 pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[0], 0, src0);
1540 shader_addline(buffer, "DP3 TMP.z, T%u, %s;\n", dst_reg, src0);
1541 shader_addline(buffer, "MOV %s%s, TMP;\n", dst_str, dst_mask);
1543 /* TODO: Handle output modifiers */
1546 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in ARB:
1547 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
1548 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
1549 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
1551 void pshader_hw_texm3x2depth(SHADER_OPCODE_ARG* arg) {
1552 SHADER_BUFFER* buffer = arg->buffer;
1553 DWORD dst_reg = arg->dst & WINED3DSP_REGNUM_MASK;
1556 pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[0], 0, src0);
1557 shader_addline(buffer, "DP3 TMP.y, T%u, %s;\n", dst_reg, src0);
1559 /* How to deal with the special case dst_name.g == 0? if r != 0, then
1560 * the r * (1 / 0) will give infinity, which is clamped to 1.0, the correct
1561 * result. But if r = 0.0, then 0 * inf = 0, which is incorrect.
1563 shader_addline(buffer, "RCP TMP.y, TMP.y;\n");
1564 shader_addline(buffer, "MUL TMP.x, TMP.x, TMP.y;\n");
1565 shader_addline(buffer, "MIN TMP.x, TMP.x, one.r;\n");
1566 shader_addline(buffer, "MAX result.depth, TMP.x, 0.0;\n");
1569 /** Handles transforming all WINED3DSIO_M?x? opcodes for
1570 Vertex/Pixel shaders to ARB_vertex_program codes */
1571 void shader_hw_mnxn(SHADER_OPCODE_ARG* arg) {
1574 int nComponents = 0;
1575 SHADER_OPCODE_ARG tmpArg;
1577 memset(&tmpArg, 0, sizeof(SHADER_OPCODE_ARG));
1579 /* Set constants for the temporary argument */
1580 tmpArg.shader = arg->shader;
1581 tmpArg.buffer = arg->buffer;
1582 tmpArg.src[0] = arg->src[0];
1583 tmpArg.src_addr[0] = arg->src_addr[0];
1584 tmpArg.src_addr[1] = arg->src_addr[1];
1585 tmpArg.reg_maps = arg->reg_maps;
1587 switch(arg->opcode->opcode) {
1588 case WINED3DSIO_M4x4:
1590 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP4);
1592 case WINED3DSIO_M4x3:
1594 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP4);
1596 case WINED3DSIO_M3x4:
1598 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP3);
1600 case WINED3DSIO_M3x3:
1602 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP3);
1604 case WINED3DSIO_M3x2:
1606 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP3);
1612 for (i = 0; i < nComponents; i++) {
1613 tmpArg.dst = ((arg->dst) & ~WINED3DSP_WRITEMASK_ALL)|(WINED3DSP_WRITEMASK_0<<i);
1614 tmpArg.src[1] = arg->src[1]+i;
1615 vshader_hw_map2gl(&tmpArg);
1619 void vshader_hw_rsq_rcp(SHADER_OPCODE_ARG* arg) {
1620 CONST SHADER_OPCODE* curOpcode = arg->opcode;
1621 SHADER_BUFFER* buffer = arg->buffer;
1622 DWORD dst = arg->dst;
1623 DWORD src = arg->src[0];
1624 DWORD swizzle = (src & WINED3DSP_SWIZZLE_MASK) >> WINED3DSP_SWIZZLE_SHIFT;
1628 strcpy(tmpLine, curOpcode->glname); /* Opcode */
1629 vshader_program_add_param(arg, dst, FALSE, tmpLine); /* Destination */
1630 strcat(tmpLine, ",");
1631 vshader_program_add_param(arg, src, TRUE, tmpLine);
1632 if ((WINED3DSP_NOSWIZZLE >> WINED3DSP_SWIZZLE_SHIFT) == swizzle) {
1633 /* Dx sdk says .x is used if no swizzle is given, but our test shows that
1636 strcat(tmpLine, ".w");
1639 shader_addline(buffer, "%s;\n", tmpLine);
1642 void shader_hw_nrm(SHADER_OPCODE_ARG* arg) {
1643 SHADER_BUFFER* buffer = arg->buffer;
1647 DWORD shift = (arg->dst & WINED3DSP_DSTSHIFT_MASK) >> WINED3DSP_DSTSHIFT_SHIFT;
1648 BOOL sat = (arg->dst & WINED3DSP_DSTMOD_MASK) & WINED3DSPDM_SATURATE;
1650 pshader_get_register_name(arg->shader, arg->dst, dst_name);
1651 shader_arb_get_write_mask(arg, arg->dst, dst_wmask);
1653 pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[0], 0, src_name);
1654 shader_addline(buffer, "DP3 TMP, %s, %s;\n", src_name, src_name);
1655 shader_addline(buffer, "RSQ TMP, TMP.x;\n");
1656 /* dst.w = src[0].w * 1 / (src.x^2 + src.y^2 + src.z^2)^(1/2) according to msdn*/
1657 shader_addline(buffer, "MUL%s %s%s, %s, TMP;\n", sat ? "_SAT" : "", dst_name, dst_wmask,
1661 pshader_gen_output_modifier_line(buffer, FALSE, dst_wmask, shift, dst_name);
1664 void shader_hw_sincos(SHADER_OPCODE_ARG* arg) {
1665 /* This instruction exists in ARB, but the d3d instruction takes two extra parameters which
1666 * must contain fixed constants. So we need a separate function to filter those constants and
1669 SHADER_BUFFER* buffer = arg->buffer;
1673 DWORD shift = (arg->dst & WINED3DSP_DSTSHIFT_MASK) >> WINED3DSP_DSTSHIFT_SHIFT;
1674 BOOL sat = (arg->dst & WINED3DSP_DSTMOD_MASK) & WINED3DSPDM_SATURATE;
1676 pshader_get_register_name(arg->shader, arg->dst, dst_name);
1677 shader_arb_get_write_mask(arg, arg->dst, dst_wmask);
1679 pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[0], 0, src_name);
1680 shader_addline(buffer, "SCS%s %s%s, %s;\n", sat ? "_SAT" : "", dst_name, dst_wmask,
1684 pshader_gen_output_modifier_line(buffer, FALSE, dst_wmask, shift, dst_name);
1688 /* TODO: merge with pixel shader */
1689 /* Map the opcode 1-to-1 to the GL code */
1690 void vshader_hw_map2gl(SHADER_OPCODE_ARG* arg) {
1692 IWineD3DVertexShaderImpl *shader = (IWineD3DVertexShaderImpl*) arg->shader;
1693 CONST SHADER_OPCODE* curOpcode = arg->opcode;
1694 SHADER_BUFFER* buffer = arg->buffer;
1695 DWORD dst = arg->dst;
1696 DWORD* src = arg->src;
1698 DWORD dst_regtype = shader_get_regtype(dst);
1702 if ((curOpcode->opcode == WINED3DSIO_MOV && dst_regtype == WINED3DSPR_ADDR) || curOpcode->opcode == WINED3DSIO_MOVA) {
1703 if(shader->rel_offset) {
1704 memset(tmpLine, 0, sizeof(tmpLine));
1705 vshader_program_add_param(arg, src[0], TRUE, tmpLine);
1706 shader_addline(buffer, "ADD TMP.x, %s, helper_const.z;\n", tmpLine);
1707 shader_addline(buffer, "ARL A0.x, TMP.x;\n");
1710 strcpy(tmpLine, "ARL");
1713 strcpy(tmpLine, curOpcode->glname);
1715 if (curOpcode->num_params > 0) {
1716 vshader_program_add_param(arg, dst, FALSE, tmpLine);
1717 for (i = 1; i < curOpcode->num_params; ++i) {
1718 strcat(tmpLine, ",");
1719 vshader_program_add_param(arg, src[i-1], TRUE, tmpLine);
1722 shader_addline(buffer, "%s;\n", tmpLine);
1725 static GLuint create_arb_blt_vertex_program(WineD3D_GL_Info *gl_info) {
1726 GLuint program_id = 0;
1727 const char *blt_vprogram =
1729 "PARAM c[1] = { { 1, 0.5 } };\n"
1730 "MOV result.position, vertex.position;\n"
1731 "MOV result.color, c[0].x;\n"
1732 "MAD result.texcoord[0].y, -vertex.position, c[0], c[0];\n"
1733 "MAD result.texcoord[0].x, vertex.position, c[0].y, c[0].y;\n"
1736 GL_EXTCALL(glGenProgramsARB(1, &program_id));
1737 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB, program_id));
1738 GL_EXTCALL(glProgramStringARB(GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(blt_vprogram), blt_vprogram));
1740 if (glGetError() == GL_INVALID_OPERATION) {
1742 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &pos);
1743 FIXME("Vertex program error at position %d: %s\n", pos,
1744 debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB)));
1750 static GLuint create_arb_blt_fragment_program(WineD3D_GL_Info *gl_info) {
1751 GLuint program_id = 0;
1752 const char *blt_fprogram =
1755 "TEX R0.x, fragment.texcoord[0], texture[0], 2D;\n"
1756 "MOV result.depth.z, R0.x;\n"
1759 GL_EXTCALL(glGenProgramsARB(1, &program_id));
1760 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, program_id));
1761 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(blt_fprogram), blt_fprogram));
1763 if (glGetError() == GL_INVALID_OPERATION) {
1765 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &pos);
1766 FIXME("Fragment program error at position %d: %s\n", pos,
1767 debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB)));
1773 static void shader_arb_select(IWineD3DDevice *iface, BOOL usePS, BOOL useVS) {
1774 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
1775 WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
1778 TRACE("Using vertex shader\n");
1780 /* Bind the vertex program */
1781 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB,
1782 ((IWineD3DVertexShaderImpl *)This->stateBlock->vertexShader)->baseShader.prgId));
1783 checkGLcall("glBindProgramARB(GL_VERTEX_PROGRAM_ARB, vertexShader->prgId);");
1785 /* Enable OpenGL vertex programs */
1786 glEnable(GL_VERTEX_PROGRAM_ARB);
1787 checkGLcall("glEnable(GL_VERTEX_PROGRAM_ARB);");
1788 TRACE("(%p) : Bound vertex program %u and enabled GL_VERTEX_PROGRAM_ARB\n",
1789 This, ((IWineD3DVertexShaderImpl *)This->stateBlock->vertexShader)->baseShader.prgId);
1790 } else if(GL_SUPPORT(ARB_VERTEX_PROGRAM)) {
1791 glDisable(GL_VERTEX_PROGRAM_ARB);
1792 checkGLcall("glDisable(GL_VERTEX_PROGRAM_ARB)");
1796 TRACE("Using pixel shader\n");
1798 /* Bind the fragment program */
1799 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB,
1800 ((IWineD3DPixelShaderImpl *)This->stateBlock->pixelShader)->baseShader.prgId));
1801 checkGLcall("glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, pixelShader->prgId);");
1803 /* Enable OpenGL fragment programs */
1804 glEnable(GL_FRAGMENT_PROGRAM_ARB);
1805 checkGLcall("glEnable(GL_FRAGMENT_PROGRAM_ARB);");
1806 TRACE("(%p) : Bound fragment program %u and enabled GL_FRAGMENT_PROGRAM_ARB\n",
1807 This, ((IWineD3DPixelShaderImpl *)This->stateBlock->pixelShader)->baseShader.prgId);
1808 } else if(GL_SUPPORT(ARB_FRAGMENT_PROGRAM)) {
1809 glDisable(GL_FRAGMENT_PROGRAM_ARB);
1810 checkGLcall("glDisable(GL_FRAGMENT_PROGRAM_ARB)");
1814 static void shader_arb_select_depth_blt(IWineD3DDevice *iface) {
1815 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
1816 struct shader_arb_priv *priv = (struct shader_arb_priv *) This->shader_priv;
1817 WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
1819 if (!priv->depth_blt_vprogram_id) priv->depth_blt_vprogram_id = create_arb_blt_vertex_program(gl_info);
1820 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB, priv->depth_blt_vprogram_id));
1821 glEnable(GL_VERTEX_PROGRAM_ARB);
1823 if (!priv->depth_blt_fprogram_id) priv->depth_blt_fprogram_id = create_arb_blt_fragment_program(gl_info);
1824 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, priv->depth_blt_fprogram_id));
1825 glEnable(GL_FRAGMENT_PROGRAM_ARB);
1828 static void shader_arb_destroy_depth_blt(IWineD3DDevice *iface) {
1829 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
1830 struct shader_arb_priv *priv = (struct shader_arb_priv *) This->shader_priv;
1831 WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
1833 if(priv->depth_blt_vprogram_id) {
1834 GL_EXTCALL(glDeleteProgramsARB(1, &priv->depth_blt_vprogram_id));
1835 priv->depth_blt_vprogram_id = 0;
1837 if(priv->depth_blt_fprogram_id) {
1838 GL_EXTCALL(glDeleteProgramsARB(1, &priv->depth_blt_fprogram_id));
1839 priv->depth_blt_fprogram_id = 0;
1843 static void shader_arb_cleanup(IWineD3DDevice *iface) {
1844 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
1845 WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
1846 if (GL_SUPPORT(ARB_VERTEX_PROGRAM)) glDisable(GL_VERTEX_PROGRAM_ARB);
1847 if (GL_SUPPORT(ARB_FRAGMENT_PROGRAM)) glDisable(GL_FRAGMENT_PROGRAM_ARB);
1850 static void shader_arb_destroy(IWineD3DBaseShader *iface) {
1851 IWineD3DBaseShaderImpl *This = (IWineD3DBaseShaderImpl *) iface;
1852 WineD3D_GL_Info *gl_info = &((IWineD3DDeviceImpl *) This->baseShader.device)->adapter->gl_info;
1855 GL_EXTCALL(glDeleteProgramsARB(1, &This->baseShader.prgId));
1856 checkGLcall("GL_EXTCALL(glDeleteProgramsARB(1, &This->baseShader.prgId))");
1858 This->baseShader.prgId = 0;
1859 This->baseShader.is_compiled = FALSE;
1862 static HRESULT shader_arb_alloc(IWineD3DDevice *iface) {
1863 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
1864 This->shader_priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct shader_arb_priv));
1868 static void shader_arb_free(IWineD3DDevice *iface) {
1869 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
1870 HeapFree(GetProcessHeap(), 0, This->shader_priv);
1873 static BOOL shader_arb_dirty_const(IWineD3DDevice *iface) {
1877 static void shader_arb_generate_pshader(IWineD3DPixelShader *iface, SHADER_BUFFER *buffer) {
1878 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)iface;
1879 shader_reg_maps* reg_maps = &This->baseShader.reg_maps;
1880 CONST DWORD *function = This->baseShader.function;
1881 const char *fragcolor;
1882 WineD3D_GL_Info *gl_info = &((IWineD3DDeviceImpl *)This->baseShader.device)->adapter->gl_info;
1884 /* Create the hw ARB shader */
1885 shader_addline(buffer, "!!ARBfp1.0\n");
1887 shader_addline(buffer, "TEMP TMP;\n"); /* Used in matrix ops */
1888 shader_addline(buffer, "TEMP TMP2;\n"); /* Used in matrix ops */
1889 shader_addline(buffer, "TEMP TA;\n"); /* Used for modifiers */
1890 shader_addline(buffer, "TEMP TB;\n"); /* Used for modifiers */
1891 shader_addline(buffer, "TEMP TC;\n"); /* Used for modifiers */
1892 shader_addline(buffer, "PARAM coefdiv = { 0.5, 0.25, 0.125, 0.0625 };\n");
1893 shader_addline(buffer, "PARAM coefmul = { 2, 4, 8, 16 };\n");
1894 shader_addline(buffer, "PARAM one = { 1.0, 1.0, 1.0, 1.0 };\n");
1896 /* Base Declarations */
1897 shader_generate_arb_declarations( (IWineD3DBaseShader*) This, reg_maps, buffer, &GLINFO_LOCATION);
1899 /* We need two variables for fog blending */
1900 shader_addline(buffer, "TEMP TMP_FOG;\n");
1901 if (This->baseShader.hex_version >= WINED3DPS_VERSION(2,0)) {
1902 shader_addline(buffer, "TEMP TMP_COLOR;\n");
1905 /* Base Shader Body */
1906 shader_generate_main( (IWineD3DBaseShader*) This, buffer, reg_maps, function);
1908 /* calculate fog and blend it
1909 * NOTE: state.fog.params.y and state.fog.params.z don't hold fog start s and end e but
1910 * -1/(e-s) and e/(e-s) respectively.
1912 shader_addline(buffer, "MAD_SAT TMP_FOG, fragment.fogcoord, state.fog.params.y, state.fog.params.z;\n");
1914 if (This->baseShader.hex_version < WINED3DPS_VERSION(2,0)) {
1917 fragcolor = "TMP_COLOR";
1919 if(This->srgb_enabled) {
1920 /* Perform sRGB write correction. See GLX_EXT_framebuffer_sRGB */
1922 /* Calculate the > 0.0031308 case */
1923 shader_addline(buffer, "POW TMP.x, %s.x, srgb_pow.x;\n", fragcolor);
1924 shader_addline(buffer, "POW TMP.y, %s.y, srgb_pow.y;\n", fragcolor);
1925 shader_addline(buffer, "POW TMP.z, %s.z, srgb_pow.z;\n", fragcolor);
1926 shader_addline(buffer, "MUL TMP, TMP, srgb_mul_hi;\n");
1927 shader_addline(buffer, "SUB TMP, TMP, srgb_sub_hi;\n");
1928 /* Calculate the < case */
1929 shader_addline(buffer, "MUL TMP2, srgb_mul_low, %s;\n", fragcolor);
1930 /* Get 1.0 / 0.0 masks for > 0.0031308 and < 0.0031308 */
1931 shader_addline(buffer, "SLT TA, srgb_comparison, %s;\n", fragcolor);
1932 shader_addline(buffer, "SGE TB, srgb_comparison, %s;\n", fragcolor);
1933 /* Store the components > 0.0031308 in the destination */
1934 shader_addline(buffer, "MUL %s, TMP, TA;\n", fragcolor);
1935 /* Add the components that are < 0.0031308 */
1936 shader_addline(buffer, "MAD result.color.xyz, TMP2, TB, %s;\n", fragcolor);
1937 /* [0.0;1.0] clamping. Not needed, this is done implicitly */
1939 if (This->baseShader.hex_version < WINED3DPS_VERSION(3,0)) {
1940 shader_addline(buffer, "LRP result.color.rgb, TMP_FOG.x, %s, state.fog.color;\n", fragcolor);
1941 shader_addline(buffer, "MOV result.color.a, %s.a;\n", fragcolor);
1944 shader_addline(buffer, "END\n");
1946 /* TODO: change to resource.glObjectHandle or something like that */
1947 GL_EXTCALL(glGenProgramsARB(1, &This->baseShader.prgId));
1949 TRACE("Creating a hw pixel shader, prg=%d\n", This->baseShader.prgId);
1950 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, This->baseShader.prgId));
1952 TRACE("Created hw pixel shader, prg=%d\n", This->baseShader.prgId);
1953 /* Create the program and check for errors */
1954 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
1955 buffer->bsize, buffer->buffer));
1957 if (glGetError() == GL_INVALID_OPERATION) {
1959 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &errPos);
1960 FIXME("HW PixelShader Error at position %d: %s\n",
1961 errPos, debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB)));
1962 This->baseShader.prgId = -1;
1966 static void shader_arb_generate_vshader(IWineD3DVertexShader *iface, SHADER_BUFFER *buffer) {
1967 IWineD3DVertexShaderImpl *This = (IWineD3DVertexShaderImpl *)iface;
1968 shader_reg_maps* reg_maps = &This->baseShader.reg_maps;
1969 CONST DWORD *function = This->baseShader.function;
1970 WineD3D_GL_Info *gl_info = &((IWineD3DDeviceImpl *)This->baseShader.device)->adapter->gl_info;
1972 /* Create the hw ARB shader */
1973 shader_addline(buffer, "!!ARBvp1.0\n");
1974 shader_addline(buffer, "PARAM helper_const = { 2.0, -1.0, %d.0, 0.0 };\n", This->rel_offset);
1976 /* Mesa supports only 95 constants */
1977 if (GL_VEND(MESA) || GL_VEND(WINE))
1978 This->baseShader.limits.constant_float =
1979 min(95, This->baseShader.limits.constant_float);
1981 shader_addline(buffer, "TEMP TMP;\n");
1983 /* Base Declarations */
1984 shader_generate_arb_declarations( (IWineD3DBaseShader*) This, reg_maps, buffer, &GLINFO_LOCATION);
1986 /* We need a constant to fixup the final position */
1987 shader_addline(buffer, "PARAM posFixup = program.env[%d];\n", ARB_SHADER_PRIVCONST_POS);
1989 if((GLINFO_LOCATION).set_texcoord_w) {
1991 for(i = 0; i < min(8, MAX_REG_TEXCRD); i++) {
1992 if(This->baseShader.reg_maps.texcoord_mask[i] != 0 &&
1993 This->baseShader.reg_maps.texcoord_mask[i] != WINED3DSP_WRITEMASK_ALL) {
1994 shader_addline(buffer, "MOV result.texcoord[%u].w, -helper_const.y;\n", i);
1999 /* Base Shader Body */
2000 shader_generate_main( (IWineD3DBaseShader*) This, buffer, reg_maps, function);
2002 /* If this shader doesn't use fog copy the z coord to the fog coord so that we can use table fog */
2004 shader_addline(buffer, "MOV result.fogcoord, TMP_OUT.z;\n");
2006 /* Write the final position.
2008 * OpenGL coordinates specify the center of the pixel while d3d coords specify
2009 * the corner. The offsets are stored in z and w in posFixup. posFixup.y contains
2010 * 1.0 or -1.0 to turn the rendering upside down for offscreen rendering. PosFixup.x
2011 * contains 1.0 to allow a mad, but arb vs swizzles are too restricted for that.
2013 shader_addline(buffer, "MUL TMP, posFixup, TMP_OUT.w;\n");
2014 shader_addline(buffer, "ADD TMP_OUT.x, TMP_OUT.x, TMP.z;\n");
2015 shader_addline(buffer, "MAD TMP_OUT.y, TMP_OUT.y, posFixup.y, TMP.w;\n");
2017 /* Z coord [0;1]->[-1;1] mapping, see comment in transform_projection in state.c
2018 * and the glsl equivalent
2020 shader_addline(buffer, "MAD TMP_OUT.z, TMP_OUT.z, helper_const.x, -TMP_OUT.w;\n");
2022 shader_addline(buffer, "MOV result.position, TMP_OUT;\n");
2024 shader_addline(buffer, "END\n");
2026 /* TODO: change to resource.glObjectHandle or something like that */
2027 GL_EXTCALL(glGenProgramsARB(1, &This->baseShader.prgId));
2029 TRACE("Creating a hw vertex shader, prg=%d\n", This->baseShader.prgId);
2030 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB, This->baseShader.prgId));
2032 TRACE("Created hw vertex shader, prg=%d\n", This->baseShader.prgId);
2033 /* Create the program and check for errors */
2034 GL_EXTCALL(glProgramStringARB(GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
2035 buffer->bsize, buffer->buffer));
2037 if (glGetError() == GL_INVALID_OPERATION) {
2039 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &errPos);
2040 FIXME("HW VertexShader Error at position %d: %s\n",
2041 errPos, debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB)));
2042 This->baseShader.prgId = -1;
2046 static void shader_arb_get_caps(WINED3DDEVTYPE devtype, WineD3D_GL_Info *gl_info, struct shader_caps *pCaps) {
2047 /* We don't have an ARB fixed function pipeline yet, so let the none backend set its caps,
2048 * then overwrite the shader specific ones
2050 none_shader_backend.shader_get_caps(devtype, gl_info, pCaps);
2052 pCaps->VertexShaderVersion = WINED3DVS_VERSION(1,1);
2053 TRACE_(d3d_caps)("Hardware vertex shader version 1.1 enabled (ARB_PROGRAM)\n");
2054 pCaps->MaxVertexShaderConst = GL_LIMITS(vshader_constantsF);
2056 pCaps->PixelShaderVersion = WINED3DPS_VERSION(1,4);
2057 pCaps->PixelShader1xMaxValue = 8.0;
2058 TRACE_(d3d_caps)("Hardware pixel shader version 1.4 enabled (ARB_PROGRAM)\n");
2061 static void shader_arb_load_init(void) {
2064 const shader_backend_t arb_program_shader_backend = {
2066 &shader_arb_select_depth_blt,
2067 &shader_arb_destroy_depth_blt,
2068 &shader_arb_load_constants,
2069 &shader_arb_cleanup,
2070 &shader_arb_color_correction,
2071 &shader_arb_destroy,
2074 &shader_arb_dirty_const,
2075 &shader_arb_generate_pshader,
2076 &shader_arb_generate_vshader,
2077 &shader_arb_get_caps,
2078 &shader_arb_load_init,