winex11: Remove support for owner-displayed clipboard formats.
[wine.git] / dlls / wined3d / ati_fragment_shader.c
blobcd320097bea142ba46d6dc109876b4d92d122f33
1 /*
2 * Fixed function pipeline replacement using GL_ATI_fragment_shader
4 * Copyright 2008 Stefan Dösinger(for CodeWeavers)
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <math.h>
25 #include <stdio.h>
27 #include "wined3d_private.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
30 WINE_DECLARE_DEBUG_CHANNEL(d3d);
32 /* Context activation for state handlers is done by the caller. */
34 /* Some private defines, Constant associations, etc.
35 * Env bump matrix and per stage constant should be independent,
36 * a stage that bump maps can't read the per state constant
38 #define ATIFS_CONST_BUMPMAT(i) (GL_CON_0_ATI + i)
39 #define ATIFS_CONST_STAGE(i) (GL_CON_0_ATI + i)
40 #define ATIFS_CONST_TFACTOR GL_CON_6_ATI
42 enum atifs_constant_value
44 ATIFS_CONSTANT_UNUSED = 0,
45 ATIFS_CONSTANT_BUMP,
46 ATIFS_CONSTANT_TFACTOR,
47 ATIFS_CONSTANT_STAGE,
50 /* GL_ATI_fragment_shader specific fixed function pipeline description. "Inherits" from the common one */
51 struct atifs_ffp_desc
53 struct ffp_frag_desc parent;
54 GLuint shader;
55 unsigned int num_textures_used;
56 enum atifs_constant_value constants[8];
59 struct atifs_private_data
61 struct wine_rb_tree fragment_shaders; /* A rb-tree to track fragment pipeline replacement shaders */
64 struct atifs_context_private_data
66 const struct atifs_ffp_desc *last_shader;
69 static const char *debug_dstmod(GLuint mod) {
70 switch(mod) {
71 case GL_NONE: return "GL_NONE";
72 case GL_2X_BIT_ATI: return "GL_2X_BIT_ATI";
73 case GL_4X_BIT_ATI: return "GL_4X_BIT_ATI";
74 case GL_8X_BIT_ATI: return "GL_8X_BIT_ATI";
75 case GL_HALF_BIT_ATI: return "GL_HALF_BIT_ATI";
76 case GL_QUARTER_BIT_ATI: return "GL_QUARTER_BIT_ATI";
77 case GL_EIGHTH_BIT_ATI: return "GL_EIGHTH_BIT_ATI";
78 case GL_SATURATE_BIT_ATI: return "GL_SATURATE_BIT_ATI";
79 default: return "Unexpected modifier\n";
83 static const char *debug_argmod(GLuint mod) {
84 switch(mod) {
85 case GL_NONE:
86 return "GL_NONE";
88 case GL_2X_BIT_ATI:
89 return "GL_2X_BIT_ATI";
90 case GL_COMP_BIT_ATI:
91 return "GL_COMP_BIT_ATI";
92 case GL_NEGATE_BIT_ATI:
93 return "GL_NEGATE_BIT_ATI";
94 case GL_BIAS_BIT_ATI:
95 return "GL_BIAS_BIT_ATI";
97 case GL_2X_BIT_ATI | GL_COMP_BIT_ATI:
98 return "GL_2X_BIT_ATI | GL_COMP_BIT_ATI";
99 case GL_2X_BIT_ATI | GL_NEGATE_BIT_ATI:
100 return "GL_2X_BIT_ATI | GL_NEGATE_BIT_ATI";
101 case GL_2X_BIT_ATI | GL_BIAS_BIT_ATI:
102 return "GL_2X_BIT_ATI | GL_BIAS_BIT_ATI";
103 case GL_COMP_BIT_ATI | GL_NEGATE_BIT_ATI:
104 return "GL_COMP_BIT_ATI | GL_NEGATE_BIT_ATI";
105 case GL_COMP_BIT_ATI | GL_BIAS_BIT_ATI:
106 return "GL_COMP_BIT_ATI | GL_BIAS_BIT_ATI";
107 case GL_NEGATE_BIT_ATI | GL_BIAS_BIT_ATI:
108 return "GL_NEGATE_BIT_ATI | GL_BIAS_BIT_ATI";
110 case GL_COMP_BIT_ATI | GL_NEGATE_BIT_ATI | GL_BIAS_BIT_ATI:
111 return "GL_COMP_BIT_ATI | GL_NEGATE_BIT_ATI | GL_BIAS_BIT_ATI";
112 case GL_2X_BIT_ATI | GL_NEGATE_BIT_ATI | GL_BIAS_BIT_ATI:
113 return "GL_2X_BIT_ATI | GL_NEGATE_BIT_ATI | GL_BIAS_BIT_ATI";
114 case GL_2X_BIT_ATI | GL_COMP_BIT_ATI | GL_BIAS_BIT_ATI:
115 return "GL_2X_BIT_ATI | GL_COMP_BIT_ATI | GL_BIAS_BIT_ATI";
116 case GL_2X_BIT_ATI | GL_COMP_BIT_ATI | GL_NEGATE_BIT_ATI:
117 return "GL_2X_BIT_ATI | GL_COMP_BIT_ATI | GL_NEGATE_BIT_ATI";
119 case GL_2X_BIT_ATI | GL_COMP_BIT_ATI | GL_NEGATE_BIT_ATI | GL_BIAS_BIT_ATI:
120 return "GL_2X_BIT_ATI | GL_COMP_BIT_ATI | GL_NEGATE_BIT_ATI | GL_BIAS_BIT_ATI";
122 default:
123 return "Unexpected argmod combination\n";
126 static const char *debug_register(GLuint reg) {
127 switch(reg) {
128 case GL_REG_0_ATI: return "GL_REG_0_ATI";
129 case GL_REG_1_ATI: return "GL_REG_1_ATI";
130 case GL_REG_2_ATI: return "GL_REG_2_ATI";
131 case GL_REG_3_ATI: return "GL_REG_3_ATI";
132 case GL_REG_4_ATI: return "GL_REG_4_ATI";
133 case GL_REG_5_ATI: return "GL_REG_5_ATI";
135 case GL_CON_0_ATI: return "GL_CON_0_ATI";
136 case GL_CON_1_ATI: return "GL_CON_1_ATI";
137 case GL_CON_2_ATI: return "GL_CON_2_ATI";
138 case GL_CON_3_ATI: return "GL_CON_3_ATI";
139 case GL_CON_4_ATI: return "GL_CON_4_ATI";
140 case GL_CON_5_ATI: return "GL_CON_5_ATI";
141 case GL_CON_6_ATI: return "GL_CON_6_ATI";
142 case GL_CON_7_ATI: return "GL_CON_7_ATI";
144 case GL_ZERO: return "GL_ZERO";
145 case GL_ONE: return "GL_ONE";
146 case GL_PRIMARY_COLOR: return "GL_PRIMARY_COLOR";
147 case GL_SECONDARY_INTERPOLATOR_ATI: return "GL_SECONDARY_INTERPOLATOR_ATI";
149 default: return "Unknown register\n";
153 static const char *debug_swizzle(GLuint swizzle) {
154 switch(swizzle) {
155 case GL_SWIZZLE_STR_ATI: return "GL_SWIZZLE_STR_ATI";
156 case GL_SWIZZLE_STQ_ATI: return "GL_SWIZZLE_STQ_ATI";
157 case GL_SWIZZLE_STR_DR_ATI: return "GL_SWIZZLE_STR_DR_ATI";
158 case GL_SWIZZLE_STQ_DQ_ATI: return "GL_SWIZZLE_STQ_DQ_ATI";
159 default: return "unknown swizzle";
163 static const char *debug_rep(GLuint rep) {
164 switch(rep) {
165 case GL_NONE: return "GL_NONE";
166 case GL_RED: return "GL_RED";
167 case GL_GREEN: return "GL_GREEN";
168 case GL_BLUE: return "GL_BLUE";
169 case GL_ALPHA: return "GL_ALPHA";
170 default: return "unknown argrep";
174 static const char *debug_op(GLuint op) {
175 switch(op) {
176 case GL_MOV_ATI: return "GL_MOV_ATI";
177 case GL_ADD_ATI: return "GL_ADD_ATI";
178 case GL_MUL_ATI: return "GL_MUL_ATI";
179 case GL_SUB_ATI: return "GL_SUB_ATI";
180 case GL_DOT3_ATI: return "GL_DOT3_ATI";
181 case GL_DOT4_ATI: return "GL_DOT4_ATI";
182 case GL_MAD_ATI: return "GL_MAD_ATI";
183 case GL_LERP_ATI: return "GL_LERP_ATI";
184 case GL_CND_ATI: return "GL_CND_ATI";
185 case GL_CND0_ATI: return "GL_CND0_ATI";
186 case GL_DOT2_ADD_ATI: return "GL_DOT2_ADD_ATI";
187 default: return "unexpected op";
191 static const char *debug_mask(GLuint mask) {
192 switch(mask) {
193 case GL_NONE: return "GL_NONE";
194 case GL_RED_BIT_ATI: return "GL_RED_BIT_ATI";
195 case GL_GREEN_BIT_ATI: return "GL_GREEN_BIT_ATI";
196 case GL_BLUE_BIT_ATI: return "GL_BLUE_BIT_ATI";
197 case GL_RED_BIT_ATI | GL_GREEN_BIT_ATI: return "GL_RED_BIT_ATI | GL_GREEN_BIT_ATI";
198 case GL_RED_BIT_ATI | GL_BLUE_BIT_ATI: return "GL_RED_BIT_ATI | GL_BLUE_BIT_ATI";
199 case GL_GREEN_BIT_ATI | GL_BLUE_BIT_ATI:return "GL_GREEN_BIT_ATI | GL_BLUE_BIT_ATI";
200 case GL_RED_BIT_ATI | GL_GREEN_BIT_ATI | GL_BLUE_BIT_ATI:return "GL_RED_BIT_ATI | GL_GREEN_BIT_ATI | GL_BLUE_BIT_ATI";
201 default: return "Unexpected writemask";
205 static void wrap_op1(const struct wined3d_gl_info *gl_info, GLuint op, GLuint dst, GLuint dstMask, GLuint dstMod,
206 GLuint arg1, GLuint arg1Rep, GLuint arg1Mod)
208 if(dstMask == GL_ALPHA) {
209 TRACE("glAlphaFragmentOp1ATI(%s, %s, %s, %s, %s, %s)\n", debug_op(op), debug_register(dst), debug_dstmod(dstMod),
210 debug_register(arg1), debug_rep(arg1Rep), debug_argmod(arg1Mod));
211 GL_EXTCALL(glAlphaFragmentOp1ATI(op, dst, dstMod, arg1, arg1Rep, arg1Mod));
212 } else {
213 TRACE("glColorFragmentOp1ATI(%s, %s, %s, %s, %s, %s, %s)\n", debug_op(op), debug_register(dst),
214 debug_mask(dstMask), debug_dstmod(dstMod),
215 debug_register(arg1), debug_rep(arg1Rep), debug_argmod(arg1Mod));
216 GL_EXTCALL(glColorFragmentOp1ATI(op, dst, dstMask, dstMod, arg1, arg1Rep, arg1Mod));
220 static void wrap_op2(const struct wined3d_gl_info *gl_info, GLuint op, GLuint dst, GLuint dstMask, GLuint dstMod,
221 GLuint arg1, GLuint arg1Rep, GLuint arg1Mod, GLuint arg2, GLuint arg2Rep, GLuint arg2Mod)
223 if(dstMask == GL_ALPHA) {
224 TRACE("glAlphaFragmentOp2ATI(%s, %s, %s, %s, %s, %s, %s, %s, %s)\n", debug_op(op), debug_register(dst), debug_dstmod(dstMod),
225 debug_register(arg1), debug_rep(arg1Rep), debug_argmod(arg1Mod),
226 debug_register(arg2), debug_rep(arg2Rep), debug_argmod(arg2Mod));
227 GL_EXTCALL(glAlphaFragmentOp2ATI(op, dst, dstMod, arg1, arg1Rep, arg1Mod, arg2, arg2Rep, arg2Mod));
228 } else {
229 TRACE("glColorFragmentOp2ATI(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)\n", debug_op(op), debug_register(dst),
230 debug_mask(dstMask), debug_dstmod(dstMod),
231 debug_register(arg1), debug_rep(arg1Rep), debug_argmod(arg1Mod),
232 debug_register(arg2), debug_rep(arg2Rep), debug_argmod(arg2Mod));
233 GL_EXTCALL(glColorFragmentOp2ATI(op, dst, dstMask, dstMod, arg1, arg1Rep, arg1Mod, arg2, arg2Rep, arg2Mod));
237 static void wrap_op3(const struct wined3d_gl_info *gl_info, GLuint op, GLuint dst, GLuint dstMask, GLuint dstMod,
238 GLuint arg1, GLuint arg1Rep, GLuint arg1Mod, GLuint arg2, GLuint arg2Rep, GLuint arg2Mod,
239 GLuint arg3, GLuint arg3Rep, GLuint arg3Mod)
241 if(dstMask == GL_ALPHA) {
242 /* Leave some free space to fit "GL_NONE, " in to align most alpha and color op lines */
243 TRACE("glAlphaFragmentOp3ATI(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)\n", debug_op(op), debug_register(dst), debug_dstmod(dstMod),
244 debug_register(arg1), debug_rep(arg1Rep), debug_argmod(arg1Mod),
245 debug_register(arg2), debug_rep(arg2Rep), debug_argmod(arg2Mod),
246 debug_register(arg3), debug_rep(arg3Rep), debug_argmod(arg3Mod));
247 GL_EXTCALL(glAlphaFragmentOp3ATI(op, dst, dstMod,
248 arg1, arg1Rep, arg1Mod,
249 arg2, arg2Rep, arg2Mod,
250 arg3, arg3Rep, arg3Mod));
251 } else {
252 TRACE("glColorFragmentOp3ATI(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)\n", debug_op(op), debug_register(dst),
253 debug_mask(dstMask), debug_dstmod(dstMod),
254 debug_register(arg1), debug_rep(arg1Rep), debug_argmod(arg1Mod),
255 debug_register(arg2), debug_rep(arg2Rep), debug_argmod(arg2Mod),
256 debug_register(arg3), debug_rep(arg3Rep), debug_argmod(arg3Mod));
257 GL_EXTCALL(glColorFragmentOp3ATI(op, dst, dstMask, dstMod,
258 arg1, arg1Rep, arg1Mod,
259 arg2, arg2Rep, arg2Mod,
260 arg3, arg3Rep, arg3Mod));
264 static GLuint register_for_arg(DWORD arg, const struct wined3d_gl_info *gl_info,
265 unsigned int stage, GLuint *mod, GLuint *rep, GLuint tmparg)
267 GLenum ret;
269 if(mod) *mod = GL_NONE;
270 if(arg == ARG_UNUSED)
272 if (rep) *rep = GL_NONE;
273 return -1; /* This is the marker for unused registers */
276 switch(arg & WINED3DTA_SELECTMASK) {
277 case WINED3DTA_DIFFUSE:
278 ret = GL_PRIMARY_COLOR;
279 break;
281 case WINED3DTA_CURRENT:
282 /* Note that using GL_REG_0_ATI for the passed on register is safe because
283 * texture0 is read at stage0, so in the worst case it is read in the
284 * instruction writing to reg0. Afterwards texture0 is not used any longer.
285 * If we're reading from current
287 ret = stage ? GL_REG_0_ATI : GL_PRIMARY_COLOR;
288 break;
290 case WINED3DTA_TEXTURE:
291 ret = GL_REG_0_ATI + stage;
292 break;
294 case WINED3DTA_TFACTOR:
295 ret = ATIFS_CONST_TFACTOR;
296 break;
298 case WINED3DTA_SPECULAR:
299 ret = GL_SECONDARY_INTERPOLATOR_ATI;
300 break;
302 case WINED3DTA_TEMP:
303 ret = tmparg;
304 break;
306 case WINED3DTA_CONSTANT:
307 ret = ATIFS_CONST_STAGE(stage);
308 break;
310 default:
311 FIXME("Unknown source argument %d\n", arg);
312 ret = GL_ZERO;
315 if(arg & WINED3DTA_COMPLEMENT) {
316 if(mod) *mod |= GL_COMP_BIT_ATI;
318 if(arg & WINED3DTA_ALPHAREPLICATE) {
319 if(rep) *rep = GL_ALPHA;
320 } else {
321 if(rep) *rep = GL_NONE;
323 return ret;
326 static GLuint find_tmpreg(const struct texture_stage_op op[MAX_TEXTURES])
328 int lowest_read = -1;
329 int lowest_write = -1;
330 int i;
331 BOOL tex_used[MAX_TEXTURES];
333 memset(tex_used, 0, sizeof(tex_used));
334 for (i = 0; i < MAX_TEXTURES; ++i)
336 if (op[i].cop == WINED3D_TOP_DISABLE)
337 break;
339 if(lowest_read == -1 &&
340 (op[i].carg1 == WINED3DTA_TEMP || op[i].carg2 == WINED3DTA_TEMP || op[i].carg0 == WINED3DTA_TEMP ||
341 op[i].aarg1 == WINED3DTA_TEMP || op[i].aarg2 == WINED3DTA_TEMP || op[i].aarg0 == WINED3DTA_TEMP)) {
342 lowest_read = i;
345 if(lowest_write == -1 && op[i].dst == tempreg) {
346 lowest_write = i;
349 if(op[i].carg1 == WINED3DTA_TEXTURE || op[i].carg2 == WINED3DTA_TEXTURE || op[i].carg0 == WINED3DTA_TEXTURE ||
350 op[i].aarg1 == WINED3DTA_TEXTURE || op[i].aarg2 == WINED3DTA_TEXTURE || op[i].aarg0 == WINED3DTA_TEXTURE) {
351 tex_used[i] = TRUE;
355 /* Temp reg not read? We don't need it, return GL_NONE */
356 if(lowest_read == -1) return GL_NONE;
358 if(lowest_write >= lowest_read) {
359 FIXME("Temp register read before being written\n");
362 if(lowest_write == -1) {
363 /* This needs a test. Maybe we are supposed to return 0.0/0.0/0.0/0.0, or fail drawprim, or whatever */
364 FIXME("Temp register read without being written\n");
365 return GL_REG_1_ATI;
366 } else if(lowest_write >= 1) {
367 /* If we're writing to the temp reg at earliest in stage 1, we can use register 1 for the temp result.
368 * there may be texture data stored in reg 1, but we do not need it any longer since stage 1 already
369 * read it
371 return GL_REG_1_ATI;
372 } else {
373 /* Search for a free texture register. We have 6 registers available. GL_REG_0_ATI is already used
374 * for the regular result
376 for(i = 1; i < 6; i++) {
377 if(!tex_used[i]) {
378 return GL_REG_0_ATI + i;
381 /* What to do here? Report it in ValidateDevice? */
382 FIXME("Could not find a register for the temporary register\n");
383 return 0;
387 static const struct color_fixup_desc color_fixup_rg =
389 1, CHANNEL_SOURCE_X,
390 1, CHANNEL_SOURCE_Y,
391 0, CHANNEL_SOURCE_ONE,
392 0, CHANNEL_SOURCE_ONE
394 static const struct color_fixup_desc color_fixup_rgl =
396 1, CHANNEL_SOURCE_X,
397 1, CHANNEL_SOURCE_Y,
398 0, CHANNEL_SOURCE_Z,
399 0, CHANNEL_SOURCE_W
401 static const struct color_fixup_desc color_fixup_rgba =
403 1, CHANNEL_SOURCE_X,
404 1, CHANNEL_SOURCE_Y,
405 1, CHANNEL_SOURCE_Z,
406 1, CHANNEL_SOURCE_W
409 static BOOL op_reads_texture(const struct texture_stage_op *op)
411 return (op->carg0 & WINED3DTA_SELECTMASK) == WINED3DTA_TEXTURE
412 || (op->carg1 & WINED3DTA_SELECTMASK) == WINED3DTA_TEXTURE
413 || (op->carg2 & WINED3DTA_SELECTMASK) == WINED3DTA_TEXTURE
414 || (op->aarg0 & WINED3DTA_SELECTMASK) == WINED3DTA_TEXTURE
415 || (op->aarg1 & WINED3DTA_SELECTMASK) == WINED3DTA_TEXTURE
416 || (op->aarg2 & WINED3DTA_SELECTMASK) == WINED3DTA_TEXTURE
417 || op->cop == WINED3D_TOP_BLEND_TEXTURE_ALPHA;
420 static void atifs_color_fixup(const struct wined3d_gl_info *gl_info, struct color_fixup_desc fixup, GLuint reg)
422 if(is_same_fixup(fixup, color_fixup_rg))
424 wrap_op1(gl_info, GL_MOV_ATI, reg, GL_RED_BIT_ATI | GL_GREEN_BIT_ATI, GL_NONE,
425 reg, GL_NONE, GL_2X_BIT_ATI | GL_BIAS_BIT_ATI);
426 wrap_op1(gl_info, GL_MOV_ATI, reg, GL_BLUE_BIT_ATI, GL_NONE,
427 GL_ONE, GL_NONE, GL_NONE);
428 wrap_op1(gl_info, GL_MOV_ATI, reg, GL_ALPHA, GL_NONE,
429 GL_ONE, GL_NONE, GL_NONE);
431 else if(is_same_fixup(fixup, color_fixup_rgl))
433 wrap_op1(gl_info, GL_MOV_ATI, reg, GL_RED_BIT_ATI | GL_GREEN_BIT_ATI, GL_NONE,
434 reg, GL_NONE, GL_2X_BIT_ATI | GL_BIAS_BIT_ATI);
436 else if (is_same_fixup(fixup, color_fixup_rgba))
438 wrap_op1(gl_info, GL_MOV_ATI, reg, GL_NONE, GL_NONE,
439 reg, GL_NONE, GL_2X_BIT_ATI | GL_BIAS_BIT_ATI);
440 wrap_op1(gl_info, GL_MOV_ATI, reg, GL_ALPHA, GL_NONE,
441 reg, GL_NONE, GL_2X_BIT_ATI | GL_BIAS_BIT_ATI);
443 else
445 /* Should not happen - atifs_color_fixup_supported refuses other fixups. */
446 ERR("Unsupported color fixup.\n");
450 static BOOL op_reads_tfactor(const struct texture_stage_op *op)
452 return (op->carg0 & WINED3DTA_SELECTMASK) == WINED3DTA_TFACTOR
453 || (op->carg1 & WINED3DTA_SELECTMASK) == WINED3DTA_TFACTOR
454 || (op->carg2 & WINED3DTA_SELECTMASK) == WINED3DTA_TFACTOR
455 || (op->aarg0 & WINED3DTA_SELECTMASK) == WINED3DTA_TFACTOR
456 || (op->aarg1 & WINED3DTA_SELECTMASK) == WINED3DTA_TFACTOR
457 || (op->aarg2 & WINED3DTA_SELECTMASK) == WINED3DTA_TFACTOR
458 || op->cop == WINED3D_TOP_BLEND_FACTOR_ALPHA
459 || op->aop == WINED3D_TOP_BLEND_FACTOR_ALPHA;
462 static BOOL op_reads_constant(const struct texture_stage_op *op)
464 return (op->carg0 & WINED3DTA_SELECTMASK) == WINED3DTA_CONSTANT
465 || (op->carg1 & WINED3DTA_SELECTMASK) == WINED3DTA_CONSTANT
466 || (op->carg2 & WINED3DTA_SELECTMASK) == WINED3DTA_CONSTANT
467 || (op->aarg0 & WINED3DTA_SELECTMASK) == WINED3DTA_CONSTANT
468 || (op->aarg1 & WINED3DTA_SELECTMASK) == WINED3DTA_CONSTANT
469 || (op->aarg2 & WINED3DTA_SELECTMASK) == WINED3DTA_CONSTANT;
472 static GLuint gen_ati_shader(const struct texture_stage_op op[MAX_TEXTURES],
473 const struct wined3d_gl_info *gl_info, enum atifs_constant_value *constants)
475 GLuint ret = GL_EXTCALL(glGenFragmentShadersATI(1));
476 unsigned int stage;
477 GLuint arg0, arg1, arg2, extrarg;
478 GLuint dstmod, argmod0, argmod1, argmod2, argmodextra;
479 GLuint rep0, rep1, rep2;
480 GLuint swizzle;
481 GLuint tmparg = find_tmpreg(op);
482 GLuint dstreg;
483 BOOL tfactor_used = FALSE;
485 if(!ret) {
486 ERR("Failed to generate a GL_ATI_fragment_shader shader id\n");
487 return 0;
489 GL_EXTCALL(glBindFragmentShaderATI(ret));
490 checkGLcall("GL_EXTCALL(glBindFragmentShaderATI(ret))");
492 TRACE("glBeginFragmentShaderATI()\n");
493 GL_EXTCALL(glBeginFragmentShaderATI());
494 checkGLcall("GL_EXTCALL(glBeginFragmentShaderATI())");
496 /* Pass 1: Generate sampling instructions for perturbation maps */
497 for (stage = 0; stage < gl_info->limits.textures; ++stage)
499 if (op[stage].cop == WINED3D_TOP_DISABLE)
500 break;
501 if (op[stage].cop != WINED3D_TOP_BUMPENVMAP
502 && op[stage].cop != WINED3D_TOP_BUMPENVMAP_LUMINANCE)
503 continue;
505 constants[stage] = ATIFS_CONSTANT_BUMP;
507 TRACE("glSampleMapATI(GL_REG_%d_ATI, GL_TEXTURE_%d_ARB, GL_SWIZZLE_STR_ATI)\n",
508 stage, stage);
509 GL_EXTCALL(glSampleMapATI(GL_REG_0_ATI + stage,
510 GL_TEXTURE0_ARB + stage,
511 GL_SWIZZLE_STR_ATI));
512 if(op[stage + 1].projected == proj_none) {
513 swizzle = GL_SWIZZLE_STR_ATI;
514 } else if(op[stage + 1].projected == proj_count4) {
515 swizzle = GL_SWIZZLE_STQ_DQ_ATI;
516 } else {
517 swizzle = GL_SWIZZLE_STR_DR_ATI;
519 TRACE("glPassTexCoordATI(GL_REG_%d_ATI, GL_TEXTURE_%d_ARB, %s)\n",
520 stage + 1, stage + 1, debug_swizzle(swizzle));
521 GL_EXTCALL(glPassTexCoordATI(GL_REG_0_ATI + stage + 1,
522 GL_TEXTURE0_ARB + stage + 1,
523 swizzle));
526 /* Pass 2: Generate perturbation calculations */
527 for (stage = 0; stage < gl_info->limits.textures; ++stage)
529 GLuint argmodextra_x, argmodextra_y;
530 struct color_fixup_desc fixup;
532 if (op[stage].cop == WINED3D_TOP_DISABLE)
533 break;
534 if (op[stage].cop != WINED3D_TOP_BUMPENVMAP
535 && op[stage].cop != WINED3D_TOP_BUMPENVMAP_LUMINANCE)
536 continue;
538 fixup = op[stage].color_fixup;
539 if (fixup.x_source != CHANNEL_SOURCE_X || fixup.y_source != CHANNEL_SOURCE_Y)
541 FIXME("Swizzles not implemented\n");
542 argmodextra_x = GL_NONE;
543 argmodextra_y = GL_NONE;
545 else
547 /* Nice thing, we get the color correction for free :-) */
548 argmodextra_x = fixup.x_sign_fixup ? GL_2X_BIT_ATI | GL_BIAS_BIT_ATI : GL_NONE;
549 argmodextra_y = fixup.y_sign_fixup ? GL_2X_BIT_ATI | GL_BIAS_BIT_ATI : GL_NONE;
552 wrap_op3(gl_info, GL_DOT2_ADD_ATI, GL_REG_0_ATI + stage + 1, GL_RED_BIT_ATI, GL_NONE,
553 GL_REG_0_ATI + stage, GL_NONE, argmodextra_x,
554 ATIFS_CONST_BUMPMAT(stage), GL_NONE, GL_2X_BIT_ATI | GL_BIAS_BIT_ATI,
555 GL_REG_0_ATI + stage + 1, GL_RED, GL_NONE);
557 /* Don't use GL_DOT2_ADD_ATI here because we cannot configure it to read the blue and alpha
558 * component of the bump matrix. Instead do this with two MADs:
560 * coord.a = tex.r * bump.b + coord.g
561 * coord.g = tex.g * bump.a + coord.a
563 * The first instruction writes to alpha so it can be coissued with the above DOT2_ADD.
564 * coord.a is unused. If the perturbed texture is projected, this was already handled
565 * in the glPassTexCoordATI above.
567 wrap_op3(gl_info, GL_MAD_ATI, GL_REG_0_ATI + stage + 1, GL_ALPHA, GL_NONE,
568 GL_REG_0_ATI + stage, GL_RED, argmodextra_y,
569 ATIFS_CONST_BUMPMAT(stage), GL_BLUE, GL_2X_BIT_ATI | GL_BIAS_BIT_ATI,
570 GL_REG_0_ATI + stage + 1, GL_GREEN, GL_NONE);
571 wrap_op3(gl_info, GL_MAD_ATI, GL_REG_0_ATI + stage + 1, GL_GREEN_BIT_ATI, GL_NONE,
572 GL_REG_0_ATI + stage, GL_GREEN, argmodextra_y,
573 ATIFS_CONST_BUMPMAT(stage), GL_ALPHA, GL_2X_BIT_ATI | GL_BIAS_BIT_ATI,
574 GL_REG_0_ATI + stage + 1, GL_ALPHA, GL_NONE);
577 /* Pass 3: Generate sampling instructions for regular textures */
578 for (stage = 0; stage < gl_info->limits.textures; ++stage)
580 if (op[stage].cop == WINED3D_TOP_DISABLE)
581 break;
583 if(op[stage].projected == proj_none) {
584 swizzle = GL_SWIZZLE_STR_ATI;
585 } else if(op[stage].projected == proj_count3) {
586 swizzle = GL_SWIZZLE_STR_DR_ATI;
587 } else {
588 swizzle = GL_SWIZZLE_STQ_DQ_ATI;
591 if (op_reads_texture(&op[stage]))
593 if (stage > 0
594 && (op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP
595 || op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE))
597 TRACE("glSampleMapATI(GL_REG_%d_ATI, GL_REG_%d_ATI, GL_SWIZZLE_STR_ATI)\n",
598 stage, stage);
599 GL_EXTCALL(glSampleMapATI(GL_REG_0_ATI + stage,
600 GL_REG_0_ATI + stage,
601 GL_SWIZZLE_STR_ATI));
602 } else {
603 TRACE("glSampleMapATI(GL_REG_%d_ATI, GL_TEXTURE_%d_ARB, %s)\n",
604 stage, stage, debug_swizzle(swizzle));
605 GL_EXTCALL(glSampleMapATI(GL_REG_0_ATI + stage,
606 GL_TEXTURE0_ARB + stage,
607 swizzle));
612 /* Pass 4: Generate the arithmetic instructions */
613 for (stage = 0; stage < MAX_TEXTURES; ++stage)
615 if (op[stage].cop == WINED3D_TOP_DISABLE)
617 if (!stage)
619 /* Handle complete texture disabling gracefully */
620 wrap_op1(gl_info, GL_MOV_ATI, GL_REG_0_ATI, GL_NONE, GL_NONE,
621 GL_PRIMARY_COLOR, GL_NONE, GL_NONE);
622 wrap_op1(gl_info, GL_MOV_ATI, GL_REG_0_ATI, GL_ALPHA, GL_NONE,
623 GL_PRIMARY_COLOR, GL_NONE, GL_NONE);
625 break;
628 if(op[stage].dst == tempreg) {
629 /* If we're writing to D3DTA_TEMP, but never reading from it we don't have to write there in the first place.
630 * skip the entire stage, this saves some GPU time
632 if(tmparg == GL_NONE) continue;
634 dstreg = tmparg;
635 } else {
636 dstreg = GL_REG_0_ATI;
639 if (op[stage].cop == WINED3D_TOP_BUMPENVMAP || op[stage].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
641 /* Those are handled in the first pass of the shader (generation pass 1 and 2) already */
642 continue;
645 arg0 = register_for_arg(op[stage].carg0, gl_info, stage, &argmod0, &rep0, tmparg);
646 arg1 = register_for_arg(op[stage].carg1, gl_info, stage, &argmod1, &rep1, tmparg);
647 arg2 = register_for_arg(op[stage].carg2, gl_info, stage, &argmod2, &rep2, tmparg);
648 dstmod = GL_NONE;
649 argmodextra = GL_NONE;
650 extrarg = GL_NONE;
652 if (op_reads_tfactor(&op[stage]))
653 tfactor_used = TRUE;
655 if (op_reads_constant(&op[stage]))
657 if (constants[stage] != ATIFS_CONSTANT_UNUSED)
658 FIXME("Constant %u already used.\n", stage);
659 constants[stage] = ATIFS_CONSTANT_STAGE;
662 if (op_reads_texture(&op[stage]) && !is_identity_fixup(op[stage].color_fixup))
663 atifs_color_fixup(gl_info, op[stage].color_fixup, GL_REG_0_ATI + stage);
665 switch (op[stage].cop)
667 case WINED3D_TOP_SELECT_ARG2:
668 arg1 = arg2;
669 argmod1 = argmod2;
670 rep1 = rep2;
671 /* fall through */
672 case WINED3D_TOP_SELECT_ARG1:
673 wrap_op1(gl_info, GL_MOV_ATI, dstreg, GL_NONE, GL_NONE,
674 arg1, rep1, argmod1);
675 break;
677 case WINED3D_TOP_MODULATE_4X:
678 if(dstmod == GL_NONE) dstmod = GL_4X_BIT_ATI;
679 /* fall through */
680 case WINED3D_TOP_MODULATE_2X:
681 if(dstmod == GL_NONE) dstmod = GL_2X_BIT_ATI;
682 dstmod |= GL_SATURATE_BIT_ATI;
683 /* fall through */
684 case WINED3D_TOP_MODULATE:
685 wrap_op2(gl_info, GL_MUL_ATI, dstreg, GL_NONE, dstmod,
686 arg1, rep1, argmod1,
687 arg2, rep2, argmod2);
688 break;
690 case WINED3D_TOP_ADD_SIGNED_2X:
691 dstmod = GL_2X_BIT_ATI;
692 /* fall through */
693 case WINED3D_TOP_ADD_SIGNED:
694 argmodextra = GL_BIAS_BIT_ATI;
695 /* fall through */
696 case WINED3D_TOP_ADD:
697 dstmod |= GL_SATURATE_BIT_ATI;
698 wrap_op2(gl_info, GL_ADD_ATI, GL_REG_0_ATI, GL_NONE, dstmod,
699 arg1, rep1, argmod1,
700 arg2, rep2, argmodextra | argmod2);
701 break;
703 case WINED3D_TOP_SUBTRACT:
704 dstmod |= GL_SATURATE_BIT_ATI;
705 wrap_op2(gl_info, GL_SUB_ATI, dstreg, GL_NONE, dstmod,
706 arg1, rep1, argmod1,
707 arg2, rep2, argmod2);
708 break;
710 case WINED3D_TOP_ADD_SMOOTH:
711 argmodextra = argmod1 & GL_COMP_BIT_ATI ? argmod1 & ~GL_COMP_BIT_ATI : argmod1 | GL_COMP_BIT_ATI;
712 /* Dst = arg1 + * arg2(1 -arg 1)
713 * = arg2 * (1 - arg1) + arg1
715 wrap_op3(gl_info, GL_MAD_ATI, dstreg, GL_NONE, GL_SATURATE_BIT_ATI,
716 arg2, rep2, argmod2,
717 arg1, rep1, argmodextra,
718 arg1, rep1, argmod1);
719 break;
721 case WINED3D_TOP_BLEND_CURRENT_ALPHA:
722 if (extrarg == GL_NONE)
723 extrarg = register_for_arg(WINED3DTA_CURRENT, gl_info, stage, NULL, NULL, -1);
724 /* fall through */
725 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
726 if (extrarg == GL_NONE)
727 extrarg = register_for_arg(WINED3DTA_TFACTOR, gl_info, stage, NULL, NULL, -1);
728 /* fall through */
729 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
730 if (extrarg == GL_NONE)
731 extrarg = register_for_arg(WINED3DTA_TEXTURE, gl_info, stage, NULL, NULL, -1);
732 /* fall through */
733 case WINED3D_TOP_BLEND_DIFFUSE_ALPHA:
734 if (extrarg == GL_NONE)
735 extrarg = register_for_arg(WINED3DTA_DIFFUSE, gl_info, stage, NULL, NULL, -1);
736 wrap_op3(gl_info, GL_LERP_ATI, dstreg, GL_NONE, GL_NONE,
737 extrarg, GL_ALPHA, GL_NONE,
738 arg1, rep1, argmod1,
739 arg2, rep2, argmod2);
740 break;
742 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
743 arg0 = register_for_arg(WINED3DTA_TEXTURE, gl_info, stage, NULL, NULL, -1);
744 wrap_op3(gl_info, GL_MAD_ATI, dstreg, GL_NONE, GL_NONE,
745 arg2, rep2, argmod2,
746 arg0, GL_ALPHA, GL_COMP_BIT_ATI,
747 arg1, rep1, argmod1);
748 break;
750 /* D3DTOP_PREMODULATE ???? */
752 case WINED3D_TOP_MODULATE_INVALPHA_ADD_COLOR:
753 argmodextra = argmod1 & GL_COMP_BIT_ATI ? argmod1 & ~GL_COMP_BIT_ATI : argmod1 | GL_COMP_BIT_ATI;
754 /* fall through */
755 case WINED3D_TOP_MODULATE_ALPHA_ADD_COLOR:
756 if (!argmodextra)
757 argmodextra = argmod1;
758 wrap_op3(gl_info, GL_MAD_ATI, dstreg, GL_NONE, GL_SATURATE_BIT_ATI,
759 arg2, rep2, argmod2,
760 arg1, GL_ALPHA, argmodextra,
761 arg1, rep1, argmod1);
762 break;
764 case WINED3D_TOP_MODULATE_INVCOLOR_ADD_ALPHA:
765 argmodextra = argmod1 & GL_COMP_BIT_ATI ? argmod1 & ~GL_COMP_BIT_ATI : argmod1 | GL_COMP_BIT_ATI;
766 /* fall through */
767 case WINED3D_TOP_MODULATE_COLOR_ADD_ALPHA:
768 if (!argmodextra)
769 argmodextra = argmod1;
770 wrap_op3(gl_info, GL_MAD_ATI, dstreg, GL_NONE, GL_SATURATE_BIT_ATI,
771 arg2, rep2, argmod2,
772 arg1, rep1, argmodextra,
773 arg1, GL_ALPHA, argmod1);
774 break;
776 case WINED3D_TOP_DOTPRODUCT3:
777 wrap_op2(gl_info, GL_DOT3_ATI, dstreg, GL_NONE, GL_4X_BIT_ATI | GL_SATURATE_BIT_ATI,
778 arg1, rep1, argmod1 | GL_BIAS_BIT_ATI,
779 arg2, rep2, argmod2 | GL_BIAS_BIT_ATI);
780 break;
782 case WINED3D_TOP_MULTIPLY_ADD:
783 wrap_op3(gl_info, GL_MAD_ATI, dstreg, GL_NONE, GL_SATURATE_BIT_ATI,
784 arg1, rep1, argmod1,
785 arg2, rep2, argmod2,
786 arg0, rep0, argmod0);
787 break;
789 case WINED3D_TOP_LERP:
790 wrap_op3(gl_info, GL_LERP_ATI, dstreg, GL_NONE, GL_NONE,
791 arg0, rep0, argmod0,
792 arg1, rep1, argmod1,
793 arg2, rep2, argmod2);
794 break;
796 default: FIXME("Unhandled color operation %d on stage %d\n", op[stage].cop, stage);
799 arg0 = register_for_arg(op[stage].aarg0, gl_info, stage, &argmod0, NULL, tmparg);
800 arg1 = register_for_arg(op[stage].aarg1, gl_info, stage, &argmod1, NULL, tmparg);
801 arg2 = register_for_arg(op[stage].aarg2, gl_info, stage, &argmod2, NULL, tmparg);
802 dstmod = GL_NONE;
803 argmodextra = GL_NONE;
804 extrarg = GL_NONE;
806 switch (op[stage].aop)
808 case WINED3D_TOP_DISABLE:
809 /* Get the primary color to the output if on stage 0, otherwise leave register 0 untouched */
810 if (!stage)
812 wrap_op1(gl_info, GL_MOV_ATI, GL_REG_0_ATI, GL_ALPHA, GL_NONE,
813 GL_PRIMARY_COLOR, GL_NONE, GL_NONE);
815 break;
817 case WINED3D_TOP_SELECT_ARG2:
818 arg1 = arg2;
819 argmod1 = argmod2;
820 /* fall through */
821 case WINED3D_TOP_SELECT_ARG1:
822 wrap_op1(gl_info, GL_MOV_ATI, dstreg, GL_ALPHA, GL_NONE,
823 arg1, GL_NONE, argmod1);
824 break;
826 case WINED3D_TOP_MODULATE_4X:
827 if (dstmod == GL_NONE)
828 dstmod = GL_4X_BIT_ATI;
829 /* fall through */
830 case WINED3D_TOP_MODULATE_2X:
831 if (dstmod == GL_NONE)
832 dstmod = GL_2X_BIT_ATI;
833 dstmod |= GL_SATURATE_BIT_ATI;
834 /* fall through */
835 case WINED3D_TOP_MODULATE:
836 wrap_op2(gl_info, GL_MUL_ATI, dstreg, GL_ALPHA, dstmod,
837 arg1, GL_NONE, argmod1,
838 arg2, GL_NONE, argmod2);
839 break;
841 case WINED3D_TOP_ADD_SIGNED_2X:
842 dstmod = GL_2X_BIT_ATI;
843 /* fall through */
844 case WINED3D_TOP_ADD_SIGNED:
845 argmodextra = GL_BIAS_BIT_ATI;
846 /* fall through */
847 case WINED3D_TOP_ADD:
848 dstmod |= GL_SATURATE_BIT_ATI;
849 wrap_op2(gl_info, GL_ADD_ATI, dstreg, GL_ALPHA, dstmod,
850 arg1, GL_NONE, argmod1,
851 arg2, GL_NONE, argmodextra | argmod2);
852 break;
854 case WINED3D_TOP_SUBTRACT:
855 dstmod |= GL_SATURATE_BIT_ATI;
856 wrap_op2(gl_info, GL_SUB_ATI, dstreg, GL_ALPHA, dstmod,
857 arg1, GL_NONE, argmod1,
858 arg2, GL_NONE, argmod2);
859 break;
861 case WINED3D_TOP_ADD_SMOOTH:
862 argmodextra = argmod1 & GL_COMP_BIT_ATI ? argmod1 & ~GL_COMP_BIT_ATI : argmod1 | GL_COMP_BIT_ATI;
863 /* Dst = arg1 + * arg2(1 -arg 1)
864 * = arg2 * (1 - arg1) + arg1
866 wrap_op3(gl_info, GL_MAD_ATI, dstreg, GL_ALPHA, GL_SATURATE_BIT_ATI,
867 arg2, GL_NONE, argmod2,
868 arg1, GL_NONE, argmodextra,
869 arg1, GL_NONE, argmod1);
870 break;
872 case WINED3D_TOP_BLEND_CURRENT_ALPHA:
873 if (extrarg == GL_NONE)
874 extrarg = register_for_arg(WINED3DTA_CURRENT, gl_info, stage, NULL, NULL, -1);
875 /* fall through */
876 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
877 if (extrarg == GL_NONE)
878 extrarg = register_for_arg(WINED3DTA_TFACTOR, gl_info, stage, NULL, NULL, -1);
879 /* fall through */
880 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
881 if (extrarg == GL_NONE)
882 extrarg = register_for_arg(WINED3DTA_TEXTURE, gl_info, stage, NULL, NULL, -1);
883 /* fall through */
884 case WINED3D_TOP_BLEND_DIFFUSE_ALPHA:
885 if (extrarg == GL_NONE)
886 extrarg = register_for_arg(WINED3DTA_DIFFUSE, gl_info, stage, NULL, NULL, -1);
887 wrap_op3(gl_info, GL_LERP_ATI, dstreg, GL_ALPHA, GL_NONE,
888 extrarg, GL_ALPHA, GL_NONE,
889 arg1, GL_NONE, argmod1,
890 arg2, GL_NONE, argmod2);
891 break;
893 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
894 arg0 = register_for_arg(WINED3DTA_TEXTURE, gl_info, stage, NULL, NULL, -1);
895 wrap_op3(gl_info, GL_MAD_ATI, dstreg, GL_ALPHA, GL_NONE,
896 arg2, GL_NONE, argmod2,
897 arg0, GL_ALPHA, GL_COMP_BIT_ATI,
898 arg1, GL_NONE, argmod1);
899 break;
901 /* D3DTOP_PREMODULATE ???? */
903 case WINED3D_TOP_DOTPRODUCT3:
904 wrap_op2(gl_info, GL_DOT3_ATI, dstreg, GL_ALPHA, GL_4X_BIT_ATI | GL_SATURATE_BIT_ATI,
905 arg1, GL_NONE, argmod1 | GL_BIAS_BIT_ATI,
906 arg2, GL_NONE, argmod2 | GL_BIAS_BIT_ATI);
907 break;
909 case WINED3D_TOP_MULTIPLY_ADD:
910 wrap_op3(gl_info, GL_MAD_ATI, dstreg, GL_ALPHA, GL_SATURATE_BIT_ATI,
911 arg1, GL_NONE, argmod1,
912 arg2, GL_NONE, argmod2,
913 arg0, GL_NONE, argmod0);
914 break;
916 case WINED3D_TOP_LERP:
917 wrap_op3(gl_info, GL_LERP_ATI, dstreg, GL_ALPHA, GL_SATURATE_BIT_ATI,
918 arg1, GL_NONE, argmod1,
919 arg2, GL_NONE, argmod2,
920 arg0, GL_NONE, argmod0);
921 break;
923 case WINED3D_TOP_MODULATE_INVALPHA_ADD_COLOR:
924 case WINED3D_TOP_MODULATE_ALPHA_ADD_COLOR:
925 case WINED3D_TOP_MODULATE_COLOR_ADD_ALPHA:
926 case WINED3D_TOP_MODULATE_INVCOLOR_ADD_ALPHA:
927 case WINED3D_TOP_BUMPENVMAP:
928 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
929 ERR("Application uses an invalid alpha operation\n");
930 break;
932 default: FIXME("Unhandled alpha operation %d on stage %d\n", op[stage].aop, stage);
936 if (tfactor_used && constants[ATIFS_CONST_TFACTOR - GL_CON_0_ATI] != ATIFS_CONSTANT_UNUSED)
937 FIXME("Texture factor constant already used.\n");
938 constants[ATIFS_CONST_TFACTOR - GL_CON_0_ATI] = ATIFS_CONSTANT_TFACTOR;
940 /* Assign unused constants to avoid reloading due to unused <-> bump matrix switches. */
941 for (stage = 0; stage < 8; ++stage)
943 if (constants[stage] == ATIFS_CONSTANT_UNUSED)
944 constants[stage] = ATIFS_CONSTANT_BUMP;
947 TRACE("glEndFragmentShaderATI()\n");
948 GL_EXTCALL(glEndFragmentShaderATI());
949 checkGLcall("GL_EXTCALL(glEndFragmentShaderATI())");
950 return ret;
953 static void atifs_tfactor(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
955 const struct wined3d_gl_info *gl_info = context->gl_info;
956 struct atifs_context_private_data *ctx_priv = context->fragment_pipe_data;
957 struct wined3d_color color;
959 if (!ctx_priv->last_shader
960 || ctx_priv->last_shader->constants[ATIFS_CONST_TFACTOR - GL_CON_0_ATI] != ATIFS_CONSTANT_TFACTOR)
961 return;
963 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_TEXTUREFACTOR]);
964 GL_EXTCALL(glSetFragmentShaderConstantATI(ATIFS_CONST_TFACTOR, &color.r));
965 checkGLcall("glSetFragmentShaderConstantATI(ATIFS_CONST_TFACTOR, &color.r)");
968 static void set_bumpmat(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
970 DWORD stage = (state_id - STATE_TEXTURESTAGE(0, 0)) / (WINED3D_HIGHEST_TEXTURE_STATE + 1);
971 const struct wined3d_gl_info *gl_info = context->gl_info;
972 float mat[2][2];
973 struct atifs_context_private_data *ctx_priv = context->fragment_pipe_data;
975 if (!ctx_priv->last_shader
976 || ctx_priv->last_shader->constants[stage] != ATIFS_CONSTANT_BUMP)
977 return;
979 mat[0][0] = *((float *)&state->texture_states[stage][WINED3D_TSS_BUMPENV_MAT00]);
980 mat[1][0] = *((float *)&state->texture_states[stage][WINED3D_TSS_BUMPENV_MAT01]);
981 mat[0][1] = *((float *)&state->texture_states[stage][WINED3D_TSS_BUMPENV_MAT10]);
982 mat[1][1] = *((float *)&state->texture_states[stage][WINED3D_TSS_BUMPENV_MAT11]);
983 /* GL_ATI_fragment_shader allows only constants from 0.0 to 1.0, but the bumpmat
984 * constants can be in any range. While they should stay between [-1.0 and 1.0] because
985 * Shader Model 1.x pixel shaders are clamped to that range negative values are used occasionally,
986 * for example by our d3d9 test. So to get negative values scale -1;1 to 0;1 and undo that in the
987 * shader(it is free). This might potentially reduce precision. However, if the hardware does
988 * support proper floats it shouldn't, and if it doesn't we can't get anything better anyway. */
989 mat[0][0] = (mat[0][0] + 1.0f) * 0.5f;
990 mat[1][0] = (mat[1][0] + 1.0f) * 0.5f;
991 mat[0][1] = (mat[0][1] + 1.0f) * 0.5f;
992 mat[1][1] = (mat[1][1] + 1.0f) * 0.5f;
993 GL_EXTCALL(glSetFragmentShaderConstantATI(ATIFS_CONST_BUMPMAT(stage), (float *) mat));
994 checkGLcall("glSetFragmentShaderConstantATI(ATIFS_CONST_BUMPMAT(stage), mat)");
997 static void atifs_stage_constant(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
999 DWORD stage = (state_id - STATE_TEXTURESTAGE(0, 0)) / (WINED3D_HIGHEST_TEXTURE_STATE + 1);
1000 const struct wined3d_gl_info *gl_info = context->gl_info;
1001 struct atifs_context_private_data *ctx_priv = context->fragment_pipe_data;
1002 struct wined3d_color color;
1004 if (!ctx_priv->last_shader
1005 || ctx_priv->last_shader->constants[stage] != ATIFS_CONSTANT_STAGE)
1006 return;
1008 wined3d_color_from_d3dcolor(&color, state->texture_states[stage][WINED3D_TSS_CONSTANT]);
1009 GL_EXTCALL(glSetFragmentShaderConstantATI(ATIFS_CONST_STAGE(stage), &color.r));
1010 checkGLcall("glSetFragmentShaderConstantATI(ATIFS_CONST_STAGE(stage), &color.r)");
1013 static void set_tex_op_atifs(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
1015 const struct wined3d_device *device = context->swapchain->device;
1016 const struct wined3d_gl_info *gl_info = context->gl_info;
1017 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
1018 struct atifs_context_private_data *ctx_priv = context->fragment_pipe_data;
1019 const struct atifs_ffp_desc *desc, *last_shader = ctx_priv->last_shader;
1020 struct ffp_frag_settings settings;
1021 struct atifs_private_data *priv = device->fragment_priv;
1022 DWORD mapped_stage;
1023 unsigned int i;
1025 gen_ffp_frag_op(context, state, &settings, TRUE);
1026 desc = (const struct atifs_ffp_desc *)find_ffp_frag_shader(&priv->fragment_shaders, &settings);
1027 if(!desc) {
1028 struct atifs_ffp_desc *new_desc = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*new_desc));
1029 if (!new_desc)
1031 ERR("Out of memory\n");
1032 return;
1034 new_desc->num_textures_used = 0;
1035 for (i = 0; i < d3d_info->limits.ffp_blend_stages; ++i)
1037 if (settings.op[i].cop == WINED3D_TOP_DISABLE)
1038 break;
1039 new_desc->num_textures_used = i + 1;
1042 new_desc->parent.settings = settings;
1043 new_desc->shader = gen_ati_shader(settings.op, gl_info, new_desc->constants);
1044 add_ffp_frag_shader(&priv->fragment_shaders, &new_desc->parent);
1045 TRACE("Allocated fixed function replacement shader descriptor %p\n", new_desc);
1046 desc = new_desc;
1049 /* GL_ATI_fragment_shader depends on the GL_TEXTURE_xD enable settings. Update the texture stages
1050 * used by this shader
1052 for (i = 0; i < desc->num_textures_used; ++i)
1054 mapped_stage = context->tex_unit_map[i];
1055 if (mapped_stage != WINED3D_UNMAPPED_STAGE)
1057 context_active_texture(context, gl_info, mapped_stage);
1058 texture_activate_dimensions(state->textures[i], gl_info);
1062 GL_EXTCALL(glBindFragmentShaderATI(desc->shader));
1063 ctx_priv->last_shader = desc;
1065 for (i = 0; i < 8; i++)
1067 if (last_shader && last_shader->constants[i] == desc->constants[i])
1068 continue;
1070 switch (desc->constants[i])
1072 case ATIFS_CONSTANT_BUMP:
1073 set_bumpmat(context, state, STATE_TEXTURESTAGE(i, WINED3D_TSS_BUMPENV_MAT00));
1074 break;
1076 case ATIFS_CONSTANT_TFACTOR:
1077 atifs_tfactor(context, state, STATE_RENDER(WINED3D_RS_TEXTUREFACTOR));
1078 break;
1080 case ATIFS_CONSTANT_STAGE:
1081 atifs_stage_constant(context, state, STATE_TEXTURESTAGE(i, WINED3D_TSS_CONSTANT));
1082 break;
1084 default:
1085 ERR("Unexpected constant type %u.\n", desc->constants[i]);
1090 static void textransform(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
1092 if (!isStateDirty(context, STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL)))
1093 set_tex_op_atifs(context, state, state_id);
1096 static void atifs_srgbwriteenable(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
1098 if (state->render_states[WINED3D_RS_SRGBWRITEENABLE])
1099 WARN("sRGB writes are not supported by this fragment pipe.\n");
1102 static const struct StateEntryTemplate atifs_fragmentstate_template[] = {
1103 {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), { STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), atifs_tfactor }, WINED3D_GL_EXT_NONE },
1104 {STATE_RENDER(WINED3D_RS_ALPHAFUNC), { STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), NULL }, WINED3D_GL_EXT_NONE },
1105 {STATE_RENDER(WINED3D_RS_ALPHAREF), { STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), NULL }, WINED3D_GL_EXT_NONE },
1106 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), { STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), state_alpha_test }, WINED3D_GL_EXT_NONE },
1107 {STATE_RENDER(WINED3D_RS_COLORKEYENABLE), { STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), NULL }, WINED3D_GL_EXT_NONE },
1108 {STATE_RENDER(WINED3D_RS_FOGCOLOR), { STATE_RENDER(WINED3D_RS_FOGCOLOR), state_fogcolor }, WINED3D_GL_EXT_NONE },
1109 {STATE_RENDER(WINED3D_RS_FOGDENSITY), { STATE_RENDER(WINED3D_RS_FOGDENSITY), state_fogdensity }, WINED3D_GL_EXT_NONE },
1110 {STATE_RENDER(WINED3D_RS_FOGENABLE), { STATE_RENDER(WINED3D_RS_FOGENABLE), state_fog_fragpart }, WINED3D_GL_EXT_NONE },
1111 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), { STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
1112 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), { STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
1113 {STATE_RENDER(WINED3D_RS_FOGSTART), { STATE_RENDER(WINED3D_RS_FOGSTART), state_fogstartend }, WINED3D_GL_EXT_NONE },
1114 {STATE_RENDER(WINED3D_RS_FOGEND), { STATE_RENDER(WINED3D_RS_FOGSTART), NULL }, WINED3D_GL_EXT_NONE },
1115 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), { STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), atifs_srgbwriteenable }, WINED3D_GL_EXT_NONE },
1116 {STATE_COLOR_KEY, { STATE_COLOR_KEY, state_nop }, WINED3D_GL_EXT_NONE },
1117 {STATE_RENDER(WINED3D_RS_SHADEMODE), { STATE_RENDER(WINED3D_RS_SHADEMODE), state_shademode }, WINED3D_GL_EXT_NONE },
1118 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
1119 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG1), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
1120 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG2), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
1121 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG0), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
1122 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_OP), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
1123 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG1), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
1124 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG2), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
1125 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG0), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
1126 {STATE_TEXTURESTAGE(0, WINED3D_TSS_RESULT_ARG), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
1127 {STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_MAT00), { STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_MAT00), set_bumpmat }, WINED3D_GL_EXT_NONE },
1128 {STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_MAT01), { STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
1129 {STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_MAT10), { STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
1130 {STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_MAT11), { STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
1131 {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), { STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), atifs_stage_constant }, WINED3D_GL_EXT_NONE },
1132 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_OP), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
1133 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG1), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
1134 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG2), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
1135 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG0), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
1136 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_OP), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
1137 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG1), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
1138 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG2), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
1139 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG0), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
1140 {STATE_TEXTURESTAGE(1, WINED3D_TSS_RESULT_ARG), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
1141 {STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_MAT00), { STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_MAT00), set_bumpmat }, WINED3D_GL_EXT_NONE },
1142 {STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_MAT01), { STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
1143 {STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_MAT10), { STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
1144 {STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_MAT11), { STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
1145 {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), { STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), atifs_stage_constant }, WINED3D_GL_EXT_NONE },
1146 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_OP), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
1147 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG1), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
1148 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG2), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
1149 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG0), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
1150 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_OP), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
1151 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG1), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
1152 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG2), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
1153 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG0), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
1154 {STATE_TEXTURESTAGE(2, WINED3D_TSS_RESULT_ARG), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
1155 {STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_MAT00), { STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_MAT00), set_bumpmat }, WINED3D_GL_EXT_NONE },
1156 {STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_MAT01), { STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
1157 {STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_MAT10), { STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
1158 {STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_MAT11), { STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
1159 {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), { STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), atifs_stage_constant }, WINED3D_GL_EXT_NONE },
1160 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_OP), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
1161 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG1), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
1162 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG2), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
1163 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG0), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
1164 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_OP), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
1165 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG1), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
1166 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG2), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
1167 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG0), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
1168 {STATE_TEXTURESTAGE(3, WINED3D_TSS_RESULT_ARG), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
1169 {STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_MAT00), { STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_MAT00), set_bumpmat }, WINED3D_GL_EXT_NONE },
1170 {STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_MAT01), { STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
1171 {STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_MAT10), { STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
1172 {STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_MAT11), { STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
1173 {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), { STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), atifs_stage_constant }, WINED3D_GL_EXT_NONE },
1174 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_OP), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
1175 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG1), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
1176 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG2), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
1177 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG0), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
1178 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_OP), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
1179 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG1), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
1180 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG2), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
1181 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG0), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
1182 {STATE_TEXTURESTAGE(4, WINED3D_TSS_RESULT_ARG), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
1183 {STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_MAT00), { STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_MAT00), set_bumpmat }, WINED3D_GL_EXT_NONE },
1184 {STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_MAT01), { STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
1185 {STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_MAT10), { STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
1186 {STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_MAT11), { STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
1187 {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), { STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), atifs_stage_constant }, WINED3D_GL_EXT_NONE },
1188 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_OP), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
1189 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG1), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
1190 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG2), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
1191 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG0), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
1192 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_OP), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
1193 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG1), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
1194 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG2), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
1195 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG0), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
1196 {STATE_TEXTURESTAGE(5, WINED3D_TSS_RESULT_ARG), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
1197 {STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_MAT00), { STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_MAT00), set_bumpmat }, WINED3D_GL_EXT_NONE },
1198 {STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_MAT01), { STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
1199 {STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_MAT10), { STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
1200 {STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_MAT11), { STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
1201 {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), { STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), atifs_stage_constant }, WINED3D_GL_EXT_NONE },
1202 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_OP), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
1203 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG1), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
1204 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG2), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
1205 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG0), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
1206 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_OP), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
1207 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG1), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
1208 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG2), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
1209 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG0), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
1210 {STATE_TEXTURESTAGE(6, WINED3D_TSS_RESULT_ARG), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
1211 {STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_MAT00), { STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_MAT00), set_bumpmat }, WINED3D_GL_EXT_NONE },
1212 {STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_MAT01), { STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
1213 {STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_MAT10), { STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
1214 {STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_MAT11), { STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
1215 {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), { STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), atifs_stage_constant }, WINED3D_GL_EXT_NONE },
1216 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_OP), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
1217 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG1), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
1218 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG2), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
1219 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG0), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
1220 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_OP), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
1221 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG1), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
1222 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG2), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
1223 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG0), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
1224 {STATE_TEXTURESTAGE(7, WINED3D_TSS_RESULT_ARG), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
1225 {STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_MAT00), { STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_MAT00), set_bumpmat }, WINED3D_GL_EXT_NONE },
1226 {STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_MAT01), { STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
1227 {STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_MAT10), { STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
1228 {STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_MAT11), { STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
1229 {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), { STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), atifs_stage_constant }, WINED3D_GL_EXT_NONE },
1230 { STATE_SAMPLER(0), { STATE_SAMPLER(0), sampler_texdim }, WINED3D_GL_EXT_NONE },
1231 { STATE_SAMPLER(1), { STATE_SAMPLER(1), sampler_texdim }, WINED3D_GL_EXT_NONE },
1232 { STATE_SAMPLER(2), { STATE_SAMPLER(2), sampler_texdim }, WINED3D_GL_EXT_NONE },
1233 { STATE_SAMPLER(3), { STATE_SAMPLER(3), sampler_texdim }, WINED3D_GL_EXT_NONE },
1234 { STATE_SAMPLER(4), { STATE_SAMPLER(4), sampler_texdim }, WINED3D_GL_EXT_NONE },
1235 { STATE_SAMPLER(5), { STATE_SAMPLER(5), sampler_texdim }, WINED3D_GL_EXT_NONE },
1236 { STATE_SAMPLER(6), { STATE_SAMPLER(6), sampler_texdim }, WINED3D_GL_EXT_NONE },
1237 { STATE_SAMPLER(7), { STATE_SAMPLER(7), sampler_texdim }, WINED3D_GL_EXT_NONE },
1238 {STATE_TEXTURESTAGE(0,WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), textransform }, WINED3D_GL_EXT_NONE },
1239 {STATE_TEXTURESTAGE(1,WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), textransform }, WINED3D_GL_EXT_NONE },
1240 {STATE_TEXTURESTAGE(2,WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), textransform }, WINED3D_GL_EXT_NONE },
1241 {STATE_TEXTURESTAGE(3,WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), textransform }, WINED3D_GL_EXT_NONE },
1242 {STATE_TEXTURESTAGE(4,WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), textransform }, WINED3D_GL_EXT_NONE },
1243 {STATE_TEXTURESTAGE(5,WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), textransform }, WINED3D_GL_EXT_NONE },
1244 {STATE_TEXTURESTAGE(6,WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), textransform }, WINED3D_GL_EXT_NONE },
1245 {STATE_TEXTURESTAGE(7,WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), textransform }, WINED3D_GL_EXT_NONE },
1246 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), set_tex_op_atifs }, WINED3D_GL_EXT_NONE },
1247 {0 /* Terminate */, { 0, 0 }, WINED3D_GL_EXT_NONE },
1250 /* Context activation is done by the caller. */
1251 static void atifs_enable(const struct wined3d_gl_info *gl_info, BOOL enable)
1253 if (enable)
1255 gl_info->gl_ops.gl.p_glEnable(GL_FRAGMENT_SHADER_ATI);
1256 checkGLcall("glEnable(GL_FRAGMENT_SHADER_ATI)");
1258 else
1260 gl_info->gl_ops.gl.p_glDisable(GL_FRAGMENT_SHADER_ATI);
1261 checkGLcall("glDisable(GL_FRAGMENT_SHADER_ATI)");
1265 static void atifs_get_caps(const struct wined3d_gl_info *gl_info, struct fragment_caps *caps)
1267 caps->wined3d_caps = WINED3D_FRAGMENT_CAP_PROJ_CONTROL;
1268 caps->PrimitiveMiscCaps = WINED3DPMISCCAPS_TSSARGTEMP |
1269 WINED3DPMISCCAPS_PERSTAGECONSTANT;
1270 caps->TextureOpCaps = WINED3DTEXOPCAPS_DISABLE |
1271 WINED3DTEXOPCAPS_SELECTARG1 |
1272 WINED3DTEXOPCAPS_SELECTARG2 |
1273 WINED3DTEXOPCAPS_MODULATE4X |
1274 WINED3DTEXOPCAPS_MODULATE2X |
1275 WINED3DTEXOPCAPS_MODULATE |
1276 WINED3DTEXOPCAPS_ADDSIGNED2X |
1277 WINED3DTEXOPCAPS_ADDSIGNED |
1278 WINED3DTEXOPCAPS_ADD |
1279 WINED3DTEXOPCAPS_SUBTRACT |
1280 WINED3DTEXOPCAPS_ADDSMOOTH |
1281 WINED3DTEXOPCAPS_BLENDCURRENTALPHA |
1282 WINED3DTEXOPCAPS_BLENDFACTORALPHA |
1283 WINED3DTEXOPCAPS_BLENDTEXTUREALPHA |
1284 WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA |
1285 WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM |
1286 WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR |
1287 WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA |
1288 WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA |
1289 WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR |
1290 WINED3DTEXOPCAPS_DOTPRODUCT3 |
1291 WINED3DTEXOPCAPS_MULTIPLYADD |
1292 WINED3DTEXOPCAPS_LERP |
1293 WINED3DTEXOPCAPS_BUMPENVMAP;
1295 /* TODO: Implement WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE
1296 and WINED3DTEXOPCAPS_PREMODULATE */
1298 /* GL_ATI_fragment_shader always supports 6 textures, which was the limit on r200 cards
1299 * which this extension is exclusively focused on(later cards have GL_ARB_fragment_program).
1300 * If the current card has more than 8 fixed function textures in OpenGL's regular fixed
1301 * function pipeline then the ATI_fragment_shader backend imposes a stricter limit. This
1302 * shouldn't be too hard since Nvidia cards have a limit of 4 textures with the default ffp
1303 * pipeline, and almost all games are happy with that. We can however support up to 8
1304 * texture stages because we have a 2nd pass limit of 8 instructions, and per stage we use
1305 * only 1 instruction.
1307 * The proper fix for this is not to use GL_ATI_fragment_shader on cards newer than the
1308 * r200 series and use an ARB or GLSL shader instead
1310 caps->MaxTextureBlendStages = 8;
1311 caps->MaxSimultaneousTextures = 6;
1314 static DWORD atifs_get_emul_mask(const struct wined3d_gl_info *gl_info)
1316 return GL_EXT_EMUL_ARB_MULTITEXTURE | GL_EXT_EMUL_EXT_FOG_COORD;
1319 static void *atifs_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
1321 struct atifs_private_data *priv;
1323 if (!(priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*priv))))
1324 return NULL;
1326 wine_rb_init(&priv->fragment_shaders, wined3d_ffp_frag_program_key_compare);
1327 return priv;
1330 /* Context activation is done by the caller. */
1331 static void atifs_free_ffpshader(struct wine_rb_entry *entry, void *cb_ctx)
1333 const struct wined3d_gl_info *gl_info = cb_ctx;
1334 struct atifs_ffp_desc *entry_ati = WINE_RB_ENTRY_VALUE(entry, struct atifs_ffp_desc, parent.entry);
1336 GL_EXTCALL(glDeleteFragmentShaderATI(entry_ati->shader));
1337 checkGLcall("glDeleteFragmentShaderATI(entry->shader)");
1338 HeapFree(GetProcessHeap(), 0, entry_ati);
1341 /* Context activation is done by the caller. */
1342 static void atifs_free(struct wined3d_device *device)
1344 struct atifs_private_data *priv = device->fragment_priv;
1346 wine_rb_destroy(&priv->fragment_shaders, atifs_free_ffpshader, &device->adapter->gl_info);
1348 HeapFree(GetProcessHeap(), 0, priv);
1349 device->fragment_priv = NULL;
1352 static BOOL atifs_color_fixup_supported(struct color_fixup_desc fixup)
1354 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
1356 TRACE("Checking support for fixup:\n");
1357 dump_color_fixup_desc(fixup);
1360 /* We only support sign fixup of the first two channels. */
1361 if (is_identity_fixup(fixup) || is_same_fixup(fixup, color_fixup_rg)
1362 || is_same_fixup(fixup, color_fixup_rgl) || is_same_fixup(fixup, color_fixup_rgba))
1364 TRACE("[OK]\n");
1365 return TRUE;
1368 TRACE("[FAILED]\n");
1369 return FALSE;
1372 static BOOL atifs_alloc_context_data(struct wined3d_context *context)
1374 struct atifs_context_private_data *priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*priv));
1375 if (!priv)
1376 return FALSE;
1377 context->fragment_pipe_data = priv;
1378 return TRUE;
1381 static void atifs_free_context_data(struct wined3d_context *context)
1383 HeapFree(GetProcessHeap(), 0, context->fragment_pipe_data);
1386 const struct fragment_pipeline atifs_fragment_pipeline = {
1387 atifs_enable,
1388 atifs_get_caps,
1389 atifs_get_emul_mask,
1390 atifs_alloc,
1391 atifs_free,
1392 atifs_alloc_context_data,
1393 atifs_free_context_data,
1394 atifs_color_fixup_supported,
1395 atifs_fragmentstate_template,