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 0x01
48 #define WINED3D_GLSL_SAMPLE_NPOT 0x02
49 #define WINED3D_GLSL_SAMPLE_LOD 0x04
50 #define WINED3D_GLSL_SAMPLE_GRAD 0x08
51 #define WINED3D_GLSL_SAMPLE_LOAD 0x10
65 struct glsl_sample_function
67 struct wined3d_string_buffer
*name
;
69 enum wined3d_data_type data_type
;
70 BOOL output_single_component
;
75 HEAP_NODE_TRAVERSE_LEFT
,
76 HEAP_NODE_TRAVERSE_RIGHT
,
88 struct constant_entry
*entries
;
90 unsigned int *positions
;
94 /* GLSL shader private data */
95 struct shader_glsl_priv
{
96 struct wined3d_string_buffer shader_buffer
;
97 struct wined3d_string_buffer_list string_buffers
;
98 struct wine_rb_tree program_lookup
;
99 struct constant_heap vconst_heap
;
100 struct constant_heap pconst_heap
;
101 unsigned char *stack
;
102 GLuint depth_blt_program_full
[WINED3D_GL_RES_TYPE_COUNT
];
103 GLuint depth_blt_program_masked
[WINED3D_GL_RES_TYPE_COUNT
];
104 UINT next_constant_version
;
106 const struct wined3d_vertex_pipe_ops
*vertex_pipe
;
107 const struct fragment_pipeline
*fragment_pipe
;
108 struct wine_rb_tree ffp_vertex_shaders
;
109 struct wine_rb_tree ffp_fragment_shaders
;
110 BOOL ffp_proj_control
;
111 BOOL legacy_lighting
;
114 struct glsl_vs_program
116 struct list shader_entry
;
118 GLenum vertex_color_clamp
;
119 GLint
*uniform_f_locations
;
120 GLint uniform_i_locations
[MAX_CONST_I
];
121 GLint uniform_b_locations
[MAX_CONST_B
];
122 GLint pos_fixup_location
;
124 GLint modelview_matrix_location
[MAX_VERTEX_BLENDS
];
125 GLint projection_matrix_location
;
126 GLint normal_matrix_location
;
127 GLint texture_matrix_location
[MAX_TEXTURES
];
128 GLint material_ambient_location
;
129 GLint material_diffuse_location
;
130 GLint material_specular_location
;
131 GLint material_emissive_location
;
132 GLint material_shininess_location
;
133 GLint light_ambient_location
;
148 } light_location
[MAX_ACTIVE_LIGHTS
];
149 GLint pointsize_location
;
150 GLint pointsize_min_location
;
151 GLint pointsize_max_location
;
152 GLint pointsize_c_att_location
;
153 GLint pointsize_l_att_location
;
154 GLint pointsize_q_att_location
;
157 struct glsl_gs_program
159 struct list shader_entry
;
163 struct glsl_ps_program
165 struct list shader_entry
;
167 GLint
*uniform_f_locations
;
168 GLint uniform_i_locations
[MAX_CONST_I
];
169 GLint uniform_b_locations
[MAX_CONST_B
];
170 GLint bumpenv_mat_location
[MAX_TEXTURES
];
171 GLint bumpenv_lum_scale_location
[MAX_TEXTURES
];
172 GLint bumpenv_lum_offset_location
[MAX_TEXTURES
];
173 GLint tss_constant_location
[MAX_TEXTURES
];
174 GLint tex_factor_location
;
175 GLint specular_enable_location
;
176 GLint fog_color_location
;
177 GLint fog_density_location
;
178 GLint fog_end_location
;
179 GLint fog_scale_location
;
180 GLint ycorrection_location
;
181 GLint np2_fixup_location
;
182 GLint color_key_location
;
183 const struct ps_np2fixup_info
*np2_fixup_info
;
186 /* Struct to maintain data about a linked GLSL program */
187 struct glsl_shader_prog_link
189 struct wine_rb_entry program_lookup_entry
;
190 struct glsl_vs_program vs
;
191 struct glsl_gs_program gs
;
192 struct glsl_ps_program ps
;
194 DWORD constant_update_mask
;
195 UINT constant_version
;
198 struct glsl_program_key
205 struct shader_glsl_ctx_priv
{
206 const struct vs_compile_args
*cur_vs_args
;
207 const struct ps_compile_args
*cur_ps_args
;
208 struct ps_np2fixup_info
*cur_np2fixup_info
;
209 struct wined3d_string_buffer_list
*string_buffers
;
212 struct glsl_context_data
214 struct glsl_shader_prog_link
*glsl_program
;
217 struct glsl_ps_compiled_shader
219 struct ps_compile_args args
;
220 struct ps_np2fixup_info np2fixup
;
224 struct glsl_vs_compiled_shader
226 struct vs_compile_args args
;
230 struct glsl_gs_compiled_shader
235 struct glsl_shader_private
239 struct glsl_vs_compiled_shader
*vs
;
240 struct glsl_gs_compiled_shader
*gs
;
241 struct glsl_ps_compiled_shader
*ps
;
243 UINT num_gl_shaders
, shader_array_size
;
246 struct glsl_ffp_vertex_shader
248 struct wined3d_ffp_vs_desc desc
;
250 struct list linked_programs
;
253 struct glsl_ffp_fragment_shader
255 struct ffp_frag_desc entry
;
257 struct list linked_programs
;
260 struct glsl_ffp_destroy_ctx
262 struct shader_glsl_priv
*priv
;
263 const struct wined3d_gl_info
*gl_info
;
266 static const char *debug_gl_shader_type(GLenum type
)
270 #define WINED3D_TO_STR(u) case u: return #u
271 WINED3D_TO_STR(GL_VERTEX_SHADER
);
272 WINED3D_TO_STR(GL_GEOMETRY_SHADER
);
273 WINED3D_TO_STR(GL_FRAGMENT_SHADER
);
274 #undef WINED3D_TO_STR
276 return wine_dbg_sprintf("UNKNOWN(%#x)", type
);
280 static const char *shader_glsl_get_prefix(enum wined3d_shader_type type
)
284 case WINED3D_SHADER_TYPE_VERTEX
:
287 case WINED3D_SHADER_TYPE_GEOMETRY
:
290 case WINED3D_SHADER_TYPE_PIXEL
:
294 FIXME("Unhandled shader type %#x.\n", type
);
299 static const char *shader_glsl_get_version(const struct wined3d_gl_info
*gl_info
,
300 const struct wined3d_shader_version
*version
)
302 if (!gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
])
303 return "#version 150";
304 else if (gl_info
->glsl_version
>= MAKEDWORD_VERSION(1, 30) && version
&& version
->major
>= 4)
305 return "#version 130";
307 return "#version 120";
310 static void shader_glsl_append_imm_vec4(struct wined3d_string_buffer
*buffer
, const float *values
)
314 wined3d_ftoa(values
[0], str
[0]);
315 wined3d_ftoa(values
[1], str
[1]);
316 wined3d_ftoa(values
[2], str
[2]);
317 wined3d_ftoa(values
[3], str
[3]);
318 shader_addline(buffer
, "vec4(%s, %s, %s, %s)", str
[0], str
[1], str
[2], str
[3]);
321 static const char *get_info_log_line(const char **ptr
)
326 if (!(q
= strstr(p
, "\n")))
328 if (!*p
) return NULL
;
337 /* Context activation is done by the caller. */
338 void print_glsl_info_log(const struct wined3d_gl_info
*gl_info
, GLuint id
, BOOL program
)
343 if (!WARN_ON(d3d_shader
) && !FIXME_ON(d3d_shader
))
347 GL_EXTCALL(glGetProgramiv(id
, GL_INFO_LOG_LENGTH
, &length
));
349 GL_EXTCALL(glGetShaderiv(id
, GL_INFO_LOG_LENGTH
, &length
));
351 /* A size of 1 is just a null-terminated string, so the log should be bigger than
352 * that if there are errors. */
355 const char *ptr
, *line
;
357 log
= HeapAlloc(GetProcessHeap(), 0, length
);
358 /* The info log is supposed to be zero-terminated, but at least some
359 * versions of fglrx don't terminate the string properly. The reported
360 * length does include the terminator, so explicitly set it to zero
364 GL_EXTCALL(glGetProgramInfoLog(id
, length
, NULL
, log
));
366 GL_EXTCALL(glGetShaderInfoLog(id
, length
, NULL
, log
));
369 if (gl_info
->quirks
& WINED3D_QUIRK_INFO_LOG_SPAM
)
371 WARN("Info log received from GLSL shader #%u:\n", id
);
372 while ((line
= get_info_log_line(&ptr
))) WARN(" %.*s", (int)(ptr
- line
), line
);
376 FIXME("Info log received from GLSL shader #%u:\n", id
);
377 while ((line
= get_info_log_line(&ptr
))) FIXME(" %.*s", (int)(ptr
- line
), line
);
379 HeapFree(GetProcessHeap(), 0, log
);
383 /* Context activation is done by the caller. */
384 static void shader_glsl_compile(const struct wined3d_gl_info
*gl_info
, GLuint shader
, const char *src
)
386 const char *ptr
, *line
;
388 TRACE("Compiling shader object %u.\n", shader
);
390 if (TRACE_ON(d3d_shader
))
393 while ((line
= get_info_log_line(&ptr
))) TRACE_(d3d_shader
)(" %.*s", (int)(ptr
- line
), line
);
396 GL_EXTCALL(glShaderSource(shader
, 1, &src
, NULL
));
397 checkGLcall("glShaderSource");
398 GL_EXTCALL(glCompileShader(shader
));
399 checkGLcall("glCompileShader");
400 print_glsl_info_log(gl_info
, shader
, FALSE
);
403 /* Context activation is done by the caller. */
404 static void shader_glsl_dump_program_source(const struct wined3d_gl_info
*gl_info
, GLuint program
)
406 GLint i
, shader_count
, source_size
= -1;
410 GL_EXTCALL(glGetProgramiv(program
, GL_ATTACHED_SHADERS
, &shader_count
));
411 shaders
= HeapAlloc(GetProcessHeap(), 0, shader_count
* sizeof(*shaders
));
414 ERR("Failed to allocate shader array memory.\n");
418 GL_EXTCALL(glGetAttachedShaders(program
, shader_count
, NULL
, shaders
));
419 for (i
= 0; i
< shader_count
; ++i
)
421 const char *ptr
, *line
;
424 GL_EXTCALL(glGetShaderiv(shaders
[i
], GL_SHADER_SOURCE_LENGTH
, &tmp
));
426 if (source_size
< tmp
)
428 HeapFree(GetProcessHeap(), 0, source
);
430 source
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, tmp
);
433 ERR("Failed to allocate %d bytes for shader source.\n", tmp
);
434 HeapFree(GetProcessHeap(), 0, shaders
);
440 FIXME("Shader %u:\n", shaders
[i
]);
441 GL_EXTCALL(glGetShaderiv(shaders
[i
], GL_SHADER_TYPE
, &tmp
));
442 FIXME(" GL_SHADER_TYPE: %s.\n", debug_gl_shader_type(tmp
));
443 GL_EXTCALL(glGetShaderiv(shaders
[i
], GL_COMPILE_STATUS
, &tmp
));
444 FIXME(" GL_COMPILE_STATUS: %d.\n", tmp
);
448 GL_EXTCALL(glGetShaderSource(shaders
[i
], source_size
, NULL
, source
));
449 while ((line
= get_info_log_line(&ptr
))) FIXME(" %.*s", (int)(ptr
- line
), line
);
453 HeapFree(GetProcessHeap(), 0, source
);
454 HeapFree(GetProcessHeap(), 0, shaders
);
457 /* Context activation is done by the caller. */
458 void shader_glsl_validate_link(const struct wined3d_gl_info
*gl_info
, GLuint program
)
462 if (!TRACE_ON(d3d_shader
) && !FIXME_ON(d3d_shader
))
465 GL_EXTCALL(glGetProgramiv(program
, GL_LINK_STATUS
, &tmp
));
468 FIXME("Program %u link status invalid.\n", program
);
469 shader_glsl_dump_program_source(gl_info
, program
);
472 print_glsl_info_log(gl_info
, program
, TRUE
);
475 /* Context activation is done by the caller. */
476 static void shader_glsl_load_samplers(const struct wined3d_gl_info
*gl_info
,
477 struct shader_glsl_priv
*priv
, const DWORD
*tex_unit_map
, GLuint program_id
)
479 unsigned int mapped_unit
;
480 struct wined3d_string_buffer
*sampler_name
= string_buffer_get(&priv
->string_buffers
);
487 enum wined3d_shader_type type
;
488 unsigned int base_idx
;
493 {WINED3D_SHADER_TYPE_PIXEL
, 0, MAX_FRAGMENT_SAMPLERS
},
494 {WINED3D_SHADER_TYPE_VERTEX
, MAX_FRAGMENT_SAMPLERS
, MAX_VERTEX_SAMPLERS
},
497 for (i
= 0; i
< ARRAY_SIZE(sampler_info
); ++i
)
499 prefix
= shader_glsl_get_prefix(sampler_info
[i
].type
);
501 for (j
= 0; j
< sampler_info
[i
].count
; ++j
)
503 string_buffer_sprintf(sampler_name
, "%s_sampler%u", prefix
, j
);
504 name_loc
= GL_EXTCALL(glGetUniformLocation(program_id
, sampler_name
->buffer
));
508 mapped_unit
= tex_unit_map
[sampler_info
[i
].base_idx
+ j
];
509 if (mapped_unit
== WINED3D_UNMAPPED_STAGE
|| mapped_unit
>= gl_info
->limits
.combined_samplers
)
511 ERR("Trying to load sampler %s on unsupported unit %u.\n", sampler_name
->buffer
, mapped_unit
);
515 TRACE("Loading sampler %s on unit %u.\n", sampler_name
->buffer
, mapped_unit
);
516 GL_EXTCALL(glUniform1i(name_loc
, mapped_unit
));
519 checkGLcall("glUniform1i");
520 string_buffer_release(&priv
->string_buffers
, sampler_name
);
523 /* Context activation is done by the caller. */
524 static inline void walk_constant_heap(const struct wined3d_gl_info
*gl_info
, const float *constants
,
525 const GLint
*constant_locations
, const struct constant_heap
*heap
, unsigned char *stack
, DWORD version
)
527 unsigned int start
= ~0U, end
= 0;
529 unsigned int heap_idx
= 1;
532 if (heap
->entries
[heap_idx
].version
<= version
) return;
534 idx
= heap
->entries
[heap_idx
].idx
;
535 if (constant_locations
[idx
] != -1)
537 stack
[stack_idx
] = HEAP_NODE_TRAVERSE_LEFT
;
539 while (stack_idx
>= 0)
541 /* Note that we fall through to the next case statement. */
542 switch(stack
[stack_idx
])
544 case HEAP_NODE_TRAVERSE_LEFT
:
546 unsigned int left_idx
= heap_idx
<< 1;
547 if (left_idx
< heap
->size
&& heap
->entries
[left_idx
].version
> version
)
550 idx
= heap
->entries
[heap_idx
].idx
;
551 if (constant_locations
[idx
] != -1)
559 stack
[stack_idx
++] = HEAP_NODE_TRAVERSE_RIGHT
;
560 stack
[stack_idx
] = HEAP_NODE_TRAVERSE_LEFT
;
565 case HEAP_NODE_TRAVERSE_RIGHT
:
567 unsigned int right_idx
= (heap_idx
<< 1) + 1;
568 if (right_idx
< heap
->size
&& heap
->entries
[right_idx
].version
> version
)
570 heap_idx
= right_idx
;
571 idx
= heap
->entries
[heap_idx
].idx
;
572 if (constant_locations
[idx
] != -1)
580 stack
[stack_idx
++] = HEAP_NODE_POP
;
581 stack
[stack_idx
] = HEAP_NODE_TRAVERSE_LEFT
;
593 GL_EXTCALL(glUniform4fv(constant_locations
[start
], end
- start
+ 1, &constants
[start
* 4]));
594 checkGLcall("walk_constant_heap()");
597 /* Context activation is done by the caller. */
598 static inline void apply_clamped_constant(const struct wined3d_gl_info
*gl_info
, GLint location
, const GLfloat
*data
)
600 GLfloat clamped_constant
[4];
602 if (location
== -1) return;
604 clamped_constant
[0] = data
[0] < -1.0f
? -1.0f
: data
[0] > 1.0f
? 1.0f
: data
[0];
605 clamped_constant
[1] = data
[1] < -1.0f
? -1.0f
: data
[1] > 1.0f
? 1.0f
: data
[1];
606 clamped_constant
[2] = data
[2] < -1.0f
? -1.0f
: data
[2] > 1.0f
? 1.0f
: data
[2];
607 clamped_constant
[3] = data
[3] < -1.0f
? -1.0f
: data
[3] > 1.0f
? 1.0f
: data
[3];
609 GL_EXTCALL(glUniform4fv(location
, 1, clamped_constant
));
612 /* Context activation is done by the caller. */
613 static inline void walk_constant_heap_clamped(const struct wined3d_gl_info
*gl_info
, const float *constants
,
614 const GLint
*constant_locations
, const struct constant_heap
*heap
, unsigned char *stack
, DWORD version
)
617 unsigned int heap_idx
= 1;
620 if (heap
->entries
[heap_idx
].version
<= version
) return;
622 idx
= heap
->entries
[heap_idx
].idx
;
623 apply_clamped_constant(gl_info
, constant_locations
[idx
], &constants
[idx
* 4]);
624 stack
[stack_idx
] = HEAP_NODE_TRAVERSE_LEFT
;
626 while (stack_idx
>= 0)
628 /* Note that we fall through to the next case statement. */
629 switch(stack
[stack_idx
])
631 case HEAP_NODE_TRAVERSE_LEFT
:
633 unsigned int left_idx
= heap_idx
<< 1;
634 if (left_idx
< heap
->size
&& heap
->entries
[left_idx
].version
> version
)
637 idx
= heap
->entries
[heap_idx
].idx
;
638 apply_clamped_constant(gl_info
, constant_locations
[idx
], &constants
[idx
* 4]);
640 stack
[stack_idx
++] = HEAP_NODE_TRAVERSE_RIGHT
;
641 stack
[stack_idx
] = HEAP_NODE_TRAVERSE_LEFT
;
646 case HEAP_NODE_TRAVERSE_RIGHT
:
648 unsigned int right_idx
= (heap_idx
<< 1) + 1;
649 if (right_idx
< heap
->size
&& heap
->entries
[right_idx
].version
> version
)
651 heap_idx
= right_idx
;
652 idx
= heap
->entries
[heap_idx
].idx
;
653 apply_clamped_constant(gl_info
, constant_locations
[idx
], &constants
[idx
* 4]);
655 stack
[stack_idx
++] = HEAP_NODE_POP
;
656 stack
[stack_idx
] = HEAP_NODE_TRAVERSE_LEFT
;
667 checkGLcall("walk_constant_heap_clamped()");
670 /* Context activation is done by the caller. */
671 static void shader_glsl_load_constantsF(const struct wined3d_shader
*shader
, const struct wined3d_gl_info
*gl_info
,
672 const float *constants
, const GLint
*constant_locations
, const struct constant_heap
*heap
,
673 unsigned char *stack
, UINT version
)
675 const struct wined3d_shader_lconst
*lconst
;
677 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
678 if (shader
->reg_maps
.shader_version
.major
== 1
679 && shader
->reg_maps
.shader_version
.type
== WINED3D_SHADER_TYPE_PIXEL
)
680 walk_constant_heap_clamped(gl_info
, constants
, constant_locations
, heap
, stack
, version
);
682 walk_constant_heap(gl_info
, constants
, constant_locations
, heap
, stack
, version
);
684 if (!shader
->load_local_constsF
)
686 TRACE("No need to load local float constants for this shader\n");
690 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
691 LIST_FOR_EACH_ENTRY(lconst
, &shader
->constantsF
, struct wined3d_shader_lconst
, entry
)
693 GL_EXTCALL(glUniform4fv(constant_locations
[lconst
->idx
], 1, (const GLfloat
*)lconst
->value
));
695 checkGLcall("glUniform4fv()");
698 /* Context activation is done by the caller. */
699 static void shader_glsl_load_constantsI(const struct wined3d_shader
*shader
, const struct wined3d_gl_info
*gl_info
,
700 const GLint locations
[MAX_CONST_I
], const int *constants
, WORD constants_set
)
705 for (i
= 0; constants_set
; constants_set
>>= 1, ++i
)
707 if (!(constants_set
& 1)) continue;
709 /* We found this uniform name in the program - go ahead and send the data */
710 GL_EXTCALL(glUniform4iv(locations
[i
], 1, &constants
[i
* 4]));
713 /* Load immediate constants */
714 ptr
= list_head(&shader
->constantsI
);
717 const struct wined3d_shader_lconst
*lconst
= LIST_ENTRY(ptr
, const struct wined3d_shader_lconst
, entry
);
718 unsigned int idx
= lconst
->idx
;
719 const GLint
*values
= (const GLint
*)lconst
->value
;
721 /* We found this uniform name in the program - go ahead and send the data */
722 GL_EXTCALL(glUniform4iv(locations
[idx
], 1, values
));
723 ptr
= list_next(&shader
->constantsI
, ptr
);
725 checkGLcall("glUniform4iv()");
728 /* Context activation is done by the caller. */
729 static void shader_glsl_load_constantsB(const struct wined3d_shader
*shader
, const struct wined3d_gl_info
*gl_info
,
730 const GLint locations
[MAX_CONST_B
], const BOOL
*constants
, WORD constants_set
)
735 for (i
= 0; constants_set
; constants_set
>>= 1, ++i
)
737 if (!(constants_set
& 1)) continue;
739 GL_EXTCALL(glUniform1iv(locations
[i
], 1, &constants
[i
]));
742 /* Load immediate constants */
743 ptr
= list_head(&shader
->constantsB
);
746 const struct wined3d_shader_lconst
*lconst
= LIST_ENTRY(ptr
, const struct wined3d_shader_lconst
, entry
);
747 unsigned int idx
= lconst
->idx
;
748 const GLint
*values
= (const GLint
*)lconst
->value
;
750 GL_EXTCALL(glUniform1iv(locations
[idx
], 1, values
));
751 ptr
= list_next(&shader
->constantsB
, ptr
);
753 checkGLcall("glUniform1iv()");
756 static void reset_program_constant_version(struct wine_rb_entry
*entry
, void *context
)
758 WINE_RB_ENTRY_VALUE(entry
, struct glsl_shader_prog_link
, program_lookup_entry
)->constant_version
= 0;
761 /* Context activation is done by the caller (state handler). */
762 static void shader_glsl_load_np2fixup_constants(const struct glsl_ps_program
*ps
,
763 const struct wined3d_gl_info
*gl_info
, const struct wined3d_state
*state
)
765 GLfloat np2fixup_constants
[4 * MAX_FRAGMENT_SAMPLERS
];
766 UINT fixup
= ps
->np2_fixup_info
->active
;
769 for (i
= 0; fixup
; fixup
>>= 1, ++i
)
771 const struct wined3d_texture
*tex
= state
->textures
[i
];
772 unsigned char idx
= ps
->np2_fixup_info
->idx
[i
];
773 GLfloat
*tex_dim
= &np2fixup_constants
[(idx
>> 1) * 4];
777 ERR("Nonexistent texture is flagged for NP2 texcoord fixup.\n");
783 tex_dim
[2] = tex
->pow2_matrix
[0];
784 tex_dim
[3] = tex
->pow2_matrix
[5];
788 tex_dim
[0] = tex
->pow2_matrix
[0];
789 tex_dim
[1] = tex
->pow2_matrix
[5];
793 GL_EXTCALL(glUniform4fv(ps
->np2_fixup_location
, ps
->np2_fixup_info
->num_consts
, np2fixup_constants
));
796 /* Taken and adapted from Mesa. */
797 static BOOL
invert_matrix_3d(struct wined3d_matrix
*out
, const struct wined3d_matrix
*in
)
799 float pos
, neg
, t
, det
;
800 struct wined3d_matrix temp
;
802 /* Calculate the determinant of upper left 3x3 submatrix and
803 * determine if the matrix is singular. */
805 t
= in
->_11
* in
->_22
* in
->_33
;
811 t
= in
->_21
* in
->_32
* in
->_13
;
816 t
= in
->_31
* in
->_12
* in
->_23
;
822 t
= -in
->_31
* in
->_22
* in
->_13
;
827 t
= -in
->_21
* in
->_12
* in
->_33
;
833 t
= -in
->_11
* in
->_32
* in
->_23
;
841 if (fabsf(det
) < 1e-25f
)
845 temp
._11
= (in
->_22
* in
->_33
- in
->_32
* in
->_23
) * det
;
846 temp
._12
= -(in
->_12
* in
->_33
- in
->_32
* in
->_13
) * det
;
847 temp
._13
= (in
->_12
* in
->_23
- in
->_22
* in
->_13
) * det
;
848 temp
._21
= -(in
->_21
* in
->_33
- in
->_31
* in
->_23
) * det
;
849 temp
._22
= (in
->_11
* in
->_33
- in
->_31
* in
->_13
) * det
;
850 temp
._23
= -(in
->_11
* in
->_23
- in
->_21
* in
->_13
) * det
;
851 temp
._31
= (in
->_21
* in
->_32
- in
->_31
* in
->_22
) * det
;
852 temp
._32
= -(in
->_11
* in
->_32
- in
->_31
* in
->_12
) * det
;
853 temp
._33
= (in
->_11
* in
->_22
- in
->_21
* in
->_12
) * det
;
859 static void swap_rows(float **a
, float **b
)
867 static BOOL
invert_matrix(struct wined3d_matrix
*out
, struct wined3d_matrix
*m
)
870 float m0
, m1
, m2
, m3
, s
;
871 float *r0
, *r1
, *r2
, *r3
;
883 r0
[5] = r0
[6] = r0
[7] = 0.0f
;
890 r1
[4] = r1
[6] = r1
[7] = 0.0f
;
897 r2
[4] = r2
[5] = r2
[7] = 0.0f
;
904 r3
[4] = r3
[5] = r3
[6] = 0.0f
;
906 /* Choose pivot - or die. */
907 if (fabsf(r3
[0]) > fabsf(r2
[0]))
909 if (fabsf(r2
[0]) > fabsf(r1
[0]))
911 if (fabsf(r1
[0]) > fabsf(r0
[0]))
916 /* Eliminate first variable. */
917 m1
= r1
[0] / r0
[0]; m2
= r2
[0] / r0
[0]; m3
= r3
[0] / r0
[0];
918 s
= r0
[1]; r1
[1] -= m1
* s
; r2
[1] -= m2
* s
; r3
[1] -= m3
* s
;
919 s
= r0
[2]; r1
[2] -= m1
* s
; r2
[2] -= m2
* s
; r3
[2] -= m3
* s
;
920 s
= r0
[3]; r1
[3] -= m1
* s
; r2
[3] -= m2
* s
; r3
[3] -= m3
* s
;
950 /* Choose pivot - or die. */
951 if (fabsf(r3
[1]) > fabsf(r2
[1]))
953 if (fabsf(r2
[1]) > fabsf(r1
[1]))
958 /* Eliminate second variable. */
959 m2
= r2
[1] / r1
[1]; m3
= r3
[1] / r1
[1];
960 r2
[2] -= m2
* r1
[2]; r3
[2] -= m3
* r1
[2];
961 r2
[3] -= m2
* r1
[3]; r3
[3] -= m3
* r1
[3];
987 /* Choose pivot - or die. */
988 if (fabsf(r3
[2]) > fabsf(r2
[2]))
993 /* Eliminate third variable. */
1005 /* Back substitute row 3. */
1012 /* Back substitute row 2. */
1015 r2
[4] = s
* (r2
[4] - r3
[4] * m2
);
1016 r2
[5] = s
* (r2
[5] - r3
[5] * m2
);
1017 r2
[6] = s
* (r2
[6] - r3
[6] * m2
);
1018 r2
[7] = s
* (r2
[7] - r3
[7] * m2
);
1020 r1
[4] -= r3
[4] * m1
;
1021 r1
[5] -= r3
[5] * m1
;
1022 r1
[6] -= r3
[6] * m1
;
1023 r1
[7] -= r3
[7] * m1
;
1025 r0
[4] -= r3
[4] * m0
;
1026 r0
[5] -= r3
[5] * m0
;
1027 r0
[6] -= r3
[6] * m0
;
1028 r0
[7] -= r3
[7] * m0
;
1030 /* Back substitute row 1. */
1033 r1
[4] = s
* (r1
[4] - r2
[4] * m1
);
1034 r1
[5] = s
* (r1
[5] - r2
[5] * m1
);
1035 r1
[6] = s
* (r1
[6] - r2
[6] * m1
);
1036 r1
[7] = s
* (r1
[7] - r2
[7] * m1
);
1038 r0
[4] -= r2
[4] * m0
;
1039 r0
[5] -= r2
[5] * m0
;
1040 r0
[6] -= r2
[6] * m0
;
1041 r0
[7] -= r2
[7] * m0
;
1043 /* Back substitute row 0. */
1046 r0
[4] = s
* (r0
[4] - r1
[4] * m0
);
1047 r0
[5] = s
* (r0
[5] - r1
[5] * m0
);
1048 r0
[6] = s
* (r0
[6] - r1
[6] * m0
);
1049 r0
[7] = s
* (r0
[7] - r1
[7] * m0
);
1071 static void shader_glsl_ffp_vertex_normalmatrix_uniform(const struct wined3d_context
*context
,
1072 const struct wined3d_state
*state
, struct glsl_shader_prog_link
*prog
)
1074 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1076 struct wined3d_matrix mv
;
1079 if (prog
->vs
.normal_matrix_location
== -1)
1082 get_modelview_matrix(context
, state
, 0, &mv
);
1083 if (context
->swapchain
->device
->wined3d
->flags
& WINED3D_LEGACY_FFP_LIGHTING
)
1084 invert_matrix_3d(&mv
, &mv
);
1086 invert_matrix(&mv
, &mv
);
1087 /* Tests show that singular modelview matrices are used unchanged as normal
1088 * matrices on D3D3 and older. There seems to be no clearly consistent
1089 * behavior on newer D3D versions so always follow older ddraw behavior. */
1090 for (i
= 0; i
< 3; ++i
)
1091 for (j
= 0; j
< 3; ++j
)
1092 mat
[i
* 3 + j
] = (&mv
._11
)[j
* 4 + i
];
1094 GL_EXTCALL(glUniformMatrix3fv(prog
->vs
.normal_matrix_location
, 1, FALSE
, mat
));
1095 checkGLcall("glUniformMatrix3fv");
1098 static void shader_glsl_ffp_vertex_texmatrix_uniform(const struct wined3d_context
*context
,
1099 const struct wined3d_state
*state
, unsigned int tex
, struct glsl_shader_prog_link
*prog
)
1101 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1102 struct wined3d_matrix mat
;
1104 if (tex
>= MAX_TEXTURES
)
1106 if (prog
->vs
.texture_matrix_location
[tex
] == -1)
1109 get_texture_matrix(context
, state
, tex
, &mat
);
1110 GL_EXTCALL(glUniformMatrix4fv(prog
->vs
.texture_matrix_location
[tex
], 1, FALSE
, &mat
._11
));
1111 checkGLcall("glUniformMatrix4fv");
1114 static void shader_glsl_ffp_vertex_material_uniform(const struct wined3d_context
*context
,
1115 const struct wined3d_state
*state
, struct glsl_shader_prog_link
*prog
)
1117 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1119 if (state
->render_states
[WINED3D_RS_SPECULARENABLE
])
1121 GL_EXTCALL(glUniform4fv(prog
->vs
.material_specular_location
, 1, &state
->material
.specular
.r
));
1122 GL_EXTCALL(glUniform1f(prog
->vs
.material_shininess_location
, state
->material
.power
));
1126 static const float black
[] = {0.0f
, 0.0f
, 0.0f
, 0.0f
};
1128 GL_EXTCALL(glUniform4fv(prog
->vs
.material_specular_location
, 1, black
));
1130 GL_EXTCALL(glUniform4fv(prog
->vs
.material_ambient_location
, 1, &state
->material
.ambient
.r
));
1131 GL_EXTCALL(glUniform4fv(prog
->vs
.material_diffuse_location
, 1, &state
->material
.diffuse
.r
));
1132 GL_EXTCALL(glUniform4fv(prog
->vs
.material_emissive_location
, 1, &state
->material
.emissive
.r
));
1133 checkGLcall("setting FFP material uniforms");
1136 static void shader_glsl_ffp_vertex_lightambient_uniform(const struct wined3d_context
*context
,
1137 const struct wined3d_state
*state
, struct glsl_shader_prog_link
*prog
)
1139 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1142 D3DCOLORTOGLFLOAT4(state
->render_states
[WINED3D_RS_AMBIENT
], col
);
1143 GL_EXTCALL(glUniform3fv(prog
->vs
.light_ambient_location
, 1, col
));
1144 checkGLcall("glUniform3fv");
1147 static void multiply_vector_matrix(struct wined3d_vec4
*dest
, const struct wined3d_vec4
*src1
,
1148 const struct wined3d_matrix
*src2
)
1150 struct wined3d_vec4 temp
;
1152 temp
.x
= (src1
->x
* src2
->_11
) + (src1
->y
* src2
->_21
) + (src1
->z
* src2
->_31
) + (src1
->w
* src2
->_41
);
1153 temp
.y
= (src1
->x
* src2
->_12
) + (src1
->y
* src2
->_22
) + (src1
->z
* src2
->_32
) + (src1
->w
* src2
->_42
);
1154 temp
.z
= (src1
->x
* src2
->_13
) + (src1
->y
* src2
->_23
) + (src1
->z
* src2
->_33
) + (src1
->w
* src2
->_43
);
1155 temp
.w
= (src1
->x
* src2
->_14
) + (src1
->y
* src2
->_24
) + (src1
->z
* src2
->_34
) + (src1
->w
* src2
->_44
);
1160 static void shader_glsl_ffp_vertex_light_uniform(const struct wined3d_context
*context
,
1161 const struct wined3d_state
*state
, unsigned int light
, struct glsl_shader_prog_link
*prog
)
1163 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1164 const struct wined3d_light_info
*light_info
= state
->lights
[light
];
1165 struct wined3d_vec4 vec4
;
1166 const struct wined3d_matrix
*view
= &state
->transforms
[WINED3D_TS_VIEW
];
1171 GL_EXTCALL(glUniform4fv(prog
->vs
.light_location
[light
].diffuse
, 1, &light_info
->OriginalParms
.diffuse
.r
));
1172 GL_EXTCALL(glUniform4fv(prog
->vs
.light_location
[light
].specular
, 1, &light_info
->OriginalParms
.specular
.r
));
1173 GL_EXTCALL(glUniform4fv(prog
->vs
.light_location
[light
].ambient
, 1, &light_info
->OriginalParms
.ambient
.r
));
1175 switch (light_info
->OriginalParms
.type
)
1177 case WINED3D_LIGHT_POINT
:
1178 multiply_vector_matrix(&vec4
, &light_info
->position
, view
);
1179 GL_EXTCALL(glUniform4fv(prog
->vs
.light_location
[light
].position
, 1, &vec4
.x
));
1180 GL_EXTCALL(glUniform1f(prog
->vs
.light_location
[light
].range
, light_info
->OriginalParms
.range
));
1181 GL_EXTCALL(glUniform1f(prog
->vs
.light_location
[light
].c_att
, light_info
->OriginalParms
.attenuation0
));
1182 GL_EXTCALL(glUniform1f(prog
->vs
.light_location
[light
].l_att
, light_info
->OriginalParms
.attenuation1
));
1183 GL_EXTCALL(glUniform1f(prog
->vs
.light_location
[light
].q_att
, light_info
->OriginalParms
.attenuation2
));
1186 case WINED3D_LIGHT_SPOT
:
1187 multiply_vector_matrix(&vec4
, &light_info
->position
, view
);
1188 GL_EXTCALL(glUniform4fv(prog
->vs
.light_location
[light
].position
, 1, &vec4
.x
));
1190 multiply_vector_matrix(&vec4
, &light_info
->direction
, view
);
1191 GL_EXTCALL(glUniform3fv(prog
->vs
.light_location
[light
].direction
, 1, &vec4
.x
));
1193 GL_EXTCALL(glUniform1f(prog
->vs
.light_location
[light
].range
, light_info
->OriginalParms
.range
));
1194 GL_EXTCALL(glUniform1f(prog
->vs
.light_location
[light
].falloff
, light_info
->OriginalParms
.falloff
));
1195 GL_EXTCALL(glUniform1f(prog
->vs
.light_location
[light
].c_att
, light_info
->OriginalParms
.attenuation0
));
1196 GL_EXTCALL(glUniform1f(prog
->vs
.light_location
[light
].l_att
, light_info
->OriginalParms
.attenuation1
));
1197 GL_EXTCALL(glUniform1f(prog
->vs
.light_location
[light
].q_att
, light_info
->OriginalParms
.attenuation2
));
1198 GL_EXTCALL(glUniform1f(prog
->vs
.light_location
[light
].cos_htheta
, cosf(light_info
->OriginalParms
.theta
/ 2.0f
)));
1199 GL_EXTCALL(glUniform1f(prog
->vs
.light_location
[light
].cos_hphi
, cosf(light_info
->OriginalParms
.phi
/ 2.0f
)));
1202 case WINED3D_LIGHT_DIRECTIONAL
:
1203 multiply_vector_matrix(&vec4
, &light_info
->direction
, view
);
1204 GL_EXTCALL(glUniform3fv(prog
->vs
.light_location
[light
].direction
, 1, &vec4
.x
));
1207 case WINED3D_LIGHT_PARALLELPOINT
:
1208 multiply_vector_matrix(&vec4
, &light_info
->position
, view
);
1209 GL_EXTCALL(glUniform4fv(prog
->vs
.light_location
[light
].position
, 1, &vec4
.x
));
1213 FIXME("Unrecognized light type %#x.\n", light_info
->OriginalParms
.type
);
1215 checkGLcall("setting FFP lights uniforms");
1218 static void shader_glsl_pointsize_uniform(const struct wined3d_context
*context
,
1219 const struct wined3d_state
*state
, struct glsl_shader_prog_link
*prog
)
1221 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1225 get_pointsize_minmax(context
, state
, &min
, &max
);
1227 GL_EXTCALL(glUniform1f(prog
->vs
.pointsize_min_location
, min
));
1228 checkGLcall("glUniform1f");
1229 GL_EXTCALL(glUniform1f(prog
->vs
.pointsize_max_location
, max
));
1230 checkGLcall("glUniform1f");
1232 get_pointsize(context
, state
, &size
, att
);
1234 GL_EXTCALL(glUniform1f(prog
->vs
.pointsize_location
, size
));
1235 checkGLcall("glUniform1f");
1236 GL_EXTCALL(glUniform1f(prog
->vs
.pointsize_c_att_location
, att
[0]));
1237 checkGLcall("glUniform1f");
1238 GL_EXTCALL(glUniform1f(prog
->vs
.pointsize_l_att_location
, att
[1]));
1239 checkGLcall("glUniform1f");
1240 GL_EXTCALL(glUniform1f(prog
->vs
.pointsize_q_att_location
, att
[2]));
1241 checkGLcall("glUniform1f");
1244 static void shader_glsl_load_fog_uniform(const struct wined3d_context
*context
,
1245 const struct wined3d_state
*state
, struct glsl_shader_prog_link
*prog
)
1247 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1248 float start
, end
, scale
;
1256 D3DCOLORTOGLFLOAT4(state
->render_states
[WINED3D_RS_FOGCOLOR
], col
);
1257 GL_EXTCALL(glUniform4fv(prog
->ps
.fog_color_location
, 1, col
));
1258 tmpvalue
.d
= state
->render_states
[WINED3D_RS_FOGDENSITY
];
1259 GL_EXTCALL(glUniform1f(prog
->ps
.fog_density_location
, tmpvalue
.f
));
1260 get_fog_start_end(context
, state
, &start
, &end
);
1261 scale
= 1.0f
/ (end
- start
);
1262 GL_EXTCALL(glUniform1f(prog
->ps
.fog_end_location
, end
));
1263 GL_EXTCALL(glUniform1f(prog
->ps
.fog_scale_location
, scale
));
1264 checkGLcall("fog emulation uniforms");
1267 /* Context activation is done by the caller (state handler). */
1268 static void shader_glsl_load_color_key_constant(const struct glsl_ps_program
*ps
,
1269 const struct wined3d_gl_info
*gl_info
, const struct wined3d_state
*state
)
1271 struct wined3d_color float_key
[2];
1272 const struct wined3d_texture
*texture
= state
->textures
[0];
1274 wined3d_format_get_float_color_key(texture
->resource
.format
, &texture
->async
.src_blt_color_key
, float_key
);
1275 GL_EXTCALL(glUniform4fv(ps
->color_key_location
, 2, &float_key
[0].r
));
1278 /* Context activation is done by the caller (state handler). */
1279 static void shader_glsl_load_constants(void *shader_priv
, struct wined3d_context
*context
,
1280 const struct wined3d_state
*state
)
1282 const struct glsl_context_data
*ctx_data
= context
->shader_backend_data
;
1283 const struct wined3d_shader
*vshader
= state
->shader
[WINED3D_SHADER_TYPE_VERTEX
];
1284 const struct wined3d_shader
*pshader
= state
->shader
[WINED3D_SHADER_TYPE_PIXEL
];
1285 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1286 struct shader_glsl_priv
*priv
= shader_priv
;
1287 float position_fixup
[4];
1290 struct glsl_shader_prog_link
*prog
= ctx_data
->glsl_program
;
1291 UINT constant_version
;
1295 /* No GLSL program set - nothing to do. */
1298 constant_version
= prog
->constant_version
;
1299 update_mask
= context
->constant_update_mask
& prog
->constant_update_mask
;
1301 if (update_mask
& WINED3D_SHADER_CONST_VS_F
)
1302 shader_glsl_load_constantsF(vshader
, gl_info
, state
->vs_consts_f
,
1303 prog
->vs
.uniform_f_locations
, &priv
->vconst_heap
, priv
->stack
, constant_version
);
1305 if (update_mask
& WINED3D_SHADER_CONST_VS_I
)
1306 shader_glsl_load_constantsI(vshader
, gl_info
, prog
->vs
.uniform_i_locations
, state
->vs_consts_i
,
1307 vshader
->reg_maps
.integer_constants
);
1309 if (update_mask
& WINED3D_SHADER_CONST_VS_B
)
1310 shader_glsl_load_constantsB(vshader
, gl_info
, prog
->vs
.uniform_b_locations
, state
->vs_consts_b
,
1311 vshader
->reg_maps
.boolean_constants
);
1313 if (update_mask
& WINED3D_SHADER_CONST_VS_POINTSIZE
)
1314 shader_glsl_pointsize_uniform(context
, state
, prog
);
1316 if (update_mask
& WINED3D_SHADER_CONST_VS_POS_FIXUP
)
1318 shader_get_position_fixup(context
, state
, position_fixup
);
1319 GL_EXTCALL(glUniform4fv(prog
->vs
.pos_fixup_location
, 1, position_fixup
));
1320 checkGLcall("glUniform4fv");
1323 if (update_mask
& WINED3D_SHADER_CONST_FFP_MODELVIEW
)
1325 struct wined3d_matrix mat
;
1327 get_modelview_matrix(context
, state
, 0, &mat
);
1328 GL_EXTCALL(glUniformMatrix4fv(prog
->vs
.modelview_matrix_location
[0], 1, FALSE
, &mat
._11
));
1329 checkGLcall("glUniformMatrix4fv");
1331 shader_glsl_ffp_vertex_normalmatrix_uniform(context
, state
, prog
);
1334 if (update_mask
& WINED3D_SHADER_CONST_FFP_VERTEXBLEND
)
1336 struct wined3d_matrix mat
;
1338 for (i
= 1; i
< MAX_VERTEX_BLENDS
; ++i
)
1340 if (prog
->vs
.modelview_matrix_location
[i
] == -1)
1343 get_modelview_matrix(context
, state
, i
, &mat
);
1344 GL_EXTCALL(glUniformMatrix4fv(prog
->vs
.modelview_matrix_location
[i
], 1, FALSE
, &mat
._11
));
1345 checkGLcall("glUniformMatrix4fv");
1349 if (update_mask
& WINED3D_SHADER_CONST_FFP_PROJ
)
1351 struct wined3d_matrix projection
;
1353 get_projection_matrix(context
, state
, &projection
);
1354 GL_EXTCALL(glUniformMatrix4fv(prog
->vs
.projection_matrix_location
, 1, FALSE
, &projection
._11
));
1355 checkGLcall("glUniformMatrix4fv");
1358 if (update_mask
& WINED3D_SHADER_CONST_FFP_TEXMATRIX
)
1360 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
1361 shader_glsl_ffp_vertex_texmatrix_uniform(context
, state
, i
, prog
);
1364 if (update_mask
& WINED3D_SHADER_CONST_FFP_MATERIAL
)
1365 shader_glsl_ffp_vertex_material_uniform(context
, state
, prog
);
1367 if (update_mask
& WINED3D_SHADER_CONST_FFP_LIGHTS
)
1369 shader_glsl_ffp_vertex_lightambient_uniform(context
, state
, prog
);
1370 for (i
= 0; i
< MAX_ACTIVE_LIGHTS
; ++i
)
1371 shader_glsl_ffp_vertex_light_uniform(context
, state
, i
, prog
);
1374 if (update_mask
& WINED3D_SHADER_CONST_PS_F
)
1375 shader_glsl_load_constantsF(pshader
, gl_info
, state
->ps_consts_f
,
1376 prog
->ps
.uniform_f_locations
, &priv
->pconst_heap
, priv
->stack
, constant_version
);
1378 if (update_mask
& WINED3D_SHADER_CONST_PS_I
)
1379 shader_glsl_load_constantsI(pshader
, gl_info
, prog
->ps
.uniform_i_locations
, state
->ps_consts_i
,
1380 pshader
->reg_maps
.integer_constants
);
1382 if (update_mask
& WINED3D_SHADER_CONST_PS_B
)
1383 shader_glsl_load_constantsB(pshader
, gl_info
, prog
->ps
.uniform_b_locations
, state
->ps_consts_b
,
1384 pshader
->reg_maps
.boolean_constants
);
1386 if (update_mask
& WINED3D_SHADER_CONST_PS_BUMP_ENV
)
1388 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
1390 if (prog
->ps
.bumpenv_mat_location
[i
] == -1)
1393 GL_EXTCALL(glUniformMatrix2fv(prog
->ps
.bumpenv_mat_location
[i
], 1, 0,
1394 (const GLfloat
*)&state
->texture_states
[i
][WINED3D_TSS_BUMPENV_MAT00
]));
1396 if (prog
->ps
.bumpenv_lum_scale_location
[i
] != -1)
1398 GL_EXTCALL(glUniform1fv(prog
->ps
.bumpenv_lum_scale_location
[i
], 1,
1399 (const GLfloat
*)&state
->texture_states
[i
][WINED3D_TSS_BUMPENV_LSCALE
]));
1400 GL_EXTCALL(glUniform1fv(prog
->ps
.bumpenv_lum_offset_location
[i
], 1,
1401 (const GLfloat
*)&state
->texture_states
[i
][WINED3D_TSS_BUMPENV_LOFFSET
]));
1405 checkGLcall("bump env uniforms");
1408 if (update_mask
& WINED3D_SHADER_CONST_PS_Y_CORR
)
1410 float correction_params
[] =
1412 /* position is window relative, not viewport relative */
1413 context
->render_offscreen
? 0.0f
: (float)context
->current_rt
->resource
.height
,
1414 context
->render_offscreen
? 1.0f
: -1.0f
,
1419 GL_EXTCALL(glUniform4fv(prog
->ps
.ycorrection_location
, 1, correction_params
));
1422 if (update_mask
& WINED3D_SHADER_CONST_PS_NP2_FIXUP
)
1423 shader_glsl_load_np2fixup_constants(&prog
->ps
, gl_info
, state
);
1424 if (update_mask
& WINED3D_SHADER_CONST_FFP_COLOR_KEY
)
1425 shader_glsl_load_color_key_constant(&prog
->ps
, gl_info
, state
);
1427 if (update_mask
& WINED3D_SHADER_CONST_FFP_PS
)
1431 if (prog
->ps
.tex_factor_location
!= -1)
1433 D3DCOLORTOGLFLOAT4(state
->render_states
[WINED3D_RS_TEXTUREFACTOR
], col
);
1434 GL_EXTCALL(glUniform4fv(prog
->ps
.tex_factor_location
, 1, col
));
1437 if (state
->render_states
[WINED3D_RS_SPECULARENABLE
])
1438 GL_EXTCALL(glUniform4f(prog
->ps
.specular_enable_location
, 1.0f
, 1.0f
, 1.0f
, 0.0f
));
1440 GL_EXTCALL(glUniform4f(prog
->ps
.specular_enable_location
, 0.0f
, 0.0f
, 0.0f
, 0.0f
));
1442 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
1444 if (prog
->ps
.tss_constant_location
[i
] == -1)
1447 D3DCOLORTOGLFLOAT4(state
->texture_states
[i
][WINED3D_TSS_CONSTANT
], col
);
1448 GL_EXTCALL(glUniform4fv(prog
->ps
.tss_constant_location
[i
], 1, col
));
1451 checkGLcall("fixed function uniforms");
1454 if (update_mask
& WINED3D_SHADER_CONST_PS_FOG
)
1455 shader_glsl_load_fog_uniform(context
, state
, prog
);
1457 if (priv
->next_constant_version
== UINT_MAX
)
1459 TRACE("Max constant version reached, resetting to 0.\n");
1460 wine_rb_for_each_entry(&priv
->program_lookup
, reset_program_constant_version
, NULL
);
1461 priv
->next_constant_version
= 1;
1465 prog
->constant_version
= priv
->next_constant_version
++;
1469 static void update_heap_entry(struct constant_heap
*heap
, unsigned int idx
, DWORD new_version
)
1471 struct constant_entry
*entries
= heap
->entries
;
1472 unsigned int *positions
= heap
->positions
;
1473 unsigned int heap_idx
, parent_idx
;
1475 if (!heap
->contained
[idx
])
1477 heap_idx
= heap
->size
++;
1478 heap
->contained
[idx
] = TRUE
;
1482 heap_idx
= positions
[idx
];
1485 while (heap_idx
> 1)
1487 parent_idx
= heap_idx
>> 1;
1489 if (new_version
<= entries
[parent_idx
].version
) break;
1491 entries
[heap_idx
] = entries
[parent_idx
];
1492 positions
[entries
[parent_idx
].idx
] = heap_idx
;
1493 heap_idx
= parent_idx
;
1496 entries
[heap_idx
].version
= new_version
;
1497 entries
[heap_idx
].idx
= idx
;
1498 positions
[idx
] = heap_idx
;
1501 static void shader_glsl_update_float_vertex_constants(struct wined3d_device
*device
, UINT start
, UINT count
)
1503 struct shader_glsl_priv
*priv
= device
->shader_priv
;
1504 struct constant_heap
*heap
= &priv
->vconst_heap
;
1507 for (i
= start
; i
< count
+ start
; ++i
)
1509 update_heap_entry(heap
, i
, priv
->next_constant_version
);
1512 for (i
= 0; i
< device
->context_count
; ++i
)
1514 device
->contexts
[i
]->constant_update_mask
|= WINED3D_SHADER_CONST_VS_F
;
1518 static void shader_glsl_update_float_pixel_constants(struct wined3d_device
*device
, UINT start
, UINT count
)
1520 struct shader_glsl_priv
*priv
= device
->shader_priv
;
1521 struct constant_heap
*heap
= &priv
->pconst_heap
;
1524 for (i
= start
; i
< count
+ start
; ++i
)
1526 update_heap_entry(heap
, i
, priv
->next_constant_version
);
1529 for (i
= 0; i
< device
->context_count
; ++i
)
1531 device
->contexts
[i
]->constant_update_mask
|= WINED3D_SHADER_CONST_PS_F
;
1535 static unsigned int vec4_varyings(DWORD shader_major
, const struct wined3d_gl_info
*gl_info
)
1537 unsigned int ret
= gl_info
->limits
.glsl_varyings
/ 4;
1538 /* 4.0 shaders do not write clip coords because d3d10 does not support user clipplanes */
1539 if(shader_major
> 3) return ret
;
1541 /* 3.0 shaders may need an extra varying for the clip coord on some cards(mostly dx10 ones) */
1542 if (gl_info
->quirks
& WINED3D_QUIRK_GLSL_CLIP_VARYING
) ret
-= 1;
1546 static BOOL
needs_legacy_glsl_syntax(const struct wined3d_gl_info
*gl_info
)
1548 return gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
];
1551 static void PRINTF_ATTR(4, 5) declare_in_varying(const struct wined3d_gl_info
*gl_info
,
1552 struct wined3d_string_buffer
*buffer
, BOOL flat
, const char *format
, ...)
1557 shader_addline(buffer
, "%s%s ", flat
? "flat " : "",
1558 needs_legacy_glsl_syntax(gl_info
) ? "varying" : "in");
1561 va_start(args
, format
);
1562 ret
= shader_vaddline(buffer
, format
, args
);
1566 if (!string_buffer_resize(buffer
, ret
))
1571 static void PRINTF_ATTR(4, 5) declare_out_varying(const struct wined3d_gl_info
*gl_info
,
1572 struct wined3d_string_buffer
*buffer
, BOOL flat
, const char *format
, ...)
1577 shader_addline(buffer
, "%s%s ", flat
? "flat " : "",
1578 needs_legacy_glsl_syntax(gl_info
) ? "varying" : "out");
1581 va_start(args
, format
);
1582 ret
= shader_vaddline(buffer
, format
, args
);
1586 if (!string_buffer_resize(buffer
, ret
))
1591 static BOOL
glsl_is_color_reg_read(const struct wined3d_shader
*shader
, unsigned int idx
)
1593 const struct wined3d_shader_signature
*input_signature
= &shader
->input_signature
;
1594 const struct wined3d_shader_reg_maps
*reg_maps
= &shader
->reg_maps
;
1595 const BOOL
*input_reg_used
= shader
->u
.ps
.input_reg_used
;
1598 if (reg_maps
->shader_version
.major
< 3)
1599 return input_reg_used
[idx
];
1601 for (i
= 0; i
< input_signature
->element_count
; ++i
)
1603 const struct wined3d_shader_signature_element
*input
= &input_signature
->elements
[i
];
1605 if (!(reg_maps
->input_registers
& (1u << input
->register_idx
)))
1608 if (shader_match_semantic(input
->semantic_name
, WINED3D_DECL_USAGE_COLOR
)
1609 && input
->semantic_idx
== idx
)
1611 if (input_reg_used
[input
->register_idx
])
1620 /** Generate the variable & register declarations for the GLSL output target */
1621 static void shader_generate_glsl_declarations(const struct wined3d_context
*context
,
1622 struct wined3d_string_buffer
*buffer
, const struct wined3d_shader
*shader
,
1623 const struct wined3d_shader_reg_maps
*reg_maps
, const struct shader_glsl_ctx_priv
*ctx_priv
)
1625 const struct wined3d_shader_version
*version
= ®_maps
->shader_version
;
1626 const struct wined3d_state
*state
= &shader
->device
->state
;
1627 const struct vs_compile_args
*vs_args
= ctx_priv
->cur_vs_args
;
1628 const struct ps_compile_args
*ps_args
= ctx_priv
->cur_ps_args
;
1629 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1630 const struct wined3d_fb_state
*fb
= &shader
->device
->fb
;
1631 unsigned int i
, extra_constants_needed
= 0;
1632 const struct wined3d_shader_lconst
*lconst
;
1636 prefix
= shader_glsl_get_prefix(version
->type
);
1638 /* Prototype the subroutines */
1639 for (i
= 0, map
= reg_maps
->labels
; map
; map
>>= 1, ++i
)
1641 if (map
& 1) shader_addline(buffer
, "void subroutine%u();\n", i
);
1644 /* Declare the constants (aka uniforms) */
1645 if (shader
->limits
->constant_float
> 0)
1647 unsigned max_constantsF
;
1649 /* Unless the shader uses indirect addressing, always declare the
1650 * maximum array size and ignore that we need some uniforms privately.
1651 * E.g. if GL supports 256 uniforms, and we need 2 for the pos fixup
1652 * and immediate values, still declare VC[256]. If the shader needs
1653 * more uniforms than we have it won't work in any case. If it uses
1654 * less, the compiler will figure out which uniforms are really used
1655 * and strip them out. This allows a shader to use c255 on a dx9 card,
1656 * as long as it doesn't also use all the other constants.
1658 * If the shader uses indirect addressing the compiler must assume
1659 * that all declared uniforms are used. In this case, declare only the
1660 * amount that we're assured to have.
1662 * Thus we run into problems in these two cases:
1663 * 1) The shader really uses more uniforms than supported.
1664 * 2) The shader uses indirect addressing, less constants than
1665 * supported, but uses a constant index > #supported consts. */
1666 if (version
->type
== WINED3D_SHADER_TYPE_PIXEL
)
1668 /* No indirect addressing here. */
1669 max_constantsF
= gl_info
->limits
.glsl_ps_float_constants
;
1673 if (reg_maps
->usesrelconstF
)
1675 /* Subtract the other potential uniforms from the max
1676 * available (bools, ints, and 1 row of projection matrix).
1677 * Subtract another uniform for immediate values, which have
1678 * to be loaded via uniform by the driver as well. The shader
1679 * code only uses 0.5, 2.0, 1.0, 128 and -128 in vertex
1680 * shader code, so one vec4 should be enough. (Unfortunately
1681 * the Nvidia driver doesn't store 128 and -128 in one float).
1683 * Writing gl_ClipVertex requires one uniform for each
1684 * clipplane as well. */
1685 max_constantsF
= gl_info
->limits
.glsl_vs_float_constants
- 3;
1686 if (vs_args
->clip_enabled
)
1687 max_constantsF
-= gl_info
->limits
.clipplanes
;
1688 max_constantsF
-= count_bits(reg_maps
->integer_constants
);
1689 /* Strictly speaking a bool only uses one scalar, but the nvidia(Linux) compiler doesn't pack them properly,
1690 * so each scalar requires a full vec4. We could work around this by packing the booleans ourselves, but
1691 * for now take this into account when calculating the number of available constants
1693 max_constantsF
-= count_bits(reg_maps
->boolean_constants
);
1694 /* Set by driver quirks in directx.c */
1695 max_constantsF
-= gl_info
->reserved_glsl_constants
;
1697 if (max_constantsF
< shader
->limits
->constant_float
)
1699 static unsigned int once
;
1702 ERR_(winediag
)("The hardware does not support enough uniform components to run this shader,"
1703 " it may not render correctly.\n");
1705 WARN("The hardware does not support enough uniform components to run this shader.\n");
1710 max_constantsF
= gl_info
->limits
.glsl_vs_float_constants
;
1713 max_constantsF
= min(shader
->limits
->constant_float
, max_constantsF
);
1714 shader_addline(buffer
, "uniform vec4 %s_c[%u];\n", prefix
, max_constantsF
);
1717 /* Always declare the full set of constants, the compiler can remove the
1718 * unused ones because d3d doesn't (yet) support indirect int and bool
1719 * constant addressing. This avoids problems if the app uses e.g. i0 and i9. */
1720 if (shader
->limits
->constant_int
> 0 && reg_maps
->integer_constants
)
1721 shader_addline(buffer
, "uniform ivec4 %s_i[%u];\n", prefix
, shader
->limits
->constant_int
);
1723 if (shader
->limits
->constant_bool
> 0 && reg_maps
->boolean_constants
)
1724 shader_addline(buffer
, "uniform bool %s_b[%u];\n", prefix
, shader
->limits
->constant_bool
);
1726 for (i
= 0; i
< WINED3D_MAX_CBS
; ++i
)
1728 if (reg_maps
->cb_sizes
[i
])
1729 shader_addline(buffer
, "layout(std140) uniform block_%s_cb%u { vec4 %s_cb%u[%u]; };\n",
1730 prefix
, i
, prefix
, i
, reg_maps
->cb_sizes
[i
]);
1733 /* Declare texture samplers */
1734 for (i
= 0; i
< reg_maps
->sampler_map
.count
; ++i
)
1736 struct wined3d_shader_sampler_map_entry
*entry
;
1737 BOOL shadow_sampler
, tex_rect
;
1738 const char *sampler_type
;
1740 entry
= ®_maps
->sampler_map
.entries
[i
];
1742 if (entry
->resource_idx
>= ARRAY_SIZE(reg_maps
->resource_info
))
1744 ERR("Invalid resource index %u.\n", entry
->resource_idx
);
1748 shadow_sampler
= version
->type
== WINED3D_SHADER_TYPE_PIXEL
&& (ps_args
->shadow
& (1u << entry
->sampler_idx
));
1749 switch (reg_maps
->resource_info
[entry
->resource_idx
].type
)
1751 case WINED3D_SHADER_RESOURCE_TEXTURE_1D
:
1753 sampler_type
= "sampler1DShadow";
1755 sampler_type
= "sampler1D";
1758 case WINED3D_SHADER_RESOURCE_TEXTURE_2D
:
1759 tex_rect
= version
->type
== WINED3D_SHADER_TYPE_PIXEL
1760 && (ps_args
->np2_fixup
& (1u << entry
->resource_idx
))
1761 && gl_info
->supported
[ARB_TEXTURE_RECTANGLE
];
1765 sampler_type
= "sampler2DRectShadow";
1767 sampler_type
= "sampler2DShadow";
1772 sampler_type
= "sampler2DRect";
1774 sampler_type
= "sampler2D";
1778 case WINED3D_SHADER_RESOURCE_TEXTURE_3D
:
1780 FIXME("Unsupported 3D shadow sampler.\n");
1781 sampler_type
= "sampler3D";
1784 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE
:
1786 FIXME("Unsupported Cube shadow sampler.\n");
1787 sampler_type
= "samplerCube";
1791 sampler_type
= "unsupported_sampler";
1792 FIXME("Unhandled resource type %#x.\n", reg_maps
->resource_info
[i
].type
);
1795 shader_addline(buffer
, "uniform %s %s_sampler%u;\n", sampler_type
, prefix
, entry
->bind_idx
);
1798 /* Declare uniforms for NP2 texcoord fixup:
1799 * This is NOT done inside the loop that declares the texture samplers
1800 * since the NP2 fixup code is currently only used for the GeforceFX
1801 * series and when forcing the ARB_npot extension off. Modern cards just
1802 * skip the code anyway, so put it inside a separate loop. */
1803 if (version
->type
== WINED3D_SHADER_TYPE_PIXEL
&& ps_args
->np2_fixup
)
1805 struct ps_np2fixup_info
*fixup
= ctx_priv
->cur_np2fixup_info
;
1808 /* NP2/RECT textures in OpenGL use texcoords in the range [0,width]x[0,height]
1809 * while D3D has them in the (normalized) [0,1]x[0,1] range.
1810 * samplerNP2Fixup stores texture dimensions and is updated through
1811 * shader_glsl_load_np2fixup_constants when the sampler changes. */
1813 for (i
= 0; i
< shader
->limits
->sampler
; ++i
)
1815 if (!reg_maps
->resource_info
[i
].type
|| !(ps_args
->np2_fixup
& (1u << i
)))
1818 if (reg_maps
->resource_info
[i
].type
!= WINED3D_SHADER_RESOURCE_TEXTURE_2D
)
1820 FIXME("Non-2D texture is flagged for NP2 texcoord fixup.\n");
1824 fixup
->idx
[i
] = cur
++;
1827 fixup
->num_consts
= (cur
+ 1) >> 1;
1828 fixup
->active
= ps_args
->np2_fixup
;
1829 shader_addline(buffer
, "uniform vec4 %s_samplerNP2Fixup[%u];\n", prefix
, fixup
->num_consts
);
1832 /* Declare address variables */
1833 for (i
= 0, map
= reg_maps
->address
; map
; map
>>= 1, ++i
)
1835 if (map
& 1) shader_addline(buffer
, "ivec4 A%u;\n", i
);
1838 if (version
->type
== WINED3D_SHADER_TYPE_VERTEX
)
1840 for (i
= 0; i
< shader
->input_signature
.element_count
; ++i
)
1842 const struct wined3d_shader_signature_element
*e
= &shader
->input_signature
.elements
[i
];
1843 if (e
->sysval_semantic
== WINED3D_SV_INSTANCEID
)
1844 shader_addline(buffer
, "vec4 %s_in%u = vec4(intBitsToFloat(gl_InstanceID), 0.0, 0.0, 0.0);\n",
1845 prefix
, e
->register_idx
);
1847 shader_addline(buffer
, "attribute vec4 %s_in%u;\n", prefix
, e
->register_idx
);
1850 if (vs_args
->point_size
&& !vs_args
->per_vertex_point_size
)
1852 shader_addline(buffer
, "uniform struct\n{\n");
1853 shader_addline(buffer
, " float size;\n");
1854 shader_addline(buffer
, " float size_min;\n");
1855 shader_addline(buffer
, " float size_max;\n");
1856 shader_addline(buffer
, "} ffp_point;\n");
1859 if (!gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
] && version
->major
< 3)
1861 declare_out_varying(gl_info
, buffer
, vs_args
->flatshading
, "vec4 ffp_varying_diffuse;\n");
1862 declare_out_varying(gl_info
, buffer
, vs_args
->flatshading
, "vec4 ffp_varying_specular;\n");
1863 declare_out_varying(gl_info
, buffer
, FALSE
, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES
);
1864 declare_out_varying(gl_info
, buffer
, FALSE
, "float ffp_varying_fogcoord;\n");
1867 shader_addline(buffer
, "uniform vec4 posFixup;\n");
1868 shader_addline(buffer
, "void order_ps_input(in vec4[%u]);\n", shader
->limits
->packed_output
);
1870 else if (version
->type
== WINED3D_SHADER_TYPE_GEOMETRY
)
1872 shader_addline(buffer
, "varying in vec4 gs_in[][%u];\n", shader
->limits
->packed_input
);
1874 else if (version
->type
== WINED3D_SHADER_TYPE_PIXEL
)
1876 if (version
->major
< 3 || ps_args
->vp_mode
!= vertexshader
)
1878 shader_addline(buffer
, "uniform struct\n{\n");
1879 shader_addline(buffer
, " vec4 color;\n");
1880 shader_addline(buffer
, " float density;\n");
1881 shader_addline(buffer
, " float end;\n");
1882 shader_addline(buffer
, " float scale;\n");
1883 shader_addline(buffer
, "} ffp_fog;\n");
1885 if (gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
])
1887 if (glsl_is_color_reg_read(shader
, 0))
1888 shader_addline(buffer
, "vec4 ffp_varying_diffuse;\n");
1889 if (glsl_is_color_reg_read(shader
, 1))
1890 shader_addline(buffer
, "vec4 ffp_varying_specular;\n");
1891 shader_addline(buffer
, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES
);
1892 shader_addline(buffer
, "float ffp_varying_fogcoord;\n");
1896 if (glsl_is_color_reg_read(shader
, 0))
1897 declare_in_varying(gl_info
, buffer
, ps_args
->flatshading
, "vec4 ffp_varying_diffuse;\n");
1898 if (glsl_is_color_reg_read(shader
, 1))
1899 declare_in_varying(gl_info
, buffer
, ps_args
->flatshading
, "vec4 ffp_varying_specular;\n");
1900 declare_in_varying(gl_info
, buffer
, FALSE
, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES
);
1901 shader_addline(buffer
, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES
);
1902 declare_in_varying(gl_info
, buffer
, FALSE
, "float ffp_varying_fogcoord;\n");
1906 if (version
->major
>= 3)
1908 UINT in_count
= min(vec4_varyings(version
->major
, gl_info
), shader
->limits
->packed_input
);
1911 declare_in_varying(gl_info
, buffer
, FALSE
, "vec4 %s_link[%u];\n", prefix
, in_count
);
1912 shader_addline(buffer
, "vec4 %s_in[%u];\n", prefix
, in_count
);
1915 for (i
= 0, map
= reg_maps
->bumpmat
; map
; map
>>= 1, ++i
)
1920 shader_addline(buffer
, "uniform mat2 bumpenv_mat%u;\n", i
);
1922 if (reg_maps
->luminanceparams
& (1u << i
))
1924 shader_addline(buffer
, "uniform float bumpenv_lum_scale%u;\n", i
);
1925 shader_addline(buffer
, "uniform float bumpenv_lum_offset%u;\n", i
);
1926 extra_constants_needed
++;
1929 extra_constants_needed
++;
1932 if (ps_args
->srgb_correction
)
1934 shader_addline(buffer
, "const vec4 srgb_const0 = ");
1935 shader_glsl_append_imm_vec4(buffer
, wined3d_srgb_const0
);
1936 shader_addline(buffer
, ";\n");
1937 shader_addline(buffer
, "const vec4 srgb_const1 = ");
1938 shader_glsl_append_imm_vec4(buffer
, wined3d_srgb_const1
);
1939 shader_addline(buffer
, ";\n");
1941 if (reg_maps
->vpos
|| reg_maps
->usesdsy
)
1943 if (shader
->limits
->constant_float
+ extra_constants_needed
1944 + 1 < gl_info
->limits
.glsl_ps_float_constants
)
1946 shader_addline(buffer
, "uniform vec4 ycorrection;\n");
1947 extra_constants_needed
++;
1951 float ycorrection
[] =
1953 context
->render_offscreen
? 0.0f
: fb
->render_targets
[0]->height
,
1954 context
->render_offscreen
? 1.0f
: -1.0f
,
1959 /* This happens because we do not have proper tracking of the
1960 * constant registers that are actually used, only the max
1961 * limit of the shader version. */
1962 FIXME("Cannot find a free uniform for vpos correction params\n");
1963 shader_addline(buffer
, "const vec4 ycorrection = ");
1964 shader_glsl_append_imm_vec4(buffer
, ycorrection
);
1965 shader_addline(buffer
, ";\n");
1967 shader_addline(buffer
, "vec4 vpos;\n");
1971 /* Declare output register temporaries */
1972 if (shader
->limits
->packed_output
)
1973 shader_addline(buffer
, "vec4 %s_out[%u];\n", prefix
, shader
->limits
->packed_output
);
1975 /* Declare temporary variables */
1976 for (i
= 0, map
= reg_maps
->temporary
; map
; map
>>= 1, ++i
)
1978 if (map
& 1) shader_addline(buffer
, "vec4 R%u;\n", i
);
1981 /* Declare loop registers aLx */
1982 if (version
->major
< 4)
1984 for (i
= 0; i
< reg_maps
->loop_depth
; ++i
)
1986 shader_addline(buffer
, "int aL%u;\n", i
);
1987 shader_addline(buffer
, "int tmpInt%u;\n", i
);
1991 /* Temporary variables for matrix operations */
1992 shader_addline(buffer
, "vec4 tmp0;\n");
1993 shader_addline(buffer
, "vec4 tmp1;\n");
1995 if (!shader
->load_local_constsF
)
1997 LIST_FOR_EACH_ENTRY(lconst
, &shader
->constantsF
, struct wined3d_shader_lconst
, entry
)
1999 shader_addline(buffer
, "const vec4 %s_lc%u = ", prefix
, lconst
->idx
);
2000 shader_glsl_append_imm_vec4(buffer
, (const float *)lconst
->value
);
2001 shader_addline(buffer
, ";\n");
2005 /* Start the main program. */
2006 shader_addline(buffer
, "void main()\n{\n");
2008 /* Direct3D applications expect integer vPos values, while OpenGL drivers
2009 * add approximately 0.5. This causes off-by-one problems as spotted by
2010 * the vPos d3d9 visual test. Unfortunately ATI cards do not add exactly
2011 * 0.5, but rather something like 0.49999999 or 0.50000001, which still
2012 * causes precision troubles when we just subtract 0.5.
2014 * To deal with that, just floor() the position. This will eliminate the
2015 * fraction on all cards.
2017 * TODO: Test how this behaves with multisampling.
2019 * An advantage of floor is that it works even if the driver doesn't add
2020 * 0.5. It is somewhat questionable if 1.5, 2.5, ... are the proper values
2021 * to return in gl_FragCoord, even though coordinates specify the pixel
2022 * centers instead of the pixel corners. This code will behave correctly
2023 * on drivers that returns integer values. */
2024 if (version
->type
== WINED3D_SHADER_TYPE_PIXEL
&& reg_maps
->vpos
)
2026 if (shader
->device
->wined3d
->flags
& WINED3D_PIXEL_CENTER_INTEGER
)
2027 shader_addline(buffer
,
2028 "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
2030 shader_addline(buffer
,
2031 "vpos = vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1);\n");
2035 /*****************************************************************************
2036 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
2038 * For more information, see http://wiki.winehq.org/DirectX-Shaders
2039 ****************************************************************************/
2042 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction
*ins
,
2043 const struct wined3d_shader_src_param
*wined3d_src
, DWORD mask
, struct glsl_src_param
*glsl_src
);
2045 /** Used for opcode modifiers - They multiply the result by the specified amount */
2046 static const char * const shift_glsl_tab
[] = {
2048 "2.0 * ", /* 1 (x2) */
2049 "4.0 * ", /* 2 (x4) */
2050 "8.0 * ", /* 3 (x8) */
2051 "16.0 * ", /* 4 (x16) */
2052 "32.0 * ", /* 5 (x32) */
2059 "0.0625 * ", /* 12 (d16) */
2060 "0.125 * ", /* 13 (d8) */
2061 "0.25 * ", /* 14 (d4) */
2062 "0.5 * " /* 15 (d2) */
2065 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
2066 static void shader_glsl_gen_modifier(enum wined3d_shader_src_modifier src_modifier
,
2067 const char *in_reg
, const char *in_regswizzle
, char *out_str
)
2069 switch (src_modifier
)
2071 case WINED3DSPSM_DZ
: /* Need to handle this in the instructions itself (texld & texcrd). */
2072 case WINED3DSPSM_DW
:
2073 case WINED3DSPSM_NONE
:
2074 sprintf(out_str
, "%s%s", in_reg
, in_regswizzle
);
2076 case WINED3DSPSM_NEG
:
2077 sprintf(out_str
, "-%s%s", in_reg
, in_regswizzle
);
2079 case WINED3DSPSM_NOT
:
2080 sprintf(out_str
, "!%s%s", in_reg
, in_regswizzle
);
2082 case WINED3DSPSM_BIAS
:
2083 sprintf(out_str
, "(%s%s - vec4(0.5)%s)", in_reg
, in_regswizzle
, in_regswizzle
);
2085 case WINED3DSPSM_BIASNEG
:
2086 sprintf(out_str
, "-(%s%s - vec4(0.5)%s)", in_reg
, in_regswizzle
, in_regswizzle
);
2088 case WINED3DSPSM_SIGN
:
2089 sprintf(out_str
, "(2.0 * (%s%s - 0.5))", in_reg
, in_regswizzle
);
2091 case WINED3DSPSM_SIGNNEG
:
2092 sprintf(out_str
, "-(2.0 * (%s%s - 0.5))", in_reg
, in_regswizzle
);
2094 case WINED3DSPSM_COMP
:
2095 sprintf(out_str
, "(1.0 - %s%s)", in_reg
, in_regswizzle
);
2097 case WINED3DSPSM_X2
:
2098 sprintf(out_str
, "(2.0 * %s%s)", in_reg
, in_regswizzle
);
2100 case WINED3DSPSM_X2NEG
:
2101 sprintf(out_str
, "-(2.0 * %s%s)", in_reg
, in_regswizzle
);
2103 case WINED3DSPSM_ABS
:
2104 sprintf(out_str
, "abs(%s%s)", in_reg
, in_regswizzle
);
2106 case WINED3DSPSM_ABSNEG
:
2107 sprintf(out_str
, "-abs(%s%s)", in_reg
, in_regswizzle
);
2110 FIXME("Unhandled modifier %u\n", src_modifier
);
2111 sprintf(out_str
, "%s%s", in_reg
, in_regswizzle
);
2115 /** Writes the GLSL variable name that corresponds to the register that the
2116 * DX opcode parameter is trying to access */
2117 static void shader_glsl_get_register_name(const struct wined3d_shader_register
*reg
,
2118 char *register_name
, BOOL
*is_color
, const struct wined3d_shader_instruction
*ins
)
2120 /* oPos, oFog and oPts in D3D */
2121 static const char * const hwrastout_reg_names
[] = {"vs_out[10]", "vs_out[11].x", "vs_out[11].y"};
2123 const struct wined3d_shader
*shader
= ins
->ctx
->shader
;
2124 const struct wined3d_shader_reg_maps
*reg_maps
= ins
->ctx
->reg_maps
;
2125 const struct wined3d_shader_version
*version
= ®_maps
->shader_version
;
2126 const struct wined3d_gl_info
*gl_info
= ins
->ctx
->gl_info
;
2127 const char *prefix
= shader_glsl_get_prefix(version
->type
);
2128 struct glsl_src_param rel_param0
, rel_param1
;
2129 char imm_str
[4][17];
2131 if (reg
->idx
[0].offset
!= ~0U && reg
->idx
[0].rel_addr
)
2132 shader_glsl_add_src_param(ins
, reg
->idx
[0].rel_addr
, WINED3DSP_WRITEMASK_0
, &rel_param0
);
2133 if (reg
->idx
[1].offset
!= ~0U && reg
->idx
[1].rel_addr
)
2134 shader_glsl_add_src_param(ins
, reg
->idx
[1].rel_addr
, WINED3DSP_WRITEMASK_0
, &rel_param1
);
2139 case WINED3DSPR_TEMP
:
2140 sprintf(register_name
, "R%u", reg
->idx
[0].offset
);
2143 case WINED3DSPR_INPUT
:
2144 /* vertex shaders */
2145 if (version
->type
== WINED3D_SHADER_TYPE_VERTEX
)
2147 struct shader_glsl_ctx_priv
*priv
= ins
->ctx
->backend_data
;
2148 if (priv
->cur_vs_args
->swizzle_map
& (1u << reg
->idx
[0].offset
))
2150 sprintf(register_name
, "%s_in%u", prefix
, reg
->idx
[0].offset
);
2154 if (version
->type
== WINED3D_SHADER_TYPE_GEOMETRY
)
2156 if (reg
->idx
[0].rel_addr
)
2158 if (reg
->idx
[1].rel_addr
)
2159 sprintf(register_name
, "gs_in[%s + %u][%s + %u]",
2160 rel_param0
.param_str
, reg
->idx
[0].offset
, rel_param1
.param_str
, reg
->idx
[1].offset
);
2162 sprintf(register_name
, "gs_in[%s + %u][%u]",
2163 rel_param0
.param_str
, reg
->idx
[0].offset
, reg
->idx
[1].offset
);
2165 else if (reg
->idx
[1].rel_addr
)
2166 sprintf(register_name
, "gs_in[%u][%s + %u]",
2167 reg
->idx
[0].offset
, rel_param1
.param_str
, reg
->idx
[1].offset
);
2169 sprintf(register_name
, "gs_in[%u][%u]", reg
->idx
[0].offset
, reg
->idx
[1].offset
);
2173 /* pixel shaders >= 3.0 */
2174 if (version
->major
>= 3)
2176 DWORD idx
= shader
->u
.ps
.input_reg_map
[reg
->idx
[0].offset
];
2177 unsigned int in_count
= vec4_varyings(version
->major
, gl_info
);
2179 if (reg
->idx
[0].rel_addr
)
2181 /* Removing a + 0 would be an obvious optimization, but
2182 * OS X doesn't see the NOP operation there. */
2185 if (gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
]
2186 && shader
->u
.ps
.declared_in_count
> in_count
)
2188 sprintf(register_name
,
2189 "((%s + %u) > %u ? (%s + %u) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s + %u])",
2190 rel_param0
.param_str
, idx
, in_count
- 1, rel_param0
.param_str
, idx
, in_count
,
2191 prefix
, rel_param0
.param_str
, idx
);
2195 sprintf(register_name
, "%s_in[%s + %u]", prefix
, rel_param0
.param_str
, idx
);
2200 if (gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
]
2201 && shader
->u
.ps
.declared_in_count
> in_count
)
2203 sprintf(register_name
, "((%s) > %u ? (%s) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s])",
2204 rel_param0
.param_str
, in_count
- 1, rel_param0
.param_str
, in_count
,
2205 prefix
, rel_param0
.param_str
);
2209 sprintf(register_name
, "%s_in[%s]", prefix
, rel_param0
.param_str
);
2215 if (idx
== in_count
) sprintf(register_name
, "gl_Color");
2216 else if (idx
== in_count
+ 1) sprintf(register_name
, "gl_SecondaryColor");
2217 else sprintf(register_name
, "%s_in[%u]", prefix
, idx
);
2222 if (!reg
->idx
[0].offset
)
2223 strcpy(register_name
, "ffp_varying_diffuse");
2225 strcpy(register_name
, "ffp_varying_specular");
2230 case WINED3DSPR_CONST
:
2232 /* Relative addressing */
2233 if (reg
->idx
[0].rel_addr
)
2235 if (reg
->idx
[0].offset
)
2236 sprintf(register_name
, "%s_c[%s + %u]", prefix
, rel_param0
.param_str
, reg
->idx
[0].offset
);
2238 sprintf(register_name
, "%s_c[%s]", prefix
, rel_param0
.param_str
);
2242 if (shader_constant_is_local(shader
, reg
->idx
[0].offset
))
2243 sprintf(register_name
, "%s_lc%u", prefix
, reg
->idx
[0].offset
);
2245 sprintf(register_name
, "%s_c[%u]", prefix
, reg
->idx
[0].offset
);
2250 case WINED3DSPR_CONSTINT
:
2251 sprintf(register_name
, "%s_i[%u]", prefix
, reg
->idx
[0].offset
);
2254 case WINED3DSPR_CONSTBOOL
:
2255 sprintf(register_name
, "%s_b[%u]", prefix
, reg
->idx
[0].offset
);
2258 case WINED3DSPR_TEXTURE
: /* case WINED3DSPR_ADDR: */
2259 if (version
->type
== WINED3D_SHADER_TYPE_PIXEL
)
2260 sprintf(register_name
, "T%u", reg
->idx
[0].offset
);
2262 sprintf(register_name
, "A%u", reg
->idx
[0].offset
);
2265 case WINED3DSPR_LOOP
:
2266 sprintf(register_name
, "aL%u", ins
->ctx
->loop_state
->current_reg
- 1);
2269 case WINED3DSPR_SAMPLER
:
2270 sprintf(register_name
, "%s_sampler%u", prefix
, reg
->idx
[0].offset
);
2273 case WINED3DSPR_COLOROUT
:
2274 if (reg
->idx
[0].offset
>= gl_info
->limits
.buffers
)
2275 WARN("Write to render target %u, only %d supported.\n",
2276 reg
->idx
[0].offset
, gl_info
->limits
.buffers
);
2278 sprintf(register_name
, "gl_FragData[%u]", reg
->idx
[0].offset
);
2281 case WINED3DSPR_RASTOUT
:
2282 sprintf(register_name
, "%s", hwrastout_reg_names
[reg
->idx
[0].offset
]);
2285 case WINED3DSPR_DEPTHOUT
:
2286 sprintf(register_name
, "gl_FragDepth");
2289 case WINED3DSPR_ATTROUT
:
2290 if (!reg
->idx
[0].offset
)
2291 sprintf(register_name
, "%s_out[8]", prefix
);
2293 sprintf(register_name
, "%s_out[9]", prefix
);
2296 case WINED3DSPR_TEXCRDOUT
:
2297 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
2298 sprintf(register_name
, "%s_out[%u]", prefix
, reg
->idx
[0].offset
);
2301 case WINED3DSPR_MISCTYPE
:
2302 if (!reg
->idx
[0].offset
)
2305 sprintf(register_name
, "vpos");
2307 else if (reg
->idx
[0].offset
== 1)
2309 /* Note that gl_FrontFacing is a bool, while vFace is
2310 * a float for which the sign determines front/back */
2311 sprintf(register_name
, "(gl_FrontFacing ? 1.0 : -1.0)");
2315 FIXME("Unhandled misctype register %u.\n", reg
->idx
[0].offset
);
2316 sprintf(register_name
, "unrecognized_register");
2320 case WINED3DSPR_IMMCONST
:
2321 switch (reg
->immconst_type
)
2323 case WINED3D_IMMCONST_SCALAR
:
2324 switch (reg
->data_type
)
2326 case WINED3D_DATA_FLOAT
:
2327 wined3d_ftoa(*(const float *)reg
->immconst_data
, register_name
);
2329 case WINED3D_DATA_INT
:
2330 sprintf(register_name
, "%#x", reg
->immconst_data
[0]);
2332 case WINED3D_DATA_RESOURCE
:
2333 case WINED3D_DATA_SAMPLER
:
2334 case WINED3D_DATA_UINT
:
2335 sprintf(register_name
, "%#xu", reg
->immconst_data
[0]);
2338 sprintf(register_name
, "<unhandled data type %#x>", reg
->data_type
);
2343 case WINED3D_IMMCONST_VEC4
:
2344 switch (reg
->data_type
)
2346 case WINED3D_DATA_FLOAT
:
2347 wined3d_ftoa(*(const float *)®
->immconst_data
[0], imm_str
[0]);
2348 wined3d_ftoa(*(const float *)®
->immconst_data
[1], imm_str
[1]);
2349 wined3d_ftoa(*(const float *)®
->immconst_data
[2], imm_str
[2]);
2350 wined3d_ftoa(*(const float *)®
->immconst_data
[3], imm_str
[3]);
2351 sprintf(register_name
, "vec4(%s, %s, %s, %s)",
2352 imm_str
[0], imm_str
[1], imm_str
[2], imm_str
[3]);
2354 case WINED3D_DATA_INT
:
2355 sprintf(register_name
, "ivec4(%#x, %#x, %#x, %#x)",
2356 reg
->immconst_data
[0], reg
->immconst_data
[1],
2357 reg
->immconst_data
[2], reg
->immconst_data
[3]);
2359 case WINED3D_DATA_RESOURCE
:
2360 case WINED3D_DATA_SAMPLER
:
2361 case WINED3D_DATA_UINT
:
2362 sprintf(register_name
, "uvec4(%#xu, %#xu, %#xu, %#xu)",
2363 reg
->immconst_data
[0], reg
->immconst_data
[1],
2364 reg
->immconst_data
[2], reg
->immconst_data
[3]);
2367 sprintf(register_name
, "<unhandled data type %#x>", reg
->data_type
);
2373 FIXME("Unhandled immconst type %#x\n", reg
->immconst_type
);
2374 sprintf(register_name
, "<unhandled_immconst_type %#x>", reg
->immconst_type
);
2378 case WINED3DSPR_CONSTBUFFER
:
2379 if (reg
->idx
[1].rel_addr
)
2380 sprintf(register_name
, "%s_cb%u[%s + %u]",
2381 prefix
, reg
->idx
[0].offset
, rel_param1
.param_str
, reg
->idx
[1].offset
);
2383 sprintf(register_name
, "%s_cb%u[%u]", prefix
, reg
->idx
[0].offset
, reg
->idx
[1].offset
);
2386 case WINED3DSPR_PRIMID
:
2387 sprintf(register_name
, "uint(gl_PrimitiveIDIn)");
2391 FIXME("Unhandled register type %#x.\n", reg
->type
);
2392 sprintf(register_name
, "unrecognized_register");
2397 static void shader_glsl_write_mask_to_str(DWORD write_mask
, char *str
)
2400 if (write_mask
& WINED3DSP_WRITEMASK_0
) *str
++ = 'x';
2401 if (write_mask
& WINED3DSP_WRITEMASK_1
) *str
++ = 'y';
2402 if (write_mask
& WINED3DSP_WRITEMASK_2
) *str
++ = 'z';
2403 if (write_mask
& WINED3DSP_WRITEMASK_3
) *str
++ = 'w';
2407 /* Get the GLSL write mask for the destination register */
2408 static DWORD
shader_glsl_get_write_mask(const struct wined3d_shader_dst_param
*param
, char *write_mask
)
2410 DWORD mask
= param
->write_mask
;
2412 if (shader_is_scalar(¶m
->reg
))
2414 mask
= WINED3DSP_WRITEMASK_0
;
2419 shader_glsl_write_mask_to_str(mask
, write_mask
);
2425 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask
) {
2426 unsigned int size
= 0;
2428 if (write_mask
& WINED3DSP_WRITEMASK_0
) ++size
;
2429 if (write_mask
& WINED3DSP_WRITEMASK_1
) ++size
;
2430 if (write_mask
& WINED3DSP_WRITEMASK_2
) ++size
;
2431 if (write_mask
& WINED3DSP_WRITEMASK_3
) ++size
;
2436 static void shader_glsl_swizzle_to_str(const DWORD swizzle
, BOOL fixup
, DWORD mask
, char *str
)
2438 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
2439 * but addressed as "rgba". To fix this we need to swap the register's x
2440 * and z components. */
2441 const char *swizzle_chars
= fixup
? "zyxw" : "xyzw";
2444 /* swizzle bits fields: wwzzyyxx */
2445 if (mask
& WINED3DSP_WRITEMASK_0
) *str
++ = swizzle_chars
[swizzle
& 0x03];
2446 if (mask
& WINED3DSP_WRITEMASK_1
) *str
++ = swizzle_chars
[(swizzle
>> 2) & 0x03];
2447 if (mask
& WINED3DSP_WRITEMASK_2
) *str
++ = swizzle_chars
[(swizzle
>> 4) & 0x03];
2448 if (mask
& WINED3DSP_WRITEMASK_3
) *str
++ = swizzle_chars
[(swizzle
>> 6) & 0x03];
2452 static void shader_glsl_get_swizzle(const struct wined3d_shader_src_param
*param
,
2453 BOOL fixup
, DWORD mask
, char *swizzle_str
)
2455 if (shader_is_scalar(¶m
->reg
))
2456 *swizzle_str
= '\0';
2458 shader_glsl_swizzle_to_str(param
->swizzle
, fixup
, mask
, swizzle_str
);
2461 /* From a given parameter token, generate the corresponding GLSL string.
2462 * Also, return the actual register name and swizzle in case the
2463 * caller needs this information as well. */
2464 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction
*ins
,
2465 const struct wined3d_shader_src_param
*wined3d_src
, DWORD mask
, struct glsl_src_param
*glsl_src
)
2467 BOOL is_color
= FALSE
;
2468 char swizzle_str
[6];
2470 glsl_src
->reg_name
[0] = '\0';
2471 glsl_src
->param_str
[0] = '\0';
2472 swizzle_str
[0] = '\0';
2474 shader_glsl_get_register_name(&wined3d_src
->reg
, glsl_src
->reg_name
, &is_color
, ins
);
2475 shader_glsl_get_swizzle(wined3d_src
, is_color
, mask
, swizzle_str
);
2477 if (wined3d_src
->reg
.type
== WINED3DSPR_IMMCONST
|| wined3d_src
->reg
.type
== WINED3DSPR_PRIMID
)
2479 shader_glsl_gen_modifier(wined3d_src
->modifiers
, glsl_src
->reg_name
, swizzle_str
, glsl_src
->param_str
);
2485 switch (wined3d_src
->reg
.data_type
)
2487 case WINED3D_DATA_FLOAT
:
2488 sprintf(reg_name
, "%s", glsl_src
->reg_name
);
2490 case WINED3D_DATA_INT
:
2491 sprintf(reg_name
, "floatBitsToInt(%s)", glsl_src
->reg_name
);
2493 case WINED3D_DATA_RESOURCE
:
2494 case WINED3D_DATA_SAMPLER
:
2495 case WINED3D_DATA_UINT
:
2496 sprintf(reg_name
, "floatBitsToUint(%s)", glsl_src
->reg_name
);
2499 FIXME("Unhandled data type %#x.\n", wined3d_src
->reg
.data_type
);
2500 sprintf(reg_name
, "%s", glsl_src
->reg_name
);
2504 shader_glsl_gen_modifier(wined3d_src
->modifiers
, reg_name
, swizzle_str
, glsl_src
->param_str
);
2508 /* From a given parameter token, generate the corresponding GLSL string.
2509 * Also, return the actual register name and swizzle in case the
2510 * caller needs this information as well. */
2511 static DWORD
shader_glsl_add_dst_param(const struct wined3d_shader_instruction
*ins
,
2512 const struct wined3d_shader_dst_param
*wined3d_dst
, struct glsl_dst_param
*glsl_dst
)
2514 BOOL is_color
= FALSE
;
2516 glsl_dst
->mask_str
[0] = '\0';
2517 glsl_dst
->reg_name
[0] = '\0';
2519 shader_glsl_get_register_name(&wined3d_dst
->reg
, glsl_dst
->reg_name
, &is_color
, ins
);
2520 return shader_glsl_get_write_mask(wined3d_dst
, glsl_dst
->mask_str
);
2523 /* Append the destination part of the instruction to the buffer, return the effective write mask */
2524 static DWORD
shader_glsl_append_dst_ext(struct wined3d_string_buffer
*buffer
,
2525 const struct wined3d_shader_instruction
*ins
, const struct wined3d_shader_dst_param
*dst
,
2526 enum wined3d_data_type data_type
)
2528 struct glsl_dst_param glsl_dst
;
2531 if ((mask
= shader_glsl_add_dst_param(ins
, dst
, &glsl_dst
)))
2535 case WINED3D_DATA_FLOAT
:
2536 shader_addline(buffer
, "%s%s = %s(",
2537 glsl_dst
.reg_name
, glsl_dst
.mask_str
, shift_glsl_tab
[dst
->shift
]);
2539 case WINED3D_DATA_INT
:
2540 shader_addline(buffer
, "%s%s = %sintBitsToFloat(",
2541 glsl_dst
.reg_name
, glsl_dst
.mask_str
, shift_glsl_tab
[dst
->shift
]);
2543 case WINED3D_DATA_RESOURCE
:
2544 case WINED3D_DATA_SAMPLER
:
2545 case WINED3D_DATA_UINT
:
2546 shader_addline(buffer
, "%s%s = %suintBitsToFloat(",
2547 glsl_dst
.reg_name
, glsl_dst
.mask_str
, shift_glsl_tab
[dst
->shift
]);
2550 FIXME("Unhandled data type %#x.\n", data_type
);
2551 shader_addline(buffer
, "%s%s = %s(",
2552 glsl_dst
.reg_name
, glsl_dst
.mask_str
, shift_glsl_tab
[dst
->shift
]);
2560 /* Append the destination part of the instruction to the buffer, return the effective write mask */
2561 static DWORD
shader_glsl_append_dst(struct wined3d_string_buffer
*buffer
, const struct wined3d_shader_instruction
*ins
)
2563 return shader_glsl_append_dst_ext(buffer
, ins
, &ins
->dst
[0], ins
->dst
[0].reg
.data_type
);
2566 /** Process GLSL instruction modifiers */
2567 static void shader_glsl_add_instruction_modifiers(const struct wined3d_shader_instruction
*ins
)
2569 struct glsl_dst_param dst_param
;
2572 if (!ins
->dst_count
) return;
2574 modifiers
= ins
->dst
[0].modifiers
;
2575 if (!modifiers
) return;
2577 shader_glsl_add_dst_param(ins
, &ins
->dst
[0], &dst_param
);
2579 if (modifiers
& WINED3DSPDM_SATURATE
)
2581 /* _SAT means to clamp the value of the register to between 0 and 1 */
2582 shader_addline(ins
->ctx
->buffer
, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param
.reg_name
,
2583 dst_param
.mask_str
, dst_param
.reg_name
, dst_param
.mask_str
);
2586 if (modifiers
& WINED3DSPDM_MSAMPCENTROID
)
2588 FIXME("_centroid modifier not handled\n");
2591 if (modifiers
& WINED3DSPDM_PARTIALPRECISION
)
2593 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
2597 static const char *shader_glsl_get_rel_op(enum wined3d_shader_rel_op op
)
2601 case WINED3D_SHADER_REL_OP_GT
: return ">";
2602 case WINED3D_SHADER_REL_OP_EQ
: return "==";
2603 case WINED3D_SHADER_REL_OP_GE
: return ">=";
2604 case WINED3D_SHADER_REL_OP_LT
: return "<";
2605 case WINED3D_SHADER_REL_OP_NE
: return "!=";
2606 case WINED3D_SHADER_REL_OP_LE
: return "<=";
2608 FIXME("Unrecognized operator %#x.\n", op
);
2613 static void shader_glsl_get_sample_function(const struct wined3d_shader_context
*ctx
,
2614 DWORD resource_idx
, DWORD flags
, struct glsl_sample_function
*sample_function
)
2618 unsigned int coord_size
;
2619 const char *type_part
;
2623 {0, ""}, /* WINED3D_SHADER_RESOURCE_NONE */
2624 {1, ""}, /* WINED3D_SHADER_RESOURCE_BUFFER */
2625 {1, "1D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_1D */
2626 {2, "2D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2D */
2627 {2, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMS */
2628 {3, "3D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_3D */
2629 {3, "Cube"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_CUBE */
2630 {2, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY */
2631 {3, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY */
2632 {3, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY */
2634 struct shader_glsl_ctx_priv
*priv
= ctx
->backend_data
;
2635 enum wined3d_shader_resource_type resource_type
= ctx
->reg_maps
->resource_info
[resource_idx
].type
;
2636 const struct wined3d_gl_info
*gl_info
= ctx
->gl_info
;
2637 BOOL shadow
= ctx
->reg_maps
->shader_version
.type
== WINED3D_SHADER_TYPE_PIXEL
2638 && (priv
->cur_ps_args
->shadow
& (1u << resource_idx
));
2639 BOOL projected
= flags
& WINED3D_GLSL_SAMPLE_PROJECTED
;
2640 BOOL texrect
= flags
& WINED3D_GLSL_SAMPLE_NPOT
&& gl_info
->supported
[ARB_TEXTURE_RECTANGLE
];
2641 BOOL lod
= flags
& WINED3D_GLSL_SAMPLE_LOD
;
2642 BOOL grad
= flags
& WINED3D_GLSL_SAMPLE_GRAD
;
2643 const char *base
= "texture", *type_part
= "", *suffix
= "";
2644 unsigned int coord_size
;
2646 sample_function
->data_type
= ctx
->reg_maps
->resource_info
[resource_idx
].data_type
;
2648 if (resource_type
>= ARRAY_SIZE(resource_types
))
2650 ERR("Unexpected resource type %#x.\n", resource_type
);
2651 resource_type
= WINED3D_SHADER_RESOURCE_TEXTURE_2D
;
2654 /* Note that there's no such thing as a projected cube texture. */
2655 if (resource_type
== WINED3D_SHADER_RESOURCE_TEXTURE_CUBE
)
2658 if (needs_legacy_glsl_syntax(gl_info
))
2663 type_part
= resource_types
[resource_type
].type_part
;
2664 if (resource_type
== WINED3D_SHADER_RESOURCE_TEXTURE_2D
&& texrect
)
2665 type_part
= "2DRect";
2667 FIXME("Unhandled resource type %#x.\n", resource_type
);
2669 if (!lod
&& grad
&& !gl_info
->supported
[EXT_GPU_SHADER4
])
2671 if (gl_info
->supported
[ARB_SHADER_TEXTURE_LOD
])
2674 FIXME("Unsupported grad function.\n");
2678 if (flags
& WINED3D_GLSL_SAMPLE_LOAD
)
2680 if (flags
!= WINED3D_GLSL_SAMPLE_LOAD
)
2681 ERR("Unexpected flags for texelFetch %#x.\n", flags
& ~WINED3D_GLSL_SAMPLE_LOAD
);
2683 base
= "texelFetch";
2687 sample_function
->name
= string_buffer_get(priv
->string_buffers
);
2688 string_buffer_sprintf(sample_function
->name
, "%s%s%s%s%s", base
, type_part
, projected
? "Proj" : "",
2689 lod
? "Lod" : grad
? "Grad" : "", suffix
);
2691 coord_size
= resource_types
[resource_type
].coord_size
;
2694 sample_function
->coord_mask
= (1u << coord_size
) - 1;
2695 sample_function
->output_single_component
= shadow
&& !needs_legacy_glsl_syntax(gl_info
);
2698 static void shader_glsl_release_sample_function(const struct wined3d_shader_context
*ctx
,
2699 struct glsl_sample_function
*sample_function
)
2701 const struct shader_glsl_ctx_priv
*priv
= ctx
->backend_data
;
2703 string_buffer_release(priv
->string_buffers
, sample_function
->name
);
2706 static void shader_glsl_append_fixup_arg(char *arguments
, const char *reg_name
,
2707 BOOL sign_fixup
, enum fixup_channel_source channel_source
)
2709 switch(channel_source
)
2711 case CHANNEL_SOURCE_ZERO
:
2712 strcat(arguments
, "0.0");
2715 case CHANNEL_SOURCE_ONE
:
2716 strcat(arguments
, "1.0");
2719 case CHANNEL_SOURCE_X
:
2720 strcat(arguments
, reg_name
);
2721 strcat(arguments
, ".x");
2724 case CHANNEL_SOURCE_Y
:
2725 strcat(arguments
, reg_name
);
2726 strcat(arguments
, ".y");
2729 case CHANNEL_SOURCE_Z
:
2730 strcat(arguments
, reg_name
);
2731 strcat(arguments
, ".z");
2734 case CHANNEL_SOURCE_W
:
2735 strcat(arguments
, reg_name
);
2736 strcat(arguments
, ".w");
2740 FIXME("Unhandled channel source %#x\n", channel_source
);
2741 strcat(arguments
, "undefined");
2745 if (sign_fixup
) strcat(arguments
, " * 2.0 - 1.0");
2748 static void shader_glsl_color_correction_ext(struct wined3d_string_buffer
*buffer
,
2749 const char *reg_name
, DWORD mask
, struct color_fixup_desc fixup
)
2751 unsigned int mask_size
, remaining
;
2752 DWORD fixup_mask
= 0;
2753 char arguments
[256];
2756 if (fixup
.x_sign_fixup
|| fixup
.x_source
!= CHANNEL_SOURCE_X
) fixup_mask
|= WINED3DSP_WRITEMASK_0
;
2757 if (fixup
.y_sign_fixup
|| fixup
.y_source
!= CHANNEL_SOURCE_Y
) fixup_mask
|= WINED3DSP_WRITEMASK_1
;
2758 if (fixup
.z_sign_fixup
|| fixup
.z_source
!= CHANNEL_SOURCE_Z
) fixup_mask
|= WINED3DSP_WRITEMASK_2
;
2759 if (fixup
.w_sign_fixup
|| fixup
.w_source
!= CHANNEL_SOURCE_W
) fixup_mask
|= WINED3DSP_WRITEMASK_3
;
2760 if (!(mask
&= fixup_mask
))
2763 if (is_complex_fixup(fixup
))
2765 enum complex_fixup complex_fixup
= get_complex_fixup(fixup
);
2766 FIXME("Complex fixup (%#x) not supported\n",complex_fixup
);
2770 shader_glsl_write_mask_to_str(mask
, mask_str
);
2771 mask_size
= shader_glsl_get_write_mask_size(mask
);
2773 arguments
[0] = '\0';
2774 remaining
= mask_size
;
2775 if (mask
& WINED3DSP_WRITEMASK_0
)
2777 shader_glsl_append_fixup_arg(arguments
, reg_name
, fixup
.x_sign_fixup
, fixup
.x_source
);
2778 if (--remaining
) strcat(arguments
, ", ");
2780 if (mask
& WINED3DSP_WRITEMASK_1
)
2782 shader_glsl_append_fixup_arg(arguments
, reg_name
, fixup
.y_sign_fixup
, fixup
.y_source
);
2783 if (--remaining
) strcat(arguments
, ", ");
2785 if (mask
& WINED3DSP_WRITEMASK_2
)
2787 shader_glsl_append_fixup_arg(arguments
, reg_name
, fixup
.z_sign_fixup
, fixup
.z_source
);
2788 if (--remaining
) strcat(arguments
, ", ");
2790 if (mask
& WINED3DSP_WRITEMASK_3
)
2792 shader_glsl_append_fixup_arg(arguments
, reg_name
, fixup
.w_sign_fixup
, fixup
.w_source
);
2793 if (--remaining
) strcat(arguments
, ", ");
2797 shader_addline(buffer
, "%s%s = vec%u(%s);\n", reg_name
, mask_str
, mask_size
, arguments
);
2799 shader_addline(buffer
, "%s%s = %s;\n", reg_name
, mask_str
, arguments
);
2802 static void shader_glsl_color_correction(const struct wined3d_shader_instruction
*ins
, struct color_fixup_desc fixup
)
2807 shader_glsl_get_register_name(&ins
->dst
[0].reg
, reg_name
, &is_color
, ins
);
2808 shader_glsl_color_correction_ext(ins
->ctx
->buffer
, reg_name
, ins
->dst
[0].write_mask
, fixup
);
2811 static void PRINTF_ATTR(8, 9) shader_glsl_gen_sample_code(const struct wined3d_shader_instruction
*ins
,
2812 DWORD sampler
, const struct glsl_sample_function
*sample_function
, DWORD swizzle
,
2813 const char *dx
, const char *dy
, const char *bias
, const char *coord_reg_fmt
, ...)
2815 const struct wined3d_shader_version
*version
= &ins
->ctx
->reg_maps
->shader_version
;
2816 char dst_swizzle
[6];
2817 struct color_fixup_desc fixup
;
2818 BOOL np2_fixup
= FALSE
;
2822 shader_glsl_swizzle_to_str(swizzle
, FALSE
, ins
->dst
[0].write_mask
, dst_swizzle
);
2824 /* FIXME: We currently don't support fixups for vertex shaders or anything
2825 * above SM3. Note that for SM4+ the sampler index doesn't have to match
2826 * the resource index. */
2827 if (version
->type
== WINED3D_SHADER_TYPE_PIXEL
&& version
->major
< 4)
2829 const struct shader_glsl_ctx_priv
*priv
= ins
->ctx
->backend_data
;
2830 fixup
= priv
->cur_ps_args
->color_fixup
[sampler
];
2832 if (priv
->cur_ps_args
->np2_fixup
& (1u << sampler
))
2837 fixup
= COLOR_FIXUP_IDENTITY
;
2840 shader_glsl_append_dst_ext(ins
->ctx
->buffer
, ins
, &ins
->dst
[0], sample_function
->data_type
);
2842 if (sample_function
->output_single_component
)
2843 shader_addline(ins
->ctx
->buffer
, "vec4(");
2845 shader_addline(ins
->ctx
->buffer
, "%s(%s_sampler%u, ",
2846 sample_function
->name
->buffer
, shader_glsl_get_prefix(version
->type
), sampler
);
2850 va_start(args
, coord_reg_fmt
);
2851 ret
= shader_vaddline(ins
->ctx
->buffer
, coord_reg_fmt
, args
);
2855 if (!string_buffer_resize(ins
->ctx
->buffer
, ret
))
2861 const struct shader_glsl_ctx_priv
*priv
= ins
->ctx
->backend_data
;
2862 const unsigned char idx
= priv
->cur_np2fixup_info
->idx
[sampler
];
2864 switch (shader_glsl_get_write_mask_size(sample_function
->coord_mask
))
2867 shader_addline(ins
->ctx
->buffer
, " * ps_samplerNP2Fixup[%u].%s",
2868 idx
>> 1, (idx
% 2) ? "z" : "x");
2871 shader_addline(ins
->ctx
->buffer
, " * ps_samplerNP2Fixup[%u].%s",
2872 idx
>> 1, (idx
% 2) ? "zw" : "xy");
2875 shader_addline(ins
->ctx
->buffer
, " * vec3(ps_samplerNP2Fixup[%u].%s, 1.0)",
2876 idx
>> 1, (idx
% 2) ? "zw" : "xy");
2879 shader_addline(ins
->ctx
->buffer
, " * vec4(ps_samplerNP2Fixup[%u].%s, 1.0, 1.0)",
2880 idx
>> 1, (idx
% 2) ? "zw" : "xy");
2885 shader_addline(ins
->ctx
->buffer
, ", %s, %s)", dx
, dy
);
2887 shader_addline(ins
->ctx
->buffer
, ", %s)", bias
);
2889 shader_addline(ins
->ctx
->buffer
, ")");
2891 if (sample_function
->output_single_component
)
2892 shader_addline(ins
->ctx
->buffer
, ")");
2894 shader_addline(ins
->ctx
->buffer
, "%s);\n", dst_swizzle
);
2896 if (!is_identity_fixup(fixup
))
2897 shader_glsl_color_correction(ins
, fixup
);
2900 /*****************************************************************************
2901 * Begin processing individual instruction opcodes
2902 ****************************************************************************/
2904 static void shader_glsl_binop(const struct wined3d_shader_instruction
*ins
)
2906 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
2907 struct glsl_src_param src0_param
;
2908 struct glsl_src_param src1_param
;
2912 /* Determine the GLSL operator to use based on the opcode */
2913 switch (ins
->handler_idx
)
2915 case WINED3DSIH_ADD
: op
= "+"; break;
2916 case WINED3DSIH_AND
: op
= "&"; break;
2917 case WINED3DSIH_DIV
: op
= "/"; break;
2918 case WINED3DSIH_IADD
: op
= "+"; break;
2919 case WINED3DSIH_ISHL
: op
= "<<"; break;
2920 case WINED3DSIH_MUL
: op
= "*"; break;
2921 case WINED3DSIH_OR
: op
= "|"; break;
2922 case WINED3DSIH_SUB
: op
= "-"; break;
2923 case WINED3DSIH_USHR
: op
= ">>"; break;
2924 case WINED3DSIH_XOR
: op
= "^"; break;
2926 op
= "<unhandled operator>";
2927 FIXME("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins
->handler_idx
));
2931 write_mask
= shader_glsl_append_dst(buffer
, ins
);
2932 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
2933 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
2934 shader_addline(buffer
, "%s %s %s);\n", src0_param
.param_str
, op
, src1_param
.param_str
);
2937 static void shader_glsl_relop(const struct wined3d_shader_instruction
*ins
)
2939 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
2940 struct glsl_src_param src0_param
;
2941 struct glsl_src_param src1_param
;
2942 unsigned int mask_size
;
2946 write_mask
= shader_glsl_append_dst(buffer
, ins
);
2947 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
2948 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
2949 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
2953 switch (ins
->handler_idx
)
2955 case WINED3DSIH_EQ
: op
= "equal"; break;
2956 case WINED3DSIH_GE
: op
= "greaterThanEqual"; break;
2957 case WINED3DSIH_IGE
: op
= "greaterThanEqual"; break;
2958 case WINED3DSIH_UGE
: op
= "greaterThanEqual"; break;
2959 case WINED3DSIH_LT
: op
= "lessThan"; break;
2960 case WINED3DSIH_NE
: op
= "notEqual"; break;
2962 op
= "<unhandled operator>";
2963 ERR("Unhandled opcode %#x.\n", ins
->handler_idx
);
2967 shader_addline(buffer
, "uvec%u(%s(%s, %s)) * 0xffffffffu);\n",
2968 mask_size
, op
, src0_param
.param_str
, src1_param
.param_str
);
2972 switch (ins
->handler_idx
)
2974 case WINED3DSIH_EQ
: op
= "=="; break;
2975 case WINED3DSIH_GE
: op
= ">="; break;
2976 case WINED3DSIH_IGE
: op
= ">="; break;
2977 case WINED3DSIH_UGE
: op
= ">="; break;
2978 case WINED3DSIH_LT
: op
= "<"; break;
2979 case WINED3DSIH_NE
: op
= "!="; break;
2981 op
= "<unhandled operator>";
2982 ERR("Unhandled opcode %#x.\n", ins
->handler_idx
);
2986 shader_addline(buffer
, "%s %s %s ? 0xffffffffu : 0u);\n",
2987 src0_param
.param_str
, op
, src1_param
.param_str
);
2991 static void shader_glsl_imul(const struct wined3d_shader_instruction
*ins
)
2993 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
2994 struct glsl_src_param src0_param
;
2995 struct glsl_src_param src1_param
;
2998 /* If we have ARB_gpu_shader5 or GLSL 4.0, we can use imulExtended(). If
2999 * not, we can emulate it. */
3000 if (ins
->dst
[0].reg
.type
!= WINED3DSPR_NULL
)
3001 FIXME("64-bit integer multiplies not implemented.\n");
3003 if (ins
->dst
[1].reg
.type
!= WINED3DSPR_NULL
)
3005 write_mask
= shader_glsl_append_dst_ext(buffer
, ins
, &ins
->dst
[1], ins
->dst
[1].reg
.data_type
);
3006 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
3007 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
3009 shader_addline(ins
->ctx
->buffer
, "%s * %s);\n",
3010 src0_param
.param_str
, src1_param
.param_str
);
3014 static void shader_glsl_udiv(const struct wined3d_shader_instruction
*ins
)
3016 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
3017 struct glsl_src_param src0_param
, src1_param
;
3020 if (ins
->dst
[0].reg
.type
!= WINED3DSPR_NULL
)
3023 if (ins
->dst
[1].reg
.type
!= WINED3DSPR_NULL
)
3027 write_mask
= shader_glsl_get_write_mask(&ins
->dst
[0], dst_mask
);
3028 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
3029 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
3030 shader_addline(buffer
, "tmp0%s = %s / %s;\n",
3031 dst_mask
, src0_param
.param_str
, src1_param
.param_str
);
3033 write_mask
= shader_glsl_append_dst_ext(buffer
, ins
, &ins
->dst
[1], ins
->dst
[1].reg
.data_type
);
3034 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
3035 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
3036 shader_addline(buffer
, "%s %% %s));\n", src0_param
.param_str
, src1_param
.param_str
);
3038 shader_glsl_append_dst_ext(buffer
, ins
, &ins
->dst
[0], ins
->dst
[0].reg
.data_type
);
3039 shader_addline(buffer
, "tmp0%s);\n", dst_mask
);
3043 write_mask
= shader_glsl_append_dst_ext(buffer
, ins
, &ins
->dst
[0], ins
->dst
[0].reg
.data_type
);
3044 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
3045 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
3046 shader_addline(buffer
, "%s / %s);\n", src0_param
.param_str
, src1_param
.param_str
);
3049 else if (ins
->dst
[1].reg
.type
!= WINED3DSPR_NULL
)
3051 write_mask
= shader_glsl_append_dst_ext(buffer
, ins
, &ins
->dst
[1], ins
->dst
[1].reg
.data_type
);
3052 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
3053 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
3054 shader_addline(buffer
, "%s %% %s);\n", src0_param
.param_str
, src1_param
.param_str
);
3058 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
3059 static void shader_glsl_mov(const struct wined3d_shader_instruction
*ins
)
3061 const struct wined3d_gl_info
*gl_info
= ins
->ctx
->gl_info
;
3062 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
3063 struct glsl_src_param src0_param
;
3066 write_mask
= shader_glsl_append_dst(buffer
, ins
);
3067 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
3069 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
3070 * shader versions WINED3DSIO_MOVA is used for this. */
3071 if (ins
->ctx
->reg_maps
->shader_version
.major
== 1
3072 && ins
->ctx
->reg_maps
->shader_version
.type
== WINED3D_SHADER_TYPE_VERTEX
3073 && ins
->dst
[0].reg
.type
== WINED3DSPR_ADDR
)
3075 /* This is a simple floor() */
3076 unsigned int mask_size
= shader_glsl_get_write_mask_size(write_mask
);
3077 if (mask_size
> 1) {
3078 shader_addline(buffer
, "ivec%d(floor(%s)));\n", mask_size
, src0_param
.param_str
);
3080 shader_addline(buffer
, "int(floor(%s)));\n", src0_param
.param_str
);
3083 else if(ins
->handler_idx
== WINED3DSIH_MOVA
)
3085 /* We need to *round* to the nearest int here. */
3086 unsigned int mask_size
= shader_glsl_get_write_mask_size(write_mask
);
3088 if (gl_info
->supported
[EXT_GPU_SHADER4
])
3091 shader_addline(buffer
, "ivec%d(round(%s)));\n", mask_size
, src0_param
.param_str
);
3093 shader_addline(buffer
, "int(round(%s)));\n", src0_param
.param_str
);
3098 shader_addline(buffer
, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n",
3099 mask_size
, src0_param
.param_str
, mask_size
, src0_param
.param_str
);
3101 shader_addline(buffer
, "int(floor(abs(%s) + 0.5) * sign(%s)));\n",
3102 src0_param
.param_str
, src0_param
.param_str
);
3107 shader_addline(buffer
, "%s);\n", src0_param
.param_str
);
3111 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
3112 static void shader_glsl_dot(const struct wined3d_shader_instruction
*ins
)
3114 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
3115 struct glsl_src_param src0_param
;
3116 struct glsl_src_param src1_param
;
3117 DWORD dst_write_mask
, src_write_mask
;
3118 unsigned int dst_size
;
3120 dst_write_mask
= shader_glsl_append_dst(buffer
, ins
);
3121 dst_size
= shader_glsl_get_write_mask_size(dst_write_mask
);
3123 /* dp4 works on vec4, dp3 on vec3, etc. */
3124 if (ins
->handler_idx
== WINED3DSIH_DP4
)
3125 src_write_mask
= WINED3DSP_WRITEMASK_ALL
;
3126 else if (ins
->handler_idx
== WINED3DSIH_DP3
)
3127 src_write_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
3129 src_write_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
;
3131 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_write_mask
, &src0_param
);
3132 shader_glsl_add_src_param(ins
, &ins
->src
[1], src_write_mask
, &src1_param
);
3135 shader_addline(buffer
, "vec%d(dot(%s, %s)));\n", dst_size
, src0_param
.param_str
, src1_param
.param_str
);
3137 shader_addline(buffer
, "dot(%s, %s));\n", src0_param
.param_str
, src1_param
.param_str
);
3141 /* Note that this instruction has some restrictions. The destination write mask
3142 * can't contain the w component, and the source swizzles have to be .xyzw */
3143 static void shader_glsl_cross(const struct wined3d_shader_instruction
*ins
)
3145 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
3146 struct glsl_src_param src0_param
;
3147 struct glsl_src_param src1_param
;
3150 shader_glsl_get_write_mask(&ins
->dst
[0], dst_mask
);
3151 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
3152 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
3153 shader_glsl_add_src_param(ins
, &ins
->src
[1], src_mask
, &src1_param
);
3154 shader_addline(ins
->ctx
->buffer
, "cross(%s, %s)%s);\n", src0_param
.param_str
, src1_param
.param_str
, dst_mask
);
3157 static void shader_glsl_cut(const struct wined3d_shader_instruction
*ins
)
3159 shader_addline(ins
->ctx
->buffer
, "EndPrimitive();\n");
3162 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
3163 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
3164 * GLSL uses the value as-is. */
3165 static void shader_glsl_pow(const struct wined3d_shader_instruction
*ins
)
3167 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
3168 struct glsl_src_param src0_param
;
3169 struct glsl_src_param src1_param
;
3170 DWORD dst_write_mask
;
3171 unsigned int dst_size
;
3173 dst_write_mask
= shader_glsl_append_dst(buffer
, ins
);
3174 dst_size
= shader_glsl_get_write_mask_size(dst_write_mask
);
3176 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
3177 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_0
, &src1_param
);
3181 shader_addline(buffer
, "vec%u(%s == 0.0 ? 1.0 : pow(abs(%s), %s)));\n",
3182 dst_size
, src1_param
.param_str
, src0_param
.param_str
, src1_param
.param_str
);
3186 shader_addline(buffer
, "%s == 0.0 ? 1.0 : pow(abs(%s), %s));\n",
3187 src1_param
.param_str
, src0_param
.param_str
, src1_param
.param_str
);
3191 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
3192 static void shader_glsl_map2gl(const struct wined3d_shader_instruction
*ins
)
3194 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
3195 struct glsl_src_param src_param
;
3196 const char *instruction
;
3200 /* Determine the GLSL function to use based on the opcode */
3201 /* TODO: Possibly make this a table for faster lookups */
3202 switch (ins
->handler_idx
)
3204 case WINED3DSIH_ABS
: instruction
= "abs"; break;
3205 case WINED3DSIH_DSX
: instruction
= "dFdx"; break;
3206 case WINED3DSIH_DSY
: instruction
= "ycorrection.y * dFdy"; break;
3207 case WINED3DSIH_FRC
: instruction
= "fract"; break;
3208 case WINED3DSIH_IMAX
: instruction
= "max"; break;
3209 case WINED3DSIH_IMIN
: instruction
= "min"; break;
3210 case WINED3DSIH_MAX
: instruction
= "max"; break;
3211 case WINED3DSIH_MIN
: instruction
= "min"; break;
3212 case WINED3DSIH_ROUND_NI
: instruction
= "floor"; break;
3213 case WINED3DSIH_SQRT
: instruction
= "sqrt"; break;
3214 default: instruction
= "";
3215 FIXME("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins
->handler_idx
));
3219 write_mask
= shader_glsl_append_dst(buffer
, ins
);
3221 shader_addline(buffer
, "%s(", instruction
);
3225 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src_param
);
3226 shader_addline(buffer
, "%s", src_param
.param_str
);
3227 for (i
= 1; i
< ins
->src_count
; ++i
)
3229 shader_glsl_add_src_param(ins
, &ins
->src
[i
], write_mask
, &src_param
);
3230 shader_addline(buffer
, ", %s", src_param
.param_str
);
3234 shader_addline(buffer
, "));\n");
3237 static void shader_glsl_nop(const struct wined3d_shader_instruction
*ins
) {}
3239 static void shader_glsl_nrm(const struct wined3d_shader_instruction
*ins
)
3241 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
3242 struct glsl_src_param src_param
;
3243 unsigned int mask_size
;
3247 write_mask
= shader_glsl_get_write_mask(ins
->dst
, dst_mask
);
3248 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
3249 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src_param
);
3251 shader_addline(buffer
, "tmp0.x = dot(%s, %s);\n",
3252 src_param
.param_str
, src_param
.param_str
);
3253 shader_glsl_append_dst(buffer
, ins
);
3257 shader_addline(buffer
, "tmp0.x == 0.0 ? vec%u(0.0) : (%s * inversesqrt(tmp0.x)));\n",
3258 mask_size
, src_param
.param_str
);
3262 shader_addline(buffer
, "tmp0.x == 0.0 ? 0.0 : (%s * inversesqrt(tmp0.x)));\n",
3263 src_param
.param_str
);
3267 static void shader_glsl_scalar_op(const struct wined3d_shader_instruction
*ins
)
3269 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
3270 struct glsl_src_param src0_param
;
3271 const char *prefix
, *suffix
;
3272 unsigned int dst_size
;
3273 DWORD dst_write_mask
;
3275 dst_write_mask
= shader_glsl_append_dst(buffer
, ins
);
3276 dst_size
= shader_glsl_get_write_mask_size(dst_write_mask
);
3278 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_3
, &src0_param
);
3280 switch (ins
->handler_idx
)
3282 case WINED3DSIH_EXP
:
3283 case WINED3DSIH_EXPP
:
3288 case WINED3DSIH_LOG
:
3289 case WINED3DSIH_LOGP
:
3290 prefix
= "log2(abs(";
3294 case WINED3DSIH_RCP
:
3299 case WINED3DSIH_RSQ
:
3300 prefix
= "inversesqrt(abs(";
3307 FIXME("Unhandled instruction %#x.\n", ins
->handler_idx
);
3312 shader_addline(buffer
, "vec%u(%s%s%s));\n", dst_size
, prefix
, src0_param
.param_str
, suffix
);
3314 shader_addline(buffer
, "%s%s%s);\n", prefix
, src0_param
.param_str
, suffix
);
3317 /** Process the WINED3DSIO_EXPP instruction in GLSL:
3318 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
3319 * dst.x = 2^(floor(src))
3320 * dst.y = src - floor(src)
3321 * dst.z = 2^src (partial precision is allowed, but optional)
3323 * For 2.0 shaders, just do this (honoring writemask and swizzle):
3324 * dst = 2^src; (partial precision is allowed, but optional)
3326 static void shader_glsl_expp(const struct wined3d_shader_instruction
*ins
)
3328 if (ins
->ctx
->reg_maps
->shader_version
.major
< 2)
3330 struct glsl_src_param src_param
;
3333 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_3
, &src_param
);
3335 shader_addline(ins
->ctx
->buffer
, "tmp0.x = exp2(floor(%s));\n", src_param
.param_str
);
3336 shader_addline(ins
->ctx
->buffer
, "tmp0.y = %s - floor(%s);\n", src_param
.param_str
, src_param
.param_str
);
3337 shader_addline(ins
->ctx
->buffer
, "tmp0.z = exp2(%s);\n", src_param
.param_str
);
3338 shader_addline(ins
->ctx
->buffer
, "tmp0.w = 1.0;\n");
3340 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
3341 shader_glsl_get_write_mask(&ins
->dst
[0], dst_mask
);
3342 shader_addline(ins
->ctx
->buffer
, "tmp0%s);\n", dst_mask
);
3346 shader_glsl_scalar_op(ins
);
3349 static void shader_glsl_cast(const struct wined3d_shader_instruction
*ins
,
3350 const char *vector_constructor
, const char *scalar_constructor
)
3352 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
3353 struct glsl_src_param src_param
;
3354 unsigned int mask_size
;
3357 write_mask
= shader_glsl_append_dst(buffer
, ins
);
3358 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
3359 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src_param
);
3362 shader_addline(buffer
, "%s%u(%s));\n", vector_constructor
, mask_size
, src_param
.param_str
);
3364 shader_addline(buffer
, "%s(%s));\n", scalar_constructor
, src_param
.param_str
);
3367 static void shader_glsl_to_int(const struct wined3d_shader_instruction
*ins
)
3369 shader_glsl_cast(ins
, "ivec", "int");
3372 static void shader_glsl_to_uint(const struct wined3d_shader_instruction
*ins
)
3374 shader_glsl_cast(ins
, "uvec", "uint");
3377 static void shader_glsl_to_float(const struct wined3d_shader_instruction
*ins
)
3379 shader_glsl_cast(ins
, "vec", "float");
3382 /** Process signed comparison opcodes in GLSL. */
3383 static void shader_glsl_compare(const struct wined3d_shader_instruction
*ins
)
3385 struct glsl_src_param src0_param
;
3386 struct glsl_src_param src1_param
;
3388 unsigned int mask_size
;
3390 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
3391 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
3392 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
3393 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
3395 if (mask_size
> 1) {
3396 const char *compare
;
3398 switch(ins
->handler_idx
)
3400 case WINED3DSIH_SLT
: compare
= "lessThan"; break;
3401 case WINED3DSIH_SGE
: compare
= "greaterThanEqual"; break;
3402 default: compare
= "";
3403 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins
->handler_idx
));
3406 shader_addline(ins
->ctx
->buffer
, "vec%d(%s(%s, %s)));\n", mask_size
, compare
,
3407 src0_param
.param_str
, src1_param
.param_str
);
3409 switch(ins
->handler_idx
)
3411 case WINED3DSIH_SLT
:
3412 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
3413 * to return 0.0 but step returns 1.0 because step is not < x
3414 * An alternative is a bvec compare padded with an unused second component.
3415 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
3416 * issue. Playing with not() is not possible either because not() does not accept
3419 shader_addline(ins
->ctx
->buffer
, "(%s < %s) ? 1.0 : 0.0);\n",
3420 src0_param
.param_str
, src1_param
.param_str
);
3422 case WINED3DSIH_SGE
:
3423 /* Here we can use the step() function and safe a conditional */
3424 shader_addline(ins
->ctx
->buffer
, "step(%s, %s));\n", src1_param
.param_str
, src0_param
.param_str
);
3427 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins
->handler_idx
));
3433 static void shader_glsl_conditional_move(const struct wined3d_shader_instruction
*ins
)
3435 const char *condition_prefix
, *condition_suffix
;
3436 struct wined3d_shader_dst_param dst
;
3437 struct glsl_src_param src0_param
;
3438 struct glsl_src_param src1_param
;
3439 struct glsl_src_param src2_param
;
3440 BOOL temp_destination
= FALSE
;
3441 DWORD cmp_channel
= 0;
3446 switch (ins
->handler_idx
)
3448 case WINED3DSIH_CMP
:
3449 condition_prefix
= "";
3450 condition_suffix
= " >= 0.0";
3453 case WINED3DSIH_CND
:
3454 condition_prefix
= "";
3455 condition_suffix
= " > 0.5";
3458 case WINED3DSIH_MOVC
:
3459 condition_prefix
= "bool(";
3460 condition_suffix
= ")";
3464 FIXME("Unhandled instruction %#x.\n", ins
->handler_idx
);
3465 condition_prefix
= "<unhandled prefix>";
3466 condition_suffix
= "<unhandled suffix>";
3470 if (shader_is_scalar(&ins
->dst
[0].reg
) || shader_is_scalar(&ins
->src
[0].reg
))
3472 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
3473 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
3474 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
3475 shader_glsl_add_src_param(ins
, &ins
->src
[2], write_mask
, &src2_param
);
3477 shader_addline(ins
->ctx
->buffer
, "%s%s%s ? %s : %s);\n",
3478 condition_prefix
, src0_param
.param_str
, condition_suffix
,
3479 src1_param
.param_str
, src2_param
.param_str
);
3485 /* Splitting the instruction up in multiple lines imposes a problem:
3486 * The first lines may overwrite source parameters of the following lines.
3487 * Deal with that by using a temporary destination register if needed. */
3488 if ((ins
->src
[0].reg
.idx
[0].offset
== dst
.reg
.idx
[0].offset
3489 && ins
->src
[0].reg
.type
== dst
.reg
.type
)
3490 || (ins
->src
[1].reg
.idx
[0].offset
== dst
.reg
.idx
[0].offset
3491 && ins
->src
[1].reg
.type
== dst
.reg
.type
)
3492 || (ins
->src
[2].reg
.idx
[0].offset
== dst
.reg
.idx
[0].offset
3493 && ins
->src
[2].reg
.type
== dst
.reg
.type
))
3494 temp_destination
= TRUE
;
3496 /* Cycle through all source0 channels. */
3497 for (i
= 0; i
< 4; ++i
)
3500 /* Find the destination channels which use the current source0 channel. */
3501 for (j
= 0; j
< 4; ++j
)
3503 if (((ins
->src
[0].swizzle
>> (2 * j
)) & 0x3) == i
)
3505 write_mask
|= WINED3DSP_WRITEMASK_0
<< j
;
3506 cmp_channel
= WINED3DSP_WRITEMASK_0
<< j
;
3509 dst
.write_mask
= ins
->dst
[0].write_mask
& write_mask
;
3511 if (temp_destination
)
3513 if (!(write_mask
= shader_glsl_get_write_mask(&dst
, mask_char
)))
3515 shader_addline(ins
->ctx
->buffer
, "tmp0%s = (", mask_char
);
3517 else if (!(write_mask
= shader_glsl_append_dst_ext(ins
->ctx
->buffer
, ins
, &dst
, dst
.reg
.data_type
)))
3520 shader_glsl_add_src_param(ins
, &ins
->src
[0], cmp_channel
, &src0_param
);
3521 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
3522 shader_glsl_add_src_param(ins
, &ins
->src
[2], write_mask
, &src2_param
);
3524 shader_addline(ins
->ctx
->buffer
, "%s%s%s ? %s : %s);\n",
3525 condition_prefix
, src0_param
.param_str
, condition_suffix
,
3526 src1_param
.param_str
, src2_param
.param_str
);
3529 if (temp_destination
)
3531 shader_glsl_get_write_mask(&ins
->dst
[0], mask_char
);
3532 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
3533 shader_addline(ins
->ctx
->buffer
, "tmp0%s);\n", mask_char
);
3537 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
3538 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
3539 * the compare is done per component of src0. */
3540 static void shader_glsl_cnd(const struct wined3d_shader_instruction
*ins
)
3542 struct glsl_src_param src0_param
;
3543 struct glsl_src_param src1_param
;
3544 struct glsl_src_param src2_param
;
3546 DWORD shader_version
= WINED3D_SHADER_VERSION(ins
->ctx
->reg_maps
->shader_version
.major
,
3547 ins
->ctx
->reg_maps
->shader_version
.minor
);
3549 if (shader_version
< WINED3D_SHADER_VERSION(1, 4))
3551 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
3552 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
3553 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
3554 shader_glsl_add_src_param(ins
, &ins
->src
[2], write_mask
, &src2_param
);
3556 if (ins
->coissue
&& ins
->dst
->write_mask
!= WINED3DSP_WRITEMASK_3
)
3557 shader_addline(ins
->ctx
->buffer
, "%s /* COISSUE! */);\n", src1_param
.param_str
);
3559 shader_addline(ins
->ctx
->buffer
, "%s > 0.5 ? %s : %s);\n",
3560 src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
3564 shader_glsl_conditional_move(ins
);
3567 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
3568 static void shader_glsl_mad(const struct wined3d_shader_instruction
*ins
)
3570 struct glsl_src_param src0_param
;
3571 struct glsl_src_param src1_param
;
3572 struct glsl_src_param src2_param
;
3575 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
3576 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
3577 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
3578 shader_glsl_add_src_param(ins
, &ins
->src
[2], write_mask
, &src2_param
);
3579 shader_addline(ins
->ctx
->buffer
, "(%s * %s) + %s);\n",
3580 src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
3583 /* Handles transforming all WINED3DSIO_M?x? opcodes for
3584 Vertex shaders to GLSL codes */
3585 static void shader_glsl_mnxn(const struct wined3d_shader_instruction
*ins
)
3588 int nComponents
= 0;
3589 struct wined3d_shader_dst_param tmp_dst
= {{0}};
3590 struct wined3d_shader_src_param tmp_src
[2] = {{{0}}};
3591 struct wined3d_shader_instruction tmp_ins
;
3593 memset(&tmp_ins
, 0, sizeof(tmp_ins
));
3595 /* Set constants for the temporary argument */
3596 tmp_ins
.ctx
= ins
->ctx
;
3597 tmp_ins
.dst_count
= 1;
3598 tmp_ins
.dst
= &tmp_dst
;
3599 tmp_ins
.src_count
= 2;
3600 tmp_ins
.src
= tmp_src
;
3602 switch(ins
->handler_idx
)
3604 case WINED3DSIH_M4x4
:
3606 tmp_ins
.handler_idx
= WINED3DSIH_DP4
;
3608 case WINED3DSIH_M4x3
:
3610 tmp_ins
.handler_idx
= WINED3DSIH_DP4
;
3612 case WINED3DSIH_M3x4
:
3614 tmp_ins
.handler_idx
= WINED3DSIH_DP3
;
3616 case WINED3DSIH_M3x3
:
3618 tmp_ins
.handler_idx
= WINED3DSIH_DP3
;
3620 case WINED3DSIH_M3x2
:
3622 tmp_ins
.handler_idx
= WINED3DSIH_DP3
;
3628 tmp_dst
= ins
->dst
[0];
3629 tmp_src
[0] = ins
->src
[0];
3630 tmp_src
[1] = ins
->src
[1];
3631 for (i
= 0; i
< nComponents
; ++i
)
3633 tmp_dst
.write_mask
= WINED3DSP_WRITEMASK_0
<< i
;
3634 shader_glsl_dot(&tmp_ins
);
3635 ++tmp_src
[1].reg
.idx
[0].offset
;
3640 The LRP instruction performs a component-wise linear interpolation
3641 between the second and third operands using the first operand as the
3642 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
3643 This is equivalent to mix(src2, src1, src0);
3645 static void shader_glsl_lrp(const struct wined3d_shader_instruction
*ins
)
3647 struct glsl_src_param src0_param
;
3648 struct glsl_src_param src1_param
;
3649 struct glsl_src_param src2_param
;
3652 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
3654 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
3655 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
3656 shader_glsl_add_src_param(ins
, &ins
->src
[2], write_mask
, &src2_param
);
3658 shader_addline(ins
->ctx
->buffer
, "mix(%s, %s, %s));\n",
3659 src2_param
.param_str
, src1_param
.param_str
, src0_param
.param_str
);
3662 /** Process the WINED3DSIO_LIT instruction in GLSL:
3663 * dst.x = dst.w = 1.0
3664 * dst.y = (src0.x > 0) ? src0.x
3665 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
3666 * where src.w is clamped at +- 128
3668 static void shader_glsl_lit(const struct wined3d_shader_instruction
*ins
)
3670 struct glsl_src_param src0_param
;
3671 struct glsl_src_param src1_param
;
3672 struct glsl_src_param src3_param
;
3675 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
3676 shader_glsl_get_write_mask(&ins
->dst
[0], dst_mask
);
3678 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
3679 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_1
, &src1_param
);
3680 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_3
, &src3_param
);
3682 /* The sdk specifies the instruction like this
3684 * if(src.x > 0.0) dst.y = src.x
3686 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
3689 * (where power = src.w clamped between -128 and 128)
3691 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
3692 * dst.x = 1.0 ... No further explanation needed
3693 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
3694 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
3695 * dst.w = 1.0. ... Nothing fancy.
3697 * So we still have one conditional in there. So do this:
3698 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
3700 * 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),
3701 * 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.
3702 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to.
3704 * Unfortunately pow(0.0 ^ 0.0) returns NaN on most GPUs, but lit with src.y = 0 and src.w = 0 returns
3705 * a non-NaN value in dst.z. What we return doesn't matter, as long as it is not NaN. Return 0, which is
3706 * what all Windows HW drivers and GL_ARB_vertex_program's LIT do.
3708 shader_addline(ins
->ctx
->buffer
,
3709 "vec4(1.0, max(%s, 0.0), %s == 0.0 ? 0.0 : "
3710 "pow(max(0.0, %s) * step(0.0, %s), clamp(%s, -128.0, 128.0)), 1.0)%s);\n",
3711 src0_param
.param_str
, src3_param
.param_str
, src1_param
.param_str
,
3712 src0_param
.param_str
, src3_param
.param_str
, dst_mask
);
3715 /** Process the WINED3DSIO_DST instruction in GLSL:
3717 * dst.y = src0.x * src0.y
3721 static void shader_glsl_dst(const struct wined3d_shader_instruction
*ins
)
3723 struct glsl_src_param src0y_param
;
3724 struct glsl_src_param src0z_param
;
3725 struct glsl_src_param src1y_param
;
3726 struct glsl_src_param src1w_param
;
3729 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
3730 shader_glsl_get_write_mask(&ins
->dst
[0], dst_mask
);
3732 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_1
, &src0y_param
);
3733 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_2
, &src0z_param
);
3734 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_1
, &src1y_param
);
3735 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_3
, &src1w_param
);
3737 shader_addline(ins
->ctx
->buffer
, "vec4(1.0, %s * %s, %s, %s))%s;\n",
3738 src0y_param
.param_str
, src1y_param
.param_str
, src0z_param
.param_str
, src1w_param
.param_str
, dst_mask
);
3741 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
3742 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
3743 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
3745 * dst.x = cos(src0.?)
3746 * dst.y = sin(src0.?)
3750 static void shader_glsl_sincos(const struct wined3d_shader_instruction
*ins
)
3752 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
3753 struct glsl_src_param src0_param
;
3756 if (ins
->ctx
->reg_maps
->shader_version
.major
< 4)
3758 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
3760 write_mask
= shader_glsl_append_dst(buffer
, ins
);
3763 case WINED3DSP_WRITEMASK_0
:
3764 shader_addline(buffer
, "cos(%s));\n", src0_param
.param_str
);
3767 case WINED3DSP_WRITEMASK_1
:
3768 shader_addline(buffer
, "sin(%s));\n", src0_param
.param_str
);
3771 case (WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
):
3772 shader_addline(buffer
, "vec2(cos(%s), sin(%s)));\n",
3773 src0_param
.param_str
, src0_param
.param_str
);
3777 ERR("Write mask should be .x, .y or .xy\n");
3784 if (ins
->dst
[0].reg
.type
!= WINED3DSPR_NULL
)
3787 if (ins
->dst
[1].reg
.type
!= WINED3DSPR_NULL
)
3791 write_mask
= shader_glsl_get_write_mask(&ins
->dst
[0], dst_mask
);
3792 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
3793 shader_addline(buffer
, "tmp0%s = sin(%s);\n", dst_mask
, src0_param
.param_str
);
3795 write_mask
= shader_glsl_append_dst_ext(buffer
, ins
, &ins
->dst
[1], ins
->dst
[1].reg
.data_type
);
3796 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
3797 shader_addline(buffer
, "cos(%s));\n", src0_param
.param_str
);
3799 shader_glsl_append_dst_ext(buffer
, ins
, &ins
->dst
[0], ins
->dst
[0].reg
.data_type
);
3800 shader_addline(buffer
, "tmp0%s);\n", dst_mask
);
3804 write_mask
= shader_glsl_append_dst_ext(buffer
, ins
, &ins
->dst
[0], ins
->dst
[0].reg
.data_type
);
3805 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
3806 shader_addline(buffer
, "sin(%s));\n", src0_param
.param_str
);
3809 else if (ins
->dst
[1].reg
.type
!= WINED3DSPR_NULL
)
3811 write_mask
= shader_glsl_append_dst_ext(buffer
, ins
, &ins
->dst
[1], ins
->dst
[1].reg
.data_type
);
3812 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
3813 shader_addline(buffer
, "cos(%s));\n", src0_param
.param_str
);
3817 /* sgn in vs_2_0 has 2 extra parameters(registers for temporary storage) which we don't use
3818 * here. But those extra parameters require a dedicated function for sgn, since map2gl would
3819 * generate invalid code
3821 static void shader_glsl_sgn(const struct wined3d_shader_instruction
*ins
)
3823 struct glsl_src_param src0_param
;
3826 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
3827 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
3829 shader_addline(ins
->ctx
->buffer
, "sign(%s));\n", src0_param
.param_str
);
3832 /** Process the WINED3DSIO_LOOP instruction in GLSL:
3833 * Start a for() loop where src1.y is the initial value of aL,
3834 * increment aL by src1.z for a total of src1.x iterations.
3835 * Need to use a temporary variable for this operation.
3837 /* FIXME: I don't think nested loops will work correctly this way. */
3838 static void shader_glsl_loop(const struct wined3d_shader_instruction
*ins
)
3840 struct wined3d_shader_loop_state
*loop_state
= ins
->ctx
->loop_state
;
3841 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
3842 const struct wined3d_shader
*shader
= ins
->ctx
->shader
;
3843 const struct wined3d_shader_lconst
*constant
;
3844 struct glsl_src_param src1_param
;
3845 const DWORD
*control_values
= NULL
;
3847 if (ins
->ctx
->reg_maps
->shader_version
.major
< 4)
3849 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_ALL
, &src1_param
);
3851 /* Try to hardcode the loop control parameters if possible. Direct3D 9
3852 * class hardware doesn't support real varying indexing, but Microsoft
3853 * designed this feature for Shader model 2.x+. If the loop control is
3854 * known at compile time, the GLSL compiler can unroll the loop, and
3855 * replace indirect addressing with direct addressing. */
3856 if (ins
->src
[1].reg
.type
== WINED3DSPR_CONSTINT
)
3858 LIST_FOR_EACH_ENTRY(constant
, &shader
->constantsI
, struct wined3d_shader_lconst
, entry
)
3860 if (constant
->idx
== ins
->src
[1].reg
.idx
[0].offset
)
3862 control_values
= constant
->value
;
3870 struct wined3d_shader_loop_control loop_control
;
3871 loop_control
.count
= control_values
[0];
3872 loop_control
.start
= control_values
[1];
3873 loop_control
.step
= (int)control_values
[2];
3875 if (loop_control
.step
> 0)
3877 shader_addline(buffer
, "for (aL%u = %u; aL%u < (%u * %d + %u); aL%u += %d)\n{\n",
3878 loop_state
->current_depth
, loop_control
.start
,
3879 loop_state
->current_depth
, loop_control
.count
, loop_control
.step
, loop_control
.start
,
3880 loop_state
->current_depth
, loop_control
.step
);
3882 else if (loop_control
.step
< 0)
3884 shader_addline(buffer
, "for (aL%u = %u; aL%u > (%u * %d + %u); aL%u += %d)\n{\n",
3885 loop_state
->current_depth
, loop_control
.start
,
3886 loop_state
->current_depth
, loop_control
.count
, loop_control
.step
, loop_control
.start
,
3887 loop_state
->current_depth
, loop_control
.step
);
3891 shader_addline(buffer
, "for (aL%u = %u, tmpInt%u = 0; tmpInt%u < %u; tmpInt%u++)\n{\n",
3892 loop_state
->current_depth
, loop_control
.start
, loop_state
->current_depth
,
3893 loop_state
->current_depth
, loop_control
.count
,
3894 loop_state
->current_depth
);
3899 shader_addline(buffer
, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z)\n{\n",
3900 loop_state
->current_depth
, loop_state
->current_reg
,
3901 src1_param
.reg_name
, loop_state
->current_depth
, src1_param
.reg_name
,
3902 loop_state
->current_depth
, loop_state
->current_reg
, src1_param
.reg_name
);
3905 ++loop_state
->current_reg
;
3909 shader_addline(buffer
, "for (;;)\n{\n");
3912 ++loop_state
->current_depth
;
3915 static void shader_glsl_end(const struct wined3d_shader_instruction
*ins
)
3917 struct wined3d_shader_loop_state
*loop_state
= ins
->ctx
->loop_state
;
3919 shader_addline(ins
->ctx
->buffer
, "}\n");
3921 if (ins
->handler_idx
== WINED3DSIH_ENDLOOP
)
3923 --loop_state
->current_depth
;
3924 --loop_state
->current_reg
;
3927 if (ins
->handler_idx
== WINED3DSIH_ENDREP
)
3929 --loop_state
->current_depth
;
3933 static void shader_glsl_rep(const struct wined3d_shader_instruction
*ins
)
3935 const struct wined3d_shader
*shader
= ins
->ctx
->shader
;
3936 struct wined3d_shader_loop_state
*loop_state
= ins
->ctx
->loop_state
;
3937 const struct wined3d_shader_lconst
*constant
;
3938 struct glsl_src_param src0_param
;
3939 const DWORD
*control_values
= NULL
;
3941 /* Try to hardcode local values to help the GLSL compiler to unroll and optimize the loop */
3942 if (ins
->src
[0].reg
.type
== WINED3DSPR_CONSTINT
)
3944 LIST_FOR_EACH_ENTRY(constant
, &shader
->constantsI
, struct wined3d_shader_lconst
, entry
)
3946 if (constant
->idx
== ins
->src
[0].reg
.idx
[0].offset
)
3948 control_values
= constant
->value
;
3956 shader_addline(ins
->ctx
->buffer
, "for (tmpInt%d = 0; tmpInt%d < %d; tmpInt%d++) {\n",
3957 loop_state
->current_depth
, loop_state
->current_depth
,
3958 control_values
[0], loop_state
->current_depth
);
3962 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
3963 shader_addline(ins
->ctx
->buffer
, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
3964 loop_state
->current_depth
, loop_state
->current_depth
,
3965 src0_param
.param_str
, loop_state
->current_depth
);
3968 ++loop_state
->current_depth
;
3971 static void shader_glsl_if(const struct wined3d_shader_instruction
*ins
)
3973 struct glsl_src_param src0_param
;
3975 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
3976 shader_addline(ins
->ctx
->buffer
, "if (bool(%s)) {\n", src0_param
.param_str
);
3979 static void shader_glsl_ifc(const struct wined3d_shader_instruction
*ins
)
3981 struct glsl_src_param src0_param
;
3982 struct glsl_src_param src1_param
;
3984 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
3985 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_0
, &src1_param
);
3987 shader_addline(ins
->ctx
->buffer
, "if (%s %s %s) {\n",
3988 src0_param
.param_str
, shader_glsl_get_rel_op(ins
->flags
), src1_param
.param_str
);
3991 static void shader_glsl_else(const struct wined3d_shader_instruction
*ins
)
3993 shader_addline(ins
->ctx
->buffer
, "} else {\n");
3996 static void shader_glsl_emit(const struct wined3d_shader_instruction
*ins
)
3998 shader_addline(ins
->ctx
->buffer
, "EmitVertex();\n");
4001 static void shader_glsl_break(const struct wined3d_shader_instruction
*ins
)
4003 shader_addline(ins
->ctx
->buffer
, "break;\n");
4006 /* FIXME: According to MSDN the compare is done per component. */
4007 static void shader_glsl_breakc(const struct wined3d_shader_instruction
*ins
)
4009 struct glsl_src_param src0_param
;
4010 struct glsl_src_param src1_param
;
4012 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
4013 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_0
, &src1_param
);
4015 shader_addline(ins
->ctx
->buffer
, "if (%s %s %s) break;\n",
4016 src0_param
.param_str
, shader_glsl_get_rel_op(ins
->flags
), src1_param
.param_str
);
4019 static void shader_glsl_breakp(const struct wined3d_shader_instruction
*ins
)
4021 struct glsl_src_param src_param
;
4023 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src_param
);
4024 shader_addline(ins
->ctx
->buffer
, "if (bool(%s)) break;\n", src_param
.param_str
);
4027 static void shader_glsl_label(const struct wined3d_shader_instruction
*ins
)
4029 shader_addline(ins
->ctx
->buffer
, "}\n");
4030 shader_addline(ins
->ctx
->buffer
, "void subroutine%u()\n{\n", ins
->src
[0].reg
.idx
[0].offset
);
4033 static void shader_glsl_call(const struct wined3d_shader_instruction
*ins
)
4035 shader_addline(ins
->ctx
->buffer
, "subroutine%u();\n", ins
->src
[0].reg
.idx
[0].offset
);
4038 static void shader_glsl_callnz(const struct wined3d_shader_instruction
*ins
)
4040 struct glsl_src_param src1_param
;
4042 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_0
, &src1_param
);
4043 shader_addline(ins
->ctx
->buffer
, "if (%s) subroutine%u();\n",
4044 src1_param
.param_str
, ins
->src
[0].reg
.idx
[0].offset
);
4047 static void shader_glsl_ret(const struct wined3d_shader_instruction
*ins
)
4049 /* No-op. The closing } is written when a new function is started, and at the end of the shader. This
4050 * function only suppresses the unhandled instruction warning
4054 /*********************************************
4055 * Pixel Shader Specific Code begins here
4056 ********************************************/
4057 static void shader_glsl_tex(const struct wined3d_shader_instruction
*ins
)
4059 DWORD shader_version
= WINED3D_SHADER_VERSION(ins
->ctx
->reg_maps
->shader_version
.major
,
4060 ins
->ctx
->reg_maps
->shader_version
.minor
);
4061 struct glsl_sample_function sample_function
;
4062 DWORD sample_flags
= 0;
4064 DWORD mask
= 0, swizzle
;
4065 const struct shader_glsl_ctx_priv
*priv
= ins
->ctx
->backend_data
;
4067 /* 1.0-1.4: Use destination register as sampler source.
4068 * 2.0+: Use provided sampler source. */
4069 if (shader_version
< WINED3D_SHADER_VERSION(2,0))
4070 resource_idx
= ins
->dst
[0].reg
.idx
[0].offset
;
4072 resource_idx
= ins
->src
[1].reg
.idx
[0].offset
;
4074 if (shader_version
< WINED3D_SHADER_VERSION(1,4))
4076 DWORD flags
= (priv
->cur_ps_args
->tex_transform
>> resource_idx
* WINED3D_PSARGS_TEXTRANSFORM_SHIFT
)
4077 & WINED3D_PSARGS_TEXTRANSFORM_MASK
;
4078 enum wined3d_shader_resource_type resource_type
= ins
->ctx
->reg_maps
->resource_info
[resource_idx
].type
;
4080 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
4081 if (flags
& WINED3D_PSARGS_PROJECTED
&& resource_type
!= WINED3D_SHADER_RESOURCE_TEXTURE_CUBE
)
4083 sample_flags
|= WINED3D_GLSL_SAMPLE_PROJECTED
;
4084 switch (flags
& ~WINED3D_PSARGS_PROJECTED
)
4086 case WINED3D_TTFF_COUNT1
:
4087 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
4089 case WINED3D_TTFF_COUNT2
:
4090 mask
= WINED3DSP_WRITEMASK_1
;
4092 case WINED3D_TTFF_COUNT3
:
4093 mask
= WINED3DSP_WRITEMASK_2
;
4095 case WINED3D_TTFF_COUNT4
:
4096 case WINED3D_TTFF_DISABLE
:
4097 mask
= WINED3DSP_WRITEMASK_3
;
4102 else if (shader_version
< WINED3D_SHADER_VERSION(2,0))
4104 enum wined3d_shader_src_modifier src_mod
= ins
->src
[0].modifiers
;
4106 if (src_mod
== WINED3DSPSM_DZ
) {
4107 sample_flags
|= WINED3D_GLSL_SAMPLE_PROJECTED
;
4108 mask
= WINED3DSP_WRITEMASK_2
;
4109 } else if (src_mod
== WINED3DSPSM_DW
) {
4110 sample_flags
|= WINED3D_GLSL_SAMPLE_PROJECTED
;
4111 mask
= WINED3DSP_WRITEMASK_3
;
4116 if ((ins
->flags
& WINED3DSI_TEXLD_PROJECT
)
4117 && ins
->ctx
->reg_maps
->resource_info
[resource_idx
].type
!= WINED3D_SHADER_RESOURCE_TEXTURE_CUBE
)
4119 /* ps 2.0 texldp instruction always divides by the fourth component. */
4120 sample_flags
|= WINED3D_GLSL_SAMPLE_PROJECTED
;
4121 mask
= WINED3DSP_WRITEMASK_3
;
4125 if (priv
->cur_ps_args
->np2_fixup
& (1u << resource_idx
))
4126 sample_flags
|= WINED3D_GLSL_SAMPLE_NPOT
;
4128 shader_glsl_get_sample_function(ins
->ctx
, resource_idx
, sample_flags
, &sample_function
);
4129 mask
|= sample_function
.coord_mask
;
4130 sample_function
.coord_mask
= mask
;
4132 if (shader_version
< WINED3D_SHADER_VERSION(2,0)) swizzle
= WINED3DSP_NOSWIZZLE
;
4133 else swizzle
= ins
->src
[1].swizzle
;
4135 /* 1.0-1.3: Use destination register as coordinate source.
4136 1.4+: Use provided coordinate source register. */
4137 if (shader_version
< WINED3D_SHADER_VERSION(1,4))
4140 shader_glsl_write_mask_to_str(mask
, coord_mask
);
4141 shader_glsl_gen_sample_code(ins
, resource_idx
, &sample_function
, swizzle
, NULL
, NULL
, NULL
,
4142 "T%u%s", resource_idx
, coord_mask
);
4146 struct glsl_src_param coord_param
;
4147 shader_glsl_add_src_param(ins
, &ins
->src
[0], mask
, &coord_param
);
4148 if (ins
->flags
& WINED3DSI_TEXLD_BIAS
)
4150 struct glsl_src_param bias
;
4151 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_3
, &bias
);
4152 shader_glsl_gen_sample_code(ins
, resource_idx
, &sample_function
, swizzle
, NULL
, NULL
, bias
.param_str
,
4153 "%s", coord_param
.param_str
);
4155 shader_glsl_gen_sample_code(ins
, resource_idx
, &sample_function
, swizzle
, NULL
, NULL
, NULL
,
4156 "%s", coord_param
.param_str
);
4159 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
4162 static void shader_glsl_texldd(const struct wined3d_shader_instruction
*ins
)
4164 const struct wined3d_gl_info
*gl_info
= ins
->ctx
->gl_info
;
4165 struct glsl_src_param coord_param
, dx_param
, dy_param
;
4166 DWORD sample_flags
= WINED3D_GLSL_SAMPLE_GRAD
;
4167 struct glsl_sample_function sample_function
;
4169 DWORD swizzle
= ins
->src
[1].swizzle
;
4170 const struct shader_glsl_ctx_priv
*priv
= ins
->ctx
->backend_data
;
4172 if (!gl_info
->supported
[ARB_SHADER_TEXTURE_LOD
] && !gl_info
->supported
[EXT_GPU_SHADER4
])
4174 FIXME("texldd used, but not supported by hardware. Falling back to regular tex\n");
4175 shader_glsl_tex(ins
);
4179 sampler_idx
= ins
->src
[1].reg
.idx
[0].offset
;
4180 if (priv
->cur_ps_args
->np2_fixup
& (1u << sampler_idx
))
4181 sample_flags
|= WINED3D_GLSL_SAMPLE_NPOT
;
4183 shader_glsl_get_sample_function(ins
->ctx
, sampler_idx
, sample_flags
, &sample_function
);
4184 shader_glsl_add_src_param(ins
, &ins
->src
[0], sample_function
.coord_mask
, &coord_param
);
4185 shader_glsl_add_src_param(ins
, &ins
->src
[2], sample_function
.coord_mask
, &dx_param
);
4186 shader_glsl_add_src_param(ins
, &ins
->src
[3], sample_function
.coord_mask
, &dy_param
);
4188 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, swizzle
, dx_param
.param_str
, dy_param
.param_str
, NULL
,
4189 "%s", coord_param
.param_str
);
4190 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
4193 static void shader_glsl_texldl(const struct wined3d_shader_instruction
*ins
)
4195 const struct wined3d_gl_info
*gl_info
= ins
->ctx
->gl_info
;
4196 struct glsl_src_param coord_param
, lod_param
;
4197 DWORD sample_flags
= WINED3D_GLSL_SAMPLE_LOD
;
4198 struct glsl_sample_function sample_function
;
4200 DWORD swizzle
= ins
->src
[1].swizzle
;
4201 const struct shader_glsl_ctx_priv
*priv
= ins
->ctx
->backend_data
;
4203 sampler_idx
= ins
->src
[1].reg
.idx
[0].offset
;
4204 if (ins
->ctx
->reg_maps
->shader_version
.type
== WINED3D_SHADER_TYPE_PIXEL
4205 && priv
->cur_ps_args
->np2_fixup
& (1u << sampler_idx
))
4206 sample_flags
|= WINED3D_GLSL_SAMPLE_NPOT
;
4208 shader_glsl_get_sample_function(ins
->ctx
, sampler_idx
, sample_flags
, &sample_function
);
4209 shader_glsl_add_src_param(ins
, &ins
->src
[0], sample_function
.coord_mask
, &coord_param
);
4211 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_3
, &lod_param
);
4213 if (!gl_info
->supported
[ARB_SHADER_TEXTURE_LOD
] && !gl_info
->supported
[EXT_GPU_SHADER4
]
4214 && ins
->ctx
->reg_maps
->shader_version
.type
== WINED3D_SHADER_TYPE_PIXEL
)
4216 /* Plain GLSL only supports Lod sampling functions in vertex shaders.
4217 * However, the NVIDIA drivers allow them in fragment shaders as well,
4218 * even without the appropriate extension. */
4219 WARN("Using %s in fragment shader.\n", sample_function
.name
->buffer
);
4221 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, swizzle
, NULL
, NULL
, lod_param
.param_str
,
4222 "%s", coord_param
.param_str
);
4223 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
4226 static unsigned int shader_glsl_find_sampler(const struct wined3d_shader_sampler_map
*sampler_map
,
4227 unsigned int resource_idx
, unsigned int sampler_idx
)
4229 struct wined3d_shader_sampler_map_entry
*entries
= sampler_map
->entries
;
4232 for (i
= 0; i
< sampler_map
->count
; ++i
)
4234 if (entries
[i
].resource_idx
== resource_idx
&& entries
[i
].sampler_idx
== sampler_idx
)
4235 return entries
[i
].bind_idx
;
4238 ERR("No GLSL sampler found for resource %u / sampler %u.\n", resource_idx
, sampler_idx
);
4243 /* FIXME: The current implementation does not handle multisample textures correctly. */
4244 static void shader_glsl_ld(const struct wined3d_shader_instruction
*ins
)
4246 struct glsl_src_param coord_param
, lod_param
;
4247 struct glsl_sample_function sample_function
;
4248 unsigned int sampler_idx
;
4250 shader_glsl_get_sample_function(ins
->ctx
, ins
->src
[1].reg
.idx
[0].offset
, WINED3D_GLSL_SAMPLE_LOAD
,
4252 shader_glsl_add_src_param(ins
, &ins
->src
[0], sample_function
.coord_mask
, &coord_param
);
4253 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_3
, &lod_param
);
4254 sampler_idx
= shader_glsl_find_sampler(&ins
->ctx
->reg_maps
->sampler_map
,
4255 ins
->src
[1].reg
.idx
[0].offset
, WINED3D_SAMPLER_DEFAULT
);
4256 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
,
4257 NULL
, NULL
, lod_param
.param_str
, "%s", coord_param
.param_str
);
4258 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
4261 static void shader_glsl_sample(const struct wined3d_shader_instruction
*ins
)
4263 struct glsl_sample_function sample_function
;
4264 struct glsl_src_param coord_param
;
4265 unsigned int sampler_idx
;
4267 shader_glsl_get_sample_function(ins
->ctx
, ins
->src
[1].reg
.idx
[0].offset
, 0, &sample_function
);
4268 shader_glsl_add_src_param(ins
, &ins
->src
[0], sample_function
.coord_mask
, &coord_param
);
4269 sampler_idx
= shader_glsl_find_sampler(&ins
->ctx
->reg_maps
->sampler_map
,
4270 ins
->src
[1].reg
.idx
[0].offset
, ins
->src
[2].reg
.idx
[0].offset
);
4271 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
,
4272 NULL
, NULL
, NULL
, "%s", coord_param
.param_str
);
4273 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
4276 static void shader_glsl_sample_lod(const struct wined3d_shader_instruction
*ins
)
4278 struct glsl_src_param coord_param
, lod_param
;
4279 struct glsl_sample_function sample_function
;
4280 unsigned int sampler_idx
;
4282 shader_glsl_get_sample_function(ins
->ctx
, ins
->src
[1].reg
.idx
[0].offset
,
4283 WINED3D_GLSL_SAMPLE_LOD
, &sample_function
);
4284 shader_glsl_add_src_param(ins
, &ins
->src
[0], sample_function
.coord_mask
, &coord_param
);
4285 shader_glsl_add_src_param(ins
, &ins
->src
[3], WINED3DSP_WRITEMASK_0
, &lod_param
);
4286 sampler_idx
= shader_glsl_find_sampler(&ins
->ctx
->reg_maps
->sampler_map
,
4287 ins
->src
[1].reg
.idx
[0].offset
, ins
->src
[2].reg
.idx
[0].offset
);
4288 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
,
4289 NULL
, NULL
, lod_param
.param_str
, "%s", coord_param
.param_str
);
4290 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
4293 static void shader_glsl_texcoord(const struct wined3d_shader_instruction
*ins
)
4295 /* FIXME: Make this work for more than just 2D textures */
4296 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
4297 DWORD write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
4299 if (!(ins
->ctx
->reg_maps
->shader_version
.major
== 1 && ins
->ctx
->reg_maps
->shader_version
.minor
== 4))
4303 shader_glsl_get_write_mask(&ins
->dst
[0], dst_mask
);
4304 shader_addline(buffer
, "clamp(ffp_texcoord[%u], 0.0, 1.0)%s);\n",
4305 ins
->dst
[0].reg
.idx
[0].offset
, dst_mask
);
4309 enum wined3d_shader_src_modifier src_mod
= ins
->src
[0].modifiers
;
4310 DWORD reg
= ins
->src
[0].reg
.idx
[0].offset
;
4311 char dst_swizzle
[6];
4313 shader_glsl_get_swizzle(&ins
->src
[0], FALSE
, write_mask
, dst_swizzle
);
4315 if (src_mod
== WINED3DSPSM_DZ
|| src_mod
== WINED3DSPSM_DW
)
4317 unsigned int mask_size
= shader_glsl_get_write_mask_size(write_mask
);
4318 struct glsl_src_param div_param
;
4319 DWORD src_writemask
= src_mod
== WINED3DSPSM_DZ
? WINED3DSP_WRITEMASK_2
: WINED3DSP_WRITEMASK_3
;
4321 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_writemask
, &div_param
);
4324 shader_addline(buffer
, "ffp_texcoord[%u]%s / vec%d(%s));\n", reg
, dst_swizzle
, mask_size
, div_param
.param_str
);
4326 shader_addline(buffer
, "ffp_texcoord[%u]%s / %s);\n", reg
, dst_swizzle
, div_param
.param_str
);
4330 shader_addline(buffer
, "ffp_texcoord[%u]%s);\n", reg
, dst_swizzle
);
4335 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
4336 * Take a 3-component dot product of the TexCoord[dstreg] and src,
4337 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
4338 static void shader_glsl_texdp3tex(const struct wined3d_shader_instruction
*ins
)
4340 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
4341 DWORD sampler_idx
= ins
->dst
[0].reg
.idx
[0].offset
;
4342 struct glsl_sample_function sample_function
;
4343 struct glsl_src_param src0_param
;
4346 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
4348 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
4349 * scalar, and projected sampling would require 4.
4351 * It is a dependent read - not valid with conditional NP2 textures
4353 shader_glsl_get_sample_function(ins
->ctx
, sampler_idx
, 0, &sample_function
);
4354 mask_size
= shader_glsl_get_write_mask_size(sample_function
.coord_mask
);
4359 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
,
4360 "dot(ffp_texcoord[%u].xyz, %s)", sampler_idx
, src0_param
.param_str
);
4364 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
,
4365 "vec2(dot(ffp_texcoord[%u].xyz, %s), 0.0)", sampler_idx
, src0_param
.param_str
);
4369 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
,
4370 "vec3(dot(ffp_texcoord[%u].xyz, %s), 0.0, 0.0)", sampler_idx
, src0_param
.param_str
);
4374 FIXME("Unexpected mask size %u\n", mask_size
);
4377 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
4380 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
4381 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
4382 static void shader_glsl_texdp3(const struct wined3d_shader_instruction
*ins
)
4384 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
4385 DWORD dstreg
= ins
->dst
[0].reg
.idx
[0].offset
;
4386 struct glsl_src_param src0_param
;
4388 unsigned int mask_size
;
4390 dst_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
4391 mask_size
= shader_glsl_get_write_mask_size(dst_mask
);
4392 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
4394 if (mask_size
> 1) {
4395 shader_addline(ins
->ctx
->buffer
, "vec%d(dot(T%u.xyz, %s)));\n", mask_size
, dstreg
, src0_param
.param_str
);
4397 shader_addline(ins
->ctx
->buffer
, "dot(T%u.xyz, %s));\n", dstreg
, src0_param
.param_str
);
4401 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
4402 * Calculate the depth as dst.x / dst.y */
4403 static void shader_glsl_texdepth(const struct wined3d_shader_instruction
*ins
)
4405 struct glsl_dst_param dst_param
;
4407 shader_glsl_add_dst_param(ins
, &ins
->dst
[0], &dst_param
);
4409 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
4410 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
4411 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
4412 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
4415 shader_addline(ins
->ctx
->buffer
, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n",
4416 dst_param
.reg_name
, dst_param
.reg_name
);
4419 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
4420 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
4421 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
4422 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
4424 static void shader_glsl_texm3x2depth(const struct wined3d_shader_instruction
*ins
)
4426 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
4427 DWORD dstreg
= ins
->dst
[0].reg
.idx
[0].offset
;
4428 struct glsl_src_param src0_param
;
4430 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
4432 shader_addline(ins
->ctx
->buffer
, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg
, src0_param
.param_str
);
4433 shader_addline(ins
->ctx
->buffer
, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
4436 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
4437 * Calculate the 1st of a 2-row matrix multiplication. */
4438 static void shader_glsl_texm3x2pad(const struct wined3d_shader_instruction
*ins
)
4440 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
4441 DWORD reg
= ins
->dst
[0].reg
.idx
[0].offset
;
4442 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
4443 struct glsl_src_param src0_param
;
4445 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
4446 shader_addline(buffer
, "tmp0.x = dot(T%u.xyz, %s);\n", reg
, src0_param
.param_str
);
4449 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
4450 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
4451 static void shader_glsl_texm3x3pad(const struct wined3d_shader_instruction
*ins
)
4453 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
4454 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
4455 struct wined3d_shader_tex_mx
*tex_mx
= ins
->ctx
->tex_mx
;
4456 DWORD reg
= ins
->dst
[0].reg
.idx
[0].offset
;
4457 struct glsl_src_param src0_param
;
4459 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
4460 shader_addline(buffer
, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + tex_mx
->current_row
, reg
, src0_param
.param_str
);
4461 tex_mx
->texcoord_w
[tex_mx
->current_row
++] = reg
;
4464 static void shader_glsl_texm3x2tex(const struct wined3d_shader_instruction
*ins
)
4466 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
4467 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
4468 struct glsl_sample_function sample_function
;
4469 DWORD reg
= ins
->dst
[0].reg
.idx
[0].offset
;
4470 struct glsl_src_param src0_param
;
4472 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
4473 shader_addline(buffer
, "tmp0.y = dot(T%u.xyz, %s);\n", reg
, src0_param
.param_str
);
4475 shader_glsl_get_sample_function(ins
->ctx
, reg
, 0, &sample_function
);
4477 /* Sample the texture using the calculated coordinates */
4478 shader_glsl_gen_sample_code(ins
, reg
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
, "tmp0.xy");
4479 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
4482 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
4483 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
4484 static void shader_glsl_texm3x3tex(const struct wined3d_shader_instruction
*ins
)
4486 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
4487 struct wined3d_shader_tex_mx
*tex_mx
= ins
->ctx
->tex_mx
;
4488 struct glsl_sample_function sample_function
;
4489 DWORD reg
= ins
->dst
[0].reg
.idx
[0].offset
;
4490 struct glsl_src_param src0_param
;
4492 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
4493 shader_addline(ins
->ctx
->buffer
, "tmp0.z = dot(T%u.xyz, %s);\n", reg
, src0_param
.param_str
);
4495 /* Dependent read, not valid with conditional NP2 */
4496 shader_glsl_get_sample_function(ins
->ctx
, reg
, 0, &sample_function
);
4498 /* Sample the texture using the calculated coordinates */
4499 shader_glsl_gen_sample_code(ins
, reg
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
, "tmp0.xyz");
4500 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
4502 tex_mx
->current_row
= 0;
4505 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
4506 * Perform the 3rd row of a 3x3 matrix multiply */
4507 static void shader_glsl_texm3x3(const struct wined3d_shader_instruction
*ins
)
4509 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
4510 struct wined3d_shader_tex_mx
*tex_mx
= ins
->ctx
->tex_mx
;
4511 DWORD reg
= ins
->dst
[0].reg
.idx
[0].offset
;
4512 struct glsl_src_param src0_param
;
4515 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
4517 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
4518 shader_glsl_get_write_mask(&ins
->dst
[0], dst_mask
);
4519 shader_addline(ins
->ctx
->buffer
, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg
, src0_param
.param_str
, dst_mask
);
4521 tex_mx
->current_row
= 0;
4524 /* Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
4525 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
4526 static void shader_glsl_texm3x3spec(const struct wined3d_shader_instruction
*ins
)
4528 struct glsl_src_param src0_param
;
4529 struct glsl_src_param src1_param
;
4530 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
4531 struct wined3d_shader_tex_mx
*tex_mx
= ins
->ctx
->tex_mx
;
4532 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
4533 struct glsl_sample_function sample_function
;
4534 DWORD reg
= ins
->dst
[0].reg
.idx
[0].offset
;
4537 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
4538 shader_glsl_add_src_param(ins
, &ins
->src
[1], src_mask
, &src1_param
);
4540 /* Perform the last matrix multiply operation */
4541 shader_addline(buffer
, "tmp0.z = dot(T%u.xyz, %s);\n", reg
, src0_param
.param_str
);
4542 /* Reflection calculation */
4543 shader_addline(buffer
, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param
.param_str
);
4545 /* Dependent read, not valid with conditional NP2 */
4546 shader_glsl_get_sample_function(ins
->ctx
, reg
, 0, &sample_function
);
4547 shader_glsl_write_mask_to_str(sample_function
.coord_mask
, coord_mask
);
4549 /* Sample the texture */
4550 shader_glsl_gen_sample_code(ins
, reg
, &sample_function
, WINED3DSP_NOSWIZZLE
,
4551 NULL
, NULL
, NULL
, "tmp0%s", coord_mask
);
4552 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
4554 tex_mx
->current_row
= 0;
4557 /* Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
4558 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
4559 static void shader_glsl_texm3x3vspec(const struct wined3d_shader_instruction
*ins
)
4561 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
4562 struct wined3d_shader_tex_mx
*tex_mx
= ins
->ctx
->tex_mx
;
4563 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
4564 struct glsl_sample_function sample_function
;
4565 DWORD reg
= ins
->dst
[0].reg
.idx
[0].offset
;
4566 struct glsl_src_param src0_param
;
4569 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
4571 /* Perform the last matrix multiply operation */
4572 shader_addline(buffer
, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg
, src0_param
.param_str
);
4574 /* Construct the eye-ray vector from w coordinates */
4575 shader_addline(buffer
, "tmp1.xyz = normalize(vec3(ffp_texcoord[%u].w, ffp_texcoord[%u].w, ffp_texcoord[%u].w));\n",
4576 tex_mx
->texcoord_w
[0], tex_mx
->texcoord_w
[1], reg
);
4577 shader_addline(buffer
, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
4579 /* Dependent read, not valid with conditional NP2 */
4580 shader_glsl_get_sample_function(ins
->ctx
, reg
, 0, &sample_function
);
4581 shader_glsl_write_mask_to_str(sample_function
.coord_mask
, coord_mask
);
4583 /* Sample the texture using the calculated coordinates */
4584 shader_glsl_gen_sample_code(ins
, reg
, &sample_function
, WINED3DSP_NOSWIZZLE
,
4585 NULL
, NULL
, NULL
, "tmp0%s", coord_mask
);
4586 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
4588 tex_mx
->current_row
= 0;
4591 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
4592 * Apply a fake bump map transform.
4593 * texbem is pshader <= 1.3 only, this saves a few version checks
4595 static void shader_glsl_texbem(const struct wined3d_shader_instruction
*ins
)
4597 const struct shader_glsl_ctx_priv
*priv
= ins
->ctx
->backend_data
;
4598 struct glsl_sample_function sample_function
;
4599 struct glsl_src_param coord_param
;
4605 sampler_idx
= ins
->dst
[0].reg
.idx
[0].offset
;
4606 flags
= (priv
->cur_ps_args
->tex_transform
>> sampler_idx
* WINED3D_PSARGS_TEXTRANSFORM_SHIFT
)
4607 & WINED3D_PSARGS_TEXTRANSFORM_MASK
;
4609 /* Dependent read, not valid with conditional NP2 */
4610 shader_glsl_get_sample_function(ins
->ctx
, sampler_idx
, 0, &sample_function
);
4611 mask
= sample_function
.coord_mask
;
4613 shader_glsl_write_mask_to_str(mask
, coord_mask
);
4615 /* With projected textures, texbem only divides the static texture coord,
4616 * not the displacement, so we can't let GL handle this. */
4617 if (flags
& WINED3D_PSARGS_PROJECTED
)
4620 char coord_div_mask
[3];
4621 switch (flags
& ~WINED3D_PSARGS_PROJECTED
)
4623 case WINED3D_TTFF_COUNT1
:
4624 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
4626 case WINED3D_TTFF_COUNT2
:
4627 div_mask
= WINED3DSP_WRITEMASK_1
;
4629 case WINED3D_TTFF_COUNT3
:
4630 div_mask
= WINED3DSP_WRITEMASK_2
;
4632 case WINED3D_TTFF_COUNT4
:
4633 case WINED3D_TTFF_DISABLE
:
4634 div_mask
= WINED3DSP_WRITEMASK_3
;
4637 shader_glsl_write_mask_to_str(div_mask
, coord_div_mask
);
4638 shader_addline(ins
->ctx
->buffer
, "T%u%s /= T%u%s;\n", sampler_idx
, coord_mask
, sampler_idx
, coord_div_mask
);
4641 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
, &coord_param
);
4643 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
,
4644 "T%u%s + vec4(bumpenv_mat%u * %s, 0.0, 0.0)%s", sampler_idx
, coord_mask
, sampler_idx
,
4645 coord_param
.param_str
, coord_mask
);
4647 if (ins
->handler_idx
== WINED3DSIH_TEXBEML
)
4649 struct glsl_src_param luminance_param
;
4650 struct glsl_dst_param dst_param
;
4652 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_2
, &luminance_param
);
4653 shader_glsl_add_dst_param(ins
, &ins
->dst
[0], &dst_param
);
4655 shader_addline(ins
->ctx
->buffer
, "%s%s *= (%s * bumpenv_lum_scale%u + bumpenv_lum_offset%u);\n",
4656 dst_param
.reg_name
, dst_param
.mask_str
,
4657 luminance_param
.param_str
, sampler_idx
, sampler_idx
);
4659 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
4662 static void shader_glsl_bem(const struct wined3d_shader_instruction
*ins
)
4664 DWORD sampler_idx
= ins
->dst
[0].reg
.idx
[0].offset
;
4665 struct glsl_src_param src0_param
, src1_param
;
4667 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
, &src0_param
);
4668 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
, &src1_param
);
4670 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
4671 shader_addline(ins
->ctx
->buffer
, "%s + bumpenv_mat%u * %s);\n",
4672 src0_param
.param_str
, sampler_idx
, src1_param
.param_str
);
4675 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
4676 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
4677 static void shader_glsl_texreg2ar(const struct wined3d_shader_instruction
*ins
)
4679 DWORD sampler_idx
= ins
->dst
[0].reg
.idx
[0].offset
;
4680 struct glsl_sample_function sample_function
;
4681 struct glsl_src_param src0_param
;
4683 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_ALL
, &src0_param
);
4685 shader_glsl_get_sample_function(ins
->ctx
, sampler_idx
, 0, &sample_function
);
4686 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
,
4687 "%s.wx", src0_param
.reg_name
);
4688 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
4691 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
4692 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
4693 static void shader_glsl_texreg2gb(const struct wined3d_shader_instruction
*ins
)
4695 DWORD sampler_idx
= ins
->dst
[0].reg
.idx
[0].offset
;
4696 struct glsl_sample_function sample_function
;
4697 struct glsl_src_param src0_param
;
4699 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_ALL
, &src0_param
);
4701 shader_glsl_get_sample_function(ins
->ctx
, sampler_idx
, 0, &sample_function
);
4702 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
,
4703 "%s.yz", src0_param
.reg_name
);
4704 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
4707 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
4708 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
4709 static void shader_glsl_texreg2rgb(const struct wined3d_shader_instruction
*ins
)
4711 DWORD sampler_idx
= ins
->dst
[0].reg
.idx
[0].offset
;
4712 struct glsl_sample_function sample_function
;
4713 struct glsl_src_param src0_param
;
4715 /* Dependent read, not valid with conditional NP2 */
4716 shader_glsl_get_sample_function(ins
->ctx
, sampler_idx
, 0, &sample_function
);
4717 shader_glsl_add_src_param(ins
, &ins
->src
[0], sample_function
.coord_mask
, &src0_param
);
4719 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
,
4720 "%s", src0_param
.param_str
);
4721 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
4724 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
4725 * If any of the first 3 components are < 0, discard this pixel */
4726 static void shader_glsl_texkill(const struct wined3d_shader_instruction
*ins
)
4728 if (ins
->ctx
->reg_maps
->shader_version
.major
>= 4)
4730 struct glsl_src_param src_param
;
4732 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src_param
);
4733 shader_addline(ins
->ctx
->buffer
, "if (bool(floatBitsToUint(%s))) discard;\n", src_param
.param_str
);
4737 struct glsl_dst_param dst_param
;
4739 /* The argument is a destination parameter, and no writemasks are allowed */
4740 shader_glsl_add_dst_param(ins
, &ins
->dst
[0], &dst_param
);
4742 /* 2.0 shaders compare all 4 components in texkill. */
4743 if (ins
->ctx
->reg_maps
->shader_version
.major
>= 2)
4744 shader_addline(ins
->ctx
->buffer
, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param
.reg_name
);
4745 /* 1.x shaders only compare the first 3 components, probably due to
4746 * the nature of the texkill instruction as a tex* instruction, and
4747 * phase, which kills all .w components. Even if all 4 components are
4748 * defined, only the first 3 are used. */
4750 shader_addline(ins
->ctx
->buffer
, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param
.reg_name
);
4754 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
4755 * dst = dot2(src0, src1) + src2 */
4756 static void shader_glsl_dp2add(const struct wined3d_shader_instruction
*ins
)
4758 struct glsl_src_param src0_param
;
4759 struct glsl_src_param src1_param
;
4760 struct glsl_src_param src2_param
;
4762 unsigned int mask_size
;
4764 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
4765 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
4767 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
, &src0_param
);
4768 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
, &src1_param
);
4769 shader_glsl_add_src_param(ins
, &ins
->src
[2], WINED3DSP_WRITEMASK_0
, &src2_param
);
4771 if (mask_size
> 1) {
4772 shader_addline(ins
->ctx
->buffer
, "vec%d(dot(%s, %s) + %s));\n",
4773 mask_size
, src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
4775 shader_addline(ins
->ctx
->buffer
, "dot(%s, %s) + %s);\n",
4776 src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
4780 static void shader_glsl_input_pack(const struct wined3d_shader
*shader
, struct wined3d_string_buffer
*buffer
,
4781 const struct wined3d_shader_signature
*input_signature
,
4782 const struct wined3d_shader_reg_maps
*reg_maps
,
4783 const struct ps_compile_args
*args
, const struct wined3d_gl_info
*gl_info
)
4787 for (i
= 0; i
< input_signature
->element_count
; ++i
)
4789 const struct wined3d_shader_signature_element
*input
= &input_signature
->elements
[i
];
4790 const char *semantic_name
;
4795 if (!(reg_maps
->input_registers
& (1u << input
->register_idx
)))
4798 semantic_name
= input
->semantic_name
;
4799 semantic_idx
= input
->semantic_idx
;
4800 shader_glsl_write_mask_to_str(input
->mask
, reg_mask
);
4802 if (args
->vp_mode
== vertexshader
)
4804 if (input
->sysval_semantic
== WINED3D_SV_POSITION
)
4805 shader_addline(buffer
, "ps_in[%u]%s = vpos%s;\n",
4806 shader
->u
.ps
.input_reg_map
[input
->register_idx
], reg_mask
, reg_mask
);
4807 else if (args
->pointsprite
&& shader_match_semantic(semantic_name
, WINED3D_DECL_USAGE_TEXCOORD
))
4808 shader_addline(buffer
, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", input
->register_idx
);
4810 shader_addline(buffer
, "ps_in[%u]%s = ps_link[%u]%s;\n",
4811 shader
->u
.ps
.input_reg_map
[input
->register_idx
], reg_mask
,
4812 shader
->u
.ps
.input_reg_map
[input
->register_idx
], reg_mask
);
4814 else if (shader_match_semantic(semantic_name
, WINED3D_DECL_USAGE_TEXCOORD
))
4816 if (args
->pointsprite
)
4817 shader_addline(buffer
, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n",
4818 shader
->u
.ps
.input_reg_map
[input
->register_idx
]);
4819 else if (args
->vp_mode
== pretransformed
&& args
->texcoords_initialized
& (1u << semantic_idx
))
4820 shader_addline(buffer
, "ps_in[%u]%s = %s[%u]%s;\n",
4821 shader
->u
.ps
.input_reg_map
[input
->register_idx
], reg_mask
,
4822 gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
]
4823 ? "gl_TexCoord" : "ffp_varying_texcoord", semantic_idx
, reg_mask
);
4825 shader_addline(buffer
, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
4826 shader
->u
.ps
.input_reg_map
[input
->register_idx
], reg_mask
, reg_mask
);
4828 else if (shader_match_semantic(semantic_name
, WINED3D_DECL_USAGE_COLOR
))
4831 shader_addline(buffer
, "ps_in[%u]%s = vec4(ffp_varying_diffuse)%s;\n",
4832 shader
->u
.ps
.input_reg_map
[input
->register_idx
], reg_mask
, reg_mask
);
4833 else if (semantic_idx
== 1)
4834 shader_addline(buffer
, "ps_in[%u]%s = vec4(ffp_varying_specular)%s;\n",
4835 shader
->u
.ps
.input_reg_map
[input
->register_idx
], reg_mask
, reg_mask
);
4837 shader_addline(buffer
, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
4838 shader
->u
.ps
.input_reg_map
[input
->register_idx
], reg_mask
, reg_mask
);
4842 shader_addline(buffer
, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
4843 shader
->u
.ps
.input_reg_map
[input
->register_idx
], reg_mask
, reg_mask
);
4848 /*********************************************
4849 * Vertex Shader Specific Code begins here
4850 ********************************************/
4852 static void add_glsl_program_entry(struct shader_glsl_priv
*priv
, struct glsl_shader_prog_link
*entry
)
4854 struct glsl_program_key key
;
4856 key
.vs_id
= entry
->vs
.id
;
4857 key
.gs_id
= entry
->gs
.id
;
4858 key
.ps_id
= entry
->ps
.id
;
4860 if (wine_rb_put(&priv
->program_lookup
, &key
, &entry
->program_lookup_entry
) == -1)
4862 ERR("Failed to insert program entry.\n");
4866 static struct glsl_shader_prog_link
*get_glsl_program_entry(const struct shader_glsl_priv
*priv
,
4867 GLuint vs_id
, GLuint gs_id
, GLuint ps_id
)
4869 struct wine_rb_entry
*entry
;
4870 struct glsl_program_key key
;
4876 entry
= wine_rb_get(&priv
->program_lookup
, &key
);
4877 return entry
? WINE_RB_ENTRY_VALUE(entry
, struct glsl_shader_prog_link
, program_lookup_entry
) : NULL
;
4880 /* Context activation is done by the caller. */
4881 static void delete_glsl_program_entry(struct shader_glsl_priv
*priv
, const struct wined3d_gl_info
*gl_info
,
4882 struct glsl_shader_prog_link
*entry
)
4884 struct glsl_program_key key
;
4886 key
.vs_id
= entry
->vs
.id
;
4887 key
.gs_id
= entry
->gs
.id
;
4888 key
.ps_id
= entry
->ps
.id
;
4889 wine_rb_remove(&priv
->program_lookup
, &key
);
4891 GL_EXTCALL(glDeleteProgram(entry
->id
));
4893 list_remove(&entry
->vs
.shader_entry
);
4895 list_remove(&entry
->gs
.shader_entry
);
4897 list_remove(&entry
->ps
.shader_entry
);
4898 HeapFree(GetProcessHeap(), 0, entry
->vs
.uniform_f_locations
);
4899 HeapFree(GetProcessHeap(), 0, entry
->ps
.uniform_f_locations
);
4900 HeapFree(GetProcessHeap(), 0, entry
);
4903 static void handle_ps3_input(struct shader_glsl_priv
*priv
,
4904 const struct wined3d_gl_info
*gl_info
, const DWORD
*map
,
4905 const struct wined3d_shader_signature
*input_signature
,
4906 const struct wined3d_shader_reg_maps
*reg_maps_in
,
4907 const struct wined3d_shader_signature
*output_signature
,
4908 const struct wined3d_shader_reg_maps
*reg_maps_out
)
4910 struct wined3d_string_buffer
*buffer
= &priv
->shader_buffer
;
4911 BOOL legacy_context
= gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
];
4915 unsigned int in_count
= vec4_varyings(3, gl_info
);
4916 unsigned int max_varyings
= legacy_context
? in_count
+ 2 : in_count
;
4918 struct wined3d_string_buffer
*destination
= string_buffer_get(&priv
->string_buffers
);
4920 set
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*set
) * max_varyings
);
4922 for (i
= 0; i
< input_signature
->element_count
; ++i
)
4924 const struct wined3d_shader_signature_element
*input
= &input_signature
->elements
[i
];
4926 if (!(reg_maps_in
->input_registers
& (1u << input
->register_idx
)))
4929 in_idx
= map
[input
->register_idx
];
4930 /* Declared, but not read register */
4933 if (in_idx
>= max_varyings
)
4935 FIXME("More input varyings declared than supported, expect issues.\n");
4939 if (in_idx
== in_count
)
4940 string_buffer_sprintf(destination
, "gl_FrontColor");
4941 else if (in_idx
== in_count
+ 1)
4942 string_buffer_sprintf(destination
, "gl_FrontSecondaryColor");
4944 string_buffer_sprintf(destination
, "ps_link[%u]", in_idx
);
4949 for (j
= 0; j
< output_signature
->element_count
; ++j
)
4951 const struct wined3d_shader_signature_element
*output
= &output_signature
->elements
[j
];
4954 if (!(reg_maps_out
->output_registers
& (1u << output
->register_idx
))
4955 || input
->semantic_idx
!= output
->semantic_idx
4956 || strcmp(input
->semantic_name
, output
->semantic_name
)
4957 || !(mask
= input
->mask
& output
->mask
))
4960 if (set
[in_idx
] == ~0u)
4962 set
[in_idx
] |= mask
& reg_maps_out
->u
.output_registers_mask
[output
->register_idx
];
4963 shader_glsl_write_mask_to_str(mask
, reg_mask
);
4965 shader_addline(buffer
, "%s%s = vs_out[%u]%s;\n",
4966 destination
->buffer
, reg_mask
, output
->register_idx
, reg_mask
);
4970 for (i
= 0; i
< max_varyings
; ++i
)
4974 if (!set
[i
] || set
[i
] == WINED3DSP_WRITEMASK_ALL
)
4977 if (set
[i
] == ~0U) set
[i
] = 0;
4980 if (!(set
[i
] & WINED3DSP_WRITEMASK_0
)) reg_mask
[size
++] = 'x';
4981 if (!(set
[i
] & WINED3DSP_WRITEMASK_1
)) reg_mask
[size
++] = 'y';
4982 if (!(set
[i
] & WINED3DSP_WRITEMASK_2
)) reg_mask
[size
++] = 'z';
4983 if (!(set
[i
] & WINED3DSP_WRITEMASK_3
)) reg_mask
[size
++] = 'w';
4984 reg_mask
[size
] = '\0';
4987 string_buffer_sprintf(destination
, "gl_FrontColor");
4988 else if (i
== in_count
+ 1)
4989 string_buffer_sprintf(destination
, "gl_FrontSecondaryColor");
4991 string_buffer_sprintf(destination
, "ps_link[%u]", i
);
4993 if (size
== 1) shader_addline(buffer
, "%s.%s = 0.0;\n", destination
->buffer
, reg_mask
);
4994 else shader_addline(buffer
, "%s.%s = vec%u(0.0);\n", destination
->buffer
, reg_mask
, size
);
4997 HeapFree(GetProcessHeap(), 0, set
);
4998 string_buffer_release(&priv
->string_buffers
, destination
);
5001 /* Context activation is done by the caller. */
5002 static GLuint
generate_param_reorder_function(struct shader_glsl_priv
*priv
,
5003 const struct wined3d_shader
*vs
, const struct wined3d_shader
*ps
,
5004 BOOL per_vertex_point_size
, BOOL flatshading
, const struct wined3d_gl_info
*gl_info
)
5006 struct wined3d_string_buffer
*buffer
= &priv
->shader_buffer
;
5008 DWORD ps_major
= ps
? ps
->reg_maps
.shader_version
.major
: 0;
5010 const char *semantic_name
;
5013 BOOL legacy_context
= gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
];
5015 string_buffer_clear(buffer
);
5017 shader_addline(buffer
, "%s\n", shader_glsl_get_version(gl_info
, &vs
->reg_maps
.shader_version
));
5019 if (per_vertex_point_size
)
5021 shader_addline(buffer
, "uniform struct\n{\n");
5022 shader_addline(buffer
, " float size_min;\n");
5023 shader_addline(buffer
, " float size_max;\n");
5024 shader_addline(buffer
, "} ffp_point;\n");
5029 DWORD colors_written_mask
[2] = {0};
5030 DWORD texcoords_written_mask
[MAX_TEXTURES
] = {0};
5032 if (!legacy_context
)
5034 declare_out_varying(gl_info
, buffer
, flatshading
, "vec4 ffp_varying_diffuse;\n");
5035 declare_out_varying(gl_info
, buffer
, flatshading
, "vec4 ffp_varying_specular;\n");
5036 declare_out_varying(gl_info
, buffer
, FALSE
, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES
);
5037 declare_out_varying(gl_info
, buffer
, FALSE
, "float ffp_varying_fogcoord;\n");
5040 shader_addline(buffer
, "void order_ps_input(in vec4 vs_out[%u])\n{\n", vs
->limits
->packed_output
);
5042 for (i
= 0; i
< vs
->output_signature
.element_count
; ++i
)
5044 const struct wined3d_shader_signature_element
*output
= &vs
->output_signature
.elements
[i
];
5047 if (!(vs
->reg_maps
.output_registers
& (1u << output
->register_idx
)))
5050 semantic_name
= output
->semantic_name
;
5051 semantic_idx
= output
->semantic_idx
;
5052 write_mask
= output
->mask
;
5053 shader_glsl_write_mask_to_str(write_mask
, reg_mask
);
5055 if (shader_match_semantic(semantic_name
, WINED3D_DECL_USAGE_COLOR
) && semantic_idx
< 2)
5058 shader_addline(buffer
, "gl_Front%sColor%s = vs_out[%u]%s;\n",
5059 semantic_idx
? "Secondary" : "", reg_mask
, output
->register_idx
, reg_mask
);
5061 shader_addline(buffer
, "ffp_varying_%s%s = clamp(vs_out[%u]%s, 0.0, 1.0);\n",
5062 semantic_idx
? "specular" : "diffuse", reg_mask
, output
->register_idx
, reg_mask
);
5064 colors_written_mask
[semantic_idx
] = write_mask
;
5066 else if (shader_match_semantic(semantic_name
, WINED3D_DECL_USAGE_POSITION
) && !semantic_idx
)
5068 shader_addline(buffer
, "gl_Position%s = vs_out[%u]%s;\n",
5069 reg_mask
, output
->register_idx
, reg_mask
);
5071 else if (shader_match_semantic(semantic_name
, WINED3D_DECL_USAGE_TEXCOORD
))
5073 if (semantic_idx
< MAX_TEXTURES
)
5075 shader_addline(buffer
, "%s[%u]%s = vs_out[%u]%s;\n",
5076 legacy_context
? "gl_TexCoord" : "ffp_varying_texcoord",
5077 semantic_idx
, reg_mask
, output
->register_idx
, reg_mask
);
5078 texcoords_written_mask
[semantic_idx
] = write_mask
;
5081 else if (shader_match_semantic(semantic_name
, WINED3D_DECL_USAGE_PSIZE
) && per_vertex_point_size
)
5083 shader_addline(buffer
, "gl_PointSize = clamp(vs_out[%u].%c, ffp_point.size_min, ffp_point.size_max);\n",
5084 output
->register_idx
, reg_mask
[1]);
5086 else if (shader_match_semantic(semantic_name
, WINED3D_DECL_USAGE_FOG
))
5088 shader_addline(buffer
, "%s = clamp(vs_out[%u].%c, 0.0, 1.0);\n",
5089 legacy_context
? "gl_FogFragCoord" : "ffp_varying_fogcoord",
5090 output
->register_idx
, reg_mask
[1]);
5094 for (i
= 0; i
< 2; ++i
)
5096 if (colors_written_mask
[i
] != WINED3DSP_WRITEMASK_ALL
)
5098 shader_glsl_write_mask_to_str(~colors_written_mask
[i
] & WINED3DSP_WRITEMASK_ALL
, reg_mask
);
5100 shader_addline(buffer
, "%s%s = vec4(1.0)%s;\n",
5101 legacy_context
? "gl_FrontColor" : "ffp_varying_diffuse",
5102 reg_mask
, reg_mask
);
5104 shader_addline(buffer
, "%s%s = vec4(0.0)%s;\n",
5105 legacy_context
? "gl_FrontSecondaryColor" : "ffp_varying_specular",
5106 reg_mask
, reg_mask
);
5109 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
5111 if (ps
&& !(ps
->reg_maps
.texcoord
& (1u << i
)))
5114 if (texcoords_written_mask
[i
] != WINED3DSP_WRITEMASK_ALL
)
5116 if (gl_info
->limits
.glsl_varyings
< wined3d_max_compat_varyings(gl_info
)
5117 && !texcoords_written_mask
[i
])
5120 shader_glsl_write_mask_to_str(~texcoords_written_mask
[i
] & WINED3DSP_WRITEMASK_ALL
, reg_mask
);
5121 shader_addline(buffer
, "%s[%u]%s = vec4(0.0)%s;\n",
5122 legacy_context
? "gl_TexCoord" : "ffp_varying_texcoord", i
, reg_mask
, reg_mask
);
5128 UINT in_count
= min(vec4_varyings(ps_major
, gl_info
), ps
->limits
->packed_input
);
5130 declare_out_varying(gl_info
, buffer
, FALSE
, "vec4 ps_link[%u];\n", in_count
);
5131 shader_addline(buffer
, "void order_ps_input(in vec4 vs_out[%u])\n{\n", vs
->limits
->packed_output
);
5133 /* First, sort out position and point size. Those are not passed to the pixel shader */
5134 for (i
= 0; i
< vs
->output_signature
.element_count
; ++i
)
5136 const struct wined3d_shader_signature_element
*output
= &vs
->output_signature
.elements
[i
];
5138 if (!(vs
->reg_maps
.output_registers
& (1u << output
->register_idx
)))
5141 semantic_name
= output
->semantic_name
;
5142 semantic_idx
= output
->semantic_idx
;
5143 shader_glsl_write_mask_to_str(output
->mask
, reg_mask
);
5145 if (shader_match_semantic(semantic_name
, WINED3D_DECL_USAGE_POSITION
) && !semantic_idx
)
5147 shader_addline(buffer
, "gl_Position%s = vs_out[%u]%s;\n",
5148 reg_mask
, output
->register_idx
, reg_mask
);
5150 else if (shader_match_semantic(semantic_name
, WINED3D_DECL_USAGE_PSIZE
) && per_vertex_point_size
)
5152 shader_addline(buffer
, "gl_PointSize = clamp(vs_out[%u].%c, ffp_point.size_min, ffp_point.size_max);\n",
5153 output
->register_idx
, reg_mask
[1]);
5157 /* Then, fix the pixel shader input */
5158 handle_ps3_input(priv
, gl_info
, ps
->u
.ps
.input_reg_map
, &ps
->input_signature
,
5159 &ps
->reg_maps
, &vs
->output_signature
, &vs
->reg_maps
);
5162 shader_addline(buffer
, "}\n");
5164 ret
= GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER
));
5165 checkGLcall("glCreateShader(GL_VERTEX_SHADER)");
5166 shader_glsl_compile(gl_info
, ret
, buffer
->buffer
);
5171 static void shader_glsl_generate_srgb_write_correction(struct wined3d_string_buffer
*buffer
)
5173 shader_addline(buffer
, "tmp0.xyz = pow(gl_FragData[0].xyz, vec3(srgb_const0.x));\n");
5174 shader_addline(buffer
, "tmp0.xyz = tmp0.xyz * vec3(srgb_const0.y) - vec3(srgb_const0.z);\n");
5175 shader_addline(buffer
, "tmp1.xyz = gl_FragData[0].xyz * vec3(srgb_const0.w);\n");
5176 shader_addline(buffer
, "bvec3 srgb_compare = lessThan(gl_FragData[0].xyz, vec3(srgb_const1.x));\n");
5177 shader_addline(buffer
, "gl_FragData[0].xyz = mix(tmp0.xyz, tmp1.xyz, vec3(srgb_compare));\n");
5178 shader_addline(buffer
, "gl_FragData[0] = clamp(gl_FragData[0], 0.0, 1.0);\n");
5181 static void shader_glsl_generate_fog_code(struct wined3d_string_buffer
*buffer
, enum wined3d_ffp_ps_fog_mode mode
)
5185 case WINED3D_FFP_PS_FOG_OFF
:
5188 case WINED3D_FFP_PS_FOG_LINEAR
:
5189 shader_addline(buffer
, "float fog = (ffp_fog.end - ffp_varying_fogcoord) * ffp_fog.scale;\n");
5192 case WINED3D_FFP_PS_FOG_EXP
:
5193 shader_addline(buffer
, "float fog = exp(-ffp_fog.density * ffp_varying_fogcoord);\n");
5196 case WINED3D_FFP_PS_FOG_EXP2
:
5197 shader_addline(buffer
, "float fog = exp(-ffp_fog.density * ffp_fog.density"
5198 " * ffp_varying_fogcoord * ffp_varying_fogcoord);\n");
5202 ERR("Invalid fog mode %#x.\n", mode
);
5206 shader_addline(buffer
, "gl_FragData[0].xyz = mix(ffp_fog.color.xyz, gl_FragData[0].xyz,"
5207 " clamp(fog, 0.0, 1.0));\n");
5210 /* Context activation is done by the caller. */
5211 static GLuint
shader_glsl_generate_pshader(const struct wined3d_context
*context
,
5212 struct wined3d_string_buffer
*buffer
, struct wined3d_string_buffer_list
*string_buffers
,
5213 const struct wined3d_shader
*shader
,
5214 const struct ps_compile_args
*args
, struct ps_np2fixup_info
*np2fixup_info
)
5216 const struct wined3d_shader_reg_maps
*reg_maps
= &shader
->reg_maps
;
5217 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
5218 const DWORD
*function
= shader
->function
;
5219 struct shader_glsl_ctx_priv priv_ctx
;
5220 BOOL legacy_context
= gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
];
5222 /* Create the hw GLSL shader object and assign it as the shader->prgId */
5223 GLuint shader_id
= GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER
));
5225 memset(&priv_ctx
, 0, sizeof(priv_ctx
));
5226 priv_ctx
.cur_ps_args
= args
;
5227 priv_ctx
.cur_np2fixup_info
= np2fixup_info
;
5228 priv_ctx
.string_buffers
= string_buffers
;
5230 shader_addline(buffer
, "%s\n", shader_glsl_get_version(gl_info
, ®_maps
->shader_version
));
5232 if (gl_info
->supported
[ARB_SHADER_BIT_ENCODING
])
5233 shader_addline(buffer
, "#extension GL_ARB_shader_bit_encoding : enable\n");
5234 if (gl_info
->supported
[ARB_SHADER_TEXTURE_LOD
])
5235 shader_addline(buffer
, "#extension GL_ARB_shader_texture_lod : enable\n");
5236 if (gl_info
->supported
[ARB_TEXTURE_QUERY_LEVELS
])
5237 shader_addline(buffer
, "#extension GL_ARB_texture_query_levels : enable\n");
5238 /* The spec says that it doesn't have to be explicitly enabled, but the
5239 * nvidia drivers write a warning if we don't do so. */
5240 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
5241 shader_addline(buffer
, "#extension GL_ARB_texture_rectangle : enable\n");
5242 if (gl_info
->supported
[ARB_UNIFORM_BUFFER_OBJECT
])
5243 shader_addline(buffer
, "#extension GL_ARB_uniform_buffer_object : enable\n");
5244 if (gl_info
->supported
[EXT_GPU_SHADER4
])
5245 shader_addline(buffer
, "#extension GL_EXT_gpu_shader4 : enable\n");
5247 /* Base Declarations */
5248 shader_generate_glsl_declarations(context
, buffer
, shader
, reg_maps
, &priv_ctx
);
5250 if (reg_maps
->shader_version
.major
< 3 || args
->vp_mode
!= vertexshader
)
5253 WORD map
= reg_maps
->texcoord
;
5257 if (glsl_is_color_reg_read(shader
, 0))
5258 shader_addline(buffer
, "ffp_varying_diffuse = gl_Color;\n");
5259 if (glsl_is_color_reg_read(shader
, 1))
5260 shader_addline(buffer
, "ffp_varying_specular = gl_SecondaryColor;\n");
5263 for (i
= 0; map
; map
>>= 1, ++i
)
5267 if (args
->pointsprite
)
5268 shader_addline(buffer
, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", i
);
5269 else if (args
->texcoords_initialized
& (1u << i
))
5270 shader_addline(buffer
, "ffp_texcoord[%u] = %s[%u];\n", i
,
5271 legacy_context
? "gl_TexCoord" : "ffp_varying_texcoord", i
);
5273 shader_addline(buffer
, "ffp_texcoord[%u] = vec4(0.0);\n", i
);
5274 shader_addline(buffer
, "vec4 T%u = ffp_texcoord[%u];\n", i
, i
);
5279 shader_addline(buffer
, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
5282 /* Pack 3.0 inputs */
5283 if (reg_maps
->shader_version
.major
>= 3)
5284 shader_glsl_input_pack(shader
, buffer
, &shader
->input_signature
, reg_maps
, args
, gl_info
);
5286 /* Base Shader Body */
5287 shader_generate_main(shader
, buffer
, reg_maps
, function
, &priv_ctx
);
5289 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly */
5290 if (reg_maps
->shader_version
.major
< 2)
5292 /* Some older cards like GeforceFX ones don't support multiple buffers, so also not gl_FragData */
5293 shader_addline(buffer
, "gl_FragData[0] = R0;\n");
5296 if (args
->srgb_correction
)
5297 shader_glsl_generate_srgb_write_correction(buffer
);
5299 /* SM < 3 does not replace the fog stage. */
5300 if (reg_maps
->shader_version
.major
< 3)
5301 shader_glsl_generate_fog_code(buffer
, args
->fog
);
5303 shader_addline(buffer
, "}\n");
5305 TRACE("Compiling shader object %u.\n", shader_id
);
5306 shader_glsl_compile(gl_info
, shader_id
, buffer
->buffer
);
5311 /* Context activation is done by the caller. */
5312 static GLuint
shader_glsl_generate_vshader(const struct wined3d_context
*context
,
5313 struct wined3d_string_buffer
*buffer
, struct wined3d_string_buffer_list
*string_buffers
,
5314 const struct wined3d_shader
*shader
,
5315 const struct vs_compile_args
*args
)
5317 const struct wined3d_shader_reg_maps
*reg_maps
= &shader
->reg_maps
;
5318 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
5319 const DWORD
*function
= shader
->function
;
5320 struct shader_glsl_ctx_priv priv_ctx
;
5321 BOOL legacy_context
= gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
];
5323 /* Create the hw GLSL shader program and assign it as the shader->prgId */
5324 GLuint shader_id
= GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER
));
5326 shader_addline(buffer
, "%s\n", shader_glsl_get_version(gl_info
, ®_maps
->shader_version
));
5328 if (gl_info
->supported
[ARB_DRAW_INSTANCED
])
5329 shader_addline(buffer
, "#extension GL_ARB_draw_instanced : enable\n");
5330 if (gl_info
->supported
[ARB_SHADER_BIT_ENCODING
])
5331 shader_addline(buffer
, "#extension GL_ARB_shader_bit_encoding : enable\n");
5332 if (gl_info
->supported
[ARB_TEXTURE_QUERY_LEVELS
])
5333 shader_addline(buffer
, "#extension GL_ARB_texture_query_levels : enable\n");
5334 if (gl_info
->supported
[ARB_UNIFORM_BUFFER_OBJECT
])
5335 shader_addline(buffer
, "#extension GL_ARB_uniform_buffer_object : enable\n");
5336 if (gl_info
->supported
[EXT_GPU_SHADER4
])
5337 shader_addline(buffer
, "#extension GL_EXT_gpu_shader4 : enable\n");
5339 memset(&priv_ctx
, 0, sizeof(priv_ctx
));
5340 priv_ctx
.cur_vs_args
= args
;
5341 priv_ctx
.string_buffers
= string_buffers
;
5343 /* Base Declarations */
5344 shader_generate_glsl_declarations(context
, buffer
, shader
, reg_maps
, &priv_ctx
);
5346 /* Base Shader Body */
5347 shader_generate_main(shader
, buffer
, reg_maps
, function
, &priv_ctx
);
5349 /* Unpack outputs */
5350 shader_addline(buffer
, "order_ps_input(vs_out);\n");
5352 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
5353 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
5354 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
5355 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0)
5357 if (reg_maps
->shader_version
.major
< 3)
5359 if (args
->fog_src
== VS_FOG_Z
)
5360 shader_addline(buffer
, "%s = gl_Position.z;\n",
5361 legacy_context
? "gl_FogFragCoord" : "ffp_varying_fogcoord");
5362 else if (!reg_maps
->fog
)
5363 shader_addline(buffer
, "%s = 0.0;\n",
5364 legacy_context
? "gl_FogFragCoord" : "ffp_varying_fogcoord");
5367 /* We always store the clipplanes without y inversion */
5368 if (args
->clip_enabled
)
5369 shader_addline(buffer
, "gl_ClipVertex = gl_Position;\n");
5371 if (args
->point_size
&& !args
->per_vertex_point_size
)
5372 shader_addline(buffer
, "gl_PointSize = clamp(ffp_point.size, ffp_point.size_min, ffp_point.size_max);\n");
5374 /* Write the final position.
5376 * OpenGL coordinates specify the center of the pixel while d3d coords specify
5377 * the corner. The offsets are stored in z and w in posFixup. posFixup.y contains
5378 * 1.0 or -1.0 to turn the rendering upside down for offscreen rendering. PosFixup.x
5379 * contains 1.0 to allow a mad.
5381 shader_addline(buffer
, "gl_Position.y = gl_Position.y * posFixup.y;\n");
5382 shader_addline(buffer
, "gl_Position.xy += posFixup.zw * gl_Position.ww;\n");
5384 /* Z coord [0;1]->[-1;1] mapping, see comment in transform_projection in state.c
5386 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However, shaders are run
5387 * before the homogeneous divide, so we have to take the w into account: z = ((z / w) * 2 - 1) * w,
5388 * which is the same as z = z * 2 - w.
5390 shader_addline(buffer
, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
5392 shader_addline(buffer
, "}\n");
5394 TRACE("Compiling shader object %u.\n", shader_id
);
5395 shader_glsl_compile(gl_info
, shader_id
, buffer
->buffer
);
5400 /* Context activation is done by the caller. */
5401 static GLuint
shader_glsl_generate_geometry_shader(const struct wined3d_context
*context
,
5402 struct wined3d_string_buffer
*buffer
, struct wined3d_string_buffer_list
*string_buffers
,
5403 const struct wined3d_shader
*shader
)
5405 const struct wined3d_shader_reg_maps
*reg_maps
= &shader
->reg_maps
;
5406 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
5407 const DWORD
*function
= shader
->function
;
5408 struct shader_glsl_ctx_priv priv_ctx
;
5411 shader_id
= GL_EXTCALL(glCreateShader(GL_GEOMETRY_SHADER
));
5413 shader_addline(buffer
, "%s\n", shader_glsl_get_version(gl_info
, ®_maps
->shader_version
));
5415 if (gl_info
->supported
[ARB_GEOMETRY_SHADER4
])
5416 shader_addline(buffer
, "#extension GL_ARB_geometry_shader4 : enable\n");
5417 if (gl_info
->supported
[ARB_SHADER_BIT_ENCODING
])
5418 shader_addline(buffer
, "#extension GL_ARB_shader_bit_encoding : enable\n");
5419 if (gl_info
->supported
[ARB_TEXTURE_QUERY_LEVELS
])
5420 shader_addline(buffer
, "#extension GL_ARB_texture_query_levels : enable\n");
5421 if (gl_info
->supported
[ARB_UNIFORM_BUFFER_OBJECT
])
5422 shader_addline(buffer
, "#extension GL_ARB_uniform_buffer_object : enable\n");
5423 if (gl_info
->supported
[EXT_GPU_SHADER4
])
5424 shader_addline(buffer
, "#extension GL_EXT_gpu_shader4 : enable\n");
5426 memset(&priv_ctx
, 0, sizeof(priv_ctx
));
5427 priv_ctx
.string_buffers
= string_buffers
;
5428 shader_generate_glsl_declarations(context
, buffer
, shader
, reg_maps
, &priv_ctx
);
5429 shader_generate_main(shader
, buffer
, reg_maps
, function
, &priv_ctx
);
5430 shader_addline(buffer
, "}\n");
5432 TRACE("Compiling shader object %u.\n", shader_id
);
5433 shader_glsl_compile(gl_info
, shader_id
, buffer
->buffer
);
5438 static GLuint
find_glsl_pshader(const struct wined3d_context
*context
,
5439 struct wined3d_string_buffer
*buffer
, struct wined3d_string_buffer_list
*string_buffers
,
5440 struct wined3d_shader
*shader
,
5441 const struct ps_compile_args
*args
, const struct ps_np2fixup_info
**np2fixup_info
)
5443 struct glsl_ps_compiled_shader
*gl_shaders
, *new_array
;
5444 struct glsl_shader_private
*shader_data
;
5445 struct ps_np2fixup_info
*np2fixup
;
5450 if (!shader
->backend_data
)
5452 shader
->backend_data
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*shader_data
));
5453 if (!shader
->backend_data
)
5455 ERR("Failed to allocate backend data.\n");
5459 shader_data
= shader
->backend_data
;
5460 gl_shaders
= shader_data
->gl_shaders
.ps
;
5462 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
5463 * so a linear search is more performant than a hashmap or a binary search
5464 * (cache coherency etc)
5466 for (i
= 0; i
< shader_data
->num_gl_shaders
; ++i
)
5468 if (!memcmp(&gl_shaders
[i
].args
, args
, sizeof(*args
)))
5470 if (args
->np2_fixup
)
5471 *np2fixup_info
= &gl_shaders
[i
].np2fixup
;
5472 return gl_shaders
[i
].id
;
5476 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader
);
5477 if(shader_data
->shader_array_size
== shader_data
->num_gl_shaders
) {
5478 if (shader_data
->num_gl_shaders
)
5480 new_size
= shader_data
->shader_array_size
+ max(1, shader_data
->shader_array_size
/ 2);
5481 new_array
= HeapReAlloc(GetProcessHeap(), 0, shader_data
->gl_shaders
.ps
,
5482 new_size
* sizeof(*gl_shaders
));
5486 new_array
= HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders
));
5491 ERR("Out of memory\n");
5494 shader_data
->gl_shaders
.ps
= new_array
;
5495 shader_data
->shader_array_size
= new_size
;
5496 gl_shaders
= new_array
;
5499 gl_shaders
[shader_data
->num_gl_shaders
].args
= *args
;
5501 np2fixup
= &gl_shaders
[shader_data
->num_gl_shaders
].np2fixup
;
5502 memset(np2fixup
, 0, sizeof(*np2fixup
));
5503 *np2fixup_info
= args
->np2_fixup
? np2fixup
: NULL
;
5505 pixelshader_update_resource_types(shader
, args
->tex_types
);
5507 string_buffer_clear(buffer
);
5508 ret
= shader_glsl_generate_pshader(context
, buffer
, string_buffers
, shader
, args
, np2fixup
);
5509 gl_shaders
[shader_data
->num_gl_shaders
++].id
= ret
;
5514 static inline BOOL
vs_args_equal(const struct vs_compile_args
*stored
, const struct vs_compile_args
*new,
5515 const DWORD use_map
)
5517 if((stored
->swizzle_map
& use_map
) != new->swizzle_map
) return FALSE
;
5518 if((stored
->clip_enabled
) != new->clip_enabled
) return FALSE
;
5519 if (stored
->point_size
!= new->point_size
)
5521 if (stored
->per_vertex_point_size
!= new->per_vertex_point_size
)
5523 if (stored
->flatshading
!= new->flatshading
)
5525 return stored
->fog_src
== new->fog_src
;
5528 static GLuint
find_glsl_vshader(const struct wined3d_context
*context
,
5529 struct wined3d_string_buffer
*buffer
, struct wined3d_string_buffer_list
*string_buffers
,
5530 struct wined3d_shader
*shader
,
5531 const struct vs_compile_args
*args
)
5535 DWORD use_map
= context
->stream_info
.use_map
;
5536 struct glsl_vs_compiled_shader
*gl_shaders
, *new_array
;
5537 struct glsl_shader_private
*shader_data
;
5540 if (!shader
->backend_data
)
5542 shader
->backend_data
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*shader_data
));
5543 if (!shader
->backend_data
)
5545 ERR("Failed to allocate backend data.\n");
5549 shader_data
= shader
->backend_data
;
5550 gl_shaders
= shader_data
->gl_shaders
.vs
;
5552 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
5553 * so a linear search is more performant than a hashmap or a binary search
5554 * (cache coherency etc)
5556 for (i
= 0; i
< shader_data
->num_gl_shaders
; ++i
)
5558 if (vs_args_equal(&gl_shaders
[i
].args
, args
, use_map
))
5559 return gl_shaders
[i
].id
;
5562 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader
);
5564 if(shader_data
->shader_array_size
== shader_data
->num_gl_shaders
) {
5565 if (shader_data
->num_gl_shaders
)
5567 new_size
= shader_data
->shader_array_size
+ max(1, shader_data
->shader_array_size
/ 2);
5568 new_array
= HeapReAlloc(GetProcessHeap(), 0, shader_data
->gl_shaders
.vs
,
5569 new_size
* sizeof(*gl_shaders
));
5573 new_array
= HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders
));
5578 ERR("Out of memory\n");
5581 shader_data
->gl_shaders
.vs
= new_array
;
5582 shader_data
->shader_array_size
= new_size
;
5583 gl_shaders
= new_array
;
5586 gl_shaders
[shader_data
->num_gl_shaders
].args
= *args
;
5588 string_buffer_clear(buffer
);
5589 ret
= shader_glsl_generate_vshader(context
, buffer
, string_buffers
, shader
, args
);
5590 gl_shaders
[shader_data
->num_gl_shaders
++].id
= ret
;
5595 static GLuint
find_glsl_geometry_shader(const struct wined3d_context
*context
,
5596 struct wined3d_string_buffer
*buffer
, struct wined3d_string_buffer_list
*string_buffers
,
5597 struct wined3d_shader
*shader
)
5599 struct glsl_gs_compiled_shader
*gl_shaders
;
5600 struct glsl_shader_private
*shader_data
;
5603 if (!shader
->backend_data
)
5605 if (!(shader
->backend_data
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*shader_data
))))
5607 ERR("Failed to allocate backend data.\n");
5611 shader_data
= shader
->backend_data
;
5612 gl_shaders
= shader_data
->gl_shaders
.gs
;
5614 if (shader_data
->num_gl_shaders
)
5615 return gl_shaders
[0].id
;
5617 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader
);
5619 if (!(shader_data
->gl_shaders
.gs
= HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders
))))
5621 ERR("Failed to allocate GL shader array.\n");
5624 shader_data
->shader_array_size
= 1;
5625 gl_shaders
= shader_data
->gl_shaders
.gs
;
5627 string_buffer_clear(buffer
);
5628 ret
= shader_glsl_generate_geometry_shader(context
, buffer
, string_buffers
, shader
);
5629 gl_shaders
[shader_data
->num_gl_shaders
++].id
= ret
;
5634 static const char *shader_glsl_ffp_mcs(enum wined3d_material_color_source mcs
, const char *material
)
5638 case WINED3D_MCS_MATERIAL
:
5640 case WINED3D_MCS_COLOR1
:
5641 return "ffp_attrib_diffuse";
5642 case WINED3D_MCS_COLOR2
:
5643 return "ffp_attrib_specular";
5645 ERR("Invalid material color source %#x.\n", mcs
);
5650 static void shader_glsl_ffp_vertex_lighting(struct wined3d_string_buffer
*buffer
,
5651 const struct wined3d_ffp_vs_settings
*settings
, BOOL legacy_lighting
)
5653 const char *diffuse
, *specular
, *emissive
, *ambient
;
5654 enum wined3d_light_type light_type
;
5657 if (!settings
->lighting
)
5659 shader_addline(buffer
, "ffp_varying_diffuse = ffp_attrib_diffuse;\n");
5660 shader_addline(buffer
, "ffp_varying_specular = ffp_attrib_specular;\n");
5664 shader_addline(buffer
, "vec3 ambient = ffp_light_ambient;\n");
5665 shader_addline(buffer
, "vec3 diffuse = vec3(0.0);\n");
5666 shader_addline(buffer
, "vec4 specular = vec4(0.0);\n");
5667 shader_addline(buffer
, "vec3 dir, dst;\n");
5668 shader_addline(buffer
, "float att, t;\n");
5670 ambient
= shader_glsl_ffp_mcs(settings
->ambient_source
, "ffp_material.ambient");
5671 diffuse
= shader_glsl_ffp_mcs(settings
->diffuse_source
, "ffp_material.diffuse");
5672 specular
= shader_glsl_ffp_mcs(settings
->specular_source
, "ffp_material.specular");
5673 emissive
= shader_glsl_ffp_mcs(settings
->emissive_source
, "ffp_material.emissive");
5675 for (i
= 0; i
< MAX_ACTIVE_LIGHTS
; ++i
)
5677 light_type
= (settings
->light_type
>> WINED3D_FFP_LIGHT_TYPE_SHIFT(i
)) & WINED3D_FFP_LIGHT_TYPE_MASK
;
5680 case WINED3D_LIGHT_POINT
:
5681 shader_addline(buffer
, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", i
);
5682 shader_addline(buffer
, "dst.z = dot(dir, dir);\n");
5683 shader_addline(buffer
, "dst.y = sqrt(dst.z);\n");
5684 shader_addline(buffer
, "dst.x = 1.0;\n");
5685 if (legacy_lighting
)
5687 shader_addline(buffer
, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", i
, i
);
5688 shader_addline(buffer
, "dst.z = dst.y * dst.y;\n");
5692 shader_addline(buffer
, "if (dst.y <= ffp_light[%u].range)\n{\n", i
);
5694 shader_addline(buffer
, "att = dot(dst.xyz, vec3(ffp_light[%u].c_att,"
5695 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n", i
, i
, i
);
5696 if (!legacy_lighting
)
5697 shader_addline(buffer
, "att = 1.0 / att;\n");
5698 shader_addline(buffer
, "ambient += ffp_light[%u].ambient.xyz * att;\n", i
);
5699 if (!settings
->normal
)
5701 if (!legacy_lighting
)
5702 shader_addline(buffer
, "}\n");
5705 shader_addline(buffer
, "dir = normalize(dir);\n");
5706 shader_addline(buffer
, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
5707 " * ffp_light[%u].diffuse.xyz) * att;\n", i
);
5708 if (settings
->localviewer
)
5709 shader_addline(buffer
, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
5711 shader_addline(buffer
, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
5712 shader_addline(buffer
, "if (t > 0.0) specular += (pow(t, ffp_material.shininess)"
5713 " * ffp_light[%u].specular) * att;\n", i
);
5714 if (!legacy_lighting
)
5715 shader_addline(buffer
, "}\n");
5718 case WINED3D_LIGHT_SPOT
:
5719 shader_addline(buffer
, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", i
);
5720 shader_addline(buffer
, "dst.z = dot(dir, dir);\n");
5721 shader_addline(buffer
, "dst.y = sqrt(dst.z);\n");
5722 shader_addline(buffer
, "dst.x = 1.0;\n");
5723 if (legacy_lighting
)
5725 shader_addline(buffer
, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", i
, i
);
5726 shader_addline(buffer
, "dst.z = dst.y * dst.y;\n");
5730 shader_addline(buffer
, "if (dst.y <= ffp_light[%u].range)\n{\n", i
);
5732 shader_addline(buffer
, "dir = normalize(dir);\n");
5733 shader_addline(buffer
, "t = dot(-dir, normalize(ffp_light[%u].direction));\n", i
);
5734 shader_addline(buffer
, "if (t > ffp_light[%u].cos_htheta) att = 1.0;\n", i
);
5735 shader_addline(buffer
, "else if (t <= ffp_light[%u].cos_hphi) att = 0.0;\n", i
);
5736 shader_addline(buffer
, "else att = pow((t - ffp_light[%u].cos_hphi)"
5737 " / (ffp_light[%u].cos_htheta - ffp_light[%u].cos_hphi), ffp_light[%u].falloff);\n",
5739 if (legacy_lighting
)
5740 shader_addline(buffer
, "att *= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
5741 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
5744 shader_addline(buffer
, "att /= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
5745 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
5747 shader_addline(buffer
, "ambient += ffp_light[%u].ambient.xyz * att;\n", i
);
5748 if (!settings
->normal
)
5750 if (!legacy_lighting
)
5751 shader_addline(buffer
, "}\n");
5754 shader_addline(buffer
, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
5755 " * ffp_light[%u].diffuse.xyz) * att;\n", i
);
5756 if (settings
->localviewer
)
5757 shader_addline(buffer
, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
5759 shader_addline(buffer
, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
5760 shader_addline(buffer
, "if (t > 0.0) specular += (pow(t, ffp_material.shininess)"
5761 " * ffp_light[%u].specular) * att;\n", i
);
5762 if (!legacy_lighting
)
5763 shader_addline(buffer
, "}\n");
5766 case WINED3D_LIGHT_DIRECTIONAL
:
5767 shader_addline(buffer
, "ambient += ffp_light[%u].ambient.xyz;\n", i
);
5768 if (!settings
->normal
)
5770 shader_addline(buffer
, "dir = normalize(ffp_light[%u].direction.xyz);\n", i
);
5771 shader_addline(buffer
, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
5772 " * ffp_light[%u].diffuse.xyz;\n", i
);
5773 /* TODO: In the non-local viewer case the halfvector is constant
5774 * and could be precomputed and stored in a uniform. */
5775 if (settings
->localviewer
)
5776 shader_addline(buffer
, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
5778 shader_addline(buffer
, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
5779 shader_addline(buffer
, "if (t > 0.0) specular += pow(t, ffp_material.shininess)"
5780 " * ffp_light[%u].specular;\n", i
);
5783 case WINED3D_LIGHT_PARALLELPOINT
:
5784 shader_addline(buffer
, "ambient += ffp_light[%u].ambient.xyz;\n", i
);
5785 if (!settings
->normal
)
5787 shader_addline(buffer
, "dir = normalize(ffp_light[%u].position.xyz);\n", i
);
5788 shader_addline(buffer
, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
5789 " * ffp_light[%u].diffuse.xyz;\n", i
);
5790 shader_addline(buffer
, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
5791 shader_addline(buffer
, "if (t > 0.0) specular += pow(t, ffp_material.shininess)"
5792 " * ffp_light[%u].specular;\n", i
);
5797 FIXME("Unhandled light type %#x.\n", light_type
);
5802 shader_addline(buffer
, "ffp_varying_diffuse.xyz = %s.xyz * ambient + %s.xyz * diffuse + %s.xyz;\n",
5803 ambient
, diffuse
, emissive
);
5804 shader_addline(buffer
, "ffp_varying_diffuse.w = %s.w;\n", diffuse
);
5805 shader_addline(buffer
, "ffp_varying_specular = %s * specular;\n", specular
);
5808 /* Context activation is done by the caller. */
5809 static GLuint
shader_glsl_generate_ffp_vertex_shader(struct shader_glsl_priv
*priv
,
5810 const struct wined3d_ffp_vs_settings
*settings
, const struct wined3d_gl_info
*gl_info
)
5812 static const struct attrib_info
5815 const char name
[24];
5819 {"vec4", "ffp_attrib_position"}, /* WINED3D_FFP_POSITION */
5820 {"vec4", "ffp_attrib_blendweight"}, /* WINED3D_FFP_BLENDWEIGHT */
5821 /* TODO: Indexed vertex blending */
5822 {"float", ""}, /* WINED3D_FFP_BLENDINDICES */
5823 {"vec3", "ffp_attrib_normal"}, /* WINED3D_FFP_NORMAL */
5824 {"float", "ffp_attrib_psize"}, /* WINED3D_FFP_PSIZE */
5825 {"vec4", "ffp_attrib_diffuse"}, /* WINED3D_FFP_DIFFUSE */
5826 {"vec4", "ffp_attrib_specular"}, /* WINED3D_FFP_SPECULAR */
5828 struct wined3d_string_buffer
*buffer
= &priv
->shader_buffer
;
5829 BOOL legacy_lighting
= priv
->legacy_lighting
;
5832 BOOL legacy_context
= gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
];
5833 BOOL output_legacy_fogcoord
= legacy_context
;
5835 string_buffer_clear(buffer
);
5837 shader_addline(buffer
, "%s\n", shader_glsl_get_version(gl_info
, NULL
));
5839 for (i
= 0; i
< WINED3D_FFP_ATTRIBS_COUNT
; ++i
)
5841 const char *type
= i
< ARRAY_SIZE(attrib_info
) ? attrib_info
[i
].type
: "vec4";
5843 shader_addline(buffer
, "attribute %s vs_in%u;\n", type
, i
);
5845 shader_addline(buffer
, "\n");
5847 shader_addline(buffer
, "uniform mat4 ffp_modelview_matrix[%u];\n", MAX_VERTEX_BLENDS
);
5848 shader_addline(buffer
, "uniform mat4 ffp_projection_matrix;\n");
5849 shader_addline(buffer
, "uniform mat3 ffp_normal_matrix;\n");
5850 shader_addline(buffer
, "uniform mat4 ffp_texture_matrix[%u];\n", MAX_TEXTURES
);
5852 shader_addline(buffer
, "uniform struct\n{\n");
5853 shader_addline(buffer
, " vec4 emissive;\n");
5854 shader_addline(buffer
, " vec4 ambient;\n");
5855 shader_addline(buffer
, " vec4 diffuse;\n");
5856 shader_addline(buffer
, " vec4 specular;\n");
5857 shader_addline(buffer
, " float shininess;\n");
5858 shader_addline(buffer
, "} ffp_material;\n");
5860 shader_addline(buffer
, "uniform vec3 ffp_light_ambient;\n");
5861 shader_addline(buffer
, "uniform struct\n{\n");
5862 shader_addline(buffer
, " vec4 diffuse;\n");
5863 shader_addline(buffer
, " vec4 specular;\n");
5864 shader_addline(buffer
, " vec4 ambient;\n");
5865 shader_addline(buffer
, " vec4 position;\n");
5866 shader_addline(buffer
, " vec3 direction;\n");
5867 shader_addline(buffer
, " float range;\n");
5868 shader_addline(buffer
, " float falloff;\n");
5869 shader_addline(buffer
, " float c_att;\n");
5870 shader_addline(buffer
, " float l_att;\n");
5871 shader_addline(buffer
, " float q_att;\n");
5872 shader_addline(buffer
, " float cos_htheta;\n");
5873 shader_addline(buffer
, " float cos_hphi;\n");
5874 shader_addline(buffer
, "} ffp_light[%u];\n", MAX_ACTIVE_LIGHTS
);
5876 if (settings
->point_size
)
5878 shader_addline(buffer
, "uniform struct\n{\n");
5879 shader_addline(buffer
, " float size;\n");
5880 shader_addline(buffer
, " float size_min;\n");
5881 shader_addline(buffer
, " float size_max;\n");
5882 shader_addline(buffer
, " float c_att;\n");
5883 shader_addline(buffer
, " float l_att;\n");
5884 shader_addline(buffer
, " float q_att;\n");
5885 shader_addline(buffer
, "} ffp_point;\n");
5890 shader_addline(buffer
, "vec4 ffp_varying_diffuse;\n");
5891 shader_addline(buffer
, "vec4 ffp_varying_specular;\n");
5892 shader_addline(buffer
, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES
);
5893 shader_addline(buffer
, "float ffp_varying_fogcoord;\n");
5897 declare_out_varying(gl_info
, buffer
, settings
->flatshading
, "vec4 ffp_varying_diffuse;\n");
5898 declare_out_varying(gl_info
, buffer
, settings
->flatshading
, "vec4 ffp_varying_specular;\n");
5899 declare_out_varying(gl_info
, buffer
, FALSE
, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES
);
5900 declare_out_varying(gl_info
, buffer
, FALSE
, "float ffp_varying_fogcoord;\n");
5903 shader_addline(buffer
, "\nvoid main()\n{\n");
5904 shader_addline(buffer
, "float m;\n");
5905 shader_addline(buffer
, "vec3 r;\n");
5907 for (i
= 0; i
< ARRAY_SIZE(attrib_info
); ++i
)
5909 if (attrib_info
[i
].name
[0])
5910 shader_addline(buffer
, "%s %s = vs_in%u;\n",
5911 attrib_info
[i
].type
, attrib_info
[i
].name
, i
);
5913 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
5915 unsigned int coord_idx
= settings
->texgen
[i
] & 0x0000ffff;
5916 if ((settings
->texgen
[i
] & 0xffff0000) == WINED3DTSS_TCI_PASSTHRU
)
5918 shader_addline(buffer
, "vec4 ffp_attrib_texcoord%u = vs_in%u;\n", i
, coord_idx
+ WINED3D_FFP_TEXCOORD0
);
5922 shader_addline(buffer
, "ffp_attrib_blendweight[%u] = 1.0;\n", settings
->vertexblends
);
5924 if (settings
->transformed
)
5926 shader_addline(buffer
, "vec4 ec_pos = vec4(ffp_attrib_position.xyz, 1.0);\n");
5927 shader_addline(buffer
, "gl_Position = ffp_projection_matrix * ec_pos;\n");
5928 shader_addline(buffer
, "if (ffp_attrib_position.w != 0.0) gl_Position /= ffp_attrib_position.w;\n");
5932 for (i
= 0; i
< settings
->vertexblends
; ++i
)
5933 shader_addline(buffer
, "ffp_attrib_blendweight[%u] -= ffp_attrib_blendweight[%u];\n", settings
->vertexblends
, i
);
5935 shader_addline(buffer
, "vec4 ec_pos = vec4(0.0);\n");
5936 for (i
= 0; i
< settings
->vertexblends
+ 1; ++i
)
5937 shader_addline(buffer
, "ec_pos += ffp_attrib_blendweight[%u] * (ffp_modelview_matrix[%u] * ffp_attrib_position);\n", i
, i
);
5939 shader_addline(buffer
, "gl_Position = ffp_projection_matrix * ec_pos;\n");
5940 if (settings
->clipping
)
5941 shader_addline(buffer
, "gl_ClipVertex = ec_pos;\n");
5942 shader_addline(buffer
, "ec_pos /= ec_pos.w;\n");
5945 shader_addline(buffer
, "vec3 normal = vec3(0.0);\n");
5946 if (settings
->normal
)
5948 if (!settings
->vertexblends
)
5950 shader_addline(buffer
, "normal = ffp_normal_matrix * ffp_attrib_normal;\n");
5954 for (i
= 0; i
< settings
->vertexblends
+ 1; ++i
)
5955 shader_addline(buffer
, "normal += ffp_attrib_blendweight[%u] * (mat3(ffp_modelview_matrix[%u]) * ffp_attrib_normal);\n", i
, i
);
5958 if (settings
->normalize
)
5959 shader_addline(buffer
, "normal = normalize(normal);\n");
5962 shader_glsl_ffp_vertex_lighting(buffer
, settings
, legacy_lighting
);
5965 shader_addline(buffer
, "gl_FrontColor = ffp_varying_diffuse;\n");
5966 shader_addline(buffer
, "gl_FrontSecondaryColor = ffp_varying_specular;\n");
5969 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
5971 BOOL output_legacy_texcoord
= gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
];
5973 switch (settings
->texgen
[i
] & 0xffff0000)
5975 case WINED3DTSS_TCI_PASSTHRU
:
5976 if (settings
->texcoords
& (1u << i
))
5977 shader_addline(buffer
, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ffp_attrib_texcoord%u;\n",
5979 else if (gl_info
->limits
.glsl_varyings
>= wined3d_max_compat_varyings(gl_info
))
5980 shader_addline(buffer
, "ffp_varying_texcoord[%u] = vec4(0.0);\n", i
);
5982 output_legacy_texcoord
= FALSE
;
5985 case WINED3DTSS_TCI_CAMERASPACENORMAL
:
5986 shader_addline(buffer
, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * vec4(normal, 1.0);\n", i
, i
);
5989 case WINED3DTSS_TCI_CAMERASPACEPOSITION
:
5990 shader_addline(buffer
, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ec_pos;\n", i
, i
);
5993 case WINED3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR
:
5994 shader_addline(buffer
, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
5995 " * vec4(reflect(normalize(ec_pos.xyz), normal), 1.0);\n", i
, i
);
5998 case WINED3DTSS_TCI_SPHEREMAP
:
5999 shader_addline(buffer
, "r = reflect(normalize(ec_pos.xyz), normal);\n");
6000 shader_addline(buffer
, "m = 2.0 * length(vec3(r.x, r.y, r.z + 1.0));\n");
6001 shader_addline(buffer
, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
6002 " * vec4(r.x / m + 0.5, r.y / m + 0.5, 0.0, 1.0);\n", i
, i
);
6006 ERR("Unhandled texgen %#x.\n", settings
->texgen
[i
]);
6009 if (output_legacy_texcoord
)
6010 shader_addline(buffer
, "gl_TexCoord[%u] = ffp_varying_texcoord[%u];\n", i
, i
);
6013 switch (settings
->fog_mode
)
6015 case WINED3D_FFP_VS_FOG_OFF
:
6016 output_legacy_fogcoord
= FALSE
;
6019 case WINED3D_FFP_VS_FOG_FOGCOORD
:
6020 shader_addline(buffer
, "ffp_varying_fogcoord = ffp_attrib_specular.w * 255.0;\n");
6023 case WINED3D_FFP_VS_FOG_RANGE
:
6024 shader_addline(buffer
, "ffp_varying_fogcoord = length(ec_pos.xyz);\n");
6027 case WINED3D_FFP_VS_FOG_DEPTH
:
6028 if (settings
->ortho_fog
)
6029 /* Need to undo the [0.0 - 1.0] -> [-1.0 - 1.0] transformation from D3D to GL coordinates. */
6030 shader_addline(buffer
, "ffp_varying_fogcoord = gl_Position.z * 0.5 + 0.5;\n");
6031 else if (settings
->transformed
)
6032 shader_addline(buffer
, "ffp_varying_fogcoord = ec_pos.z;\n");
6034 shader_addline(buffer
, "ffp_varying_fogcoord = abs(ec_pos.z);\n");
6038 ERR("Unhandled fog mode %#x.\n", settings
->fog_mode
);
6041 if (output_legacy_fogcoord
)
6042 shader_addline(buffer
, "gl_FogFragCoord = ffp_varying_fogcoord;\n");
6044 if (settings
->point_size
)
6046 shader_addline(buffer
, "gl_PointSize = %s / sqrt(ffp_point.c_att"
6047 " + ffp_point.l_att * length(ec_pos.xyz)"
6048 " + ffp_point.q_att * dot(ec_pos.xyz, ec_pos.xyz));\n",
6049 settings
->per_vertex_point_size
? "ffp_attrib_psize" : "ffp_point.size");
6050 shader_addline(buffer
, "gl_PointSize = clamp(gl_PointSize, ffp_point.size_min, ffp_point.size_max);\n");
6053 shader_addline(buffer
, "}\n");
6055 shader_obj
= GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER
));
6056 shader_glsl_compile(gl_info
, shader_obj
, buffer
->buffer
);
6061 static const char *shader_glsl_get_ffp_fragment_op_arg(struct wined3d_string_buffer
*buffer
,
6062 DWORD argnum
, unsigned int stage
, DWORD arg
)
6066 if (arg
== ARG_UNUSED
)
6067 return "<unused arg>";
6069 switch (arg
& WINED3DTA_SELECTMASK
)
6071 case WINED3DTA_DIFFUSE
:
6072 ret
= "ffp_varying_diffuse";
6075 case WINED3DTA_CURRENT
:
6079 case WINED3DTA_TEXTURE
:
6082 case 0: ret
= "tex0"; break;
6083 case 1: ret
= "tex1"; break;
6084 case 2: ret
= "tex2"; break;
6085 case 3: ret
= "tex3"; break;
6086 case 4: ret
= "tex4"; break;
6087 case 5: ret
= "tex5"; break;
6088 case 6: ret
= "tex6"; break;
6089 case 7: ret
= "tex7"; break;
6091 ret
= "<invalid texture>";
6096 case WINED3DTA_TFACTOR
:
6100 case WINED3DTA_SPECULAR
:
6101 ret
= "ffp_varying_specular";
6104 case WINED3DTA_TEMP
:
6108 case WINED3DTA_CONSTANT
:
6111 case 0: ret
= "tss_const0"; break;
6112 case 1: ret
= "tss_const1"; break;
6113 case 2: ret
= "tss_const2"; break;
6114 case 3: ret
= "tss_const3"; break;
6115 case 4: ret
= "tss_const4"; break;
6116 case 5: ret
= "tss_const5"; break;
6117 case 6: ret
= "tss_const6"; break;
6118 case 7: ret
= "tss_const7"; break;
6120 ret
= "<invalid constant>";
6126 return "<unhandled arg>";
6129 if (arg
& WINED3DTA_COMPLEMENT
)
6131 shader_addline(buffer
, "arg%u = vec4(1.0) - %s;\n", argnum
, ret
);
6134 else if (argnum
== 1)
6136 else if (argnum
== 2)
6140 if (arg
& WINED3DTA_ALPHAREPLICATE
)
6142 shader_addline(buffer
, "arg%u = vec4(%s.w);\n", argnum
, ret
);
6145 else if (argnum
== 1)
6147 else if (argnum
== 2)
6154 static void shader_glsl_ffp_fragment_op(struct wined3d_string_buffer
*buffer
, unsigned int stage
, BOOL color
,
6155 BOOL alpha
, DWORD dst
, DWORD op
, DWORD dw_arg0
, DWORD dw_arg1
, DWORD dw_arg2
)
6157 const char *dstmask
, *dstreg
, *arg0
, *arg1
, *arg2
;
6167 dstreg
= "temp_reg";
6171 arg0
= shader_glsl_get_ffp_fragment_op_arg(buffer
, 0, stage
, dw_arg0
);
6172 arg1
= shader_glsl_get_ffp_fragment_op_arg(buffer
, 1, stage
, dw_arg1
);
6173 arg2
= shader_glsl_get_ffp_fragment_op_arg(buffer
, 2, stage
, dw_arg2
);
6177 case WINED3D_TOP_DISABLE
:
6180 case WINED3D_TOP_SELECT_ARG1
:
6181 shader_addline(buffer
, "%s%s = %s%s;\n", dstreg
, dstmask
, arg1
, dstmask
);
6184 case WINED3D_TOP_SELECT_ARG2
:
6185 shader_addline(buffer
, "%s%s = %s%s;\n", dstreg
, dstmask
, arg2
, dstmask
);
6188 case WINED3D_TOP_MODULATE
:
6189 shader_addline(buffer
, "%s%s = %s%s * %s%s;\n", dstreg
, dstmask
, arg1
, dstmask
, arg2
, dstmask
);
6192 case WINED3D_TOP_MODULATE_4X
:
6193 shader_addline(buffer
, "%s%s = clamp(%s%s * %s%s * 4.0, 0.0, 1.0);\n",
6194 dstreg
, dstmask
, arg1
, dstmask
, arg2
, dstmask
);
6197 case WINED3D_TOP_MODULATE_2X
:
6198 shader_addline(buffer
, "%s%s = clamp(%s%s * %s%s * 2.0, 0.0, 1.0);\n",
6199 dstreg
, dstmask
, arg1
, dstmask
, arg2
, dstmask
);
6202 case WINED3D_TOP_ADD
:
6203 shader_addline(buffer
, "%s%s = clamp(%s%s + %s%s, 0.0, 1.0);\n",
6204 dstreg
, dstmask
, arg1
, dstmask
, arg2
, dstmask
);
6207 case WINED3D_TOP_ADD_SIGNED
:
6208 shader_addline(buffer
, "%s%s = clamp(%s%s + (%s - vec4(0.5))%s, 0.0, 1.0);\n",
6209 dstreg
, dstmask
, arg1
, dstmask
, arg2
, dstmask
);
6212 case WINED3D_TOP_ADD_SIGNED_2X
:
6213 shader_addline(buffer
, "%s%s = clamp((%s%s + (%s - vec4(0.5))%s) * 2.0, 0.0, 1.0);\n",
6214 dstreg
, dstmask
, arg1
, dstmask
, arg2
, dstmask
);
6217 case WINED3D_TOP_SUBTRACT
:
6218 shader_addline(buffer
, "%s%s = clamp(%s%s - %s%s, 0.0, 1.0);\n",
6219 dstreg
, dstmask
, arg1
, dstmask
, arg2
, dstmask
);
6222 case WINED3D_TOP_ADD_SMOOTH
:
6223 shader_addline(buffer
, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s%s, 0.0, 1.0);\n",
6224 dstreg
, dstmask
, arg1
, dstmask
, arg2
, dstmask
, arg1
, dstmask
);
6227 case WINED3D_TOP_BLEND_DIFFUSE_ALPHA
:
6228 arg0
= shader_glsl_get_ffp_fragment_op_arg(buffer
, 0, stage
, WINED3DTA_DIFFUSE
);
6229 shader_addline(buffer
, "%s%s = mix(%s%s, %s%s, %s.w);\n",
6230 dstreg
, dstmask
, arg2
, dstmask
, arg1
, dstmask
, arg0
);
6233 case WINED3D_TOP_BLEND_TEXTURE_ALPHA
:
6234 arg0
= shader_glsl_get_ffp_fragment_op_arg(buffer
, 0, stage
, WINED3DTA_TEXTURE
);
6235 shader_addline(buffer
, "%s%s = mix(%s%s, %s%s, %s.w);\n",
6236 dstreg
, dstmask
, arg2
, dstmask
, arg1
, dstmask
, arg0
);
6239 case WINED3D_TOP_BLEND_FACTOR_ALPHA
:
6240 arg0
= shader_glsl_get_ffp_fragment_op_arg(buffer
, 0, stage
, WINED3DTA_TFACTOR
);
6241 shader_addline(buffer
, "%s%s = mix(%s%s, %s%s, %s.w);\n",
6242 dstreg
, dstmask
, arg2
, dstmask
, arg1
, dstmask
, arg0
);
6245 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM
:
6246 arg0
= shader_glsl_get_ffp_fragment_op_arg(buffer
, 0, stage
, WINED3DTA_TEXTURE
);
6247 shader_addline(buffer
, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
6248 dstreg
, dstmask
, arg2
, dstmask
, arg0
, arg1
, dstmask
);
6251 case WINED3D_TOP_BLEND_CURRENT_ALPHA
:
6252 arg0
= shader_glsl_get_ffp_fragment_op_arg(buffer
, 0, stage
, WINED3DTA_CURRENT
);
6253 shader_addline(buffer
, "%s%s = mix(%s%s, %s%s, %s.w);\n",
6254 dstreg
, dstmask
, arg2
, dstmask
, arg1
, dstmask
, arg0
);
6257 case WINED3D_TOP_MODULATE_ALPHA_ADD_COLOR
:
6258 shader_addline(buffer
, "%s%s = clamp(%s%s * %s.w + %s%s, 0.0, 1.0);\n",
6259 dstreg
, dstmask
, arg2
, dstmask
, arg1
, arg1
, dstmask
);
6262 case WINED3D_TOP_MODULATE_COLOR_ADD_ALPHA
:
6263 shader_addline(buffer
, "%s%s = clamp(%s%s * %s%s + %s.w, 0.0, 1.0);\n",
6264 dstreg
, dstmask
, arg1
, dstmask
, arg2
, dstmask
, arg1
);
6267 case WINED3D_TOP_MODULATE_INVALPHA_ADD_COLOR
:
6268 shader_addline(buffer
, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
6269 dstreg
, dstmask
, arg2
, dstmask
, arg1
, arg1
, dstmask
);
6271 case WINED3D_TOP_MODULATE_INVCOLOR_ADD_ALPHA
:
6272 shader_addline(buffer
, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s.w, 0.0, 1.0);\n",
6273 dstreg
, dstmask
, arg1
, dstmask
, arg2
, dstmask
, arg1
);
6276 case WINED3D_TOP_BUMPENVMAP
:
6277 case WINED3D_TOP_BUMPENVMAP_LUMINANCE
:
6278 /* These are handled in the first pass, nothing to do. */
6281 case WINED3D_TOP_DOTPRODUCT3
:
6282 shader_addline(buffer
, "%s%s = vec4(clamp(dot(%s.xyz - 0.5, %s.xyz - 0.5) * 4.0, 0.0, 1.0))%s;\n",
6283 dstreg
, dstmask
, arg1
, arg2
, dstmask
);
6286 case WINED3D_TOP_MULTIPLY_ADD
:
6287 shader_addline(buffer
, "%s%s = clamp(%s%s * %s%s + %s%s, 0.0, 1.0);\n",
6288 dstreg
, dstmask
, arg1
, dstmask
, arg2
, dstmask
, arg0
, dstmask
);
6291 case WINED3D_TOP_LERP
:
6292 /* MSDN isn't quite right here. */
6293 shader_addline(buffer
, "%s%s = mix(%s%s, %s%s, %s%s);\n",
6294 dstreg
, dstmask
, arg2
, dstmask
, arg1
, dstmask
, arg0
, dstmask
);
6298 FIXME("Unhandled operation %#x.\n", op
);
6303 /* Context activation is done by the caller. */
6304 static GLuint
shader_glsl_generate_ffp_fragment_shader(struct shader_glsl_priv
*priv
,
6305 const struct ffp_frag_settings
*settings
, const struct wined3d_gl_info
*gl_info
)
6307 struct wined3d_string_buffer
*buffer
= &priv
->shader_buffer
;
6308 BYTE lum_map
= 0, bump_map
= 0, tex_map
= 0, tss_const_map
= 0;
6309 BOOL tempreg_used
= FALSE
, tfactor_used
= FALSE
;
6310 UINT lowest_disabled_stage
;
6312 DWORD arg0
, arg1
, arg2
;
6314 struct wined3d_string_buffer
*tex_reg_name
= string_buffer_get(&priv
->string_buffers
);
6315 BOOL legacy_context
= gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
];
6317 string_buffer_clear(buffer
);
6319 /* Find out which textures are read */
6320 for (stage
= 0; stage
< MAX_TEXTURES
; ++stage
)
6322 if (settings
->op
[stage
].cop
== WINED3D_TOP_DISABLE
)
6325 arg0
= settings
->op
[stage
].carg0
& WINED3DTA_SELECTMASK
;
6326 arg1
= settings
->op
[stage
].carg1
& WINED3DTA_SELECTMASK
;
6327 arg2
= settings
->op
[stage
].carg2
& WINED3DTA_SELECTMASK
;
6329 if (arg0
== WINED3DTA_TEXTURE
|| arg1
== WINED3DTA_TEXTURE
|| arg2
== WINED3DTA_TEXTURE
6330 || (stage
== 0 && settings
->color_key_enabled
))
6331 tex_map
|= 1u << stage
;
6332 if (arg0
== WINED3DTA_TFACTOR
|| arg1
== WINED3DTA_TFACTOR
|| arg2
== WINED3DTA_TFACTOR
)
6333 tfactor_used
= TRUE
;
6334 if (arg0
== WINED3DTA_TEMP
|| arg1
== WINED3DTA_TEMP
|| arg2
== WINED3DTA_TEMP
)
6335 tempreg_used
= TRUE
;
6336 if (settings
->op
[stage
].dst
== tempreg
)
6337 tempreg_used
= TRUE
;
6338 if (arg0
== WINED3DTA_CONSTANT
|| arg1
== WINED3DTA_CONSTANT
|| arg2
== WINED3DTA_CONSTANT
)
6339 tss_const_map
|= 1u << stage
;
6341 switch (settings
->op
[stage
].cop
)
6343 case WINED3D_TOP_BUMPENVMAP_LUMINANCE
:
6344 lum_map
|= 1u << stage
;
6346 case WINED3D_TOP_BUMPENVMAP
:
6347 bump_map
|= 1u << stage
;
6349 case WINED3D_TOP_BLEND_TEXTURE_ALPHA
:
6350 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM
:
6351 tex_map
|= 1u << stage
;
6354 case WINED3D_TOP_BLEND_FACTOR_ALPHA
:
6355 tfactor_used
= TRUE
;
6362 if (settings
->op
[stage
].aop
== WINED3D_TOP_DISABLE
)
6365 arg0
= settings
->op
[stage
].aarg0
& WINED3DTA_SELECTMASK
;
6366 arg1
= settings
->op
[stage
].aarg1
& WINED3DTA_SELECTMASK
;
6367 arg2
= settings
->op
[stage
].aarg2
& WINED3DTA_SELECTMASK
;
6369 if (arg0
== WINED3DTA_TEXTURE
|| arg1
== WINED3DTA_TEXTURE
|| arg2
== WINED3DTA_TEXTURE
)
6370 tex_map
|= 1u << stage
;
6371 if (arg0
== WINED3DTA_TFACTOR
|| arg1
== WINED3DTA_TFACTOR
|| arg2
== WINED3DTA_TFACTOR
)
6372 tfactor_used
= TRUE
;
6373 if (arg0
== WINED3DTA_TEMP
|| arg1
== WINED3DTA_TEMP
|| arg2
== WINED3DTA_TEMP
)
6374 tempreg_used
= TRUE
;
6375 if (arg0
== WINED3DTA_CONSTANT
|| arg1
== WINED3DTA_CONSTANT
|| arg2
== WINED3DTA_CONSTANT
)
6376 tss_const_map
|= 1u << stage
;
6378 lowest_disabled_stage
= stage
;
6380 shader_addline(buffer
, "%s\n", shader_glsl_get_version(gl_info
, NULL
));
6382 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
6383 shader_addline(buffer
, "#extension GL_ARB_texture_rectangle : enable\n");
6385 shader_addline(buffer
, "vec4 tmp0, tmp1;\n");
6386 shader_addline(buffer
, "vec4 ret;\n");
6387 if (tempreg_used
|| settings
->sRGB_write
)
6388 shader_addline(buffer
, "vec4 temp_reg = vec4(0.0);\n");
6389 shader_addline(buffer
, "vec4 arg0, arg1, arg2;\n");
6391 for (stage
= 0; stage
< MAX_TEXTURES
; ++stage
)
6393 if (tss_const_map
& (1u << stage
))
6394 shader_addline(buffer
, "uniform vec4 tss_const%u;\n", stage
);
6396 if (!(tex_map
& (1u << stage
)))
6399 switch (settings
->op
[stage
].tex_type
)
6401 case WINED3D_GL_RES_TYPE_TEX_1D
:
6402 shader_addline(buffer
, "uniform sampler1D ps_sampler%u;\n", stage
);
6404 case WINED3D_GL_RES_TYPE_TEX_2D
:
6405 shader_addline(buffer
, "uniform sampler2D ps_sampler%u;\n", stage
);
6407 case WINED3D_GL_RES_TYPE_TEX_3D
:
6408 shader_addline(buffer
, "uniform sampler3D ps_sampler%u;\n", stage
);
6410 case WINED3D_GL_RES_TYPE_TEX_CUBE
:
6411 shader_addline(buffer
, "uniform samplerCube ps_sampler%u;\n", stage
);
6413 case WINED3D_GL_RES_TYPE_TEX_RECT
:
6414 shader_addline(buffer
, "uniform sampler2DRect ps_sampler%u;\n", stage
);
6417 FIXME("Unhandled sampler type %#x.\n", settings
->op
[stage
].tex_type
);
6421 shader_addline(buffer
, "vec4 tex%u;\n", stage
);
6423 if (!(bump_map
& (1u << stage
)))
6425 shader_addline(buffer
, "uniform mat2 bumpenv_mat%u;\n", stage
);
6427 if (!(lum_map
& (1u << stage
)))
6429 shader_addline(buffer
, "uniform float bumpenv_lum_scale%u;\n", stage
);
6430 shader_addline(buffer
, "uniform float bumpenv_lum_offset%u;\n", stage
);
6433 shader_addline(buffer
, "uniform vec4 tex_factor;\n");
6434 if (settings
->color_key_enabled
)
6435 shader_addline(buffer
, "uniform vec4 color_key[2];\n");
6436 shader_addline(buffer
, "uniform vec4 specular_enable;\n");
6438 if (settings
->sRGB_write
)
6440 shader_addline(buffer
, "const vec4 srgb_const0 = ");
6441 shader_glsl_append_imm_vec4(buffer
, wined3d_srgb_const0
);
6442 shader_addline(buffer
, ";\n");
6443 shader_addline(buffer
, "const vec4 srgb_const1 = ");
6444 shader_glsl_append_imm_vec4(buffer
, wined3d_srgb_const1
);
6445 shader_addline(buffer
, ";\n");
6448 shader_addline(buffer
, "uniform struct\n{\n");
6449 shader_addline(buffer
, " vec4 color;\n");
6450 shader_addline(buffer
, " float density;\n");
6451 shader_addline(buffer
, " float end;\n");
6452 shader_addline(buffer
, " float scale;\n");
6453 shader_addline(buffer
, "} ffp_fog;\n");
6457 shader_addline(buffer
, "vec4 ffp_varying_diffuse;\n");
6458 shader_addline(buffer
, "vec4 ffp_varying_specular;\n");
6459 shader_addline(buffer
, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES
);
6460 shader_addline(buffer
, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES
);
6461 shader_addline(buffer
, "float ffp_varying_fogcoord;\n");
6465 declare_in_varying(gl_info
, buffer
, settings
->flatshading
, "vec4 ffp_varying_diffuse;\n");
6466 declare_in_varying(gl_info
, buffer
, settings
->flatshading
, "vec4 ffp_varying_specular;\n");
6467 declare_in_varying(gl_info
, buffer
, FALSE
, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES
);
6468 shader_addline(buffer
, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES
);
6469 declare_in_varying(gl_info
, buffer
, FALSE
, "float ffp_varying_fogcoord;\n");
6472 shader_addline(buffer
, "void main()\n{\n");
6476 shader_addline(buffer
, "ffp_varying_diffuse = gl_Color;\n");
6477 shader_addline(buffer
, "ffp_varying_specular = gl_SecondaryColor;\n");
6480 for (stage
= 0; stage
< MAX_TEXTURES
; ++stage
)
6482 if (tex_map
& (1u << stage
))
6484 if (settings
->pointsprite
)
6485 shader_addline(buffer
, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", stage
);
6486 else if (settings
->texcoords_initialized
& (1u << stage
))
6487 shader_addline(buffer
, "ffp_texcoord[%u] = %s[%u];\n",
6488 stage
, legacy_context
? "gl_TexCoord" : "ffp_varying_texcoord", stage
);
6490 shader_addline(buffer
, "ffp_texcoord[%u] = vec4(0.0);\n", stage
);
6494 if (legacy_context
&& settings
->fog
!= WINED3D_FFP_PS_FOG_OFF
)
6495 shader_addline(buffer
, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
6497 if (lowest_disabled_stage
< 7 && settings
->emul_clipplanes
)
6498 shader_addline(buffer
, "if (any(lessThan(ffp_texcoord[7], vec4(0.0)))) discard;\n");
6500 /* Generate texture sampling instructions */
6501 for (stage
= 0; stage
< MAX_TEXTURES
&& settings
->op
[stage
].cop
!= WINED3D_TOP_DISABLE
; ++stage
)
6503 const char *texture_function
, *coord_mask
;
6506 if (!(tex_map
& (1u << stage
)))
6509 if (settings
->op
[stage
].projected
== proj_none
)
6513 else if (settings
->op
[stage
].projected
== proj_count4
6514 || settings
->op
[stage
].projected
== proj_count3
)
6520 FIXME("Unexpected projection mode %d\n", settings
->op
[stage
].projected
);
6524 if (settings
->op
[stage
].tex_type
== WINED3D_GL_RES_TYPE_TEX_CUBE
)
6527 switch (settings
->op
[stage
].tex_type
)
6529 case WINED3D_GL_RES_TYPE_TEX_1D
:
6532 texture_function
= "texture1DProj";
6537 texture_function
= "texture1D";
6541 case WINED3D_GL_RES_TYPE_TEX_2D
:
6544 texture_function
= "texture2DProj";
6549 texture_function
= "texture2D";
6553 case WINED3D_GL_RES_TYPE_TEX_3D
:
6556 texture_function
= "texture3DProj";
6557 coord_mask
= "xyzw";
6561 texture_function
= "texture3D";
6565 case WINED3D_GL_RES_TYPE_TEX_CUBE
:
6566 texture_function
= "textureCube";
6569 case WINED3D_GL_RES_TYPE_TEX_RECT
:
6572 texture_function
= "texture2DRectProj";
6577 texture_function
= "texture2DRect";
6582 FIXME("Unhandled texture type %#x.\n", settings
->op
[stage
].tex_type
);
6583 texture_function
= "";
6584 coord_mask
= "xyzw";
6587 if (!needs_legacy_glsl_syntax(gl_info
))
6588 texture_function
= proj
? "textureProj" : "texture";
6591 && (settings
->op
[stage
- 1].cop
== WINED3D_TOP_BUMPENVMAP
6592 || settings
->op
[stage
- 1].cop
== WINED3D_TOP_BUMPENVMAP_LUMINANCE
))
6594 shader_addline(buffer
, "ret.xy = bumpenv_mat%u * tex%u.xy;\n", stage
- 1, stage
- 1);
6596 /* With projective textures, texbem only divides the static
6597 * texture coord, not the displacement, so multiply the
6598 * displacement with the dividing parameter before passing it to
6600 if (settings
->op
[stage
].projected
!= proj_none
)
6602 if (settings
->op
[stage
].projected
== proj_count4
)
6604 shader_addline(buffer
, "ret.xy = (ret.xy * ffp_texcoord[%u].w) + ffp_texcoord[%u].xy;\n",
6606 shader_addline(buffer
, "ret.zw = ffp_texcoord[%u].ww;\n", stage
);
6610 shader_addline(buffer
, "ret.xy = (ret.xy * ffp_texcoord[%u].z) + ffp_texcoord[%u].xy;\n",
6612 shader_addline(buffer
, "ret.zw = ffp_texcoord[%u].zz;\n", stage
);
6617 shader_addline(buffer
, "ret = ffp_texcoord[%u] + ret.xyxy;\n", stage
);
6620 shader_addline(buffer
, "tex%u = %s(ps_sampler%u, ret.%s);\n",
6621 stage
, texture_function
, stage
, coord_mask
);
6623 if (settings
->op
[stage
- 1].cop
== WINED3D_TOP_BUMPENVMAP_LUMINANCE
)
6624 shader_addline(buffer
, "tex%u *= clamp(tex%u.z * bumpenv_lum_scale%u + bumpenv_lum_offset%u, 0.0, 1.0);\n",
6625 stage
, stage
- 1, stage
- 1, stage
- 1);
6627 else if (settings
->op
[stage
].projected
== proj_count3
)
6629 shader_addline(buffer
, "tex%u = %s(ps_sampler%u, ffp_texcoord[%u].xyz);\n",
6630 stage
, texture_function
, stage
, stage
);
6634 shader_addline(buffer
, "tex%u = %s(ps_sampler%u, ffp_texcoord[%u].%s);\n",
6635 stage
, texture_function
, stage
, stage
, coord_mask
);
6638 string_buffer_sprintf(tex_reg_name
, "tex%u", stage
);
6639 shader_glsl_color_correction_ext(buffer
, tex_reg_name
->buffer
, WINED3DSP_WRITEMASK_ALL
,
6640 settings
->op
[stage
].color_fixup
);
6643 if (settings
->color_key_enabled
)
6645 shader_addline(buffer
, "if (all(greaterThanEqual(tex0, color_key[0])) && all(lessThan(tex0, color_key[1])))\n");
6646 shader_addline(buffer
, " discard;\n");
6649 shader_addline(buffer
, "ret = ffp_varying_diffuse;\n");
6651 /* Generate the main shader */
6652 for (stage
= 0; stage
< MAX_TEXTURES
; ++stage
)
6656 if (settings
->op
[stage
].cop
== WINED3D_TOP_DISABLE
)
6659 if (settings
->op
[stage
].cop
== WINED3D_TOP_SELECT_ARG1
6660 && settings
->op
[stage
].aop
== WINED3D_TOP_SELECT_ARG1
)
6661 op_equal
= settings
->op
[stage
].carg1
== settings
->op
[stage
].aarg1
;
6662 else if (settings
->op
[stage
].cop
== WINED3D_TOP_SELECT_ARG1
6663 && settings
->op
[stage
].aop
== WINED3D_TOP_SELECT_ARG2
)
6664 op_equal
= settings
->op
[stage
].carg1
== settings
->op
[stage
].aarg2
;
6665 else if (settings
->op
[stage
].cop
== WINED3D_TOP_SELECT_ARG2
6666 && settings
->op
[stage
].aop
== WINED3D_TOP_SELECT_ARG1
)
6667 op_equal
= settings
->op
[stage
].carg2
== settings
->op
[stage
].aarg1
;
6668 else if (settings
->op
[stage
].cop
== WINED3D_TOP_SELECT_ARG2
6669 && settings
->op
[stage
].aop
== WINED3D_TOP_SELECT_ARG2
)
6670 op_equal
= settings
->op
[stage
].carg2
== settings
->op
[stage
].aarg2
;
6672 op_equal
= settings
->op
[stage
].aop
== settings
->op
[stage
].cop
6673 && settings
->op
[stage
].carg0
== settings
->op
[stage
].aarg0
6674 && settings
->op
[stage
].carg1
== settings
->op
[stage
].aarg1
6675 && settings
->op
[stage
].carg2
== settings
->op
[stage
].aarg2
;
6677 if (settings
->op
[stage
].aop
== WINED3D_TOP_DISABLE
)
6679 shader_glsl_ffp_fragment_op(buffer
, stage
, TRUE
, FALSE
, settings
->op
[stage
].dst
,
6680 settings
->op
[stage
].cop
, settings
->op
[stage
].carg0
,
6681 settings
->op
[stage
].carg1
, settings
->op
[stage
].carg2
);
6685 shader_glsl_ffp_fragment_op(buffer
, stage
, TRUE
, TRUE
, settings
->op
[stage
].dst
,
6686 settings
->op
[stage
].cop
, settings
->op
[stage
].carg0
,
6687 settings
->op
[stage
].carg1
, settings
->op
[stage
].carg2
);
6689 else if (settings
->op
[stage
].cop
!= WINED3D_TOP_BUMPENVMAP
6690 && settings
->op
[stage
].cop
!= WINED3D_TOP_BUMPENVMAP_LUMINANCE
)
6692 shader_glsl_ffp_fragment_op(buffer
, stage
, TRUE
, FALSE
, settings
->op
[stage
].dst
,
6693 settings
->op
[stage
].cop
, settings
->op
[stage
].carg0
,
6694 settings
->op
[stage
].carg1
, settings
->op
[stage
].carg2
);
6695 shader_glsl_ffp_fragment_op(buffer
, stage
, FALSE
, TRUE
, settings
->op
[stage
].dst
,
6696 settings
->op
[stage
].aop
, settings
->op
[stage
].aarg0
,
6697 settings
->op
[stage
].aarg1
, settings
->op
[stage
].aarg2
);
6701 shader_addline(buffer
, "gl_FragData[0] = ffp_varying_specular * specular_enable + ret;\n");
6703 if (settings
->sRGB_write
)
6704 shader_glsl_generate_srgb_write_correction(buffer
);
6706 shader_glsl_generate_fog_code(buffer
, settings
->fog
);
6708 shader_addline(buffer
, "}\n");
6710 shader_id
= GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER
));
6711 shader_glsl_compile(gl_info
, shader_id
, buffer
->buffer
);
6713 string_buffer_release(&priv
->string_buffers
, tex_reg_name
);
6717 static struct glsl_ffp_vertex_shader
*shader_glsl_find_ffp_vertex_shader(struct shader_glsl_priv
*priv
,
6718 const struct wined3d_gl_info
*gl_info
, const struct wined3d_ffp_vs_settings
*settings
)
6720 struct glsl_ffp_vertex_shader
*shader
;
6721 const struct wine_rb_entry
*entry
;
6723 if ((entry
= wine_rb_get(&priv
->ffp_vertex_shaders
, settings
)))
6724 return WINE_RB_ENTRY_VALUE(entry
, struct glsl_ffp_vertex_shader
, desc
.entry
);
6726 if (!(shader
= HeapAlloc(GetProcessHeap(), 0, sizeof(*shader
))))
6729 shader
->desc
.settings
= *settings
;
6730 shader
->id
= shader_glsl_generate_ffp_vertex_shader(priv
, settings
, gl_info
);
6731 list_init(&shader
->linked_programs
);
6732 if (wine_rb_put(&priv
->ffp_vertex_shaders
, &shader
->desc
.settings
, &shader
->desc
.entry
) == -1)
6733 ERR("Failed to insert ffp vertex shader.\n");
6738 static struct glsl_ffp_fragment_shader
*shader_glsl_find_ffp_fragment_shader(struct shader_glsl_priv
*priv
,
6739 const struct wined3d_gl_info
*gl_info
, const struct ffp_frag_settings
*args
)
6741 struct glsl_ffp_fragment_shader
*glsl_desc
;
6742 const struct ffp_frag_desc
*desc
;
6744 if ((desc
= find_ffp_frag_shader(&priv
->ffp_fragment_shaders
, args
)))
6745 return CONTAINING_RECORD(desc
, struct glsl_ffp_fragment_shader
, entry
);
6747 if (!(glsl_desc
= HeapAlloc(GetProcessHeap(), 0, sizeof(*glsl_desc
))))
6750 glsl_desc
->entry
.settings
= *args
;
6751 glsl_desc
->id
= shader_glsl_generate_ffp_fragment_shader(priv
, args
, gl_info
);
6752 list_init(&glsl_desc
->linked_programs
);
6753 add_ffp_frag_shader(&priv
->ffp_fragment_shaders
, &glsl_desc
->entry
);
6759 static void shader_glsl_init_vs_uniform_locations(const struct wined3d_gl_info
*gl_info
,
6760 struct shader_glsl_priv
*priv
, GLuint program_id
, struct glsl_vs_program
*vs
, unsigned int vs_c_count
)
6763 struct wined3d_string_buffer
*name
= string_buffer_get(&priv
->string_buffers
);
6765 vs
->uniform_f_locations
= HeapAlloc(GetProcessHeap(), 0,
6766 sizeof(GLuint
) * gl_info
->limits
.glsl_vs_float_constants
);
6767 for (i
= 0; i
< vs_c_count
; ++i
)
6769 string_buffer_sprintf(name
, "vs_c[%u]", i
);
6770 vs
->uniform_f_locations
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
6772 memset(&vs
->uniform_f_locations
[vs_c_count
], 0xff,
6773 (gl_info
->limits
.glsl_vs_float_constants
- vs_c_count
) * sizeof(GLuint
));
6775 for (i
= 0; i
< MAX_CONST_I
; ++i
)
6777 string_buffer_sprintf(name
, "vs_i[%u]", i
);
6778 vs
->uniform_i_locations
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
6781 for (i
= 0; i
< MAX_CONST_B
; ++i
)
6783 string_buffer_sprintf(name
, "vs_b[%u]", i
);
6784 vs
->uniform_b_locations
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
6787 vs
->pos_fixup_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "posFixup"));
6789 for (i
= 0; i
< MAX_VERTEX_BLENDS
; ++i
)
6791 string_buffer_sprintf(name
, "ffp_modelview_matrix[%u]", i
);
6792 vs
->modelview_matrix_location
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
6794 vs
->projection_matrix_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_projection_matrix"));
6795 vs
->normal_matrix_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_normal_matrix"));
6796 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
6798 string_buffer_sprintf(name
, "ffp_texture_matrix[%u]", i
);
6799 vs
->texture_matrix_location
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
6801 vs
->material_ambient_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_material.ambient"));
6802 vs
->material_diffuse_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_material.diffuse"));
6803 vs
->material_specular_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_material.specular"));
6804 vs
->material_emissive_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_material.emissive"));
6805 vs
->material_shininess_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_material.shininess"));
6806 vs
->light_ambient_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_light_ambient"));
6807 for (i
= 0; i
< MAX_ACTIVE_LIGHTS
; ++i
)
6809 string_buffer_sprintf(name
, "ffp_light[%u].diffuse", i
);
6810 vs
->light_location
[i
].diffuse
= GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
6811 string_buffer_sprintf(name
, "ffp_light[%u].specular", i
);
6812 vs
->light_location
[i
].specular
= GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
6813 string_buffer_sprintf(name
, "ffp_light[%u].ambient", i
);
6814 vs
->light_location
[i
].ambient
= GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
6815 string_buffer_sprintf(name
, "ffp_light[%u].position", i
);
6816 vs
->light_location
[i
].position
= GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
6817 string_buffer_sprintf(name
, "ffp_light[%u].direction", i
);
6818 vs
->light_location
[i
].direction
= GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
6819 string_buffer_sprintf(name
, "ffp_light[%u].range", i
);
6820 vs
->light_location
[i
].range
= GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
6821 string_buffer_sprintf(name
, "ffp_light[%u].falloff", i
);
6822 vs
->light_location
[i
].falloff
= GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
6823 string_buffer_sprintf(name
, "ffp_light[%u].c_att", i
);
6824 vs
->light_location
[i
].c_att
= GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
6825 string_buffer_sprintf(name
, "ffp_light[%u].l_att", i
);
6826 vs
->light_location
[i
].l_att
= GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
6827 string_buffer_sprintf(name
, "ffp_light[%u].q_att", i
);
6828 vs
->light_location
[i
].q_att
= GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
6829 string_buffer_sprintf(name
, "ffp_light[%u].cos_htheta", i
);
6830 vs
->light_location
[i
].cos_htheta
= GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
6831 string_buffer_sprintf(name
, "ffp_light[%u].cos_hphi", i
);
6832 vs
->light_location
[i
].cos_hphi
= GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
6834 vs
->pointsize_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_point.size"));
6835 vs
->pointsize_min_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_point.size_min"));
6836 vs
->pointsize_max_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_point.size_max"));
6837 vs
->pointsize_c_att_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_point.c_att"));
6838 vs
->pointsize_l_att_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_point.l_att"));
6839 vs
->pointsize_q_att_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_point.q_att"));
6841 string_buffer_release(&priv
->string_buffers
, name
);
6844 static void shader_glsl_init_ps_uniform_locations(const struct wined3d_gl_info
*gl_info
,
6845 struct shader_glsl_priv
*priv
, GLuint program_id
, struct glsl_ps_program
*ps
, unsigned int ps_c_count
)
6848 struct wined3d_string_buffer
*name
= string_buffer_get(&priv
->string_buffers
);
6850 ps
->uniform_f_locations
= HeapAlloc(GetProcessHeap(), 0,
6851 sizeof(GLuint
) * gl_info
->limits
.glsl_ps_float_constants
);
6852 for (i
= 0; i
< ps_c_count
; ++i
)
6854 string_buffer_sprintf(name
, "ps_c[%u]", i
);
6855 ps
->uniform_f_locations
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
6857 memset(&ps
->uniform_f_locations
[ps_c_count
], 0xff,
6858 (gl_info
->limits
.glsl_ps_float_constants
- ps_c_count
) * sizeof(GLuint
));
6860 for (i
= 0; i
< MAX_CONST_I
; ++i
)
6862 string_buffer_sprintf(name
, "ps_i[%u]", i
);
6863 ps
->uniform_i_locations
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
6866 for (i
= 0; i
< MAX_CONST_B
; ++i
)
6868 string_buffer_sprintf(name
, "ps_b[%u]", i
);
6869 ps
->uniform_b_locations
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
6872 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
6874 string_buffer_sprintf(name
, "bumpenv_mat%u", i
);
6875 ps
->bumpenv_mat_location
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
6876 string_buffer_sprintf(name
, "bumpenv_lum_scale%u", i
);
6877 ps
->bumpenv_lum_scale_location
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
6878 string_buffer_sprintf(name
, "bumpenv_lum_offset%u", i
);
6879 ps
->bumpenv_lum_offset_location
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
6880 string_buffer_sprintf(name
, "tss_const%u", i
);
6881 ps
->tss_constant_location
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
6884 ps
->tex_factor_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "tex_factor"));
6885 ps
->specular_enable_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "specular_enable"));
6887 ps
->fog_color_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_fog.color"));
6888 ps
->fog_density_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_fog.density"));
6889 ps
->fog_end_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_fog.end"));
6890 ps
->fog_scale_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_fog.scale"));
6892 ps
->np2_fixup_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ps_samplerNP2Fixup"));
6893 ps
->ycorrection_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ycorrection"));
6894 ps
->color_key_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "color_key"));
6896 string_buffer_release(&priv
->string_buffers
, name
);
6899 static void shader_glsl_init_uniform_block_bindings(const struct wined3d_gl_info
*gl_info
,
6900 struct shader_glsl_priv
*priv
, GLuint program_id
,
6901 const struct wined3d_shader_reg_maps
*reg_maps
, unsigned int base
, unsigned int count
)
6903 const char *prefix
= shader_glsl_get_prefix(reg_maps
->shader_version
.type
);
6906 struct wined3d_string_buffer
*name
= string_buffer_get(&priv
->string_buffers
);
6908 for (i
= 0; i
< count
; ++i
)
6910 if (!reg_maps
->cb_sizes
[i
])
6913 string_buffer_sprintf(name
, "block_%s_cb%u", prefix
, i
);
6914 block_idx
= GL_EXTCALL(glGetUniformBlockIndex(program_id
, name
->buffer
));
6915 GL_EXTCALL(glUniformBlockBinding(program_id
, block_idx
, base
+ i
));
6917 checkGLcall("glUniformBlockBinding");
6918 string_buffer_release(&priv
->string_buffers
, name
);
6921 /* Context activation is done by the caller. */
6922 static void set_glsl_shader_program(const struct wined3d_context
*context
, const struct wined3d_state
*state
,
6923 struct shader_glsl_priv
*priv
, struct glsl_context_data
*ctx_data
)
6925 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
6926 const struct wined3d_d3d_info
*d3d_info
= context
->d3d_info
;
6927 const struct ps_np2fixup_info
*np2fixup_info
= NULL
;
6928 struct glsl_shader_prog_link
*entry
= NULL
;
6929 struct wined3d_shader
*vshader
= NULL
;
6930 struct wined3d_shader
*gshader
= NULL
;
6931 struct wined3d_shader
*pshader
= NULL
;
6933 GLuint reorder_shader_id
= 0;
6938 struct list
*ps_list
, *vs_list
;
6940 struct wined3d_string_buffer
*tmp_name
;
6942 if (!(context
->shader_update_mask
& (1u << WINED3D_SHADER_TYPE_VERTEX
)) && ctx_data
->glsl_program
)
6944 vs_id
= ctx_data
->glsl_program
->vs
.id
;
6945 vs_list
= &ctx_data
->glsl_program
->vs
.shader_entry
;
6949 vshader
= state
->shader
[WINED3D_SHADER_TYPE_VERTEX
];
6950 gshader
= state
->shader
[WINED3D_SHADER_TYPE_GEOMETRY
];
6952 if (!(context
->shader_update_mask
& (1u << WINED3D_SHADER_TYPE_GEOMETRY
))
6953 && ctx_data
->glsl_program
->gs
.id
)
6954 gs_id
= ctx_data
->glsl_program
->gs
.id
;
6956 gs_id
= find_glsl_geometry_shader(context
, &priv
->shader_buffer
, &priv
->string_buffers
, gshader
);
6959 else if (use_vs(state
))
6961 struct vs_compile_args vs_compile_args
;
6963 vshader
= state
->shader
[WINED3D_SHADER_TYPE_VERTEX
];
6965 find_vs_compile_args(state
, vshader
, context
->stream_info
.swizzle_map
, &vs_compile_args
, d3d_info
);
6966 vs_id
= find_glsl_vshader(context
, &priv
->shader_buffer
, &priv
->string_buffers
, vshader
, &vs_compile_args
);
6967 vs_list
= &vshader
->linked_programs
;
6969 if ((gshader
= state
->shader
[WINED3D_SHADER_TYPE_GEOMETRY
]))
6970 gs_id
= find_glsl_geometry_shader(context
, &priv
->shader_buffer
, &priv
->string_buffers
, gshader
);
6972 else if (priv
->vertex_pipe
== &glsl_vertex_pipe
)
6974 struct glsl_ffp_vertex_shader
*ffp_shader
;
6975 struct wined3d_ffp_vs_settings settings
;
6977 wined3d_ffp_get_vs_settings(context
, state
, &settings
);
6978 ffp_shader
= shader_glsl_find_ffp_vertex_shader(priv
, gl_info
, &settings
);
6979 vs_id
= ffp_shader
->id
;
6980 vs_list
= &ffp_shader
->linked_programs
;
6983 if (!(context
->shader_update_mask
& (1u << WINED3D_SHADER_TYPE_PIXEL
)) && ctx_data
->glsl_program
)
6985 ps_id
= ctx_data
->glsl_program
->ps
.id
;
6986 ps_list
= &ctx_data
->glsl_program
->ps
.shader_entry
;
6989 pshader
= state
->shader
[WINED3D_SHADER_TYPE_PIXEL
];
6991 else if (use_ps(state
))
6993 struct ps_compile_args ps_compile_args
;
6994 pshader
= state
->shader
[WINED3D_SHADER_TYPE_PIXEL
];
6995 find_ps_compile_args(state
, pshader
, context
->stream_info
.position_transformed
, &ps_compile_args
, context
);
6996 ps_id
= find_glsl_pshader(context
, &priv
->shader_buffer
, &priv
->string_buffers
,
6997 pshader
, &ps_compile_args
, &np2fixup_info
);
6998 ps_list
= &pshader
->linked_programs
;
7000 else if (priv
->fragment_pipe
== &glsl_fragment_pipe
)
7002 struct glsl_ffp_fragment_shader
*ffp_shader
;
7003 struct ffp_frag_settings settings
;
7005 gen_ffp_frag_op(context
, state
, &settings
, FALSE
);
7006 ffp_shader
= shader_glsl_find_ffp_fragment_shader(priv
, gl_info
, &settings
);
7007 ps_id
= ffp_shader
->id
;
7008 ps_list
= &ffp_shader
->linked_programs
;
7011 if ((!vs_id
&& !gs_id
&& !ps_id
) || (entry
= get_glsl_program_entry(priv
, vs_id
, gs_id
, ps_id
)))
7013 ctx_data
->glsl_program
= entry
;
7017 /* If we get to this point, then no matching program exists, so we create one */
7018 program_id
= GL_EXTCALL(glCreateProgram());
7019 TRACE("Created new GLSL shader program %u.\n", program_id
);
7021 /* Create the entry */
7022 entry
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link
));
7023 entry
->id
= program_id
;
7024 entry
->vs
.id
= vs_id
;
7025 entry
->gs
.id
= gs_id
;
7026 entry
->ps
.id
= ps_id
;
7027 entry
->constant_version
= 0;
7028 entry
->ps
.np2_fixup_info
= np2fixup_info
;
7029 /* Add the hash table entry */
7030 add_glsl_program_entry(priv
, entry
);
7032 /* Set the current program */
7033 ctx_data
->glsl_program
= entry
;
7035 /* Attach GLSL vshader */
7038 TRACE("Attaching GLSL shader object %u to program %u.\n", vs_id
, program_id
);
7039 GL_EXTCALL(glAttachShader(program_id
, vs_id
));
7040 checkGLcall("glAttachShader");
7042 list_add_head(vs_list
, &entry
->vs
.shader_entry
);
7047 attribs_map
= vshader
->reg_maps
.input_registers
;
7048 reorder_shader_id
= generate_param_reorder_function(priv
, vshader
, pshader
,
7049 state
->gl_primitive_type
== GL_POINTS
&& vshader
->reg_maps
.point_size
,
7050 d3d_info
->emulated_flatshading
7051 && state
->render_states
[WINED3D_RS_SHADEMODE
] == WINED3D_SHADE_FLAT
, gl_info
);
7052 TRACE("Attaching GLSL shader object %u to program %u.\n", reorder_shader_id
, program_id
);
7053 GL_EXTCALL(glAttachShader(program_id
, reorder_shader_id
));
7054 checkGLcall("glAttachShader");
7055 /* Flag the reorder function for deletion, then it will be freed automatically when the program
7058 GL_EXTCALL(glDeleteShader(reorder_shader_id
));
7062 attribs_map
= (1u << WINED3D_FFP_ATTRIBS_COUNT
) - 1;
7065 /* Bind vertex attributes to a corresponding index number to match
7066 * the same index numbers as ARB_vertex_programs (makes loading
7067 * vertex attributes simpler). With this method, we can use the
7068 * exact same code to load the attributes later for both ARB and
7071 * We have to do this here because we need to know the Program ID
7072 * in order to make the bindings work, and it has to be done prior
7073 * to linking the GLSL program. */
7074 tmp_name
= string_buffer_get(&priv
->string_buffers
);
7075 for (i
= 0; attribs_map
; attribs_map
>>= 1, ++i
)
7077 if (!(attribs_map
& 1))
7080 string_buffer_sprintf(tmp_name
, "vs_in%u", i
);
7081 GL_EXTCALL(glBindAttribLocation(program_id
, i
, tmp_name
->buffer
));
7083 checkGLcall("glBindAttribLocation");
7084 string_buffer_release(&priv
->string_buffers
, tmp_name
);
7088 TRACE("Attaching GLSL geometry shader object %u to program %u.\n", gs_id
, program_id
);
7089 GL_EXTCALL(glAttachShader(program_id
, gs_id
));
7090 checkGLcall("glAttachShader");
7092 TRACE("input type %s, output type %s, vertices out %u.\n",
7093 debug_d3dprimitivetype(gshader
->u
.gs
.input_type
),
7094 debug_d3dprimitivetype(gshader
->u
.gs
.output_type
),
7095 gshader
->u
.gs
.vertices_out
);
7096 GL_EXTCALL(glProgramParameteriARB(program_id
, GL_GEOMETRY_INPUT_TYPE_ARB
,
7097 gl_primitive_type_from_d3d(gshader
->u
.gs
.input_type
)));
7098 GL_EXTCALL(glProgramParameteriARB(program_id
, GL_GEOMETRY_OUTPUT_TYPE_ARB
,
7099 gl_primitive_type_from_d3d(gshader
->u
.gs
.output_type
)));
7100 GL_EXTCALL(glProgramParameteriARB(program_id
, GL_GEOMETRY_VERTICES_OUT_ARB
,
7101 gshader
->u
.gs
.vertices_out
));
7102 checkGLcall("glProgramParameteriARB");
7104 list_add_head(&gshader
->linked_programs
, &entry
->gs
.shader_entry
);
7107 /* Attach GLSL pshader */
7110 TRACE("Attaching GLSL shader object %u to program %u.\n", ps_id
, program_id
);
7111 GL_EXTCALL(glAttachShader(program_id
, ps_id
));
7112 checkGLcall("glAttachShader");
7114 list_add_head(ps_list
, &entry
->ps
.shader_entry
);
7117 /* Link the program */
7118 TRACE("Linking GLSL shader program %u.\n", program_id
);
7119 GL_EXTCALL(glLinkProgram(program_id
));
7120 shader_glsl_validate_link(gl_info
, program_id
);
7122 shader_glsl_init_vs_uniform_locations(gl_info
, priv
, program_id
, &entry
->vs
,
7123 vshader
? min(vshader
->limits
->constant_float
, gl_info
->limits
.glsl_vs_float_constants
) : 0);
7124 shader_glsl_init_ps_uniform_locations(gl_info
, priv
, program_id
, &entry
->ps
,
7125 pshader
? min(pshader
->limits
->constant_float
, gl_info
->limits
.glsl_ps_float_constants
) : 0);
7126 checkGLcall("Find glsl program uniform locations");
7128 if (pshader
&& pshader
->reg_maps
.shader_version
.major
>= 3
7129 && pshader
->u
.ps
.declared_in_count
> vec4_varyings(3, gl_info
))
7131 TRACE("Shader %d needs vertex color clamping disabled.\n", program_id
);
7132 entry
->vs
.vertex_color_clamp
= GL_FALSE
;
7136 entry
->vs
.vertex_color_clamp
= GL_FIXED_ONLY_ARB
;
7139 /* Set the shader to allow uniform loading on it */
7140 GL_EXTCALL(glUseProgram(program_id
));
7141 checkGLcall("glUseProgram");
7143 /* Texture unit mapping is set up to be the same each time the shader
7144 * program is used so we can hardcode the sampler uniform values. */
7145 shader_glsl_load_samplers(gl_info
, priv
, context
->tex_unit_map
, program_id
);
7147 entry
->constant_update_mask
= 0;
7150 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_VS_F
;
7151 if (vshader
->reg_maps
.integer_constants
)
7152 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_VS_I
;
7153 if (vshader
->reg_maps
.boolean_constants
)
7154 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_VS_B
;
7155 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_VS_POS_FIXUP
;
7157 shader_glsl_init_uniform_block_bindings(gl_info
, priv
, program_id
, &vshader
->reg_maps
,
7158 0, gl_info
->limits
.vertex_uniform_blocks
);
7162 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_MODELVIEW
7163 | WINED3D_SHADER_CONST_FFP_PROJ
;
7165 for (i
= 1; i
< MAX_VERTEX_BLENDS
; ++i
)
7167 if (entry
->vs
.modelview_matrix_location
[i
] != -1)
7169 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_VERTEXBLEND
;
7174 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
7176 if (entry
->vs
.texture_matrix_location
[i
] != -1)
7178 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_TEXMATRIX
;
7182 if (entry
->vs
.material_ambient_location
!= -1 || entry
->vs
.material_diffuse_location
!= -1
7183 || entry
->vs
.material_specular_location
!= -1
7184 || entry
->vs
.material_emissive_location
!= -1
7185 || entry
->vs
.material_shininess_location
!= -1)
7186 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_MATERIAL
;
7187 if (entry
->vs
.light_ambient_location
!= -1)
7188 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_LIGHTS
;
7190 if (entry
->vs
.pointsize_min_location
!= -1)
7191 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_VS_POINTSIZE
;
7194 shader_glsl_init_uniform_block_bindings(gl_info
, priv
, program_id
, &gshader
->reg_maps
,
7195 gl_info
->limits
.vertex_uniform_blocks
, gl_info
->limits
.geometry_uniform_blocks
);
7201 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_PS_F
;
7202 if (pshader
->reg_maps
.integer_constants
)
7203 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_PS_I
;
7204 if (pshader
->reg_maps
.boolean_constants
)
7205 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_PS_B
;
7206 if (entry
->ps
.ycorrection_location
!= -1)
7207 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_PS_Y_CORR
;
7209 shader_glsl_init_uniform_block_bindings(gl_info
, priv
, program_id
, &pshader
->reg_maps
,
7210 gl_info
->limits
.vertex_uniform_blocks
+ gl_info
->limits
.geometry_uniform_blocks
,
7211 gl_info
->limits
.fragment_uniform_blocks
);
7215 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_PS
;
7218 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
7220 if (entry
->ps
.bumpenv_mat_location
[i
] != -1)
7222 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_PS_BUMP_ENV
;
7227 if (entry
->ps
.fog_color_location
!= -1)
7228 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_PS_FOG
;
7229 if (entry
->ps
.np2_fixup_location
!= -1)
7230 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_PS_NP2_FIXUP
;
7231 if (entry
->ps
.color_key_location
!= -1)
7232 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_COLOR_KEY
;
7236 /* Context activation is done by the caller. */
7237 static GLuint
create_glsl_blt_shader(const struct wined3d_gl_info
*gl_info
, enum wined3d_gl_resource_type tex_type
,
7241 GLuint vshader_id
, pshader_id
;
7242 const char *blt_pshader
;
7244 static const char blt_vshader
[] =
7248 " gl_Position = gl_Vertex;\n"
7249 " gl_FrontColor = vec4(1.0);\n"
7250 " gl_TexCoord[0] = gl_MultiTexCoord0;\n"
7253 static const char * const blt_pshaders_full
[WINED3D_GL_RES_TYPE_COUNT
] =
7255 /* WINED3D_GL_RES_TYPE_TEX_1D */
7257 /* WINED3D_GL_RES_TYPE_TEX_2D */
7259 "uniform sampler2D sampler;\n"
7262 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
7264 /* WINED3D_GL_RES_TYPE_TEX_3D */
7266 /* WINED3D_GL_RES_TYPE_TEX_CUBE */
7268 "uniform samplerCube sampler;\n"
7271 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
7273 /* WINED3D_GL_RES_TYPE_TEX_RECT */
7275 "#extension GL_ARB_texture_rectangle : enable\n"
7276 "uniform sampler2DRect sampler;\n"
7279 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
7281 /* WINED3D_GL_RES_TYPE_BUFFER */
7283 /* WINED3D_GL_RES_TYPE_RB */
7287 static const char * const blt_pshaders_masked
[WINED3D_GL_RES_TYPE_COUNT
] =
7289 /* WINED3D_GL_RES_TYPE_TEX_1D */
7291 /* WINED3D_GL_RES_TYPE_TEX_2D */
7293 "uniform sampler2D sampler;\n"
7294 "uniform vec4 mask;\n"
7297 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
7298 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
7300 /* WINED3D_GL_RES_TYPE_TEX_3D */
7302 /* WINED3D_GL_RES_TYPE_TEX_CUBE */
7304 "uniform samplerCube sampler;\n"
7305 "uniform vec4 mask;\n"
7308 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
7309 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
7311 /* WINED3D_GL_RES_TYPE_TEX_RECT */
7313 "#extension GL_ARB_texture_rectangle : enable\n"
7314 "uniform sampler2DRect sampler;\n"
7315 "uniform vec4 mask;\n"
7318 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
7319 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
7321 /* WINED3D_GL_RES_TYPE_BUFFER */
7323 /* WINED3D_GL_RES_TYPE_RB */
7327 blt_pshader
= masked
? blt_pshaders_masked
[tex_type
] : blt_pshaders_full
[tex_type
];
7330 FIXME("tex_type %#x not supported\n", tex_type
);
7334 vshader_id
= GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER
));
7335 shader_glsl_compile(gl_info
, vshader_id
, blt_vshader
);
7337 pshader_id
= GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER
));
7338 shader_glsl_compile(gl_info
, pshader_id
, blt_pshader
);
7340 program_id
= GL_EXTCALL(glCreateProgram());
7341 GL_EXTCALL(glAttachShader(program_id
, vshader_id
));
7342 GL_EXTCALL(glAttachShader(program_id
, pshader_id
));
7343 GL_EXTCALL(glLinkProgram(program_id
));
7345 shader_glsl_validate_link(gl_info
, program_id
);
7347 /* Once linked we can mark the shaders for deletion. They will be deleted once the program
7350 GL_EXTCALL(glDeleteShader(vshader_id
));
7351 GL_EXTCALL(glDeleteShader(pshader_id
));
7355 /* Context activation is done by the caller. */
7356 static void shader_glsl_select(void *shader_priv
, struct wined3d_context
*context
,
7357 const struct wined3d_state
*state
)
7359 struct glsl_context_data
*ctx_data
= context
->shader_backend_data
;
7360 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
7361 struct shader_glsl_priv
*priv
= shader_priv
;
7362 GLuint program_id
= 0, prev_id
= 0;
7363 GLenum old_vertex_color_clamp
, current_vertex_color_clamp
;
7365 priv
->vertex_pipe
->vp_enable(gl_info
, !use_vs(state
));
7366 priv
->fragment_pipe
->enable_extension(gl_info
, !use_ps(state
));
7368 if (ctx_data
->glsl_program
)
7370 prev_id
= ctx_data
->glsl_program
->id
;
7371 old_vertex_color_clamp
= ctx_data
->glsl_program
->vs
.vertex_color_clamp
;
7376 old_vertex_color_clamp
= GL_FIXED_ONLY_ARB
;
7379 set_glsl_shader_program(context
, state
, priv
, ctx_data
);
7381 if (ctx_data
->glsl_program
)
7383 program_id
= ctx_data
->glsl_program
->id
;
7384 current_vertex_color_clamp
= ctx_data
->glsl_program
->vs
.vertex_color_clamp
;
7389 current_vertex_color_clamp
= GL_FIXED_ONLY_ARB
;
7392 if (old_vertex_color_clamp
!= current_vertex_color_clamp
)
7394 if (gl_info
->supported
[ARB_COLOR_BUFFER_FLOAT
])
7396 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB
, current_vertex_color_clamp
));
7397 checkGLcall("glClampColorARB");
7401 FIXME("vertex color clamp needs to be changed, but extension not supported.\n");
7405 TRACE("Using GLSL program %u.\n", program_id
);
7407 if (prev_id
!= program_id
)
7409 GL_EXTCALL(glUseProgram(program_id
));
7410 checkGLcall("glUseProgram");
7413 context
->constant_update_mask
|= ctx_data
->glsl_program
->constant_update_mask
;
7417 /* "context" is not necessarily the currently active context. */
7418 static void shader_glsl_invalidate_current_program(struct wined3d_context
*context
)
7420 struct glsl_context_data
*ctx_data
= context
->shader_backend_data
;
7422 ctx_data
->glsl_program
= NULL
;
7423 context
->shader_update_mask
= (1u << WINED3D_SHADER_TYPE_PIXEL
)
7424 | (1u << WINED3D_SHADER_TYPE_VERTEX
)
7425 | (1u << WINED3D_SHADER_TYPE_GEOMETRY
);
7428 /* Context activation is done by the caller. */
7429 static void shader_glsl_disable(void *shader_priv
, struct wined3d_context
*context
)
7431 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
7432 struct shader_glsl_priv
*priv
= shader_priv
;
7434 shader_glsl_invalidate_current_program(context
);
7435 GL_EXTCALL(glUseProgram(0));
7436 checkGLcall("glUseProgram");
7438 priv
->vertex_pipe
->vp_enable(gl_info
, FALSE
);
7439 priv
->fragment_pipe
->enable_extension(gl_info
, FALSE
);
7441 if (gl_info
->supported
[ARB_COLOR_BUFFER_FLOAT
])
7443 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB
, GL_FIXED_ONLY_ARB
));
7444 checkGLcall("glClampColorARB");
7448 /* Context activation is done by the caller. */
7449 static void shader_glsl_select_depth_blt(void *shader_priv
, const struct wined3d_gl_info
*gl_info
,
7450 enum wined3d_gl_resource_type tex_type
, const SIZE
*ds_mask_size
)
7452 BOOL masked
= ds_mask_size
->cx
&& ds_mask_size
->cy
;
7453 struct shader_glsl_priv
*priv
= shader_priv
;
7454 GLuint
*blt_program
;
7457 blt_program
= masked
? &priv
->depth_blt_program_masked
[tex_type
] : &priv
->depth_blt_program_full
[tex_type
];
7460 *blt_program
= create_glsl_blt_shader(gl_info
, tex_type
, masked
);
7461 loc
= GL_EXTCALL(glGetUniformLocation(*blt_program
, "sampler"));
7462 GL_EXTCALL(glUseProgram(*blt_program
));
7463 GL_EXTCALL(glUniform1i(loc
, 0));
7467 GL_EXTCALL(glUseProgram(*blt_program
));
7472 loc
= GL_EXTCALL(glGetUniformLocation(*blt_program
, "mask"));
7473 GL_EXTCALL(glUniform4f(loc
, 0.0f
, 0.0f
, (float)ds_mask_size
->cx
, (float)ds_mask_size
->cy
));
7477 /* Context activation is done by the caller. */
7478 static void shader_glsl_deselect_depth_blt(void *shader_priv
, const struct wined3d_gl_info
*gl_info
)
7480 const struct glsl_context_data
*ctx_data
= context_get_current()->shader_backend_data
;
7483 program_id
= ctx_data
->glsl_program
? ctx_data
->glsl_program
->id
: 0;
7484 if (program_id
) TRACE("Using GLSL program %u\n", program_id
);
7486 GL_EXTCALL(glUseProgram(program_id
));
7487 checkGLcall("glUseProgram");
7490 static void shader_glsl_invalidate_contexts_program(struct wined3d_device
*device
,
7491 const struct glsl_shader_prog_link
*program
)
7493 const struct glsl_context_data
*ctx_data
;
7494 struct wined3d_context
*context
;
7497 for (i
= 0; i
< device
->context_count
; ++i
)
7499 context
= device
->contexts
[i
];
7500 ctx_data
= context
->shader_backend_data
;
7502 if (ctx_data
->glsl_program
== program
)
7503 shader_glsl_invalidate_current_program(context
);
7507 static void shader_glsl_destroy(struct wined3d_shader
*shader
)
7509 struct glsl_shader_private
*shader_data
= shader
->backend_data
;
7510 struct wined3d_device
*device
= shader
->device
;
7511 struct shader_glsl_priv
*priv
= device
->shader_priv
;
7512 const struct wined3d_gl_info
*gl_info
;
7513 const struct list
*linked_programs
;
7514 struct wined3d_context
*context
;
7516 if (!shader_data
|| !shader_data
->num_gl_shaders
)
7518 HeapFree(GetProcessHeap(), 0, shader_data
);
7519 shader
->backend_data
= NULL
;
7523 context
= context_acquire(device
, NULL
);
7524 gl_info
= context
->gl_info
;
7526 TRACE("Deleting linked programs.\n");
7527 linked_programs
= &shader
->linked_programs
;
7528 if (linked_programs
->next
)
7530 struct glsl_shader_prog_link
*entry
, *entry2
;
7533 switch (shader
->reg_maps
.shader_version
.type
)
7535 case WINED3D_SHADER_TYPE_PIXEL
:
7537 struct glsl_ps_compiled_shader
*gl_shaders
= shader_data
->gl_shaders
.ps
;
7539 for (i
= 0; i
< shader_data
->num_gl_shaders
; ++i
)
7541 TRACE("Deleting pixel shader %u.\n", gl_shaders
[i
].id
);
7542 GL_EXTCALL(glDeleteShader(gl_shaders
[i
].id
));
7543 checkGLcall("glDeleteShader");
7545 HeapFree(GetProcessHeap(), 0, shader_data
->gl_shaders
.ps
);
7547 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, linked_programs
,
7548 struct glsl_shader_prog_link
, ps
.shader_entry
)
7550 shader_glsl_invalidate_contexts_program(device
, entry
);
7551 delete_glsl_program_entry(priv
, gl_info
, entry
);
7557 case WINED3D_SHADER_TYPE_VERTEX
:
7559 struct glsl_vs_compiled_shader
*gl_shaders
= shader_data
->gl_shaders
.vs
;
7561 for (i
= 0; i
< shader_data
->num_gl_shaders
; ++i
)
7563 TRACE("Deleting vertex shader %u.\n", gl_shaders
[i
].id
);
7564 GL_EXTCALL(glDeleteShader(gl_shaders
[i
].id
));
7565 checkGLcall("glDeleteShader");
7567 HeapFree(GetProcessHeap(), 0, shader_data
->gl_shaders
.vs
);
7569 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, linked_programs
,
7570 struct glsl_shader_prog_link
, vs
.shader_entry
)
7572 shader_glsl_invalidate_contexts_program(device
, entry
);
7573 delete_glsl_program_entry(priv
, gl_info
, entry
);
7579 case WINED3D_SHADER_TYPE_GEOMETRY
:
7581 struct glsl_gs_compiled_shader
*gl_shaders
= shader_data
->gl_shaders
.gs
;
7583 for (i
= 0; i
< shader_data
->num_gl_shaders
; ++i
)
7585 TRACE("Deleting geometry shader %u.\n", gl_shaders
[i
].id
);
7586 GL_EXTCALL(glDeleteShader(gl_shaders
[i
].id
));
7587 checkGLcall("glDeleteShader");
7589 HeapFree(GetProcessHeap(), 0, shader_data
->gl_shaders
.gs
);
7591 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, linked_programs
,
7592 struct glsl_shader_prog_link
, gs
.shader_entry
)
7594 shader_glsl_invalidate_contexts_program(device
, entry
);
7595 delete_glsl_program_entry(priv
, gl_info
, entry
);
7602 ERR("Unhandled shader type %#x.\n", shader
->reg_maps
.shader_version
.type
);
7607 HeapFree(GetProcessHeap(), 0, shader
->backend_data
);
7608 shader
->backend_data
= NULL
;
7610 context_release(context
);
7613 static int glsl_program_key_compare(const void *key
, const struct wine_rb_entry
*entry
)
7615 const struct glsl_program_key
*k
= key
;
7616 const struct glsl_shader_prog_link
*prog
= WINE_RB_ENTRY_VALUE(entry
,
7617 const struct glsl_shader_prog_link
, program_lookup_entry
);
7619 if (k
->vs_id
> prog
->vs
.id
) return 1;
7620 else if (k
->vs_id
< prog
->vs
.id
) return -1;
7622 if (k
->gs_id
> prog
->gs
.id
) return 1;
7623 else if (k
->gs_id
< prog
->gs
.id
) return -1;
7625 if (k
->ps_id
> prog
->ps
.id
) return 1;
7626 else if (k
->ps_id
< prog
->ps
.id
) return -1;
7631 static BOOL
constant_heap_init(struct constant_heap
*heap
, unsigned int constant_count
)
7633 SIZE_T size
= (constant_count
+ 1) * sizeof(*heap
->entries
)
7634 + constant_count
* sizeof(*heap
->contained
)
7635 + constant_count
* sizeof(*heap
->positions
);
7636 void *mem
= HeapAlloc(GetProcessHeap(), 0, size
);
7640 ERR("Failed to allocate memory\n");
7644 heap
->entries
= mem
;
7645 heap
->entries
[1].version
= 0;
7646 heap
->contained
= (BOOL
*)(heap
->entries
+ constant_count
+ 1);
7647 memset(heap
->contained
, 0, constant_count
* sizeof(*heap
->contained
));
7648 heap
->positions
= (unsigned int *)(heap
->contained
+ constant_count
);
7654 static void constant_heap_free(struct constant_heap
*heap
)
7656 HeapFree(GetProcessHeap(), 0, heap
->entries
);
7659 static const struct wine_rb_functions wined3d_glsl_program_rb_functions
=
7664 glsl_program_key_compare
,
7667 static HRESULT
shader_glsl_alloc(struct wined3d_device
*device
, const struct wined3d_vertex_pipe_ops
*vertex_pipe
,
7668 const struct fragment_pipeline
*fragment_pipe
)
7670 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
7671 struct shader_glsl_priv
*priv
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(struct shader_glsl_priv
));
7672 SIZE_T stack_size
= wined3d_log2i(max(gl_info
->limits
.glsl_vs_float_constants
,
7673 gl_info
->limits
.glsl_ps_float_constants
)) + 1;
7674 struct fragment_caps fragment_caps
;
7675 void *vertex_priv
, *fragment_priv
;
7677 string_buffer_list_init(&priv
->string_buffers
);
7679 if (!(vertex_priv
= vertex_pipe
->vp_alloc(&glsl_shader_backend
, priv
)))
7681 ERR("Failed to initialize vertex pipe.\n");
7682 HeapFree(GetProcessHeap(), 0, priv
);
7686 if (!(fragment_priv
= fragment_pipe
->alloc_private(&glsl_shader_backend
, priv
)))
7688 ERR("Failed to initialize fragment pipe.\n");
7689 vertex_pipe
->vp_free(device
);
7690 HeapFree(GetProcessHeap(), 0, priv
);
7694 if (!string_buffer_init(&priv
->shader_buffer
))
7696 ERR("Failed to initialize shader buffer.\n");
7700 priv
->stack
= HeapAlloc(GetProcessHeap(), 0, stack_size
* sizeof(*priv
->stack
));
7703 ERR("Failed to allocate memory.\n");
7707 if (!constant_heap_init(&priv
->vconst_heap
, gl_info
->limits
.glsl_vs_float_constants
))
7709 ERR("Failed to initialize vertex shader constant heap\n");
7713 if (!constant_heap_init(&priv
->pconst_heap
, gl_info
->limits
.glsl_ps_float_constants
))
7715 ERR("Failed to initialize pixel shader constant heap\n");
7719 if (wine_rb_init(&priv
->program_lookup
, &wined3d_glsl_program_rb_functions
) == -1)
7721 ERR("Failed to initialize rbtree.\n");
7725 priv
->next_constant_version
= 1;
7726 priv
->vertex_pipe
= vertex_pipe
;
7727 priv
->fragment_pipe
= fragment_pipe
;
7728 fragment_pipe
->get_caps(gl_info
, &fragment_caps
);
7729 priv
->ffp_proj_control
= fragment_caps
.wined3d_caps
& WINED3D_FRAGMENT_CAP_PROJ_CONTROL
;
7730 priv
->legacy_lighting
= device
->wined3d
->flags
& WINED3D_LEGACY_FFP_LIGHTING
;
7732 device
->vertex_priv
= vertex_priv
;
7733 device
->fragment_priv
= fragment_priv
;
7734 device
->shader_priv
= priv
;
7739 constant_heap_free(&priv
->pconst_heap
);
7740 constant_heap_free(&priv
->vconst_heap
);
7741 HeapFree(GetProcessHeap(), 0, priv
->stack
);
7742 string_buffer_free(&priv
->shader_buffer
);
7743 fragment_pipe
->free_private(device
);
7744 vertex_pipe
->vp_free(device
);
7745 HeapFree(GetProcessHeap(), 0, priv
);
7746 return E_OUTOFMEMORY
;
7749 /* Context activation is done by the caller. */
7750 static void shader_glsl_free(struct wined3d_device
*device
)
7752 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
7753 struct shader_glsl_priv
*priv
= device
->shader_priv
;
7756 for (i
= 0; i
< WINED3D_GL_RES_TYPE_COUNT
; ++i
)
7758 if (priv
->depth_blt_program_full
[i
])
7760 GL_EXTCALL(glDeleteProgram(priv
->depth_blt_program_full
[i
]));
7762 if (priv
->depth_blt_program_masked
[i
])
7764 GL_EXTCALL(glDeleteProgram(priv
->depth_blt_program_masked
[i
]));
7768 wine_rb_destroy(&priv
->program_lookup
, NULL
, NULL
);
7769 constant_heap_free(&priv
->pconst_heap
);
7770 constant_heap_free(&priv
->vconst_heap
);
7771 HeapFree(GetProcessHeap(), 0, priv
->stack
);
7772 string_buffer_list_cleanup(&priv
->string_buffers
);
7773 string_buffer_free(&priv
->shader_buffer
);
7774 priv
->fragment_pipe
->free_private(device
);
7775 priv
->vertex_pipe
->vp_free(device
);
7777 HeapFree(GetProcessHeap(), 0, device
->shader_priv
);
7778 device
->shader_priv
= NULL
;
7781 static BOOL
shader_glsl_allocate_context_data(struct wined3d_context
*context
)
7783 return !!(context
->shader_backend_data
= HeapAlloc(GetProcessHeap(),
7784 HEAP_ZERO_MEMORY
, sizeof(struct glsl_context_data
)));
7787 static void shader_glsl_free_context_data(struct wined3d_context
*context
)
7789 HeapFree(GetProcessHeap(), 0, context
->shader_backend_data
);
7792 static void shader_glsl_init_context_state(struct wined3d_context
*context
)
7794 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
7796 gl_info
->gl_ops
.gl
.p_glEnable(GL_PROGRAM_POINT_SIZE
);
7797 checkGLcall("GL_PROGRAM_POINT_SIZE");
7800 static void shader_glsl_get_caps(const struct wined3d_gl_info
*gl_info
, struct shader_caps
*caps
)
7804 if (gl_info
->glsl_version
>= MAKEDWORD_VERSION(1, 50) && gl_info
->supported
[WINED3D_GL_VERSION_3_2
]
7805 && gl_info
->supported
[ARB_SHADER_BIT_ENCODING
] && gl_info
->supported
[ARB_SAMPLER_OBJECTS
])
7807 /* ARB_shader_texture_lod or EXT_gpu_shader4 is required for the SM3
7808 * texldd and texldl instructions. */
7809 else if (gl_info
->supported
[ARB_SHADER_TEXTURE_LOD
] || gl_info
->supported
[EXT_GPU_SHADER4
])
7813 TRACE("Shader model %u.\n", shader_model
);
7815 caps
->vs_version
= min(wined3d_settings
.max_sm_vs
, shader_model
);
7816 caps
->gs_version
= min(wined3d_settings
.max_sm_gs
, shader_model
);
7817 caps
->ps_version
= min(wined3d_settings
.max_sm_ps
, shader_model
);
7819 caps
->vs_uniform_count
= gl_info
->limits
.glsl_vs_float_constants
;
7820 caps
->ps_uniform_count
= gl_info
->limits
.glsl_ps_float_constants
;
7821 caps
->varying_count
= gl_info
->limits
.glsl_varyings
;
7823 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
7824 * Direct3D minimum requirement.
7826 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
7827 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
7829 * The problem is that the refrast clamps temporary results in the shader to
7830 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
7831 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
7832 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
7833 * offer a way to query this.
7835 if (shader_model
>= 4)
7836 caps
->ps_1x_max_value
= FLT_MAX
;
7838 caps
->ps_1x_max_value
= 1024.0f
;
7840 /* Ideally we'd only set caps like sRGB writes here if supported by both
7841 * the shader backend and the fragment pipe, but we can get called before
7842 * shader_glsl_alloc(). */
7843 caps
->wined3d_caps
= WINED3D_SHADER_CAP_VS_CLIPPING
7844 | WINED3D_SHADER_CAP_SRGB_WRITE
;
7847 static BOOL
shader_glsl_color_fixup_supported(struct color_fixup_desc fixup
)
7849 if (TRACE_ON(d3d_shader
) && TRACE_ON(d3d
))
7851 TRACE("Checking support for fixup:\n");
7852 dump_color_fixup_desc(fixup
);
7855 /* We support everything except YUV conversions. */
7856 if (!is_complex_fixup(fixup
))
7862 TRACE("[FAILED]\n");
7866 static const SHADER_HANDLER shader_glsl_instruction_handler_table
[WINED3DSIH_TABLE_SIZE
] =
7868 /* WINED3DSIH_ABS */ shader_glsl_map2gl
,
7869 /* WINED3DSIH_ADD */ shader_glsl_binop
,
7870 /* WINED3DSIH_AND */ shader_glsl_binop
,
7871 /* WINED3DSIH_BEM */ shader_glsl_bem
,
7872 /* WINED3DSIH_BREAK */ shader_glsl_break
,
7873 /* WINED3DSIH_BREAKC */ shader_glsl_breakc
,
7874 /* WINED3DSIH_BREAKP */ shader_glsl_breakp
,
7875 /* WINED3DSIH_CALL */ shader_glsl_call
,
7876 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz
,
7877 /* WINED3DSIH_CMP */ shader_glsl_conditional_move
,
7878 /* WINED3DSIH_CND */ shader_glsl_cnd
,
7879 /* WINED3DSIH_CRS */ shader_glsl_cross
,
7880 /* WINED3DSIH_CUT */ shader_glsl_cut
,
7881 /* WINED3DSIH_DCL */ shader_glsl_nop
,
7882 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ shader_glsl_nop
,
7883 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ shader_glsl_nop
,
7884 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ shader_glsl_nop
,
7885 /* WINED3DSIH_DCL_VERTICES_OUT */ shader_glsl_nop
,
7886 /* WINED3DSIH_DEF */ shader_glsl_nop
,
7887 /* WINED3DSIH_DEFB */ shader_glsl_nop
,
7888 /* WINED3DSIH_DEFI */ shader_glsl_nop
,
7889 /* WINED3DSIH_DIV */ shader_glsl_binop
,
7890 /* WINED3DSIH_DP2 */ shader_glsl_dot
,
7891 /* WINED3DSIH_DP2ADD */ shader_glsl_dp2add
,
7892 /* WINED3DSIH_DP3 */ shader_glsl_dot
,
7893 /* WINED3DSIH_DP4 */ shader_glsl_dot
,
7894 /* WINED3DSIH_DST */ shader_glsl_dst
,
7895 /* WINED3DSIH_DSX */ shader_glsl_map2gl
,
7896 /* WINED3DSIH_DSY */ shader_glsl_map2gl
,
7897 /* WINED3DSIH_ELSE */ shader_glsl_else
,
7898 /* WINED3DSIH_EMIT */ shader_glsl_emit
,
7899 /* WINED3DSIH_ENDIF */ shader_glsl_end
,
7900 /* WINED3DSIH_ENDLOOP */ shader_glsl_end
,
7901 /* WINED3DSIH_ENDREP */ shader_glsl_end
,
7902 /* WINED3DSIH_EQ */ shader_glsl_relop
,
7903 /* WINED3DSIH_EXP */ shader_glsl_scalar_op
,
7904 /* WINED3DSIH_EXPP */ shader_glsl_expp
,
7905 /* WINED3DSIH_FRC */ shader_glsl_map2gl
,
7906 /* WINED3DSIH_FTOI */ shader_glsl_to_int
,
7907 /* WINED3DSIH_FTOU */ shader_glsl_to_uint
,
7908 /* WINED3DSIH_GE */ shader_glsl_relop
,
7909 /* WINED3DSIH_IADD */ shader_glsl_binop
,
7910 /* WINED3DSIH_IEQ */ NULL
,
7911 /* WINED3DSIH_IF */ shader_glsl_if
,
7912 /* WINED3DSIH_IFC */ shader_glsl_ifc
,
7913 /* WINED3DSIH_IGE */ shader_glsl_relop
,
7914 /* WINED3DSIH_ILT */ NULL
,
7915 /* WINED3DSIH_IMAD */ NULL
,
7916 /* WINED3DSIH_IMAX */ shader_glsl_map2gl
,
7917 /* WINED3DSIH_IMIN */ shader_glsl_map2gl
,
7918 /* WINED3DSIH_IMUL */ shader_glsl_imul
,
7919 /* WINED3DSIH_INE */ NULL
,
7920 /* WINED3DSIH_INEG */ NULL
,
7921 /* WINED3DSIH_ISHL */ shader_glsl_binop
,
7922 /* WINED3DSIH_ITOF */ shader_glsl_to_float
,
7923 /* WINED3DSIH_LABEL */ shader_glsl_label
,
7924 /* WINED3DSIH_LD */ shader_glsl_ld
,
7925 /* WINED3DSIH_LIT */ shader_glsl_lit
,
7926 /* WINED3DSIH_LOG */ shader_glsl_scalar_op
,
7927 /* WINED3DSIH_LOGP */ shader_glsl_scalar_op
,
7928 /* WINED3DSIH_LOOP */ shader_glsl_loop
,
7929 /* WINED3DSIH_LRP */ shader_glsl_lrp
,
7930 /* WINED3DSIH_LT */ shader_glsl_relop
,
7931 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn
,
7932 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn
,
7933 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn
,
7934 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn
,
7935 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn
,
7936 /* WINED3DSIH_MAD */ shader_glsl_mad
,
7937 /* WINED3DSIH_MAX */ shader_glsl_map2gl
,
7938 /* WINED3DSIH_MIN */ shader_glsl_map2gl
,
7939 /* WINED3DSIH_MOV */ shader_glsl_mov
,
7940 /* WINED3DSIH_MOVA */ shader_glsl_mov
,
7941 /* WINED3DSIH_MOVC */ shader_glsl_conditional_move
,
7942 /* WINED3DSIH_MUL */ shader_glsl_binop
,
7943 /* WINED3DSIH_NE */ shader_glsl_relop
,
7944 /* WINED3DSIH_NOP */ shader_glsl_nop
,
7945 /* WINED3DSIH_NRM */ shader_glsl_nrm
,
7946 /* WINED3DSIH_OR */ shader_glsl_binop
,
7947 /* WINED3DSIH_PHASE */ shader_glsl_nop
,
7948 /* WINED3DSIH_POW */ shader_glsl_pow
,
7949 /* WINED3DSIH_RCP */ shader_glsl_scalar_op
,
7950 /* WINED3DSIH_REP */ shader_glsl_rep
,
7951 /* WINED3DSIH_RESINFO */ NULL
,
7952 /* WINED3DSIH_RET */ shader_glsl_ret
,
7953 /* WINED3DSIH_ROUND_NI */ shader_glsl_map2gl
,
7954 /* WINED3DSIH_RSQ */ shader_glsl_scalar_op
,
7955 /* WINED3DSIH_SAMPLE */ shader_glsl_sample
,
7956 /* WINED3DSIH_SAMPLE_GRAD */ NULL
,
7957 /* WINED3DSIH_SAMPLE_LOD */ shader_glsl_sample_lod
,
7958 /* WINED3DSIH_SETP */ NULL
,
7959 /* WINED3DSIH_SGE */ shader_glsl_compare
,
7960 /* WINED3DSIH_SGN */ shader_glsl_sgn
,
7961 /* WINED3DSIH_SINCOS */ shader_glsl_sincos
,
7962 /* WINED3DSIH_SLT */ shader_glsl_compare
,
7963 /* WINED3DSIH_SQRT */ shader_glsl_map2gl
,
7964 /* WINED3DSIH_SUB */ shader_glsl_binop
,
7965 /* WINED3DSIH_TEX */ shader_glsl_tex
,
7966 /* WINED3DSIH_TEXBEM */ shader_glsl_texbem
,
7967 /* WINED3DSIH_TEXBEML */ shader_glsl_texbem
,
7968 /* WINED3DSIH_TEXCOORD */ shader_glsl_texcoord
,
7969 /* WINED3DSIH_TEXDEPTH */ shader_glsl_texdepth
,
7970 /* WINED3DSIH_TEXDP3 */ shader_glsl_texdp3
,
7971 /* WINED3DSIH_TEXDP3TEX */ shader_glsl_texdp3tex
,
7972 /* WINED3DSIH_TEXKILL */ shader_glsl_texkill
,
7973 /* WINED3DSIH_TEXLDD */ shader_glsl_texldd
,
7974 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl
,
7975 /* WINED3DSIH_TEXM3x2DEPTH */ shader_glsl_texm3x2depth
,
7976 /* WINED3DSIH_TEXM3x2PAD */ shader_glsl_texm3x2pad
,
7977 /* WINED3DSIH_TEXM3x2TEX */ shader_glsl_texm3x2tex
,
7978 /* WINED3DSIH_TEXM3x3 */ shader_glsl_texm3x3
,
7979 /* WINED3DSIH_TEXM3x3DIFF */ NULL
,
7980 /* WINED3DSIH_TEXM3x3PAD */ shader_glsl_texm3x3pad
,
7981 /* WINED3DSIH_TEXM3x3SPEC */ shader_glsl_texm3x3spec
,
7982 /* WINED3DSIH_TEXM3x3TEX */ shader_glsl_texm3x3tex
,
7983 /* WINED3DSIH_TEXM3x3VSPEC */ shader_glsl_texm3x3vspec
,
7984 /* WINED3DSIH_TEXREG2AR */ shader_glsl_texreg2ar
,
7985 /* WINED3DSIH_TEXREG2GB */ shader_glsl_texreg2gb
,
7986 /* WINED3DSIH_TEXREG2RGB */ shader_glsl_texreg2rgb
,
7987 /* WINED3DSIH_UDIV */ shader_glsl_udiv
,
7988 /* WINED3DSIH_UGE */ shader_glsl_relop
,
7989 /* WINED3DSIH_USHR */ shader_glsl_binop
,
7990 /* WINED3DSIH_UTOF */ shader_glsl_to_float
,
7991 /* WINED3DSIH_XOR */ shader_glsl_binop
,
7994 static void shader_glsl_handle_instruction(const struct wined3d_shader_instruction
*ins
) {
7995 SHADER_HANDLER hw_fct
;
7997 /* Select handler */
7998 hw_fct
= shader_glsl_instruction_handler_table
[ins
->handler_idx
];
8000 /* Unhandled opcode */
8003 FIXME("Backend can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins
->handler_idx
));
8008 shader_glsl_add_instruction_modifiers(ins
);
8011 static BOOL
shader_glsl_has_ffp_proj_control(void *shader_priv
)
8013 struct shader_glsl_priv
*priv
= shader_priv
;
8015 return priv
->ffp_proj_control
;
8018 const struct wined3d_shader_backend_ops glsl_shader_backend
=
8020 shader_glsl_handle_instruction
,
8022 shader_glsl_disable
,
8023 shader_glsl_select_depth_blt
,
8024 shader_glsl_deselect_depth_blt
,
8025 shader_glsl_update_float_vertex_constants
,
8026 shader_glsl_update_float_pixel_constants
,
8027 shader_glsl_load_constants
,
8028 shader_glsl_destroy
,
8031 shader_glsl_allocate_context_data
,
8032 shader_glsl_free_context_data
,
8033 shader_glsl_init_context_state
,
8034 shader_glsl_get_caps
,
8035 shader_glsl_color_fixup_supported
,
8036 shader_glsl_has_ffp_proj_control
,
8039 static void glsl_vertex_pipe_vp_enable(const struct wined3d_gl_info
*gl_info
, BOOL enable
) {}
8041 static void glsl_vertex_pipe_vp_get_caps(const struct wined3d_gl_info
*gl_info
, struct wined3d_vertex_caps
*caps
)
8043 caps
->xyzrhw
= TRUE
;
8044 caps
->emulated_flatshading
= !gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
];
8045 caps
->ffp_generic_attributes
= TRUE
;
8046 caps
->max_active_lights
= MAX_ACTIVE_LIGHTS
;
8047 caps
->max_vertex_blend_matrices
= MAX_VERTEX_BLENDS
;
8048 caps
->max_vertex_blend_matrix_index
= 0;
8049 caps
->vertex_processing_caps
= WINED3DVTXPCAPS_TEXGEN
8050 | WINED3DVTXPCAPS_MATERIALSOURCE7
8051 | WINED3DVTXPCAPS_VERTEXFOG
8052 | WINED3DVTXPCAPS_DIRECTIONALLIGHTS
8053 | WINED3DVTXPCAPS_POSITIONALLIGHTS
8054 | WINED3DVTXPCAPS_LOCALVIEWER
8055 | WINED3DVTXPCAPS_TEXGEN_SPHEREMAP
;
8056 caps
->fvf_caps
= WINED3DFVFCAPS_PSIZE
| 8; /* 8 texture coordinates. */
8057 caps
->max_user_clip_planes
= gl_info
->limits
.clipplanes
;
8058 caps
->raster_caps
= WINED3DPRASTERCAPS_FOGRANGE
;
8061 static DWORD
glsl_vertex_pipe_vp_get_emul_mask(const struct wined3d_gl_info
*gl_info
)
8063 if (gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
])
8064 return GL_EXT_EMUL_ARB_MULTITEXTURE
;
8068 static void *glsl_vertex_pipe_vp_alloc(const struct wined3d_shader_backend_ops
*shader_backend
, void *shader_priv
)
8070 struct shader_glsl_priv
*priv
;
8072 if (shader_backend
== &glsl_shader_backend
)
8076 if (wine_rb_init(&priv
->ffp_vertex_shaders
, &wined3d_ffp_vertex_program_rb_functions
) == -1)
8078 ERR("Failed to initialize rbtree.\n");
8085 FIXME("GLSL vertex pipe without GLSL shader backend not implemented.\n");
8090 static void shader_glsl_free_ffp_vertex_shader(struct wine_rb_entry
*entry
, void *context
)
8092 struct glsl_ffp_vertex_shader
*shader
= WINE_RB_ENTRY_VALUE(entry
,
8093 struct glsl_ffp_vertex_shader
, desc
.entry
);
8094 struct glsl_shader_prog_link
*program
, *program2
;
8095 struct glsl_ffp_destroy_ctx
*ctx
= context
;
8097 LIST_FOR_EACH_ENTRY_SAFE(program
, program2
, &shader
->linked_programs
,
8098 struct glsl_shader_prog_link
, vs
.shader_entry
)
8100 delete_glsl_program_entry(ctx
->priv
, ctx
->gl_info
, program
);
8102 ctx
->gl_info
->gl_ops
.ext
.p_glDeleteShader(shader
->id
);
8103 HeapFree(GetProcessHeap(), 0, shader
);
8106 /* Context activation is done by the caller. */
8107 static void glsl_vertex_pipe_vp_free(struct wined3d_device
*device
)
8109 struct shader_glsl_priv
*priv
= device
->vertex_priv
;
8110 struct glsl_ffp_destroy_ctx ctx
;
8113 ctx
.gl_info
= &device
->adapter
->gl_info
;
8114 wine_rb_destroy(&priv
->ffp_vertex_shaders
, shader_glsl_free_ffp_vertex_shader
, &ctx
);
8117 static void glsl_vertex_pipe_nop(struct wined3d_context
*context
,
8118 const struct wined3d_state
*state
, DWORD state_id
) {}
8120 static void glsl_vertex_pipe_shader(struct wined3d_context
*context
,
8121 const struct wined3d_state
*state
, DWORD state_id
)
8123 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_VERTEX
;
8126 static void glsl_vertex_pipe_vdecl(struct wined3d_context
*context
,
8127 const struct wined3d_state
*state
, DWORD state_id
)
8129 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
8130 BOOL normal
= !!(context
->stream_info
.use_map
& (1u << WINED3D_FFP_NORMAL
));
8131 BOOL transformed
= context
->stream_info
.position_transformed
;
8132 BOOL wasrhw
= context
->last_was_rhw
;
8135 context
->last_was_rhw
= transformed
;
8137 /* If the vertex declaration contains a transformed position attribute,
8138 * the draw uses the fixed function vertex pipeline regardless of any
8139 * vertex shader set by the application. */
8140 if (transformed
!= wasrhw
)
8141 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_VERTEX
;
8145 if (context
->last_was_vshader
)
8147 for (i
= 0; i
< gl_info
->limits
.clipplanes
; ++i
)
8148 clipplane(context
, state
, STATE_CLIPPLANE(i
));
8151 context
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_TEXMATRIX
;
8153 /* Because of settings->texcoords, we have to regenerate the vertex
8154 * shader on a vdecl change if there aren't enough varyings to just
8155 * always output all the texture coordinates. */
8156 if (gl_info
->limits
.glsl_varyings
< wined3d_max_compat_varyings(gl_info
)
8157 || normal
!= context
->last_was_normal
)
8158 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_VERTEX
;
8161 && state
->shader
[WINED3D_SHADER_TYPE_PIXEL
]->reg_maps
.shader_version
.major
== 1
8162 && state
->shader
[WINED3D_SHADER_TYPE_PIXEL
]->reg_maps
.shader_version
.minor
<= 3)
8163 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_PIXEL
;
8167 if (!context
->last_was_vshader
)
8169 /* Vertex shader clipping ignores the view matrix. Update all clipplanes. */
8170 for (i
= 0; i
< gl_info
->limits
.clipplanes
; ++i
)
8171 clipplane(context
, state
, STATE_CLIPPLANE(i
));
8175 context
->last_was_vshader
= use_vs(state
);
8176 context
->last_was_normal
= normal
;
8179 static void glsl_vertex_pipe_vs(struct wined3d_context
*context
,
8180 const struct wined3d_state
*state
, DWORD state_id
)
8182 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_VERTEX
;
8183 /* Different vertex shaders potentially require a different vertex attributes setup. */
8184 if (!isStateDirty(context
, STATE_VDECL
))
8185 context_apply_state(context
, state
, STATE_VDECL
);
8188 static void glsl_vertex_pipe_world(struct wined3d_context
*context
,
8189 const struct wined3d_state
*state
, DWORD state_id
)
8191 context
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_MODELVIEW
;
8194 static void glsl_vertex_pipe_vertexblend(struct wined3d_context
*context
,
8195 const struct wined3d_state
*state
, DWORD state_id
)
8197 context
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_VERTEXBLEND
;
8200 static void glsl_vertex_pipe_view(struct wined3d_context
*context
, const struct wined3d_state
*state
, DWORD state_id
)
8202 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
8205 context
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_MODELVIEW
8206 | WINED3D_SHADER_CONST_FFP_LIGHTS
8207 | WINED3D_SHADER_CONST_FFP_VERTEXBLEND
;
8209 for (k
= 0; k
< gl_info
->limits
.clipplanes
; ++k
)
8211 if (!isStateDirty(context
, STATE_CLIPPLANE(k
)))
8212 clipplane(context
, state
, STATE_CLIPPLANE(k
));
8216 static void glsl_vertex_pipe_projection(struct wined3d_context
*context
,
8217 const struct wined3d_state
*state
, DWORD state_id
)
8219 /* Table fog behavior depends on the projection matrix. */
8220 if (state
->render_states
[WINED3D_RS_FOGENABLE
]
8221 && state
->render_states
[WINED3D_RS_FOGTABLEMODE
] != WINED3D_FOG_NONE
)
8222 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_VERTEX
;
8223 context
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_PROJ
;
8226 static void glsl_vertex_pipe_viewport(struct wined3d_context
*context
,
8227 const struct wined3d_state
*state
, DWORD state_id
)
8229 if (!isStateDirty(context
, STATE_TRANSFORM(WINED3D_TS_PROJECTION
)))
8230 glsl_vertex_pipe_projection(context
, state
, STATE_TRANSFORM(WINED3D_TS_PROJECTION
));
8231 if (!isStateDirty(context
, STATE_RENDER(WINED3D_RS_POINTSCALEENABLE
))
8232 && state
->render_states
[WINED3D_RS_POINTSCALEENABLE
])
8233 context
->constant_update_mask
|= WINED3D_SHADER_CONST_VS_POINTSIZE
;
8234 context
->constant_update_mask
|= WINED3D_SHADER_CONST_VS_POS_FIXUP
;
8237 static void glsl_vertex_pipe_texmatrix(struct wined3d_context
*context
,
8238 const struct wined3d_state
*state
, DWORD state_id
)
8240 context
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_TEXMATRIX
;
8243 static void glsl_vertex_pipe_texmatrix_np2(struct wined3d_context
*context
,
8244 const struct wined3d_state
*state
, DWORD state_id
)
8246 DWORD sampler
= state_id
- STATE_SAMPLER(0);
8247 const struct wined3d_texture
*texture
= state
->textures
[sampler
];
8253 if (sampler
>= MAX_TEXTURES
)
8256 if ((np2
= !(texture
->flags
& WINED3D_TEXTURE_POW2_MAT_IDENT
))
8257 || context
->lastWasPow2Texture
& (1u << sampler
))
8260 context
->lastWasPow2Texture
|= 1u << sampler
;
8262 context
->lastWasPow2Texture
&= ~(1u << sampler
);
8264 context
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_TEXMATRIX
;
8268 static void glsl_vertex_pipe_material(struct wined3d_context
*context
,
8269 const struct wined3d_state
*state
, DWORD state_id
)
8271 context
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_MATERIAL
;
8274 static void glsl_vertex_pipe_light(struct wined3d_context
*context
,
8275 const struct wined3d_state
*state
, DWORD state_id
)
8277 context
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_LIGHTS
;
8280 static void glsl_vertex_pipe_pointsize(struct wined3d_context
*context
,
8281 const struct wined3d_state
*state
, DWORD state_id
)
8283 context
->constant_update_mask
|= WINED3D_SHADER_CONST_VS_POINTSIZE
;
8286 static void glsl_vertex_pipe_pointscale(struct wined3d_context
*context
,
8287 const struct wined3d_state
*state
, DWORD state_id
)
8290 context
->constant_update_mask
|= WINED3D_SHADER_CONST_VS_POINTSIZE
;
8293 static void glsl_vertex_pipe_shademode(struct wined3d_context
*context
,
8294 const struct wined3d_state
*state
, DWORD state_id
)
8296 context
->shader_update_mask
|= 1 << WINED3D_SHADER_TYPE_VERTEX
;
8299 static const struct StateEntryTemplate glsl_vertex_pipe_vp_states
[] =
8301 {STATE_VDECL
, {STATE_VDECL
, glsl_vertex_pipe_vdecl
}, WINED3D_GL_EXT_NONE
},
8302 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), glsl_vertex_pipe_vs
}, WINED3D_GL_EXT_NONE
},
8303 {STATE_MATERIAL
, {STATE_RENDER(WINED3D_RS_SPECULARENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
8304 {STATE_RENDER(WINED3D_RS_SPECULARENABLE
), {STATE_RENDER(WINED3D_RS_SPECULARENABLE
), glsl_vertex_pipe_material
}, WINED3D_GL_EXT_NONE
},
8306 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), clipplane
}, WINED3D_GL_EXT_NONE
},
8307 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), clipplane
}, WINED3D_GL_EXT_NONE
},
8308 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), clipplane
}, WINED3D_GL_EXT_NONE
},
8309 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), clipplane
}, WINED3D_GL_EXT_NONE
},
8310 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), clipplane
}, WINED3D_GL_EXT_NONE
},
8311 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), clipplane
}, WINED3D_GL_EXT_NONE
},
8312 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), clipplane
}, WINED3D_GL_EXT_NONE
},
8313 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), clipplane
}, WINED3D_GL_EXT_NONE
},
8314 {STATE_CLIPPLANE(8), {STATE_CLIPPLANE(8), clipplane
}, WINED3D_GL_EXT_NONE
},
8315 {STATE_CLIPPLANE(9), {STATE_CLIPPLANE(9), clipplane
}, WINED3D_GL_EXT_NONE
},
8316 {STATE_CLIPPLANE(10), {STATE_CLIPPLANE(10), clipplane
}, WINED3D_GL_EXT_NONE
},
8317 {STATE_CLIPPLANE(11), {STATE_CLIPPLANE(11), clipplane
}, WINED3D_GL_EXT_NONE
},
8318 {STATE_CLIPPLANE(12), {STATE_CLIPPLANE(12), clipplane
}, WINED3D_GL_EXT_NONE
},
8319 {STATE_CLIPPLANE(13), {STATE_CLIPPLANE(13), clipplane
}, WINED3D_GL_EXT_NONE
},
8320 {STATE_CLIPPLANE(14), {STATE_CLIPPLANE(14), clipplane
}, WINED3D_GL_EXT_NONE
},
8321 {STATE_CLIPPLANE(15), {STATE_CLIPPLANE(15), clipplane
}, WINED3D_GL_EXT_NONE
},
8322 {STATE_CLIPPLANE(16), {STATE_CLIPPLANE(16), clipplane
}, WINED3D_GL_EXT_NONE
},
8323 {STATE_CLIPPLANE(17), {STATE_CLIPPLANE(17), clipplane
}, WINED3D_GL_EXT_NONE
},
8324 {STATE_CLIPPLANE(18), {STATE_CLIPPLANE(18), clipplane
}, WINED3D_GL_EXT_NONE
},
8325 {STATE_CLIPPLANE(19), {STATE_CLIPPLANE(19), clipplane
}, WINED3D_GL_EXT_NONE
},
8326 {STATE_CLIPPLANE(20), {STATE_CLIPPLANE(20), clipplane
}, WINED3D_GL_EXT_NONE
},
8327 {STATE_CLIPPLANE(21), {STATE_CLIPPLANE(21), clipplane
}, WINED3D_GL_EXT_NONE
},
8328 {STATE_CLIPPLANE(22), {STATE_CLIPPLANE(22), clipplane
}, WINED3D_GL_EXT_NONE
},
8329 {STATE_CLIPPLANE(23), {STATE_CLIPPLANE(23), clipplane
}, WINED3D_GL_EXT_NONE
},
8330 {STATE_CLIPPLANE(24), {STATE_CLIPPLANE(24), clipplane
}, WINED3D_GL_EXT_NONE
},
8331 {STATE_CLIPPLANE(25), {STATE_CLIPPLANE(25), clipplane
}, WINED3D_GL_EXT_NONE
},
8332 {STATE_CLIPPLANE(26), {STATE_CLIPPLANE(26), clipplane
}, WINED3D_GL_EXT_NONE
},
8333 {STATE_CLIPPLANE(27), {STATE_CLIPPLANE(27), clipplane
}, WINED3D_GL_EXT_NONE
},
8334 {STATE_CLIPPLANE(28), {STATE_CLIPPLANE(28), clipplane
}, WINED3D_GL_EXT_NONE
},
8335 {STATE_CLIPPLANE(29), {STATE_CLIPPLANE(29), clipplane
}, WINED3D_GL_EXT_NONE
},
8336 {STATE_CLIPPLANE(30), {STATE_CLIPPLANE(30), clipplane
}, WINED3D_GL_EXT_NONE
},
8337 {STATE_CLIPPLANE(31), {STATE_CLIPPLANE(31), clipplane
}, WINED3D_GL_EXT_NONE
},
8339 {STATE_LIGHT_TYPE
, {STATE_RENDER(WINED3D_RS_FOGENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
8340 {STATE_ACTIVELIGHT(0), {STATE_ACTIVELIGHT(0), glsl_vertex_pipe_light
}, WINED3D_GL_EXT_NONE
},
8341 {STATE_ACTIVELIGHT(1), {STATE_ACTIVELIGHT(1), glsl_vertex_pipe_light
}, WINED3D_GL_EXT_NONE
},
8342 {STATE_ACTIVELIGHT(2), {STATE_ACTIVELIGHT(2), glsl_vertex_pipe_light
}, WINED3D_GL_EXT_NONE
},
8343 {STATE_ACTIVELIGHT(3), {STATE_ACTIVELIGHT(3), glsl_vertex_pipe_light
}, WINED3D_GL_EXT_NONE
},
8344 {STATE_ACTIVELIGHT(4), {STATE_ACTIVELIGHT(4), glsl_vertex_pipe_light
}, WINED3D_GL_EXT_NONE
},
8345 {STATE_ACTIVELIGHT(5), {STATE_ACTIVELIGHT(5), glsl_vertex_pipe_light
}, WINED3D_GL_EXT_NONE
},
8346 {STATE_ACTIVELIGHT(6), {STATE_ACTIVELIGHT(6), glsl_vertex_pipe_light
}, WINED3D_GL_EXT_NONE
},
8347 {STATE_ACTIVELIGHT(7), {STATE_ACTIVELIGHT(7), glsl_vertex_pipe_light
}, WINED3D_GL_EXT_NONE
},
8349 {STATE_VIEWPORT
, {STATE_VIEWPORT
, glsl_vertex_pipe_viewport
}, WINED3D_GL_EXT_NONE
},
8350 /* Transform states */
8351 {STATE_TRANSFORM(WINED3D_TS_VIEW
), {STATE_TRANSFORM(WINED3D_TS_VIEW
), glsl_vertex_pipe_view
}, WINED3D_GL_EXT_NONE
},
8352 {STATE_TRANSFORM(WINED3D_TS_PROJECTION
), {STATE_TRANSFORM(WINED3D_TS_PROJECTION
), glsl_vertex_pipe_projection
}, WINED3D_GL_EXT_NONE
},
8353 {STATE_TRANSFORM(WINED3D_TS_TEXTURE0
), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), NULL
}, WINED3D_GL_EXT_NONE
},
8354 {STATE_TRANSFORM(WINED3D_TS_TEXTURE1
), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), NULL
}, WINED3D_GL_EXT_NONE
},
8355 {STATE_TRANSFORM(WINED3D_TS_TEXTURE2
), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), NULL
}, WINED3D_GL_EXT_NONE
},
8356 {STATE_TRANSFORM(WINED3D_TS_TEXTURE3
), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), NULL
}, WINED3D_GL_EXT_NONE
},
8357 {STATE_TRANSFORM(WINED3D_TS_TEXTURE4
), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), NULL
}, WINED3D_GL_EXT_NONE
},
8358 {STATE_TRANSFORM(WINED3D_TS_TEXTURE5
), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), NULL
}, WINED3D_GL_EXT_NONE
},
8359 {STATE_TRANSFORM(WINED3D_TS_TEXTURE6
), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), NULL
}, WINED3D_GL_EXT_NONE
},
8360 {STATE_TRANSFORM(WINED3D_TS_TEXTURE7
), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), NULL
}, WINED3D_GL_EXT_NONE
},
8361 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), glsl_vertex_pipe_world
}, WINED3D_GL_EXT_NONE
},
8362 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), glsl_vertex_pipe_vertexblend
}, WINED3D_GL_EXT_NONE
},
8363 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), glsl_vertex_pipe_vertexblend
}, WINED3D_GL_EXT_NONE
},
8364 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), glsl_vertex_pipe_vertexblend
}, WINED3D_GL_EXT_NONE
},
8365 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), glsl_vertex_pipe_texmatrix
}, WINED3D_GL_EXT_NONE
},
8366 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), glsl_vertex_pipe_texmatrix
}, WINED3D_GL_EXT_NONE
},
8367 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), glsl_vertex_pipe_texmatrix
}, WINED3D_GL_EXT_NONE
},
8368 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), glsl_vertex_pipe_texmatrix
}, WINED3D_GL_EXT_NONE
},
8369 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), glsl_vertex_pipe_texmatrix
}, WINED3D_GL_EXT_NONE
},
8370 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), glsl_vertex_pipe_texmatrix
}, WINED3D_GL_EXT_NONE
},
8371 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), glsl_vertex_pipe_texmatrix
}, WINED3D_GL_EXT_NONE
},
8372 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), glsl_vertex_pipe_texmatrix
}, WINED3D_GL_EXT_NONE
},
8373 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXCOORD_INDEX
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
8374 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXCOORD_INDEX
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
8375 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXCOORD_INDEX
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
8376 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXCOORD_INDEX
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
8377 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXCOORD_INDEX
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
8378 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXCOORD_INDEX
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
8379 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXCOORD_INDEX
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
8380 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXCOORD_INDEX
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
8382 {STATE_RENDER(WINED3D_RS_FOGENABLE
), {STATE_RENDER(WINED3D_RS_FOGENABLE
), glsl_vertex_pipe_shader
}, WINED3D_GL_EXT_NONE
},
8383 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE
), {STATE_RENDER(WINED3D_RS_FOGENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
8384 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE
), {STATE_RENDER(WINED3D_RS_FOGENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
8385 {STATE_RENDER(WINED3D_RS_RANGEFOGENABLE
), {STATE_RENDER(WINED3D_RS_FOGENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
8386 {STATE_RENDER(WINED3D_RS_CLIPPING
), {STATE_RENDER(WINED3D_RS_CLIPPING
), state_clipping
}, WINED3D_GL_EXT_NONE
},
8387 {STATE_RENDER(WINED3D_RS_CLIPPLANEENABLE
), {STATE_RENDER(WINED3D_RS_CLIPPING
), NULL
}, WINED3D_GL_EXT_NONE
},
8388 {STATE_RENDER(WINED3D_RS_LIGHTING
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
8389 {STATE_RENDER(WINED3D_RS_AMBIENT
), {STATE_RENDER(WINED3D_RS_AMBIENT
), glsl_vertex_pipe_light
}, WINED3D_GL_EXT_NONE
},
8390 {STATE_RENDER(WINED3D_RS_COLORVERTEX
), {STATE_RENDER(WINED3D_RS_COLORVERTEX
), glsl_vertex_pipe_shader
}, WINED3D_GL_EXT_NONE
},
8391 {STATE_RENDER(WINED3D_RS_LOCALVIEWER
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
8392 {STATE_RENDER(WINED3D_RS_NORMALIZENORMALS
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
8393 {STATE_RENDER(WINED3D_RS_DIFFUSEMATERIALSOURCE
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
8394 {STATE_RENDER(WINED3D_RS_SPECULARMATERIALSOURCE
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
8395 {STATE_RENDER(WINED3D_RS_AMBIENTMATERIALSOURCE
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
8396 {STATE_RENDER(WINED3D_RS_EMISSIVEMATERIALSOURCE
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
8397 {STATE_RENDER(WINED3D_RS_VERTEXBLEND
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
8398 {STATE_RENDER(WINED3D_RS_POINTSIZE
), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN
), NULL
}, WINED3D_GL_EXT_NONE
},
8399 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN
), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN
), glsl_vertex_pipe_pointsize
}, WINED3D_GL_EXT_NONE
},
8400 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE
), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE
), state_pointsprite
}, ARB_POINT_SPRITE
},
8401 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE
), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE
), state_pointsprite_w
}, WINED3D_GL_EXT_NONE
},
8402 {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE
), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE
), glsl_vertex_pipe_pointscale
}, WINED3D_GL_EXT_NONE
},
8403 {STATE_RENDER(WINED3D_RS_POINTSCALE_A
), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
8404 {STATE_RENDER(WINED3D_RS_POINTSCALE_B
), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
8405 {STATE_RENDER(WINED3D_RS_POINTSCALE_C
), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
8406 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX
), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN
), NULL
}, WINED3D_GL_EXT_NONE
},
8407 {STATE_RENDER(WINED3D_RS_TWEENFACTOR
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
8408 {STATE_RENDER(WINED3D_RS_INDEXEDVERTEXBLENDENABLE
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
8409 /* NP2 texture matrix fixups. They are not needed if
8410 * GL_ARB_texture_non_power_of_two is supported. Otherwise, register
8411 * glsl_vertex_pipe_texmatrix(), which takes care of updating the texture
8413 {STATE_SAMPLER(0), {0, NULL
}, ARB_TEXTURE_NON_POWER_OF_TWO
},
8414 {STATE_SAMPLER(0), {0, NULL
}, WINED3D_GL_NORMALIZED_TEXRECT
},
8415 {STATE_SAMPLER(0), {STATE_SAMPLER(0), glsl_vertex_pipe_texmatrix_np2
}, WINED3D_GL_EXT_NONE
},
8416 {STATE_SAMPLER(1), {0, NULL
}, ARB_TEXTURE_NON_POWER_OF_TWO
},
8417 {STATE_SAMPLER(1), {0, NULL
}, WINED3D_GL_NORMALIZED_TEXRECT
},
8418 {STATE_SAMPLER(1), {STATE_SAMPLER(1), glsl_vertex_pipe_texmatrix_np2
}, WINED3D_GL_EXT_NONE
},
8419 {STATE_SAMPLER(2), {0, NULL
}, ARB_TEXTURE_NON_POWER_OF_TWO
},
8420 {STATE_SAMPLER(2), {0, NULL
}, WINED3D_GL_NORMALIZED_TEXRECT
},
8421 {STATE_SAMPLER(2), {STATE_SAMPLER(2), glsl_vertex_pipe_texmatrix_np2
}, WINED3D_GL_EXT_NONE
},
8422 {STATE_SAMPLER(3), {0, NULL
}, ARB_TEXTURE_NON_POWER_OF_TWO
},
8423 {STATE_SAMPLER(3), {0, NULL
}, WINED3D_GL_NORMALIZED_TEXRECT
},
8424 {STATE_SAMPLER(3), {STATE_SAMPLER(3), glsl_vertex_pipe_texmatrix_np2
}, WINED3D_GL_EXT_NONE
},
8425 {STATE_SAMPLER(4), {0, NULL
}, ARB_TEXTURE_NON_POWER_OF_TWO
},
8426 {STATE_SAMPLER(4), {0, NULL
}, WINED3D_GL_NORMALIZED_TEXRECT
},
8427 {STATE_SAMPLER(4), {STATE_SAMPLER(4), glsl_vertex_pipe_texmatrix_np2
}, WINED3D_GL_EXT_NONE
},
8428 {STATE_SAMPLER(5), {0, NULL
}, ARB_TEXTURE_NON_POWER_OF_TWO
},
8429 {STATE_SAMPLER(5), {0, NULL
}, WINED3D_GL_NORMALIZED_TEXRECT
},
8430 {STATE_SAMPLER(5), {STATE_SAMPLER(5), glsl_vertex_pipe_texmatrix_np2
}, WINED3D_GL_EXT_NONE
},
8431 {STATE_SAMPLER(6), {0, NULL
}, ARB_TEXTURE_NON_POWER_OF_TWO
},
8432 {STATE_SAMPLER(6), {0, NULL
}, WINED3D_GL_NORMALIZED_TEXRECT
},
8433 {STATE_SAMPLER(6), {STATE_SAMPLER(6), glsl_vertex_pipe_texmatrix_np2
}, WINED3D_GL_EXT_NONE
},
8434 {STATE_SAMPLER(7), {0, NULL
}, ARB_TEXTURE_NON_POWER_OF_TWO
},
8435 {STATE_SAMPLER(7), {0, NULL
}, WINED3D_GL_NORMALIZED_TEXRECT
},
8436 {STATE_SAMPLER(7), {STATE_SAMPLER(7), glsl_vertex_pipe_texmatrix_np2
}, WINED3D_GL_EXT_NONE
},
8437 {STATE_POINT_ENABLE
, {STATE_POINT_ENABLE
, glsl_vertex_pipe_shader
}, WINED3D_GL_EXT_NONE
},
8438 {STATE_RENDER(WINED3D_RS_SHADEMODE
), {STATE_RENDER(WINED3D_RS_SHADEMODE
), glsl_vertex_pipe_nop
}, WINED3D_GL_LEGACY_CONTEXT
},
8439 {STATE_RENDER(WINED3D_RS_SHADEMODE
), {STATE_RENDER(WINED3D_RS_SHADEMODE
), glsl_vertex_pipe_shademode
}, WINED3D_GL_EXT_NONE
},
8440 {0 /* Terminate */, {0, NULL
}, WINED3D_GL_EXT_NONE
},
8444 * - Implement vertex tweening. */
8445 const struct wined3d_vertex_pipe_ops glsl_vertex_pipe
=
8447 glsl_vertex_pipe_vp_enable
,
8448 glsl_vertex_pipe_vp_get_caps
,
8449 glsl_vertex_pipe_vp_get_emul_mask
,
8450 glsl_vertex_pipe_vp_alloc
,
8451 glsl_vertex_pipe_vp_free
,
8452 glsl_vertex_pipe_vp_states
,
8455 static void glsl_fragment_pipe_enable(const struct wined3d_gl_info
*gl_info
, BOOL enable
)
8457 /* Nothing to do. */
8460 static void glsl_fragment_pipe_get_caps(const struct wined3d_gl_info
*gl_info
, struct fragment_caps
*caps
)
8462 caps
->wined3d_caps
= WINED3D_FRAGMENT_CAP_PROJ_CONTROL
8463 | WINED3D_FRAGMENT_CAP_SRGB_WRITE
8464 | WINED3D_FRAGMENT_CAP_COLOR_KEY
;
8465 caps
->PrimitiveMiscCaps
= WINED3DPMISCCAPS_TSSARGTEMP
8466 | WINED3DPMISCCAPS_PERSTAGECONSTANT
;
8467 caps
->TextureOpCaps
= WINED3DTEXOPCAPS_DISABLE
8468 | WINED3DTEXOPCAPS_SELECTARG1
8469 | WINED3DTEXOPCAPS_SELECTARG2
8470 | WINED3DTEXOPCAPS_MODULATE4X
8471 | WINED3DTEXOPCAPS_MODULATE2X
8472 | WINED3DTEXOPCAPS_MODULATE
8473 | WINED3DTEXOPCAPS_ADDSIGNED2X
8474 | WINED3DTEXOPCAPS_ADDSIGNED
8475 | WINED3DTEXOPCAPS_ADD
8476 | WINED3DTEXOPCAPS_SUBTRACT
8477 | WINED3DTEXOPCAPS_ADDSMOOTH
8478 | WINED3DTEXOPCAPS_BLENDCURRENTALPHA
8479 | WINED3DTEXOPCAPS_BLENDFACTORALPHA
8480 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHA
8481 | WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA
8482 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM
8483 | WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR
8484 | WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA
8485 | WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA
8486 | WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR
8487 | WINED3DTEXOPCAPS_DOTPRODUCT3
8488 | WINED3DTEXOPCAPS_MULTIPLYADD
8489 | WINED3DTEXOPCAPS_LERP
8490 | WINED3DTEXOPCAPS_BUMPENVMAP
8491 | WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE
;
8492 caps
->MaxTextureBlendStages
= 8;
8493 caps
->MaxSimultaneousTextures
= min(gl_info
->limits
.fragment_samplers
, 8);
8496 static DWORD
glsl_fragment_pipe_get_emul_mask(const struct wined3d_gl_info
*gl_info
)
8498 if (gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
])
8499 return GL_EXT_EMUL_ARB_MULTITEXTURE
;
8503 static void *glsl_fragment_pipe_alloc(const struct wined3d_shader_backend_ops
*shader_backend
, void *shader_priv
)
8505 struct shader_glsl_priv
*priv
;
8507 if (shader_backend
== &glsl_shader_backend
)
8511 if (wine_rb_init(&priv
->ffp_fragment_shaders
, &wined3d_ffp_frag_program_rb_functions
) == -1)
8513 ERR("Failed to initialize rbtree.\n");
8520 FIXME("GLSL fragment pipe without GLSL shader backend not implemented.\n");
8525 static void shader_glsl_free_ffp_fragment_shader(struct wine_rb_entry
*entry
, void *context
)
8527 struct glsl_ffp_fragment_shader
*shader
= WINE_RB_ENTRY_VALUE(entry
,
8528 struct glsl_ffp_fragment_shader
, entry
.entry
);
8529 struct glsl_shader_prog_link
*program
, *program2
;
8530 struct glsl_ffp_destroy_ctx
*ctx
= context
;
8532 LIST_FOR_EACH_ENTRY_SAFE(program
, program2
, &shader
->linked_programs
,
8533 struct glsl_shader_prog_link
, ps
.shader_entry
)
8535 delete_glsl_program_entry(ctx
->priv
, ctx
->gl_info
, program
);
8537 ctx
->gl_info
->gl_ops
.ext
.p_glDeleteShader(shader
->id
);
8538 HeapFree(GetProcessHeap(), 0, shader
);
8541 /* Context activation is done by the caller. */
8542 static void glsl_fragment_pipe_free(struct wined3d_device
*device
)
8544 struct shader_glsl_priv
*priv
= device
->fragment_priv
;
8545 struct glsl_ffp_destroy_ctx ctx
;
8548 ctx
.gl_info
= &device
->adapter
->gl_info
;
8549 wine_rb_destroy(&priv
->ffp_fragment_shaders
, shader_glsl_free_ffp_fragment_shader
, &ctx
);
8552 static void glsl_fragment_pipe_shader(struct wined3d_context
*context
,
8553 const struct wined3d_state
*state
, DWORD state_id
)
8555 context
->last_was_pshader
= use_ps(state
);
8557 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_PIXEL
;
8560 static void glsl_fragment_pipe_fogparams(struct wined3d_context
*context
,
8561 const struct wined3d_state
*state
, DWORD state_id
)
8563 context
->constant_update_mask
|= WINED3D_SHADER_CONST_PS_FOG
;
8566 static void glsl_fragment_pipe_fog(struct wined3d_context
*context
,
8567 const struct wined3d_state
*state
, DWORD state_id
)
8569 BOOL use_vshader
= use_vs(state
);
8570 enum fogsource new_source
;
8571 DWORD fogstart
= state
->render_states
[WINED3D_RS_FOGSTART
];
8572 DWORD fogend
= state
->render_states
[WINED3D_RS_FOGEND
];
8574 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_PIXEL
;
8576 if (!state
->render_states
[WINED3D_RS_FOGENABLE
])
8579 if (state
->render_states
[WINED3D_RS_FOGTABLEMODE
] == WINED3D_FOG_NONE
)
8582 new_source
= FOGSOURCE_VS
;
8583 else if (state
->render_states
[WINED3D_RS_FOGVERTEXMODE
] == WINED3D_FOG_NONE
|| context
->stream_info
.position_transformed
)
8584 new_source
= FOGSOURCE_COORD
;
8586 new_source
= FOGSOURCE_FFP
;
8590 new_source
= FOGSOURCE_FFP
;
8593 if (new_source
!= context
->fog_source
|| fogstart
== fogend
)
8595 context
->fog_source
= new_source
;
8596 context
->constant_update_mask
|= WINED3D_SHADER_CONST_PS_FOG
;
8600 static void glsl_fragment_pipe_vdecl(struct wined3d_context
*context
,
8601 const struct wined3d_state
*state
, DWORD state_id
)
8603 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
8604 if (context
->gl_info
->limits
.glsl_varyings
< wined3d_max_compat_varyings(context
->gl_info
))
8605 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_PIXEL
;
8607 if (!isStateDirty(context
, STATE_RENDER(WINED3D_RS_FOGENABLE
)))
8608 glsl_fragment_pipe_fog(context
, state
, state_id
);
8611 static void glsl_fragment_pipe_vs(struct wined3d_context
*context
,
8612 const struct wined3d_state
*state
, DWORD state_id
)
8614 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
8615 if (context
->gl_info
->limits
.glsl_varyings
< wined3d_max_compat_varyings(context
->gl_info
))
8616 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_PIXEL
;
8619 static void glsl_fragment_pipe_tex_transform(struct wined3d_context
*context
,
8620 const struct wined3d_state
*state
, DWORD state_id
)
8622 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_PIXEL
;
8625 static void glsl_fragment_pipe_invalidate_constants(struct wined3d_context
*context
,
8626 const struct wined3d_state
*state
, DWORD state_id
)
8628 context
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_PS
;
8631 static void glsl_fragment_pipe_alpha_test(struct wined3d_context
*context
,
8632 const struct wined3d_state
*state
, DWORD state_id
)
8634 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
8638 TRACE("context %p, state %p, state_id %#x.\n", context
, state
, state_id
);
8640 if (state
->render_states
[WINED3D_RS_ALPHATESTENABLE
])
8642 gl_info
->gl_ops
.gl
.p_glEnable(GL_ALPHA_TEST
);
8643 checkGLcall("glEnable GL_ALPHA_TEST");
8647 gl_info
->gl_ops
.gl
.p_glDisable(GL_ALPHA_TEST
);
8648 checkGLcall("glDisable GL_ALPHA_TEST");
8652 ref
= ((float)state
->render_states
[WINED3D_RS_ALPHAREF
]) / 255.0f
;
8653 glParm
= wined3d_gl_compare_func(state
->render_states
[WINED3D_RS_ALPHAFUNC
]);
8657 gl_info
->gl_ops
.gl
.p_glAlphaFunc(glParm
, ref
);
8658 checkGLcall("glAlphaFunc");
8662 static void glsl_fragment_pipe_color_key(struct wined3d_context
*context
,
8663 const struct wined3d_state
*state
, DWORD state_id
)
8665 context
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_COLOR_KEY
;
8668 static void glsl_fragment_pipe_shademode(struct wined3d_context
*context
,
8669 const struct wined3d_state
*state
, DWORD state_id
)
8671 context
->shader_update_mask
|= 1 << WINED3D_SHADER_TYPE_PIXEL
;
8674 static const struct StateEntryTemplate glsl_fragment_pipe_state_template
[] =
8676 {STATE_VDECL
, {STATE_VDECL
, glsl_fragment_pipe_vdecl
}, WINED3D_GL_EXT_NONE
},
8677 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), glsl_fragment_pipe_vs
}, WINED3D_GL_EXT_NONE
},
8678 {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR
), {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR
), glsl_fragment_pipe_invalidate_constants
}, WINED3D_GL_EXT_NONE
},
8679 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8680 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8681 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8682 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8683 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8684 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8685 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8686 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8687 {STATE_TEXTURESTAGE(0, WINED3D_TSS_RESULT_ARG
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8688 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8689 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8690 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8691 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8692 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8693 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8694 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8695 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8696 {STATE_TEXTURESTAGE(1, WINED3D_TSS_RESULT_ARG
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8697 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8698 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8699 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8700 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8701 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8702 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8703 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8704 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8705 {STATE_TEXTURESTAGE(2, WINED3D_TSS_RESULT_ARG
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8706 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8707 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8708 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8709 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8710 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8711 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8712 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8713 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8714 {STATE_TEXTURESTAGE(3, WINED3D_TSS_RESULT_ARG
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8715 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8716 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8717 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8718 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8719 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8720 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8721 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8722 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8723 {STATE_TEXTURESTAGE(4, WINED3D_TSS_RESULT_ARG
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8724 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8725 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8726 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8727 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8728 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8729 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8730 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8731 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8732 {STATE_TEXTURESTAGE(5, WINED3D_TSS_RESULT_ARG
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8733 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8734 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8735 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8736 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8737 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8738 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8739 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8740 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8741 {STATE_TEXTURESTAGE(6, WINED3D_TSS_RESULT_ARG
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8742 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8743 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8744 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8745 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8746 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8747 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8748 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8749 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8750 {STATE_TEXTURESTAGE(7, WINED3D_TSS_RESULT_ARG
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8751 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), glsl_fragment_pipe_shader
}, WINED3D_GL_EXT_NONE
},
8752 {STATE_RENDER(WINED3D_RS_ALPHAFUNC
), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
8753 {STATE_RENDER(WINED3D_RS_ALPHAREF
), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
8754 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE
), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE
), glsl_fragment_pipe_alpha_test
}, WINED3D_GL_EXT_NONE
},
8755 {STATE_RENDER(WINED3D_RS_COLORKEYENABLE
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8756 {STATE_COLOR_KEY
, { STATE_COLOR_KEY
, glsl_fragment_pipe_color_key
}, WINED3D_GL_EXT_NONE
},
8757 {STATE_RENDER(WINED3D_RS_FOGENABLE
), {STATE_RENDER(WINED3D_RS_FOGENABLE
), glsl_fragment_pipe_fog
}, WINED3D_GL_EXT_NONE
},
8758 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE
), {STATE_RENDER(WINED3D_RS_FOGENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
8759 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE
), {STATE_RENDER(WINED3D_RS_FOGENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
8760 {STATE_RENDER(WINED3D_RS_FOGSTART
), {STATE_RENDER(WINED3D_RS_FOGSTART
), glsl_fragment_pipe_fogparams
}, WINED3D_GL_EXT_NONE
},
8761 {STATE_RENDER(WINED3D_RS_FOGEND
), {STATE_RENDER(WINED3D_RS_FOGSTART
), NULL
}, WINED3D_GL_EXT_NONE
},
8762 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE
), {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE
), state_srgbwrite
}, ARB_FRAMEBUFFER_SRGB
},
8763 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8764 {STATE_RENDER(WINED3D_RS_FOGCOLOR
), {STATE_RENDER(WINED3D_RS_FOGCOLOR
), glsl_fragment_pipe_fogparams
}, WINED3D_GL_EXT_NONE
},
8765 {STATE_RENDER(WINED3D_RS_FOGDENSITY
), {STATE_RENDER(WINED3D_RS_FOGDENSITY
), glsl_fragment_pipe_fogparams
}, WINED3D_GL_EXT_NONE
},
8766 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE
), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE
), glsl_fragment_pipe_shader
}, ARB_POINT_SPRITE
},
8767 {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
},
8768 {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
},
8769 {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
},
8770 {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
},
8771 {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
},
8772 {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
},
8773 {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
},
8774 {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
},
8775 {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT
), {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT
), glsl_fragment_pipe_invalidate_constants
}, WINED3D_GL_EXT_NONE
},
8776 {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT
), {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT
), glsl_fragment_pipe_invalidate_constants
}, WINED3D_GL_EXT_NONE
},
8777 {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT
), {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT
), glsl_fragment_pipe_invalidate_constants
}, WINED3D_GL_EXT_NONE
},
8778 {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT
), {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT
), glsl_fragment_pipe_invalidate_constants
}, WINED3D_GL_EXT_NONE
},
8779 {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT
), {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT
), glsl_fragment_pipe_invalidate_constants
}, WINED3D_GL_EXT_NONE
},
8780 {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT
), {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT
), glsl_fragment_pipe_invalidate_constants
}, WINED3D_GL_EXT_NONE
},
8781 {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT
), {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT
), glsl_fragment_pipe_invalidate_constants
}, WINED3D_GL_EXT_NONE
},
8782 {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT
), {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT
), glsl_fragment_pipe_invalidate_constants
}, WINED3D_GL_EXT_NONE
},
8783 {STATE_RENDER(WINED3D_RS_SPECULARENABLE
), {STATE_RENDER(WINED3D_RS_SPECULARENABLE
), glsl_fragment_pipe_invalidate_constants
}, WINED3D_GL_EXT_NONE
},
8784 {STATE_POINT_ENABLE
, {STATE_POINT_ENABLE
, glsl_fragment_pipe_shader
}, WINED3D_GL_EXT_NONE
},
8785 {STATE_RENDER(WINED3D_RS_SHADEMODE
), {STATE_RENDER(WINED3D_RS_SHADEMODE
), state_shademode
}, WINED3D_GL_LEGACY_CONTEXT
},
8786 {STATE_RENDER(WINED3D_RS_SHADEMODE
), {STATE_RENDER(WINED3D_RS_SHADEMODE
), glsl_fragment_pipe_shademode
}, WINED3D_GL_EXT_NONE
},
8787 {0 /* Terminate */, {0, 0 }, WINED3D_GL_EXT_NONE
},
8790 static BOOL
glsl_fragment_pipe_alloc_context_data(struct wined3d_context
*context
)
8795 static void glsl_fragment_pipe_free_context_data(struct wined3d_context
*context
)
8799 const struct fragment_pipeline glsl_fragment_pipe
=
8801 glsl_fragment_pipe_enable
,
8802 glsl_fragment_pipe_get_caps
,
8803 glsl_fragment_pipe_get_emul_mask
,
8804 glsl_fragment_pipe_alloc
,
8805 glsl_fragment_pipe_free
,
8806 glsl_fragment_pipe_alloc_context_data
,
8807 glsl_fragment_pipe_free_context_data
,
8808 shader_glsl_color_fixup_supported
,
8809 glsl_fragment_pipe_state_template
,