2 * GLSL pixel and vertex shader implementation
4 * Copyright 2006 Jason Green
5 * Copyright 2006-2007 Henri Verbeet
6 * Copyright 2007-2009, 2013 Stefan Dösinger for CodeWeavers
7 * Copyright 2009-2011 Henri Verbeet for CodeWeavers
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 * D3D shader asm has swizzles on source parameters, and write masks for
26 * destination parameters. GLSL uses swizzles for both. The result of this is
27 * that for example "mov dst.xw, src.zyxw" becomes "dst.xw = src.zw" in GLSL.
28 * Ie, to generate a proper GLSL source swizzle, we need to take the D3D write
29 * mask for the destination parameter into account.
33 #include "wine/port.h"
41 #include "wined3d_private.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader
);
44 WINE_DECLARE_DEBUG_CHANNEL(d3d
);
45 WINE_DECLARE_DEBUG_CHANNEL(winediag
);
47 #define WINED3D_GLSL_SAMPLE_PROJECTED 0x1
48 #define WINED3D_GLSL_SAMPLE_NPOT 0x2
49 #define WINED3D_GLSL_SAMPLE_LOD 0x4
50 #define WINED3D_GLSL_SAMPLE_GRAD 0x8
64 struct glsl_sample_function
68 enum wined3d_data_type data_type
;
73 HEAP_NODE_TRAVERSE_LEFT
,
74 HEAP_NODE_TRAVERSE_RIGHT
,
86 struct constant_entry
*entries
;
88 unsigned int *positions
;
92 /* GLSL shader private data */
93 struct shader_glsl_priv
{
94 struct wined3d_string_buffer shader_buffer
;
95 struct wine_rb_tree program_lookup
;
96 struct constant_heap vconst_heap
;
97 struct constant_heap pconst_heap
;
99 GLuint depth_blt_program_full
[WINED3D_GL_RES_TYPE_COUNT
];
100 GLuint depth_blt_program_masked
[WINED3D_GL_RES_TYPE_COUNT
];
101 UINT next_constant_version
;
103 const struct wined3d_vertex_pipe_ops
*vertex_pipe
;
104 const struct fragment_pipeline
*fragment_pipe
;
105 struct wine_rb_tree ffp_vertex_shaders
;
106 struct wine_rb_tree ffp_fragment_shaders
;
107 BOOL ffp_proj_control
;
110 struct glsl_vs_program
112 struct list shader_entry
;
114 GLenum vertex_color_clamp
;
115 GLint
*uniform_f_locations
;
116 GLint uniform_i_locations
[MAX_CONST_I
];
117 GLint uniform_b_locations
[MAX_CONST_B
];
118 GLint pos_fixup_location
;
120 GLint modelview_matrix_location
;
121 GLint projection_matrix_location
;
122 GLint normal_matrix_location
;
125 struct glsl_gs_program
127 struct list shader_entry
;
131 struct glsl_ps_program
133 struct list shader_entry
;
135 GLint
*uniform_f_locations
;
136 GLint uniform_i_locations
[MAX_CONST_I
];
137 GLint uniform_b_locations
[MAX_CONST_B
];
138 GLint bumpenv_mat_location
[MAX_TEXTURES
];
139 GLint bumpenv_lum_scale_location
[MAX_TEXTURES
];
140 GLint bumpenv_lum_offset_location
[MAX_TEXTURES
];
141 GLint tss_constant_location
[MAX_TEXTURES
];
142 GLint tex_factor_location
;
143 GLint specular_enable_location
;
144 GLint ycorrection_location
;
145 GLint np2_fixup_location
;
146 GLint color_key_location
;
147 const struct ps_np2fixup_info
*np2_fixup_info
;
150 /* Struct to maintain data about a linked GLSL program */
151 struct glsl_shader_prog_link
153 struct wine_rb_entry program_lookup_entry
;
154 struct glsl_vs_program vs
;
155 struct glsl_gs_program gs
;
156 struct glsl_ps_program ps
;
158 DWORD constant_update_mask
;
159 UINT constant_version
;
162 struct glsl_program_key
169 struct shader_glsl_ctx_priv
{
170 const struct vs_compile_args
*cur_vs_args
;
171 const struct ps_compile_args
*cur_ps_args
;
172 struct ps_np2fixup_info
*cur_np2fixup_info
;
175 struct glsl_context_data
177 struct glsl_shader_prog_link
*glsl_program
;
180 struct glsl_ps_compiled_shader
182 struct ps_compile_args args
;
183 struct ps_np2fixup_info np2fixup
;
187 struct glsl_vs_compiled_shader
189 struct vs_compile_args args
;
193 struct glsl_gs_compiled_shader
198 struct glsl_shader_private
202 struct glsl_vs_compiled_shader
*vs
;
203 struct glsl_gs_compiled_shader
*gs
;
204 struct glsl_ps_compiled_shader
*ps
;
206 UINT num_gl_shaders
, shader_array_size
;
209 struct glsl_ffp_vertex_shader
211 struct wined3d_ffp_vs_desc desc
;
213 struct list linked_programs
;
216 struct glsl_ffp_fragment_shader
218 struct ffp_frag_desc entry
;
220 struct list linked_programs
;
223 struct glsl_ffp_destroy_ctx
225 struct shader_glsl_priv
*priv
;
226 const struct wined3d_gl_info
*gl_info
;
229 static const char *debug_gl_shader_type(GLenum type
)
233 #define WINED3D_TO_STR(u) case u: return #u
234 WINED3D_TO_STR(GL_VERTEX_SHADER
);
235 WINED3D_TO_STR(GL_GEOMETRY_SHADER
);
236 WINED3D_TO_STR(GL_FRAGMENT_SHADER
);
237 #undef WINED3D_TO_STR
239 return wine_dbg_sprintf("UNKNOWN(%#x)", type
);
243 static const char *shader_glsl_get_prefix(enum wined3d_shader_type type
)
247 case WINED3D_SHADER_TYPE_VERTEX
:
250 case WINED3D_SHADER_TYPE_GEOMETRY
:
253 case WINED3D_SHADER_TYPE_PIXEL
:
257 FIXME("Unhandled shader type %#x.\n", type
);
262 static const char *shader_glsl_get_version(const struct wined3d_gl_info
*gl_info
,
263 const struct wined3d_shader_version
*version
)
265 if (gl_info
->glsl_version
>= MAKEDWORD_VERSION(1, 30) && version
->major
>= 4)
266 return "#version 130";
268 return "#version 120";
271 static void shader_glsl_append_imm_vec4(struct wined3d_string_buffer
*buffer
, const float *values
)
275 wined3d_ftoa(values
[0], str
[0]);
276 wined3d_ftoa(values
[1], str
[1]);
277 wined3d_ftoa(values
[2], str
[2]);
278 wined3d_ftoa(values
[3], str
[3]);
279 shader_addline(buffer
, "vec4(%s, %s, %s, %s)", str
[0], str
[1], str
[2], str
[3]);
282 static const char *get_info_log_line(const char **ptr
)
287 if (!(q
= strstr(p
, "\n")))
289 if (!*p
) return NULL
;
298 /* Context activation is done by the caller. */
299 static void print_glsl_info_log(const struct wined3d_gl_info
*gl_info
, GLuint id
, BOOL program
)
304 if (!WARN_ON(d3d_shader
) && !FIXME_ON(d3d_shader
))
308 GL_EXTCALL(glGetProgramiv(id
, GL_INFO_LOG_LENGTH
, &length
));
310 GL_EXTCALL(glGetShaderiv(id
, GL_INFO_LOG_LENGTH
, &length
));
312 /* A size of 1 is just a null-terminated string, so the log should be bigger than
313 * that if there are errors. */
316 const char *ptr
, *line
;
318 log
= HeapAlloc(GetProcessHeap(), 0, length
);
319 /* The info log is supposed to be zero-terminated, but at least some
320 * versions of fglrx don't terminate the string properly. The reported
321 * length does include the terminator, so explicitly set it to zero
325 GL_EXTCALL(glGetProgramInfoLog(id
, length
, NULL
, log
));
327 GL_EXTCALL(glGetShaderInfoLog(id
, length
, NULL
, log
));
330 if (gl_info
->quirks
& WINED3D_QUIRK_INFO_LOG_SPAM
)
332 WARN("Info log received from GLSL shader #%u:\n", id
);
333 while ((line
= get_info_log_line(&ptr
))) WARN(" %.*s", (int)(ptr
- line
), line
);
337 FIXME("Info log received from GLSL shader #%u:\n", id
);
338 while ((line
= get_info_log_line(&ptr
))) FIXME(" %.*s", (int)(ptr
- line
), line
);
340 HeapFree(GetProcessHeap(), 0, log
);
344 /* Context activation is done by the caller. */
345 static void shader_glsl_compile(const struct wined3d_gl_info
*gl_info
, GLuint shader
, const char *src
)
347 const char *ptr
, *line
;
349 TRACE("Compiling shader object %u.\n", shader
);
351 if (TRACE_ON(d3d_shader
))
354 while ((line
= get_info_log_line(&ptr
))) TRACE_(d3d_shader
)(" %.*s", (int)(ptr
- line
), line
);
357 GL_EXTCALL(glShaderSource(shader
, 1, &src
, NULL
));
358 checkGLcall("glShaderSource");
359 GL_EXTCALL(glCompileShader(shader
));
360 checkGLcall("glCompileShader");
361 print_glsl_info_log(gl_info
, shader
, FALSE
);
364 /* Context activation is done by the caller. */
365 static void shader_glsl_dump_program_source(const struct wined3d_gl_info
*gl_info
, GLuint program
)
367 GLint i
, shader_count
, source_size
= -1;
371 GL_EXTCALL(glGetProgramiv(program
, GL_ATTACHED_SHADERS
, &shader_count
));
372 shaders
= HeapAlloc(GetProcessHeap(), 0, shader_count
* sizeof(*shaders
));
375 ERR("Failed to allocate shader array memory.\n");
379 GL_EXTCALL(glGetAttachedShaders(program
, shader_count
, NULL
, shaders
));
380 for (i
= 0; i
< shader_count
; ++i
)
382 const char *ptr
, *line
;
385 GL_EXTCALL(glGetShaderiv(shaders
[i
], GL_SHADER_SOURCE_LENGTH
, &tmp
));
387 if (source_size
< tmp
)
389 HeapFree(GetProcessHeap(), 0, source
);
391 source
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, tmp
);
394 ERR("Failed to allocate %d bytes for shader source.\n", tmp
);
395 HeapFree(GetProcessHeap(), 0, shaders
);
401 FIXME("Shader %u:\n", shaders
[i
]);
402 GL_EXTCALL(glGetShaderiv(shaders
[i
], GL_SHADER_TYPE
, &tmp
));
403 FIXME(" GL_SHADER_TYPE: %s.\n", debug_gl_shader_type(tmp
));
404 GL_EXTCALL(glGetShaderiv(shaders
[i
], GL_COMPILE_STATUS
, &tmp
));
405 FIXME(" GL_COMPILE_STATUS: %d.\n", tmp
);
409 GL_EXTCALL(glGetShaderSource(shaders
[i
], source_size
, NULL
, source
));
410 while ((line
= get_info_log_line(&ptr
))) FIXME(" %.*s", (int)(ptr
- line
), line
);
414 HeapFree(GetProcessHeap(), 0, source
);
415 HeapFree(GetProcessHeap(), 0, shaders
);
418 /* Context activation is done by the caller. */
419 static void shader_glsl_validate_link(const struct wined3d_gl_info
*gl_info
, GLuint program
)
423 if (!TRACE_ON(d3d_shader
) && !FIXME_ON(d3d_shader
))
426 GL_EXTCALL(glGetProgramiv(program
, GL_LINK_STATUS
, &tmp
));
429 FIXME("Program %u link status invalid.\n", program
);
430 shader_glsl_dump_program_source(gl_info
, program
);
433 print_glsl_info_log(gl_info
, program
, TRUE
);
436 /* Context activation is done by the caller. */
437 static void shader_glsl_load_samplers(const struct wined3d_gl_info
*gl_info
,
438 const DWORD
*tex_unit_map
, GLuint program_id
)
440 unsigned int mapped_unit
;
441 char sampler_name
[20];
448 enum wined3d_shader_type type
;
449 unsigned int base_idx
;
454 {WINED3D_SHADER_TYPE_PIXEL
, 0, MAX_FRAGMENT_SAMPLERS
},
455 {WINED3D_SHADER_TYPE_VERTEX
, MAX_FRAGMENT_SAMPLERS
, MAX_VERTEX_SAMPLERS
},
458 for (i
= 0; i
< ARRAY_SIZE(sampler_info
); ++i
)
460 prefix
= shader_glsl_get_prefix(sampler_info
[i
].type
);
462 for (j
= 0; j
< sampler_info
[i
].count
; ++j
)
464 snprintf(sampler_name
, sizeof(sampler_name
), "%s_sampler%u", prefix
, j
);
465 name_loc
= GL_EXTCALL(glGetUniformLocation(program_id
, sampler_name
));
469 mapped_unit
= tex_unit_map
[sampler_info
[i
].base_idx
+ j
];
470 if (mapped_unit
== WINED3D_UNMAPPED_STAGE
|| mapped_unit
>= gl_info
->limits
.combined_samplers
)
472 ERR("Trying to load sampler %s on unsupported unit %u.\n", sampler_name
, mapped_unit
);
476 TRACE("Loading sampler %s on unit %u.\n", sampler_name
, mapped_unit
);
477 GL_EXTCALL(glUniform1i(name_loc
, mapped_unit
));
480 checkGLcall("glUniform1i");
483 /* Context activation is done by the caller. */
484 static inline void walk_constant_heap(const struct wined3d_gl_info
*gl_info
, const float *constants
,
485 const GLint
*constant_locations
, const struct constant_heap
*heap
, unsigned char *stack
, DWORD version
)
487 unsigned int start
= ~0U, end
= 0;
489 unsigned int heap_idx
= 1;
492 if (heap
->entries
[heap_idx
].version
<= version
) return;
494 idx
= heap
->entries
[heap_idx
].idx
;
495 if (constant_locations
[idx
] != -1)
497 stack
[stack_idx
] = HEAP_NODE_TRAVERSE_LEFT
;
499 while (stack_idx
>= 0)
501 /* Note that we fall through to the next case statement. */
502 switch(stack
[stack_idx
])
504 case HEAP_NODE_TRAVERSE_LEFT
:
506 unsigned int left_idx
= heap_idx
<< 1;
507 if (left_idx
< heap
->size
&& heap
->entries
[left_idx
].version
> version
)
510 idx
= heap
->entries
[heap_idx
].idx
;
511 if (constant_locations
[idx
] != -1)
519 stack
[stack_idx
++] = HEAP_NODE_TRAVERSE_RIGHT
;
520 stack
[stack_idx
] = HEAP_NODE_TRAVERSE_LEFT
;
525 case HEAP_NODE_TRAVERSE_RIGHT
:
527 unsigned int right_idx
= (heap_idx
<< 1) + 1;
528 if (right_idx
< heap
->size
&& heap
->entries
[right_idx
].version
> version
)
530 heap_idx
= right_idx
;
531 idx
= heap
->entries
[heap_idx
].idx
;
532 if (constant_locations
[idx
] != -1)
540 stack
[stack_idx
++] = HEAP_NODE_POP
;
541 stack
[stack_idx
] = HEAP_NODE_TRAVERSE_LEFT
;
553 GL_EXTCALL(glUniform4fv(constant_locations
[start
], end
- start
+ 1, &constants
[start
* 4]));
554 checkGLcall("walk_constant_heap()");
557 /* Context activation is done by the caller. */
558 static inline void apply_clamped_constant(const struct wined3d_gl_info
*gl_info
, GLint location
, const GLfloat
*data
)
560 GLfloat clamped_constant
[4];
562 if (location
== -1) return;
564 clamped_constant
[0] = data
[0] < -1.0f
? -1.0f
: data
[0] > 1.0f
? 1.0f
: data
[0];
565 clamped_constant
[1] = data
[1] < -1.0f
? -1.0f
: data
[1] > 1.0f
? 1.0f
: data
[1];
566 clamped_constant
[2] = data
[2] < -1.0f
? -1.0f
: data
[2] > 1.0f
? 1.0f
: data
[2];
567 clamped_constant
[3] = data
[3] < -1.0f
? -1.0f
: data
[3] > 1.0f
? 1.0f
: data
[3];
569 GL_EXTCALL(glUniform4fv(location
, 1, clamped_constant
));
572 /* Context activation is done by the caller. */
573 static inline void walk_constant_heap_clamped(const struct wined3d_gl_info
*gl_info
, const float *constants
,
574 const GLint
*constant_locations
, const struct constant_heap
*heap
, unsigned char *stack
, DWORD version
)
577 unsigned int heap_idx
= 1;
580 if (heap
->entries
[heap_idx
].version
<= version
) return;
582 idx
= heap
->entries
[heap_idx
].idx
;
583 apply_clamped_constant(gl_info
, constant_locations
[idx
], &constants
[idx
* 4]);
584 stack
[stack_idx
] = HEAP_NODE_TRAVERSE_LEFT
;
586 while (stack_idx
>= 0)
588 /* Note that we fall through to the next case statement. */
589 switch(stack
[stack_idx
])
591 case HEAP_NODE_TRAVERSE_LEFT
:
593 unsigned int left_idx
= heap_idx
<< 1;
594 if (left_idx
< heap
->size
&& heap
->entries
[left_idx
].version
> version
)
597 idx
= heap
->entries
[heap_idx
].idx
;
598 apply_clamped_constant(gl_info
, constant_locations
[idx
], &constants
[idx
* 4]);
600 stack
[stack_idx
++] = HEAP_NODE_TRAVERSE_RIGHT
;
601 stack
[stack_idx
] = HEAP_NODE_TRAVERSE_LEFT
;
606 case HEAP_NODE_TRAVERSE_RIGHT
:
608 unsigned int right_idx
= (heap_idx
<< 1) + 1;
609 if (right_idx
< heap
->size
&& heap
->entries
[right_idx
].version
> version
)
611 heap_idx
= right_idx
;
612 idx
= heap
->entries
[heap_idx
].idx
;
613 apply_clamped_constant(gl_info
, constant_locations
[idx
], &constants
[idx
* 4]);
615 stack
[stack_idx
++] = HEAP_NODE_POP
;
616 stack
[stack_idx
] = HEAP_NODE_TRAVERSE_LEFT
;
627 checkGLcall("walk_constant_heap_clamped()");
630 /* Context activation is done by the caller. */
631 static void shader_glsl_load_constantsF(const struct wined3d_shader
*shader
, const struct wined3d_gl_info
*gl_info
,
632 const float *constants
, const GLint
*constant_locations
, const struct constant_heap
*heap
,
633 unsigned char *stack
, UINT version
)
635 const struct wined3d_shader_lconst
*lconst
;
637 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
638 if (shader
->reg_maps
.shader_version
.major
== 1
639 && shader
->reg_maps
.shader_version
.type
== WINED3D_SHADER_TYPE_PIXEL
)
640 walk_constant_heap_clamped(gl_info
, constants
, constant_locations
, heap
, stack
, version
);
642 walk_constant_heap(gl_info
, constants
, constant_locations
, heap
, stack
, version
);
644 if (!shader
->load_local_constsF
)
646 TRACE("No need to load local float constants for this shader\n");
650 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
651 LIST_FOR_EACH_ENTRY(lconst
, &shader
->constantsF
, struct wined3d_shader_lconst
, entry
)
653 GL_EXTCALL(glUniform4fv(constant_locations
[lconst
->idx
], 1, (const GLfloat
*)lconst
->value
));
655 checkGLcall("glUniform4fv()");
658 /* Context activation is done by the caller. */
659 static void shader_glsl_load_constantsI(const struct wined3d_shader
*shader
, const struct wined3d_gl_info
*gl_info
,
660 const GLint locations
[MAX_CONST_I
], const int *constants
, WORD constants_set
)
665 for (i
= 0; constants_set
; constants_set
>>= 1, ++i
)
667 if (!(constants_set
& 1)) continue;
669 /* We found this uniform name in the program - go ahead and send the data */
670 GL_EXTCALL(glUniform4iv(locations
[i
], 1, &constants
[i
* 4]));
673 /* Load immediate constants */
674 ptr
= list_head(&shader
->constantsI
);
677 const struct wined3d_shader_lconst
*lconst
= LIST_ENTRY(ptr
, const struct wined3d_shader_lconst
, entry
);
678 unsigned int idx
= lconst
->idx
;
679 const GLint
*values
= (const GLint
*)lconst
->value
;
681 /* We found this uniform name in the program - go ahead and send the data */
682 GL_EXTCALL(glUniform4iv(locations
[idx
], 1, values
));
683 ptr
= list_next(&shader
->constantsI
, ptr
);
685 checkGLcall("glUniform4iv()");
688 /* Context activation is done by the caller. */
689 static void shader_glsl_load_constantsB(const struct wined3d_shader
*shader
, const struct wined3d_gl_info
*gl_info
,
690 const GLint locations
[MAX_CONST_B
], const BOOL
*constants
, WORD constants_set
)
695 for (i
= 0; constants_set
; constants_set
>>= 1, ++i
)
697 if (!(constants_set
& 1)) continue;
699 GL_EXTCALL(glUniform1iv(locations
[i
], 1, &constants
[i
]));
702 /* Load immediate constants */
703 ptr
= list_head(&shader
->constantsB
);
706 const struct wined3d_shader_lconst
*lconst
= LIST_ENTRY(ptr
, const struct wined3d_shader_lconst
, entry
);
707 unsigned int idx
= lconst
->idx
;
708 const GLint
*values
= (const GLint
*)lconst
->value
;
710 GL_EXTCALL(glUniform1iv(locations
[idx
], 1, values
));
711 ptr
= list_next(&shader
->constantsB
, ptr
);
713 checkGLcall("glUniform1iv()");
716 static void reset_program_constant_version(struct wine_rb_entry
*entry
, void *context
)
718 WINE_RB_ENTRY_VALUE(entry
, struct glsl_shader_prog_link
, program_lookup_entry
)->constant_version
= 0;
721 /* Context activation is done by the caller (state handler). */
722 static void shader_glsl_load_np2fixup_constants(const struct glsl_ps_program
*ps
,
723 const struct wined3d_gl_info
*gl_info
, const struct wined3d_state
*state
)
725 GLfloat np2fixup_constants
[4 * MAX_FRAGMENT_SAMPLERS
];
726 UINT fixup
= ps
->np2_fixup_info
->active
;
729 for (i
= 0; fixup
; fixup
>>= 1, ++i
)
731 const struct wined3d_texture
*tex
= state
->textures
[i
];
732 unsigned char idx
= ps
->np2_fixup_info
->idx
[i
];
733 GLfloat
*tex_dim
= &np2fixup_constants
[(idx
>> 1) * 4];
737 ERR("Nonexistent texture is flagged for NP2 texcoord fixup.\n");
743 tex_dim
[2] = tex
->pow2_matrix
[0];
744 tex_dim
[3] = tex
->pow2_matrix
[5];
748 tex_dim
[0] = tex
->pow2_matrix
[0];
749 tex_dim
[1] = tex
->pow2_matrix
[5];
753 GL_EXTCALL(glUniform4fv(ps
->np2_fixup_location
, ps
->np2_fixup_info
->num_consts
, np2fixup_constants
));
756 /* Taken and adapted from Mesa. */
757 static BOOL
invert_matrix_3d(struct wined3d_matrix
*out
, const struct wined3d_matrix
*in
)
759 float pos
, neg
, t
, det
;
760 struct wined3d_matrix temp
;
762 /* Calculate the determinant of upper left 3x3 submatrix and
763 * determine if the matrix is singular. */
765 t
= in
->_11
* in
->_22
* in
->_33
;
771 t
= in
->_21
* in
->_32
* in
->_13
;
776 t
= in
->_31
* in
->_12
* in
->_23
;
782 t
= -in
->_31
* in
->_22
* in
->_13
;
787 t
= -in
->_21
* in
->_12
* in
->_33
;
793 t
= -in
->_11
* in
->_32
* in
->_23
;
801 if (fabsf(det
) < 1e-25f
)
805 temp
._11
= (in
->_22
* in
->_33
- in
->_32
* in
->_23
) * det
;
806 temp
._12
= -(in
->_12
* in
->_33
- in
->_32
* in
->_13
) * det
;
807 temp
._13
= (in
->_12
* in
->_23
- in
->_22
* in
->_13
) * det
;
808 temp
._21
= -(in
->_21
* in
->_33
- in
->_31
* in
->_23
) * det
;
809 temp
._22
= (in
->_11
* in
->_33
- in
->_31
* in
->_13
) * det
;
810 temp
._23
= -(in
->_11
* in
->_23
- in
->_21
* in
->_13
) * det
;
811 temp
._31
= (in
->_21
* in
->_32
- in
->_31
* in
->_22
) * det
;
812 temp
._32
= -(in
->_11
* in
->_32
- in
->_31
* in
->_12
) * det
;
813 temp
._33
= (in
->_11
* in
->_22
- in
->_21
* in
->_12
) * det
;
819 static void swap_rows(float **a
, float **b
)
827 static BOOL
invert_matrix(struct wined3d_matrix
*out
, struct wined3d_matrix
*m
)
830 float m0
, m1
, m2
, m3
, s
;
831 float *r0
, *r1
, *r2
, *r3
;
843 r0
[5] = r0
[6] = r0
[7] = 0.0f
;
850 r1
[4] = r1
[6] = r1
[7] = 0.0f
;
857 r2
[4] = r2
[5] = r2
[7] = 0.0f
;
864 r3
[4] = r3
[5] = r3
[6] = 0.0f
;
866 /* Choose pivot - or die. */
867 if (fabsf(r3
[0]) > fabsf(r2
[0]))
869 if (fabsf(r2
[0]) > fabsf(r1
[0]))
871 if (fabsf(r1
[0]) > fabsf(r0
[0]))
876 /* Eliminate first variable. */
877 m1
= r1
[0] / r0
[0]; m2
= r2
[0] / r0
[0]; m3
= r3
[0] / r0
[0];
878 s
= r0
[1]; r1
[1] -= m1
* s
; r2
[1] -= m2
* s
; r3
[1] -= m3
* s
;
879 s
= r0
[2]; r1
[2] -= m1
* s
; r2
[2] -= m2
* s
; r3
[2] -= m3
* s
;
880 s
= r0
[3]; r1
[3] -= m1
* s
; r2
[3] -= m2
* s
; r3
[3] -= m3
* s
;
910 /* Choose pivot - or die. */
911 if (fabsf(r3
[1]) > fabsf(r2
[1]))
913 if (fabsf(r2
[1]) > fabsf(r1
[1]))
918 /* Eliminate second variable. */
919 m2
= r2
[1] / r1
[1]; m3
= r3
[1] / r1
[1];
920 r2
[2] -= m2
* r1
[2]; r3
[2] -= m3
* r1
[2];
921 r2
[3] -= m2
* r1
[3]; r3
[3] -= m3
* r1
[3];
947 /* Choose pivot - or die. */
948 if (fabsf(r3
[2]) > fabsf(r2
[2]))
953 /* Eliminate third variable. */
965 /* Back substitute row 3. */
972 /* Back substitute row 2. */
975 r2
[4] = s
* (r2
[4] - r3
[4] * m2
);
976 r2
[5] = s
* (r2
[5] - r3
[5] * m2
);
977 r2
[6] = s
* (r2
[6] - r3
[6] * m2
);
978 r2
[7] = s
* (r2
[7] - r3
[7] * m2
);
990 /* Back substitute row 1. */
993 r1
[4] = s
* (r1
[4] - r2
[4] * m1
);
994 r1
[5] = s
* (r1
[5] - r2
[5] * m1
);
995 r1
[6] = s
* (r1
[6] - r2
[6] * m1
);
996 r1
[7] = s
* (r1
[7] - r2
[7] * m1
);
1000 r0
[6] -= r2
[6] * m0
;
1001 r0
[7] -= r2
[7] * m0
;
1003 /* Back substitute row 0. */
1006 r0
[4] = s
* (r0
[4] - r1
[4] * m0
);
1007 r0
[5] = s
* (r0
[5] - r1
[5] * m0
);
1008 r0
[6] = s
* (r0
[6] - r1
[6] * m0
);
1009 r0
[7] = s
* (r0
[7] - r1
[7] * m0
);
1031 static void shader_glsl_ffp_vertex_normalmatrix_uniform(const struct wined3d_context
*context
,
1032 const struct wined3d_state
*state
, struct glsl_shader_prog_link
*prog
)
1034 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1036 struct wined3d_matrix mv
;
1039 get_modelview_matrix(context
, state
, &mv
);
1040 if (context
->swapchain
->device
->wined3d
->flags
& WINED3D_LEGACY_FFP_LIGHTING
)
1041 invert_matrix_3d(&mv
, &mv
);
1043 invert_matrix(&mv
, &mv
);
1044 /* Tests show that singular modelview matrices are used unchanged as normal
1045 * matrices on D3D3 and older. There seems to be no clearly consistent
1046 * behavior on newer D3D versions so always follow older ddraw behavior. */
1047 for (i
= 0; i
< 3; ++i
)
1048 for (j
= 0; j
< 3; ++j
)
1049 mat
[i
* 3 + j
] = (&mv
._11
)[j
* 4 + i
];
1051 GL_EXTCALL(glUniformMatrix3fv(prog
->vs
.normal_matrix_location
, 1, FALSE
, mat
));
1052 checkGLcall("glUniformMatrix3fv");
1055 /* Context activation is done by the caller (state handler). */
1056 static void shader_glsl_load_color_key_constant(const struct glsl_ps_program
*ps
,
1057 const struct wined3d_gl_info
*gl_info
, const struct wined3d_state
*state
)
1059 struct wined3d_color float_key
;
1060 const struct wined3d_texture
*texture
= state
->textures
[0];
1062 wined3d_format_convert_color_to_float(texture
->resource
.format
, NULL
,
1063 texture
->async
.src_blt_color_key
.color_space_high_value
, &float_key
);
1064 GL_EXTCALL(glUniform4fv(ps
->color_key_location
, 1, &float_key
.r
));
1067 /* Context activation is done by the caller (state handler). */
1068 static void shader_glsl_load_constants(void *shader_priv
, struct wined3d_context
*context
,
1069 const struct wined3d_state
*state
)
1071 const struct glsl_context_data
*ctx_data
= context
->shader_backend_data
;
1072 const struct wined3d_shader
*vshader
= state
->shader
[WINED3D_SHADER_TYPE_VERTEX
];
1073 const struct wined3d_shader
*pshader
= state
->shader
[WINED3D_SHADER_TYPE_PIXEL
];
1074 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1075 struct shader_glsl_priv
*priv
= shader_priv
;
1076 float position_fixup
[4];
1077 DWORD update_mask
= 0;
1079 struct glsl_shader_prog_link
*prog
= ctx_data
->glsl_program
;
1080 UINT constant_version
;
1084 /* No GLSL program set - nothing to do. */
1087 constant_version
= prog
->constant_version
;
1088 update_mask
= context
->constant_update_mask
& prog
->constant_update_mask
;
1090 if (update_mask
& WINED3D_SHADER_CONST_VS_F
)
1091 shader_glsl_load_constantsF(vshader
, gl_info
, state
->vs_consts_f
,
1092 prog
->vs
.uniform_f_locations
, &priv
->vconst_heap
, priv
->stack
, constant_version
);
1094 if (update_mask
& WINED3D_SHADER_CONST_VS_I
)
1095 shader_glsl_load_constantsI(vshader
, gl_info
, prog
->vs
.uniform_i_locations
, state
->vs_consts_i
,
1096 vshader
->reg_maps
.integer_constants
);
1098 if (update_mask
& WINED3D_SHADER_CONST_VS_B
)
1099 shader_glsl_load_constantsB(vshader
, gl_info
, prog
->vs
.uniform_b_locations
, state
->vs_consts_b
,
1100 vshader
->reg_maps
.boolean_constants
);
1102 if (update_mask
& WINED3D_SHADER_CONST_VS_POS_FIXUP
)
1104 shader_get_position_fixup(context
, state
, position_fixup
);
1105 GL_EXTCALL(glUniform4fv(prog
->vs
.pos_fixup_location
, 1, position_fixup
));
1106 checkGLcall("glUniform4fv");
1109 if (update_mask
& WINED3D_SHADER_CONST_FFP_MODELVIEW
)
1111 struct wined3d_matrix mat
;
1113 get_modelview_matrix(context
, state
, &mat
);
1114 GL_EXTCALL(glUniformMatrix4fv(prog
->vs
.modelview_matrix_location
, 1, FALSE
, &mat
._11
));
1115 checkGLcall("glUniformMatrix4fv");
1117 shader_glsl_ffp_vertex_normalmatrix_uniform(context
, state
, prog
);
1120 if (update_mask
& WINED3D_SHADER_CONST_FFP_PROJ
)
1122 struct wined3d_matrix projection
;
1124 get_projection_matrix(context
, state
, &projection
);
1125 GL_EXTCALL(glUniformMatrix4fv(prog
->vs
.projection_matrix_location
, 1, FALSE
, &projection
._11
));
1126 checkGLcall("glUniformMatrix4fv");
1129 if (update_mask
& WINED3D_SHADER_CONST_PS_F
)
1130 shader_glsl_load_constantsF(pshader
, gl_info
, state
->ps_consts_f
,
1131 prog
->ps
.uniform_f_locations
, &priv
->pconst_heap
, priv
->stack
, constant_version
);
1133 if (update_mask
& WINED3D_SHADER_CONST_PS_I
)
1134 shader_glsl_load_constantsI(pshader
, gl_info
, prog
->ps
.uniform_i_locations
, state
->ps_consts_i
,
1135 pshader
->reg_maps
.integer_constants
);
1137 if (update_mask
& WINED3D_SHADER_CONST_PS_B
)
1138 shader_glsl_load_constantsB(pshader
, gl_info
, prog
->ps
.uniform_b_locations
, state
->ps_consts_b
,
1139 pshader
->reg_maps
.boolean_constants
);
1141 if (update_mask
& WINED3D_SHADER_CONST_PS_BUMP_ENV
)
1143 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
1145 if (prog
->ps
.bumpenv_mat_location
[i
] == -1)
1148 GL_EXTCALL(glUniformMatrix2fv(prog
->ps
.bumpenv_mat_location
[i
], 1, 0,
1149 (const GLfloat
*)&state
->texture_states
[i
][WINED3D_TSS_BUMPENV_MAT00
]));
1151 if (prog
->ps
.bumpenv_lum_scale_location
[i
] != -1)
1153 GL_EXTCALL(glUniform1fv(prog
->ps
.bumpenv_lum_scale_location
[i
], 1,
1154 (const GLfloat
*)&state
->texture_states
[i
][WINED3D_TSS_BUMPENV_LSCALE
]));
1155 GL_EXTCALL(glUniform1fv(prog
->ps
.bumpenv_lum_offset_location
[i
], 1,
1156 (const GLfloat
*)&state
->texture_states
[i
][WINED3D_TSS_BUMPENV_LOFFSET
]));
1160 checkGLcall("bump env uniforms");
1163 if (update_mask
& WINED3D_SHADER_CONST_PS_Y_CORR
)
1165 float correction_params
[] =
1167 /* position is window relative, not viewport relative */
1168 context
->render_offscreen
? 0.0f
: (float)context
->current_rt
->resource
.height
,
1169 context
->render_offscreen
? 1.0f
: -1.0f
,
1174 GL_EXTCALL(glUniform4fv(prog
->ps
.ycorrection_location
, 1, correction_params
));
1177 if (update_mask
& WINED3D_SHADER_CONST_PS_NP2_FIXUP
)
1178 shader_glsl_load_np2fixup_constants(&prog
->ps
, gl_info
, state
);
1179 if (update_mask
& WINED3D_SHADER_CONST_FFP_COLOR_KEY
)
1180 shader_glsl_load_color_key_constant(&prog
->ps
, gl_info
, state
);
1182 if (update_mask
& WINED3D_SHADER_CONST_FFP_PS
)
1186 if (prog
->ps
.tex_factor_location
!= -1)
1188 D3DCOLORTOGLFLOAT4(state
->render_states
[WINED3D_RS_TEXTUREFACTOR
], col
);
1189 GL_EXTCALL(glUniform4fv(prog
->ps
.tex_factor_location
, 1, col
));
1192 if (state
->render_states
[WINED3D_RS_SPECULARENABLE
])
1193 GL_EXTCALL(glUniform4f(prog
->ps
.specular_enable_location
, 1.0f
, 1.0f
, 1.0f
, 0.0f
));
1195 GL_EXTCALL(glUniform4f(prog
->ps
.specular_enable_location
, 0.0f
, 0.0f
, 0.0f
, 0.0f
));
1197 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
1199 if (prog
->ps
.tss_constant_location
[i
] == -1)
1202 D3DCOLORTOGLFLOAT4(state
->texture_states
[i
][WINED3D_TSS_CONSTANT
], col
);
1203 GL_EXTCALL(glUniform4fv(prog
->ps
.tss_constant_location
[i
], 1, col
));
1206 checkGLcall("fixed function uniforms");
1209 if (priv
->next_constant_version
== UINT_MAX
)
1211 TRACE("Max constant version reached, resetting to 0.\n");
1212 wine_rb_for_each_entry(&priv
->program_lookup
, reset_program_constant_version
, NULL
);
1213 priv
->next_constant_version
= 1;
1217 prog
->constant_version
= priv
->next_constant_version
++;
1221 static void update_heap_entry(struct constant_heap
*heap
, unsigned int idx
, DWORD new_version
)
1223 struct constant_entry
*entries
= heap
->entries
;
1224 unsigned int *positions
= heap
->positions
;
1225 unsigned int heap_idx
, parent_idx
;
1227 if (!heap
->contained
[idx
])
1229 heap_idx
= heap
->size
++;
1230 heap
->contained
[idx
] = TRUE
;
1234 heap_idx
= positions
[idx
];
1237 while (heap_idx
> 1)
1239 parent_idx
= heap_idx
>> 1;
1241 if (new_version
<= entries
[parent_idx
].version
) break;
1243 entries
[heap_idx
] = entries
[parent_idx
];
1244 positions
[entries
[parent_idx
].idx
] = heap_idx
;
1245 heap_idx
= parent_idx
;
1248 entries
[heap_idx
].version
= new_version
;
1249 entries
[heap_idx
].idx
= idx
;
1250 positions
[idx
] = heap_idx
;
1253 static void shader_glsl_update_float_vertex_constants(struct wined3d_device
*device
, UINT start
, UINT count
)
1255 struct shader_glsl_priv
*priv
= device
->shader_priv
;
1256 struct constant_heap
*heap
= &priv
->vconst_heap
;
1259 for (i
= start
; i
< count
+ start
; ++i
)
1261 update_heap_entry(heap
, i
, priv
->next_constant_version
);
1264 for (i
= 0; i
< device
->context_count
; ++i
)
1266 device
->contexts
[i
]->constant_update_mask
|= WINED3D_SHADER_CONST_VS_F
;
1270 static void shader_glsl_update_float_pixel_constants(struct wined3d_device
*device
, UINT start
, UINT count
)
1272 struct shader_glsl_priv
*priv
= device
->shader_priv
;
1273 struct constant_heap
*heap
= &priv
->pconst_heap
;
1276 for (i
= start
; i
< count
+ start
; ++i
)
1278 update_heap_entry(heap
, i
, priv
->next_constant_version
);
1281 for (i
= 0; i
< device
->context_count
; ++i
)
1283 device
->contexts
[i
]->constant_update_mask
|= WINED3D_SHADER_CONST_PS_F
;
1287 static unsigned int vec4_varyings(DWORD shader_major
, const struct wined3d_gl_info
*gl_info
)
1289 unsigned int ret
= gl_info
->limits
.glsl_varyings
/ 4;
1290 /* 4.0 shaders do not write clip coords because d3d10 does not support user clipplanes */
1291 if(shader_major
> 3) return ret
;
1293 /* 3.0 shaders may need an extra varying for the clip coord on some cards(mostly dx10 ones) */
1294 if (gl_info
->quirks
& WINED3D_QUIRK_GLSL_CLIP_VARYING
) ret
-= 1;
1298 /** Generate the variable & register declarations for the GLSL output target */
1299 static void shader_generate_glsl_declarations(const struct wined3d_context
*context
,
1300 struct wined3d_string_buffer
*buffer
, const struct wined3d_shader
*shader
,
1301 const struct wined3d_shader_reg_maps
*reg_maps
, const struct shader_glsl_ctx_priv
*ctx_priv
)
1303 const struct wined3d_shader_version
*version
= ®_maps
->shader_version
;
1304 const struct wined3d_state
*state
= &shader
->device
->state
;
1305 const struct ps_compile_args
*ps_args
= ctx_priv
->cur_ps_args
;
1306 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1307 const struct wined3d_fb_state
*fb
= &shader
->device
->fb
;
1308 unsigned int i
, extra_constants_needed
= 0;
1309 const struct wined3d_shader_lconst
*lconst
;
1313 prefix
= shader_glsl_get_prefix(version
->type
);
1315 /* Prototype the subroutines */
1316 for (i
= 0, map
= reg_maps
->labels
; map
; map
>>= 1, ++i
)
1318 if (map
& 1) shader_addline(buffer
, "void subroutine%u();\n", i
);
1321 /* Declare the constants (aka uniforms) */
1322 if (shader
->limits
->constant_float
> 0)
1324 unsigned max_constantsF
;
1326 /* Unless the shader uses indirect addressing, always declare the
1327 * maximum array size and ignore that we need some uniforms privately.
1328 * E.g. if GL supports 256 uniforms, and we need 2 for the pos fixup
1329 * and immediate values, still declare VC[256]. If the shader needs
1330 * more uniforms than we have it won't work in any case. If it uses
1331 * less, the compiler will figure out which uniforms are really used
1332 * and strip them out. This allows a shader to use c255 on a dx9 card,
1333 * as long as it doesn't also use all the other constants.
1335 * If the shader uses indirect addressing the compiler must assume
1336 * that all declared uniforms are used. In this case, declare only the
1337 * amount that we're assured to have.
1339 * Thus we run into problems in these two cases:
1340 * 1) The shader really uses more uniforms than supported.
1341 * 2) The shader uses indirect addressing, less constants than
1342 * supported, but uses a constant index > #supported consts. */
1343 if (version
->type
== WINED3D_SHADER_TYPE_PIXEL
)
1345 /* No indirect addressing here. */
1346 max_constantsF
= gl_info
->limits
.glsl_ps_float_constants
;
1350 if (reg_maps
->usesrelconstF
)
1352 /* Subtract the other potential uniforms from the max
1353 * available (bools, ints, and 1 row of projection matrix).
1354 * Subtract another uniform for immediate values, which have
1355 * to be loaded via uniform by the driver as well. The shader
1356 * code only uses 0.5, 2.0, 1.0, 128 and -128 in vertex
1357 * shader code, so one vec4 should be enough. (Unfortunately
1358 * the Nvidia driver doesn't store 128 and -128 in one float).
1360 * Writing gl_ClipVertex requires one uniform for each
1361 * clipplane as well. */
1362 max_constantsF
= gl_info
->limits
.glsl_vs_float_constants
- 3;
1363 if(ctx_priv
->cur_vs_args
->clip_enabled
)
1365 max_constantsF
-= gl_info
->limits
.clipplanes
;
1367 max_constantsF
-= count_bits(reg_maps
->integer_constants
);
1368 /* Strictly speaking a bool only uses one scalar, but the nvidia(Linux) compiler doesn't pack them properly,
1369 * so each scalar requires a full vec4. We could work around this by packing the booleans ourselves, but
1370 * for now take this into account when calculating the number of available constants
1372 max_constantsF
-= count_bits(reg_maps
->boolean_constants
);
1373 /* Set by driver quirks in directx.c */
1374 max_constantsF
-= gl_info
->reserved_glsl_constants
;
1376 if (max_constantsF
< shader
->limits
->constant_float
)
1378 static unsigned int once
;
1381 ERR_(winediag
)("The hardware does not support enough uniform components to run this shader,"
1382 " it may not render correctly.\n");
1384 WARN("The hardware does not support enough uniform components to run this shader.\n");
1389 max_constantsF
= gl_info
->limits
.glsl_vs_float_constants
;
1392 max_constantsF
= min(shader
->limits
->constant_float
, max_constantsF
);
1393 shader_addline(buffer
, "uniform vec4 %s_c[%u];\n", prefix
, max_constantsF
);
1396 /* Always declare the full set of constants, the compiler can remove the
1397 * unused ones because d3d doesn't (yet) support indirect int and bool
1398 * constant addressing. This avoids problems if the app uses e.g. i0 and i9. */
1399 if (shader
->limits
->constant_int
> 0 && reg_maps
->integer_constants
)
1400 shader_addline(buffer
, "uniform ivec4 %s_i[%u];\n", prefix
, shader
->limits
->constant_int
);
1402 if (shader
->limits
->constant_bool
> 0 && reg_maps
->boolean_constants
)
1403 shader_addline(buffer
, "uniform bool %s_b[%u];\n", prefix
, shader
->limits
->constant_bool
);
1405 for (i
= 0; i
< WINED3D_MAX_CBS
; ++i
)
1407 if (reg_maps
->cb_sizes
[i
])
1408 shader_addline(buffer
, "layout(std140) uniform block_%s_cb%u { vec4 %s_cb%u[%u]; };\n",
1409 prefix
, i
, prefix
, i
, reg_maps
->cb_sizes
[i
]);
1412 /* Declare texture samplers */
1413 for (i
= 0; i
< reg_maps
->sampler_map
.count
; ++i
)
1415 struct wined3d_shader_sampler_map_entry
*entry
;
1416 BOOL shadow_sampler
, tex_rect
;
1417 const char *sampler_type
;
1419 entry
= ®_maps
->sampler_map
.entries
[i
];
1421 if (entry
->resource_idx
>= ARRAY_SIZE(reg_maps
->resource_info
))
1423 ERR("Invalid resource index %u.\n", entry
->resource_idx
);
1427 shadow_sampler
= version
->type
== WINED3D_SHADER_TYPE_PIXEL
&& (ps_args
->shadow
& (1 << entry
->sampler_idx
));
1428 switch (reg_maps
->resource_info
[entry
->resource_idx
].type
)
1430 case WINED3D_SHADER_RESOURCE_TEXTURE_1D
:
1432 sampler_type
= "sampler1DShadow";
1434 sampler_type
= "sampler1D";
1437 case WINED3D_SHADER_RESOURCE_TEXTURE_2D
:
1438 tex_rect
= version
->type
== WINED3D_SHADER_TYPE_PIXEL
1439 && (ps_args
->np2_fixup
& (1 << entry
->resource_idx
))
1440 && gl_info
->supported
[ARB_TEXTURE_RECTANGLE
];
1444 sampler_type
= "sampler2DRectShadow";
1446 sampler_type
= "sampler2DShadow";
1451 sampler_type
= "sampler2DRect";
1453 sampler_type
= "sampler2D";
1457 case WINED3D_SHADER_RESOURCE_TEXTURE_3D
:
1459 FIXME("Unsupported 3D shadow sampler.\n");
1460 sampler_type
= "sampler3D";
1463 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE
:
1465 FIXME("Unsupported Cube shadow sampler.\n");
1466 sampler_type
= "samplerCube";
1470 sampler_type
= "unsupported_sampler";
1471 FIXME("Unhandled resource type %#x.\n", reg_maps
->resource_info
[i
].type
);
1474 shader_addline(buffer
, "uniform %s %s_sampler%u;\n", sampler_type
, prefix
, entry
->bind_idx
);
1477 /* Declare uniforms for NP2 texcoord fixup:
1478 * This is NOT done inside the loop that declares the texture samplers
1479 * since the NP2 fixup code is currently only used for the GeforceFX
1480 * series and when forcing the ARB_npot extension off. Modern cards just
1481 * skip the code anyway, so put it inside a separate loop. */
1482 if (version
->type
== WINED3D_SHADER_TYPE_PIXEL
&& ps_args
->np2_fixup
)
1484 struct ps_np2fixup_info
*fixup
= ctx_priv
->cur_np2fixup_info
;
1487 /* NP2/RECT textures in OpenGL use texcoords in the range [0,width]x[0,height]
1488 * while D3D has them in the (normalized) [0,1]x[0,1] range.
1489 * samplerNP2Fixup stores texture dimensions and is updated through
1490 * shader_glsl_load_np2fixup_constants when the sampler changes. */
1492 for (i
= 0; i
< shader
->limits
->sampler
; ++i
)
1494 if (!reg_maps
->resource_info
[i
].type
|| !(ps_args
->np2_fixup
& (1 << i
)))
1497 if (reg_maps
->resource_info
[i
].type
!= WINED3D_SHADER_RESOURCE_TEXTURE_2D
)
1499 FIXME("Non-2D texture is flagged for NP2 texcoord fixup.\n");
1503 fixup
->idx
[i
] = cur
++;
1506 fixup
->num_consts
= (cur
+ 1) >> 1;
1507 fixup
->active
= ps_args
->np2_fixup
;
1508 shader_addline(buffer
, "uniform vec4 %s_samplerNP2Fixup[%u];\n", prefix
, fixup
->num_consts
);
1511 /* Declare address variables */
1512 for (i
= 0, map
= reg_maps
->address
; map
; map
>>= 1, ++i
)
1514 if (map
& 1) shader_addline(buffer
, "ivec4 A%u;\n", i
);
1517 /* Declare texture coordinate temporaries and initialize them */
1518 for (i
= 0, map
= reg_maps
->texcoord
; map
; map
>>= 1, ++i
)
1520 if (map
& 1) shader_addline(buffer
, "vec4 T%u = gl_TexCoord[%u];\n", i
, i
);
1523 if (version
->type
== WINED3D_SHADER_TYPE_VERTEX
)
1525 for (i
= 0; i
< shader
->input_signature
.element_count
; ++i
)
1527 const struct wined3d_shader_signature_element
*e
= &shader
->input_signature
.elements
[i
];
1528 if (e
->sysval_semantic
== WINED3D_SV_INSTANCEID
)
1529 shader_addline(buffer
, "vec4 %s_in%u = vec4(intBitsToFloat(gl_InstanceID), 0.0, 0.0, 0.0);\n",
1530 prefix
, e
->register_idx
);
1532 shader_addline(buffer
, "attribute vec4 %s_in%u;\n", prefix
, e
->register_idx
);
1535 shader_addline(buffer
, "uniform vec4 posFixup;\n");
1536 shader_addline(buffer
, "void order_ps_input(in vec4[%u]);\n", shader
->limits
->packed_output
);
1538 else if (version
->type
== WINED3D_SHADER_TYPE_GEOMETRY
)
1540 shader_addline(buffer
, "varying in vec4 gs_in[][%u];\n", shader
->limits
->packed_input
);
1542 else if (version
->type
== WINED3D_SHADER_TYPE_PIXEL
)
1544 if (version
->major
>= 3)
1546 UINT in_count
= min(vec4_varyings(version
->major
, gl_info
), shader
->limits
->packed_input
);
1549 shader_addline(buffer
, "varying vec4 %s_link[%u];\n", prefix
, in_count
);
1550 shader_addline(buffer
, "vec4 %s_in[%u];\n", prefix
, in_count
);
1553 for (i
= 0, map
= reg_maps
->bumpmat
; map
; map
>>= 1, ++i
)
1558 shader_addline(buffer
, "uniform mat2 bumpenv_mat%u;\n", i
);
1560 if (reg_maps
->luminanceparams
& (1 << i
))
1562 shader_addline(buffer
, "uniform float bumpenv_lum_scale%u;\n", i
);
1563 shader_addline(buffer
, "uniform float bumpenv_lum_offset%u;\n", i
);
1564 extra_constants_needed
++;
1567 extra_constants_needed
++;
1570 if (ps_args
->srgb_correction
)
1572 shader_addline(buffer
, "const vec4 srgb_const0 = ");
1573 shader_glsl_append_imm_vec4(buffer
, wined3d_srgb_const0
);
1574 shader_addline(buffer
, ";\n");
1575 shader_addline(buffer
, "const vec4 srgb_const1 = ");
1576 shader_glsl_append_imm_vec4(buffer
, wined3d_srgb_const1
);
1577 shader_addline(buffer
, ";\n");
1579 if (reg_maps
->vpos
|| reg_maps
->usesdsy
)
1581 if (shader
->limits
->constant_float
+ extra_constants_needed
1582 + 1 < gl_info
->limits
.glsl_ps_float_constants
)
1584 shader_addline(buffer
, "uniform vec4 ycorrection;\n");
1585 extra_constants_needed
++;
1589 float ycorrection
[] =
1591 context
->render_offscreen
? 0.0f
: fb
->render_targets
[0]->height
,
1592 context
->render_offscreen
? 1.0f
: -1.0f
,
1597 /* This happens because we do not have proper tracking of the
1598 * constant registers that are actually used, only the max
1599 * limit of the shader version. */
1600 FIXME("Cannot find a free uniform for vpos correction params\n");
1601 shader_addline(buffer
, "const vec4 ycorrection = ");
1602 shader_glsl_append_imm_vec4(buffer
, ycorrection
);
1603 shader_addline(buffer
, ";\n");
1605 shader_addline(buffer
, "vec4 vpos;\n");
1609 /* Declare output register temporaries */
1610 if (shader
->limits
->packed_output
)
1611 shader_addline(buffer
, "vec4 %s_out[%u];\n", prefix
, shader
->limits
->packed_output
);
1613 /* Declare temporary variables */
1614 for (i
= 0, map
= reg_maps
->temporary
; map
; map
>>= 1, ++i
)
1616 if (map
& 1) shader_addline(buffer
, "vec4 R%u;\n", i
);
1619 /* Declare loop registers aLx */
1620 if (version
->major
< 4)
1622 for (i
= 0; i
< reg_maps
->loop_depth
; ++i
)
1624 shader_addline(buffer
, "int aL%u;\n", i
);
1625 shader_addline(buffer
, "int tmpInt%u;\n", i
);
1629 /* Temporary variables for matrix operations */
1630 shader_addline(buffer
, "vec4 tmp0;\n");
1631 shader_addline(buffer
, "vec4 tmp1;\n");
1633 if (!shader
->load_local_constsF
)
1635 LIST_FOR_EACH_ENTRY(lconst
, &shader
->constantsF
, struct wined3d_shader_lconst
, entry
)
1637 shader_addline(buffer
, "const vec4 %s_lc%u = ", prefix
, lconst
->idx
);
1638 shader_glsl_append_imm_vec4(buffer
, (const float *)lconst
->value
);
1639 shader_addline(buffer
, ";\n");
1643 /* Start the main program. */
1644 shader_addline(buffer
, "void main()\n{\n");
1646 /* Direct3D applications expect integer vPos values, while OpenGL drivers
1647 * add approximately 0.5. This causes off-by-one problems as spotted by
1648 * the vPos d3d9 visual test. Unfortunately ATI cards do not add exactly
1649 * 0.5, but rather something like 0.49999999 or 0.50000001, which still
1650 * causes precision troubles when we just subtract 0.5.
1652 * To deal with that, just floor() the position. This will eliminate the
1653 * fraction on all cards.
1655 * TODO: Test how this behaves with multisampling.
1657 * An advantage of floor is that it works even if the driver doesn't add
1658 * 0.5. It is somewhat questionable if 1.5, 2.5, ... are the proper values
1659 * to return in gl_FragCoord, even though coordinates specify the pixel
1660 * centers instead of the pixel corners. This code will behave correctly
1661 * on drivers that returns integer values. */
1662 if (version
->type
== WINED3D_SHADER_TYPE_PIXEL
&& reg_maps
->vpos
)
1664 if (shader
->device
->wined3d
->flags
& WINED3D_PIXEL_CENTER_INTEGER
)
1665 shader_addline(buffer
,
1666 "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
1668 shader_addline(buffer
,
1669 "vpos = vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1);\n");
1673 /*****************************************************************************
1674 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
1676 * For more information, see http://wiki.winehq.org/DirectX-Shaders
1677 ****************************************************************************/
1680 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction
*ins
,
1681 const struct wined3d_shader_src_param
*wined3d_src
, DWORD mask
, struct glsl_src_param
*glsl_src
);
1683 /** Used for opcode modifiers - They multiply the result by the specified amount */
1684 static const char * const shift_glsl_tab
[] = {
1686 "2.0 * ", /* 1 (x2) */
1687 "4.0 * ", /* 2 (x4) */
1688 "8.0 * ", /* 3 (x8) */
1689 "16.0 * ", /* 4 (x16) */
1690 "32.0 * ", /* 5 (x32) */
1697 "0.0625 * ", /* 12 (d16) */
1698 "0.125 * ", /* 13 (d8) */
1699 "0.25 * ", /* 14 (d4) */
1700 "0.5 * " /* 15 (d2) */
1703 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
1704 static void shader_glsl_gen_modifier(enum wined3d_shader_src_modifier src_modifier
,
1705 const char *in_reg
, const char *in_regswizzle
, char *out_str
)
1709 switch (src_modifier
)
1711 case WINED3DSPSM_DZ
: /* Need to handle this in the instructions itself (texld & texcrd). */
1712 case WINED3DSPSM_DW
:
1713 case WINED3DSPSM_NONE
:
1714 sprintf(out_str
, "%s%s", in_reg
, in_regswizzle
);
1716 case WINED3DSPSM_NEG
:
1717 sprintf(out_str
, "-%s%s", in_reg
, in_regswizzle
);
1719 case WINED3DSPSM_NOT
:
1720 sprintf(out_str
, "!%s%s", in_reg
, in_regswizzle
);
1722 case WINED3DSPSM_BIAS
:
1723 sprintf(out_str
, "(%s%s - vec4(0.5)%s)", in_reg
, in_regswizzle
, in_regswizzle
);
1725 case WINED3DSPSM_BIASNEG
:
1726 sprintf(out_str
, "-(%s%s - vec4(0.5)%s)", in_reg
, in_regswizzle
, in_regswizzle
);
1728 case WINED3DSPSM_SIGN
:
1729 sprintf(out_str
, "(2.0 * (%s%s - 0.5))", in_reg
, in_regswizzle
);
1731 case WINED3DSPSM_SIGNNEG
:
1732 sprintf(out_str
, "-(2.0 * (%s%s - 0.5))", in_reg
, in_regswizzle
);
1734 case WINED3DSPSM_COMP
:
1735 sprintf(out_str
, "(1.0 - %s%s)", in_reg
, in_regswizzle
);
1737 case WINED3DSPSM_X2
:
1738 sprintf(out_str
, "(2.0 * %s%s)", in_reg
, in_regswizzle
);
1740 case WINED3DSPSM_X2NEG
:
1741 sprintf(out_str
, "-(2.0 * %s%s)", in_reg
, in_regswizzle
);
1743 case WINED3DSPSM_ABS
:
1744 sprintf(out_str
, "abs(%s%s)", in_reg
, in_regswizzle
);
1746 case WINED3DSPSM_ABSNEG
:
1747 sprintf(out_str
, "-abs(%s%s)", in_reg
, in_regswizzle
);
1750 FIXME("Unhandled modifier %u\n", src_modifier
);
1751 sprintf(out_str
, "%s%s", in_reg
, in_regswizzle
);
1755 /** Writes the GLSL variable name that corresponds to the register that the
1756 * DX opcode parameter is trying to access */
1757 static void shader_glsl_get_register_name(const struct wined3d_shader_register
*reg
,
1758 char *register_name
, BOOL
*is_color
, const struct wined3d_shader_instruction
*ins
)
1760 /* oPos, oFog and oPts in D3D */
1761 static const char * const hwrastout_reg_names
[] = {"vs_out[10]", "vs_out[11].x", "vs_out[11].y"};
1763 const struct wined3d_shader
*shader
= ins
->ctx
->shader
;
1764 const struct wined3d_shader_reg_maps
*reg_maps
= ins
->ctx
->reg_maps
;
1765 const struct wined3d_shader_version
*version
= ®_maps
->shader_version
;
1766 const struct wined3d_gl_info
*gl_info
= ins
->ctx
->gl_info
;
1767 const char *prefix
= shader_glsl_get_prefix(version
->type
);
1768 struct glsl_src_param rel_param0
, rel_param1
;
1769 char imm_str
[4][17];
1771 if (reg
->idx
[0].offset
!= ~0U && reg
->idx
[0].rel_addr
)
1772 shader_glsl_add_src_param(ins
, reg
->idx
[0].rel_addr
, WINED3DSP_WRITEMASK_0
, &rel_param0
);
1773 if (reg
->idx
[1].offset
!= ~0U && reg
->idx
[1].rel_addr
)
1774 shader_glsl_add_src_param(ins
, reg
->idx
[1].rel_addr
, WINED3DSP_WRITEMASK_0
, &rel_param1
);
1779 case WINED3DSPR_TEMP
:
1780 sprintf(register_name
, "R%u", reg
->idx
[0].offset
);
1783 case WINED3DSPR_INPUT
:
1784 /* vertex shaders */
1785 if (version
->type
== WINED3D_SHADER_TYPE_VERTEX
)
1787 struct shader_glsl_ctx_priv
*priv
= ins
->ctx
->backend_data
;
1788 if (priv
->cur_vs_args
->swizzle_map
& (1 << reg
->idx
[0].offset
))
1790 sprintf(register_name
, "%s_in%u", prefix
, reg
->idx
[0].offset
);
1794 if (version
->type
== WINED3D_SHADER_TYPE_GEOMETRY
)
1796 if (reg
->idx
[0].rel_addr
)
1798 if (reg
->idx
[1].rel_addr
)
1799 sprintf(register_name
, "gs_in[%s + %u][%s + %u]",
1800 rel_param0
.param_str
, reg
->idx
[0].offset
, rel_param1
.param_str
, reg
->idx
[1].offset
);
1802 sprintf(register_name
, "gs_in[%s + %u][%u]",
1803 rel_param0
.param_str
, reg
->idx
[0].offset
, reg
->idx
[1].offset
);
1805 else if (reg
->idx
[1].rel_addr
)
1806 sprintf(register_name
, "gs_in[%u][%s + %u]",
1807 reg
->idx
[0].offset
, rel_param1
.param_str
, reg
->idx
[1].offset
);
1809 sprintf(register_name
, "gs_in[%u][%u]", reg
->idx
[0].offset
, reg
->idx
[1].offset
);
1813 /* pixel shaders >= 3.0 */
1814 if (version
->major
>= 3)
1816 DWORD idx
= shader
->u
.ps
.input_reg_map
[reg
->idx
[0].offset
];
1817 unsigned int in_count
= vec4_varyings(version
->major
, gl_info
);
1819 if (reg
->idx
[0].rel_addr
)
1821 /* Removing a + 0 would be an obvious optimization, but
1822 * OS X doesn't see the NOP operation there. */
1825 if (shader
->u
.ps
.declared_in_count
> in_count
)
1827 sprintf(register_name
,
1828 "((%s + %u) > %u ? (%s + %u) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s + %u])",
1829 rel_param0
.param_str
, idx
, in_count
- 1, rel_param0
.param_str
, idx
, in_count
,
1830 prefix
, rel_param0
.param_str
, idx
);
1834 sprintf(register_name
, "%s_in[%s + %u]", prefix
, rel_param0
.param_str
, idx
);
1839 if (shader
->u
.ps
.declared_in_count
> in_count
)
1841 sprintf(register_name
, "((%s) > %u ? (%s) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s])",
1842 rel_param0
.param_str
, in_count
- 1, rel_param0
.param_str
, in_count
,
1843 prefix
, rel_param0
.param_str
);
1847 sprintf(register_name
, "%s_in[%s]", prefix
, rel_param0
.param_str
);
1853 if (idx
== in_count
) sprintf(register_name
, "gl_Color");
1854 else if (idx
== in_count
+ 1) sprintf(register_name
, "gl_SecondaryColor");
1855 else sprintf(register_name
, "%s_in[%u]", prefix
, idx
);
1860 if (!reg
->idx
[0].offset
)
1861 strcpy(register_name
, "gl_Color");
1863 strcpy(register_name
, "gl_SecondaryColor");
1868 case WINED3DSPR_CONST
:
1870 /* Relative addressing */
1871 if (reg
->idx
[0].rel_addr
)
1873 if (reg
->idx
[0].offset
)
1874 sprintf(register_name
, "%s_c[%s + %u]", prefix
, rel_param0
.param_str
, reg
->idx
[0].offset
);
1876 sprintf(register_name
, "%s_c[%s]", prefix
, rel_param0
.param_str
);
1880 if (shader_constant_is_local(shader
, reg
->idx
[0].offset
))
1881 sprintf(register_name
, "%s_lc%u", prefix
, reg
->idx
[0].offset
);
1883 sprintf(register_name
, "%s_c[%u]", prefix
, reg
->idx
[0].offset
);
1888 case WINED3DSPR_CONSTINT
:
1889 sprintf(register_name
, "%s_i[%u]", prefix
, reg
->idx
[0].offset
);
1892 case WINED3DSPR_CONSTBOOL
:
1893 sprintf(register_name
, "%s_b[%u]", prefix
, reg
->idx
[0].offset
);
1896 case WINED3DSPR_TEXTURE
: /* case WINED3DSPR_ADDR: */
1897 if (version
->type
== WINED3D_SHADER_TYPE_PIXEL
)
1898 sprintf(register_name
, "T%u", reg
->idx
[0].offset
);
1900 sprintf(register_name
, "A%u", reg
->idx
[0].offset
);
1903 case WINED3DSPR_LOOP
:
1904 sprintf(register_name
, "aL%u", ins
->ctx
->loop_state
->current_reg
- 1);
1907 case WINED3DSPR_SAMPLER
:
1908 sprintf(register_name
, "%s_sampler%u", prefix
, reg
->idx
[0].offset
);
1911 case WINED3DSPR_COLOROUT
:
1912 if (reg
->idx
[0].offset
>= gl_info
->limits
.buffers
)
1913 WARN("Write to render target %u, only %d supported.\n",
1914 reg
->idx
[0].offset
, gl_info
->limits
.buffers
);
1916 sprintf(register_name
, "gl_FragData[%u]", reg
->idx
[0].offset
);
1919 case WINED3DSPR_RASTOUT
:
1920 sprintf(register_name
, "%s", hwrastout_reg_names
[reg
->idx
[0].offset
]);
1923 case WINED3DSPR_DEPTHOUT
:
1924 sprintf(register_name
, "gl_FragDepth");
1927 case WINED3DSPR_ATTROUT
:
1928 if (!reg
->idx
[0].offset
)
1929 sprintf(register_name
, "%s_out[8]", prefix
);
1931 sprintf(register_name
, "%s_out[9]", prefix
);
1934 case WINED3DSPR_TEXCRDOUT
:
1935 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
1936 sprintf(register_name
, "%s_out[%u]", prefix
, reg
->idx
[0].offset
);
1939 case WINED3DSPR_MISCTYPE
:
1940 if (!reg
->idx
[0].offset
)
1943 sprintf(register_name
, "vpos");
1945 else if (reg
->idx
[0].offset
== 1)
1947 /* Note that gl_FrontFacing is a bool, while vFace is
1948 * a float for which the sign determines front/back */
1949 sprintf(register_name
, "(gl_FrontFacing ? 1.0 : -1.0)");
1953 FIXME("Unhandled misctype register %u.\n", reg
->idx
[0].offset
);
1954 sprintf(register_name
, "unrecognized_register");
1958 case WINED3DSPR_IMMCONST
:
1959 switch (reg
->immconst_type
)
1961 case WINED3D_IMMCONST_SCALAR
:
1962 switch (reg
->data_type
)
1964 case WINED3D_DATA_FLOAT
:
1965 wined3d_ftoa(*(const float *)reg
->immconst_data
, register_name
);
1967 case WINED3D_DATA_INT
:
1968 sprintf(register_name
, "%#x", reg
->immconst_data
[0]);
1970 case WINED3D_DATA_RESOURCE
:
1971 case WINED3D_DATA_SAMPLER
:
1972 case WINED3D_DATA_UINT
:
1973 sprintf(register_name
, "%#xu", reg
->immconst_data
[0]);
1976 sprintf(register_name
, "<unhandled data type %#x>", reg
->data_type
);
1981 case WINED3D_IMMCONST_VEC4
:
1982 switch (reg
->data_type
)
1984 case WINED3D_DATA_FLOAT
:
1985 wined3d_ftoa(*(const float *)®
->immconst_data
[0], imm_str
[0]);
1986 wined3d_ftoa(*(const float *)®
->immconst_data
[1], imm_str
[1]);
1987 wined3d_ftoa(*(const float *)®
->immconst_data
[2], imm_str
[2]);
1988 wined3d_ftoa(*(const float *)®
->immconst_data
[3], imm_str
[3]);
1989 sprintf(register_name
, "vec4(%s, %s, %s, %s)",
1990 imm_str
[0], imm_str
[1], imm_str
[2], imm_str
[3]);
1992 case WINED3D_DATA_INT
:
1993 sprintf(register_name
, "ivec4(%#x, %#x, %#x, %#x)",
1994 reg
->immconst_data
[0], reg
->immconst_data
[1],
1995 reg
->immconst_data
[2], reg
->immconst_data
[3]);
1997 case WINED3D_DATA_RESOURCE
:
1998 case WINED3D_DATA_SAMPLER
:
1999 case WINED3D_DATA_UINT
:
2000 sprintf(register_name
, "uvec4(%#xu, %#xu, %#xu, %#xu)",
2001 reg
->immconst_data
[0], reg
->immconst_data
[1],
2002 reg
->immconst_data
[2], reg
->immconst_data
[3]);
2005 sprintf(register_name
, "<unhandled data type %#x>", reg
->data_type
);
2011 FIXME("Unhandled immconst type %#x\n", reg
->immconst_type
);
2012 sprintf(register_name
, "<unhandled_immconst_type %#x>", reg
->immconst_type
);
2016 case WINED3DSPR_CONSTBUFFER
:
2017 if (reg
->idx
[1].rel_addr
)
2018 sprintf(register_name
, "%s_cb%u[%s + %u]",
2019 prefix
, reg
->idx
[0].offset
, rel_param1
.param_str
, reg
->idx
[1].offset
);
2021 sprintf(register_name
, "%s_cb%u[%u]", prefix
, reg
->idx
[0].offset
, reg
->idx
[1].offset
);
2024 case WINED3DSPR_PRIMID
:
2025 sprintf(register_name
, "uint(gl_PrimitiveIDIn)");
2029 FIXME("Unhandled register type %#x.\n", reg
->type
);
2030 sprintf(register_name
, "unrecognized_register");
2035 static void shader_glsl_write_mask_to_str(DWORD write_mask
, char *str
)
2038 if (write_mask
& WINED3DSP_WRITEMASK_0
) *str
++ = 'x';
2039 if (write_mask
& WINED3DSP_WRITEMASK_1
) *str
++ = 'y';
2040 if (write_mask
& WINED3DSP_WRITEMASK_2
) *str
++ = 'z';
2041 if (write_mask
& WINED3DSP_WRITEMASK_3
) *str
++ = 'w';
2045 /* Get the GLSL write mask for the destination register */
2046 static DWORD
shader_glsl_get_write_mask(const struct wined3d_shader_dst_param
*param
, char *write_mask
)
2048 DWORD mask
= param
->write_mask
;
2050 if (shader_is_scalar(¶m
->reg
))
2052 mask
= WINED3DSP_WRITEMASK_0
;
2057 shader_glsl_write_mask_to_str(mask
, write_mask
);
2063 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask
) {
2064 unsigned int size
= 0;
2066 if (write_mask
& WINED3DSP_WRITEMASK_0
) ++size
;
2067 if (write_mask
& WINED3DSP_WRITEMASK_1
) ++size
;
2068 if (write_mask
& WINED3DSP_WRITEMASK_2
) ++size
;
2069 if (write_mask
& WINED3DSP_WRITEMASK_3
) ++size
;
2074 static void shader_glsl_swizzle_to_str(const DWORD swizzle
, BOOL fixup
, DWORD mask
, char *str
)
2076 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
2077 * but addressed as "rgba". To fix this we need to swap the register's x
2078 * and z components. */
2079 const char *swizzle_chars
= fixup
? "zyxw" : "xyzw";
2082 /* swizzle bits fields: wwzzyyxx */
2083 if (mask
& WINED3DSP_WRITEMASK_0
) *str
++ = swizzle_chars
[swizzle
& 0x03];
2084 if (mask
& WINED3DSP_WRITEMASK_1
) *str
++ = swizzle_chars
[(swizzle
>> 2) & 0x03];
2085 if (mask
& WINED3DSP_WRITEMASK_2
) *str
++ = swizzle_chars
[(swizzle
>> 4) & 0x03];
2086 if (mask
& WINED3DSP_WRITEMASK_3
) *str
++ = swizzle_chars
[(swizzle
>> 6) & 0x03];
2090 static void shader_glsl_get_swizzle(const struct wined3d_shader_src_param
*param
,
2091 BOOL fixup
, DWORD mask
, char *swizzle_str
)
2093 if (shader_is_scalar(¶m
->reg
))
2094 *swizzle_str
= '\0';
2096 shader_glsl_swizzle_to_str(param
->swizzle
, fixup
, mask
, swizzle_str
);
2099 /* From a given parameter token, generate the corresponding GLSL string.
2100 * Also, return the actual register name and swizzle in case the
2101 * caller needs this information as well. */
2102 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction
*ins
,
2103 const struct wined3d_shader_src_param
*wined3d_src
, DWORD mask
, struct glsl_src_param
*glsl_src
)
2105 BOOL is_color
= FALSE
;
2106 char swizzle_str
[6];
2108 glsl_src
->reg_name
[0] = '\0';
2109 glsl_src
->param_str
[0] = '\0';
2110 swizzle_str
[0] = '\0';
2112 shader_glsl_get_register_name(&wined3d_src
->reg
, glsl_src
->reg_name
, &is_color
, ins
);
2113 shader_glsl_get_swizzle(wined3d_src
, is_color
, mask
, swizzle_str
);
2115 if (wined3d_src
->reg
.type
== WINED3DSPR_IMMCONST
|| wined3d_src
->reg
.type
== WINED3DSPR_PRIMID
)
2117 shader_glsl_gen_modifier(wined3d_src
->modifiers
, glsl_src
->reg_name
, swizzle_str
, glsl_src
->param_str
);
2123 switch (wined3d_src
->reg
.data_type
)
2125 case WINED3D_DATA_FLOAT
:
2126 sprintf(reg_name
, "%s", glsl_src
->reg_name
);
2128 case WINED3D_DATA_INT
:
2129 sprintf(reg_name
, "floatBitsToInt(%s)", glsl_src
->reg_name
);
2131 case WINED3D_DATA_RESOURCE
:
2132 case WINED3D_DATA_SAMPLER
:
2133 case WINED3D_DATA_UINT
:
2134 sprintf(reg_name
, "floatBitsToUint(%s)", glsl_src
->reg_name
);
2137 FIXME("Unhandled data type %#x.\n", wined3d_src
->reg
.data_type
);
2138 sprintf(reg_name
, "%s", glsl_src
->reg_name
);
2142 shader_glsl_gen_modifier(wined3d_src
->modifiers
, reg_name
, swizzle_str
, glsl_src
->param_str
);
2146 /* From a given parameter token, generate the corresponding GLSL string.
2147 * Also, return the actual register name and swizzle in case the
2148 * caller needs this information as well. */
2149 static DWORD
shader_glsl_add_dst_param(const struct wined3d_shader_instruction
*ins
,
2150 const struct wined3d_shader_dst_param
*wined3d_dst
, struct glsl_dst_param
*glsl_dst
)
2152 BOOL is_color
= FALSE
;
2154 glsl_dst
->mask_str
[0] = '\0';
2155 glsl_dst
->reg_name
[0] = '\0';
2157 shader_glsl_get_register_name(&wined3d_dst
->reg
, glsl_dst
->reg_name
, &is_color
, ins
);
2158 return shader_glsl_get_write_mask(wined3d_dst
, glsl_dst
->mask_str
);
2161 /* Append the destination part of the instruction to the buffer, return the effective write mask */
2162 static DWORD
shader_glsl_append_dst_ext(struct wined3d_string_buffer
*buffer
,
2163 const struct wined3d_shader_instruction
*ins
, const struct wined3d_shader_dst_param
*dst
,
2164 enum wined3d_data_type data_type
)
2166 struct glsl_dst_param glsl_dst
;
2169 if ((mask
= shader_glsl_add_dst_param(ins
, dst
, &glsl_dst
)))
2173 case WINED3D_DATA_FLOAT
:
2174 shader_addline(buffer
, "%s%s = %s(",
2175 glsl_dst
.reg_name
, glsl_dst
.mask_str
, shift_glsl_tab
[dst
->shift
]);
2177 case WINED3D_DATA_INT
:
2178 shader_addline(buffer
, "%s%s = %sintBitsToFloat(",
2179 glsl_dst
.reg_name
, glsl_dst
.mask_str
, shift_glsl_tab
[dst
->shift
]);
2181 case WINED3D_DATA_RESOURCE
:
2182 case WINED3D_DATA_SAMPLER
:
2183 case WINED3D_DATA_UINT
:
2184 shader_addline(buffer
, "%s%s = %suintBitsToFloat(",
2185 glsl_dst
.reg_name
, glsl_dst
.mask_str
, shift_glsl_tab
[dst
->shift
]);
2188 FIXME("Unhandled data type %#x.\n", data_type
);
2189 shader_addline(buffer
, "%s%s = %s(",
2190 glsl_dst
.reg_name
, glsl_dst
.mask_str
, shift_glsl_tab
[dst
->shift
]);
2198 /* Append the destination part of the instruction to the buffer, return the effective write mask */
2199 static DWORD
shader_glsl_append_dst(struct wined3d_string_buffer
*buffer
, const struct wined3d_shader_instruction
*ins
)
2201 return shader_glsl_append_dst_ext(buffer
, ins
, &ins
->dst
[0], ins
->dst
[0].reg
.data_type
);
2204 /** Process GLSL instruction modifiers */
2205 static void shader_glsl_add_instruction_modifiers(const struct wined3d_shader_instruction
*ins
)
2207 struct glsl_dst_param dst_param
;
2210 if (!ins
->dst_count
) return;
2212 modifiers
= ins
->dst
[0].modifiers
;
2213 if (!modifiers
) return;
2215 shader_glsl_add_dst_param(ins
, &ins
->dst
[0], &dst_param
);
2217 if (modifiers
& WINED3DSPDM_SATURATE
)
2219 /* _SAT means to clamp the value of the register to between 0 and 1 */
2220 shader_addline(ins
->ctx
->buffer
, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param
.reg_name
,
2221 dst_param
.mask_str
, dst_param
.reg_name
, dst_param
.mask_str
);
2224 if (modifiers
& WINED3DSPDM_MSAMPCENTROID
)
2226 FIXME("_centroid modifier not handled\n");
2229 if (modifiers
& WINED3DSPDM_PARTIALPRECISION
)
2231 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
2235 static const char *shader_glsl_get_rel_op(enum wined3d_shader_rel_op op
)
2239 case WINED3D_SHADER_REL_OP_GT
: return ">";
2240 case WINED3D_SHADER_REL_OP_EQ
: return "==";
2241 case WINED3D_SHADER_REL_OP_GE
: return ">=";
2242 case WINED3D_SHADER_REL_OP_LT
: return "<";
2243 case WINED3D_SHADER_REL_OP_NE
: return "!=";
2244 case WINED3D_SHADER_REL_OP_LE
: return "<=";
2246 FIXME("Unrecognized operator %#x.\n", op
);
2251 static void shader_glsl_get_sample_function(const struct wined3d_shader_context
*ctx
,
2252 DWORD resource_idx
, DWORD flags
, struct glsl_sample_function
*sample_function
)
2254 enum wined3d_shader_resource_type resource_type
= ctx
->reg_maps
->resource_info
[resource_idx
].type
;
2255 const struct wined3d_gl_info
*gl_info
= ctx
->gl_info
;
2256 BOOL shadow
= ctx
->reg_maps
->shader_version
.type
== WINED3D_SHADER_TYPE_PIXEL
2257 && (((const struct shader_glsl_ctx_priv
*)ctx
->backend_data
)->cur_ps_args
->shadow
& (1 << resource_idx
));
2258 BOOL projected
= flags
& WINED3D_GLSL_SAMPLE_PROJECTED
;
2259 BOOL texrect
= flags
& WINED3D_GLSL_SAMPLE_NPOT
&& gl_info
->supported
[ARB_TEXTURE_RECTANGLE
];
2260 BOOL lod
= flags
& WINED3D_GLSL_SAMPLE_LOD
;
2261 BOOL grad
= flags
& WINED3D_GLSL_SAMPLE_GRAD
;
2263 sample_function
->data_type
= ctx
->reg_maps
->resource_info
[resource_idx
].data_type
;
2265 /* Note that there's no such thing as a projected cube texture. */
2266 switch (resource_type
)
2268 case WINED3D_SHADER_RESOURCE_TEXTURE_1D
:
2273 sample_function
->name
= projected
? "shadow1DProjLod" : "shadow1DLod";
2277 if (gl_info
->supported
[EXT_GPU_SHADER4
])
2278 sample_function
->name
= projected
? "shadow1DProjGrad" : "shadow1DGrad";
2279 else if (gl_info
->supported
[ARB_SHADER_TEXTURE_LOD
])
2280 sample_function
->name
= projected
? "shadow1DProjGradARB" : "shadow1DGradARB";
2283 FIXME("Unsupported 1D shadow grad function.\n");
2284 sample_function
->name
= "unsupported1DGrad";
2289 sample_function
->name
= projected
? "shadow1DProj" : "shadow1D";
2291 sample_function
->coord_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
;
2297 sample_function
->name
= projected
? "texture1DProjLod" : "texture1DLod";
2301 if (gl_info
->supported
[EXT_GPU_SHADER4
])
2302 sample_function
->name
= projected
? "texture1DProjGrad" : "texture1DGrad";
2303 else if (gl_info
->supported
[ARB_SHADER_TEXTURE_LOD
])
2304 sample_function
->name
= projected
? "texture1DProjGradARB" : "texture1DGradARB";
2307 FIXME("Unsupported 1D grad function.\n");
2308 sample_function
->name
= "unsupported1DGrad";
2313 sample_function
->name
= projected
? "texture1DProj" : "texture1D";
2315 sample_function
->coord_mask
= WINED3DSP_WRITEMASK_0
;
2319 case WINED3D_SHADER_RESOURCE_TEXTURE_2D
:
2326 sample_function
->name
= projected
? "shadow2DRectProjLod" : "shadow2DRectLod";
2330 if (gl_info
->supported
[EXT_GPU_SHADER4
])
2331 sample_function
->name
= projected
? "shadow2DRectProjGrad" : "shadow2DRectGrad";
2332 else if (gl_info
->supported
[ARB_SHADER_TEXTURE_LOD
])
2333 sample_function
->name
= projected
? "shadow2DRectProjGradARB" : "shadow2DRectGradARB";
2336 FIXME("Unsupported RECT shadow grad function.\n");
2337 sample_function
->name
= "unsupported2DRectGrad";
2342 sample_function
->name
= projected
? "shadow2DRectProj" : "shadow2DRect";
2349 sample_function
->name
= projected
? "shadow2DProjLod" : "shadow2DLod";
2353 if (gl_info
->supported
[EXT_GPU_SHADER4
])
2354 sample_function
->name
= projected
? "shadow2DProjGrad" : "shadow2DGrad";
2355 else if (gl_info
->supported
[ARB_SHADER_TEXTURE_LOD
])
2356 sample_function
->name
= projected
? "shadow2DProjGradARB" : "shadow2DGradARB";
2359 FIXME("Unsupported 2D shadow grad function.\n");
2360 sample_function
->name
= "unsupported2DGrad";
2365 sample_function
->name
= projected
? "shadow2DProj" : "shadow2D";
2368 sample_function
->coord_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
2376 sample_function
->name
= projected
? "texture2DRectProjLod" : "texture2DRectLod";
2380 if (gl_info
->supported
[EXT_GPU_SHADER4
])
2381 sample_function
->name
= projected
? "texture2DRectProjGrad" : "texture2DRectGrad";
2382 else if (gl_info
->supported
[ARB_SHADER_TEXTURE_LOD
])
2383 sample_function
->name
= projected
? "texture2DRectProjGradARB" : "texture2DRectGradARB";
2386 FIXME("Unsupported RECT grad function.\n");
2387 sample_function
->name
= "unsupported2DRectGrad";
2392 sample_function
->name
= projected
? "texture2DRectProj" : "texture2DRect";
2399 sample_function
->name
= projected
? "texture2DProjLod" : "texture2DLod";
2403 if (gl_info
->supported
[EXT_GPU_SHADER4
])
2404 sample_function
->name
= projected
? "texture2DProjGrad" : "texture2DGrad";
2405 else if (gl_info
->supported
[ARB_SHADER_TEXTURE_LOD
])
2406 sample_function
->name
= projected
? "texture2DProjGradARB" : "texture2DGradARB";
2409 FIXME("Unsupported 2D grad function.\n");
2410 sample_function
->name
= "unsupported2DGrad";
2415 sample_function
->name
= projected
? "texture2DProj" : "texture2D";
2418 sample_function
->coord_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
;
2422 case WINED3D_SHADER_RESOURCE_TEXTURE_3D
:
2425 FIXME("Unsupported 3D shadow function.\n");
2426 sample_function
->name
= "unsupported3DShadow";
2427 sample_function
->coord_mask
= 0;
2433 sample_function
->name
= projected
? "texture3DProjLod" : "texture3DLod";
2437 if (gl_info
->supported
[EXT_GPU_SHADER4
])
2438 sample_function
->name
= projected
? "texture3DProjGrad" : "texture3DGrad";
2439 else if (gl_info
->supported
[ARB_SHADER_TEXTURE_LOD
])
2440 sample_function
->name
= projected
? "texture3DProjGradARB" : "texture3DGradARB";
2443 FIXME("Unsupported 3D grad function.\n");
2444 sample_function
->name
= "unsupported3DGrad";
2449 sample_function
->name
= projected
? "texture3DProj" : "texture3D";
2451 sample_function
->coord_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
2455 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE
:
2458 FIXME("Unsupported Cube shadow function.\n");
2459 sample_function
->name
= "unsupportedCubeShadow";
2460 sample_function
->coord_mask
= 0;
2466 sample_function
->name
= "textureCubeLod";
2470 if (gl_info
->supported
[EXT_GPU_SHADER4
])
2471 sample_function
->name
= "textureCubeGrad";
2472 else if (gl_info
->supported
[ARB_SHADER_TEXTURE_LOD
])
2473 sample_function
->name
= "textureCubeGradARB";
2476 FIXME("Unsupported Cube grad function.\n");
2477 sample_function
->name
= "unsupportedCubeGrad";
2482 sample_function
->name
= "textureCube";
2484 sample_function
->coord_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
2489 sample_function
->name
= "";
2490 sample_function
->coord_mask
= 0;
2491 FIXME("Unhandled resource type %#x.\n", resource_type
);
2496 static void shader_glsl_append_fixup_arg(char *arguments
, const char *reg_name
,
2497 BOOL sign_fixup
, enum fixup_channel_source channel_source
)
2499 switch(channel_source
)
2501 case CHANNEL_SOURCE_ZERO
:
2502 strcat(arguments
, "0.0");
2505 case CHANNEL_SOURCE_ONE
:
2506 strcat(arguments
, "1.0");
2509 case CHANNEL_SOURCE_X
:
2510 strcat(arguments
, reg_name
);
2511 strcat(arguments
, ".x");
2514 case CHANNEL_SOURCE_Y
:
2515 strcat(arguments
, reg_name
);
2516 strcat(arguments
, ".y");
2519 case CHANNEL_SOURCE_Z
:
2520 strcat(arguments
, reg_name
);
2521 strcat(arguments
, ".z");
2524 case CHANNEL_SOURCE_W
:
2525 strcat(arguments
, reg_name
);
2526 strcat(arguments
, ".w");
2530 FIXME("Unhandled channel source %#x\n", channel_source
);
2531 strcat(arguments
, "undefined");
2535 if (sign_fixup
) strcat(arguments
, " * 2.0 - 1.0");
2538 static void shader_glsl_color_correction_ext(struct wined3d_string_buffer
*buffer
,
2539 const char *reg_name
, DWORD mask
, struct color_fixup_desc fixup
)
2541 unsigned int mask_size
, remaining
;
2542 DWORD fixup_mask
= 0;
2543 char arguments
[256];
2546 if (fixup
.x_sign_fixup
|| fixup
.x_source
!= CHANNEL_SOURCE_X
) fixup_mask
|= WINED3DSP_WRITEMASK_0
;
2547 if (fixup
.y_sign_fixup
|| fixup
.y_source
!= CHANNEL_SOURCE_Y
) fixup_mask
|= WINED3DSP_WRITEMASK_1
;
2548 if (fixup
.z_sign_fixup
|| fixup
.z_source
!= CHANNEL_SOURCE_Z
) fixup_mask
|= WINED3DSP_WRITEMASK_2
;
2549 if (fixup
.w_sign_fixup
|| fixup
.w_source
!= CHANNEL_SOURCE_W
) fixup_mask
|= WINED3DSP_WRITEMASK_3
;
2550 if (!(mask
&= fixup_mask
))
2553 if (is_complex_fixup(fixup
))
2555 enum complex_fixup complex_fixup
= get_complex_fixup(fixup
);
2556 FIXME("Complex fixup (%#x) not supported\n",complex_fixup
);
2560 shader_glsl_write_mask_to_str(mask
, mask_str
);
2561 mask_size
= shader_glsl_get_write_mask_size(mask
);
2563 arguments
[0] = '\0';
2564 remaining
= mask_size
;
2565 if (mask
& WINED3DSP_WRITEMASK_0
)
2567 shader_glsl_append_fixup_arg(arguments
, reg_name
, fixup
.x_sign_fixup
, fixup
.x_source
);
2568 if (--remaining
) strcat(arguments
, ", ");
2570 if (mask
& WINED3DSP_WRITEMASK_1
)
2572 shader_glsl_append_fixup_arg(arguments
, reg_name
, fixup
.y_sign_fixup
, fixup
.y_source
);
2573 if (--remaining
) strcat(arguments
, ", ");
2575 if (mask
& WINED3DSP_WRITEMASK_2
)
2577 shader_glsl_append_fixup_arg(arguments
, reg_name
, fixup
.z_sign_fixup
, fixup
.z_source
);
2578 if (--remaining
) strcat(arguments
, ", ");
2580 if (mask
& WINED3DSP_WRITEMASK_3
)
2582 shader_glsl_append_fixup_arg(arguments
, reg_name
, fixup
.w_sign_fixup
, fixup
.w_source
);
2583 if (--remaining
) strcat(arguments
, ", ");
2587 shader_addline(buffer
, "%s%s = vec%u(%s);\n", reg_name
, mask_str
, mask_size
, arguments
);
2589 shader_addline(buffer
, "%s%s = %s;\n", reg_name
, mask_str
, arguments
);
2592 static void shader_glsl_color_correction(const struct wined3d_shader_instruction
*ins
, struct color_fixup_desc fixup
)
2597 shader_glsl_get_register_name(&ins
->dst
[0].reg
, reg_name
, &is_color
, ins
);
2598 shader_glsl_color_correction_ext(ins
->ctx
->buffer
, reg_name
, ins
->dst
[0].write_mask
, fixup
);
2601 static void PRINTF_ATTR(8, 9) shader_glsl_gen_sample_code(const struct wined3d_shader_instruction
*ins
,
2602 DWORD sampler
, const struct glsl_sample_function
*sample_function
, DWORD swizzle
,
2603 const char *dx
, const char *dy
, const char *bias
, const char *coord_reg_fmt
, ...)
2605 const struct wined3d_shader_version
*version
= &ins
->ctx
->reg_maps
->shader_version
;
2606 char dst_swizzle
[6];
2607 struct color_fixup_desc fixup
;
2608 BOOL np2_fixup
= FALSE
;
2611 shader_glsl_swizzle_to_str(swizzle
, FALSE
, ins
->dst
[0].write_mask
, dst_swizzle
);
2613 if (version
->type
== WINED3D_SHADER_TYPE_PIXEL
)
2615 const struct shader_glsl_ctx_priv
*priv
= ins
->ctx
->backend_data
;
2616 fixup
= priv
->cur_ps_args
->color_fixup
[sampler
];
2618 if(priv
->cur_ps_args
->np2_fixup
& (1 << sampler
)) {
2620 FIXME("Biased sampling from NP2 textures is unsupported\n");
2628 fixup
= COLOR_FIXUP_IDENTITY
; /* FIXME: Vshader color fixup */
2631 shader_glsl_append_dst_ext(ins
->ctx
->buffer
, ins
, &ins
->dst
[0], sample_function
->data_type
);
2633 shader_addline(ins
->ctx
->buffer
, "%s(%s_sampler%u, ",
2634 sample_function
->name
, shader_glsl_get_prefix(version
->type
), sampler
);
2636 va_start(args
, coord_reg_fmt
);
2637 shader_vaddline(ins
->ctx
->buffer
, coord_reg_fmt
, args
);
2641 shader_addline(ins
->ctx
->buffer
, ", %s)%s);\n", bias
, dst_swizzle
);
2644 const struct shader_glsl_ctx_priv
*priv
= ins
->ctx
->backend_data
;
2645 const unsigned char idx
= priv
->cur_np2fixup_info
->idx
[sampler
];
2647 shader_addline(ins
->ctx
->buffer
, " * ps_samplerNP2Fixup[%u].%s)%s);\n", idx
>> 1,
2648 (idx
% 2) ? "zw" : "xy", dst_swizzle
);
2649 } else if(dx
&& dy
) {
2650 shader_addline(ins
->ctx
->buffer
, ", %s, %s)%s);\n", dx
, dy
, dst_swizzle
);
2652 shader_addline(ins
->ctx
->buffer
, ")%s);\n", dst_swizzle
);
2656 if(!is_identity_fixup(fixup
)) {
2657 shader_glsl_color_correction(ins
, fixup
);
2661 /*****************************************************************************
2662 * Begin processing individual instruction opcodes
2663 ****************************************************************************/
2665 static void shader_glsl_binop(const struct wined3d_shader_instruction
*ins
)
2667 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
2668 struct glsl_src_param src0_param
;
2669 struct glsl_src_param src1_param
;
2673 /* Determine the GLSL operator to use based on the opcode */
2674 switch (ins
->handler_idx
)
2676 case WINED3DSIH_ADD
: op
= "+"; break;
2677 case WINED3DSIH_AND
: op
= "&"; break;
2678 case WINED3DSIH_DIV
: op
= "/"; break;
2679 case WINED3DSIH_IADD
: op
= "+"; break;
2680 case WINED3DSIH_ISHL
: op
= "<<"; break;
2681 case WINED3DSIH_MUL
: op
= "*"; break;
2682 case WINED3DSIH_OR
: op
= "|"; break;
2683 case WINED3DSIH_SUB
: op
= "-"; break;
2684 case WINED3DSIH_USHR
: op
= ">>"; break;
2685 case WINED3DSIH_XOR
: op
= "^"; break;
2687 op
= "<unhandled operator>";
2688 FIXME("Opcode %#x not yet handled in GLSL\n", ins
->handler_idx
);
2692 write_mask
= shader_glsl_append_dst(buffer
, ins
);
2693 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
2694 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
2695 shader_addline(buffer
, "%s %s %s);\n", src0_param
.param_str
, op
, src1_param
.param_str
);
2698 static void shader_glsl_relop(const struct wined3d_shader_instruction
*ins
)
2700 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
2701 struct glsl_src_param src0_param
;
2702 struct glsl_src_param src1_param
;
2703 unsigned int mask_size
;
2707 write_mask
= shader_glsl_append_dst(buffer
, ins
);
2708 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
2709 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
2710 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
2714 switch (ins
->handler_idx
)
2716 case WINED3DSIH_EQ
: op
= "equal"; break;
2717 case WINED3DSIH_GE
: op
= "greaterThanEqual"; break;
2718 case WINED3DSIH_IGE
: op
= "greaterThanEqual"; break;
2719 case WINED3DSIH_UGE
: op
= "greaterThanEqual"; break;
2720 case WINED3DSIH_LT
: op
= "lessThan"; break;
2721 case WINED3DSIH_NE
: op
= "notEqual"; break;
2723 op
= "<unhandled operator>";
2724 ERR("Unhandled opcode %#x.\n", ins
->handler_idx
);
2728 shader_addline(buffer
, "uvec%u(%s(%s, %s)) * 0xffffffffu);\n",
2729 mask_size
, op
, src0_param
.param_str
, src1_param
.param_str
);
2733 switch (ins
->handler_idx
)
2735 case WINED3DSIH_EQ
: op
= "=="; break;
2736 case WINED3DSIH_GE
: op
= ">="; break;
2737 case WINED3DSIH_IGE
: op
= ">="; break;
2738 case WINED3DSIH_UGE
: op
= ">="; break;
2739 case WINED3DSIH_LT
: op
= "<"; break;
2740 case WINED3DSIH_NE
: op
= "!="; break;
2742 op
= "<unhandled operator>";
2743 ERR("Unhandled opcode %#x.\n", ins
->handler_idx
);
2747 shader_addline(buffer
, "%s %s %s ? 0xffffffffu : 0u);\n",
2748 src0_param
.param_str
, op
, src1_param
.param_str
);
2752 static void shader_glsl_imul(const struct wined3d_shader_instruction
*ins
)
2754 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
2755 struct glsl_src_param src0_param
;
2756 struct glsl_src_param src1_param
;
2759 /* If we have ARB_gpu_shader5 or GLSL 4.0, we can use imulExtended(). If
2760 * not, we can emulate it. */
2761 if (ins
->dst
[0].reg
.type
!= WINED3DSPR_NULL
)
2762 FIXME("64-bit integer multiplies not implemented.\n");
2764 if (ins
->dst
[1].reg
.type
!= WINED3DSPR_NULL
)
2766 write_mask
= shader_glsl_append_dst_ext(buffer
, ins
, &ins
->dst
[1], ins
->dst
[1].reg
.data_type
);
2767 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
2768 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
2770 shader_addline(ins
->ctx
->buffer
, "%s * %s);\n",
2771 src0_param
.param_str
, src1_param
.param_str
);
2775 static void shader_glsl_udiv(const struct wined3d_shader_instruction
*ins
)
2777 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
2778 struct glsl_src_param src0_param
, src1_param
;
2781 if (ins
->dst
[0].reg
.type
!= WINED3DSPR_NULL
)
2784 if (ins
->dst
[1].reg
.type
!= WINED3DSPR_NULL
)
2788 write_mask
= shader_glsl_get_write_mask(&ins
->dst
[0], dst_mask
);
2789 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
2790 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
2791 shader_addline(buffer
, "tmp0%s = %s / %s;\n",
2792 dst_mask
, src0_param
.param_str
, src1_param
.param_str
);
2794 write_mask
= shader_glsl_append_dst_ext(buffer
, ins
, &ins
->dst
[1], ins
->dst
[1].reg
.data_type
);
2795 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
2796 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
2797 shader_addline(buffer
, "%s %% %s));\n", src0_param
.param_str
, src1_param
.param_str
);
2799 shader_glsl_append_dst_ext(buffer
, ins
, &ins
->dst
[0], ins
->dst
[0].reg
.data_type
);
2800 shader_addline(buffer
, "tmp0%s);\n", dst_mask
);
2804 write_mask
= shader_glsl_append_dst_ext(buffer
, ins
, &ins
->dst
[0], ins
->dst
[0].reg
.data_type
);
2805 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
2806 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
2807 shader_addline(buffer
, "%s / %s);\n", src0_param
.param_str
, src1_param
.param_str
);
2810 else if (ins
->dst
[1].reg
.type
!= WINED3DSPR_NULL
)
2812 write_mask
= shader_glsl_append_dst_ext(buffer
, ins
, &ins
->dst
[1], ins
->dst
[1].reg
.data_type
);
2813 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
2814 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
2815 shader_addline(buffer
, "%s %% %s);\n", src0_param
.param_str
, src1_param
.param_str
);
2819 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
2820 static void shader_glsl_mov(const struct wined3d_shader_instruction
*ins
)
2822 const struct wined3d_gl_info
*gl_info
= ins
->ctx
->gl_info
;
2823 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
2824 struct glsl_src_param src0_param
;
2827 write_mask
= shader_glsl_append_dst(buffer
, ins
);
2828 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
2830 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
2831 * shader versions WINED3DSIO_MOVA is used for this. */
2832 if (ins
->ctx
->reg_maps
->shader_version
.major
== 1
2833 && ins
->ctx
->reg_maps
->shader_version
.type
== WINED3D_SHADER_TYPE_VERTEX
2834 && ins
->dst
[0].reg
.type
== WINED3DSPR_ADDR
)
2836 /* This is a simple floor() */
2837 unsigned int mask_size
= shader_glsl_get_write_mask_size(write_mask
);
2838 if (mask_size
> 1) {
2839 shader_addline(buffer
, "ivec%d(floor(%s)));\n", mask_size
, src0_param
.param_str
);
2841 shader_addline(buffer
, "int(floor(%s)));\n", src0_param
.param_str
);
2844 else if(ins
->handler_idx
== WINED3DSIH_MOVA
)
2846 /* We need to *round* to the nearest int here. */
2847 unsigned int mask_size
= shader_glsl_get_write_mask_size(write_mask
);
2849 if (gl_info
->supported
[EXT_GPU_SHADER4
])
2852 shader_addline(buffer
, "ivec%d(round(%s)));\n", mask_size
, src0_param
.param_str
);
2854 shader_addline(buffer
, "int(round(%s)));\n", src0_param
.param_str
);
2859 shader_addline(buffer
, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n",
2860 mask_size
, src0_param
.param_str
, mask_size
, src0_param
.param_str
);
2862 shader_addline(buffer
, "int(floor(abs(%s) + 0.5) * sign(%s)));\n",
2863 src0_param
.param_str
, src0_param
.param_str
);
2868 shader_addline(buffer
, "%s);\n", src0_param
.param_str
);
2872 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
2873 static void shader_glsl_dot(const struct wined3d_shader_instruction
*ins
)
2875 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
2876 struct glsl_src_param src0_param
;
2877 struct glsl_src_param src1_param
;
2878 DWORD dst_write_mask
, src_write_mask
;
2879 unsigned int dst_size
= 0;
2881 dst_write_mask
= shader_glsl_append_dst(buffer
, ins
);
2882 dst_size
= shader_glsl_get_write_mask_size(dst_write_mask
);
2884 /* dp4 works on vec4, dp3 on vec3, etc. */
2885 if (ins
->handler_idx
== WINED3DSIH_DP4
)
2886 src_write_mask
= WINED3DSP_WRITEMASK_ALL
;
2887 else if (ins
->handler_idx
== WINED3DSIH_DP3
)
2888 src_write_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
2890 src_write_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
;
2892 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_write_mask
, &src0_param
);
2893 shader_glsl_add_src_param(ins
, &ins
->src
[1], src_write_mask
, &src1_param
);
2896 shader_addline(buffer
, "vec%d(dot(%s, %s)));\n", dst_size
, src0_param
.param_str
, src1_param
.param_str
);
2898 shader_addline(buffer
, "dot(%s, %s));\n", src0_param
.param_str
, src1_param
.param_str
);
2902 /* Note that this instruction has some restrictions. The destination write mask
2903 * can't contain the w component, and the source swizzles have to be .xyzw */
2904 static void shader_glsl_cross(const struct wined3d_shader_instruction
*ins
)
2906 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
2907 struct glsl_src_param src0_param
;
2908 struct glsl_src_param src1_param
;
2911 shader_glsl_get_write_mask(&ins
->dst
[0], dst_mask
);
2912 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
2913 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
2914 shader_glsl_add_src_param(ins
, &ins
->src
[1], src_mask
, &src1_param
);
2915 shader_addline(ins
->ctx
->buffer
, "cross(%s, %s)%s);\n", src0_param
.param_str
, src1_param
.param_str
, dst_mask
);
2918 static void shader_glsl_cut(const struct wined3d_shader_instruction
*ins
)
2920 shader_addline(ins
->ctx
->buffer
, "EndPrimitive();\n");
2923 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
2924 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
2925 * GLSL uses the value as-is. */
2926 static void shader_glsl_pow(const struct wined3d_shader_instruction
*ins
)
2928 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
2929 struct glsl_src_param src0_param
;
2930 struct glsl_src_param src1_param
;
2931 DWORD dst_write_mask
;
2932 unsigned int dst_size
;
2934 dst_write_mask
= shader_glsl_append_dst(buffer
, ins
);
2935 dst_size
= shader_glsl_get_write_mask_size(dst_write_mask
);
2937 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
2938 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_0
, &src1_param
);
2942 shader_addline(buffer
, "vec%u(%s == 0.0 ? 1.0 : pow(abs(%s), %s)));\n",
2943 dst_size
, src1_param
.param_str
, src0_param
.param_str
, src1_param
.param_str
);
2947 shader_addline(buffer
, "%s == 0.0 ? 1.0 : pow(abs(%s), %s));\n",
2948 src1_param
.param_str
, src0_param
.param_str
, src1_param
.param_str
);
2952 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
2953 static void shader_glsl_map2gl(const struct wined3d_shader_instruction
*ins
)
2955 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
2956 struct glsl_src_param src_param
;
2957 const char *instruction
;
2961 /* Determine the GLSL function to use based on the opcode */
2962 /* TODO: Possibly make this a table for faster lookups */
2963 switch (ins
->handler_idx
)
2965 case WINED3DSIH_MIN
: instruction
= "min"; break;
2966 case WINED3DSIH_MAX
: instruction
= "max"; break;
2967 case WINED3DSIH_ABS
: instruction
= "abs"; break;
2968 case WINED3DSIH_FRC
: instruction
= "fract"; break;
2969 case WINED3DSIH_DSX
: instruction
= "dFdx"; break;
2970 case WINED3DSIH_DSY
: instruction
= "ycorrection.y * dFdy"; break;
2971 case WINED3DSIH_ROUND_NI
: instruction
= "floor"; break;
2972 case WINED3DSIH_SQRT
: instruction
= "sqrt"; break;
2973 default: instruction
= "";
2974 FIXME("Opcode %#x not yet handled in GLSL\n", ins
->handler_idx
);
2978 write_mask
= shader_glsl_append_dst(buffer
, ins
);
2980 shader_addline(buffer
, "%s(", instruction
);
2984 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src_param
);
2985 shader_addline(buffer
, "%s", src_param
.param_str
);
2986 for (i
= 1; i
< ins
->src_count
; ++i
)
2988 shader_glsl_add_src_param(ins
, &ins
->src
[i
], write_mask
, &src_param
);
2989 shader_addline(buffer
, ", %s", src_param
.param_str
);
2993 shader_addline(buffer
, "));\n");
2996 static void shader_glsl_nop(const struct wined3d_shader_instruction
*ins
) {}
2998 static void shader_glsl_nrm(const struct wined3d_shader_instruction
*ins
)
3000 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
3001 struct glsl_src_param src_param
;
3002 unsigned int mask_size
;
3006 write_mask
= shader_glsl_get_write_mask(ins
->dst
, dst_mask
);
3007 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
3008 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src_param
);
3010 shader_addline(buffer
, "tmp0.x = dot(%s, %s);\n",
3011 src_param
.param_str
, src_param
.param_str
);
3012 shader_glsl_append_dst(buffer
, ins
);
3016 shader_addline(buffer
, "tmp0.x == 0.0 ? vec%u(0.0) : (%s * inversesqrt(tmp0.x)));\n",
3017 mask_size
, src_param
.param_str
);
3021 shader_addline(buffer
, "tmp0.x == 0.0 ? 0.0 : (%s * inversesqrt(tmp0.x)));\n",
3022 src_param
.param_str
);
3026 static void shader_glsl_scalar_op(const struct wined3d_shader_instruction
*ins
)
3028 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
3029 struct glsl_src_param src0_param
;
3030 const char *prefix
, *suffix
;
3031 unsigned int dst_size
;
3032 DWORD dst_write_mask
;
3034 dst_write_mask
= shader_glsl_append_dst(buffer
, ins
);
3035 dst_size
= shader_glsl_get_write_mask_size(dst_write_mask
);
3037 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_3
, &src0_param
);
3039 switch (ins
->handler_idx
)
3041 case WINED3DSIH_EXP
:
3042 case WINED3DSIH_EXPP
:
3047 case WINED3DSIH_LOG
:
3048 case WINED3DSIH_LOGP
:
3049 prefix
= "log2(abs(";
3053 case WINED3DSIH_RCP
:
3058 case WINED3DSIH_RSQ
:
3059 prefix
= "inversesqrt(abs(";
3066 FIXME("Unhandled instruction %#x.\n", ins
->handler_idx
);
3071 shader_addline(buffer
, "vec%u(%s%s%s));\n", dst_size
, prefix
, src0_param
.param_str
, suffix
);
3073 shader_addline(buffer
, "%s%s%s);\n", prefix
, src0_param
.param_str
, suffix
);
3076 /** Process the WINED3DSIO_EXPP instruction in GLSL:
3077 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
3078 * dst.x = 2^(floor(src))
3079 * dst.y = src - floor(src)
3080 * dst.z = 2^src (partial precision is allowed, but optional)
3082 * For 2.0 shaders, just do this (honoring writemask and swizzle):
3083 * dst = 2^src; (partial precision is allowed, but optional)
3085 static void shader_glsl_expp(const struct wined3d_shader_instruction
*ins
)
3087 if (ins
->ctx
->reg_maps
->shader_version
.major
< 2)
3089 struct glsl_src_param src_param
;
3092 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_3
, &src_param
);
3094 shader_addline(ins
->ctx
->buffer
, "tmp0.x = exp2(floor(%s));\n", src_param
.param_str
);
3095 shader_addline(ins
->ctx
->buffer
, "tmp0.y = %s - floor(%s);\n", src_param
.param_str
, src_param
.param_str
);
3096 shader_addline(ins
->ctx
->buffer
, "tmp0.z = exp2(%s);\n", src_param
.param_str
);
3097 shader_addline(ins
->ctx
->buffer
, "tmp0.w = 1.0;\n");
3099 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
3100 shader_glsl_get_write_mask(&ins
->dst
[0], dst_mask
);
3101 shader_addline(ins
->ctx
->buffer
, "tmp0%s);\n", dst_mask
);
3105 shader_glsl_scalar_op(ins
);
3108 static void shader_glsl_to_int(const struct wined3d_shader_instruction
*ins
)
3110 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
3111 struct glsl_src_param src_param
;
3112 unsigned int mask_size
;
3115 write_mask
= shader_glsl_append_dst(buffer
, ins
);
3116 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
3117 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src_param
);
3120 shader_addline(buffer
, "ivec%u(%s));\n", mask_size
, src_param
.param_str
);
3122 shader_addline(buffer
, "int(%s));\n", src_param
.param_str
);
3125 static void shader_glsl_to_float(const struct wined3d_shader_instruction
*ins
)
3127 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
3128 struct glsl_src_param src_param
;
3129 unsigned int mask_size
;
3132 write_mask
= shader_glsl_append_dst(buffer
, ins
);
3133 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
3134 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src_param
);
3137 shader_addline(buffer
, "vec%u(%s));\n", mask_size
, src_param
.param_str
);
3139 shader_addline(buffer
, "float(%s));\n", src_param
.param_str
);
3142 /** Process signed comparison opcodes in GLSL. */
3143 static void shader_glsl_compare(const struct wined3d_shader_instruction
*ins
)
3145 struct glsl_src_param src0_param
;
3146 struct glsl_src_param src1_param
;
3148 unsigned int mask_size
;
3150 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
3151 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
3152 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
3153 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
3155 if (mask_size
> 1) {
3156 const char *compare
;
3158 switch(ins
->handler_idx
)
3160 case WINED3DSIH_SLT
: compare
= "lessThan"; break;
3161 case WINED3DSIH_SGE
: compare
= "greaterThanEqual"; break;
3162 default: compare
= "";
3163 FIXME("Can't handle opcode %#x\n", ins
->handler_idx
);
3166 shader_addline(ins
->ctx
->buffer
, "vec%d(%s(%s, %s)));\n", mask_size
, compare
,
3167 src0_param
.param_str
, src1_param
.param_str
);
3169 switch(ins
->handler_idx
)
3171 case WINED3DSIH_SLT
:
3172 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
3173 * to return 0.0 but step returns 1.0 because step is not < x
3174 * An alternative is a bvec compare padded with an unused second component.
3175 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
3176 * issue. Playing with not() is not possible either because not() does not accept
3179 shader_addline(ins
->ctx
->buffer
, "(%s < %s) ? 1.0 : 0.0);\n",
3180 src0_param
.param_str
, src1_param
.param_str
);
3182 case WINED3DSIH_SGE
:
3183 /* Here we can use the step() function and safe a conditional */
3184 shader_addline(ins
->ctx
->buffer
, "step(%s, %s));\n", src1_param
.param_str
, src0_param
.param_str
);
3187 FIXME("Can't handle opcode %#x\n", ins
->handler_idx
);
3193 static void shader_glsl_conditional_move(const struct wined3d_shader_instruction
*ins
)
3195 const char *condition_prefix
, *condition_suffix
;
3196 struct wined3d_shader_dst_param dst
;
3197 struct glsl_src_param src0_param
;
3198 struct glsl_src_param src1_param
;
3199 struct glsl_src_param src2_param
;
3200 BOOL temp_destination
= FALSE
;
3201 DWORD cmp_channel
= 0;
3206 switch (ins
->handler_idx
)
3208 case WINED3DSIH_CMP
:
3209 condition_prefix
= "";
3210 condition_suffix
= " >= 0.0";
3213 case WINED3DSIH_CND
:
3214 condition_prefix
= "";
3215 condition_suffix
= " > 0.5";
3218 case WINED3DSIH_MOVC
:
3219 condition_prefix
= "bool(";
3220 condition_suffix
= ")";
3224 FIXME("Unhandled instruction %#x.\n", ins
->handler_idx
);
3225 condition_prefix
= "<unhandled prefix>";
3226 condition_suffix
= "<unhandled suffix>";
3230 if (shader_is_scalar(&ins
->dst
[0].reg
) || shader_is_scalar(&ins
->src
[0].reg
))
3232 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
3233 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
3234 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
3235 shader_glsl_add_src_param(ins
, &ins
->src
[2], write_mask
, &src2_param
);
3237 shader_addline(ins
->ctx
->buffer
, "%s%s%s ? %s : %s);\n",
3238 condition_prefix
, src0_param
.param_str
, condition_suffix
,
3239 src1_param
.param_str
, src2_param
.param_str
);
3245 /* Splitting the instruction up in multiple lines imposes a problem:
3246 * The first lines may overwrite source parameters of the following lines.
3247 * Deal with that by using a temporary destination register if needed. */
3248 if ((ins
->src
[0].reg
.idx
[0].offset
== dst
.reg
.idx
[0].offset
3249 && ins
->src
[0].reg
.type
== dst
.reg
.type
)
3250 || (ins
->src
[1].reg
.idx
[0].offset
== dst
.reg
.idx
[0].offset
3251 && ins
->src
[1].reg
.type
== dst
.reg
.type
)
3252 || (ins
->src
[2].reg
.idx
[0].offset
== dst
.reg
.idx
[0].offset
3253 && ins
->src
[2].reg
.type
== dst
.reg
.type
))
3254 temp_destination
= TRUE
;
3256 /* Cycle through all source0 channels. */
3257 for (i
= 0; i
< 4; ++i
)
3260 /* Find the destination channels which use the current source0 channel. */
3261 for (j
= 0; j
< 4; ++j
)
3263 if (((ins
->src
[0].swizzle
>> (2 * j
)) & 0x3) == i
)
3265 write_mask
|= WINED3DSP_WRITEMASK_0
<< j
;
3266 cmp_channel
= WINED3DSP_WRITEMASK_0
<< j
;
3269 dst
.write_mask
= ins
->dst
[0].write_mask
& write_mask
;
3271 if (temp_destination
)
3273 if (!(write_mask
= shader_glsl_get_write_mask(&dst
, mask_char
)))
3275 shader_addline(ins
->ctx
->buffer
, "tmp0%s = (", mask_char
);
3277 else if (!(write_mask
= shader_glsl_append_dst_ext(ins
->ctx
->buffer
, ins
, &dst
, dst
.reg
.data_type
)))
3280 shader_glsl_add_src_param(ins
, &ins
->src
[0], cmp_channel
, &src0_param
);
3281 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
3282 shader_glsl_add_src_param(ins
, &ins
->src
[2], write_mask
, &src2_param
);
3284 shader_addline(ins
->ctx
->buffer
, "%s%s%s ? %s : %s);\n",
3285 condition_prefix
, src0_param
.param_str
, condition_suffix
,
3286 src1_param
.param_str
, src2_param
.param_str
);
3289 if (temp_destination
)
3291 shader_glsl_get_write_mask(&ins
->dst
[0], mask_char
);
3292 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
3293 shader_addline(ins
->ctx
->buffer
, "tmp0%s);\n", mask_char
);
3297 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
3298 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
3299 * the compare is done per component of src0. */
3300 static void shader_glsl_cnd(const struct wined3d_shader_instruction
*ins
)
3302 struct glsl_src_param src0_param
;
3303 struct glsl_src_param src1_param
;
3304 struct glsl_src_param src2_param
;
3306 DWORD shader_version
= WINED3D_SHADER_VERSION(ins
->ctx
->reg_maps
->shader_version
.major
,
3307 ins
->ctx
->reg_maps
->shader_version
.minor
);
3309 if (shader_version
< WINED3D_SHADER_VERSION(1, 4))
3311 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
3312 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
3313 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
3314 shader_glsl_add_src_param(ins
, &ins
->src
[2], write_mask
, &src2_param
);
3316 if (ins
->coissue
&& ins
->dst
->write_mask
!= WINED3DSP_WRITEMASK_3
)
3317 shader_addline(ins
->ctx
->buffer
, "%s /* COISSUE! */);\n", src1_param
.param_str
);
3319 shader_addline(ins
->ctx
->buffer
, "%s > 0.5 ? %s : %s);\n",
3320 src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
3324 shader_glsl_conditional_move(ins
);
3327 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
3328 static void shader_glsl_mad(const struct wined3d_shader_instruction
*ins
)
3330 struct glsl_src_param src0_param
;
3331 struct glsl_src_param src1_param
;
3332 struct glsl_src_param src2_param
;
3335 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
3336 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
3337 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
3338 shader_glsl_add_src_param(ins
, &ins
->src
[2], write_mask
, &src2_param
);
3339 shader_addline(ins
->ctx
->buffer
, "(%s * %s) + %s);\n",
3340 src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
3343 /* Handles transforming all WINED3DSIO_M?x? opcodes for
3344 Vertex shaders to GLSL codes */
3345 static void shader_glsl_mnxn(const struct wined3d_shader_instruction
*ins
)
3348 int nComponents
= 0;
3349 struct wined3d_shader_dst_param tmp_dst
= {{0}};
3350 struct wined3d_shader_src_param tmp_src
[2] = {{{0}}};
3351 struct wined3d_shader_instruction tmp_ins
;
3353 memset(&tmp_ins
, 0, sizeof(tmp_ins
));
3355 /* Set constants for the temporary argument */
3356 tmp_ins
.ctx
= ins
->ctx
;
3357 tmp_ins
.dst_count
= 1;
3358 tmp_ins
.dst
= &tmp_dst
;
3359 tmp_ins
.src_count
= 2;
3360 tmp_ins
.src
= tmp_src
;
3362 switch(ins
->handler_idx
)
3364 case WINED3DSIH_M4x4
:
3366 tmp_ins
.handler_idx
= WINED3DSIH_DP4
;
3368 case WINED3DSIH_M4x3
:
3370 tmp_ins
.handler_idx
= WINED3DSIH_DP4
;
3372 case WINED3DSIH_M3x4
:
3374 tmp_ins
.handler_idx
= WINED3DSIH_DP3
;
3376 case WINED3DSIH_M3x3
:
3378 tmp_ins
.handler_idx
= WINED3DSIH_DP3
;
3380 case WINED3DSIH_M3x2
:
3382 tmp_ins
.handler_idx
= WINED3DSIH_DP3
;
3388 tmp_dst
= ins
->dst
[0];
3389 tmp_src
[0] = ins
->src
[0];
3390 tmp_src
[1] = ins
->src
[1];
3391 for (i
= 0; i
< nComponents
; ++i
)
3393 tmp_dst
.write_mask
= WINED3DSP_WRITEMASK_0
<< i
;
3394 shader_glsl_dot(&tmp_ins
);
3395 ++tmp_src
[1].reg
.idx
[0].offset
;
3400 The LRP instruction performs a component-wise linear interpolation
3401 between the second and third operands using the first operand as the
3402 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
3403 This is equivalent to mix(src2, src1, src0);
3405 static void shader_glsl_lrp(const struct wined3d_shader_instruction
*ins
)
3407 struct glsl_src_param src0_param
;
3408 struct glsl_src_param src1_param
;
3409 struct glsl_src_param src2_param
;
3412 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
3414 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
3415 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
3416 shader_glsl_add_src_param(ins
, &ins
->src
[2], write_mask
, &src2_param
);
3418 shader_addline(ins
->ctx
->buffer
, "mix(%s, %s, %s));\n",
3419 src2_param
.param_str
, src1_param
.param_str
, src0_param
.param_str
);
3422 /** Process the WINED3DSIO_LIT instruction in GLSL:
3423 * dst.x = dst.w = 1.0
3424 * dst.y = (src0.x > 0) ? src0.x
3425 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
3426 * where src.w is clamped at +- 128
3428 static void shader_glsl_lit(const struct wined3d_shader_instruction
*ins
)
3430 struct glsl_src_param src0_param
;
3431 struct glsl_src_param src1_param
;
3432 struct glsl_src_param src3_param
;
3435 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
3436 shader_glsl_get_write_mask(&ins
->dst
[0], dst_mask
);
3438 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
3439 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_1
, &src1_param
);
3440 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_3
, &src3_param
);
3442 /* The sdk specifies the instruction like this
3444 * if(src.x > 0.0) dst.y = src.x
3446 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
3449 * (where power = src.w clamped between -128 and 128)
3451 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
3452 * dst.x = 1.0 ... No further explanation needed
3453 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
3454 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
3455 * dst.w = 1.0. ... Nothing fancy.
3457 * So we still have one conditional in there. So do this:
3458 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
3460 * step(0.0, x) will return 1 if src.x > 0.0, and 0 otherwise. So if y is 0 we get pow(0.0 * 1.0, power),
3461 * which sets dst.z to 0. If y > 0, but x = 0.0, we get pow(y * 0.0, power), which results in 0 too.
3462 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to.
3464 * Unfortunately pow(0.0 ^ 0.0) returns NaN on most GPUs, but lit with src.y = 0 and src.w = 0 returns
3465 * a non-NaN value in dst.z. What we return doesn't matter, as long as it is not NaN. Return 0, which is
3466 * what all Windows HW drivers and GL_ARB_vertex_program's LIT do.
3468 shader_addline(ins
->ctx
->buffer
,
3469 "vec4(1.0, max(%s, 0.0), %s == 0.0 ? 0.0 : "
3470 "pow(max(0.0, %s) * step(0.0, %s), clamp(%s, -128.0, 128.0)), 1.0)%s);\n",
3471 src0_param
.param_str
, src3_param
.param_str
, src1_param
.param_str
,
3472 src0_param
.param_str
, src3_param
.param_str
, dst_mask
);
3475 /** Process the WINED3DSIO_DST instruction in GLSL:
3477 * dst.y = src0.x * src0.y
3481 static void shader_glsl_dst(const struct wined3d_shader_instruction
*ins
)
3483 struct glsl_src_param src0y_param
;
3484 struct glsl_src_param src0z_param
;
3485 struct glsl_src_param src1y_param
;
3486 struct glsl_src_param src1w_param
;
3489 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
3490 shader_glsl_get_write_mask(&ins
->dst
[0], dst_mask
);
3492 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_1
, &src0y_param
);
3493 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_2
, &src0z_param
);
3494 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_1
, &src1y_param
);
3495 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_3
, &src1w_param
);
3497 shader_addline(ins
->ctx
->buffer
, "vec4(1.0, %s * %s, %s, %s))%s;\n",
3498 src0y_param
.param_str
, src1y_param
.param_str
, src0z_param
.param_str
, src1w_param
.param_str
, dst_mask
);
3501 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
3502 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
3503 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
3505 * dst.x = cos(src0.?)
3506 * dst.y = sin(src0.?)
3510 static void shader_glsl_sincos(const struct wined3d_shader_instruction
*ins
)
3512 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
3513 struct glsl_src_param src0_param
;
3516 if (ins
->ctx
->reg_maps
->shader_version
.major
< 4)
3518 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
3520 write_mask
= shader_glsl_append_dst(buffer
, ins
);
3523 case WINED3DSP_WRITEMASK_0
:
3524 shader_addline(buffer
, "cos(%s));\n", src0_param
.param_str
);
3527 case WINED3DSP_WRITEMASK_1
:
3528 shader_addline(buffer
, "sin(%s));\n", src0_param
.param_str
);
3531 case (WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
):
3532 shader_addline(buffer
, "vec2(cos(%s), sin(%s)));\n",
3533 src0_param
.param_str
, src0_param
.param_str
);
3537 ERR("Write mask should be .x, .y or .xy\n");
3544 if (ins
->dst
[0].reg
.type
!= WINED3DSPR_NULL
)
3547 if (ins
->dst
[1].reg
.type
!= WINED3DSPR_NULL
)
3551 write_mask
= shader_glsl_get_write_mask(&ins
->dst
[0], dst_mask
);
3552 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
3553 shader_addline(buffer
, "tmp0%s = sin(%s);\n", dst_mask
, src0_param
.param_str
);
3555 write_mask
= shader_glsl_append_dst_ext(buffer
, ins
, &ins
->dst
[1], ins
->dst
[1].reg
.data_type
);
3556 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
3557 shader_addline(buffer
, "cos(%s));\n", src0_param
.param_str
);
3559 shader_glsl_append_dst_ext(buffer
, ins
, &ins
->dst
[0], ins
->dst
[0].reg
.data_type
);
3560 shader_addline(buffer
, "tmp0%s);\n", dst_mask
);
3564 write_mask
= shader_glsl_append_dst_ext(buffer
, ins
, &ins
->dst
[0], ins
->dst
[0].reg
.data_type
);
3565 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
3566 shader_addline(buffer
, "sin(%s));\n", src0_param
.param_str
);
3569 else if (ins
->dst
[1].reg
.type
!= WINED3DSPR_NULL
)
3571 write_mask
= shader_glsl_append_dst_ext(buffer
, ins
, &ins
->dst
[1], ins
->dst
[1].reg
.data_type
);
3572 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
3573 shader_addline(buffer
, "cos(%s));\n", src0_param
.param_str
);
3577 /* sgn in vs_2_0 has 2 extra parameters(registers for temporary storage) which we don't use
3578 * here. But those extra parameters require a dedicated function for sgn, since map2gl would
3579 * generate invalid code
3581 static void shader_glsl_sgn(const struct wined3d_shader_instruction
*ins
)
3583 struct glsl_src_param src0_param
;
3586 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
3587 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
3589 shader_addline(ins
->ctx
->buffer
, "sign(%s));\n", src0_param
.param_str
);
3592 /** Process the WINED3DSIO_LOOP instruction in GLSL:
3593 * Start a for() loop where src1.y is the initial value of aL,
3594 * increment aL by src1.z for a total of src1.x iterations.
3595 * Need to use a temporary variable for this operation.
3597 /* FIXME: I don't think nested loops will work correctly this way. */
3598 static void shader_glsl_loop(const struct wined3d_shader_instruction
*ins
)
3600 struct wined3d_shader_loop_state
*loop_state
= ins
->ctx
->loop_state
;
3601 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
3602 const struct wined3d_shader
*shader
= ins
->ctx
->shader
;
3603 const struct wined3d_shader_lconst
*constant
;
3604 struct glsl_src_param src1_param
;
3605 const DWORD
*control_values
= NULL
;
3607 if (ins
->ctx
->reg_maps
->shader_version
.major
< 4)
3609 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_ALL
, &src1_param
);
3611 /* Try to hardcode the loop control parameters if possible. Direct3D 9
3612 * class hardware doesn't support real varying indexing, but Microsoft
3613 * designed this feature for Shader model 2.x+. If the loop control is
3614 * known at compile time, the GLSL compiler can unroll the loop, and
3615 * replace indirect addressing with direct addressing. */
3616 if (ins
->src
[1].reg
.type
== WINED3DSPR_CONSTINT
)
3618 LIST_FOR_EACH_ENTRY(constant
, &shader
->constantsI
, struct wined3d_shader_lconst
, entry
)
3620 if (constant
->idx
== ins
->src
[1].reg
.idx
[0].offset
)
3622 control_values
= constant
->value
;
3630 struct wined3d_shader_loop_control loop_control
;
3631 loop_control
.count
= control_values
[0];
3632 loop_control
.start
= control_values
[1];
3633 loop_control
.step
= (int)control_values
[2];
3635 if (loop_control
.step
> 0)
3637 shader_addline(buffer
, "for (aL%u = %u; aL%u < (%u * %d + %u); aL%u += %d)\n{\n",
3638 loop_state
->current_depth
, loop_control
.start
,
3639 loop_state
->current_depth
, loop_control
.count
, loop_control
.step
, loop_control
.start
,
3640 loop_state
->current_depth
, loop_control
.step
);
3642 else if (loop_control
.step
< 0)
3644 shader_addline(buffer
, "for (aL%u = %u; aL%u > (%u * %d + %u); aL%u += %d)\n{\n",
3645 loop_state
->current_depth
, loop_control
.start
,
3646 loop_state
->current_depth
, loop_control
.count
, loop_control
.step
, loop_control
.start
,
3647 loop_state
->current_depth
, loop_control
.step
);
3651 shader_addline(buffer
, "for (aL%u = %u, tmpInt%u = 0; tmpInt%u < %u; tmpInt%u++)\n{\n",
3652 loop_state
->current_depth
, loop_control
.start
, loop_state
->current_depth
,
3653 loop_state
->current_depth
, loop_control
.count
,
3654 loop_state
->current_depth
);
3659 shader_addline(buffer
, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z)\n{\n",
3660 loop_state
->current_depth
, loop_state
->current_reg
,
3661 src1_param
.reg_name
, loop_state
->current_depth
, src1_param
.reg_name
,
3662 loop_state
->current_depth
, loop_state
->current_reg
, src1_param
.reg_name
);
3665 ++loop_state
->current_reg
;
3669 shader_addline(buffer
, "for (;;)\n{\n");
3672 ++loop_state
->current_depth
;
3675 static void shader_glsl_end(const struct wined3d_shader_instruction
*ins
)
3677 struct wined3d_shader_loop_state
*loop_state
= ins
->ctx
->loop_state
;
3679 shader_addline(ins
->ctx
->buffer
, "}\n");
3681 if (ins
->handler_idx
== WINED3DSIH_ENDLOOP
)
3683 --loop_state
->current_depth
;
3684 --loop_state
->current_reg
;
3687 if (ins
->handler_idx
== WINED3DSIH_ENDREP
)
3689 --loop_state
->current_depth
;
3693 static void shader_glsl_rep(const struct wined3d_shader_instruction
*ins
)
3695 const struct wined3d_shader
*shader
= ins
->ctx
->shader
;
3696 struct wined3d_shader_loop_state
*loop_state
= ins
->ctx
->loop_state
;
3697 const struct wined3d_shader_lconst
*constant
;
3698 struct glsl_src_param src0_param
;
3699 const DWORD
*control_values
= NULL
;
3701 /* Try to hardcode local values to help the GLSL compiler to unroll and optimize the loop */
3702 if (ins
->src
[0].reg
.type
== WINED3DSPR_CONSTINT
)
3704 LIST_FOR_EACH_ENTRY(constant
, &shader
->constantsI
, struct wined3d_shader_lconst
, entry
)
3706 if (constant
->idx
== ins
->src
[0].reg
.idx
[0].offset
)
3708 control_values
= constant
->value
;
3716 shader_addline(ins
->ctx
->buffer
, "for (tmpInt%d = 0; tmpInt%d < %d; tmpInt%d++) {\n",
3717 loop_state
->current_depth
, loop_state
->current_depth
,
3718 control_values
[0], loop_state
->current_depth
);
3722 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
3723 shader_addline(ins
->ctx
->buffer
, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
3724 loop_state
->current_depth
, loop_state
->current_depth
,
3725 src0_param
.param_str
, loop_state
->current_depth
);
3728 ++loop_state
->current_depth
;
3731 static void shader_glsl_if(const struct wined3d_shader_instruction
*ins
)
3733 struct glsl_src_param src0_param
;
3735 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
3736 shader_addline(ins
->ctx
->buffer
, "if (bool(%s)) {\n", src0_param
.param_str
);
3739 static void shader_glsl_ifc(const struct wined3d_shader_instruction
*ins
)
3741 struct glsl_src_param src0_param
;
3742 struct glsl_src_param src1_param
;
3744 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
3745 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_0
, &src1_param
);
3747 shader_addline(ins
->ctx
->buffer
, "if (%s %s %s) {\n",
3748 src0_param
.param_str
, shader_glsl_get_rel_op(ins
->flags
), src1_param
.param_str
);
3751 static void shader_glsl_else(const struct wined3d_shader_instruction
*ins
)
3753 shader_addline(ins
->ctx
->buffer
, "} else {\n");
3756 static void shader_glsl_emit(const struct wined3d_shader_instruction
*ins
)
3758 shader_addline(ins
->ctx
->buffer
, "EmitVertex();\n");
3761 static void shader_glsl_break(const struct wined3d_shader_instruction
*ins
)
3763 shader_addline(ins
->ctx
->buffer
, "break;\n");
3766 /* FIXME: According to MSDN the compare is done per component. */
3767 static void shader_glsl_breakc(const struct wined3d_shader_instruction
*ins
)
3769 struct glsl_src_param src0_param
;
3770 struct glsl_src_param src1_param
;
3772 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
3773 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_0
, &src1_param
);
3775 shader_addline(ins
->ctx
->buffer
, "if (%s %s %s) break;\n",
3776 src0_param
.param_str
, shader_glsl_get_rel_op(ins
->flags
), src1_param
.param_str
);
3779 static void shader_glsl_breakp(const struct wined3d_shader_instruction
*ins
)
3781 struct glsl_src_param src_param
;
3783 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src_param
);
3784 shader_addline(ins
->ctx
->buffer
, "if (bool(%s)) break;\n", src_param
.param_str
);
3787 static void shader_glsl_label(const struct wined3d_shader_instruction
*ins
)
3789 shader_addline(ins
->ctx
->buffer
, "}\n");
3790 shader_addline(ins
->ctx
->buffer
, "void subroutine%u()\n{\n", ins
->src
[0].reg
.idx
[0].offset
);
3793 static void shader_glsl_call(const struct wined3d_shader_instruction
*ins
)
3795 shader_addline(ins
->ctx
->buffer
, "subroutine%u();\n", ins
->src
[0].reg
.idx
[0].offset
);
3798 static void shader_glsl_callnz(const struct wined3d_shader_instruction
*ins
)
3800 struct glsl_src_param src1_param
;
3802 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_0
, &src1_param
);
3803 shader_addline(ins
->ctx
->buffer
, "if (%s) subroutine%u();\n",
3804 src1_param
.param_str
, ins
->src
[0].reg
.idx
[0].offset
);
3807 static void shader_glsl_ret(const struct wined3d_shader_instruction
*ins
)
3809 /* No-op. The closing } is written when a new function is started, and at the end of the shader. This
3810 * function only suppresses the unhandled instruction warning
3814 /*********************************************
3815 * Pixel Shader Specific Code begins here
3816 ********************************************/
3817 static void shader_glsl_tex(const struct wined3d_shader_instruction
*ins
)
3819 DWORD shader_version
= WINED3D_SHADER_VERSION(ins
->ctx
->reg_maps
->shader_version
.major
,
3820 ins
->ctx
->reg_maps
->shader_version
.minor
);
3821 struct glsl_sample_function sample_function
;
3822 DWORD sample_flags
= 0;
3824 DWORD mask
= 0, swizzle
;
3825 const struct shader_glsl_ctx_priv
*priv
= ins
->ctx
->backend_data
;
3827 /* 1.0-1.4: Use destination register as sampler source.
3828 * 2.0+: Use provided sampler source. */
3829 if (shader_version
< WINED3D_SHADER_VERSION(2,0))
3830 resource_idx
= ins
->dst
[0].reg
.idx
[0].offset
;
3832 resource_idx
= ins
->src
[1].reg
.idx
[0].offset
;
3834 if (shader_version
< WINED3D_SHADER_VERSION(1,4))
3836 DWORD flags
= (priv
->cur_ps_args
->tex_transform
>> resource_idx
* WINED3D_PSARGS_TEXTRANSFORM_SHIFT
)
3837 & WINED3D_PSARGS_TEXTRANSFORM_MASK
;
3838 enum wined3d_shader_resource_type resource_type
= ins
->ctx
->reg_maps
->resource_info
[resource_idx
].type
;
3840 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
3841 if (flags
& WINED3D_PSARGS_PROJECTED
&& resource_type
!= WINED3D_SHADER_RESOURCE_TEXTURE_CUBE
)
3843 sample_flags
|= WINED3D_GLSL_SAMPLE_PROJECTED
;
3844 switch (flags
& ~WINED3D_PSARGS_PROJECTED
)
3846 case WINED3D_TTFF_COUNT1
:
3847 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
3849 case WINED3D_TTFF_COUNT2
:
3850 mask
= WINED3DSP_WRITEMASK_1
;
3852 case WINED3D_TTFF_COUNT3
:
3853 mask
= WINED3DSP_WRITEMASK_2
;
3855 case WINED3D_TTFF_COUNT4
:
3856 case WINED3D_TTFF_DISABLE
:
3857 mask
= WINED3DSP_WRITEMASK_3
;
3862 else if (shader_version
< WINED3D_SHADER_VERSION(2,0))
3864 enum wined3d_shader_src_modifier src_mod
= ins
->src
[0].modifiers
;
3866 if (src_mod
== WINED3DSPSM_DZ
) {
3867 sample_flags
|= WINED3D_GLSL_SAMPLE_PROJECTED
;
3868 mask
= WINED3DSP_WRITEMASK_2
;
3869 } else if (src_mod
== WINED3DSPSM_DW
) {
3870 sample_flags
|= WINED3D_GLSL_SAMPLE_PROJECTED
;
3871 mask
= WINED3DSP_WRITEMASK_3
;
3876 if ((ins
->flags
& WINED3DSI_TEXLD_PROJECT
)
3877 && ins
->ctx
->reg_maps
->resource_info
[resource_idx
].type
!= WINED3D_SHADER_RESOURCE_TEXTURE_CUBE
)
3879 /* ps 2.0 texldp instruction always divides by the fourth component. */
3880 sample_flags
|= WINED3D_GLSL_SAMPLE_PROJECTED
;
3881 mask
= WINED3DSP_WRITEMASK_3
;
3885 if (priv
->cur_ps_args
->np2_fixup
& (1 << resource_idx
))
3886 sample_flags
|= WINED3D_GLSL_SAMPLE_NPOT
;
3888 shader_glsl_get_sample_function(ins
->ctx
, resource_idx
, sample_flags
, &sample_function
);
3889 mask
|= sample_function
.coord_mask
;
3891 if (shader_version
< WINED3D_SHADER_VERSION(2,0)) swizzle
= WINED3DSP_NOSWIZZLE
;
3892 else swizzle
= ins
->src
[1].swizzle
;
3894 /* 1.0-1.3: Use destination register as coordinate source.
3895 1.4+: Use provided coordinate source register. */
3896 if (shader_version
< WINED3D_SHADER_VERSION(1,4))
3899 shader_glsl_write_mask_to_str(mask
, coord_mask
);
3900 shader_glsl_gen_sample_code(ins
, resource_idx
, &sample_function
, swizzle
, NULL
, NULL
, NULL
,
3901 "T%u%s", resource_idx
, coord_mask
);
3905 struct glsl_src_param coord_param
;
3906 shader_glsl_add_src_param(ins
, &ins
->src
[0], mask
, &coord_param
);
3907 if (ins
->flags
& WINED3DSI_TEXLD_BIAS
)
3909 struct glsl_src_param bias
;
3910 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_3
, &bias
);
3911 shader_glsl_gen_sample_code(ins
, resource_idx
, &sample_function
, swizzle
, NULL
, NULL
, bias
.param_str
,
3912 "%s", coord_param
.param_str
);
3914 shader_glsl_gen_sample_code(ins
, resource_idx
, &sample_function
, swizzle
, NULL
, NULL
, NULL
,
3915 "%s", coord_param
.param_str
);
3920 static void shader_glsl_texldd(const struct wined3d_shader_instruction
*ins
)
3922 const struct wined3d_gl_info
*gl_info
= ins
->ctx
->gl_info
;
3923 struct glsl_src_param coord_param
, dx_param
, dy_param
;
3924 DWORD sample_flags
= WINED3D_GLSL_SAMPLE_GRAD
;
3925 struct glsl_sample_function sample_function
;
3927 DWORD swizzle
= ins
->src
[1].swizzle
;
3928 const struct shader_glsl_ctx_priv
*priv
= ins
->ctx
->backend_data
;
3930 if (!gl_info
->supported
[ARB_SHADER_TEXTURE_LOD
] && !gl_info
->supported
[EXT_GPU_SHADER4
])
3932 FIXME("texldd used, but not supported by hardware. Falling back to regular tex\n");
3933 shader_glsl_tex(ins
);
3937 sampler_idx
= ins
->src
[1].reg
.idx
[0].offset
;
3938 if (priv
->cur_ps_args
->np2_fixup
& (1 << sampler_idx
))
3939 sample_flags
|= WINED3D_GLSL_SAMPLE_NPOT
;
3941 shader_glsl_get_sample_function(ins
->ctx
, sampler_idx
, sample_flags
, &sample_function
);
3942 shader_glsl_add_src_param(ins
, &ins
->src
[0], sample_function
.coord_mask
, &coord_param
);
3943 shader_glsl_add_src_param(ins
, &ins
->src
[2], sample_function
.coord_mask
, &dx_param
);
3944 shader_glsl_add_src_param(ins
, &ins
->src
[3], sample_function
.coord_mask
, &dy_param
);
3946 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, swizzle
, dx_param
.param_str
, dy_param
.param_str
, NULL
,
3947 "%s", coord_param
.param_str
);
3950 static void shader_glsl_texldl(const struct wined3d_shader_instruction
*ins
)
3952 const struct wined3d_gl_info
*gl_info
= ins
->ctx
->gl_info
;
3953 struct glsl_src_param coord_param
, lod_param
;
3954 DWORD sample_flags
= WINED3D_GLSL_SAMPLE_LOD
;
3955 struct glsl_sample_function sample_function
;
3957 DWORD swizzle
= ins
->src
[1].swizzle
;
3958 const struct shader_glsl_ctx_priv
*priv
= ins
->ctx
->backend_data
;
3960 sampler_idx
= ins
->src
[1].reg
.idx
[0].offset
;
3961 if (ins
->ctx
->reg_maps
->shader_version
.type
== WINED3D_SHADER_TYPE_PIXEL
3962 && priv
->cur_ps_args
->np2_fixup
& (1 << sampler_idx
))
3963 sample_flags
|= WINED3D_GLSL_SAMPLE_NPOT
;
3965 shader_glsl_get_sample_function(ins
->ctx
, sampler_idx
, sample_flags
, &sample_function
);
3966 shader_glsl_add_src_param(ins
, &ins
->src
[0], sample_function
.coord_mask
, &coord_param
);
3968 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_3
, &lod_param
);
3970 if (!gl_info
->supported
[ARB_SHADER_TEXTURE_LOD
] && !gl_info
->supported
[EXT_GPU_SHADER4
]
3971 && ins
->ctx
->reg_maps
->shader_version
.type
== WINED3D_SHADER_TYPE_PIXEL
)
3973 /* Plain GLSL only supports Lod sampling functions in vertex shaders.
3974 * However, the NVIDIA drivers allow them in fragment shaders as well,
3975 * even without the appropriate extension. */
3976 WARN("Using %s in fragment shader.\n", sample_function
.name
);
3978 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, swizzle
, NULL
, NULL
, lod_param
.param_str
,
3979 "%s", coord_param
.param_str
);
3982 static unsigned int shader_glsl_find_sampler(const struct wined3d_shader_sampler_map
*sampler_map
,
3983 unsigned int resource_idx
, unsigned int sampler_idx
)
3985 struct wined3d_shader_sampler_map_entry
*entries
= sampler_map
->entries
;
3988 for (i
= 0; i
< sampler_map
->count
; ++i
)
3990 if (entries
[i
].resource_idx
== resource_idx
&& entries
[i
].sampler_idx
== sampler_idx
)
3991 return entries
[i
].bind_idx
;
3994 ERR("No GLSL sampler found for resource %u / sampler %u.\n", resource_idx
, sampler_idx
);
3999 static void shader_glsl_sample(const struct wined3d_shader_instruction
*ins
)
4001 struct glsl_sample_function sample_function
;
4002 struct glsl_src_param coord_param
;
4003 unsigned int sampler_idx
;
4005 shader_glsl_get_sample_function(ins
->ctx
, ins
->src
[1].reg
.idx
[0].offset
, 0, &sample_function
);
4006 shader_glsl_add_src_param(ins
, &ins
->src
[0], sample_function
.coord_mask
, &coord_param
);
4007 sampler_idx
= shader_glsl_find_sampler(&ins
->ctx
->reg_maps
->sampler_map
,
4008 ins
->src
[1].reg
.idx
[0].offset
, ins
->src
[2].reg
.idx
[0].offset
);
4009 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
,
4010 NULL
, NULL
, NULL
, "%s", coord_param
.param_str
);
4013 static void shader_glsl_texcoord(const struct wined3d_shader_instruction
*ins
)
4015 /* FIXME: Make this work for more than just 2D textures */
4016 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
4017 DWORD write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
4019 if (!(ins
->ctx
->reg_maps
->shader_version
.major
== 1 && ins
->ctx
->reg_maps
->shader_version
.minor
== 4))
4023 shader_glsl_get_write_mask(&ins
->dst
[0], dst_mask
);
4024 shader_addline(buffer
, "clamp(gl_TexCoord[%u], 0.0, 1.0)%s);\n",
4025 ins
->dst
[0].reg
.idx
[0].offset
, dst_mask
);
4029 enum wined3d_shader_src_modifier src_mod
= ins
->src
[0].modifiers
;
4030 DWORD reg
= ins
->src
[0].reg
.idx
[0].offset
;
4031 char dst_swizzle
[6];
4033 shader_glsl_get_swizzle(&ins
->src
[0], FALSE
, write_mask
, dst_swizzle
);
4035 if (src_mod
== WINED3DSPSM_DZ
)
4037 unsigned int mask_size
= shader_glsl_get_write_mask_size(write_mask
);
4038 struct glsl_src_param div_param
;
4040 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_2
, &div_param
);
4042 if (mask_size
> 1) {
4043 shader_addline(buffer
, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg
, dst_swizzle
, mask_size
, div_param
.param_str
);
4045 shader_addline(buffer
, "gl_TexCoord[%u]%s / %s);\n", reg
, dst_swizzle
, div_param
.param_str
);
4048 else if (src_mod
== WINED3DSPSM_DW
)
4050 unsigned int mask_size
= shader_glsl_get_write_mask_size(write_mask
);
4051 struct glsl_src_param div_param
;
4053 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_3
, &div_param
);
4055 if (mask_size
> 1) {
4056 shader_addline(buffer
, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg
, dst_swizzle
, mask_size
, div_param
.param_str
);
4058 shader_addline(buffer
, "gl_TexCoord[%u]%s / %s);\n", reg
, dst_swizzle
, div_param
.param_str
);
4061 shader_addline(buffer
, "gl_TexCoord[%u]%s);\n", reg
, dst_swizzle
);
4066 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
4067 * Take a 3-component dot product of the TexCoord[dstreg] and src,
4068 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
4069 static void shader_glsl_texdp3tex(const struct wined3d_shader_instruction
*ins
)
4071 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
4072 DWORD sampler_idx
= ins
->dst
[0].reg
.idx
[0].offset
;
4073 struct glsl_sample_function sample_function
;
4074 struct glsl_src_param src0_param
;
4077 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
4079 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
4080 * scalar, and projected sampling would require 4.
4082 * It is a dependent read - not valid with conditional NP2 textures
4084 shader_glsl_get_sample_function(ins
->ctx
, sampler_idx
, 0, &sample_function
);
4085 mask_size
= shader_glsl_get_write_mask_size(sample_function
.coord_mask
);
4090 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
,
4091 "dot(gl_TexCoord[%u].xyz, %s)", sampler_idx
, src0_param
.param_str
);
4095 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
,
4096 "vec2(dot(gl_TexCoord[%u].xyz, %s), 0.0)", sampler_idx
, src0_param
.param_str
);
4100 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
,
4101 "vec3(dot(gl_TexCoord[%u].xyz, %s), 0.0, 0.0)", sampler_idx
, src0_param
.param_str
);
4105 FIXME("Unexpected mask size %u\n", mask_size
);
4110 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
4111 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
4112 static void shader_glsl_texdp3(const struct wined3d_shader_instruction
*ins
)
4114 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
4115 DWORD dstreg
= ins
->dst
[0].reg
.idx
[0].offset
;
4116 struct glsl_src_param src0_param
;
4118 unsigned int mask_size
;
4120 dst_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
4121 mask_size
= shader_glsl_get_write_mask_size(dst_mask
);
4122 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
4124 if (mask_size
> 1) {
4125 shader_addline(ins
->ctx
->buffer
, "vec%d(dot(T%u.xyz, %s)));\n", mask_size
, dstreg
, src0_param
.param_str
);
4127 shader_addline(ins
->ctx
->buffer
, "dot(T%u.xyz, %s));\n", dstreg
, src0_param
.param_str
);
4131 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
4132 * Calculate the depth as dst.x / dst.y */
4133 static void shader_glsl_texdepth(const struct wined3d_shader_instruction
*ins
)
4135 struct glsl_dst_param dst_param
;
4137 shader_glsl_add_dst_param(ins
, &ins
->dst
[0], &dst_param
);
4139 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
4140 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
4141 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
4142 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
4145 shader_addline(ins
->ctx
->buffer
, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n",
4146 dst_param
.reg_name
, dst_param
.reg_name
);
4149 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
4150 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
4151 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
4152 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
4154 static void shader_glsl_texm3x2depth(const struct wined3d_shader_instruction
*ins
)
4156 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
4157 DWORD dstreg
= ins
->dst
[0].reg
.idx
[0].offset
;
4158 struct glsl_src_param src0_param
;
4160 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
4162 shader_addline(ins
->ctx
->buffer
, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg
, src0_param
.param_str
);
4163 shader_addline(ins
->ctx
->buffer
, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
4166 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
4167 * Calculate the 1st of a 2-row matrix multiplication. */
4168 static void shader_glsl_texm3x2pad(const struct wined3d_shader_instruction
*ins
)
4170 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
4171 DWORD reg
= ins
->dst
[0].reg
.idx
[0].offset
;
4172 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
4173 struct glsl_src_param src0_param
;
4175 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
4176 shader_addline(buffer
, "tmp0.x = dot(T%u.xyz, %s);\n", reg
, src0_param
.param_str
);
4179 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
4180 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
4181 static void shader_glsl_texm3x3pad(const struct wined3d_shader_instruction
*ins
)
4183 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
4184 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
4185 struct wined3d_shader_tex_mx
*tex_mx
= ins
->ctx
->tex_mx
;
4186 DWORD reg
= ins
->dst
[0].reg
.idx
[0].offset
;
4187 struct glsl_src_param src0_param
;
4189 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
4190 shader_addline(buffer
, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + tex_mx
->current_row
, reg
, src0_param
.param_str
);
4191 tex_mx
->texcoord_w
[tex_mx
->current_row
++] = reg
;
4194 static void shader_glsl_texm3x2tex(const struct wined3d_shader_instruction
*ins
)
4196 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
4197 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
4198 struct glsl_sample_function sample_function
;
4199 DWORD reg
= ins
->dst
[0].reg
.idx
[0].offset
;
4200 struct glsl_src_param src0_param
;
4202 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
4203 shader_addline(buffer
, "tmp0.y = dot(T%u.xyz, %s);\n", reg
, src0_param
.param_str
);
4205 shader_glsl_get_sample_function(ins
->ctx
, reg
, 0, &sample_function
);
4207 /* Sample the texture using the calculated coordinates */
4208 shader_glsl_gen_sample_code(ins
, reg
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
, "tmp0.xy");
4211 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
4212 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
4213 static void shader_glsl_texm3x3tex(const struct wined3d_shader_instruction
*ins
)
4215 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
4216 struct wined3d_shader_tex_mx
*tex_mx
= ins
->ctx
->tex_mx
;
4217 struct glsl_sample_function sample_function
;
4218 DWORD reg
= ins
->dst
[0].reg
.idx
[0].offset
;
4219 struct glsl_src_param src0_param
;
4221 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
4222 shader_addline(ins
->ctx
->buffer
, "tmp0.z = dot(T%u.xyz, %s);\n", reg
, src0_param
.param_str
);
4224 /* Dependent read, not valid with conditional NP2 */
4225 shader_glsl_get_sample_function(ins
->ctx
, reg
, 0, &sample_function
);
4227 /* Sample the texture using the calculated coordinates */
4228 shader_glsl_gen_sample_code(ins
, reg
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
, "tmp0.xyz");
4230 tex_mx
->current_row
= 0;
4233 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
4234 * Perform the 3rd row of a 3x3 matrix multiply */
4235 static void shader_glsl_texm3x3(const struct wined3d_shader_instruction
*ins
)
4237 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
4238 struct wined3d_shader_tex_mx
*tex_mx
= ins
->ctx
->tex_mx
;
4239 DWORD reg
= ins
->dst
[0].reg
.idx
[0].offset
;
4240 struct glsl_src_param src0_param
;
4243 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
4245 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
4246 shader_glsl_get_write_mask(&ins
->dst
[0], dst_mask
);
4247 shader_addline(ins
->ctx
->buffer
, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg
, src0_param
.param_str
, dst_mask
);
4249 tex_mx
->current_row
= 0;
4252 /* Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
4253 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
4254 static void shader_glsl_texm3x3spec(const struct wined3d_shader_instruction
*ins
)
4256 struct glsl_src_param src0_param
;
4257 struct glsl_src_param src1_param
;
4258 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
4259 struct wined3d_shader_tex_mx
*tex_mx
= ins
->ctx
->tex_mx
;
4260 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
4261 struct glsl_sample_function sample_function
;
4262 DWORD reg
= ins
->dst
[0].reg
.idx
[0].offset
;
4265 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
4266 shader_glsl_add_src_param(ins
, &ins
->src
[1], src_mask
, &src1_param
);
4268 /* Perform the last matrix multiply operation */
4269 shader_addline(buffer
, "tmp0.z = dot(T%u.xyz, %s);\n", reg
, src0_param
.param_str
);
4270 /* Reflection calculation */
4271 shader_addline(buffer
, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param
.param_str
);
4273 /* Dependent read, not valid with conditional NP2 */
4274 shader_glsl_get_sample_function(ins
->ctx
, reg
, 0, &sample_function
);
4275 shader_glsl_write_mask_to_str(sample_function
.coord_mask
, coord_mask
);
4277 /* Sample the texture */
4278 shader_glsl_gen_sample_code(ins
, reg
, &sample_function
, WINED3DSP_NOSWIZZLE
,
4279 NULL
, NULL
, NULL
, "tmp0%s", coord_mask
);
4281 tex_mx
->current_row
= 0;
4284 /* Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
4285 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
4286 static void shader_glsl_texm3x3vspec(const struct wined3d_shader_instruction
*ins
)
4288 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
4289 struct wined3d_shader_tex_mx
*tex_mx
= ins
->ctx
->tex_mx
;
4290 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
4291 struct glsl_sample_function sample_function
;
4292 DWORD reg
= ins
->dst
[0].reg
.idx
[0].offset
;
4293 struct glsl_src_param src0_param
;
4296 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
4298 /* Perform the last matrix multiply operation */
4299 shader_addline(buffer
, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg
, src0_param
.param_str
);
4301 /* Construct the eye-ray vector from w coordinates */
4302 shader_addline(buffer
, "tmp1.xyz = normalize(vec3(gl_TexCoord[%u].w, gl_TexCoord[%u].w, gl_TexCoord[%u].w));\n",
4303 tex_mx
->texcoord_w
[0], tex_mx
->texcoord_w
[1], reg
);
4304 shader_addline(buffer
, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
4306 /* Dependent read, not valid with conditional NP2 */
4307 shader_glsl_get_sample_function(ins
->ctx
, reg
, 0, &sample_function
);
4308 shader_glsl_write_mask_to_str(sample_function
.coord_mask
, coord_mask
);
4310 /* Sample the texture using the calculated coordinates */
4311 shader_glsl_gen_sample_code(ins
, reg
, &sample_function
, WINED3DSP_NOSWIZZLE
,
4312 NULL
, NULL
, NULL
, "tmp0%s", coord_mask
);
4314 tex_mx
->current_row
= 0;
4317 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
4318 * Apply a fake bump map transform.
4319 * texbem is pshader <= 1.3 only, this saves a few version checks
4321 static void shader_glsl_texbem(const struct wined3d_shader_instruction
*ins
)
4323 const struct shader_glsl_ctx_priv
*priv
= ins
->ctx
->backend_data
;
4324 struct glsl_sample_function sample_function
;
4325 struct glsl_src_param coord_param
;
4331 sampler_idx
= ins
->dst
[0].reg
.idx
[0].offset
;
4332 flags
= (priv
->cur_ps_args
->tex_transform
>> sampler_idx
* WINED3D_PSARGS_TEXTRANSFORM_SHIFT
)
4333 & WINED3D_PSARGS_TEXTRANSFORM_MASK
;
4335 /* Dependent read, not valid with conditional NP2 */
4336 shader_glsl_get_sample_function(ins
->ctx
, sampler_idx
, 0, &sample_function
);
4337 mask
= sample_function
.coord_mask
;
4339 shader_glsl_write_mask_to_str(mask
, coord_mask
);
4341 /* With projected textures, texbem only divides the static texture coord,
4342 * not the displacement, so we can't let GL handle this. */
4343 if (flags
& WINED3D_PSARGS_PROJECTED
)
4346 char coord_div_mask
[3];
4347 switch (flags
& ~WINED3D_PSARGS_PROJECTED
)
4349 case WINED3D_TTFF_COUNT1
:
4350 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
4352 case WINED3D_TTFF_COUNT2
:
4353 div_mask
= WINED3DSP_WRITEMASK_1
;
4355 case WINED3D_TTFF_COUNT3
:
4356 div_mask
= WINED3DSP_WRITEMASK_2
;
4358 case WINED3D_TTFF_COUNT4
:
4359 case WINED3D_TTFF_DISABLE
:
4360 div_mask
= WINED3DSP_WRITEMASK_3
;
4363 shader_glsl_write_mask_to_str(div_mask
, coord_div_mask
);
4364 shader_addline(ins
->ctx
->buffer
, "T%u%s /= T%u%s;\n", sampler_idx
, coord_mask
, sampler_idx
, coord_div_mask
);
4367 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
, &coord_param
);
4369 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
,
4370 "T%u%s + vec4(bumpenv_mat%u * %s, 0.0, 0.0)%s", sampler_idx
, coord_mask
, sampler_idx
,
4371 coord_param
.param_str
, coord_mask
);
4373 if (ins
->handler_idx
== WINED3DSIH_TEXBEML
)
4375 struct glsl_src_param luminance_param
;
4376 struct glsl_dst_param dst_param
;
4378 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_2
, &luminance_param
);
4379 shader_glsl_add_dst_param(ins
, &ins
->dst
[0], &dst_param
);
4381 shader_addline(ins
->ctx
->buffer
, "%s%s *= (%s * bumpenv_lum_scale%u + bumpenv_lum_offset%u);\n",
4382 dst_param
.reg_name
, dst_param
.mask_str
,
4383 luminance_param
.param_str
, sampler_idx
, sampler_idx
);
4387 static void shader_glsl_bem(const struct wined3d_shader_instruction
*ins
)
4389 DWORD sampler_idx
= ins
->dst
[0].reg
.idx
[0].offset
;
4390 struct glsl_src_param src0_param
, src1_param
;
4392 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
, &src0_param
);
4393 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
, &src1_param
);
4395 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
4396 shader_addline(ins
->ctx
->buffer
, "%s + bumpenv_mat%u * %s);\n",
4397 src0_param
.param_str
, sampler_idx
, src1_param
.param_str
);
4400 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
4401 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
4402 static void shader_glsl_texreg2ar(const struct wined3d_shader_instruction
*ins
)
4404 DWORD sampler_idx
= ins
->dst
[0].reg
.idx
[0].offset
;
4405 struct glsl_sample_function sample_function
;
4406 struct glsl_src_param src0_param
;
4408 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_ALL
, &src0_param
);
4410 shader_glsl_get_sample_function(ins
->ctx
, sampler_idx
, 0, &sample_function
);
4411 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
,
4412 "%s.wx", src0_param
.reg_name
);
4415 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
4416 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
4417 static void shader_glsl_texreg2gb(const struct wined3d_shader_instruction
*ins
)
4419 DWORD sampler_idx
= ins
->dst
[0].reg
.idx
[0].offset
;
4420 struct glsl_sample_function sample_function
;
4421 struct glsl_src_param src0_param
;
4423 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_ALL
, &src0_param
);
4425 shader_glsl_get_sample_function(ins
->ctx
, sampler_idx
, 0, &sample_function
);
4426 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
,
4427 "%s.yz", src0_param
.reg_name
);
4430 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
4431 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
4432 static void shader_glsl_texreg2rgb(const struct wined3d_shader_instruction
*ins
)
4434 DWORD sampler_idx
= ins
->dst
[0].reg
.idx
[0].offset
;
4435 struct glsl_sample_function sample_function
;
4436 struct glsl_src_param src0_param
;
4438 /* Dependent read, not valid with conditional NP2 */
4439 shader_glsl_get_sample_function(ins
->ctx
, sampler_idx
, 0, &sample_function
);
4440 shader_glsl_add_src_param(ins
, &ins
->src
[0], sample_function
.coord_mask
, &src0_param
);
4442 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
,
4443 "%s", src0_param
.param_str
);
4446 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
4447 * If any of the first 3 components are < 0, discard this pixel */
4448 static void shader_glsl_texkill(const struct wined3d_shader_instruction
*ins
)
4450 struct glsl_dst_param dst_param
;
4452 /* The argument is a destination parameter, and no writemasks are allowed */
4453 shader_glsl_add_dst_param(ins
, &ins
->dst
[0], &dst_param
);
4454 if (ins
->ctx
->reg_maps
->shader_version
.major
>= 2)
4456 if (ins
->ctx
->reg_maps
->shader_version
.major
>= 4)
4457 FIXME("SM4 discard not implemented.\n");
4458 /* 2.0 shaders compare all 4 components in texkill */
4459 shader_addline(ins
->ctx
->buffer
, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param
.reg_name
);
4461 /* 1.X shaders only compare the first 3 components, probably due to the nature of the texkill
4462 * instruction as a tex* instruction, and phase, which kills all a / w components. Even if all
4463 * 4 components are defined, only the first 3 are used
4465 shader_addline(ins
->ctx
->buffer
, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param
.reg_name
);
4469 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
4470 * dst = dot2(src0, src1) + src2 */
4471 static void shader_glsl_dp2add(const struct wined3d_shader_instruction
*ins
)
4473 struct glsl_src_param src0_param
;
4474 struct glsl_src_param src1_param
;
4475 struct glsl_src_param src2_param
;
4477 unsigned int mask_size
;
4479 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
4480 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
4482 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
, &src0_param
);
4483 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
, &src1_param
);
4484 shader_glsl_add_src_param(ins
, &ins
->src
[2], WINED3DSP_WRITEMASK_0
, &src2_param
);
4486 if (mask_size
> 1) {
4487 shader_addline(ins
->ctx
->buffer
, "vec%d(dot(%s, %s) + %s));\n",
4488 mask_size
, src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
4490 shader_addline(ins
->ctx
->buffer
, "dot(%s, %s) + %s);\n",
4491 src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
4495 static void shader_glsl_input_pack(const struct wined3d_shader
*shader
, struct wined3d_string_buffer
*buffer
,
4496 const struct wined3d_shader_signature
*input_signature
,
4497 const struct wined3d_shader_reg_maps
*reg_maps
,
4498 enum vertexprocessing_mode vertexprocessing
)
4502 for (i
= 0; i
< input_signature
->element_count
; ++i
)
4504 const struct wined3d_shader_signature_element
*input
= &input_signature
->elements
[i
];
4505 const char *semantic_name
;
4510 if (!(reg_maps
->input_registers
& (1 << input
->register_idx
)))
4513 semantic_name
= input
->semantic_name
;
4514 semantic_idx
= input
->semantic_idx
;
4515 shader_glsl_write_mask_to_str(input
->mask
, reg_mask
);
4517 if (vertexprocessing
== vertexshader
)
4519 if (input
->sysval_semantic
== WINED3D_SV_POSITION
)
4520 shader_addline(buffer
, "ps_in[%u]%s = vpos%s;\n",
4521 shader
->u
.ps
.input_reg_map
[input
->register_idx
], reg_mask
, reg_mask
);
4523 shader_addline(buffer
, "ps_in[%u]%s = ps_link[%u]%s;\n",
4524 shader
->u
.ps
.input_reg_map
[input
->register_idx
], reg_mask
,
4525 shader
->u
.ps
.input_reg_map
[input
->register_idx
], reg_mask
);
4527 else if (shader_match_semantic(semantic_name
, WINED3D_DECL_USAGE_TEXCOORD
))
4529 if (semantic_idx
< 8 && vertexprocessing
== pretransformed
)
4530 shader_addline(buffer
, "ps_in[%u]%s = gl_TexCoord[%u]%s;\n",
4531 shader
->u
.ps
.input_reg_map
[input
->register_idx
], reg_mask
, semantic_idx
, reg_mask
);
4533 shader_addline(buffer
, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
4534 shader
->u
.ps
.input_reg_map
[input
->register_idx
], reg_mask
, reg_mask
);
4536 else if (shader_match_semantic(semantic_name
, WINED3D_DECL_USAGE_COLOR
))
4539 shader_addline(buffer
, "ps_in[%u]%s = vec4(gl_Color)%s;\n",
4540 shader
->u
.ps
.input_reg_map
[input
->register_idx
], reg_mask
, reg_mask
);
4541 else if (semantic_idx
== 1)
4542 shader_addline(buffer
, "ps_in[%u]%s = vec4(gl_SecondaryColor)%s;\n",
4543 shader
->u
.ps
.input_reg_map
[input
->register_idx
], reg_mask
, reg_mask
);
4545 shader_addline(buffer
, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
4546 shader
->u
.ps
.input_reg_map
[input
->register_idx
], reg_mask
, reg_mask
);
4550 shader_addline(buffer
, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
4551 shader
->u
.ps
.input_reg_map
[input
->register_idx
], reg_mask
, reg_mask
);
4556 /*********************************************
4557 * Vertex Shader Specific Code begins here
4558 ********************************************/
4560 static void add_glsl_program_entry(struct shader_glsl_priv
*priv
, struct glsl_shader_prog_link
*entry
)
4562 struct glsl_program_key key
;
4564 key
.vs_id
= entry
->vs
.id
;
4565 key
.gs_id
= entry
->gs
.id
;
4566 key
.ps_id
= entry
->ps
.id
;
4568 if (wine_rb_put(&priv
->program_lookup
, &key
, &entry
->program_lookup_entry
) == -1)
4570 ERR("Failed to insert program entry.\n");
4574 static struct glsl_shader_prog_link
*get_glsl_program_entry(const struct shader_glsl_priv
*priv
,
4575 GLuint vs_id
, GLuint gs_id
, GLuint ps_id
)
4577 struct wine_rb_entry
*entry
;
4578 struct glsl_program_key key
;
4584 entry
= wine_rb_get(&priv
->program_lookup
, &key
);
4585 return entry
? WINE_RB_ENTRY_VALUE(entry
, struct glsl_shader_prog_link
, program_lookup_entry
) : NULL
;
4588 /* Context activation is done by the caller. */
4589 static void delete_glsl_program_entry(struct shader_glsl_priv
*priv
, const struct wined3d_gl_info
*gl_info
,
4590 struct glsl_shader_prog_link
*entry
)
4592 struct glsl_program_key key
;
4594 key
.vs_id
= entry
->vs
.id
;
4595 key
.gs_id
= entry
->gs
.id
;
4596 key
.ps_id
= entry
->ps
.id
;
4597 wine_rb_remove(&priv
->program_lookup
, &key
);
4599 GL_EXTCALL(glDeleteProgram(entry
->id
));
4601 list_remove(&entry
->vs
.shader_entry
);
4603 list_remove(&entry
->gs
.shader_entry
);
4605 list_remove(&entry
->ps
.shader_entry
);
4606 HeapFree(GetProcessHeap(), 0, entry
->vs
.uniform_f_locations
);
4607 HeapFree(GetProcessHeap(), 0, entry
->ps
.uniform_f_locations
);
4608 HeapFree(GetProcessHeap(), 0, entry
);
4611 static void handle_ps3_input(struct wined3d_string_buffer
*buffer
,
4612 const struct wined3d_gl_info
*gl_info
, const DWORD
*map
,
4613 const struct wined3d_shader_signature
*input_signature
,
4614 const struct wined3d_shader_reg_maps
*reg_maps_in
,
4615 const struct wined3d_shader_signature
*output_signature
,
4616 const struct wined3d_shader_reg_maps
*reg_maps_out
)
4621 unsigned int in_count
= vec4_varyings(3, gl_info
);
4623 char destination
[50];
4625 set
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*set
) * (in_count
+ 2));
4627 for (i
= 0; i
< input_signature
->element_count
; ++i
)
4629 const struct wined3d_shader_signature_element
*input
= &input_signature
->elements
[i
];
4631 if (!(reg_maps_in
->input_registers
& (1 << input
->register_idx
)))
4634 in_idx
= map
[input
->register_idx
];
4635 /* Declared, but not read register */
4638 if (in_idx
>= (in_count
+ 2))
4640 FIXME("More input varyings declared than supported, expect issues.\n");
4644 if (in_idx
== in_count
)
4645 sprintf(destination
, "gl_FrontColor");
4646 else if (in_idx
== in_count
+ 1)
4647 sprintf(destination
, "gl_FrontSecondaryColor");
4649 sprintf(destination
, "ps_link[%u]", in_idx
);
4654 for (j
= 0; j
< output_signature
->element_count
; ++j
)
4656 const struct wined3d_shader_signature_element
*output
= &output_signature
->elements
[j
];
4659 if (!(reg_maps_out
->output_registers
& (1 << output
->register_idx
))
4660 || input
->semantic_idx
!= output
->semantic_idx
4661 || strcmp(input
->semantic_name
, output
->semantic_name
)
4662 || !(mask
= input
->mask
& output
->mask
))
4665 if (set
[in_idx
] == ~0u)
4668 set
[in_idx
] |= mask
;
4669 shader_glsl_write_mask_to_str(mask
, reg_mask
);
4671 shader_addline(buffer
, "%s%s = vs_out[%u]%s;\n",
4672 destination
, reg_mask
, output
->register_idx
, reg_mask
);
4676 for (i
= 0; i
< in_count
+ 2; ++i
)
4680 if (!set
[i
] || set
[i
] == WINED3DSP_WRITEMASK_ALL
)
4683 if (set
[i
] == ~0U) set
[i
] = 0;
4686 if (!(set
[i
] & WINED3DSP_WRITEMASK_0
)) reg_mask
[size
++] = 'x';
4687 if (!(set
[i
] & WINED3DSP_WRITEMASK_1
)) reg_mask
[size
++] = 'y';
4688 if (!(set
[i
] & WINED3DSP_WRITEMASK_2
)) reg_mask
[size
++] = 'z';
4689 if (!(set
[i
] & WINED3DSP_WRITEMASK_3
)) reg_mask
[size
++] = 'w';
4690 reg_mask
[size
] = '\0';
4693 sprintf(destination
, "gl_FrontColor");
4694 else if (i
== in_count
+ 1)
4695 sprintf(destination
, "gl_FrontSecondaryColor");
4697 sprintf(destination
, "ps_link[%u]", i
);
4699 if (size
== 1) shader_addline(buffer
, "%s.%s = 0.0;\n", destination
, reg_mask
);
4700 else shader_addline(buffer
, "%s.%s = vec%u(0.0);\n", destination
, reg_mask
, size
);
4703 HeapFree(GetProcessHeap(), 0, set
);
4706 /* Context activation is done by the caller. */
4707 static GLuint
generate_param_reorder_function(struct wined3d_string_buffer
*buffer
,
4708 const struct wined3d_shader
*vs
, const struct wined3d_shader
*ps
,
4709 const struct wined3d_gl_info
*gl_info
)
4712 DWORD ps_major
= ps
? ps
->reg_maps
.shader_version
.major
: 0;
4714 const char *semantic_name
;
4718 string_buffer_clear(buffer
);
4720 shader_addline(buffer
, "%s\n", shader_glsl_get_version(gl_info
, &vs
->reg_maps
.shader_version
));
4724 shader_addline(buffer
, "void order_ps_input(in vec4 vs_out[%u])\n{\n", vs
->limits
->packed_output
);
4726 for (i
= 0; i
< vs
->output_signature
.element_count
; ++i
)
4728 const struct wined3d_shader_signature_element
*output
= &vs
->output_signature
.elements
[i
];
4731 if (!(vs
->reg_maps
.output_registers
& (1 << output
->register_idx
)))
4734 semantic_name
= output
->semantic_name
;
4735 semantic_idx
= output
->semantic_idx
;
4736 write_mask
= output
->mask
;
4737 shader_glsl_write_mask_to_str(write_mask
, reg_mask
);
4739 if (shader_match_semantic(semantic_name
, WINED3D_DECL_USAGE_COLOR
))
4742 shader_addline(buffer
, "gl_FrontColor%s = vs_out[%u]%s;\n",
4743 reg_mask
, output
->register_idx
, reg_mask
);
4744 else if (semantic_idx
== 1)
4745 shader_addline(buffer
, "gl_FrontSecondaryColor%s = vs_out[%u]%s;\n",
4746 reg_mask
, output
->register_idx
, reg_mask
);
4748 else if (shader_match_semantic(semantic_name
, WINED3D_DECL_USAGE_POSITION
) && !semantic_idx
)
4750 shader_addline(buffer
, "gl_Position%s = vs_out[%u]%s;\n",
4751 reg_mask
, output
->register_idx
, reg_mask
);
4753 else if (shader_match_semantic(semantic_name
, WINED3D_DECL_USAGE_TEXCOORD
))
4755 if (semantic_idx
< 8)
4757 if (!(gl_info
->quirks
& WINED3D_QUIRK_SET_TEXCOORD_W
) || ps_major
> 0)
4758 write_mask
|= WINED3DSP_WRITEMASK_3
;
4760 shader_addline(buffer
, "gl_TexCoord[%u]%s = vs_out[%u]%s;\n",
4761 semantic_idx
, reg_mask
, output
->register_idx
, reg_mask
);
4762 if (!(write_mask
& WINED3DSP_WRITEMASK_3
))
4763 shader_addline(buffer
, "gl_TexCoord[%u].w = 1.0;\n", semantic_idx
);
4766 else if (shader_match_semantic(semantic_name
, WINED3D_DECL_USAGE_PSIZE
))
4768 shader_addline(buffer
, "gl_PointSize = vs_out[%u].%c;\n", output
->register_idx
, reg_mask
[1]);
4770 else if (shader_match_semantic(semantic_name
, WINED3D_DECL_USAGE_FOG
))
4772 shader_addline(buffer
, "gl_FogFragCoord = clamp(vs_out[%u].%c, 0.0, 1.0);\n",
4773 output
->register_idx
, reg_mask
[1]);
4776 shader_addline(buffer
, "}\n");
4780 UINT in_count
= min(vec4_varyings(ps_major
, gl_info
), ps
->limits
->packed_input
);
4781 /* This one is tricky: a 3.0 pixel shader reads from a 3.0 vertex shader */
4782 shader_addline(buffer
, "varying vec4 ps_link[%u];\n", in_count
);
4783 shader_addline(buffer
, "void order_ps_input(in vec4 vs_out[%u])\n{\n", vs
->limits
->packed_output
);
4785 /* First, sort out position and point size. Those are not passed to the pixel shader */
4786 for (i
= 0; i
< vs
->output_signature
.element_count
; ++i
)
4788 const struct wined3d_shader_signature_element
*output
= &vs
->output_signature
.elements
[i
];
4790 if (!(vs
->reg_maps
.output_registers
& (1 << output
->register_idx
)))
4793 semantic_name
= output
->semantic_name
;
4794 semantic_idx
= output
->semantic_idx
;
4795 shader_glsl_write_mask_to_str(output
->mask
, reg_mask
);
4797 if (shader_match_semantic(semantic_name
, WINED3D_DECL_USAGE_POSITION
) && !semantic_idx
)
4799 shader_addline(buffer
, "gl_Position%s = vs_out[%u]%s;\n",
4800 reg_mask
, output
->register_idx
, reg_mask
);
4802 else if (shader_match_semantic(semantic_name
, WINED3D_DECL_USAGE_PSIZE
))
4804 shader_addline(buffer
, "gl_PointSize = vs_out[%u].%c;\n", output
->register_idx
, reg_mask
[1]);
4808 /* Then, fix the pixel shader input */
4809 handle_ps3_input(buffer
, gl_info
, ps
->u
.ps
.input_reg_map
, &ps
->input_signature
,
4810 &ps
->reg_maps
, &vs
->output_signature
, &vs
->reg_maps
);
4812 shader_addline(buffer
, "}\n");
4815 ret
= GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER
));
4816 checkGLcall("glCreateShader(GL_VERTEX_SHADER)");
4817 shader_glsl_compile(gl_info
, ret
, buffer
->buffer
);
4822 static void shader_glsl_generate_srgb_write_correction(struct wined3d_string_buffer
*buffer
)
4824 shader_addline(buffer
, "tmp0.xyz = pow(gl_FragData[0].xyz, vec3(srgb_const0.x));\n");
4825 shader_addline(buffer
, "tmp0.xyz = tmp0.xyz * vec3(srgb_const0.y) - vec3(srgb_const0.z);\n");
4826 shader_addline(buffer
, "tmp1.xyz = gl_FragData[0].xyz * vec3(srgb_const0.w);\n");
4827 shader_addline(buffer
, "bvec3 srgb_compare = lessThan(gl_FragData[0].xyz, vec3(srgb_const1.x));\n");
4828 shader_addline(buffer
, "gl_FragData[0].xyz = mix(tmp0.xyz, tmp1.xyz, vec3(srgb_compare));\n");
4829 shader_addline(buffer
, "gl_FragData[0] = clamp(gl_FragData[0], 0.0, 1.0);\n");
4832 static void shader_glsl_generate_fog_code(struct wined3d_string_buffer
*buffer
, enum wined3d_ffp_ps_fog_mode mode
)
4836 case WINED3D_FFP_PS_FOG_OFF
:
4839 case WINED3D_FFP_PS_FOG_LINEAR
:
4840 shader_addline(buffer
, "float Fog = (gl_Fog.end - gl_FogFragCoord) * gl_Fog.scale;\n");
4843 case WINED3D_FFP_PS_FOG_EXP
:
4844 /* Fog = e^-(gl_Fog.density * gl_FogFragCoord) */
4845 shader_addline(buffer
, "float Fog = exp(-gl_Fog.density * gl_FogFragCoord);\n");
4848 case WINED3D_FFP_PS_FOG_EXP2
:
4849 /* Fog = e^-((gl_Fog.density * gl_FogFragCoord)^2) */
4850 shader_addline(buffer
, "float Fog = exp(-gl_Fog.density * gl_Fog.density * gl_FogFragCoord * gl_FogFragCoord);\n");
4854 ERR("Invalid fog mode %#x.\n", mode
);
4858 shader_addline(buffer
, "gl_FragData[0].xyz = mix(gl_Fog.color.xyz, gl_FragData[0].xyz, clamp(Fog, 0.0, 1.0));\n");
4861 /* Context activation is done by the caller. */
4862 static GLuint
shader_glsl_generate_pshader(const struct wined3d_context
*context
,
4863 struct wined3d_string_buffer
*buffer
, const struct wined3d_shader
*shader
,
4864 const struct ps_compile_args
*args
, struct ps_np2fixup_info
*np2fixup_info
)
4866 const struct wined3d_shader_reg_maps
*reg_maps
= &shader
->reg_maps
;
4867 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
4868 const DWORD
*function
= shader
->function
;
4869 struct shader_glsl_ctx_priv priv_ctx
;
4871 /* Create the hw GLSL shader object and assign it as the shader->prgId */
4872 GLuint shader_id
= GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER
));
4874 memset(&priv_ctx
, 0, sizeof(priv_ctx
));
4875 priv_ctx
.cur_ps_args
= args
;
4876 priv_ctx
.cur_np2fixup_info
= np2fixup_info
;
4878 shader_addline(buffer
, "%s\n", shader_glsl_get_version(gl_info
, ®_maps
->shader_version
));
4880 if (gl_info
->supported
[ARB_SHADER_BIT_ENCODING
])
4881 shader_addline(buffer
, "#extension GL_ARB_shader_bit_encoding : enable\n");
4882 if (gl_info
->supported
[ARB_SHADER_TEXTURE_LOD
])
4883 shader_addline(buffer
, "#extension GL_ARB_shader_texture_lod : enable\n");
4884 /* The spec says that it doesn't have to be explicitly enabled, but the
4885 * nvidia drivers write a warning if we don't do so. */
4886 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
4887 shader_addline(buffer
, "#extension GL_ARB_texture_rectangle : enable\n");
4888 if (gl_info
->supported
[ARB_UNIFORM_BUFFER_OBJECT
])
4889 shader_addline(buffer
, "#extension GL_ARB_uniform_buffer_object : enable\n");
4890 if (gl_info
->supported
[EXT_GPU_SHADER4
])
4891 shader_addline(buffer
, "#extension GL_EXT_gpu_shader4 : enable\n");
4893 /* Base Declarations */
4894 shader_generate_glsl_declarations(context
, buffer
, shader
, reg_maps
, &priv_ctx
);
4896 /* Pack 3.0 inputs */
4897 if (reg_maps
->shader_version
.major
>= 3)
4898 shader_glsl_input_pack(shader
, buffer
, &shader
->input_signature
, reg_maps
, args
->vp_mode
);
4900 /* Base Shader Body */
4901 shader_generate_main(shader
, buffer
, reg_maps
, function
, &priv_ctx
);
4903 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly */
4904 if (reg_maps
->shader_version
.major
< 2)
4906 /* Some older cards like GeforceFX ones don't support multiple buffers, so also not gl_FragData */
4907 shader_addline(buffer
, "gl_FragData[0] = R0;\n");
4910 if (args
->srgb_correction
)
4911 shader_glsl_generate_srgb_write_correction(buffer
);
4913 /* SM < 3 does not replace the fog stage. */
4914 if (reg_maps
->shader_version
.major
< 3)
4915 shader_glsl_generate_fog_code(buffer
, args
->fog
);
4917 shader_addline(buffer
, "}\n");
4919 TRACE("Compiling shader object %u.\n", shader_id
);
4920 shader_glsl_compile(gl_info
, shader_id
, buffer
->buffer
);
4925 /* Context activation is done by the caller. */
4926 static GLuint
shader_glsl_generate_vshader(const struct wined3d_context
*context
,
4927 struct wined3d_string_buffer
*buffer
, const struct wined3d_shader
*shader
,
4928 const struct vs_compile_args
*args
)
4930 const struct wined3d_shader_reg_maps
*reg_maps
= &shader
->reg_maps
;
4931 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
4932 const DWORD
*function
= shader
->function
;
4933 struct shader_glsl_ctx_priv priv_ctx
;
4935 /* Create the hw GLSL shader program and assign it as the shader->prgId */
4936 GLuint shader_id
= GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER
));
4938 shader_addline(buffer
, "%s\n", shader_glsl_get_version(gl_info
, ®_maps
->shader_version
));
4940 if (gl_info
->supported
[ARB_DRAW_INSTANCED
])
4941 shader_addline(buffer
, "#extension GL_ARB_draw_instanced : enable\n");
4942 if (gl_info
->supported
[ARB_SHADER_BIT_ENCODING
])
4943 shader_addline(buffer
, "#extension GL_ARB_shader_bit_encoding : enable\n");
4944 if (gl_info
->supported
[ARB_UNIFORM_BUFFER_OBJECT
])
4945 shader_addline(buffer
, "#extension GL_ARB_uniform_buffer_object : enable\n");
4946 if (gl_info
->supported
[EXT_GPU_SHADER4
])
4947 shader_addline(buffer
, "#extension GL_EXT_gpu_shader4 : enable\n");
4949 memset(&priv_ctx
, 0, sizeof(priv_ctx
));
4950 priv_ctx
.cur_vs_args
= args
;
4952 /* Base Declarations */
4953 shader_generate_glsl_declarations(context
, buffer
, shader
, reg_maps
, &priv_ctx
);
4955 /* Base Shader Body */
4956 shader_generate_main(shader
, buffer
, reg_maps
, function
, &priv_ctx
);
4958 /* Unpack outputs */
4959 shader_addline(buffer
, "order_ps_input(vs_out);\n");
4961 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
4962 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
4963 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
4964 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0)
4966 if (args
->fog_src
== VS_FOG_Z
)
4967 shader_addline(buffer
, "gl_FogFragCoord = gl_Position.z;\n");
4968 else if (!reg_maps
->fog
)
4969 shader_addline(buffer
, "gl_FogFragCoord = 0.0;\n");
4971 /* We always store the clipplanes without y inversion */
4972 if (args
->clip_enabled
)
4973 shader_addline(buffer
, "gl_ClipVertex = gl_Position;\n");
4975 /* Write the final position.
4977 * OpenGL coordinates specify the center of the pixel while d3d coords specify
4978 * the corner. The offsets are stored in z and w in posFixup. posFixup.y contains
4979 * 1.0 or -1.0 to turn the rendering upside down for offscreen rendering. PosFixup.x
4980 * contains 1.0 to allow a mad.
4982 shader_addline(buffer
, "gl_Position.y = gl_Position.y * posFixup.y;\n");
4983 shader_addline(buffer
, "gl_Position.xy += posFixup.zw * gl_Position.ww;\n");
4985 /* Z coord [0;1]->[-1;1] mapping, see comment in transform_projection in state.c
4987 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However, shaders are run
4988 * before the homogeneous divide, so we have to take the w into account: z = ((z / w) * 2 - 1) * w,
4989 * which is the same as z = z * 2 - w.
4991 shader_addline(buffer
, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
4993 shader_addline(buffer
, "}\n");
4995 TRACE("Compiling shader object %u.\n", shader_id
);
4996 shader_glsl_compile(gl_info
, shader_id
, buffer
->buffer
);
5001 /* Context activation is done by the caller. */
5002 static GLuint
shader_glsl_generate_geometry_shader(const struct wined3d_context
*context
,
5003 struct wined3d_string_buffer
*buffer
, const struct wined3d_shader
*shader
)
5005 const struct wined3d_shader_reg_maps
*reg_maps
= &shader
->reg_maps
;
5006 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
5007 const DWORD
*function
= shader
->function
;
5008 struct shader_glsl_ctx_priv priv_ctx
;
5011 shader_id
= GL_EXTCALL(glCreateShader(GL_GEOMETRY_SHADER
));
5013 shader_addline(buffer
, "%s\n", shader_glsl_get_version(gl_info
, ®_maps
->shader_version
));
5015 if (gl_info
->supported
[ARB_GEOMETRY_SHADER4
])
5016 shader_addline(buffer
, "#extension GL_ARB_geometry_shader4 : enable\n");
5017 if (gl_info
->supported
[ARB_SHADER_BIT_ENCODING
])
5018 shader_addline(buffer
, "#extension GL_ARB_shader_bit_encoding : enable\n");
5019 if (gl_info
->supported
[ARB_UNIFORM_BUFFER_OBJECT
])
5020 shader_addline(buffer
, "#extension GL_ARB_uniform_buffer_object : enable\n");
5021 if (gl_info
->supported
[EXT_GPU_SHADER4
])
5022 shader_addline(buffer
, "#extension GL_EXT_gpu_shader4 : enable\n");
5024 memset(&priv_ctx
, 0, sizeof(priv_ctx
));
5025 shader_generate_glsl_declarations(context
, buffer
, shader
, reg_maps
, &priv_ctx
);
5026 shader_generate_main(shader
, buffer
, reg_maps
, function
, &priv_ctx
);
5027 shader_addline(buffer
, "}\n");
5029 TRACE("Compiling shader object %u.\n", shader_id
);
5030 shader_glsl_compile(gl_info
, shader_id
, buffer
->buffer
);
5035 static GLuint
find_glsl_pshader(const struct wined3d_context
*context
,
5036 struct wined3d_string_buffer
*buffer
, struct wined3d_shader
*shader
,
5037 const struct ps_compile_args
*args
, const struct ps_np2fixup_info
**np2fixup_info
)
5039 struct glsl_ps_compiled_shader
*gl_shaders
, *new_array
;
5040 struct glsl_shader_private
*shader_data
;
5041 struct ps_np2fixup_info
*np2fixup
;
5046 if (!shader
->backend_data
)
5048 shader
->backend_data
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*shader_data
));
5049 if (!shader
->backend_data
)
5051 ERR("Failed to allocate backend data.\n");
5055 shader_data
= shader
->backend_data
;
5056 gl_shaders
= shader_data
->gl_shaders
.ps
;
5058 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
5059 * so a linear search is more performant than a hashmap or a binary search
5060 * (cache coherency etc)
5062 for (i
= 0; i
< shader_data
->num_gl_shaders
; ++i
)
5064 if (!memcmp(&gl_shaders
[i
].args
, args
, sizeof(*args
)))
5066 if (args
->np2_fixup
)
5067 *np2fixup_info
= &gl_shaders
[i
].np2fixup
;
5068 return gl_shaders
[i
].id
;
5072 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader
);
5073 if(shader_data
->shader_array_size
== shader_data
->num_gl_shaders
) {
5074 if (shader_data
->num_gl_shaders
)
5076 new_size
= shader_data
->shader_array_size
+ max(1, shader_data
->shader_array_size
/ 2);
5077 new_array
= HeapReAlloc(GetProcessHeap(), 0, shader_data
->gl_shaders
.ps
,
5078 new_size
* sizeof(*gl_shaders
));
5082 new_array
= HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders
));
5087 ERR("Out of memory\n");
5090 shader_data
->gl_shaders
.ps
= new_array
;
5091 shader_data
->shader_array_size
= new_size
;
5092 gl_shaders
= new_array
;
5095 gl_shaders
[shader_data
->num_gl_shaders
].args
= *args
;
5097 np2fixup
= &gl_shaders
[shader_data
->num_gl_shaders
].np2fixup
;
5098 memset(np2fixup
, 0, sizeof(*np2fixup
));
5099 *np2fixup_info
= args
->np2_fixup
? np2fixup
: NULL
;
5101 pixelshader_update_resource_types(shader
, args
->tex_types
);
5103 string_buffer_clear(buffer
);
5104 ret
= shader_glsl_generate_pshader(context
, buffer
, shader
, args
, np2fixup
);
5105 gl_shaders
[shader_data
->num_gl_shaders
++].id
= ret
;
5110 static inline BOOL
vs_args_equal(const struct vs_compile_args
*stored
, const struct vs_compile_args
*new,
5111 const DWORD use_map
) {
5112 if((stored
->swizzle_map
& use_map
) != new->swizzle_map
) return FALSE
;
5113 if((stored
->clip_enabled
) != new->clip_enabled
) return FALSE
;
5114 return stored
->fog_src
== new->fog_src
;
5117 static GLuint
find_glsl_vshader(const struct wined3d_context
*context
,
5118 struct wined3d_string_buffer
*buffer
, struct wined3d_shader
*shader
,
5119 const struct vs_compile_args
*args
)
5123 DWORD use_map
= context
->stream_info
.use_map
;
5124 struct glsl_vs_compiled_shader
*gl_shaders
, *new_array
;
5125 struct glsl_shader_private
*shader_data
;
5128 if (!shader
->backend_data
)
5130 shader
->backend_data
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*shader_data
));
5131 if (!shader
->backend_data
)
5133 ERR("Failed to allocate backend data.\n");
5137 shader_data
= shader
->backend_data
;
5138 gl_shaders
= shader_data
->gl_shaders
.vs
;
5140 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
5141 * so a linear search is more performant than a hashmap or a binary search
5142 * (cache coherency etc)
5144 for (i
= 0; i
< shader_data
->num_gl_shaders
; ++i
)
5146 if (vs_args_equal(&gl_shaders
[i
].args
, args
, use_map
))
5147 return gl_shaders
[i
].id
;
5150 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader
);
5152 if(shader_data
->shader_array_size
== shader_data
->num_gl_shaders
) {
5153 if (shader_data
->num_gl_shaders
)
5155 new_size
= shader_data
->shader_array_size
+ max(1, shader_data
->shader_array_size
/ 2);
5156 new_array
= HeapReAlloc(GetProcessHeap(), 0, shader_data
->gl_shaders
.vs
,
5157 new_size
* sizeof(*gl_shaders
));
5161 new_array
= HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders
));
5166 ERR("Out of memory\n");
5169 shader_data
->gl_shaders
.vs
= new_array
;
5170 shader_data
->shader_array_size
= new_size
;
5171 gl_shaders
= new_array
;
5174 gl_shaders
[shader_data
->num_gl_shaders
].args
= *args
;
5176 string_buffer_clear(buffer
);
5177 ret
= shader_glsl_generate_vshader(context
, buffer
, shader
, args
);
5178 gl_shaders
[shader_data
->num_gl_shaders
++].id
= ret
;
5183 static GLuint
find_glsl_geometry_shader(const struct wined3d_context
*context
,
5184 struct wined3d_string_buffer
*buffer
, struct wined3d_shader
*shader
)
5186 struct glsl_gs_compiled_shader
*gl_shaders
;
5187 struct glsl_shader_private
*shader_data
;
5190 if (!shader
->backend_data
)
5192 if (!(shader
->backend_data
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*shader_data
))))
5194 ERR("Failed to allocate backend data.\n");
5198 shader_data
= shader
->backend_data
;
5199 gl_shaders
= shader_data
->gl_shaders
.gs
;
5201 if (shader_data
->num_gl_shaders
)
5202 return gl_shaders
[0].id
;
5204 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader
);
5206 if (!(shader_data
->gl_shaders
.gs
= HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders
))))
5208 ERR("Failed to allocate GL shader array.\n");
5211 shader_data
->shader_array_size
= 1;
5212 gl_shaders
= shader_data
->gl_shaders
.gs
;
5214 string_buffer_clear(buffer
);
5215 ret
= shader_glsl_generate_geometry_shader(context
, buffer
, shader
);
5216 gl_shaders
[shader_data
->num_gl_shaders
++].id
= ret
;
5221 static const char *shader_glsl_ffp_mcs(enum wined3d_material_color_source mcs
, const char *material
)
5225 case WINED3D_MCS_MATERIAL
:
5227 case WINED3D_MCS_COLOR1
:
5229 case WINED3D_MCS_COLOR2
:
5230 return "gl_SecondaryColor";
5232 ERR("Invalid material color source %#x.\n", mcs
);
5237 static void shader_glsl_ffp_vertex_lighting(struct wined3d_string_buffer
*buffer
,
5238 const struct wined3d_ffp_vs_settings
*settings
, const struct wined3d_gl_info
*gl_info
)
5240 const char *diffuse
, *specular
, *emission
, *ambient
;
5241 enum wined3d_light_type light_type
;
5244 if (!settings
->lighting
)
5246 shader_addline(buffer
, "gl_FrontColor = gl_Color;\n");
5247 shader_addline(buffer
, "gl_FrontSecondaryColor = gl_SecondaryColor;\n");
5251 shader_addline(buffer
, "vec3 ambient = gl_LightModel.ambient.xyz;\n");
5252 shader_addline(buffer
, "vec3 diffuse = vec3(0.0);\n");
5253 shader_addline(buffer
, "vec4 specular = vec4(0.0);\n");
5254 shader_addline(buffer
, "vec3 dir, dst;\n");
5255 shader_addline(buffer
, "float att, t;\n");
5257 ambient
= shader_glsl_ffp_mcs(settings
->ambient_source
, "gl_FrontMaterial.ambient");
5258 diffuse
= shader_glsl_ffp_mcs(settings
->diffuse_source
, "gl_FrontMaterial.diffuse");
5259 specular
= shader_glsl_ffp_mcs(settings
->specular_source
, "gl_FrontMaterial.specular");
5260 emission
= shader_glsl_ffp_mcs(settings
->emission_source
, "gl_FrontMaterial.emission");
5262 for (i
= 0; i
< MAX_ACTIVE_LIGHTS
; ++i
)
5264 light_type
= (settings
->light_type
>> WINED3D_FFP_LIGHT_TYPE_SHIFT(i
)) & WINED3D_FFP_LIGHT_TYPE_MASK
;
5267 case WINED3D_LIGHT_POINT
:
5268 shader_addline(buffer
, "dir = gl_LightSource[%u].position.xyz - ec_pos.xyz;\n", i
);
5269 shader_addline(buffer
, "dst.z = dot(dir, dir);\n");
5270 shader_addline(buffer
, "dst.y = sqrt(dst.z);\n");
5271 shader_addline(buffer
, "dst.x = 1.0;\n");
5272 shader_addline(buffer
, "att = dot(dst.xyz, vec3(gl_LightSource[%u].constantAttenuation,"
5273 " gl_LightSource[%u].linearAttenuation, gl_LightSource[%u].quadraticAttenuation));\n", i
, i
, i
);
5274 shader_addline(buffer
, "ambient += gl_LightSource[%u].ambient.xyz / att;\n", i
);
5275 if (!settings
->normal
)
5277 shader_addline(buffer
, "dir = normalize(dir);\n");
5278 shader_addline(buffer
, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
5279 " * gl_LightSource[%u].diffuse.xyz) / att;\n", i
);
5280 if (settings
->localviewer
)
5281 shader_addline(buffer
, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
5283 shader_addline(buffer
, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
5284 shader_addline(buffer
, "if (t > 0.0) specular += (pow(t, gl_FrontMaterial.shininess)"
5285 " * gl_LightSource[%u].specular) / att;\n", i
);
5288 case WINED3D_LIGHT_SPOT
:
5289 shader_addline(buffer
, "dir = gl_LightSource[%u].position.xyz - ec_pos.xyz;\n", i
);
5290 shader_addline(buffer
, "dst.z = dot(dir, dir);\n");
5291 shader_addline(buffer
, "dst.y = sqrt(dst.z);\n");
5292 shader_addline(buffer
, "dst.x = 1.0;\n");
5293 shader_addline(buffer
, "dir = normalize(dir);\n");
5294 shader_addline(buffer
, "t = dot(-dir, normalize(gl_LightSource[%u].spotDirection));\n", i
);
5295 shader_addline(buffer
, "if (t < gl_LightSource[%u].spotCosCutoff) att = 0.0;\n", i
);
5296 shader_addline(buffer
, "else att = pow(t, gl_LightSource[%u].spotExponent)"
5297 " / dot(dst.xyz, vec3(gl_LightSource[%u].constantAttenuation,"
5298 " gl_LightSource[%u].linearAttenuation, gl_LightSource[%u].quadraticAttenuation));\n",
5300 shader_addline(buffer
, "ambient += gl_LightSource[%u].ambient.xyz * att;\n", i
);
5301 if (!settings
->normal
)
5303 shader_addline(buffer
, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
5304 " * gl_LightSource[%u].diffuse.xyz) * att;\n", i
);
5305 if (settings
->localviewer
)
5306 shader_addline(buffer
, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
5308 shader_addline(buffer
, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
5309 shader_addline(buffer
, "if (t > 0.0) specular += (pow(t, gl_FrontMaterial.shininess)"
5310 " * gl_LightSource[%u].specular) * att;\n", i
);
5313 case WINED3D_LIGHT_DIRECTIONAL
:
5314 shader_addline(buffer
, "ambient += gl_LightSource[%u].ambient.xyz;\n", i
);
5315 if (!settings
->normal
)
5317 shader_addline(buffer
, "dir = normalize(gl_LightSource[%u].position.xyz);\n", i
);
5318 shader_addline(buffer
, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
5319 " * gl_LightSource[%u].diffuse.xyz;\n", i
);
5320 /* TODO: In the non-local viewer case the halfvector is constant
5321 * and could be precomputed and stored in a uniform. */
5322 if (settings
->localviewer
)
5323 shader_addline(buffer
, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
5325 shader_addline(buffer
, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
5326 shader_addline(buffer
, "if (t > 0.0) specular += pow(t, gl_FrontMaterial.shininess)"
5327 " * gl_LightSource[%u].specular;\n", i
);
5332 FIXME("Unhandled light type %#x.\n", light_type
);
5337 shader_addline(buffer
, "gl_FrontColor.xyz = %s.xyz * ambient + %s.xyz * diffuse + %s.xyz;\n",
5338 ambient
, diffuse
, emission
);
5339 shader_addline(buffer
, "gl_FrontColor.w = %s.w;\n", diffuse
);
5340 shader_addline(buffer
, "gl_FrontSecondaryColor = %s * specular;\n", specular
);
5343 /* Context activation is done by the caller. */
5344 static GLuint
shader_glsl_generate_ffp_vertex_shader(struct wined3d_string_buffer
*buffer
,
5345 const struct wined3d_ffp_vs_settings
*settings
, const struct wined3d_gl_info
*gl_info
)
5350 string_buffer_clear(buffer
);
5352 shader_addline(buffer
, "#version 120\n");
5353 shader_addline(buffer
, "\n");
5355 shader_addline(buffer
, "uniform mat4 ffp_modelview_matrix;\n");
5356 shader_addline(buffer
, "uniform mat4 ffp_projection_matrix;\n");
5357 shader_addline(buffer
, "uniform mat3 ffp_normal_matrix;\n");
5359 shader_addline(buffer
, "\nvoid main()\n{\n");
5360 shader_addline(buffer
, "float m;\n");
5361 shader_addline(buffer
, "vec3 r;\n");
5363 if (settings
->transformed
)
5365 shader_addline(buffer
, "vec4 ec_pos = vec4(gl_Vertex.xyz, 1.0);\n");
5366 shader_addline(buffer
, "gl_Position = ffp_projection_matrix * ec_pos;\n");
5367 shader_addline(buffer
, "if (gl_Vertex.w != 0.0) gl_Position /= gl_Vertex.w;\n");
5371 shader_addline(buffer
, "vec4 ec_pos = ffp_modelview_matrix * gl_Vertex;\n");
5372 shader_addline(buffer
, "gl_Position = ffp_projection_matrix * ec_pos;\n");
5373 if (settings
->clipping
)
5374 shader_addline(buffer
, "gl_ClipVertex = ec_pos;\n");
5375 shader_addline(buffer
, "ec_pos /= ec_pos.w;\n");
5378 if (!settings
->normal
)
5379 shader_addline(buffer
, "vec3 normal = vec3(0.0);\n");
5380 else if (settings
->normalize
)
5381 shader_addline(buffer
, "vec3 normal = normalize(ffp_normal_matrix * gl_Normal);\n");
5383 shader_addline(buffer
, "vec3 normal = ffp_normal_matrix * gl_Normal;\n");
5385 shader_glsl_ffp_vertex_lighting(buffer
, settings
, gl_info
);
5387 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
5389 switch (settings
->texgen
[i
] << WINED3D_FFP_TCI_SHIFT
)
5391 case WINED3DTSS_TCI_PASSTHRU
:
5392 if (settings
->texcoords
& (1 << i
))
5393 shader_addline(buffer
, "gl_TexCoord[%u] = gl_TextureMatrix[%u] * gl_MultiTexCoord%d;\n",
5397 case WINED3DTSS_TCI_CAMERASPACENORMAL
:
5398 shader_addline(buffer
, "gl_TexCoord[%u] = gl_TextureMatrix[%u] * vec4(normal, 1.0);\n", i
, i
);
5401 case WINED3DTSS_TCI_CAMERASPACEPOSITION
:
5402 shader_addline(buffer
, "gl_TexCoord[%u] = gl_TextureMatrix[%u] * ec_pos;\n", i
, i
);
5405 case WINED3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR
:
5406 shader_addline(buffer
, "gl_TexCoord[%u] = gl_TextureMatrix[%u]"
5407 " * vec4(reflect(normalize(ec_pos.xyz), normal), 1.0);\n", i
, i
);
5410 case WINED3DTSS_TCI_SPHEREMAP
:
5411 shader_addline(buffer
, "r = reflect(normalize(ec_pos.xyz), normal);\n");
5412 shader_addline(buffer
, "m = 2.0 * length(vec3(r.x, r.y, r.z + 1.0));\n");
5413 shader_addline(buffer
, "gl_TexCoord[%u] = gl_TextureMatrix[%u]"
5414 " * vec4(r.x / m + 0.5, r.y / m + 0.5, 0.0, 1.0);\n", i
, i
);
5418 ERR("Unhandled texgen %#x.\n", settings
->texgen
[i
]);
5423 switch (settings
->fog_mode
)
5425 case WINED3D_FFP_VS_FOG_OFF
:
5428 case WINED3D_FFP_VS_FOG_FOGCOORD
:
5429 shader_addline(buffer
, "gl_FogFragCoord = gl_SecondaryColor.w * 255.0;\n");
5432 case WINED3D_FFP_VS_FOG_RANGE
:
5433 shader_addline(buffer
, "gl_FogFragCoord = length(ec_pos.xyz);\n");
5436 case WINED3D_FFP_VS_FOG_DEPTH
:
5437 if (settings
->ortho_fog
)
5438 /* Need to undo the [0.0 - 1.0] -> [-1.0 - 1.0] transformation from D3D to GL coordinates. */
5439 shader_addline(buffer
, "gl_FogFragCoord = gl_Position.z * 0.5 + 0.5;\n");
5440 else if (settings
->transformed
)
5441 shader_addline(buffer
, "gl_FogFragCoord = ec_pos.z;\n");
5443 shader_addline(buffer
, "gl_FogFragCoord = abs(ec_pos.z);\n");
5447 ERR("Unhandled fog mode %#x.\n", settings
->fog_mode
);
5451 if (settings
->point_size
)
5453 shader_addline(buffer
, "gl_PointSize = gl_Point.size / sqrt(gl_Point.distanceConstantAttenuation"
5454 " + gl_Point.distanceLinearAttenuation * length(ec_pos.xyz)"
5455 " + gl_Point.distanceQuadraticAttenuation * dot(ec_pos.xyz, ec_pos.xyz));\n");
5456 shader_addline(buffer
, "gl_PointSize = clamp(gl_PointSize, gl_Point.sizeMin, gl_Point.sizeMax);\n");
5459 shader_addline(buffer
, "}\n");
5461 shader_obj
= GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER
));
5462 shader_glsl_compile(gl_info
, shader_obj
, buffer
->buffer
);
5467 static const char *shader_glsl_get_ffp_fragment_op_arg(struct wined3d_string_buffer
*buffer
,
5468 DWORD argnum
, unsigned int stage
, DWORD arg
)
5472 if (arg
== ARG_UNUSED
)
5473 return "<unused arg>";
5475 switch (arg
& WINED3DTA_SELECTMASK
)
5477 case WINED3DTA_DIFFUSE
:
5481 case WINED3DTA_CURRENT
:
5488 case WINED3DTA_TEXTURE
:
5491 case 0: ret
= "tex0"; break;
5492 case 1: ret
= "tex1"; break;
5493 case 2: ret
= "tex2"; break;
5494 case 3: ret
= "tex3"; break;
5495 case 4: ret
= "tex4"; break;
5496 case 5: ret
= "tex5"; break;
5497 case 6: ret
= "tex6"; break;
5498 case 7: ret
= "tex7"; break;
5500 ret
= "<invalid texture>";
5505 case WINED3DTA_TFACTOR
:
5509 case WINED3DTA_SPECULAR
:
5510 ret
= "gl_SecondaryColor";
5513 case WINED3DTA_TEMP
:
5517 case WINED3DTA_CONSTANT
:
5520 case 0: ret
= "tss_const0"; break;
5521 case 1: ret
= "tss_const1"; break;
5522 case 2: ret
= "tss_const2"; break;
5523 case 3: ret
= "tss_const3"; break;
5524 case 4: ret
= "tss_const4"; break;
5525 case 5: ret
= "tss_const5"; break;
5526 case 6: ret
= "tss_const6"; break;
5527 case 7: ret
= "tss_const7"; break;
5529 ret
= "<invalid constant>";
5535 return "<unhandled arg>";
5538 if (arg
& WINED3DTA_COMPLEMENT
)
5540 shader_addline(buffer
, "arg%u = vec4(1.0) - %s;\n", argnum
, ret
);
5543 else if (argnum
== 1)
5545 else if (argnum
== 2)
5549 if (arg
& WINED3DTA_ALPHAREPLICATE
)
5551 shader_addline(buffer
, "arg%u = vec4(%s.w);\n", argnum
, ret
);
5554 else if (argnum
== 1)
5556 else if (argnum
== 2)
5563 static void shader_glsl_ffp_fragment_op(struct wined3d_string_buffer
*buffer
, unsigned int stage
, BOOL color
,
5564 BOOL alpha
, DWORD dst
, DWORD op
, DWORD dw_arg0
, DWORD dw_arg1
, DWORD dw_arg2
)
5566 const char *dstmask
, *dstreg
, *arg0
, *arg1
, *arg2
;
5576 dstreg
= "temp_reg";
5580 arg0
= shader_glsl_get_ffp_fragment_op_arg(buffer
, 0, stage
, dw_arg0
);
5581 arg1
= shader_glsl_get_ffp_fragment_op_arg(buffer
, 1, stage
, dw_arg1
);
5582 arg2
= shader_glsl_get_ffp_fragment_op_arg(buffer
, 2, stage
, dw_arg2
);
5586 case WINED3D_TOP_DISABLE
:
5588 shader_addline(buffer
, "%s%s = gl_Color%s;\n", dstreg
, dstmask
, dstmask
);
5591 case WINED3D_TOP_SELECT_ARG1
:
5592 shader_addline(buffer
, "%s%s = %s%s;\n", dstreg
, dstmask
, arg1
, dstmask
);
5595 case WINED3D_TOP_SELECT_ARG2
:
5596 shader_addline(buffer
, "%s%s = %s%s;\n", dstreg
, dstmask
, arg2
, dstmask
);
5599 case WINED3D_TOP_MODULATE
:
5600 shader_addline(buffer
, "%s%s = %s%s * %s%s;\n", dstreg
, dstmask
, arg1
, dstmask
, arg2
, dstmask
);
5603 case WINED3D_TOP_MODULATE_4X
:
5604 shader_addline(buffer
, "%s%s = clamp(%s%s * %s%s * 4.0, 0.0, 1.0);\n",
5605 dstreg
, dstmask
, arg1
, dstmask
, arg2
, dstmask
);
5608 case WINED3D_TOP_MODULATE_2X
:
5609 shader_addline(buffer
, "%s%s = clamp(%s%s * %s%s * 2.0, 0.0, 1.0);\n",
5610 dstreg
, dstmask
, arg1
, dstmask
, arg2
, dstmask
);
5613 case WINED3D_TOP_ADD
:
5614 shader_addline(buffer
, "%s%s = clamp(%s%s + %s%s, 0.0, 1.0);\n",
5615 dstreg
, dstmask
, arg1
, dstmask
, arg2
, dstmask
);
5618 case WINED3D_TOP_ADD_SIGNED
:
5619 shader_addline(buffer
, "%s%s = clamp(%s%s + (%s - vec4(0.5))%s, 0.0, 1.0);\n",
5620 dstreg
, dstmask
, arg1
, dstmask
, arg2
, dstmask
);
5623 case WINED3D_TOP_ADD_SIGNED_2X
:
5624 shader_addline(buffer
, "%s%s = clamp((%s%s + (%s - vec4(0.5))%s) * 2.0, 0.0, 1.0);\n",
5625 dstreg
, dstmask
, arg1
, dstmask
, arg2
, dstmask
);
5628 case WINED3D_TOP_SUBTRACT
:
5629 shader_addline(buffer
, "%s%s = clamp(%s%s - %s%s, 0.0, 1.0);\n",
5630 dstreg
, dstmask
, arg1
, dstmask
, arg2
, dstmask
);
5633 case WINED3D_TOP_ADD_SMOOTH
:
5634 shader_addline(buffer
, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s%s, 0.0, 1.0);\n",
5635 dstreg
, dstmask
, arg1
, dstmask
, arg2
, dstmask
, arg1
, dstmask
);
5638 case WINED3D_TOP_BLEND_DIFFUSE_ALPHA
:
5639 arg0
= shader_glsl_get_ffp_fragment_op_arg(buffer
, 0, stage
, WINED3DTA_DIFFUSE
);
5640 shader_addline(buffer
, "%s%s = mix(%s%s, %s%s, %s.w);\n",
5641 dstreg
, dstmask
, arg2
, dstmask
, arg1
, dstmask
, arg0
);
5644 case WINED3D_TOP_BLEND_TEXTURE_ALPHA
:
5645 arg0
= shader_glsl_get_ffp_fragment_op_arg(buffer
, 0, stage
, WINED3DTA_TEXTURE
);
5646 shader_addline(buffer
, "%s%s = mix(%s%s, %s%s, %s.w);\n",
5647 dstreg
, dstmask
, arg2
, dstmask
, arg1
, dstmask
, arg0
);
5650 case WINED3D_TOP_BLEND_FACTOR_ALPHA
:
5651 arg0
= shader_glsl_get_ffp_fragment_op_arg(buffer
, 0, stage
, WINED3DTA_TFACTOR
);
5652 shader_addline(buffer
, "%s%s = mix(%s%s, %s%s, %s.w);\n",
5653 dstreg
, dstmask
, arg2
, dstmask
, arg1
, dstmask
, arg0
);
5656 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM
:
5657 arg0
= shader_glsl_get_ffp_fragment_op_arg(buffer
, 0, stage
, WINED3DTA_TEXTURE
);
5658 shader_addline(buffer
, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
5659 dstreg
, dstmask
, arg2
, dstmask
, arg0
, arg1
, dstmask
);
5662 case WINED3D_TOP_BLEND_CURRENT_ALPHA
:
5663 arg0
= shader_glsl_get_ffp_fragment_op_arg(buffer
, 0, stage
, WINED3DTA_CURRENT
);
5664 shader_addline(buffer
, "%s%s = mix(%s%s, %s%s, %s.w);\n",
5665 dstreg
, dstmask
, arg2
, dstmask
, arg1
, dstmask
, arg0
);
5668 case WINED3D_TOP_MODULATE_ALPHA_ADD_COLOR
:
5669 shader_addline(buffer
, "%s%s = clamp(%s%s * %s.w + %s%s, 0.0, 1.0);\n",
5670 dstreg
, dstmask
, arg2
, dstmask
, arg1
, arg1
, dstmask
);
5673 case WINED3D_TOP_MODULATE_COLOR_ADD_ALPHA
:
5674 shader_addline(buffer
, "%s%s = clamp(%s%s * %s%s + %s.w, 0.0, 1.0);\n",
5675 dstreg
, dstmask
, arg1
, dstmask
, arg2
, dstmask
, arg1
);
5678 case WINED3D_TOP_MODULATE_INVALPHA_ADD_COLOR
:
5679 shader_addline(buffer
, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
5680 dstreg
, dstmask
, arg2
, dstmask
, arg1
, arg1
, dstmask
);
5682 case WINED3D_TOP_MODULATE_INVCOLOR_ADD_ALPHA
:
5683 shader_addline(buffer
, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s.w, 0.0, 1.0);\n",
5684 dstreg
, dstmask
, arg1
, dstmask
, arg2
, dstmask
, arg1
);
5687 case WINED3D_TOP_BUMPENVMAP
:
5688 case WINED3D_TOP_BUMPENVMAP_LUMINANCE
:
5689 /* These are handled in the first pass, nothing to do. */
5692 case WINED3D_TOP_DOTPRODUCT3
:
5693 shader_addline(buffer
, "%s%s = vec4(clamp(dot(%s.xyz - 0.5, %s.xyz - 0.5) * 4.0, 0.0, 1.0))%s;\n",
5694 dstreg
, dstmask
, arg1
, arg2
, dstmask
);
5697 case WINED3D_TOP_MULTIPLY_ADD
:
5698 shader_addline(buffer
, "%s%s = clamp(%s%s * %s%s + %s%s, 0.0, 1.0);\n",
5699 dstreg
, dstmask
, arg1
, dstmask
, arg2
, dstmask
, arg0
, dstmask
);
5702 case WINED3D_TOP_LERP
:
5703 /* MSDN isn't quite right here. */
5704 shader_addline(buffer
, "%s%s = mix(%s%s, %s%s, %s%s);\n",
5705 dstreg
, dstmask
, arg2
, dstmask
, arg1
, dstmask
, arg0
, dstmask
);
5709 FIXME("Unhandled operation %#x.\n", op
);
5714 /* Context activation is done by the caller. */
5715 static GLuint
shader_glsl_generate_ffp_fragment_shader(struct wined3d_string_buffer
*buffer
,
5716 const struct ffp_frag_settings
*settings
, const struct wined3d_gl_info
*gl_info
)
5718 BYTE lum_map
= 0, bump_map
= 0, tex_map
= 0, tss_const_map
= 0;
5719 BOOL tempreg_used
= FALSE
, tfactor_used
= FALSE
;
5720 const char *final_combiner_src
= "ret";
5721 UINT lowest_disabled_stage
;
5723 DWORD arg0
, arg1
, arg2
;
5726 string_buffer_clear(buffer
);
5728 /* Find out which textures are read */
5729 for (stage
= 0; stage
< MAX_TEXTURES
; ++stage
)
5731 if (settings
->op
[stage
].cop
== WINED3D_TOP_DISABLE
)
5734 arg0
= settings
->op
[stage
].carg0
& WINED3DTA_SELECTMASK
;
5735 arg1
= settings
->op
[stage
].carg1
& WINED3DTA_SELECTMASK
;
5736 arg2
= settings
->op
[stage
].carg2
& WINED3DTA_SELECTMASK
;
5738 if (arg0
== WINED3DTA_TEXTURE
|| arg1
== WINED3DTA_TEXTURE
|| arg2
== WINED3DTA_TEXTURE
5739 || (stage
== 0 && settings
->color_key_enabled
))
5740 tex_map
|= 1 << stage
;
5741 if (arg0
== WINED3DTA_TFACTOR
|| arg1
== WINED3DTA_TFACTOR
|| arg2
== WINED3DTA_TFACTOR
)
5742 tfactor_used
= TRUE
;
5743 if (arg0
== WINED3DTA_TEMP
|| arg1
== WINED3DTA_TEMP
|| arg2
== WINED3DTA_TEMP
)
5744 tempreg_used
= TRUE
;
5745 if (settings
->op
[stage
].dst
== tempreg
)
5746 tempreg_used
= TRUE
;
5747 if (arg0
== WINED3DTA_CONSTANT
|| arg1
== WINED3DTA_CONSTANT
|| arg2
== WINED3DTA_CONSTANT
)
5748 tss_const_map
|= 1 << stage
;
5750 switch (settings
->op
[stage
].cop
)
5752 case WINED3D_TOP_BUMPENVMAP_LUMINANCE
:
5753 lum_map
|= 1 << stage
;
5755 case WINED3D_TOP_BUMPENVMAP
:
5756 bump_map
|= 1 << stage
;
5758 case WINED3D_TOP_BLEND_TEXTURE_ALPHA
:
5759 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM
:
5760 tex_map
|= 1 << stage
;
5763 case WINED3D_TOP_BLEND_FACTOR_ALPHA
:
5764 tfactor_used
= TRUE
;
5771 if (settings
->op
[stage
].aop
== WINED3D_TOP_DISABLE
)
5774 arg0
= settings
->op
[stage
].aarg0
& WINED3DTA_SELECTMASK
;
5775 arg1
= settings
->op
[stage
].aarg1
& WINED3DTA_SELECTMASK
;
5776 arg2
= settings
->op
[stage
].aarg2
& WINED3DTA_SELECTMASK
;
5778 if (arg0
== WINED3DTA_TEXTURE
|| arg1
== WINED3DTA_TEXTURE
|| arg2
== WINED3DTA_TEXTURE
)
5779 tex_map
|= 1 << stage
;
5780 if (arg0
== WINED3DTA_TFACTOR
|| arg1
== WINED3DTA_TFACTOR
|| arg2
== WINED3DTA_TFACTOR
)
5781 tfactor_used
= TRUE
;
5782 if (arg0
== WINED3DTA_TEMP
|| arg1
== WINED3DTA_TEMP
|| arg2
== WINED3DTA_TEMP
)
5783 tempreg_used
= TRUE
;
5784 if (arg0
== WINED3DTA_CONSTANT
|| arg1
== WINED3DTA_CONSTANT
|| arg2
== WINED3DTA_CONSTANT
)
5785 tss_const_map
|= 1 << stage
;
5787 lowest_disabled_stage
= stage
;
5789 shader_addline(buffer
, "#version 120\n");
5791 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
5792 shader_addline(buffer
, "#extension GL_ARB_texture_rectangle : enable\n");
5794 shader_addline(buffer
, "vec4 tmp0, tmp1;\n");
5795 shader_addline(buffer
, "vec4 ret;\n");
5796 if (tempreg_used
|| settings
->sRGB_write
)
5797 shader_addline(buffer
, "vec4 temp_reg = vec4(0.0);\n");
5798 shader_addline(buffer
, "vec4 arg0, arg1, arg2;\n");
5800 for (stage
= 0; stage
< MAX_TEXTURES
; ++stage
)
5802 if (tss_const_map
& (1 << stage
))
5803 shader_addline(buffer
, "uniform vec4 tss_const%u;\n", stage
);
5805 if (!(tex_map
& (1 << stage
)))
5808 switch (settings
->op
[stage
].tex_type
)
5810 case WINED3D_GL_RES_TYPE_TEX_1D
:
5811 shader_addline(buffer
, "uniform sampler1D ps_sampler%u;\n", stage
);
5813 case WINED3D_GL_RES_TYPE_TEX_2D
:
5814 shader_addline(buffer
, "uniform sampler2D ps_sampler%u;\n", stage
);
5816 case WINED3D_GL_RES_TYPE_TEX_3D
:
5817 shader_addline(buffer
, "uniform sampler3D ps_sampler%u;\n", stage
);
5819 case WINED3D_GL_RES_TYPE_TEX_CUBE
:
5820 shader_addline(buffer
, "uniform samplerCube ps_sampler%u;\n", stage
);
5822 case WINED3D_GL_RES_TYPE_TEX_RECT
:
5823 shader_addline(buffer
, "uniform sampler2DRect ps_sampler%u;\n", stage
);
5826 FIXME("Unhandled sampler type %#x.\n", settings
->op
[stage
].tex_type
);
5830 shader_addline(buffer
, "vec4 tex%u;\n", stage
);
5832 if (!(bump_map
& (1 << stage
)))
5834 shader_addline(buffer
, "uniform mat2 bumpenv_mat%u;\n", stage
);
5836 if (!(lum_map
& (1 << stage
)))
5838 shader_addline(buffer
, "uniform float bumpenv_lum_scale%u;\n", stage
);
5839 shader_addline(buffer
, "uniform float bumpenv_lum_offset%u;\n", stage
);
5842 shader_addline(buffer
, "uniform vec4 tex_factor;\n");
5843 if (settings
->color_key_enabled
)
5844 shader_addline(buffer
, "uniform vec4 color_key;\n");
5845 shader_addline(buffer
, "uniform vec4 specular_enable;\n");
5847 if (settings
->sRGB_write
)
5849 shader_addline(buffer
, "const vec4 srgb_const0 = ");
5850 shader_glsl_append_imm_vec4(buffer
, wined3d_srgb_const0
);
5851 shader_addline(buffer
, ";\n");
5852 shader_addline(buffer
, "const vec4 srgb_const1 = ");
5853 shader_glsl_append_imm_vec4(buffer
, wined3d_srgb_const1
);
5854 shader_addline(buffer
, ";\n");
5857 shader_addline(buffer
, "void main()\n{\n");
5859 if (lowest_disabled_stage
< 7 && settings
->emul_clipplanes
)
5860 shader_addline(buffer
, "if (any(lessThan(gl_TexCoord[7], vec4(0.0)))) discard;\n");
5862 /* Generate texture sampling instructions) */
5863 for (stage
= 0; stage
< MAX_TEXTURES
&& settings
->op
[stage
].cop
!= WINED3D_TOP_DISABLE
; ++stage
)
5865 const char *texture_function
, *coord_mask
;
5866 char tex_reg_name
[8];
5869 if (!(tex_map
& (1 << stage
)))
5872 if (settings
->op
[stage
].projected
== proj_none
)
5876 else if (settings
->op
[stage
].projected
== proj_count4
5877 || settings
->op
[stage
].projected
== proj_count3
)
5883 FIXME("Unexpected projection mode %d\n", settings
->op
[stage
].projected
);
5887 switch (settings
->op
[stage
].tex_type
)
5889 case WINED3D_GL_RES_TYPE_TEX_1D
:
5892 texture_function
= "texture1DProj";
5897 texture_function
= "texture1D";
5901 case WINED3D_GL_RES_TYPE_TEX_2D
:
5904 texture_function
= "texture2DProj";
5909 texture_function
= "texture2D";
5913 case WINED3D_GL_RES_TYPE_TEX_3D
:
5916 texture_function
= "texture3DProj";
5917 coord_mask
= "xyzw";
5921 texture_function
= "texture3D";
5925 case WINED3D_GL_RES_TYPE_TEX_CUBE
:
5926 texture_function
= "textureCube";
5929 case WINED3D_GL_RES_TYPE_TEX_RECT
:
5932 texture_function
= "texture2DRectProj";
5937 texture_function
= "texture2DRect";
5942 FIXME("Unhandled texture type %#x.\n", settings
->op
[stage
].tex_type
);
5943 texture_function
= "";
5944 coord_mask
= "xyzw";
5949 && (settings
->op
[stage
- 1].cop
== WINED3D_TOP_BUMPENVMAP
5950 || settings
->op
[stage
- 1].cop
== WINED3D_TOP_BUMPENVMAP_LUMINANCE
))
5952 shader_addline(buffer
, "ret.xy = bumpenv_mat%u * tex%u.xy;\n", stage
- 1, stage
- 1);
5954 /* With projective textures, texbem only divides the static
5955 * texture coord, not the displacement, so multiply the
5956 * displacement with the dividing parameter before passing it to
5958 if (settings
->op
[stage
].projected
!= proj_none
)
5960 if (settings
->op
[stage
].projected
== proj_count4
)
5962 shader_addline(buffer
, "ret.xy = (ret.xy * gl_TexCoord[%u].w) + gl_TexCoord[%u].xy;\n",
5964 shader_addline(buffer
, "ret.zw = gl_TexCoord[%u].ww;\n", stage
);
5968 shader_addline(buffer
, "ret.xy = (ret.xy * gl_TexCoord[%u].z) + gl_TexCoord[%u].xy;\n",
5970 shader_addline(buffer
, "ret.zw = gl_TexCoord[%u].zz;\n", stage
);
5975 shader_addline(buffer
, "ret = gl_TexCoord[%u] + ret.xyxy;\n", stage
);
5978 shader_addline(buffer
, "tex%u = %s(ps_sampler%u, ret.%s);\n",
5979 stage
, texture_function
, stage
, coord_mask
);
5981 if (settings
->op
[stage
- 1].cop
== WINED3D_TOP_BUMPENVMAP_LUMINANCE
)
5982 shader_addline(buffer
, "tex%u *= clamp(tex%u.z * bumpenv_lum_scale%u + bumpenv_lum_offset%u, 0.0, 1.0);\n",
5983 stage
, stage
- 1, stage
- 1, stage
- 1);
5985 else if (settings
->op
[stage
].projected
== proj_count3
)
5987 shader_addline(buffer
, "tex%u = %s(ps_sampler%u, gl_TexCoord[%u].xyz);\n",
5988 stage
, texture_function
, stage
, stage
);
5992 shader_addline(buffer
, "tex%u = %s(ps_sampler%u, gl_TexCoord[%u].%s);\n",
5993 stage
, texture_function
, stage
, stage
, coord_mask
);
5996 sprintf(tex_reg_name
, "tex%u", stage
);
5997 shader_glsl_color_correction_ext(buffer
, tex_reg_name
, WINED3DSP_WRITEMASK_ALL
,
5998 settings
->op
[stage
].color_fixup
);
6001 if (settings
->color_key_enabled
)
6002 shader_addline(buffer
, "if (all(equal(tex0, color_key))) discard;\n");
6004 /* Generate the main shader */
6005 for (stage
= 0; stage
< MAX_TEXTURES
; ++stage
)
6009 if (settings
->op
[stage
].cop
== WINED3D_TOP_DISABLE
)
6012 final_combiner_src
= "gl_Color";
6016 if (settings
->op
[stage
].cop
== WINED3D_TOP_SELECT_ARG1
6017 && settings
->op
[stage
].aop
== WINED3D_TOP_SELECT_ARG1
)
6018 op_equal
= settings
->op
[stage
].carg1
== settings
->op
[stage
].aarg1
;
6019 else if (settings
->op
[stage
].cop
== WINED3D_TOP_SELECT_ARG1
6020 && settings
->op
[stage
].aop
== WINED3D_TOP_SELECT_ARG2
)
6021 op_equal
= settings
->op
[stage
].carg1
== settings
->op
[stage
].aarg2
;
6022 else if (settings
->op
[stage
].cop
== WINED3D_TOP_SELECT_ARG2
6023 && settings
->op
[stage
].aop
== WINED3D_TOP_SELECT_ARG1
)
6024 op_equal
= settings
->op
[stage
].carg2
== settings
->op
[stage
].aarg1
;
6025 else if (settings
->op
[stage
].cop
== WINED3D_TOP_SELECT_ARG2
6026 && settings
->op
[stage
].aop
== WINED3D_TOP_SELECT_ARG2
)
6027 op_equal
= settings
->op
[stage
].carg2
== settings
->op
[stage
].aarg2
;
6029 op_equal
= settings
->op
[stage
].aop
== settings
->op
[stage
].cop
6030 && settings
->op
[stage
].carg0
== settings
->op
[stage
].aarg0
6031 && settings
->op
[stage
].carg1
== settings
->op
[stage
].aarg1
6032 && settings
->op
[stage
].carg2
== settings
->op
[stage
].aarg2
;
6034 if (settings
->op
[stage
].aop
== WINED3D_TOP_DISABLE
)
6036 shader_glsl_ffp_fragment_op(buffer
, stage
, TRUE
, FALSE
, settings
->op
[stage
].dst
,
6037 settings
->op
[stage
].cop
, settings
->op
[stage
].carg0
,
6038 settings
->op
[stage
].carg1
, settings
->op
[stage
].carg2
);
6040 shader_addline(buffer
, "ret.w = gl_Color.w;\n");
6044 shader_glsl_ffp_fragment_op(buffer
, stage
, TRUE
, TRUE
, settings
->op
[stage
].dst
,
6045 settings
->op
[stage
].cop
, settings
->op
[stage
].carg0
,
6046 settings
->op
[stage
].carg1
, settings
->op
[stage
].carg2
);
6050 shader_glsl_ffp_fragment_op(buffer
, stage
, TRUE
, FALSE
, settings
->op
[stage
].dst
,
6051 settings
->op
[stage
].cop
, settings
->op
[stage
].carg0
,
6052 settings
->op
[stage
].carg1
, settings
->op
[stage
].carg2
);
6053 shader_glsl_ffp_fragment_op(buffer
, stage
, FALSE
, TRUE
, settings
->op
[stage
].dst
,
6054 settings
->op
[stage
].aop
, settings
->op
[stage
].aarg0
,
6055 settings
->op
[stage
].aarg1
, settings
->op
[stage
].aarg2
);
6059 shader_addline(buffer
, "gl_FragData[0] = gl_SecondaryColor * specular_enable + %s;\n", final_combiner_src
);
6061 if (settings
->sRGB_write
)
6062 shader_glsl_generate_srgb_write_correction(buffer
);
6064 shader_glsl_generate_fog_code(buffer
, settings
->fog
);
6066 shader_addline(buffer
, "}\n");
6068 shader_id
= GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER
));
6069 shader_glsl_compile(gl_info
, shader_id
, buffer
->buffer
);
6073 static struct glsl_ffp_vertex_shader
*shader_glsl_find_ffp_vertex_shader(struct shader_glsl_priv
*priv
,
6074 const struct wined3d_gl_info
*gl_info
, const struct wined3d_ffp_vs_settings
*settings
)
6076 struct glsl_ffp_vertex_shader
*shader
;
6077 const struct wine_rb_entry
*entry
;
6079 if ((entry
= wine_rb_get(&priv
->ffp_vertex_shaders
, settings
)))
6080 return WINE_RB_ENTRY_VALUE(entry
, struct glsl_ffp_vertex_shader
, desc
.entry
);
6082 if (!(shader
= HeapAlloc(GetProcessHeap(), 0, sizeof(*shader
))))
6085 shader
->desc
.settings
= *settings
;
6086 shader
->id
= shader_glsl_generate_ffp_vertex_shader(&priv
->shader_buffer
, settings
, gl_info
);
6087 list_init(&shader
->linked_programs
);
6088 if (wine_rb_put(&priv
->ffp_vertex_shaders
, &shader
->desc
.settings
, &shader
->desc
.entry
) == -1)
6089 ERR("Failed to insert ffp vertex shader.\n");
6094 static struct glsl_ffp_fragment_shader
*shader_glsl_find_ffp_fragment_shader(struct shader_glsl_priv
*priv
,
6095 const struct wined3d_gl_info
*gl_info
, const struct ffp_frag_settings
*args
)
6097 struct glsl_ffp_fragment_shader
*glsl_desc
;
6098 const struct ffp_frag_desc
*desc
;
6100 if ((desc
= find_ffp_frag_shader(&priv
->ffp_fragment_shaders
, args
)))
6101 return CONTAINING_RECORD(desc
, struct glsl_ffp_fragment_shader
, entry
);
6103 if (!(glsl_desc
= HeapAlloc(GetProcessHeap(), 0, sizeof(*glsl_desc
))))
6106 glsl_desc
->entry
.settings
= *args
;
6107 glsl_desc
->id
= shader_glsl_generate_ffp_fragment_shader(&priv
->shader_buffer
, args
, gl_info
);
6108 list_init(&glsl_desc
->linked_programs
);
6109 add_ffp_frag_shader(&priv
->ffp_fragment_shaders
, &glsl_desc
->entry
);
6115 static void shader_glsl_init_vs_uniform_locations(const struct wined3d_gl_info
*gl_info
,
6116 GLuint program_id
, struct glsl_vs_program
*vs
, unsigned int vs_c_count
)
6121 vs
->uniform_f_locations
= HeapAlloc(GetProcessHeap(), 0,
6122 sizeof(GLuint
) * gl_info
->limits
.glsl_vs_float_constants
);
6123 for (i
= 0; i
< vs_c_count
; ++i
)
6125 snprintf(name
, sizeof(name
), "vs_c[%u]", i
);
6126 vs
->uniform_f_locations
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
));
6128 memset(&vs
->uniform_f_locations
[vs_c_count
], 0xff,
6129 (gl_info
->limits
.glsl_vs_float_constants
- vs_c_count
) * sizeof(GLuint
));
6131 for (i
= 0; i
< MAX_CONST_I
; ++i
)
6133 snprintf(name
, sizeof(name
), "vs_i[%u]", i
);
6134 vs
->uniform_i_locations
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
));
6137 for (i
= 0; i
< MAX_CONST_B
; ++i
)
6139 snprintf(name
, sizeof(name
), "vs_b[%u]", i
);
6140 vs
->uniform_b_locations
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
));
6143 vs
->pos_fixup_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "posFixup"));
6145 vs
->modelview_matrix_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_modelview_matrix"));
6146 vs
->projection_matrix_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_projection_matrix"));
6147 vs
->normal_matrix_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_normal_matrix"));
6150 static void shader_glsl_init_ps_uniform_locations(const struct wined3d_gl_info
*gl_info
,
6151 GLuint program_id
, struct glsl_ps_program
*ps
, unsigned int ps_c_count
)
6156 ps
->uniform_f_locations
= HeapAlloc(GetProcessHeap(), 0,
6157 sizeof(GLuint
) * gl_info
->limits
.glsl_ps_float_constants
);
6158 for (i
= 0; i
< ps_c_count
; ++i
)
6160 snprintf(name
, sizeof(name
), "ps_c[%u]", i
);
6161 ps
->uniform_f_locations
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
));
6163 memset(&ps
->uniform_f_locations
[ps_c_count
], 0xff,
6164 (gl_info
->limits
.glsl_ps_float_constants
- ps_c_count
) * sizeof(GLuint
));
6166 for (i
= 0; i
< MAX_CONST_I
; ++i
)
6168 snprintf(name
, sizeof(name
), "ps_i[%u]", i
);
6169 ps
->uniform_i_locations
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
));
6172 for (i
= 0; i
< MAX_CONST_B
; ++i
)
6174 snprintf(name
, sizeof(name
), "ps_b[%u]", i
);
6175 ps
->uniform_b_locations
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
));
6178 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
6180 snprintf(name
, sizeof(name
), "bumpenv_mat%u", i
);
6181 ps
->bumpenv_mat_location
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
));
6182 snprintf(name
, sizeof(name
), "bumpenv_lum_scale%u", i
);
6183 ps
->bumpenv_lum_scale_location
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
));
6184 snprintf(name
, sizeof(name
), "bumpenv_lum_offset%u", i
);
6185 ps
->bumpenv_lum_offset_location
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
));
6186 snprintf(name
, sizeof(name
), "tss_const%u", i
);
6187 ps
->tss_constant_location
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
));
6190 ps
->tex_factor_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "tex_factor"));
6191 ps
->specular_enable_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "specular_enable"));
6192 ps
->np2_fixup_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ps_samplerNP2Fixup"));
6193 ps
->ycorrection_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ycorrection"));
6194 ps
->color_key_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "color_key"));
6197 static void shader_glsl_init_uniform_block_bindings(const struct wined3d_gl_info
*gl_info
, GLuint program_id
,
6198 const struct wined3d_shader_reg_maps
*reg_maps
, unsigned int base
, unsigned int count
)
6200 const char *prefix
= shader_glsl_get_prefix(reg_maps
->shader_version
.type
);
6205 for (i
= 0; i
< count
; ++i
)
6207 if (!reg_maps
->cb_sizes
[i
])
6210 snprintf(name
, sizeof(name
), "block_%s_cb%u", prefix
, i
);
6211 block_idx
= GL_EXTCALL(glGetUniformBlockIndex(program_id
, name
));
6212 GL_EXTCALL(glUniformBlockBinding(program_id
, block_idx
, base
+ i
));
6214 checkGLcall("glUniformBlockBinding");
6217 /* Context activation is done by the caller. */
6218 static void set_glsl_shader_program(const struct wined3d_context
*context
, const struct wined3d_state
*state
,
6219 struct shader_glsl_priv
*priv
, struct glsl_context_data
*ctx_data
)
6221 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
6222 const struct ps_np2fixup_info
*np2fixup_info
= NULL
;
6223 struct glsl_shader_prog_link
*entry
= NULL
;
6224 struct wined3d_shader
*vshader
= NULL
;
6225 struct wined3d_shader
*gshader
= NULL
;
6226 struct wined3d_shader
*pshader
= NULL
;
6227 GLuint program_id
= 0;
6228 GLuint reorder_shader_id
= 0;
6233 struct list
*ps_list
, *vs_list
;
6235 if (!(context
->shader_update_mask
& (1 << WINED3D_SHADER_TYPE_VERTEX
)))
6237 vs_id
= ctx_data
->glsl_program
->vs
.id
;
6238 vs_list
= &ctx_data
->glsl_program
->vs
.shader_entry
;
6242 vshader
= state
->shader
[WINED3D_SHADER_TYPE_VERTEX
];
6243 gshader
= state
->shader
[WINED3D_SHADER_TYPE_GEOMETRY
];
6245 if (!(context
->shader_update_mask
& (1 << WINED3D_SHADER_TYPE_GEOMETRY
))
6246 && ctx_data
->glsl_program
->gs
.id
)
6247 gs_id
= ctx_data
->glsl_program
->gs
.id
;
6249 gs_id
= find_glsl_geometry_shader(context
, &priv
->shader_buffer
, gshader
);
6252 else if (use_vs(state
))
6254 struct vs_compile_args vs_compile_args
;
6255 vshader
= state
->shader
[WINED3D_SHADER_TYPE_VERTEX
];
6257 find_vs_compile_args(state
, vshader
, context
->stream_info
.swizzle_map
, &vs_compile_args
);
6258 vs_id
= find_glsl_vshader(context
, &priv
->shader_buffer
, vshader
, &vs_compile_args
);
6259 vs_list
= &vshader
->linked_programs
;
6261 if ((gshader
= state
->shader
[WINED3D_SHADER_TYPE_GEOMETRY
]))
6262 gs_id
= find_glsl_geometry_shader(context
, &priv
->shader_buffer
, gshader
);
6264 else if (priv
->vertex_pipe
== &glsl_vertex_pipe
)
6266 struct glsl_ffp_vertex_shader
*ffp_shader
;
6267 struct wined3d_ffp_vs_settings settings
;
6269 wined3d_ffp_get_vs_settings(state
, &context
->stream_info
, &settings
);
6270 ffp_shader
= shader_glsl_find_ffp_vertex_shader(priv
, gl_info
, &settings
);
6271 vs_id
= ffp_shader
->id
;
6272 vs_list
= &ffp_shader
->linked_programs
;
6275 if (!(context
->shader_update_mask
& (1 << WINED3D_SHADER_TYPE_PIXEL
)))
6277 ps_id
= ctx_data
->glsl_program
->ps
.id
;
6278 ps_list
= &ctx_data
->glsl_program
->ps
.shader_entry
;
6281 pshader
= state
->shader
[WINED3D_SHADER_TYPE_PIXEL
];
6283 else if (use_ps(state
))
6285 struct ps_compile_args ps_compile_args
;
6286 pshader
= state
->shader
[WINED3D_SHADER_TYPE_PIXEL
];
6287 find_ps_compile_args(state
, pshader
, context
->stream_info
.position_transformed
, &ps_compile_args
, gl_info
);
6288 ps_id
= find_glsl_pshader(context
, &priv
->shader_buffer
,
6289 pshader
, &ps_compile_args
, &np2fixup_info
);
6290 ps_list
= &pshader
->linked_programs
;
6292 else if (priv
->fragment_pipe
== &glsl_fragment_pipe
)
6294 struct glsl_ffp_fragment_shader
*ffp_shader
;
6295 struct ffp_frag_settings settings
;
6297 gen_ffp_frag_op(context
, state
, &settings
, FALSE
);
6298 ffp_shader
= shader_glsl_find_ffp_fragment_shader(priv
, gl_info
, &settings
);
6299 ps_id
= ffp_shader
->id
;
6300 ps_list
= &ffp_shader
->linked_programs
;
6303 if ((!vs_id
&& !gs_id
&& !ps_id
) || (entry
= get_glsl_program_entry(priv
, vs_id
, gs_id
, ps_id
)))
6305 ctx_data
->glsl_program
= entry
;
6309 /* If we get to this point, then no matching program exists, so we create one */
6310 program_id
= GL_EXTCALL(glCreateProgram());
6311 TRACE("Created new GLSL shader program %u.\n", program_id
);
6313 /* Create the entry */
6314 entry
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link
));
6315 entry
->id
= program_id
;
6316 entry
->vs
.id
= vs_id
;
6317 entry
->gs
.id
= gs_id
;
6318 entry
->ps
.id
= ps_id
;
6319 entry
->constant_version
= 0;
6320 entry
->ps
.np2_fixup_info
= np2fixup_info
;
6321 /* Add the hash table entry */
6322 add_glsl_program_entry(priv
, entry
);
6324 /* Set the current program */
6325 ctx_data
->glsl_program
= entry
;
6327 /* Attach GLSL vshader */
6330 TRACE("Attaching GLSL shader object %u to program %u.\n", vs_id
, program_id
);
6331 GL_EXTCALL(glAttachShader(program_id
, vs_id
));
6332 checkGLcall("glAttachShader");
6334 list_add_head(vs_list
, &entry
->vs
.shader_entry
);
6339 WORD map
= vshader
->reg_maps
.input_registers
;
6342 reorder_shader_id
= generate_param_reorder_function(&priv
->shader_buffer
, vshader
, pshader
, gl_info
);
6343 TRACE("Attaching GLSL shader object %u to program %u.\n", reorder_shader_id
, program_id
);
6344 GL_EXTCALL(glAttachShader(program_id
, reorder_shader_id
));
6345 checkGLcall("glAttachShader");
6346 /* Flag the reorder function for deletion, then it will be freed automatically when the program
6349 GL_EXTCALL(glDeleteShader(reorder_shader_id
));
6351 /* Bind vertex attributes to a corresponding index number to match
6352 * the same index numbers as ARB_vertex_programs (makes loading
6353 * vertex attributes simpler). With this method, we can use the
6354 * exact same code to load the attributes later for both ARB and
6357 * We have to do this here because we need to know the Program ID
6358 * in order to make the bindings work, and it has to be done prior
6359 * to linking the GLSL program. */
6360 for (i
= 0; map
; map
>>= 1, ++i
)
6362 if (!(map
& 1)) continue;
6364 snprintf(tmp_name
, sizeof(tmp_name
), "vs_in%u", i
);
6365 GL_EXTCALL(glBindAttribLocation(program_id
, i
, tmp_name
));
6367 checkGLcall("glBindAttribLocation");
6372 TRACE("Attaching GLSL geometry shader object %u to program %u.\n", gs_id
, program_id
);
6373 GL_EXTCALL(glAttachShader(program_id
, gs_id
));
6374 checkGLcall("glAttachShader");
6376 TRACE("input type %s, output type %s, vertices out %u.\n",
6377 debug_d3dprimitivetype(gshader
->u
.gs
.input_type
),
6378 debug_d3dprimitivetype(gshader
->u
.gs
.output_type
),
6379 gshader
->u
.gs
.vertices_out
);
6380 GL_EXTCALL(glProgramParameteriARB(program_id
, GL_GEOMETRY_INPUT_TYPE_ARB
,
6381 gl_primitive_type_from_d3d(gshader
->u
.gs
.input_type
)));
6382 GL_EXTCALL(glProgramParameteriARB(program_id
, GL_GEOMETRY_OUTPUT_TYPE_ARB
,
6383 gl_primitive_type_from_d3d(gshader
->u
.gs
.output_type
)));
6384 GL_EXTCALL(glProgramParameteriARB(program_id
, GL_GEOMETRY_VERTICES_OUT_ARB
,
6385 gshader
->u
.gs
.vertices_out
));
6386 checkGLcall("glProgramParameteriARB");
6388 list_add_head(&gshader
->linked_programs
, &entry
->gs
.shader_entry
);
6391 /* Attach GLSL pshader */
6394 TRACE("Attaching GLSL shader object %u to program %u.\n", ps_id
, program_id
);
6395 GL_EXTCALL(glAttachShader(program_id
, ps_id
));
6396 checkGLcall("glAttachShader");
6398 list_add_head(ps_list
, &entry
->ps
.shader_entry
);
6401 /* Link the program */
6402 TRACE("Linking GLSL shader program %u.\n", program_id
);
6403 GL_EXTCALL(glLinkProgram(program_id
));
6404 shader_glsl_validate_link(gl_info
, program_id
);
6406 shader_glsl_init_vs_uniform_locations(gl_info
, program_id
, &entry
->vs
,
6407 vshader
? min(vshader
->limits
->constant_float
, gl_info
->limits
.glsl_vs_float_constants
) : 0);
6408 shader_glsl_init_ps_uniform_locations(gl_info
, program_id
, &entry
->ps
,
6409 pshader
? min(pshader
->limits
->constant_float
, gl_info
->limits
.glsl_ps_float_constants
) : 0);
6410 checkGLcall("Find glsl program uniform locations");
6412 if (pshader
&& pshader
->reg_maps
.shader_version
.major
>= 3
6413 && pshader
->u
.ps
.declared_in_count
> vec4_varyings(3, gl_info
))
6415 TRACE("Shader %d needs vertex color clamping disabled.\n", program_id
);
6416 entry
->vs
.vertex_color_clamp
= GL_FALSE
;
6420 entry
->vs
.vertex_color_clamp
= GL_FIXED_ONLY_ARB
;
6423 /* Set the shader to allow uniform loading on it */
6424 GL_EXTCALL(glUseProgram(program_id
));
6425 checkGLcall("glUseProgram");
6427 /* Load the vertex and pixel samplers now. The function that finds the mappings makes sure
6428 * that it stays the same for each vertexshader-pixelshader pair(=linked glsl program). If
6429 * a pshader with fixed function pipeline is used there are no vertex samplers, and if a
6430 * vertex shader with fixed function pixel processing is used we make sure that the card
6431 * supports enough samplers to allow the max number of vertex samplers with all possible
6432 * fixed function fragment processing setups. So once the program is linked these samplers
6434 shader_glsl_load_samplers(gl_info
, context
->tex_unit_map
, program_id
);
6436 entry
->constant_update_mask
= 0;
6439 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_VS_F
;
6440 if (vshader
->reg_maps
.integer_constants
)
6441 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_VS_I
;
6442 if (vshader
->reg_maps
.boolean_constants
)
6443 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_VS_B
;
6444 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_VS_POS_FIXUP
;
6446 shader_glsl_init_uniform_block_bindings(gl_info
, program_id
, &vshader
->reg_maps
,
6447 0, gl_info
->limits
.vertex_uniform_blocks
);
6451 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_MODELVIEW
6452 | WINED3D_SHADER_CONST_FFP_PROJ
;
6456 shader_glsl_init_uniform_block_bindings(gl_info
, program_id
, &gshader
->reg_maps
,
6457 gl_info
->limits
.vertex_uniform_blocks
, gl_info
->limits
.geometry_uniform_blocks
);
6463 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_PS_F
;
6464 if (pshader
->reg_maps
.integer_constants
)
6465 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_PS_I
;
6466 if (pshader
->reg_maps
.boolean_constants
)
6467 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_PS_B
;
6468 if (entry
->ps
.ycorrection_location
!= -1)
6469 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_PS_Y_CORR
;
6471 shader_glsl_init_uniform_block_bindings(gl_info
, program_id
, &pshader
->reg_maps
,
6472 gl_info
->limits
.vertex_uniform_blocks
+ gl_info
->limits
.geometry_uniform_blocks
,
6473 gl_info
->limits
.fragment_uniform_blocks
);
6477 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_PS
;
6480 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
6482 if (entry
->ps
.bumpenv_mat_location
[i
] != -1)
6484 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_PS_BUMP_ENV
;
6489 if (entry
->ps
.np2_fixup_location
!= -1)
6490 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_PS_NP2_FIXUP
;
6491 if (entry
->ps
.color_key_location
!= -1)
6492 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_COLOR_KEY
;
6496 /* Context activation is done by the caller. */
6497 static GLuint
create_glsl_blt_shader(const struct wined3d_gl_info
*gl_info
, enum wined3d_gl_resource_type tex_type
,
6501 GLuint vshader_id
, pshader_id
;
6502 const char *blt_pshader
;
6504 static const char blt_vshader
[] =
6508 " gl_Position = gl_Vertex;\n"
6509 " gl_FrontColor = vec4(1.0);\n"
6510 " gl_TexCoord[0] = gl_MultiTexCoord0;\n"
6513 static const char * const blt_pshaders_full
[WINED3D_GL_RES_TYPE_COUNT
] =
6515 /* WINED3D_GL_RES_TYPE_TEX_1D */
6517 /* WINED3D_GL_RES_TYPE_TEX_2D */
6519 "uniform sampler2D sampler;\n"
6522 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
6524 /* WINED3D_GL_RES_TYPE_TEX_3D */
6526 /* WINED3D_GL_RES_TYPE_TEX_CUBE */
6528 "uniform samplerCube sampler;\n"
6531 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
6533 /* WINED3D_GL_RES_TYPE_TEX_RECT */
6535 "#extension GL_ARB_texture_rectangle : enable\n"
6536 "uniform sampler2DRect sampler;\n"
6539 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
6543 static const char * const blt_pshaders_masked
[WINED3D_GL_RES_TYPE_COUNT
] =
6545 /* WINED3D_GL_RES_TYPE_TEX_1D */
6547 /* WINED3D_GL_RES_TYPE_TEX_2D */
6549 "uniform sampler2D sampler;\n"
6550 "uniform vec4 mask;\n"
6553 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
6554 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
6556 /* WINED3D_GL_RES_TYPE_TEX_3D */
6558 /* WINED3D_GL_RES_TYPE_TEX_CUBE */
6560 "uniform samplerCube sampler;\n"
6561 "uniform vec4 mask;\n"
6564 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
6565 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
6567 /* WINED3D_GL_RES_TYPE_TEX_RECT */
6569 "#extension GL_ARB_texture_rectangle : enable\n"
6570 "uniform sampler2DRect sampler;\n"
6571 "uniform vec4 mask;\n"
6574 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
6575 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
6579 blt_pshader
= masked
? blt_pshaders_masked
[tex_type
] : blt_pshaders_full
[tex_type
];
6582 FIXME("tex_type %#x not supported\n", tex_type
);
6586 vshader_id
= GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER
));
6587 shader_glsl_compile(gl_info
, vshader_id
, blt_vshader
);
6589 pshader_id
= GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER
));
6590 shader_glsl_compile(gl_info
, pshader_id
, blt_pshader
);
6592 program_id
= GL_EXTCALL(glCreateProgram());
6593 GL_EXTCALL(glAttachShader(program_id
, vshader_id
));
6594 GL_EXTCALL(glAttachShader(program_id
, pshader_id
));
6595 GL_EXTCALL(glLinkProgram(program_id
));
6597 shader_glsl_validate_link(gl_info
, program_id
);
6599 /* Once linked we can mark the shaders for deletion. They will be deleted once the program
6602 GL_EXTCALL(glDeleteShader(vshader_id
));
6603 GL_EXTCALL(glDeleteShader(pshader_id
));
6607 /* Context activation is done by the caller. */
6608 static void shader_glsl_select(void *shader_priv
, struct wined3d_context
*context
,
6609 const struct wined3d_state
*state
)
6611 struct glsl_context_data
*ctx_data
= context
->shader_backend_data
;
6612 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
6613 struct shader_glsl_priv
*priv
= shader_priv
;
6614 GLuint program_id
= 0, prev_id
= 0;
6615 GLenum old_vertex_color_clamp
, current_vertex_color_clamp
;
6617 priv
->vertex_pipe
->vp_enable(gl_info
, !use_vs(state
));
6618 priv
->fragment_pipe
->enable_extension(gl_info
, !use_ps(state
));
6620 if (ctx_data
->glsl_program
)
6622 prev_id
= ctx_data
->glsl_program
->id
;
6623 old_vertex_color_clamp
= ctx_data
->glsl_program
->vs
.vertex_color_clamp
;
6628 old_vertex_color_clamp
= GL_FIXED_ONLY_ARB
;
6631 set_glsl_shader_program(context
, state
, priv
, ctx_data
);
6633 if (ctx_data
->glsl_program
)
6635 program_id
= ctx_data
->glsl_program
->id
;
6636 current_vertex_color_clamp
= ctx_data
->glsl_program
->vs
.vertex_color_clamp
;
6641 current_vertex_color_clamp
= GL_FIXED_ONLY_ARB
;
6644 if (old_vertex_color_clamp
!= current_vertex_color_clamp
)
6646 if (gl_info
->supported
[ARB_COLOR_BUFFER_FLOAT
])
6648 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB
, current_vertex_color_clamp
));
6649 checkGLcall("glClampColorARB");
6653 FIXME("vertex color clamp needs to be changed, but extension not supported.\n");
6657 TRACE("Using GLSL program %u.\n", program_id
);
6659 if (prev_id
!= program_id
)
6661 GL_EXTCALL(glUseProgram(program_id
));
6662 checkGLcall("glUseProgram");
6665 context
->constant_update_mask
|= ctx_data
->glsl_program
->constant_update_mask
;
6669 /* "context" is not necessarily the currently active context. */
6670 static void shader_glsl_invalidate_current_program(struct wined3d_context
*context
)
6672 struct glsl_context_data
*ctx_data
= context
->shader_backend_data
;
6674 ctx_data
->glsl_program
= NULL
;
6675 context
->shader_update_mask
= (1 << WINED3D_SHADER_TYPE_PIXEL
)
6676 | (1 << WINED3D_SHADER_TYPE_VERTEX
)
6677 | (1 << WINED3D_SHADER_TYPE_GEOMETRY
);
6680 /* Context activation is done by the caller. */
6681 static void shader_glsl_disable(void *shader_priv
, struct wined3d_context
*context
)
6683 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
6684 struct shader_glsl_priv
*priv
= shader_priv
;
6686 shader_glsl_invalidate_current_program(context
);
6687 GL_EXTCALL(glUseProgram(0));
6688 checkGLcall("glUseProgram");
6690 priv
->vertex_pipe
->vp_enable(gl_info
, FALSE
);
6691 priv
->fragment_pipe
->enable_extension(gl_info
, FALSE
);
6693 if (gl_info
->supported
[ARB_COLOR_BUFFER_FLOAT
])
6695 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB
, GL_FIXED_ONLY_ARB
));
6696 checkGLcall("glClampColorARB");
6700 /* Context activation is done by the caller. */
6701 static void shader_glsl_select_depth_blt(void *shader_priv
, const struct wined3d_gl_info
*gl_info
,
6702 enum wined3d_gl_resource_type tex_type
, const SIZE
*ds_mask_size
)
6704 BOOL masked
= ds_mask_size
->cx
&& ds_mask_size
->cy
;
6705 struct shader_glsl_priv
*priv
= shader_priv
;
6706 GLuint
*blt_program
;
6709 blt_program
= masked
? &priv
->depth_blt_program_masked
[tex_type
] : &priv
->depth_blt_program_full
[tex_type
];
6712 *blt_program
= create_glsl_blt_shader(gl_info
, tex_type
, masked
);
6713 loc
= GL_EXTCALL(glGetUniformLocation(*blt_program
, "sampler"));
6714 GL_EXTCALL(glUseProgram(*blt_program
));
6715 GL_EXTCALL(glUniform1i(loc
, 0));
6719 GL_EXTCALL(glUseProgram(*blt_program
));
6724 loc
= GL_EXTCALL(glGetUniformLocation(*blt_program
, "mask"));
6725 GL_EXTCALL(glUniform4f(loc
, 0.0f
, 0.0f
, (float)ds_mask_size
->cx
, (float)ds_mask_size
->cy
));
6729 /* Context activation is done by the caller. */
6730 static void shader_glsl_deselect_depth_blt(void *shader_priv
, const struct wined3d_gl_info
*gl_info
)
6732 const struct glsl_context_data
*ctx_data
= context_get_current()->shader_backend_data
;
6735 program_id
= ctx_data
->glsl_program
? ctx_data
->glsl_program
->id
: 0;
6736 if (program_id
) TRACE("Using GLSL program %u\n", program_id
);
6738 GL_EXTCALL(glUseProgram(program_id
));
6739 checkGLcall("glUseProgram");
6742 static void shader_glsl_invalidate_contexts_program(struct wined3d_device
*device
,
6743 const struct glsl_shader_prog_link
*program
)
6745 const struct glsl_context_data
*ctx_data
;
6746 struct wined3d_context
*context
;
6749 for (i
= 0; i
< device
->context_count
; ++i
)
6751 context
= device
->contexts
[i
];
6752 ctx_data
= context
->shader_backend_data
;
6754 if (ctx_data
->glsl_program
== program
)
6755 shader_glsl_invalidate_current_program(context
);
6759 static void shader_glsl_destroy(struct wined3d_shader
*shader
)
6761 struct glsl_shader_private
*shader_data
= shader
->backend_data
;
6762 struct wined3d_device
*device
= shader
->device
;
6763 struct shader_glsl_priv
*priv
= device
->shader_priv
;
6764 const struct wined3d_gl_info
*gl_info
;
6765 const struct list
*linked_programs
;
6766 struct wined3d_context
*context
;
6768 if (!shader_data
|| !shader_data
->num_gl_shaders
)
6770 HeapFree(GetProcessHeap(), 0, shader_data
);
6771 shader
->backend_data
= NULL
;
6775 context
= context_acquire(device
, NULL
);
6776 gl_info
= context
->gl_info
;
6778 TRACE("Deleting linked programs.\n");
6779 linked_programs
= &shader
->linked_programs
;
6780 if (linked_programs
->next
)
6782 struct glsl_shader_prog_link
*entry
, *entry2
;
6785 switch (shader
->reg_maps
.shader_version
.type
)
6787 case WINED3D_SHADER_TYPE_PIXEL
:
6789 struct glsl_ps_compiled_shader
*gl_shaders
= shader_data
->gl_shaders
.ps
;
6791 for (i
= 0; i
< shader_data
->num_gl_shaders
; ++i
)
6793 TRACE("Deleting pixel shader %u.\n", gl_shaders
[i
].id
);
6794 GL_EXTCALL(glDeleteShader(gl_shaders
[i
].id
));
6795 checkGLcall("glDeleteShader");
6797 HeapFree(GetProcessHeap(), 0, shader_data
->gl_shaders
.ps
);
6799 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, linked_programs
,
6800 struct glsl_shader_prog_link
, ps
.shader_entry
)
6802 shader_glsl_invalidate_contexts_program(device
, entry
);
6803 delete_glsl_program_entry(priv
, gl_info
, entry
);
6809 case WINED3D_SHADER_TYPE_VERTEX
:
6811 struct glsl_vs_compiled_shader
*gl_shaders
= shader_data
->gl_shaders
.vs
;
6813 for (i
= 0; i
< shader_data
->num_gl_shaders
; ++i
)
6815 TRACE("Deleting vertex shader %u.\n", gl_shaders
[i
].id
);
6816 GL_EXTCALL(glDeleteShader(gl_shaders
[i
].id
));
6817 checkGLcall("glDeleteShader");
6819 HeapFree(GetProcessHeap(), 0, shader_data
->gl_shaders
.vs
);
6821 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, linked_programs
,
6822 struct glsl_shader_prog_link
, vs
.shader_entry
)
6824 shader_glsl_invalidate_contexts_program(device
, entry
);
6825 delete_glsl_program_entry(priv
, gl_info
, entry
);
6831 case WINED3D_SHADER_TYPE_GEOMETRY
:
6833 struct glsl_gs_compiled_shader
*gl_shaders
= shader_data
->gl_shaders
.gs
;
6835 for (i
= 0; i
< shader_data
->num_gl_shaders
; ++i
)
6837 TRACE("Deleting geometry shader %u.\n", gl_shaders
[i
].id
);
6838 GL_EXTCALL(glDeleteShader(gl_shaders
[i
].id
));
6839 checkGLcall("glDeleteShader");
6841 HeapFree(GetProcessHeap(), 0, shader_data
->gl_shaders
.gs
);
6843 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, linked_programs
,
6844 struct glsl_shader_prog_link
, gs
.shader_entry
)
6846 shader_glsl_invalidate_contexts_program(device
, entry
);
6847 delete_glsl_program_entry(priv
, gl_info
, entry
);
6854 ERR("Unhandled shader type %#x.\n", shader
->reg_maps
.shader_version
.type
);
6859 HeapFree(GetProcessHeap(), 0, shader
->backend_data
);
6860 shader
->backend_data
= NULL
;
6862 context_release(context
);
6865 static int glsl_program_key_compare(const void *key
, const struct wine_rb_entry
*entry
)
6867 const struct glsl_program_key
*k
= key
;
6868 const struct glsl_shader_prog_link
*prog
= WINE_RB_ENTRY_VALUE(entry
,
6869 const struct glsl_shader_prog_link
, program_lookup_entry
);
6871 if (k
->vs_id
> prog
->vs
.id
) return 1;
6872 else if (k
->vs_id
< prog
->vs
.id
) return -1;
6874 if (k
->gs_id
> prog
->gs
.id
) return 1;
6875 else if (k
->gs_id
< prog
->gs
.id
) return -1;
6877 if (k
->ps_id
> prog
->ps
.id
) return 1;
6878 else if (k
->ps_id
< prog
->ps
.id
) return -1;
6883 static BOOL
constant_heap_init(struct constant_heap
*heap
, unsigned int constant_count
)
6885 SIZE_T size
= (constant_count
+ 1) * sizeof(*heap
->entries
)
6886 + constant_count
* sizeof(*heap
->contained
)
6887 + constant_count
* sizeof(*heap
->positions
);
6888 void *mem
= HeapAlloc(GetProcessHeap(), 0, size
);
6892 ERR("Failed to allocate memory\n");
6896 heap
->entries
= mem
;
6897 heap
->entries
[1].version
= 0;
6898 heap
->contained
= (BOOL
*)(heap
->entries
+ constant_count
+ 1);
6899 memset(heap
->contained
, 0, constant_count
* sizeof(*heap
->contained
));
6900 heap
->positions
= (unsigned int *)(heap
->contained
+ constant_count
);
6906 static void constant_heap_free(struct constant_heap
*heap
)
6908 HeapFree(GetProcessHeap(), 0, heap
->entries
);
6911 static const struct wine_rb_functions wined3d_glsl_program_rb_functions
=
6916 glsl_program_key_compare
,
6919 static HRESULT
shader_glsl_alloc(struct wined3d_device
*device
, const struct wined3d_vertex_pipe_ops
*vertex_pipe
,
6920 const struct fragment_pipeline
*fragment_pipe
)
6922 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
6923 struct shader_glsl_priv
*priv
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(struct shader_glsl_priv
));
6924 SIZE_T stack_size
= wined3d_log2i(max(gl_info
->limits
.glsl_vs_float_constants
,
6925 gl_info
->limits
.glsl_ps_float_constants
)) + 1;
6926 struct fragment_caps fragment_caps
;
6927 void *vertex_priv
, *fragment_priv
;
6929 if (!(vertex_priv
= vertex_pipe
->vp_alloc(&glsl_shader_backend
, priv
)))
6931 ERR("Failed to initialize vertex pipe.\n");
6932 HeapFree(GetProcessHeap(), 0, priv
);
6936 if (!(fragment_priv
= fragment_pipe
->alloc_private(&glsl_shader_backend
, priv
)))
6938 ERR("Failed to initialize fragment pipe.\n");
6939 vertex_pipe
->vp_free(device
);
6940 HeapFree(GetProcessHeap(), 0, priv
);
6944 if (!string_buffer_init(&priv
->shader_buffer
))
6946 ERR("Failed to initialize shader buffer.\n");
6950 priv
->stack
= HeapAlloc(GetProcessHeap(), 0, stack_size
* sizeof(*priv
->stack
));
6953 ERR("Failed to allocate memory.\n");
6957 if (!constant_heap_init(&priv
->vconst_heap
, gl_info
->limits
.glsl_vs_float_constants
))
6959 ERR("Failed to initialize vertex shader constant heap\n");
6963 if (!constant_heap_init(&priv
->pconst_heap
, gl_info
->limits
.glsl_ps_float_constants
))
6965 ERR("Failed to initialize pixel shader constant heap\n");
6969 if (wine_rb_init(&priv
->program_lookup
, &wined3d_glsl_program_rb_functions
) == -1)
6971 ERR("Failed to initialize rbtree.\n");
6975 priv
->next_constant_version
= 1;
6976 priv
->vertex_pipe
= vertex_pipe
;
6977 priv
->fragment_pipe
= fragment_pipe
;
6978 fragment_pipe
->get_caps(gl_info
, &fragment_caps
);
6979 priv
->ffp_proj_control
= fragment_caps
.wined3d_caps
& WINED3D_FRAGMENT_CAP_PROJ_CONTROL
;
6981 device
->vertex_priv
= vertex_priv
;
6982 device
->fragment_priv
= fragment_priv
;
6983 device
->shader_priv
= priv
;
6988 constant_heap_free(&priv
->pconst_heap
);
6989 constant_heap_free(&priv
->vconst_heap
);
6990 HeapFree(GetProcessHeap(), 0, priv
->stack
);
6991 string_buffer_free(&priv
->shader_buffer
);
6992 fragment_pipe
->free_private(device
);
6993 vertex_pipe
->vp_free(device
);
6994 HeapFree(GetProcessHeap(), 0, priv
);
6995 return E_OUTOFMEMORY
;
6998 /* Context activation is done by the caller. */
6999 static void shader_glsl_free(struct wined3d_device
*device
)
7001 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
7002 struct shader_glsl_priv
*priv
= device
->shader_priv
;
7005 for (i
= 0; i
< WINED3D_GL_RES_TYPE_COUNT
; ++i
)
7007 if (priv
->depth_blt_program_full
[i
])
7009 GL_EXTCALL(glDeleteProgram(priv
->depth_blt_program_full
[i
]));
7011 if (priv
->depth_blt_program_masked
[i
])
7013 GL_EXTCALL(glDeleteProgram(priv
->depth_blt_program_masked
[i
]));
7017 wine_rb_destroy(&priv
->program_lookup
, NULL
, NULL
);
7018 constant_heap_free(&priv
->pconst_heap
);
7019 constant_heap_free(&priv
->vconst_heap
);
7020 HeapFree(GetProcessHeap(), 0, priv
->stack
);
7021 string_buffer_free(&priv
->shader_buffer
);
7022 priv
->fragment_pipe
->free_private(device
);
7023 priv
->vertex_pipe
->vp_free(device
);
7025 HeapFree(GetProcessHeap(), 0, device
->shader_priv
);
7026 device
->shader_priv
= NULL
;
7029 static BOOL
shader_glsl_allocate_context_data(struct wined3d_context
*context
)
7031 return !!(context
->shader_backend_data
= HeapAlloc(GetProcessHeap(),
7032 HEAP_ZERO_MEMORY
, sizeof(struct glsl_context_data
)));
7035 static void shader_glsl_free_context_data(struct wined3d_context
*context
)
7037 HeapFree(GetProcessHeap(), 0, context
->shader_backend_data
);
7040 static void shader_glsl_get_caps(const struct wined3d_gl_info
*gl_info
, struct shader_caps
*caps
)
7044 if (gl_info
->supported
[EXT_GPU_SHADER4
] && gl_info
->supported
[ARB_SHADER_BIT_ENCODING
]
7045 && gl_info
->supported
[ARB_GEOMETRY_SHADER4
] && gl_info
->glsl_version
>= MAKEDWORD_VERSION(1, 50)
7046 && gl_info
->supported
[ARB_DRAW_ELEMENTS_BASE_VERTEX
] && gl_info
->supported
[ARB_DRAW_INSTANCED
]
7047 && gl_info
->supported
[ARB_TEXTURE_RG
] && gl_info
->supported
[ARB_SAMPLER_OBJECTS
])
7049 /* ARB_shader_texture_lod or EXT_gpu_shader4 is required for the SM3
7050 * texldd and texldl instructions. */
7051 else if (gl_info
->supported
[ARB_SHADER_TEXTURE_LOD
] || gl_info
->supported
[EXT_GPU_SHADER4
])
7055 TRACE("Shader model %u.\n", shader_model
);
7057 caps
->vs_version
= min(wined3d_settings
.max_sm_vs
, shader_model
);
7058 caps
->gs_version
= min(wined3d_settings
.max_sm_gs
, shader_model
);
7059 caps
->ps_version
= min(wined3d_settings
.max_sm_ps
, shader_model
);
7061 caps
->vs_uniform_count
= gl_info
->limits
.glsl_vs_float_constants
;
7062 caps
->ps_uniform_count
= gl_info
->limits
.glsl_ps_float_constants
;
7064 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
7065 * Direct3D minimum requirement.
7067 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
7068 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
7070 * The problem is that the refrast clamps temporary results in the shader to
7071 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
7072 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
7073 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
7074 * offer a way to query this.
7076 if (shader_model
>= 4)
7077 caps
->ps_1x_max_value
= FLT_MAX
;
7079 caps
->ps_1x_max_value
= 1024.0f
;
7081 /* Ideally we'd only set caps like sRGB writes here if supported by both
7082 * the shader backend and the fragment pipe, but we can get called before
7083 * shader_glsl_alloc(). */
7084 caps
->wined3d_caps
= WINED3D_SHADER_CAP_VS_CLIPPING
7085 | WINED3D_SHADER_CAP_SRGB_WRITE
;
7088 static BOOL
shader_glsl_color_fixup_supported(struct color_fixup_desc fixup
)
7090 if (TRACE_ON(d3d_shader
) && TRACE_ON(d3d
))
7092 TRACE("Checking support for fixup:\n");
7093 dump_color_fixup_desc(fixup
);
7096 /* We support everything except YUV conversions. */
7097 if (!is_complex_fixup(fixup
))
7103 TRACE("[FAILED]\n");
7107 static const SHADER_HANDLER shader_glsl_instruction_handler_table
[WINED3DSIH_TABLE_SIZE
] =
7109 /* WINED3DSIH_ABS */ shader_glsl_map2gl
,
7110 /* WINED3DSIH_ADD */ shader_glsl_binop
,
7111 /* WINED3DSIH_AND */ shader_glsl_binop
,
7112 /* WINED3DSIH_BEM */ shader_glsl_bem
,
7113 /* WINED3DSIH_BREAK */ shader_glsl_break
,
7114 /* WINED3DSIH_BREAKC */ shader_glsl_breakc
,
7115 /* WINED3DSIH_BREAKP */ shader_glsl_breakp
,
7116 /* WINED3DSIH_CALL */ shader_glsl_call
,
7117 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz
,
7118 /* WINED3DSIH_CMP */ shader_glsl_conditional_move
,
7119 /* WINED3DSIH_CND */ shader_glsl_cnd
,
7120 /* WINED3DSIH_CRS */ shader_glsl_cross
,
7121 /* WINED3DSIH_CUT */ shader_glsl_cut
,
7122 /* WINED3DSIH_DCL */ shader_glsl_nop
,
7123 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ shader_glsl_nop
,
7124 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ shader_glsl_nop
,
7125 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ shader_glsl_nop
,
7126 /* WINED3DSIH_DCL_VERTICES_OUT */ shader_glsl_nop
,
7127 /* WINED3DSIH_DEF */ shader_glsl_nop
,
7128 /* WINED3DSIH_DEFB */ shader_glsl_nop
,
7129 /* WINED3DSIH_DEFI */ shader_glsl_nop
,
7130 /* WINED3DSIH_DIV */ shader_glsl_binop
,
7131 /* WINED3DSIH_DP2 */ shader_glsl_dot
,
7132 /* WINED3DSIH_DP2ADD */ shader_glsl_dp2add
,
7133 /* WINED3DSIH_DP3 */ shader_glsl_dot
,
7134 /* WINED3DSIH_DP4 */ shader_glsl_dot
,
7135 /* WINED3DSIH_DST */ shader_glsl_dst
,
7136 /* WINED3DSIH_DSX */ shader_glsl_map2gl
,
7137 /* WINED3DSIH_DSY */ shader_glsl_map2gl
,
7138 /* WINED3DSIH_ELSE */ shader_glsl_else
,
7139 /* WINED3DSIH_EMIT */ shader_glsl_emit
,
7140 /* WINED3DSIH_ENDIF */ shader_glsl_end
,
7141 /* WINED3DSIH_ENDLOOP */ shader_glsl_end
,
7142 /* WINED3DSIH_ENDREP */ shader_glsl_end
,
7143 /* WINED3DSIH_EQ */ shader_glsl_relop
,
7144 /* WINED3DSIH_EXP */ shader_glsl_scalar_op
,
7145 /* WINED3DSIH_EXPP */ shader_glsl_expp
,
7146 /* WINED3DSIH_FRC */ shader_glsl_map2gl
,
7147 /* WINED3DSIH_FTOI */ shader_glsl_to_int
,
7148 /* WINED3DSIH_GE */ shader_glsl_relop
,
7149 /* WINED3DSIH_IADD */ shader_glsl_binop
,
7150 /* WINED3DSIH_IEQ */ NULL
,
7151 /* WINED3DSIH_IF */ shader_glsl_if
,
7152 /* WINED3DSIH_IFC */ shader_glsl_ifc
,
7153 /* WINED3DSIH_IGE */ shader_glsl_relop
,
7154 /* WINED3DSIH_IMUL */ shader_glsl_imul
,
7155 /* WINED3DSIH_ISHL */ shader_glsl_binop
,
7156 /* WINED3DSIH_ITOF */ shader_glsl_to_float
,
7157 /* WINED3DSIH_LABEL */ shader_glsl_label
,
7158 /* WINED3DSIH_LD */ NULL
,
7159 /* WINED3DSIH_LIT */ shader_glsl_lit
,
7160 /* WINED3DSIH_LOG */ shader_glsl_scalar_op
,
7161 /* WINED3DSIH_LOGP */ shader_glsl_scalar_op
,
7162 /* WINED3DSIH_LOOP */ shader_glsl_loop
,
7163 /* WINED3DSIH_LRP */ shader_glsl_lrp
,
7164 /* WINED3DSIH_LT */ shader_glsl_relop
,
7165 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn
,
7166 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn
,
7167 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn
,
7168 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn
,
7169 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn
,
7170 /* WINED3DSIH_MAD */ shader_glsl_mad
,
7171 /* WINED3DSIH_MAX */ shader_glsl_map2gl
,
7172 /* WINED3DSIH_MIN */ shader_glsl_map2gl
,
7173 /* WINED3DSIH_MOV */ shader_glsl_mov
,
7174 /* WINED3DSIH_MOVA */ shader_glsl_mov
,
7175 /* WINED3DSIH_MOVC */ shader_glsl_conditional_move
,
7176 /* WINED3DSIH_MUL */ shader_glsl_binop
,
7177 /* WINED3DSIH_NE */ shader_glsl_relop
,
7178 /* WINED3DSIH_NOP */ shader_glsl_nop
,
7179 /* WINED3DSIH_NRM */ shader_glsl_nrm
,
7180 /* WINED3DSIH_OR */ shader_glsl_binop
,
7181 /* WINED3DSIH_PHASE */ shader_glsl_nop
,
7182 /* WINED3DSIH_POW */ shader_glsl_pow
,
7183 /* WINED3DSIH_RCP */ shader_glsl_scalar_op
,
7184 /* WINED3DSIH_REP */ shader_glsl_rep
,
7185 /* WINED3DSIH_RET */ shader_glsl_ret
,
7186 /* WINED3DSIH_ROUND_NI */ shader_glsl_map2gl
,
7187 /* WINED3DSIH_RSQ */ shader_glsl_scalar_op
,
7188 /* WINED3DSIH_SAMPLE */ shader_glsl_sample
,
7189 /* WINED3DSIH_SAMPLE_GRAD */ NULL
,
7190 /* WINED3DSIH_SAMPLE_LOD */ NULL
,
7191 /* WINED3DSIH_SETP */ NULL
,
7192 /* WINED3DSIH_SGE */ shader_glsl_compare
,
7193 /* WINED3DSIH_SGN */ shader_glsl_sgn
,
7194 /* WINED3DSIH_SINCOS */ shader_glsl_sincos
,
7195 /* WINED3DSIH_SLT */ shader_glsl_compare
,
7196 /* WINED3DSIH_SQRT */ shader_glsl_map2gl
,
7197 /* WINED3DSIH_SUB */ shader_glsl_binop
,
7198 /* WINED3DSIH_TEX */ shader_glsl_tex
,
7199 /* WINED3DSIH_TEXBEM */ shader_glsl_texbem
,
7200 /* WINED3DSIH_TEXBEML */ shader_glsl_texbem
,
7201 /* WINED3DSIH_TEXCOORD */ shader_glsl_texcoord
,
7202 /* WINED3DSIH_TEXDEPTH */ shader_glsl_texdepth
,
7203 /* WINED3DSIH_TEXDP3 */ shader_glsl_texdp3
,
7204 /* WINED3DSIH_TEXDP3TEX */ shader_glsl_texdp3tex
,
7205 /* WINED3DSIH_TEXKILL */ shader_glsl_texkill
,
7206 /* WINED3DSIH_TEXLDD */ shader_glsl_texldd
,
7207 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl
,
7208 /* WINED3DSIH_TEXM3x2DEPTH */ shader_glsl_texm3x2depth
,
7209 /* WINED3DSIH_TEXM3x2PAD */ shader_glsl_texm3x2pad
,
7210 /* WINED3DSIH_TEXM3x2TEX */ shader_glsl_texm3x2tex
,
7211 /* WINED3DSIH_TEXM3x3 */ shader_glsl_texm3x3
,
7212 /* WINED3DSIH_TEXM3x3DIFF */ NULL
,
7213 /* WINED3DSIH_TEXM3x3PAD */ shader_glsl_texm3x3pad
,
7214 /* WINED3DSIH_TEXM3x3SPEC */ shader_glsl_texm3x3spec
,
7215 /* WINED3DSIH_TEXM3x3TEX */ shader_glsl_texm3x3tex
,
7216 /* WINED3DSIH_TEXM3x3VSPEC */ shader_glsl_texm3x3vspec
,
7217 /* WINED3DSIH_TEXREG2AR */ shader_glsl_texreg2ar
,
7218 /* WINED3DSIH_TEXREG2GB */ shader_glsl_texreg2gb
,
7219 /* WINED3DSIH_TEXREG2RGB */ shader_glsl_texreg2rgb
,
7220 /* WINED3DSIH_UDIV */ shader_glsl_udiv
,
7221 /* WINED3DSIH_UGE */ shader_glsl_relop
,
7222 /* WINED3DSIH_USHR */ shader_glsl_binop
,
7223 /* WINED3DSIH_UTOF */ shader_glsl_to_float
,
7224 /* WINED3DSIH_XOR */ shader_glsl_binop
,
7227 static void shader_glsl_handle_instruction(const struct wined3d_shader_instruction
*ins
) {
7228 SHADER_HANDLER hw_fct
;
7230 /* Select handler */
7231 hw_fct
= shader_glsl_instruction_handler_table
[ins
->handler_idx
];
7233 /* Unhandled opcode */
7236 FIXME("Backend can't handle opcode %#x\n", ins
->handler_idx
);
7241 shader_glsl_add_instruction_modifiers(ins
);
7244 static BOOL
shader_glsl_has_ffp_proj_control(void *shader_priv
)
7246 struct shader_glsl_priv
*priv
= shader_priv
;
7248 return priv
->ffp_proj_control
;
7251 const struct wined3d_shader_backend_ops glsl_shader_backend
=
7253 shader_glsl_handle_instruction
,
7255 shader_glsl_disable
,
7256 shader_glsl_select_depth_blt
,
7257 shader_glsl_deselect_depth_blt
,
7258 shader_glsl_update_float_vertex_constants
,
7259 shader_glsl_update_float_pixel_constants
,
7260 shader_glsl_load_constants
,
7261 shader_glsl_destroy
,
7264 shader_glsl_allocate_context_data
,
7265 shader_glsl_free_context_data
,
7266 shader_glsl_get_caps
,
7267 shader_glsl_color_fixup_supported
,
7268 shader_glsl_has_ffp_proj_control
,
7271 static void glsl_vertex_pipe_vp_enable(const struct wined3d_gl_info
*gl_info
, BOOL enable
)
7274 gl_info
->gl_ops
.gl
.p_glEnable(GL_VERTEX_PROGRAM_POINT_SIZE_ARB
);
7276 gl_info
->gl_ops
.gl
.p_glDisable(GL_VERTEX_PROGRAM_POINT_SIZE_ARB
);
7277 checkGLcall("GL_VERTEX_PROGRAM_POINT_SIZE_ARB");
7280 static void glsl_vertex_pipe_vp_get_caps(const struct wined3d_gl_info
*gl_info
, struct wined3d_vertex_caps
*caps
)
7282 caps
->xyzrhw
= TRUE
;
7283 caps
->max_active_lights
= gl_info
->limits
.lights
;
7284 caps
->max_vertex_blend_matrices
= 1;
7285 caps
->max_vertex_blend_matrix_index
= 0;
7286 caps
->vertex_processing_caps
= WINED3DVTXPCAPS_TEXGEN
7287 | WINED3DVTXPCAPS_MATERIALSOURCE7
7288 | WINED3DVTXPCAPS_VERTEXFOG
7289 | WINED3DVTXPCAPS_DIRECTIONALLIGHTS
7290 | WINED3DVTXPCAPS_POSITIONALLIGHTS
7291 | WINED3DVTXPCAPS_LOCALVIEWER
7292 | WINED3DVTXPCAPS_TEXGEN_SPHEREMAP
;
7293 caps
->fvf_caps
= WINED3DFVFCAPS_PSIZE
| 8; /* 8 texture coordinates. */
7294 caps
->max_user_clip_planes
= gl_info
->limits
.clipplanes
;
7295 caps
->raster_caps
= WINED3DPRASTERCAPS_FOGRANGE
;
7298 static void *glsl_vertex_pipe_vp_alloc(const struct wined3d_shader_backend_ops
*shader_backend
, void *shader_priv
)
7300 struct shader_glsl_priv
*priv
;
7302 if (shader_backend
== &glsl_shader_backend
)
7306 if (wine_rb_init(&priv
->ffp_vertex_shaders
, &wined3d_ffp_vertex_program_rb_functions
) == -1)
7308 ERR("Failed to initialize rbtree.\n");
7315 FIXME("GLSL vertex pipe without GLSL shader backend not implemented.\n");
7320 static void shader_glsl_free_ffp_vertex_shader(struct wine_rb_entry
*entry
, void *context
)
7322 struct glsl_ffp_vertex_shader
*shader
= WINE_RB_ENTRY_VALUE(entry
,
7323 struct glsl_ffp_vertex_shader
, desc
.entry
);
7324 struct glsl_shader_prog_link
*program
, *program2
;
7325 struct glsl_ffp_destroy_ctx
*ctx
= context
;
7327 LIST_FOR_EACH_ENTRY_SAFE(program
, program2
, &shader
->linked_programs
,
7328 struct glsl_shader_prog_link
, vs
.shader_entry
)
7330 delete_glsl_program_entry(ctx
->priv
, ctx
->gl_info
, program
);
7332 ctx
->gl_info
->gl_ops
.ext
.p_glDeleteShader(shader
->id
);
7333 HeapFree(GetProcessHeap(), 0, shader
);
7336 /* Context activation is done by the caller. */
7337 static void glsl_vertex_pipe_vp_free(struct wined3d_device
*device
)
7339 struct shader_glsl_priv
*priv
= device
->vertex_priv
;
7340 struct glsl_ffp_destroy_ctx ctx
;
7343 ctx
.gl_info
= &device
->adapter
->gl_info
;
7344 wine_rb_destroy(&priv
->ffp_vertex_shaders
, shader_glsl_free_ffp_vertex_shader
, &ctx
);
7347 static void glsl_vertex_pipe_shader(struct wined3d_context
*context
,
7348 const struct wined3d_state
*state
, DWORD state_id
)
7350 context
->shader_update_mask
|= 1 << WINED3D_SHADER_TYPE_VERTEX
;
7353 static void glsl_vertex_pipe_vdecl(struct wined3d_context
*context
,
7354 const struct wined3d_state
*state
, DWORD state_id
)
7356 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
7357 BOOL transformed
= context
->stream_info
.position_transformed
;
7358 BOOL wasrhw
= context
->last_was_rhw
;
7361 context
->last_was_rhw
= transformed
;
7365 if (context
->last_was_vshader
)
7367 for (i
= 0; i
< gl_info
->limits
.clipplanes
; ++i
)
7368 clipplane(context
, state
, STATE_CLIPPLANE(i
));
7371 if (transformed
!= wasrhw
)
7372 context
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_PROJ
;
7374 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
7376 if (!isStateDirty(context
, STATE_TRANSFORM(WINED3D_TS_TEXTURE0
+ i
)))
7377 transform_texture(context
, state
, STATE_TEXTURESTAGE(i
, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
));
7380 if (!isStateDirty(context
, STATE_RENDER(WINED3D_RS_LIGHTING
)))
7381 state_lighting(context
, state
, STATE_RENDER(WINED3D_RS_LIGHTING
));
7382 if (!isStateDirty(context
, STATE_RENDER(WINED3D_RS_NORMALIZENORMALS
)))
7383 state_normalize(context
, state
, STATE_RENDER(WINED3D_RS_NORMALIZENORMALS
));
7385 /* Because of settings->texcoords, we have to always regenerate the
7386 * vertex shader on a vdecl change.
7387 * TODO: Just always output all the texcoords when there are enough
7388 * varyings available to drop the dependency. */
7389 context
->shader_update_mask
|= 1 << WINED3D_SHADER_TYPE_VERTEX
;
7392 && state
->shader
[WINED3D_SHADER_TYPE_PIXEL
]->reg_maps
.shader_version
.major
== 1
7393 && state
->shader
[WINED3D_SHADER_TYPE_PIXEL
]->reg_maps
.shader_version
.minor
<= 3)
7394 context
->shader_update_mask
|= 1 << WINED3D_SHADER_TYPE_PIXEL
;
7398 if (!context
->last_was_vshader
)
7400 /* Vertex shader clipping ignores the view matrix. Update all clipplanes. */
7401 for (i
= 0; i
< gl_info
->limits
.clipplanes
; ++i
)
7402 clipplane(context
, state
, STATE_CLIPPLANE(i
));
7406 context
->last_was_vshader
= use_vs(state
);
7409 static void glsl_vertex_pipe_vs(struct wined3d_context
*context
,
7410 const struct wined3d_state
*state
, DWORD state_id
)
7412 context
->shader_update_mask
|= 1 << WINED3D_SHADER_TYPE_VERTEX
;
7413 /* Different vertex shaders potentially require a different vertex attributes setup. */
7414 if (!isStateDirty(context
, STATE_VDECL
))
7415 context_apply_state(context
, state
, STATE_VDECL
);
7418 static void glsl_vertex_pipe_world(struct wined3d_context
*context
,
7419 const struct wined3d_state
*state
, DWORD state_id
)
7421 context
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_MODELVIEW
;
7424 static void glsl_vertex_pipe_view(struct wined3d_context
*context
, const struct wined3d_state
*state
, DWORD state_id
)
7426 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
7427 const struct wined3d_light_info
*light
= NULL
;
7430 context
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_MODELVIEW
;
7432 /* Light settings are affected by the ModelView transform in OpenGL, the View transform in Direct3D. */
7433 gl_info
->gl_ops
.gl
.p_glMatrixMode(GL_MODELVIEW
);
7434 gl_info
->gl_ops
.gl
.p_glPushMatrix();
7435 gl_info
->gl_ops
.gl
.p_glLoadMatrixf(&state
->transforms
[WINED3D_TS_VIEW
]._11
);
7437 for (k
= 0; k
< gl_info
->limits
.lights
; ++k
)
7439 if (!(light
= state
->lights
[k
]))
7441 gl_info
->gl_ops
.gl
.p_glLightfv(GL_LIGHT0
+ light
->glIndex
, GL_POSITION
, light
->lightPosn
);
7442 checkGLcall("glLightfv posn");
7443 gl_info
->gl_ops
.gl
.p_glLightfv(GL_LIGHT0
+ light
->glIndex
, GL_SPOT_DIRECTION
, light
->lightDirn
);
7444 checkGLcall("glLightfv dirn");
7447 gl_info
->gl_ops
.gl
.p_glPopMatrix();
7449 for (k
= 0; k
< gl_info
->limits
.clipplanes
; ++k
)
7451 if (!isStateDirty(context
, STATE_CLIPPLANE(k
)))
7452 clipplane(context
, state
, STATE_CLIPPLANE(k
));
7455 if (context
->swapchain
->device
->vertexBlendUsed
)
7460 FIXME("Vertex blending emulation.\n");
7464 static void glsl_vertex_pipe_projection(struct wined3d_context
*context
,
7465 const struct wined3d_state
*state
, DWORD state_id
)
7467 /* Table fog behavior depends on the projection matrix. */
7468 if (state
->render_states
[WINED3D_RS_FOGENABLE
]
7469 && state
->render_states
[WINED3D_RS_FOGTABLEMODE
] != WINED3D_FOG_NONE
)
7470 context
->shader_update_mask
|= 1 << WINED3D_SHADER_TYPE_VERTEX
;
7471 context
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_PROJ
;
7474 static void glsl_vertex_pipe_viewport(struct wined3d_context
*context
,
7475 const struct wined3d_state
*state
, DWORD state_id
)
7477 if (!isStateDirty(context
, STATE_TRANSFORM(WINED3D_TS_PROJECTION
)))
7478 glsl_vertex_pipe_projection(context
, state
, STATE_TRANSFORM(WINED3D_TS_PROJECTION
));
7479 if (!isStateDirty(context
, STATE_RENDER(WINED3D_RS_POINTSCALEENABLE
))
7480 && state
->render_states
[WINED3D_RS_POINTSCALEENABLE
])
7481 state_pscale(context
, state
, STATE_RENDER(WINED3D_RS_POINTSCALEENABLE
));
7482 context
->constant_update_mask
|= WINED3D_SHADER_CONST_VS_POS_FIXUP
;
7485 static const struct StateEntryTemplate glsl_vertex_pipe_vp_states
[] =
7487 {STATE_VDECL
, {STATE_VDECL
, glsl_vertex_pipe_vdecl
}, WINED3D_GL_EXT_NONE
},
7488 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), glsl_vertex_pipe_vs
}, WINED3D_GL_EXT_NONE
},
7489 {STATE_MATERIAL
, {STATE_RENDER(WINED3D_RS_SPECULARENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
7490 {STATE_RENDER(WINED3D_RS_SPECULARENABLE
), {STATE_RENDER(WINED3D_RS_SPECULARENABLE
), state_specularenable
}, WINED3D_GL_EXT_NONE
},
7492 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), clipplane
}, WINED3D_GL_EXT_NONE
},
7493 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), clipplane
}, WINED3D_GL_EXT_NONE
},
7494 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), clipplane
}, WINED3D_GL_EXT_NONE
},
7495 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), clipplane
}, WINED3D_GL_EXT_NONE
},
7496 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), clipplane
}, WINED3D_GL_EXT_NONE
},
7497 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), clipplane
}, WINED3D_GL_EXT_NONE
},
7498 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), clipplane
}, WINED3D_GL_EXT_NONE
},
7499 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), clipplane
}, WINED3D_GL_EXT_NONE
},
7500 {STATE_CLIPPLANE(8), {STATE_CLIPPLANE(8), clipplane
}, WINED3D_GL_EXT_NONE
},
7501 {STATE_CLIPPLANE(9), {STATE_CLIPPLANE(9), clipplane
}, WINED3D_GL_EXT_NONE
},
7502 {STATE_CLIPPLANE(10), {STATE_CLIPPLANE(10), clipplane
}, WINED3D_GL_EXT_NONE
},
7503 {STATE_CLIPPLANE(11), {STATE_CLIPPLANE(11), clipplane
}, WINED3D_GL_EXT_NONE
},
7504 {STATE_CLIPPLANE(12), {STATE_CLIPPLANE(12), clipplane
}, WINED3D_GL_EXT_NONE
},
7505 {STATE_CLIPPLANE(13), {STATE_CLIPPLANE(13), clipplane
}, WINED3D_GL_EXT_NONE
},
7506 {STATE_CLIPPLANE(14), {STATE_CLIPPLANE(14), clipplane
}, WINED3D_GL_EXT_NONE
},
7507 {STATE_CLIPPLANE(15), {STATE_CLIPPLANE(15), clipplane
}, WINED3D_GL_EXT_NONE
},
7508 {STATE_CLIPPLANE(16), {STATE_CLIPPLANE(16), clipplane
}, WINED3D_GL_EXT_NONE
},
7509 {STATE_CLIPPLANE(17), {STATE_CLIPPLANE(17), clipplane
}, WINED3D_GL_EXT_NONE
},
7510 {STATE_CLIPPLANE(18), {STATE_CLIPPLANE(18), clipplane
}, WINED3D_GL_EXT_NONE
},
7511 {STATE_CLIPPLANE(19), {STATE_CLIPPLANE(19), clipplane
}, WINED3D_GL_EXT_NONE
},
7512 {STATE_CLIPPLANE(20), {STATE_CLIPPLANE(20), clipplane
}, WINED3D_GL_EXT_NONE
},
7513 {STATE_CLIPPLANE(21), {STATE_CLIPPLANE(21), clipplane
}, WINED3D_GL_EXT_NONE
},
7514 {STATE_CLIPPLANE(22), {STATE_CLIPPLANE(22), clipplane
}, WINED3D_GL_EXT_NONE
},
7515 {STATE_CLIPPLANE(23), {STATE_CLIPPLANE(23), clipplane
}, WINED3D_GL_EXT_NONE
},
7516 {STATE_CLIPPLANE(24), {STATE_CLIPPLANE(24), clipplane
}, WINED3D_GL_EXT_NONE
},
7517 {STATE_CLIPPLANE(25), {STATE_CLIPPLANE(25), clipplane
}, WINED3D_GL_EXT_NONE
},
7518 {STATE_CLIPPLANE(26), {STATE_CLIPPLANE(26), clipplane
}, WINED3D_GL_EXT_NONE
},
7519 {STATE_CLIPPLANE(27), {STATE_CLIPPLANE(27), clipplane
}, WINED3D_GL_EXT_NONE
},
7520 {STATE_CLIPPLANE(28), {STATE_CLIPPLANE(28), clipplane
}, WINED3D_GL_EXT_NONE
},
7521 {STATE_CLIPPLANE(29), {STATE_CLIPPLANE(29), clipplane
}, WINED3D_GL_EXT_NONE
},
7522 {STATE_CLIPPLANE(30), {STATE_CLIPPLANE(30), clipplane
}, WINED3D_GL_EXT_NONE
},
7523 {STATE_CLIPPLANE(31), {STATE_CLIPPLANE(31), clipplane
}, WINED3D_GL_EXT_NONE
},
7525 {STATE_LIGHT_TYPE
, {STATE_RENDER(WINED3D_RS_FOGENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
7526 {STATE_ACTIVELIGHT(0), {STATE_ACTIVELIGHT(0), light
}, WINED3D_GL_EXT_NONE
},
7527 {STATE_ACTIVELIGHT(1), {STATE_ACTIVELIGHT(1), light
}, WINED3D_GL_EXT_NONE
},
7528 {STATE_ACTIVELIGHT(2), {STATE_ACTIVELIGHT(2), light
}, WINED3D_GL_EXT_NONE
},
7529 {STATE_ACTIVELIGHT(3), {STATE_ACTIVELIGHT(3), light
}, WINED3D_GL_EXT_NONE
},
7530 {STATE_ACTIVELIGHT(4), {STATE_ACTIVELIGHT(4), light
}, WINED3D_GL_EXT_NONE
},
7531 {STATE_ACTIVELIGHT(5), {STATE_ACTIVELIGHT(5), light
}, WINED3D_GL_EXT_NONE
},
7532 {STATE_ACTIVELIGHT(6), {STATE_ACTIVELIGHT(6), light
}, WINED3D_GL_EXT_NONE
},
7533 {STATE_ACTIVELIGHT(7), {STATE_ACTIVELIGHT(7), light
}, WINED3D_GL_EXT_NONE
},
7535 {STATE_VIEWPORT
, {STATE_VIEWPORT
, glsl_vertex_pipe_viewport
}, WINED3D_GL_EXT_NONE
},
7536 /* Transform states */
7537 {STATE_TRANSFORM(WINED3D_TS_VIEW
), {STATE_TRANSFORM(WINED3D_TS_VIEW
), glsl_vertex_pipe_view
}, WINED3D_GL_EXT_NONE
},
7538 {STATE_TRANSFORM(WINED3D_TS_PROJECTION
), {STATE_TRANSFORM(WINED3D_TS_PROJECTION
), glsl_vertex_pipe_projection
}, WINED3D_GL_EXT_NONE
},
7539 {STATE_TRANSFORM(WINED3D_TS_TEXTURE0
), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), NULL
}, WINED3D_GL_EXT_NONE
},
7540 {STATE_TRANSFORM(WINED3D_TS_TEXTURE1
), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), NULL
}, WINED3D_GL_EXT_NONE
},
7541 {STATE_TRANSFORM(WINED3D_TS_TEXTURE2
), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), NULL
}, WINED3D_GL_EXT_NONE
},
7542 {STATE_TRANSFORM(WINED3D_TS_TEXTURE3
), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), NULL
}, WINED3D_GL_EXT_NONE
},
7543 {STATE_TRANSFORM(WINED3D_TS_TEXTURE4
), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), NULL
}, WINED3D_GL_EXT_NONE
},
7544 {STATE_TRANSFORM(WINED3D_TS_TEXTURE5
), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), NULL
}, WINED3D_GL_EXT_NONE
},
7545 {STATE_TRANSFORM(WINED3D_TS_TEXTURE6
), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), NULL
}, WINED3D_GL_EXT_NONE
},
7546 {STATE_TRANSFORM(WINED3D_TS_TEXTURE7
), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), NULL
}, WINED3D_GL_EXT_NONE
},
7547 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), glsl_vertex_pipe_world
}, WINED3D_GL_EXT_NONE
},
7548 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), transform_texture
}, WINED3D_GL_EXT_NONE
},
7549 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), transform_texture
}, WINED3D_GL_EXT_NONE
},
7550 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), transform_texture
}, WINED3D_GL_EXT_NONE
},
7551 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), transform_texture
}, WINED3D_GL_EXT_NONE
},
7552 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), transform_texture
}, WINED3D_GL_EXT_NONE
},
7553 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), transform_texture
}, WINED3D_GL_EXT_NONE
},
7554 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), transform_texture
}, WINED3D_GL_EXT_NONE
},
7555 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), transform_texture
}, WINED3D_GL_EXT_NONE
},
7556 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXCOORD_INDEX
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
7557 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXCOORD_INDEX
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
7558 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXCOORD_INDEX
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
7559 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXCOORD_INDEX
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
7560 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXCOORD_INDEX
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
7561 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXCOORD_INDEX
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
7562 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXCOORD_INDEX
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
7563 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXCOORD_INDEX
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
7565 {STATE_RENDER(WINED3D_RS_FOGENABLE
), {STATE_RENDER(WINED3D_RS_FOGENABLE
), glsl_vertex_pipe_shader
}, WINED3D_GL_EXT_NONE
},
7566 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE
), {STATE_RENDER(WINED3D_RS_FOGENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
7567 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE
), {STATE_RENDER(WINED3D_RS_FOGENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
7568 {STATE_RENDER(WINED3D_RS_RANGEFOGENABLE
), {STATE_RENDER(WINED3D_RS_FOGENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
7569 {STATE_RENDER(WINED3D_RS_CLIPPING
), {STATE_RENDER(WINED3D_RS_CLIPPING
), state_clipping
}, WINED3D_GL_EXT_NONE
},
7570 {STATE_RENDER(WINED3D_RS_CLIPPLANEENABLE
), {STATE_RENDER(WINED3D_RS_CLIPPING
), NULL
}, WINED3D_GL_EXT_NONE
},
7571 {STATE_RENDER(WINED3D_RS_LIGHTING
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
7572 {STATE_RENDER(WINED3D_RS_AMBIENT
), {STATE_RENDER(WINED3D_RS_AMBIENT
), state_ambient
}, WINED3D_GL_EXT_NONE
},
7573 {STATE_RENDER(WINED3D_RS_COLORVERTEX
), {STATE_RENDER(WINED3D_RS_COLORVERTEX
), glsl_vertex_pipe_shader
}, WINED3D_GL_EXT_NONE
},
7574 {STATE_RENDER(WINED3D_RS_LOCALVIEWER
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
7575 {STATE_RENDER(WINED3D_RS_NORMALIZENORMALS
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
7576 {STATE_RENDER(WINED3D_RS_DIFFUSEMATERIALSOURCE
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
7577 {STATE_RENDER(WINED3D_RS_SPECULARMATERIALSOURCE
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
7578 {STATE_RENDER(WINED3D_RS_AMBIENTMATERIALSOURCE
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
7579 {STATE_RENDER(WINED3D_RS_EMISSIVEMATERIALSOURCE
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
7580 {STATE_RENDER(WINED3D_RS_VERTEXBLEND
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
7581 {STATE_RENDER(WINED3D_RS_POINTSIZE
), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
7582 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN
), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN
), state_psizemin_arb
}, ARB_POINT_PARAMETERS
},
7583 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN
), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN
), state_psizemin_ext
}, EXT_POINT_PARAMETERS
},
7584 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN
), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN
), state_psizemin_w
}, WINED3D_GL_EXT_NONE
},
7585 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE
), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE
), state_pointsprite
}, ARB_POINT_SPRITE
},
7586 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE
), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE
), state_pointsprite_w
}, WINED3D_GL_EXT_NONE
},
7587 {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE
), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE
), state_pscale
}, WINED3D_GL_EXT_NONE
},
7588 {STATE_RENDER(WINED3D_RS_POINTSCALE_A
), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
7589 {STATE_RENDER(WINED3D_RS_POINTSCALE_B
), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
7590 {STATE_RENDER(WINED3D_RS_POINTSCALE_C
), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
7591 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX
), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN
), NULL
}, ARB_POINT_PARAMETERS
},
7592 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX
), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN
), NULL
}, EXT_POINT_PARAMETERS
},
7593 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX
), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN
), NULL
}, WINED3D_GL_EXT_NONE
},
7594 {STATE_RENDER(WINED3D_RS_TWEENFACTOR
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
7595 {STATE_RENDER(WINED3D_RS_INDEXEDVERTEXBLENDENABLE
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
7596 /* Samplers for NP2 texture matrix adjustions. They are not needed if
7597 * GL_ARB_texture_non_power_of_two is supported, so register a NULL state
7598 * handler in that case to get the vertex part of sampler() skipped (VTF
7599 * is handled in the misc states). Otherwise, register
7600 * sampler_texmatrix(), which takes care of updating the texture matrix. */
7601 {STATE_SAMPLER(0), {0, NULL
}, ARB_TEXTURE_NON_POWER_OF_TWO
},
7602 {STATE_SAMPLER(0), {0, NULL
}, WINED3D_GL_NORMALIZED_TEXRECT
},
7603 {STATE_SAMPLER(0), {STATE_SAMPLER(0), sampler_texmatrix
}, WINED3D_GL_EXT_NONE
},
7604 {STATE_SAMPLER(1), {0, NULL
}, ARB_TEXTURE_NON_POWER_OF_TWO
},
7605 {STATE_SAMPLER(1), {0, NULL
}, WINED3D_GL_NORMALIZED_TEXRECT
},
7606 {STATE_SAMPLER(1), {STATE_SAMPLER(1), sampler_texmatrix
}, WINED3D_GL_EXT_NONE
},
7607 {STATE_SAMPLER(2), {0, NULL
}, ARB_TEXTURE_NON_POWER_OF_TWO
},
7608 {STATE_SAMPLER(2), {0, NULL
}, WINED3D_GL_NORMALIZED_TEXRECT
},
7609 {STATE_SAMPLER(2), {STATE_SAMPLER(2), sampler_texmatrix
}, WINED3D_GL_EXT_NONE
},
7610 {STATE_SAMPLER(3), {0, NULL
}, ARB_TEXTURE_NON_POWER_OF_TWO
},
7611 {STATE_SAMPLER(3), {0, NULL
}, WINED3D_GL_NORMALIZED_TEXRECT
},
7612 {STATE_SAMPLER(3), {STATE_SAMPLER(3), sampler_texmatrix
}, WINED3D_GL_EXT_NONE
},
7613 {STATE_SAMPLER(4), {0, NULL
}, ARB_TEXTURE_NON_POWER_OF_TWO
},
7614 {STATE_SAMPLER(4), {0, NULL
}, WINED3D_GL_NORMALIZED_TEXRECT
},
7615 {STATE_SAMPLER(4), {STATE_SAMPLER(4), sampler_texmatrix
}, WINED3D_GL_EXT_NONE
},
7616 {STATE_SAMPLER(5), {0, NULL
}, ARB_TEXTURE_NON_POWER_OF_TWO
},
7617 {STATE_SAMPLER(5), {0, NULL
}, WINED3D_GL_NORMALIZED_TEXRECT
},
7618 {STATE_SAMPLER(5), {STATE_SAMPLER(5), sampler_texmatrix
}, WINED3D_GL_EXT_NONE
},
7619 {STATE_SAMPLER(6), {0, NULL
}, ARB_TEXTURE_NON_POWER_OF_TWO
},
7620 {STATE_SAMPLER(6), {0, NULL
}, WINED3D_GL_NORMALIZED_TEXRECT
},
7621 {STATE_SAMPLER(6), {STATE_SAMPLER(6), sampler_texmatrix
}, WINED3D_GL_EXT_NONE
},
7622 {STATE_SAMPLER(7), {0, NULL
}, ARB_TEXTURE_NON_POWER_OF_TWO
},
7623 {STATE_SAMPLER(7), {0, NULL
}, WINED3D_GL_NORMALIZED_TEXRECT
},
7624 {STATE_SAMPLER(7), {STATE_SAMPLER(7), sampler_texmatrix
}, WINED3D_GL_EXT_NONE
},
7625 {STATE_POINT_SIZE_ENABLE
, {STATE_RENDER(WINED3D_RS_FOGENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
7626 {0 /* Terminate */, {0, NULL
}, WINED3D_GL_EXT_NONE
},
7630 * - This currently depends on GL fixed function functions to set things
7631 * like light parameters. Ideally we'd use regular uniforms for that.
7632 * - In part because of the previous point, much of this is modelled after
7633 * GL fixed function, and has much of the same limitations. For example,
7634 * D3D spot lights are slightly different from GL spot lights.
7635 * - We can now implement drawing transformed vertices using the GLSL pipe,
7636 * instead of using the immediate mode fallback.
7637 * - Similarly, we don't need the fallback for certain combinations of
7638 * material sources anymore.
7639 * - Implement vertex blending and vertex tweening.
7640 * - Handle WINED3D_TSS_TEXCOORD_INDEX in the shader, instead of duplicating
7641 * attribute arrays in load_tex_coords().
7642 * - Per-vertex point sizes. */
7643 const struct wined3d_vertex_pipe_ops glsl_vertex_pipe
=
7645 glsl_vertex_pipe_vp_enable
,
7646 glsl_vertex_pipe_vp_get_caps
,
7647 glsl_vertex_pipe_vp_alloc
,
7648 glsl_vertex_pipe_vp_free
,
7649 glsl_vertex_pipe_vp_states
,
7652 static void glsl_fragment_pipe_enable(const struct wined3d_gl_info
*gl_info
, BOOL enable
)
7654 /* Nothing to do. */
7657 static void glsl_fragment_pipe_get_caps(const struct wined3d_gl_info
*gl_info
, struct fragment_caps
*caps
)
7659 caps
->wined3d_caps
= WINED3D_FRAGMENT_CAP_PROJ_CONTROL
7660 | WINED3D_FRAGMENT_CAP_SRGB_WRITE
7661 | WINED3D_FRAGMENT_CAP_COLOR_KEY
;
7662 caps
->PrimitiveMiscCaps
= WINED3DPMISCCAPS_TSSARGTEMP
7663 | WINED3DPMISCCAPS_PERSTAGECONSTANT
;
7664 caps
->TextureOpCaps
= WINED3DTEXOPCAPS_DISABLE
7665 | WINED3DTEXOPCAPS_SELECTARG1
7666 | WINED3DTEXOPCAPS_SELECTARG2
7667 | WINED3DTEXOPCAPS_MODULATE4X
7668 | WINED3DTEXOPCAPS_MODULATE2X
7669 | WINED3DTEXOPCAPS_MODULATE
7670 | WINED3DTEXOPCAPS_ADDSIGNED2X
7671 | WINED3DTEXOPCAPS_ADDSIGNED
7672 | WINED3DTEXOPCAPS_ADD
7673 | WINED3DTEXOPCAPS_SUBTRACT
7674 | WINED3DTEXOPCAPS_ADDSMOOTH
7675 | WINED3DTEXOPCAPS_BLENDCURRENTALPHA
7676 | WINED3DTEXOPCAPS_BLENDFACTORALPHA
7677 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHA
7678 | WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA
7679 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM
7680 | WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR
7681 | WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA
7682 | WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA
7683 | WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR
7684 | WINED3DTEXOPCAPS_DOTPRODUCT3
7685 | WINED3DTEXOPCAPS_MULTIPLYADD
7686 | WINED3DTEXOPCAPS_LERP
7687 | WINED3DTEXOPCAPS_BUMPENVMAP
7688 | WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE
;
7689 caps
->MaxTextureBlendStages
= 8;
7690 caps
->MaxSimultaneousTextures
= min(gl_info
->limits
.fragment_samplers
, 8);
7693 static void *glsl_fragment_pipe_alloc(const struct wined3d_shader_backend_ops
*shader_backend
, void *shader_priv
)
7695 struct shader_glsl_priv
*priv
;
7697 if (shader_backend
== &glsl_shader_backend
)
7701 if (wine_rb_init(&priv
->ffp_fragment_shaders
, &wined3d_ffp_frag_program_rb_functions
) == -1)
7703 ERR("Failed to initialize rbtree.\n");
7710 FIXME("GLSL fragment pipe without GLSL shader backend not implemented.\n");
7715 static void shader_glsl_free_ffp_fragment_shader(struct wine_rb_entry
*entry
, void *context
)
7717 struct glsl_ffp_fragment_shader
*shader
= WINE_RB_ENTRY_VALUE(entry
,
7718 struct glsl_ffp_fragment_shader
, entry
.entry
);
7719 struct glsl_shader_prog_link
*program
, *program2
;
7720 struct glsl_ffp_destroy_ctx
*ctx
= context
;
7722 LIST_FOR_EACH_ENTRY_SAFE(program
, program2
, &shader
->linked_programs
,
7723 struct glsl_shader_prog_link
, ps
.shader_entry
)
7725 delete_glsl_program_entry(ctx
->priv
, ctx
->gl_info
, program
);
7727 ctx
->gl_info
->gl_ops
.ext
.p_glDeleteShader(shader
->id
);
7728 HeapFree(GetProcessHeap(), 0, shader
);
7731 /* Context activation is done by the caller. */
7732 static void glsl_fragment_pipe_free(struct wined3d_device
*device
)
7734 struct shader_glsl_priv
*priv
= device
->fragment_priv
;
7735 struct glsl_ffp_destroy_ctx ctx
;
7738 ctx
.gl_info
= &device
->adapter
->gl_info
;
7739 wine_rb_destroy(&priv
->ffp_fragment_shaders
, shader_glsl_free_ffp_fragment_shader
, &ctx
);
7742 static void glsl_fragment_pipe_shader(struct wined3d_context
*context
,
7743 const struct wined3d_state
*state
, DWORD state_id
)
7745 context
->last_was_pshader
= use_ps(state
);
7747 context
->shader_update_mask
|= 1 << WINED3D_SHADER_TYPE_PIXEL
;
7750 static void glsl_fragment_pipe_fog(struct wined3d_context
*context
,
7751 const struct wined3d_state
*state
, DWORD state_id
)
7753 BOOL use_vshader
= use_vs(state
);
7754 enum fogsource new_source
;
7755 DWORD fogstart
= state
->render_states
[WINED3D_RS_FOGSTART
];
7756 DWORD fogend
= state
->render_states
[WINED3D_RS_FOGEND
];
7758 context
->shader_update_mask
|= 1 << WINED3D_SHADER_TYPE_PIXEL
;
7760 if (!state
->render_states
[WINED3D_RS_FOGENABLE
])
7763 if (state
->render_states
[WINED3D_RS_FOGTABLEMODE
] == WINED3D_FOG_NONE
)
7766 new_source
= FOGSOURCE_VS
;
7767 else if (state
->render_states
[WINED3D_RS_FOGVERTEXMODE
] == WINED3D_FOG_NONE
|| context
->stream_info
.position_transformed
)
7768 new_source
= FOGSOURCE_COORD
;
7770 new_source
= FOGSOURCE_FFP
;
7774 new_source
= FOGSOURCE_FFP
;
7777 if (new_source
!= context
->fog_source
|| fogstart
== fogend
)
7779 context
->fog_source
= new_source
;
7780 state_fogstartend(context
, state
, STATE_RENDER(WINED3D_RS_FOGSTART
));
7784 static void glsl_fragment_pipe_vdecl(struct wined3d_context
*context
,
7785 const struct wined3d_state
*state
, DWORD state_id
)
7787 if (!isStateDirty(context
, STATE_RENDER(WINED3D_RS_FOGENABLE
)))
7788 glsl_fragment_pipe_fog(context
, state
, state_id
);
7791 static void glsl_fragment_pipe_tex_transform(struct wined3d_context
*context
,
7792 const struct wined3d_state
*state
, DWORD state_id
)
7794 context
->shader_update_mask
|= 1 << WINED3D_SHADER_TYPE_PIXEL
;
7797 static void glsl_fragment_pipe_invalidate_constants(struct wined3d_context
*context
,
7798 const struct wined3d_state
*state
, DWORD state_id
)
7800 context
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_PS
;
7803 static void glsl_fragment_pipe_alpha_test(struct wined3d_context
*context
,
7804 const struct wined3d_state
*state
, DWORD state_id
)
7806 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
7810 TRACE("context %p, state %p, state_id %#x.\n", context
, state
, state_id
);
7812 if (state
->render_states
[WINED3D_RS_ALPHATESTENABLE
])
7814 gl_info
->gl_ops
.gl
.p_glEnable(GL_ALPHA_TEST
);
7815 checkGLcall("glEnable GL_ALPHA_TEST");
7819 gl_info
->gl_ops
.gl
.p_glDisable(GL_ALPHA_TEST
);
7820 checkGLcall("glDisable GL_ALPHA_TEST");
7824 ref
= ((float)state
->render_states
[WINED3D_RS_ALPHAREF
]) / 255.0f
;
7825 glParm
= wined3d_gl_compare_func(state
->render_states
[WINED3D_RS_ALPHAFUNC
]);
7829 gl_info
->gl_ops
.gl
.p_glAlphaFunc(glParm
, ref
);
7830 checkGLcall("glAlphaFunc");
7834 static void glsl_fragment_pipe_color_key(struct wined3d_context
*context
,
7835 const struct wined3d_state
*state
, DWORD state_id
)
7837 context
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_COLOR_KEY
;
7840 static const struct StateEntryTemplate glsl_fragment_pipe_state_template
[] =
7842 {STATE_VDECL
, {STATE_VDECL
, glsl_fragment_pipe_vdecl
}, WINED3D_GL_EXT_NONE
},
7843 {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR
), {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR
), glsl_fragment_pipe_invalidate_constants
}, WINED3D_GL_EXT_NONE
},
7844 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
7845 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
7846 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
7847 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
7848 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
7849 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
7850 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
7851 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
7852 {STATE_TEXTURESTAGE(0, WINED3D_TSS_RESULT_ARG
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
7853 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
7854 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
7855 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
7856 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
7857 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
7858 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
7859 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
7860 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
7861 {STATE_TEXTURESTAGE(1, WINED3D_TSS_RESULT_ARG
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
7862 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
7863 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
7864 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
7865 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
7866 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
7867 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
7868 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
7869 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
7870 {STATE_TEXTURESTAGE(2, WINED3D_TSS_RESULT_ARG
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
7871 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
7872 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
7873 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
7874 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
7875 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
7876 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
7877 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
7878 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
7879 {STATE_TEXTURESTAGE(3, WINED3D_TSS_RESULT_ARG
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
7880 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
7881 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
7882 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
7883 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
7884 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
7885 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
7886 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
7887 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
7888 {STATE_TEXTURESTAGE(4, WINED3D_TSS_RESULT_ARG
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
7889 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
7890 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
7891 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
7892 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
7893 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
7894 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
7895 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
7896 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
7897 {STATE_TEXTURESTAGE(5, WINED3D_TSS_RESULT_ARG
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
7898 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
7899 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
7900 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
7901 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
7902 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
7903 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
7904 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
7905 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
7906 {STATE_TEXTURESTAGE(6, WINED3D_TSS_RESULT_ARG
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
7907 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
7908 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
7909 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
7910 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
7911 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
7912 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
7913 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
7914 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
7915 {STATE_TEXTURESTAGE(7, WINED3D_TSS_RESULT_ARG
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
7916 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), glsl_fragment_pipe_shader
}, WINED3D_GL_EXT_NONE
},
7917 {STATE_RENDER(WINED3D_RS_ALPHAFUNC
), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
7918 {STATE_RENDER(WINED3D_RS_ALPHAREF
), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
7919 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE
), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE
), glsl_fragment_pipe_alpha_test
}, WINED3D_GL_EXT_NONE
},
7920 {STATE_RENDER(WINED3D_RS_COLORKEYENABLE
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
7921 {STATE_COLOR_KEY
, { STATE_COLOR_KEY
, glsl_fragment_pipe_color_key
}, WINED3D_GL_EXT_NONE
},
7922 {STATE_RENDER(WINED3D_RS_FOGENABLE
), {STATE_RENDER(WINED3D_RS_FOGENABLE
), glsl_fragment_pipe_fog
}, WINED3D_GL_EXT_NONE
},
7923 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE
), {STATE_RENDER(WINED3D_RS_FOGENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
7924 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE
), {STATE_RENDER(WINED3D_RS_FOGENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
7925 {STATE_RENDER(WINED3D_RS_FOGSTART
), {STATE_RENDER(WINED3D_RS_FOGSTART
), state_fogstartend
}, WINED3D_GL_EXT_NONE
},
7926 {STATE_RENDER(WINED3D_RS_FOGEND
), {STATE_RENDER(WINED3D_RS_FOGSTART
), NULL
}, WINED3D_GL_EXT_NONE
},
7927 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE
), {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE
), state_srgbwrite
}, ARB_FRAMEBUFFER_SRGB
},
7928 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
7929 {STATE_RENDER(WINED3D_RS_FOGCOLOR
), {STATE_RENDER(WINED3D_RS_FOGCOLOR
), state_fogcolor
}, WINED3D_GL_EXT_NONE
},
7930 {STATE_RENDER(WINED3D_RS_FOGDENSITY
), {STATE_RENDER(WINED3D_RS_FOGDENSITY
), state_fogdensity
}, WINED3D_GL_EXT_NONE
},
7931 {STATE_TEXTURESTAGE(0,WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), glsl_fragment_pipe_tex_transform
}, WINED3D_GL_EXT_NONE
},
7932 {STATE_TEXTURESTAGE(1,WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), glsl_fragment_pipe_tex_transform
}, WINED3D_GL_EXT_NONE
},
7933 {STATE_TEXTURESTAGE(2,WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), glsl_fragment_pipe_tex_transform
}, WINED3D_GL_EXT_NONE
},
7934 {STATE_TEXTURESTAGE(3,WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), glsl_fragment_pipe_tex_transform
}, WINED3D_GL_EXT_NONE
},
7935 {STATE_TEXTURESTAGE(4,WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), glsl_fragment_pipe_tex_transform
}, WINED3D_GL_EXT_NONE
},
7936 {STATE_TEXTURESTAGE(5,WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), glsl_fragment_pipe_tex_transform
}, WINED3D_GL_EXT_NONE
},
7937 {STATE_TEXTURESTAGE(6,WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), glsl_fragment_pipe_tex_transform
}, WINED3D_GL_EXT_NONE
},
7938 {STATE_TEXTURESTAGE(7,WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), glsl_fragment_pipe_tex_transform
}, WINED3D_GL_EXT_NONE
},
7939 {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT
), {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT
), glsl_fragment_pipe_invalidate_constants
}, WINED3D_GL_EXT_NONE
},
7940 {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT
), {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT
), glsl_fragment_pipe_invalidate_constants
}, WINED3D_GL_EXT_NONE
},
7941 {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT
), {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT
), glsl_fragment_pipe_invalidate_constants
}, WINED3D_GL_EXT_NONE
},
7942 {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT
), {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT
), glsl_fragment_pipe_invalidate_constants
}, WINED3D_GL_EXT_NONE
},
7943 {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT
), {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT
), glsl_fragment_pipe_invalidate_constants
}, WINED3D_GL_EXT_NONE
},
7944 {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT
), {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT
), glsl_fragment_pipe_invalidate_constants
}, WINED3D_GL_EXT_NONE
},
7945 {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT
), {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT
), glsl_fragment_pipe_invalidate_constants
}, WINED3D_GL_EXT_NONE
},
7946 {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT
), {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT
), glsl_fragment_pipe_invalidate_constants
}, WINED3D_GL_EXT_NONE
},
7947 {STATE_RENDER(WINED3D_RS_SPECULARENABLE
), {STATE_RENDER(WINED3D_RS_SPECULARENABLE
), glsl_fragment_pipe_invalidate_constants
}, WINED3D_GL_EXT_NONE
},
7948 {0 /* Terminate */, {0, 0 }, WINED3D_GL_EXT_NONE
},
7951 static BOOL
glsl_fragment_pipe_alloc_context_data(struct wined3d_context
*context
)
7956 static void glsl_fragment_pipe_free_context_data(struct wined3d_context
*context
)
7960 const struct fragment_pipeline glsl_fragment_pipe
=
7962 glsl_fragment_pipe_enable
,
7963 glsl_fragment_pipe_get_caps
,
7964 glsl_fragment_pipe_alloc
,
7965 glsl_fragment_pipe_free
,
7966 glsl_fragment_pipe_alloc_context_data
,
7967 glsl_fragment_pipe_free_context_data
,
7968 shader_glsl_color_fixup_supported
,
7969 glsl_fragment_pipe_state_template
,