ole32: Move private data structure out of header file.
[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 != -1) {
466         if (tex_used && mapped_stage >= GL_LIMITS(textures)) {
467             FIXME("Attempt to enable unsupported stage!\n");
468             return;
469         }
470         GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB + mapped_stage));
471         checkGLcall("glActiveTextureARB");
472     }
473
474     if(stateblock->lowest_disabled_stage > 0) {
475         glEnable(GL_REGISTER_COMBINERS_NV);
476         GL_EXTCALL(glCombinerParameteriNV(GL_NUM_GENERAL_COMBINERS_NV, stateblock->lowest_disabled_stage));
477     } else {
478         glDisable(GL_REGISTER_COMBINERS_NV);
479     }
480     if(stage >= stateblock->lowest_disabled_stage) {
481         TRACE("Stage disabled\n");
482         if (mapped_stage != -1) {
483             /* Disable everything here */
484             glDisable(GL_TEXTURE_2D);
485             checkGLcall("glDisable(GL_TEXTURE_2D)");
486             glDisable(GL_TEXTURE_3D);
487             checkGLcall("glDisable(GL_TEXTURE_3D)");
488             if(GL_SUPPORT(ARB_TEXTURE_CUBE_MAP)) {
489                 glDisable(GL_TEXTURE_CUBE_MAP_ARB);
490                 checkGLcall("glDisable(GL_TEXTURE_CUBE_MAP_ARB)");
491             }
492             if(GL_SUPPORT(ARB_TEXTURE_RECTANGLE)) {
493                 glDisable(GL_TEXTURE_RECTANGLE_ARB);
494                 checkGLcall("glDisable(GL_TEXTURE_RECTANGLE_ARB)");
495             }
496             if(GL_SUPPORT(NV_TEXTURE_SHADER2) && mapped_stage < GL_LIMITS(textures)) {
497                 glTexEnvi(GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_NONE);
498             }
499         }
500         /* All done */
501         return;
502     }
503
504     /* The sampler will also activate the correct texture dimensions, so no need to do it here
505      * if the sampler for this stage is dirty
506      */
507     if(!isStateDirty(context, STATE_SAMPLER(stage))) {
508         if (tex_used) {
509             if(GL_SUPPORT(NV_TEXTURE_SHADER2)) {
510                 nvts_activate_dimensions(stage, stateblock, context);
511             } else {
512                 texture_activate_dimensions(stage, stateblock, context);
513             }
514         }
515     }
516
517     /* Set the texture combiners */
518     set_tex_op_nvrc((IWineD3DDevice *)stateblock->wineD3DDevice, FALSE, stage,
519                         stateblock->textureState[stage][WINED3DTSS_COLOROP],
520                         stateblock->textureState[stage][WINED3DTSS_COLORARG1],
521                         stateblock->textureState[stage][WINED3DTSS_COLORARG2],
522                         stateblock->textureState[stage][WINED3DTSS_COLORARG0],
523                         mapped_stage,
524                         stateblock->textureState[stage][WINED3DTSS_RESULTARG]);
525
526     /* In register combiners bump mapping is done in the stage AFTER the one that has the bump map operation set,
527      * thus the texture shader may have to be updated
528      */
529     if(GL_SUPPORT(NV_TEXTURE_SHADER2)) {
530         BOOL usesBump = (stateblock->textureState[stage][WINED3DTSS_COLOROP] == WINED3DTOP_BUMPENVMAPLUMINANCE ||
531                             stateblock->textureState[stage][WINED3DTSS_COLOROP] == WINED3DTOP_BUMPENVMAP) ? TRUE : FALSE;
532         BOOL usedBump = (context->texShaderBumpMap & 1 << (stage + 1)) ? TRUE : FALSE;
533         if(usesBump != usedBump) {
534             GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB + mapped_stage + 1));
535             checkGLcall("glActiveTextureARB");
536             nvts_activate_dimensions(stage + 1, stateblock, context);
537             GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB + mapped_stage));
538             checkGLcall("glActiveTextureARB");
539         }
540     }
541 }
542
543 static void nvts_texdim(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
544     DWORD sampler = state - STATE_SAMPLER(0);
545     DWORD mapped_stage = stateblock->wineD3DDevice->texUnitMap[sampler];
546
547     /* No need to enable / disable anything here for unused samplers. The tex_colorop
548     * handler takes care. Also no action is needed with pixel shaders, or if tex_colorop
549     * will take care of this business
550     */
551     if(mapped_stage == -1 || mapped_stage >= GL_LIMITS(textures)) return;
552     if(sampler >= stateblock->lowest_disabled_stage) return;
553     if(isStateDirty(context, STATE_TEXTURESTAGE(sampler, WINED3DTSS_COLOROP))) return;
554
555     nvts_activate_dimensions(sampler, stateblock, context);
556 }
557
558 static void nvts_bumpenvmat(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
559     DWORD stage = (state - STATE_TEXTURESTAGE(0, 0)) / (WINED3D_HIGHEST_TEXTURE_STATE + 1);
560     DWORD mapped_stage = stateblock->wineD3DDevice->texUnitMap[stage + 1];
561     float mat[2][2];
562
563     /* Direct3D sets the matrix in the stage reading the perturbation map. The result is used to
564      * offset the destination stage(always stage + 1 in d3d). In GL_NV_texture_shader, the bump
565      * map offsetting is done in the stage reading the bump mapped texture, and the perturbation
566      * map is read from a specified source stage(always stage - 1 for d3d). Thus set the matrix
567      * for stage + 1. Keep the nvrc tex unit mapping in mind too
568      */
569     if(mapped_stage < GL_LIMITS(textures)) {
570         GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB + mapped_stage));
571         checkGLcall("GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB + mapped_stage))");
572
573         /* We can't just pass a pointer to the stateblock to GL due to the different matrix
574          * format(column major vs row major)
575          */
576         mat[0][0] = *((float *) &stateblock->textureState[stage][WINED3DTSS_BUMPENVMAT00]);
577         mat[1][0] = *((float *) &stateblock->textureState[stage][WINED3DTSS_BUMPENVMAT01]);
578         mat[0][1] = *((float *) &stateblock->textureState[stage][WINED3DTSS_BUMPENVMAT10]);
579         mat[1][1] = *((float *) &stateblock->textureState[stage][WINED3DTSS_BUMPENVMAT11]);
580         glTexEnvfv(GL_TEXTURE_SHADER_NV, GL_OFFSET_TEXTURE_MATRIX_NV, (float *) mat);
581         checkGLcall("glTexEnvfv(GL_TEXTURE_SHADER_NV, GL_OFFSET_TEXTURE_MATRIX_NV, mat)");
582     }
583 }
584
585 static void nvrc_texfactor(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
586     float col[4];
587     D3DCOLORTOGLFLOAT4(stateblock->renderState[WINED3DRS_TEXTUREFACTOR], col);
588     GL_EXTCALL(glCombinerParameterfvNV(GL_CONSTANT_COLOR0_NV, &col[0]));
589 }
590 #undef GLINFO_LOCATION
591
592 #define GLINFO_LOCATION (*gl_info)
593 static void nvrc_enable(IWineD3DDevice *iface, BOOL enable) { }
594
595 static void nvts_enable(IWineD3DDevice *iface, BOOL enable) {
596     if(enable) {
597         glEnable(GL_TEXTURE_SHADER_NV);
598         checkGLcall("glEnable(GL_TEXTURE_SHADER_NV)");
599     } else {
600         glDisable(GL_TEXTURE_SHADER_NV);
601         checkGLcall("glDisable(GL_TEXTURE_SHADER_NV)");
602     }
603 }
604
605 static void nvrc_fragment_get_caps(WINED3DDEVTYPE devtype, const WineD3D_GL_Info *gl_info, struct fragment_caps *pCaps)
606 {
607     pCaps->TextureOpCaps =  WINED3DTEXOPCAPS_ADD                        |
608                             WINED3DTEXOPCAPS_ADDSIGNED                  |
609                             WINED3DTEXOPCAPS_ADDSIGNED2X                |
610                             WINED3DTEXOPCAPS_MODULATE                   |
611                             WINED3DTEXOPCAPS_MODULATE2X                 |
612                             WINED3DTEXOPCAPS_MODULATE4X                 |
613                             WINED3DTEXOPCAPS_SELECTARG1                 |
614                             WINED3DTEXOPCAPS_SELECTARG2                 |
615                             WINED3DTEXOPCAPS_DISABLE                    |
616                             WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA          |
617                             WINED3DTEXOPCAPS_BLENDTEXTUREALPHA          |
618                             WINED3DTEXOPCAPS_BLENDFACTORALPHA           |
619                             WINED3DTEXOPCAPS_BLENDCURRENTALPHA          |
620                             WINED3DTEXOPCAPS_LERP                       |
621                             WINED3DTEXOPCAPS_SUBTRACT                   |
622                             WINED3DTEXOPCAPS_ADDSMOOTH                  |
623                             WINED3DTEXOPCAPS_MULTIPLYADD                |
624                             WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR     |
625                             WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA     |
626                             WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM        |
627                             WINED3DTEXOPCAPS_DOTPRODUCT3                |
628                             WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR  |
629                             WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA;
630
631     if(GL_SUPPORT(NV_TEXTURE_SHADER2)) {
632         /* Bump mapping is supported already in NV_TEXTURE_SHADER, but that extension does
633          * not support 3D textures. This asks for trouble if an app uses both bump mapping
634          * and 3D textures. It also allows us to keep the code simpler by having texture
635          * shaders constantly enabled.
636          */
637         pCaps->TextureOpCaps |= WINED3DTEXOPCAPS_BUMPENVMAP;
638         /* TODO: Luminance bump map? */
639     }
640
641 #if 0
642     /* FIXME: Add
643             pCaps->TextureOpCaps |= WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE
644             WINED3DTEXOPCAPS_PREMODULATE */
645 #endif
646
647     pCaps->MaxTextureBlendStages   = GL_LIMITS(texture_stages);
648     pCaps->MaxSimultaneousTextures = GL_LIMITS(textures);
649
650     pCaps->PrimitiveMiscCaps |=  WINED3DPMISCCAPS_TSSARGTEMP;
651
652     /* The caps below can be supported but aren't handled yet in utils.c 'd3dta_to_combiner_input', disable them until support is fixed */
653 #if 0
654     if (GL_SUPPORT(NV_REGISTER_COMBINERS2))
655     pCaps->PrimitiveMiscCaps |=  WINED3DPMISCCAPS_PERSTAGECONSTANT;
656 #endif
657 }
658
659 static HRESULT nvrc_fragment_alloc(IWineD3DDevice *iface) { return WINED3D_OK; }
660 static void nvrc_fragment_free(IWineD3DDevice *iface) {}
661
662 /* Two fixed function pipeline implementations using GL_NV_register_combiners and
663  * GL_NV_texture_shader. The nvts_fragment_pipeline assumes that both extensions
664  * are available(geforce 3 and newer), while nvrc_fragment_pipeline uses only the
665  * register combiners extension(Pre-GF3).
666  */
667
668 static BOOL nvts_color_fixup_supported(struct color_fixup_desc fixup)
669 {
670     if (TRACE_ON(d3d))
671     {
672         TRACE("Checking support for fixup:\n");
673         dump_color_fixup_desc(fixup);
674     }
675
676     /* We only support identity conversions. */
677     if (is_identity_fixup(fixup))
678     {
679         TRACE("[OK]\n");
680         return TRUE;
681     }
682
683     TRACE("[FAILED]\n");
684     return FALSE;
685 }
686
687 static const struct StateEntryTemplate nvrc_fragmentstate_template[] = {
688     { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          nvrc_colorop        }, 0                               },
689     { STATE_TEXTURESTAGE(0, WINED3DTSS_COLORARG1),        { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          nvrc_colorop        }, 0                               },
690     { STATE_TEXTURESTAGE(0, WINED3DTSS_COLORARG2),        { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          nvrc_colorop        }, 0                               },
691     { STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAOP),          { STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
692     { STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAARG1),        { STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
693     { STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAARG2),        { STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
694     { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00),     { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00),     nvts_bumpenvmat     }, NV_TEXTURE_SHADER2              },
695     { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT01),     { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00),     nvts_bumpenvmat     }, NV_TEXTURE_SHADER2              },
696     { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT10),     { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00),     nvts_bumpenvmat     }, NV_TEXTURE_SHADER2              },
697     { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT11),     { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00),     nvts_bumpenvmat     }, NV_TEXTURE_SHADER2              },
698     { STATE_TEXTURESTAGE(0, WINED3DTSS_COLORARG0),        { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          nvrc_colorop        }, 0                               },
699     { STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAARG0),        { STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
700     { STATE_TEXTURESTAGE(0, WINED3DTSS_RESULTARG),        { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          nvrc_colorop        }, 0                               },
701     { STATE_TEXTURESTAGE(1, WINED3DTSS_COLOROP),          { STATE_TEXTURESTAGE(1, WINED3DTSS_COLOROP),          nvrc_colorop        }, 0                               },
702     { STATE_TEXTURESTAGE(1, WINED3DTSS_COLORARG1),        { STATE_TEXTURESTAGE(1, WINED3DTSS_COLOROP),          nvrc_colorop        }, 0                               },
703     { STATE_TEXTURESTAGE(1, WINED3DTSS_COLORARG2),        { STATE_TEXTURESTAGE(1, WINED3DTSS_COLOROP),          nvrc_colorop        }, 0                               },
704     { STATE_TEXTURESTAGE(1, WINED3DTSS_ALPHAOP),          { STATE_TEXTURESTAGE(1, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
705     { STATE_TEXTURESTAGE(1, WINED3DTSS_ALPHAARG1),        { STATE_TEXTURESTAGE(1, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
706     { STATE_TEXTURESTAGE(1, WINED3DTSS_ALPHAARG2),        { STATE_TEXTURESTAGE(1, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
707     { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00),     { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00),     nvts_bumpenvmat     }, NV_TEXTURE_SHADER2              },
708     { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT01),     { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00),     nvts_bumpenvmat     }, NV_TEXTURE_SHADER2              },
709     { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT10),     { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00),     nvts_bumpenvmat     }, NV_TEXTURE_SHADER2              },
710     { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT11),     { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00),     nvts_bumpenvmat     }, NV_TEXTURE_SHADER2              },
711     { STATE_TEXTURESTAGE(1, WINED3DTSS_COLORARG0),        { STATE_TEXTURESTAGE(1, WINED3DTSS_COLOROP),          nvrc_colorop        }, 0                               },
712     { STATE_TEXTURESTAGE(1, WINED3DTSS_ALPHAARG0),        { STATE_TEXTURESTAGE(1, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
713     { STATE_TEXTURESTAGE(1, WINED3DTSS_RESULTARG),        { STATE_TEXTURESTAGE(1, WINED3DTSS_COLOROP),          nvrc_colorop        }, 0                               },
714     { STATE_TEXTURESTAGE(2, WINED3DTSS_COLOROP),          { STATE_TEXTURESTAGE(2, WINED3DTSS_COLOROP),          nvrc_colorop        }, 0                               },
715     { STATE_TEXTURESTAGE(2, WINED3DTSS_COLORARG1),        { STATE_TEXTURESTAGE(2, WINED3DTSS_COLOROP),          nvrc_colorop        }, 0                               },
716     { STATE_TEXTURESTAGE(2, WINED3DTSS_COLORARG2),        { STATE_TEXTURESTAGE(2, WINED3DTSS_COLOROP),          nvrc_colorop        }, 0                               },
717     { STATE_TEXTURESTAGE(2, WINED3DTSS_ALPHAOP),          { STATE_TEXTURESTAGE(2, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
718     { STATE_TEXTURESTAGE(2, WINED3DTSS_ALPHAARG1),        { STATE_TEXTURESTAGE(2, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
719     { STATE_TEXTURESTAGE(2, WINED3DTSS_ALPHAARG2),        { STATE_TEXTURESTAGE(2, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
720     { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00),     { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00),     nvts_bumpenvmat     }, NV_TEXTURE_SHADER2              },
721     { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT01),     { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00),     nvts_bumpenvmat     }, NV_TEXTURE_SHADER2              },
722     { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT10),     { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00),     nvts_bumpenvmat     }, NV_TEXTURE_SHADER2              },
723     { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT11),     { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00),     nvts_bumpenvmat     }, NV_TEXTURE_SHADER2              },
724     { STATE_TEXTURESTAGE(2, WINED3DTSS_COLORARG0),        { STATE_TEXTURESTAGE(2, WINED3DTSS_COLOROP),          nvrc_colorop        }, 0                               },
725     { STATE_TEXTURESTAGE(2, WINED3DTSS_ALPHAARG0),        { STATE_TEXTURESTAGE(2, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
726     { STATE_TEXTURESTAGE(2, WINED3DTSS_RESULTARG),        { STATE_TEXTURESTAGE(2, WINED3DTSS_COLOROP),          nvrc_colorop        }, 0                               },
727     { STATE_TEXTURESTAGE(3, WINED3DTSS_COLOROP),          { STATE_TEXTURESTAGE(3, WINED3DTSS_COLOROP),          nvrc_colorop        }, 0                               },
728     { STATE_TEXTURESTAGE(3, WINED3DTSS_COLORARG1),        { STATE_TEXTURESTAGE(3, WINED3DTSS_COLOROP),          nvrc_colorop        }, 0                               },
729     { STATE_TEXTURESTAGE(3, WINED3DTSS_COLORARG2),        { STATE_TEXTURESTAGE(3, WINED3DTSS_COLOROP),          nvrc_colorop        }, 0                               },
730     { STATE_TEXTURESTAGE(3, WINED3DTSS_ALPHAOP),          { STATE_TEXTURESTAGE(3, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
731     { STATE_TEXTURESTAGE(3, WINED3DTSS_ALPHAARG1),        { STATE_TEXTURESTAGE(3, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
732     { STATE_TEXTURESTAGE(3, WINED3DTSS_ALPHAARG2),        { STATE_TEXTURESTAGE(3, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
733     { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00),     { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00),     nvts_bumpenvmat     }, NV_TEXTURE_SHADER2              },
734     { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT01),     { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00),     nvts_bumpenvmat     }, NV_TEXTURE_SHADER2              },
735     { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT10),     { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00),     nvts_bumpenvmat     }, NV_TEXTURE_SHADER2              },
736     { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT11),     { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00),     nvts_bumpenvmat     }, NV_TEXTURE_SHADER2              },
737     { STATE_TEXTURESTAGE(3, WINED3DTSS_COLORARG0),        { STATE_TEXTURESTAGE(3, WINED3DTSS_COLOROP),          nvrc_colorop        }, 0                               },
738     { STATE_TEXTURESTAGE(3, WINED3DTSS_ALPHAARG0),        { STATE_TEXTURESTAGE(3, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
739     { STATE_TEXTURESTAGE(3, WINED3DTSS_RESULTARG),        { STATE_TEXTURESTAGE(3, WINED3DTSS_COLOROP),          nvrc_colorop        }, 0                               },
740     { STATE_TEXTURESTAGE(4, WINED3DTSS_COLOROP),          { STATE_TEXTURESTAGE(4, WINED3DTSS_COLOROP),          nvrc_colorop        }, 0                               },
741     { STATE_TEXTURESTAGE(4, WINED3DTSS_COLORARG1),        { STATE_TEXTURESTAGE(4, WINED3DTSS_COLOROP),          nvrc_colorop        }, 0                               },
742     { STATE_TEXTURESTAGE(4, WINED3DTSS_COLORARG2),        { STATE_TEXTURESTAGE(4, WINED3DTSS_COLOROP),          nvrc_colorop        }, 0                               },
743     { STATE_TEXTURESTAGE(4, WINED3DTSS_ALPHAOP),          { STATE_TEXTURESTAGE(4, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
744     { STATE_TEXTURESTAGE(4, WINED3DTSS_ALPHAARG1),        { STATE_TEXTURESTAGE(4, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
745     { STATE_TEXTURESTAGE(4, WINED3DTSS_ALPHAARG2),        { STATE_TEXTURESTAGE(4, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
746     { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00),     { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00),     nvts_bumpenvmat     }, NV_TEXTURE_SHADER2              },
747     { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT01),     { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00),     nvts_bumpenvmat     }, NV_TEXTURE_SHADER2              },
748     { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT10),     { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00),     nvts_bumpenvmat     }, NV_TEXTURE_SHADER2              },
749     { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT11),     { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00),     nvts_bumpenvmat     }, NV_TEXTURE_SHADER2              },
750     { STATE_TEXTURESTAGE(4, WINED3DTSS_COLORARG0),        { STATE_TEXTURESTAGE(4, WINED3DTSS_COLOROP),          nvrc_colorop        }, 0                               },
751     { STATE_TEXTURESTAGE(4, WINED3DTSS_ALPHAARG0),        { STATE_TEXTURESTAGE(4, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
752     { STATE_TEXTURESTAGE(4, WINED3DTSS_RESULTARG),        { STATE_TEXTURESTAGE(4, WINED3DTSS_COLOROP),          nvrc_colorop        }, 0                               },
753     { STATE_TEXTURESTAGE(5, WINED3DTSS_COLOROP),          { STATE_TEXTURESTAGE(5, WINED3DTSS_COLOROP),          nvrc_colorop        }, 0                               },
754     { STATE_TEXTURESTAGE(5, WINED3DTSS_COLORARG1),        { STATE_TEXTURESTAGE(5, WINED3DTSS_COLOROP),          nvrc_colorop        }, 0                               },
755     { STATE_TEXTURESTAGE(5, WINED3DTSS_COLORARG2),        { STATE_TEXTURESTAGE(5, WINED3DTSS_COLOROP),          nvrc_colorop        }, 0                               },
756     { STATE_TEXTURESTAGE(5, WINED3DTSS_ALPHAOP),          { STATE_TEXTURESTAGE(5, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
757     { STATE_TEXTURESTAGE(5, WINED3DTSS_ALPHAARG1),        { STATE_TEXTURESTAGE(5, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
758     { STATE_TEXTURESTAGE(5, WINED3DTSS_ALPHAARG2),        { STATE_TEXTURESTAGE(5, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
759     { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00),     { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00),     nvts_bumpenvmat     }, NV_TEXTURE_SHADER2              },
760     { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT01),     { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00),     nvts_bumpenvmat     }, NV_TEXTURE_SHADER2              },
761     { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT10),     { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00),     nvts_bumpenvmat     }, NV_TEXTURE_SHADER2              },
762     { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT11),     { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00),     nvts_bumpenvmat     }, NV_TEXTURE_SHADER2              },
763     { STATE_TEXTURESTAGE(5, WINED3DTSS_COLORARG0),        { STATE_TEXTURESTAGE(5, WINED3DTSS_COLOROP),          nvrc_colorop        }, 0                               },
764     { STATE_TEXTURESTAGE(5, WINED3DTSS_ALPHAARG0),        { STATE_TEXTURESTAGE(5, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
765     { STATE_TEXTURESTAGE(5, WINED3DTSS_RESULTARG),        { STATE_TEXTURESTAGE(5, WINED3DTSS_COLOROP),          nvrc_colorop        }, 0                               },
766     { STATE_TEXTURESTAGE(6, WINED3DTSS_COLOROP),          { STATE_TEXTURESTAGE(6, WINED3DTSS_COLOROP),          nvrc_colorop        }, 0                               },
767     { STATE_TEXTURESTAGE(6, WINED3DTSS_COLORARG1),        { STATE_TEXTURESTAGE(6, WINED3DTSS_COLOROP),          nvrc_colorop        }, 0                               },
768     { STATE_TEXTURESTAGE(6, WINED3DTSS_COLORARG2),        { STATE_TEXTURESTAGE(6, WINED3DTSS_COLOROP),          nvrc_colorop        }, 0                               },
769     { STATE_TEXTURESTAGE(6, WINED3DTSS_ALPHAOP),          { STATE_TEXTURESTAGE(6, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
770     { STATE_TEXTURESTAGE(6, WINED3DTSS_ALPHAARG1),        { STATE_TEXTURESTAGE(6, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
771     { STATE_TEXTURESTAGE(6, WINED3DTSS_ALPHAARG2),        { STATE_TEXTURESTAGE(6, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
772     { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00),     { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00),     nvts_bumpenvmat     }, NV_TEXTURE_SHADER2              },
773     { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT01),     { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00),     nvts_bumpenvmat     }, NV_TEXTURE_SHADER2              },
774     { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT10),     { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00),     nvts_bumpenvmat     }, NV_TEXTURE_SHADER2              },
775     { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT11),     { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00),     nvts_bumpenvmat     }, NV_TEXTURE_SHADER2              },
776     { STATE_TEXTURESTAGE(6, WINED3DTSS_COLORARG0),        { STATE_TEXTURESTAGE(6, WINED3DTSS_COLOROP),          nvrc_colorop        }, 0                               },
777     { STATE_TEXTURESTAGE(6, WINED3DTSS_ALPHAARG0),        { STATE_TEXTURESTAGE(6, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
778     { STATE_TEXTURESTAGE(6, WINED3DTSS_RESULTARG),        { STATE_TEXTURESTAGE(6, WINED3DTSS_COLOROP),          nvrc_colorop        }, 0                               },
779     { STATE_TEXTURESTAGE(7, WINED3DTSS_COLOROP),          { STATE_TEXTURESTAGE(7, WINED3DTSS_COLOROP),          nvrc_colorop        }, 0                               },
780     { STATE_TEXTURESTAGE(7, WINED3DTSS_COLORARG1),        { STATE_TEXTURESTAGE(7, WINED3DTSS_COLOROP),          nvrc_colorop        }, 0                               },
781     { STATE_TEXTURESTAGE(7, WINED3DTSS_COLORARG2),        { STATE_TEXTURESTAGE(7, WINED3DTSS_COLOROP),          nvrc_colorop        }, 0                               },
782     { STATE_TEXTURESTAGE(7, WINED3DTSS_ALPHAOP),          { STATE_TEXTURESTAGE(7, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
783     { STATE_TEXTURESTAGE(7, WINED3DTSS_ALPHAARG1),        { STATE_TEXTURESTAGE(7, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
784     { STATE_TEXTURESTAGE(7, WINED3DTSS_ALPHAARG2),        { STATE_TEXTURESTAGE(7, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
785     { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00),     { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00),     nvts_bumpenvmat     }, NV_TEXTURE_SHADER2              },
786     { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT01),     { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00),     nvts_bumpenvmat     }, NV_TEXTURE_SHADER2              },
787     { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT10),     { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00),     nvts_bumpenvmat     }, NV_TEXTURE_SHADER2              },
788     { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT11),     { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00),     nvts_bumpenvmat     }, NV_TEXTURE_SHADER2              },
789     { STATE_TEXTURESTAGE(7, WINED3DTSS_COLORARG0),        { STATE_TEXTURESTAGE(7, WINED3DTSS_COLOROP),          nvrc_colorop        }, 0                               },
790     { STATE_TEXTURESTAGE(7, WINED3DTSS_ALPHAARG0),        { STATE_TEXTURESTAGE(7, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
791     { STATE_TEXTURESTAGE(7, WINED3DTSS_RESULTARG),        { STATE_TEXTURESTAGE(7, WINED3DTSS_COLOROP),          nvrc_colorop        }, 0                               },
792     { STATE_PIXELSHADER,                                  { STATE_PIXELSHADER,                                  apply_pixelshader   }, 0                               },
793     { STATE_RENDER(WINED3DRS_SRGBWRITEENABLE),            { STATE_PIXELSHADER,                                  apply_pixelshader   }, 0                               },
794     { STATE_RENDER(WINED3DRS_TEXTUREFACTOR),              { STATE_RENDER(WINED3DRS_TEXTUREFACTOR),              nvrc_texfactor      }, 0                               },
795     { STATE_RENDER(WINED3DRS_FOGCOLOR),                   { STATE_RENDER(WINED3DRS_FOGCOLOR),                   state_fogcolor      }, 0                               },
796     { STATE_RENDER(WINED3DRS_FOGDENSITY),                 { STATE_RENDER(WINED3DRS_FOGDENSITY),                 state_fogdensity    }, 0                               },
797     { STATE_RENDER(WINED3DRS_FOGENABLE),                  { STATE_RENDER(WINED3DRS_FOGENABLE),                  state_fog_fragpart  }, 0                               },
798     { STATE_RENDER(WINED3DRS_FOGTABLEMODE),               { STATE_RENDER(WINED3DRS_FOGENABLE),                  state_fog_fragpart  }, 0                               },
799     { STATE_RENDER(WINED3DRS_FOGVERTEXMODE),              { STATE_RENDER(WINED3DRS_FOGENABLE),                  state_fog_fragpart  }, 0                               },
800     { STATE_RENDER(WINED3DRS_FOGSTART),                   { STATE_RENDER(WINED3DRS_FOGSTART),                   state_fogstartend   }, 0                               },
801     { STATE_RENDER(WINED3DRS_FOGEND),                     { STATE_RENDER(WINED3DRS_FOGSTART),                   state_fogstartend   }, 0                               },
802     { STATE_SAMPLER(0),                                   { STATE_SAMPLER(0),                                   nvts_texdim         }, NV_TEXTURE_SHADER2              },
803     { STATE_SAMPLER(0),                                   { STATE_SAMPLER(0),                                   sampler_texdim      }, 0                               },
804     { STATE_SAMPLER(1),                                   { STATE_SAMPLER(1),                                   nvts_texdim         }, NV_TEXTURE_SHADER2              },
805     { STATE_SAMPLER(1),                                   { STATE_SAMPLER(1),                                   sampler_texdim      }, 0                               },
806     { STATE_SAMPLER(2),                                   { STATE_SAMPLER(2),                                   nvts_texdim         }, NV_TEXTURE_SHADER2              },
807     { STATE_SAMPLER(2),                                   { STATE_SAMPLER(2),                                   sampler_texdim      }, 0                               },
808     { STATE_SAMPLER(3),                                   { STATE_SAMPLER(3),                                   nvts_texdim         }, NV_TEXTURE_SHADER2              },
809     { STATE_SAMPLER(3),                                   { STATE_SAMPLER(3),                                   sampler_texdim      }, 0                               },
810     { STATE_SAMPLER(4),                                   { STATE_SAMPLER(4),                                   nvts_texdim         }, NV_TEXTURE_SHADER2              },
811     { STATE_SAMPLER(4),                                   { STATE_SAMPLER(4),                                   sampler_texdim      }, 0                               },
812     { STATE_SAMPLER(5),                                   { STATE_SAMPLER(5),                                   nvts_texdim         }, NV_TEXTURE_SHADER2              },
813     { STATE_SAMPLER(5),                                   { STATE_SAMPLER(5),                                   sampler_texdim      }, 0                               },
814     { STATE_SAMPLER(6),                                   { STATE_SAMPLER(6),                                   nvts_texdim         }, NV_TEXTURE_SHADER2              },
815     { STATE_SAMPLER(6),                                   { STATE_SAMPLER(6),                                   sampler_texdim      }, 0                               },
816     { STATE_SAMPLER(7),                                   { STATE_SAMPLER(7),                                   nvts_texdim         }, NV_TEXTURE_SHADER2              },
817     { STATE_SAMPLER(7),                                   { STATE_SAMPLER(7),                                   sampler_texdim      }, 0                               },
818     {0 /* Terminate */,                                   { 0,                                                  0                   }, 0                               },
819 };
820
821 const struct fragment_pipeline nvts_fragment_pipeline = {
822     nvts_enable,
823     nvrc_fragment_get_caps,
824     nvrc_fragment_alloc,
825     nvrc_fragment_free,
826     nvts_color_fixup_supported,
827     nvrc_fragmentstate_template,
828     FALSE /* we cannot disable projected textures. The vertex pipe has to do it */
829 };
830
831 const struct fragment_pipeline nvrc_fragment_pipeline = {
832     nvrc_enable,
833     nvrc_fragment_get_caps,
834     nvrc_fragment_alloc,
835     nvrc_fragment_free,
836     nvts_color_fixup_supported,
837     nvrc_fragmentstate_template,
838     FALSE /* we cannot disable projected textures. The vertex pipe has to do it */
839 };