wined3d: Add a function to read a destination parameter from SM1-3 bytecode.
[wine] / dlls / wined3d / nvidia_texture_shader.c
1 /*
2  * Fixed function pipeline replacement using GL_NV_register_combiners
3  * and GL_NV_texture_shader
4  *
5  * Copyright 2006 Henri Verbeet
6  * Copyright 2008 Stefan Dösinger(for CodeWeavers)
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22 #include "config.h"
23
24 #include <math.h>
25 #include <stdio.h>
26
27 #include "wined3d_private.h"
28
29 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
30
31 #define GLINFO_LOCATION stateblock->wineD3DDevice->adapter->gl_info
32 static void nvts_activate_dimensions(DWORD stage, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
33     BOOL bumpmap = FALSE;
34
35     if(stage > 0 && (stateblock->textureState[stage - 1][WINED3DTSS_COLOROP] == WINED3DTOP_BUMPENVMAPLUMINANCE ||
36                      stateblock->textureState[stage - 1][WINED3DTSS_COLOROP] == WINED3DTOP_BUMPENVMAP)) {
37         bumpmap = TRUE;
38         context->texShaderBumpMap |= (1 << stage);
39     } else {
40         context->texShaderBumpMap &= ~(1 << stage);
41     }
42
43     if(stateblock->textures[stage]) {
44         switch(IWineD3DBaseTexture_GetTextureDimensions(stateblock->textures[stage])) {
45             case GL_TEXTURE_2D:
46                 glTexEnvi(GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, bumpmap ? GL_OFFSET_TEXTURE_2D_NV : GL_TEXTURE_2D);
47                 checkGLcall("glTexEnvi(GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, ...)");
48                 break;
49             case GL_TEXTURE_RECTANGLE_ARB:
50                 glTexEnvi(GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, bumpmap ? GL_OFFSET_TEXTURE_2D_NV : GL_TEXTURE_RECTANGLE_ARB);
51                 checkGLcall("glTexEnvi(GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, ...)");
52                 break;
53             case GL_TEXTURE_3D:
54                 glTexEnvi(GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_TEXTURE_3D);
55                 checkGLcall("glTexEnvi(GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_TEXTURE_3D)");
56                 break;
57             case GL_TEXTURE_CUBE_MAP_ARB:
58                 glTexEnvi(GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_TEXTURE_CUBE_MAP_ARB);
59                 checkGLcall("glTexEnvi(GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_TEXTURE_CUBE_MAP_ARB)");
60                 break;
61         }
62     } else {
63         glTexEnvi(GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_NONE);
64         checkGLcall("glTexEnvi(GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_NONE)");
65     }
66 }
67
68 typedef struct {
69     GLenum input[3];
70     GLenum mapping[3];
71     GLenum component_usage[3];
72 } tex_op_args;
73
74 static GLenum d3dta_to_combiner_input(DWORD d3dta, DWORD stage, INT texture_idx) {
75     switch (d3dta) {
76         case WINED3DTA_DIFFUSE:
77             return GL_PRIMARY_COLOR_NV;
78
79         case WINED3DTA_CURRENT:
80             if (stage) return GL_SPARE0_NV;
81             else return GL_PRIMARY_COLOR_NV;
82
83         case WINED3DTA_TEXTURE:
84             if (texture_idx > -1) return GL_TEXTURE0_ARB + texture_idx;
85             else return GL_PRIMARY_COLOR_NV;
86
87         case WINED3DTA_TFACTOR:
88             return GL_CONSTANT_COLOR0_NV;
89
90         case WINED3DTA_SPECULAR:
91             return GL_SECONDARY_COLOR_NV;
92
93         case WINED3DTA_TEMP:
94             return GL_SPARE1_NV;
95
96         case WINED3DTA_CONSTANT:
97             /* TODO: Support per stage constants (WINED3DTSS_CONSTANT, NV_register_combiners2) */
98             FIXME("WINED3DTA_CONSTANT, not properly supported.\n");
99             return GL_CONSTANT_COLOR1_NV;
100
101         default:
102             FIXME("Unrecognized texture arg %#x\n", d3dta);
103             return GL_TEXTURE;
104     }
105 }
106
107 static GLenum invert_mapping(GLenum mapping) {
108     if (mapping == GL_UNSIGNED_INVERT_NV) return GL_UNSIGNED_IDENTITY_NV;
109     else if (mapping == GL_UNSIGNED_IDENTITY_NV) return GL_UNSIGNED_INVERT_NV;
110
111     FIXME("Unhandled mapping %#x\n", mapping);
112     return mapping;
113 }
114
115 static void get_src_and_opr_nvrc(DWORD stage, DWORD arg, BOOL is_alpha, GLenum* input, GLenum* mapping, GLenum *component_usage, INT texture_idx) {
116     /* The WINED3DTA_COMPLEMENT flag specifies the complement of the input should
117     * be used. */
118     if (arg & WINED3DTA_COMPLEMENT) *mapping = GL_UNSIGNED_INVERT_NV;
119     else *mapping = GL_UNSIGNED_IDENTITY_NV; /* Clamp all values to positive ranges */
120
121     /* The WINED3DTA_ALPHAREPLICATE flag specifies the alpha component of the input
122     * should be used for all input components. */
123     if (is_alpha || arg & WINED3DTA_ALPHAREPLICATE) *component_usage = GL_ALPHA;
124     else *component_usage = GL_RGB;
125
126     *input = d3dta_to_combiner_input(arg & WINED3DTA_SELECTMASK, stage, texture_idx);
127 }
128
129 void set_tex_op_nvrc(IWineD3DDevice *iface, BOOL is_alpha, int stage, WINED3DTEXTUREOP op, DWORD arg1, DWORD arg2, DWORD arg3, INT texture_idx, DWORD dst) {
130     IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl*)iface;
131     tex_op_args tex_op_args = {{0}, {0}, {0}};
132     GLenum portion = is_alpha ? GL_ALPHA : GL_RGB;
133     GLenum target = GL_COMBINER0_NV + stage;
134     GLenum output;
135     IWineD3DStateBlockImpl *stateblock = This->stateBlock; /* For GLINFO_LOCATION */
136
137     TRACE("stage %d, is_alpha %d, op %s, arg1 %#x, arg2 %#x, arg3 %#x, texture_idx %d\n",
138           stage, is_alpha, debug_d3dtop(op), arg1, arg2, arg3, texture_idx);
139
140     /* If a texture stage references an invalid texture unit the stage just
141     * passes through the result from the previous stage */
142     if (is_invalid_op(This, stage, op, arg1, arg2, arg3)) {
143         arg1 = WINED3DTA_CURRENT;
144         op = WINED3DTOP_SELECTARG1;
145     }
146
147     get_src_and_opr_nvrc(stage, arg1, is_alpha, &tex_op_args.input[0],
148                          &tex_op_args.mapping[0], &tex_op_args.component_usage[0], texture_idx);
149     get_src_and_opr_nvrc(stage, arg2, is_alpha, &tex_op_args.input[1],
150                          &tex_op_args.mapping[1], &tex_op_args.component_usage[1], texture_idx);
151     get_src_and_opr_nvrc(stage, arg3, is_alpha, &tex_op_args.input[2],
152                          &tex_op_args.mapping[2], &tex_op_args.component_usage[2], texture_idx);
153
154
155     if(dst == WINED3DTA_TEMP) {
156         output = GL_SPARE1_NV;
157     } else {
158         output = GL_SPARE0_NV;
159     }
160
161     /* This is called by a state handler which has the gl lock held and a context for the thread */
162     switch(op)
163     {
164         case WINED3DTOP_DISABLE:
165             /* Only for alpha */
166             if (!is_alpha) ERR("Shouldn't be called for WINED3DTSS_COLOROP (WINED3DTOP_DISABLE)\n");
167             /* Input, prev_alpha*1 */
168             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_A_NV,
169                        GL_SPARE0_NV, GL_UNSIGNED_IDENTITY_NV, GL_ALPHA));
170             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_B_NV,
171                        GL_ZERO, GL_UNSIGNED_INVERT_NV, GL_ALPHA));
172
173             /* Output */
174             GL_EXTCALL(glCombinerOutputNV(target, portion, GL_SPARE0_NV, GL_DISCARD_NV,
175                        GL_DISCARD_NV, GL_NONE, GL_NONE, GL_FALSE, GL_FALSE, GL_FALSE));
176             break;
177
178         case WINED3DTOP_SELECTARG1:
179         case WINED3DTOP_SELECTARG2:
180             /* Input, arg*1 */
181             if (op == WINED3DTOP_SELECTARG1) {
182                 GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_A_NV,
183                            tex_op_args.input[0], tex_op_args.mapping[0], tex_op_args.component_usage[0]));
184             } else {
185                 GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_A_NV,
186                            tex_op_args.input[1], tex_op_args.mapping[1], tex_op_args.component_usage[1]));
187             }
188             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_B_NV,
189                        GL_ZERO, GL_UNSIGNED_INVERT_NV, portion));
190
191             /* Output */
192             GL_EXTCALL(glCombinerOutputNV(target, portion, output, GL_DISCARD_NV,
193                        GL_DISCARD_NV, GL_NONE, GL_NONE, GL_FALSE, GL_FALSE, GL_FALSE));
194             break;
195
196         case WINED3DTOP_MODULATE:
197         case WINED3DTOP_MODULATE2X:
198         case WINED3DTOP_MODULATE4X:
199             /* Input, arg1*arg2 */
200             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_A_NV,
201                        tex_op_args.input[0], tex_op_args.mapping[0], tex_op_args.component_usage[0]));
202             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_B_NV,
203                        tex_op_args.input[1], tex_op_args.mapping[1], tex_op_args.component_usage[1]));
204
205             /* Output */
206             if (op == WINED3DTOP_MODULATE) {
207                 GL_EXTCALL(glCombinerOutputNV(target, portion, output, GL_DISCARD_NV,
208                            GL_DISCARD_NV, GL_NONE, GL_NONE, GL_FALSE, GL_FALSE, GL_FALSE));
209             } else if (op == WINED3DTOP_MODULATE2X) {
210                 GL_EXTCALL(glCombinerOutputNV(target, portion, output, GL_DISCARD_NV,
211                            GL_DISCARD_NV, GL_SCALE_BY_TWO_NV, GL_NONE, GL_FALSE, GL_FALSE, GL_FALSE));
212             } else if (op == WINED3DTOP_MODULATE4X) {
213                 GL_EXTCALL(glCombinerOutputNV(target, portion, output, GL_DISCARD_NV,
214                            GL_DISCARD_NV, GL_SCALE_BY_FOUR_NV, GL_NONE, GL_FALSE, GL_FALSE, GL_FALSE));
215             }
216             break;
217
218         case WINED3DTOP_ADD:
219         case WINED3DTOP_ADDSIGNED:
220         case WINED3DTOP_ADDSIGNED2X:
221             /* Input, arg1*1+arg2*1 */
222             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_A_NV,
223                        tex_op_args.input[0], tex_op_args.mapping[0], tex_op_args.component_usage[0]));
224             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_B_NV,
225                        GL_ZERO, GL_UNSIGNED_INVERT_NV, portion));
226             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_C_NV,
227                        tex_op_args.input[1], tex_op_args.mapping[1], tex_op_args.component_usage[1]));
228             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_D_NV,
229                        GL_ZERO, GL_UNSIGNED_INVERT_NV, portion));
230
231             /* Output */
232             if (op == WINED3DTOP_ADD) {
233                 GL_EXTCALL(glCombinerOutputNV(target, portion, GL_DISCARD_NV, GL_DISCARD_NV,
234                            output, GL_NONE, GL_NONE, GL_FALSE, GL_FALSE, GL_FALSE));
235             } else if (op == WINED3DTOP_ADDSIGNED) {
236                 GL_EXTCALL(glCombinerOutputNV(target, portion, GL_DISCARD_NV, GL_DISCARD_NV,
237                            output, GL_NONE, GL_BIAS_BY_NEGATIVE_ONE_HALF_NV, GL_FALSE, GL_FALSE, GL_FALSE));
238             } else if (op == WINED3DTOP_ADDSIGNED2X) {
239                 GL_EXTCALL(glCombinerOutputNV(target, portion, GL_DISCARD_NV, GL_DISCARD_NV,
240                            output, GL_SCALE_BY_TWO_NV, GL_BIAS_BY_NEGATIVE_ONE_HALF_NV, GL_FALSE, GL_FALSE, GL_FALSE));
241             }
242             break;
243
244         case WINED3DTOP_SUBTRACT:
245             /* Input, arg1*1+-arg2*1 */
246             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_A_NV,
247                        tex_op_args.input[0], tex_op_args.mapping[0], tex_op_args.component_usage[0]));
248             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_B_NV,
249                        GL_ZERO, GL_UNSIGNED_INVERT_NV, portion));
250             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_C_NV,
251                        tex_op_args.input[1], GL_SIGNED_NEGATE_NV, tex_op_args.component_usage[1]));
252             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_D_NV,
253                        GL_ZERO, GL_UNSIGNED_INVERT_NV, portion));
254
255             /* Output */
256             GL_EXTCALL(glCombinerOutputNV(target, portion, GL_DISCARD_NV, GL_DISCARD_NV,
257                        output, GL_NONE, GL_NONE, GL_FALSE, GL_FALSE, GL_FALSE));
258             break;
259
260         case WINED3DTOP_ADDSMOOTH:
261             /* Input, arg1*1+(1-arg1)*arg2 */
262             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_A_NV,
263                        tex_op_args.input[0], tex_op_args.mapping[0], tex_op_args.component_usage[0]));
264             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_B_NV,
265                        GL_ZERO, GL_UNSIGNED_INVERT_NV, portion));
266             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_C_NV,
267                        tex_op_args.input[0], invert_mapping(tex_op_args.mapping[0]), tex_op_args.component_usage[0]));
268             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_D_NV,
269                        tex_op_args.input[1], tex_op_args.mapping[1], tex_op_args.component_usage[1]));
270
271             /* Output */
272             GL_EXTCALL(glCombinerOutputNV(target, portion, GL_DISCARD_NV, GL_DISCARD_NV,
273                        output, GL_NONE, GL_NONE, GL_FALSE, GL_FALSE, GL_FALSE));
274             break;
275
276         case WINED3DTOP_BLENDDIFFUSEALPHA:
277         case WINED3DTOP_BLENDTEXTUREALPHA:
278         case WINED3DTOP_BLENDFACTORALPHA:
279         case WINED3DTOP_BLENDTEXTUREALPHAPM:
280         case WINED3DTOP_BLENDCURRENTALPHA:
281         {
282             GLenum alpha_src = GL_PRIMARY_COLOR_NV;
283             if (op == WINED3DTOP_BLENDDIFFUSEALPHA) alpha_src = d3dta_to_combiner_input(WINED3DTA_DIFFUSE, stage, texture_idx);
284             else if (op == WINED3DTOP_BLENDTEXTUREALPHA) alpha_src = d3dta_to_combiner_input(WINED3DTA_TEXTURE, stage, texture_idx);
285             else if (op == WINED3DTOP_BLENDFACTORALPHA) alpha_src = d3dta_to_combiner_input(WINED3DTA_TFACTOR, stage, texture_idx);
286             else if (op == WINED3DTOP_BLENDTEXTUREALPHAPM) alpha_src = d3dta_to_combiner_input(WINED3DTA_TEXTURE, stage, texture_idx);
287             else if (op == WINED3DTOP_BLENDCURRENTALPHA) alpha_src = d3dta_to_combiner_input(WINED3DTA_CURRENT, stage, texture_idx);
288             else FIXME("Unhandled WINED3DTOP %s, shouldn't happen\n", debug_d3dtop(op));
289
290             /* Input, arg1*alpha_src+arg2*(1-alpha_src) */
291             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_A_NV,
292                        tex_op_args.input[0], tex_op_args.mapping[0], tex_op_args.component_usage[0]));
293             if (op == WINED3DTOP_BLENDTEXTUREALPHAPM)
294             {
295                 GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_B_NV,
296                            GL_ZERO, GL_UNSIGNED_INVERT_NV, portion));
297             } else {
298                 GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_B_NV,
299                            alpha_src, GL_UNSIGNED_IDENTITY_NV, GL_ALPHA));
300             }
301             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_C_NV,
302                        tex_op_args.input[1], tex_op_args.mapping[1], tex_op_args.component_usage[1]));
303             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_D_NV,
304                        alpha_src, GL_UNSIGNED_INVERT_NV, GL_ALPHA));
305
306             /* Output */
307             GL_EXTCALL(glCombinerOutputNV(target, portion, GL_DISCARD_NV, GL_DISCARD_NV,
308                        output, GL_NONE, GL_NONE, GL_FALSE, GL_FALSE, GL_FALSE));
309             break;
310         }
311
312         case WINED3DTOP_MODULATEALPHA_ADDCOLOR:
313             /* Input, arg1_alpha*arg2_rgb+arg1_rgb*1 */
314             if (is_alpha) ERR("Only supported for WINED3DTSS_COLOROP (WINED3DTOP_MODULATEALPHA_ADDCOLOR)\n");
315             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_A_NV,
316                        tex_op_args.input[0], tex_op_args.mapping[0], GL_ALPHA));
317             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_B_NV,
318                        tex_op_args.input[1], tex_op_args.mapping[1], tex_op_args.component_usage[1]));
319             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_C_NV,
320                        tex_op_args.input[0], tex_op_args.mapping[0], tex_op_args.component_usage[0]));
321             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_D_NV,
322                        GL_ZERO, GL_UNSIGNED_INVERT_NV, portion));
323
324             /* Output */
325             GL_EXTCALL(glCombinerOutputNV(target, portion, GL_DISCARD_NV, GL_DISCARD_NV,
326                        output, GL_NONE, GL_NONE, GL_FALSE, GL_FALSE, GL_FALSE));
327             break;
328
329         case WINED3DTOP_MODULATECOLOR_ADDALPHA:
330             /* Input, arg1_rgb*arg2_rgb+arg1_alpha*1 */
331             if (is_alpha) ERR("Only supported for WINED3DTSS_COLOROP (WINED3DTOP_MODULATECOLOR_ADDALPHA)\n");
332             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_A_NV,
333                        tex_op_args.input[0], tex_op_args.mapping[0], tex_op_args.component_usage[0]));
334             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_B_NV,
335                        tex_op_args.input[1], tex_op_args.mapping[1], tex_op_args.component_usage[1]));
336             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_C_NV,
337                        tex_op_args.input[0], tex_op_args.mapping[0], GL_ALPHA));
338             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_D_NV,
339                        GL_ZERO, GL_UNSIGNED_INVERT_NV, portion));
340
341             /* Output */
342             GL_EXTCALL(glCombinerOutputNV(target, portion, GL_DISCARD_NV, GL_DISCARD_NV,
343                        output, GL_NONE, GL_NONE, GL_FALSE, GL_FALSE, GL_FALSE));
344             break;
345
346         case WINED3DTOP_MODULATEINVALPHA_ADDCOLOR:
347             /* Input, (1-arg1_alpha)*arg2_rgb+arg1_rgb*1 */
348             if (is_alpha) ERR("Only supported for WINED3DTSS_COLOROP (WINED3DTOP_MODULATEINVALPHA_ADDCOLOR)\n");
349             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_A_NV,
350                        tex_op_args.input[0], invert_mapping(tex_op_args.mapping[0]), GL_ALPHA));
351             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_B_NV,
352                        tex_op_args.input[1], tex_op_args.mapping[1], tex_op_args.component_usage[1]));
353             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_C_NV,
354                        tex_op_args.input[0], tex_op_args.mapping[0], tex_op_args.component_usage[0]));
355             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_D_NV,
356                        GL_ZERO, GL_UNSIGNED_INVERT_NV, portion));
357
358             /* Output */
359             GL_EXTCALL(glCombinerOutputNV(target, portion, GL_DISCARD_NV, GL_DISCARD_NV,
360                        output, GL_NONE, GL_NONE, GL_FALSE, GL_FALSE, GL_FALSE));
361             break;
362
363         case WINED3DTOP_MODULATEINVCOLOR_ADDALPHA:
364             /* Input, (1-arg1_rgb)*arg2_rgb+arg1_alpha*1 */
365             if (is_alpha) ERR("Only supported for WINED3DTSS_COLOROP (WINED3DTOP_MODULATEINVCOLOR_ADDALPHA)\n");
366             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_A_NV,
367                        tex_op_args.input[0], invert_mapping(tex_op_args.mapping[0]), tex_op_args.component_usage[0]));
368             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_B_NV,
369                        tex_op_args.input[1], tex_op_args.mapping[1], tex_op_args.component_usage[1]));
370             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_C_NV,
371                        tex_op_args.input[0], tex_op_args.mapping[0], GL_ALPHA));
372             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_D_NV,
373                        GL_ZERO, GL_UNSIGNED_INVERT_NV, portion));
374
375             /* Output */
376             GL_EXTCALL(glCombinerOutputNV(target, portion, GL_DISCARD_NV, GL_DISCARD_NV,
377                        output, GL_NONE, GL_NONE, GL_FALSE, GL_FALSE, GL_FALSE));
378             break;
379
380         case WINED3DTOP_DOTPRODUCT3:
381             /* Input, arg1 . arg2 */
382             /* FIXME: DX7 uses a different calculation? */
383             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_A_NV,
384                        tex_op_args.input[0], GL_EXPAND_NORMAL_NV, tex_op_args.component_usage[0]));
385             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_B_NV,
386                        tex_op_args.input[1], GL_EXPAND_NORMAL_NV, tex_op_args.component_usage[1]));
387
388             /* Output */
389             GL_EXTCALL(glCombinerOutputNV(target, portion, output, GL_DISCARD_NV,
390                        GL_DISCARD_NV, GL_NONE, GL_NONE, GL_TRUE, GL_FALSE, GL_FALSE));
391             break;
392
393         case WINED3DTOP_MULTIPLYADD:
394             /* Input, arg3*1+arg1*arg2 */
395             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_A_NV,
396                        tex_op_args.input[2], tex_op_args.mapping[2], tex_op_args.component_usage[2]));
397             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_B_NV,
398                        GL_ZERO, GL_UNSIGNED_INVERT_NV, portion));
399             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_C_NV,
400                        tex_op_args.input[0], tex_op_args.mapping[0], tex_op_args.component_usage[0]));
401             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_D_NV,
402                        tex_op_args.input[1], tex_op_args.mapping[1], tex_op_args.component_usage[1]));
403
404             /* Output */
405             GL_EXTCALL(glCombinerOutputNV(target, portion, GL_DISCARD_NV, GL_DISCARD_NV,
406                        output, GL_NONE, GL_NONE, GL_FALSE, GL_FALSE, GL_FALSE));
407             break;
408
409         case WINED3DTOP_LERP:
410             /* Input, arg3*arg1+(1-arg3)*arg2 */
411             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_A_NV,
412                        tex_op_args.input[2], tex_op_args.mapping[2], tex_op_args.component_usage[2]));
413             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_B_NV,
414                        tex_op_args.input[0], tex_op_args.mapping[0], tex_op_args.component_usage[0]));
415             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_C_NV,
416                        tex_op_args.input[2], invert_mapping(tex_op_args.mapping[2]), tex_op_args.component_usage[2]));
417             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_D_NV,
418                        tex_op_args.input[1], tex_op_args.mapping[1], tex_op_args.component_usage[1]));
419
420             /* Output */
421             GL_EXTCALL(glCombinerOutputNV(target, portion, GL_DISCARD_NV, GL_DISCARD_NV,
422                        output, GL_NONE, GL_NONE, GL_FALSE, GL_FALSE, GL_FALSE));
423             break;
424
425         case WINED3DTOP_BUMPENVMAPLUMINANCE:
426         case WINED3DTOP_BUMPENVMAP:
427             if(GL_SUPPORT(NV_TEXTURE_SHADER)) {
428                 /* The bump map stage itself isn't exciting, just read the texture. But tell the next stage to
429                  * perform bump mapping and source from the current stage. Pretty much a SELECTARG2.
430                  * ARG2 is passed through unmodified(apps will most likely use D3DTA_CURRENT for arg2, arg1
431                  * (which will most likely be D3DTA_TEXTURE) is available as a texture shader input for the next stage
432                  */
433                 GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_A_NV,
434                            tex_op_args.input[1], tex_op_args.mapping[1], tex_op_args.component_usage[1]));
435                 GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_B_NV,
436                            GL_ZERO, GL_UNSIGNED_INVERT_NV, portion));
437                 /* Always pass through to CURRENT, ignore temp arg */
438                 GL_EXTCALL(glCombinerOutputNV(target, portion, GL_SPARE0_NV, GL_DISCARD_NV,
439                            GL_DISCARD_NV, GL_NONE, GL_NONE, GL_FALSE, GL_FALSE, GL_FALSE));
440                 break;
441             }
442
443         default:
444             FIXME("Unhandled WINED3DTOP: stage %d, is_alpha %d, op %s (%#x), arg1 %#x, arg2 %#x, arg3 %#x, texture_idx %d\n",
445                   stage, is_alpha, debug_d3dtop(op), op, arg1, arg2, arg3, texture_idx);
446     }
447
448     checkGLcall("set_tex_op_nvrc()\n");
449
450 }
451
452
453 static void nvrc_colorop(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
454     DWORD stage = (state - STATE_TEXTURESTAGE(0, 0)) / (WINED3D_HIGHEST_TEXTURE_STATE + 1);
455     DWORD mapped_stage = stateblock->wineD3DDevice->texUnitMap[stage];
456     BOOL tex_used = stateblock->wineD3DDevice->fixed_function_usage_map & (1 << stage);
457
458     TRACE("Setting color op for stage %d\n", stage);
459
460     /* Using a pixel shader? Don't care for anything here, the shader applying does it */
461     if (use_ps(stateblock)) return;
462
463     if (stage != mapped_stage) WARN("Using non 1:1 mapping: %d -> %d!\n", stage, mapped_stage);
464
465     if (mapped_stage != WINED3D_UNMAPPED_STAGE)
466     {
467         if (tex_used && mapped_stage >= GL_LIMITS(textures)) {
468             FIXME("Attempt to enable unsupported stage!\n");
469             return;
470         }
471         GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB + mapped_stage));
472         checkGLcall("glActiveTextureARB");
473     }
474
475     if(stateblock->lowest_disabled_stage > 0) {
476         glEnable(GL_REGISTER_COMBINERS_NV);
477         GL_EXTCALL(glCombinerParameteriNV(GL_NUM_GENERAL_COMBINERS_NV, stateblock->lowest_disabled_stage));
478     } else {
479         glDisable(GL_REGISTER_COMBINERS_NV);
480     }
481     if(stage >= stateblock->lowest_disabled_stage) {
482         TRACE("Stage disabled\n");
483         if (mapped_stage != WINED3D_UNMAPPED_STAGE)
484         {
485             /* Disable everything here */
486             glDisable(GL_TEXTURE_2D);
487             checkGLcall("glDisable(GL_TEXTURE_2D)");
488             glDisable(GL_TEXTURE_3D);
489             checkGLcall("glDisable(GL_TEXTURE_3D)");
490             if(GL_SUPPORT(ARB_TEXTURE_CUBE_MAP)) {
491                 glDisable(GL_TEXTURE_CUBE_MAP_ARB);
492                 checkGLcall("glDisable(GL_TEXTURE_CUBE_MAP_ARB)");
493             }
494             if(GL_SUPPORT(ARB_TEXTURE_RECTANGLE)) {
495                 glDisable(GL_TEXTURE_RECTANGLE_ARB);
496                 checkGLcall("glDisable(GL_TEXTURE_RECTANGLE_ARB)");
497             }
498             if(GL_SUPPORT(NV_TEXTURE_SHADER2) && mapped_stage < GL_LIMITS(textures)) {
499                 glTexEnvi(GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_NONE);
500             }
501         }
502         /* All done */
503         return;
504     }
505
506     /* The sampler will also activate the correct texture dimensions, so no need to do it here
507      * if the sampler for this stage is dirty
508      */
509     if(!isStateDirty(context, STATE_SAMPLER(stage))) {
510         if (tex_used) {
511             if(GL_SUPPORT(NV_TEXTURE_SHADER2)) {
512                 nvts_activate_dimensions(stage, stateblock, context);
513             } else {
514                 texture_activate_dimensions(stage, stateblock, context);
515             }
516         }
517     }
518
519     /* Set the texture combiners */
520     set_tex_op_nvrc((IWineD3DDevice *)stateblock->wineD3DDevice, FALSE, stage,
521                         stateblock->textureState[stage][WINED3DTSS_COLOROP],
522                         stateblock->textureState[stage][WINED3DTSS_COLORARG1],
523                         stateblock->textureState[stage][WINED3DTSS_COLORARG2],
524                         stateblock->textureState[stage][WINED3DTSS_COLORARG0],
525                         mapped_stage,
526                         stateblock->textureState[stage][WINED3DTSS_RESULTARG]);
527
528     /* In register combiners bump mapping is done in the stage AFTER the one that has the bump map operation set,
529      * thus the texture shader may have to be updated
530      */
531     if(GL_SUPPORT(NV_TEXTURE_SHADER2)) {
532         BOOL usesBump = (stateblock->textureState[stage][WINED3DTSS_COLOROP] == WINED3DTOP_BUMPENVMAPLUMINANCE ||
533                             stateblock->textureState[stage][WINED3DTSS_COLOROP] == WINED3DTOP_BUMPENVMAP) ? TRUE : FALSE;
534         BOOL usedBump = (context->texShaderBumpMap & 1 << (stage + 1)) ? TRUE : FALSE;
535         if(usesBump != usedBump) {
536             GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB + mapped_stage + 1));
537             checkGLcall("glActiveTextureARB");
538             nvts_activate_dimensions(stage + 1, stateblock, context);
539             GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB + mapped_stage));
540             checkGLcall("glActiveTextureARB");
541         }
542     }
543 }
544
545 static void nvts_texdim(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
546     DWORD sampler = state - STATE_SAMPLER(0);
547     DWORD mapped_stage = stateblock->wineD3DDevice->texUnitMap[sampler];
548
549     /* No need to enable / disable anything here for unused samplers. The tex_colorop
550     * handler takes care. Also no action is needed with pixel shaders, or if tex_colorop
551     * will take care of this business
552     */
553     if(mapped_stage == WINED3D_UNMAPPED_STAGE || mapped_stage >= GL_LIMITS(textures)) return;
554     if(sampler >= stateblock->lowest_disabled_stage) return;
555     if(isStateDirty(context, STATE_TEXTURESTAGE(sampler, WINED3DTSS_COLOROP))) return;
556
557     nvts_activate_dimensions(sampler, stateblock, context);
558 }
559
560 static void nvts_bumpenvmat(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
561     DWORD stage = (state - STATE_TEXTURESTAGE(0, 0)) / (WINED3D_HIGHEST_TEXTURE_STATE + 1);
562     DWORD mapped_stage = stateblock->wineD3DDevice->texUnitMap[stage + 1];
563     float mat[2][2];
564
565     /* Direct3D sets the matrix in the stage reading the perturbation map. The result is used to
566      * offset the destination stage(always stage + 1 in d3d). In GL_NV_texture_shader, the bump
567      * map offsetting is done in the stage reading the bump mapped texture, and the perturbation
568      * map is read from a specified source stage(always stage - 1 for d3d). Thus set the matrix
569      * for stage + 1. Keep the nvrc tex unit mapping in mind too
570      */
571     if(mapped_stage < GL_LIMITS(textures)) {
572         GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB + mapped_stage));
573         checkGLcall("GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB + mapped_stage))");
574
575         /* We can't just pass a pointer to the stateblock to GL due to the different matrix
576          * format(column major vs row major)
577          */
578         mat[0][0] = *((float *) &stateblock->textureState[stage][WINED3DTSS_BUMPENVMAT00]);
579         mat[1][0] = *((float *) &stateblock->textureState[stage][WINED3DTSS_BUMPENVMAT01]);
580         mat[0][1] = *((float *) &stateblock->textureState[stage][WINED3DTSS_BUMPENVMAT10]);
581         mat[1][1] = *((float *) &stateblock->textureState[stage][WINED3DTSS_BUMPENVMAT11]);
582         glTexEnvfv(GL_TEXTURE_SHADER_NV, GL_OFFSET_TEXTURE_MATRIX_NV, (float *) mat);
583         checkGLcall("glTexEnvfv(GL_TEXTURE_SHADER_NV, GL_OFFSET_TEXTURE_MATRIX_NV, mat)");
584     }
585 }
586
587 static void nvrc_texfactor(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
588     float col[4];
589     D3DCOLORTOGLFLOAT4(stateblock->renderState[WINED3DRS_TEXTUREFACTOR], col);
590     GL_EXTCALL(glCombinerParameterfvNV(GL_CONSTANT_COLOR0_NV, &col[0]));
591 }
592 #undef GLINFO_LOCATION
593
594 #define GLINFO_LOCATION (*gl_info)
595 static void nvrc_enable(IWineD3DDevice *iface, BOOL enable) { }
596
597 static void nvts_enable(IWineD3DDevice *iface, BOOL enable) {
598     if(enable) {
599         glEnable(GL_TEXTURE_SHADER_NV);
600         checkGLcall("glEnable(GL_TEXTURE_SHADER_NV)");
601     } else {
602         glDisable(GL_TEXTURE_SHADER_NV);
603         checkGLcall("glDisable(GL_TEXTURE_SHADER_NV)");
604     }
605 }
606
607 static void nvrc_fragment_get_caps(WINED3DDEVTYPE devtype, const WineD3D_GL_Info *gl_info, struct fragment_caps *pCaps)
608 {
609     pCaps->TextureOpCaps =  WINED3DTEXOPCAPS_ADD                        |
610                             WINED3DTEXOPCAPS_ADDSIGNED                  |
611                             WINED3DTEXOPCAPS_ADDSIGNED2X                |
612                             WINED3DTEXOPCAPS_MODULATE                   |
613                             WINED3DTEXOPCAPS_MODULATE2X                 |
614                             WINED3DTEXOPCAPS_MODULATE4X                 |
615                             WINED3DTEXOPCAPS_SELECTARG1                 |
616                             WINED3DTEXOPCAPS_SELECTARG2                 |
617                             WINED3DTEXOPCAPS_DISABLE                    |
618                             WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA          |
619                             WINED3DTEXOPCAPS_BLENDTEXTUREALPHA          |
620                             WINED3DTEXOPCAPS_BLENDFACTORALPHA           |
621                             WINED3DTEXOPCAPS_BLENDCURRENTALPHA          |
622                             WINED3DTEXOPCAPS_LERP                       |
623                             WINED3DTEXOPCAPS_SUBTRACT                   |
624                             WINED3DTEXOPCAPS_ADDSMOOTH                  |
625                             WINED3DTEXOPCAPS_MULTIPLYADD                |
626                             WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR     |
627                             WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA     |
628                             WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM        |
629                             WINED3DTEXOPCAPS_DOTPRODUCT3                |
630                             WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR  |
631                             WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA;
632
633     if(GL_SUPPORT(NV_TEXTURE_SHADER2)) {
634         /* Bump mapping is supported already in NV_TEXTURE_SHADER, but that extension does
635          * not support 3D textures. This asks for trouble if an app uses both bump mapping
636          * and 3D textures. It also allows us to keep the code simpler by having texture
637          * shaders constantly enabled.
638          */
639         pCaps->TextureOpCaps |= WINED3DTEXOPCAPS_BUMPENVMAP;
640         /* TODO: Luminance bump map? */
641     }
642
643 #if 0
644     /* FIXME: Add
645             pCaps->TextureOpCaps |= WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE
646             WINED3DTEXOPCAPS_PREMODULATE */
647 #endif
648
649     pCaps->MaxTextureBlendStages   = GL_LIMITS(texture_stages);
650     pCaps->MaxSimultaneousTextures = GL_LIMITS(textures);
651
652     pCaps->PrimitiveMiscCaps |=  WINED3DPMISCCAPS_TSSARGTEMP;
653
654     /* The caps below can be supported but aren't handled yet in utils.c 'd3dta_to_combiner_input', disable them until support is fixed */
655 #if 0
656     if (GL_SUPPORT(NV_REGISTER_COMBINERS2))
657     pCaps->PrimitiveMiscCaps |=  WINED3DPMISCCAPS_PERSTAGECONSTANT;
658 #endif
659 }
660
661 static HRESULT nvrc_fragment_alloc(IWineD3DDevice *iface) { return WINED3D_OK; }
662 static void nvrc_fragment_free(IWineD3DDevice *iface) {}
663
664 /* Two fixed function pipeline implementations using GL_NV_register_combiners and
665  * GL_NV_texture_shader. The nvts_fragment_pipeline assumes that both extensions
666  * are available(geforce 3 and newer), while nvrc_fragment_pipeline uses only the
667  * register combiners extension(Pre-GF3).
668  */
669
670 static BOOL nvts_color_fixup_supported(struct color_fixup_desc fixup)
671 {
672     if (TRACE_ON(d3d))
673     {
674         TRACE("Checking support for fixup:\n");
675         dump_color_fixup_desc(fixup);
676     }
677
678     /* We only support identity conversions. */
679     if (is_identity_fixup(fixup))
680     {
681         TRACE("[OK]\n");
682         return TRUE;
683     }
684
685     TRACE("[FAILED]\n");
686     return FALSE;
687 }
688
689 static const struct StateEntryTemplate nvrc_fragmentstate_template[] = {
690     { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          nvrc_colorop        }, 0                               },
691     { STATE_TEXTURESTAGE(0, WINED3DTSS_COLORARG1),        { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          nvrc_colorop        }, 0                               },
692     { STATE_TEXTURESTAGE(0, WINED3DTSS_COLORARG2),        { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          nvrc_colorop        }, 0                               },
693     { STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAOP),          { STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
694     { STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAARG1),        { STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
695     { STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAARG2),        { STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
696     { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00),     { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00),     nvts_bumpenvmat     }, NV_TEXTURE_SHADER2              },
697     { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT01),     { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00),     nvts_bumpenvmat     }, NV_TEXTURE_SHADER2              },
698     { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT10),     { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00),     nvts_bumpenvmat     }, NV_TEXTURE_SHADER2              },
699     { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT11),     { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00),     nvts_bumpenvmat     }, NV_TEXTURE_SHADER2              },
700     { STATE_TEXTURESTAGE(0, WINED3DTSS_COLORARG0),        { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          nvrc_colorop        }, 0                               },
701     { STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAARG0),        { STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
702     { STATE_TEXTURESTAGE(0, WINED3DTSS_RESULTARG),        { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          nvrc_colorop        }, 0                               },
703     { STATE_TEXTURESTAGE(1, WINED3DTSS_COLOROP),          { STATE_TEXTURESTAGE(1, WINED3DTSS_COLOROP),          nvrc_colorop        }, 0                               },
704     { STATE_TEXTURESTAGE(1, WINED3DTSS_COLORARG1),        { STATE_TEXTURESTAGE(1, WINED3DTSS_COLOROP),          nvrc_colorop        }, 0                               },
705     { STATE_TEXTURESTAGE(1, WINED3DTSS_COLORARG2),        { STATE_TEXTURESTAGE(1, WINED3DTSS_COLOROP),          nvrc_colorop        }, 0                               },
706     { STATE_TEXTURESTAGE(1, WINED3DTSS_ALPHAOP),          { STATE_TEXTURESTAGE(1, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
707     { STATE_TEXTURESTAGE(1, WINED3DTSS_ALPHAARG1),        { STATE_TEXTURESTAGE(1, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
708     { STATE_TEXTURESTAGE(1, WINED3DTSS_ALPHAARG2),        { STATE_TEXTURESTAGE(1, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
709     { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00),     { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00),     nvts_bumpenvmat     }, NV_TEXTURE_SHADER2              },
710     { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT01),     { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00),     nvts_bumpenvmat     }, NV_TEXTURE_SHADER2              },
711     { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT10),     { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00),     nvts_bumpenvmat     }, NV_TEXTURE_SHADER2              },
712     { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT11),     { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00),     nvts_bumpenvmat     }, NV_TEXTURE_SHADER2              },
713     { STATE_TEXTURESTAGE(1, WINED3DTSS_COLORARG0),        { STATE_TEXTURESTAGE(1, WINED3DTSS_COLOROP),          nvrc_colorop        }, 0                               },
714     { STATE_TEXTURESTAGE(1, WINED3DTSS_ALPHAARG0),        { STATE_TEXTURESTAGE(1, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
715     { STATE_TEXTURESTAGE(1, WINED3DTSS_RESULTARG),        { STATE_TEXTURESTAGE(1, WINED3DTSS_COLOROP),          nvrc_colorop        }, 0                               },
716     { STATE_TEXTURESTAGE(2, WINED3DTSS_COLOROP),          { STATE_TEXTURESTAGE(2, WINED3DTSS_COLOROP),          nvrc_colorop        }, 0                               },
717     { STATE_TEXTURESTAGE(2, WINED3DTSS_COLORARG1),        { STATE_TEXTURESTAGE(2, WINED3DTSS_COLOROP),          nvrc_colorop        }, 0                               },
718     { STATE_TEXTURESTAGE(2, WINED3DTSS_COLORARG2),        { STATE_TEXTURESTAGE(2, WINED3DTSS_COLOROP),          nvrc_colorop        }, 0                               },
719     { STATE_TEXTURESTAGE(2, WINED3DTSS_ALPHAOP),          { STATE_TEXTURESTAGE(2, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
720     { STATE_TEXTURESTAGE(2, WINED3DTSS_ALPHAARG1),        { STATE_TEXTURESTAGE(2, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
721     { STATE_TEXTURESTAGE(2, WINED3DTSS_ALPHAARG2),        { STATE_TEXTURESTAGE(2, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
722     { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00),     { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00),     nvts_bumpenvmat     }, NV_TEXTURE_SHADER2              },
723     { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT01),     { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00),     nvts_bumpenvmat     }, NV_TEXTURE_SHADER2              },
724     { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT10),     { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00),     nvts_bumpenvmat     }, NV_TEXTURE_SHADER2              },
725     { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT11),     { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00),     nvts_bumpenvmat     }, NV_TEXTURE_SHADER2              },
726     { STATE_TEXTURESTAGE(2, WINED3DTSS_COLORARG0),        { STATE_TEXTURESTAGE(2, WINED3DTSS_COLOROP),          nvrc_colorop        }, 0                               },
727     { STATE_TEXTURESTAGE(2, WINED3DTSS_ALPHAARG0),        { STATE_TEXTURESTAGE(2, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
728     { STATE_TEXTURESTAGE(2, WINED3DTSS_RESULTARG),        { STATE_TEXTURESTAGE(2, WINED3DTSS_COLOROP),          nvrc_colorop        }, 0                               },
729     { STATE_TEXTURESTAGE(3, WINED3DTSS_COLOROP),          { STATE_TEXTURESTAGE(3, WINED3DTSS_COLOROP),          nvrc_colorop        }, 0                               },
730     { STATE_TEXTURESTAGE(3, WINED3DTSS_COLORARG1),        { STATE_TEXTURESTAGE(3, WINED3DTSS_COLOROP),          nvrc_colorop        }, 0                               },
731     { STATE_TEXTURESTAGE(3, WINED3DTSS_COLORARG2),        { STATE_TEXTURESTAGE(3, WINED3DTSS_COLOROP),          nvrc_colorop        }, 0                               },
732     { STATE_TEXTURESTAGE(3, WINED3DTSS_ALPHAOP),          { STATE_TEXTURESTAGE(3, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
733     { STATE_TEXTURESTAGE(3, WINED3DTSS_ALPHAARG1),        { STATE_TEXTURESTAGE(3, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
734     { STATE_TEXTURESTAGE(3, WINED3DTSS_ALPHAARG2),        { STATE_TEXTURESTAGE(3, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
735     { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00),     { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00),     nvts_bumpenvmat     }, NV_TEXTURE_SHADER2              },
736     { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT01),     { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00),     nvts_bumpenvmat     }, NV_TEXTURE_SHADER2              },
737     { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT10),     { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00),     nvts_bumpenvmat     }, NV_TEXTURE_SHADER2              },
738     { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT11),     { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00),     nvts_bumpenvmat     }, NV_TEXTURE_SHADER2              },
739     { STATE_TEXTURESTAGE(3, WINED3DTSS_COLORARG0),        { STATE_TEXTURESTAGE(3, WINED3DTSS_COLOROP),          nvrc_colorop        }, 0                               },
740     { STATE_TEXTURESTAGE(3, WINED3DTSS_ALPHAARG0),        { STATE_TEXTURESTAGE(3, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
741     { STATE_TEXTURESTAGE(3, WINED3DTSS_RESULTARG),        { STATE_TEXTURESTAGE(3, WINED3DTSS_COLOROP),          nvrc_colorop        }, 0                               },
742     { STATE_TEXTURESTAGE(4, WINED3DTSS_COLOROP),          { STATE_TEXTURESTAGE(4, WINED3DTSS_COLOROP),          nvrc_colorop        }, 0                               },
743     { STATE_TEXTURESTAGE(4, WINED3DTSS_COLORARG1),        { STATE_TEXTURESTAGE(4, WINED3DTSS_COLOROP),          nvrc_colorop        }, 0                               },
744     { STATE_TEXTURESTAGE(4, WINED3DTSS_COLORARG2),        { STATE_TEXTURESTAGE(4, WINED3DTSS_COLOROP),          nvrc_colorop        }, 0                               },
745     { STATE_TEXTURESTAGE(4, WINED3DTSS_ALPHAOP),          { STATE_TEXTURESTAGE(4, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
746     { STATE_TEXTURESTAGE(4, WINED3DTSS_ALPHAARG1),        { STATE_TEXTURESTAGE(4, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
747     { STATE_TEXTURESTAGE(4, WINED3DTSS_ALPHAARG2),        { STATE_TEXTURESTAGE(4, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
748     { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00),     { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00),     nvts_bumpenvmat     }, NV_TEXTURE_SHADER2              },
749     { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT01),     { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00),     nvts_bumpenvmat     }, NV_TEXTURE_SHADER2              },
750     { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT10),     { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00),     nvts_bumpenvmat     }, NV_TEXTURE_SHADER2              },
751     { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT11),     { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00),     nvts_bumpenvmat     }, NV_TEXTURE_SHADER2              },
752     { STATE_TEXTURESTAGE(4, WINED3DTSS_COLORARG0),        { STATE_TEXTURESTAGE(4, WINED3DTSS_COLOROP),          nvrc_colorop        }, 0                               },
753     { STATE_TEXTURESTAGE(4, WINED3DTSS_ALPHAARG0),        { STATE_TEXTURESTAGE(4, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
754     { STATE_TEXTURESTAGE(4, WINED3DTSS_RESULTARG),        { STATE_TEXTURESTAGE(4, WINED3DTSS_COLOROP),          nvrc_colorop        }, 0                               },
755     { STATE_TEXTURESTAGE(5, WINED3DTSS_COLOROP),          { STATE_TEXTURESTAGE(5, WINED3DTSS_COLOROP),          nvrc_colorop        }, 0                               },
756     { STATE_TEXTURESTAGE(5, WINED3DTSS_COLORARG1),        { STATE_TEXTURESTAGE(5, WINED3DTSS_COLOROP),          nvrc_colorop        }, 0                               },
757     { STATE_TEXTURESTAGE(5, WINED3DTSS_COLORARG2),        { STATE_TEXTURESTAGE(5, WINED3DTSS_COLOROP),          nvrc_colorop        }, 0                               },
758     { STATE_TEXTURESTAGE(5, WINED3DTSS_ALPHAOP),          { STATE_TEXTURESTAGE(5, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
759     { STATE_TEXTURESTAGE(5, WINED3DTSS_ALPHAARG1),        { STATE_TEXTURESTAGE(5, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
760     { STATE_TEXTURESTAGE(5, WINED3DTSS_ALPHAARG2),        { STATE_TEXTURESTAGE(5, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
761     { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00),     { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00),     nvts_bumpenvmat     }, NV_TEXTURE_SHADER2              },
762     { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT01),     { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00),     nvts_bumpenvmat     }, NV_TEXTURE_SHADER2              },
763     { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT10),     { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00),     nvts_bumpenvmat     }, NV_TEXTURE_SHADER2              },
764     { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT11),     { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00),     nvts_bumpenvmat     }, NV_TEXTURE_SHADER2              },
765     { STATE_TEXTURESTAGE(5, WINED3DTSS_COLORARG0),        { STATE_TEXTURESTAGE(5, WINED3DTSS_COLOROP),          nvrc_colorop        }, 0                               },
766     { STATE_TEXTURESTAGE(5, WINED3DTSS_ALPHAARG0),        { STATE_TEXTURESTAGE(5, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
767     { STATE_TEXTURESTAGE(5, WINED3DTSS_RESULTARG),        { STATE_TEXTURESTAGE(5, WINED3DTSS_COLOROP),          nvrc_colorop        }, 0                               },
768     { STATE_TEXTURESTAGE(6, WINED3DTSS_COLOROP),          { STATE_TEXTURESTAGE(6, WINED3DTSS_COLOROP),          nvrc_colorop        }, 0                               },
769     { STATE_TEXTURESTAGE(6, WINED3DTSS_COLORARG1),        { STATE_TEXTURESTAGE(6, WINED3DTSS_COLOROP),          nvrc_colorop        }, 0                               },
770     { STATE_TEXTURESTAGE(6, WINED3DTSS_COLORARG2),        { STATE_TEXTURESTAGE(6, WINED3DTSS_COLOROP),          nvrc_colorop        }, 0                               },
771     { STATE_TEXTURESTAGE(6, WINED3DTSS_ALPHAOP),          { STATE_TEXTURESTAGE(6, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
772     { STATE_TEXTURESTAGE(6, WINED3DTSS_ALPHAARG1),        { STATE_TEXTURESTAGE(6, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
773     { STATE_TEXTURESTAGE(6, WINED3DTSS_ALPHAARG2),        { STATE_TEXTURESTAGE(6, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
774     { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00),     { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00),     nvts_bumpenvmat     }, NV_TEXTURE_SHADER2              },
775     { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT01),     { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00),     nvts_bumpenvmat     }, NV_TEXTURE_SHADER2              },
776     { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT10),     { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00),     nvts_bumpenvmat     }, NV_TEXTURE_SHADER2              },
777     { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT11),     { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00),     nvts_bumpenvmat     }, NV_TEXTURE_SHADER2              },
778     { STATE_TEXTURESTAGE(6, WINED3DTSS_COLORARG0),        { STATE_TEXTURESTAGE(6, WINED3DTSS_COLOROP),          nvrc_colorop        }, 0                               },
779     { STATE_TEXTURESTAGE(6, WINED3DTSS_ALPHAARG0),        { STATE_TEXTURESTAGE(6, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
780     { STATE_TEXTURESTAGE(6, WINED3DTSS_RESULTARG),        { STATE_TEXTURESTAGE(6, WINED3DTSS_COLOROP),          nvrc_colorop        }, 0                               },
781     { STATE_TEXTURESTAGE(7, WINED3DTSS_COLOROP),          { STATE_TEXTURESTAGE(7, WINED3DTSS_COLOROP),          nvrc_colorop        }, 0                               },
782     { STATE_TEXTURESTAGE(7, WINED3DTSS_COLORARG1),        { STATE_TEXTURESTAGE(7, WINED3DTSS_COLOROP),          nvrc_colorop        }, 0                               },
783     { STATE_TEXTURESTAGE(7, WINED3DTSS_COLORARG2),        { STATE_TEXTURESTAGE(7, WINED3DTSS_COLOROP),          nvrc_colorop        }, 0                               },
784     { STATE_TEXTURESTAGE(7, WINED3DTSS_ALPHAOP),          { STATE_TEXTURESTAGE(7, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
785     { STATE_TEXTURESTAGE(7, WINED3DTSS_ALPHAARG1),        { STATE_TEXTURESTAGE(7, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
786     { STATE_TEXTURESTAGE(7, WINED3DTSS_ALPHAARG2),        { STATE_TEXTURESTAGE(7, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
787     { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00),     { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00),     nvts_bumpenvmat     }, NV_TEXTURE_SHADER2              },
788     { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT01),     { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00),     nvts_bumpenvmat     }, NV_TEXTURE_SHADER2              },
789     { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT10),     { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00),     nvts_bumpenvmat     }, NV_TEXTURE_SHADER2              },
790     { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT11),     { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00),     nvts_bumpenvmat     }, NV_TEXTURE_SHADER2              },
791     { STATE_TEXTURESTAGE(7, WINED3DTSS_COLORARG0),        { STATE_TEXTURESTAGE(7, WINED3DTSS_COLOROP),          nvrc_colorop        }, 0                               },
792     { STATE_TEXTURESTAGE(7, WINED3DTSS_ALPHAARG0),        { STATE_TEXTURESTAGE(7, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
793     { STATE_TEXTURESTAGE(7, WINED3DTSS_RESULTARG),        { STATE_TEXTURESTAGE(7, WINED3DTSS_COLOROP),          nvrc_colorop        }, 0                               },
794     { STATE_PIXELSHADER,                                  { STATE_PIXELSHADER,                                  apply_pixelshader   }, 0                               },
795     { STATE_RENDER(WINED3DRS_SRGBWRITEENABLE),            { STATE_PIXELSHADER,                                  apply_pixelshader   }, 0                               },
796     { STATE_RENDER(WINED3DRS_TEXTUREFACTOR),              { STATE_RENDER(WINED3DRS_TEXTUREFACTOR),              nvrc_texfactor      }, 0                               },
797     { STATE_RENDER(WINED3DRS_FOGCOLOR),                   { STATE_RENDER(WINED3DRS_FOGCOLOR),                   state_fogcolor      }, 0                               },
798     { STATE_RENDER(WINED3DRS_FOGDENSITY),                 { STATE_RENDER(WINED3DRS_FOGDENSITY),                 state_fogdensity    }, 0                               },
799     { STATE_RENDER(WINED3DRS_FOGENABLE),                  { STATE_RENDER(WINED3DRS_FOGENABLE),                  state_fog_fragpart  }, 0                               },
800     { STATE_RENDER(WINED3DRS_FOGTABLEMODE),               { STATE_RENDER(WINED3DRS_FOGENABLE),                  state_fog_fragpart  }, 0                               },
801     { STATE_RENDER(WINED3DRS_FOGVERTEXMODE),              { STATE_RENDER(WINED3DRS_FOGENABLE),                  state_fog_fragpart  }, 0                               },
802     { STATE_RENDER(WINED3DRS_FOGSTART),                   { STATE_RENDER(WINED3DRS_FOGSTART),                   state_fogstartend   }, 0                               },
803     { STATE_RENDER(WINED3DRS_FOGEND),                     { STATE_RENDER(WINED3DRS_FOGSTART),                   state_fogstartend   }, 0                               },
804     { STATE_SAMPLER(0),                                   { STATE_SAMPLER(0),                                   nvts_texdim         }, NV_TEXTURE_SHADER2              },
805     { STATE_SAMPLER(0),                                   { STATE_SAMPLER(0),                                   sampler_texdim      }, 0                               },
806     { STATE_SAMPLER(1),                                   { STATE_SAMPLER(1),                                   nvts_texdim         }, NV_TEXTURE_SHADER2              },
807     { STATE_SAMPLER(1),                                   { STATE_SAMPLER(1),                                   sampler_texdim      }, 0                               },
808     { STATE_SAMPLER(2),                                   { STATE_SAMPLER(2),                                   nvts_texdim         }, NV_TEXTURE_SHADER2              },
809     { STATE_SAMPLER(2),                                   { STATE_SAMPLER(2),                                   sampler_texdim      }, 0                               },
810     { STATE_SAMPLER(3),                                   { STATE_SAMPLER(3),                                   nvts_texdim         }, NV_TEXTURE_SHADER2              },
811     { STATE_SAMPLER(3),                                   { STATE_SAMPLER(3),                                   sampler_texdim      }, 0                               },
812     { STATE_SAMPLER(4),                                   { STATE_SAMPLER(4),                                   nvts_texdim         }, NV_TEXTURE_SHADER2              },
813     { STATE_SAMPLER(4),                                   { STATE_SAMPLER(4),                                   sampler_texdim      }, 0                               },
814     { STATE_SAMPLER(5),                                   { STATE_SAMPLER(5),                                   nvts_texdim         }, NV_TEXTURE_SHADER2              },
815     { STATE_SAMPLER(5),                                   { STATE_SAMPLER(5),                                   sampler_texdim      }, 0                               },
816     { STATE_SAMPLER(6),                                   { STATE_SAMPLER(6),                                   nvts_texdim         }, NV_TEXTURE_SHADER2              },
817     { STATE_SAMPLER(6),                                   { STATE_SAMPLER(6),                                   sampler_texdim      }, 0                               },
818     { STATE_SAMPLER(7),                                   { STATE_SAMPLER(7),                                   nvts_texdim         }, NV_TEXTURE_SHADER2              },
819     { STATE_SAMPLER(7),                                   { STATE_SAMPLER(7),                                   sampler_texdim      }, 0                               },
820     {0 /* Terminate */,                                   { 0,                                                  0                   }, 0                               },
821 };
822
823 const struct fragment_pipeline nvts_fragment_pipeline = {
824     nvts_enable,
825     nvrc_fragment_get_caps,
826     nvrc_fragment_alloc,
827     nvrc_fragment_free,
828     nvts_color_fixup_supported,
829     nvrc_fragmentstate_template,
830     FALSE /* we cannot disable projected textures. The vertex pipe has to do it */
831 };
832
833 const struct fragment_pipeline nvrc_fragment_pipeline = {
834     nvrc_enable,
835     nvrc_fragment_get_caps,
836     nvrc_fragment_alloc,
837     nvrc_fragment_free,
838     nvts_color_fixup_supported,
839     nvrc_fragmentstate_template,
840     FALSE /* we cannot disable projected textures. The vertex pipe has to do it */
841 };