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_LOD 0x02
49 #define WINED3D_GLSL_SAMPLE_GRAD 0x04
50 #define WINED3D_GLSL_SAMPLE_LOAD 0x08
51 #define WINED3D_GLSL_SAMPLE_OFFSET 0x10
55 unsigned int coord_size
;
56 unsigned int resinfo_size
;
57 const char *type_part
;
59 resource_type_info
[] =
61 {0, 0, ""}, /* WINED3D_SHADER_RESOURCE_NONE */
62 {1, 1, "Buffer"}, /* WINED3D_SHADER_RESOURCE_BUFFER */
63 {1, 1, "1D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_1D */
64 {2, 2, "2D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2D */
65 {2, 2, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMS */
66 {3, 3, "3D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_3D */
67 {3, 2, "Cube"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_CUBE */
68 {2, 2, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY */
69 {3, 3, "2DArray"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY */
70 {3, 3, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY */
71 {4, 3, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_CUBEARRAY */
86 struct glsl_sample_function
88 struct wined3d_string_buffer
*name
;
89 unsigned int coord_mask
;
90 unsigned int deriv_mask
;
91 enum wined3d_data_type data_type
;
92 BOOL output_single_component
;
93 unsigned int offset_size
;
98 HEAP_NODE_TRAVERSE_LEFT
,
99 HEAP_NODE_TRAVERSE_RIGHT
,
103 struct constant_entry
106 unsigned int version
;
111 struct constant_entry
*entries
;
113 unsigned int *positions
;
117 /* GLSL shader private data */
118 struct shader_glsl_priv
120 struct wined3d_string_buffer shader_buffer
;
121 struct wined3d_string_buffer_list string_buffers
;
122 struct wine_rb_tree program_lookup
;
123 struct constant_heap vconst_heap
;
124 struct constant_heap pconst_heap
;
125 unsigned char *stack
;
126 UINT next_constant_version
;
128 const struct wined3d_vertex_pipe_ops
*vertex_pipe
;
129 const struct fragment_pipeline
*fragment_pipe
;
130 struct wine_rb_tree ffp_vertex_shaders
;
131 struct wine_rb_tree ffp_fragment_shaders
;
132 BOOL ffp_proj_control
;
133 BOOL legacy_lighting
;
136 struct glsl_vs_program
138 struct list shader_entry
;
140 GLenum vertex_color_clamp
;
141 GLint uniform_f_locations
[WINED3D_MAX_VS_CONSTS_F
];
142 GLint uniform_i_locations
[WINED3D_MAX_CONSTS_I
];
143 GLint uniform_b_locations
[WINED3D_MAX_CONSTS_B
];
144 GLint pos_fixup_location
;
146 GLint modelview_matrix_location
[MAX_VERTEX_BLENDS
];
147 GLint projection_matrix_location
;
148 GLint normal_matrix_location
;
149 GLint texture_matrix_location
[MAX_TEXTURES
];
150 GLint material_ambient_location
;
151 GLint material_diffuse_location
;
152 GLint material_specular_location
;
153 GLint material_emissive_location
;
154 GLint material_shininess_location
;
155 GLint light_ambient_location
;
170 } light_location
[MAX_ACTIVE_LIGHTS
];
171 GLint pointsize_location
;
172 GLint pointsize_min_location
;
173 GLint pointsize_max_location
;
174 GLint pointsize_c_att_location
;
175 GLint pointsize_l_att_location
;
176 GLint pointsize_q_att_location
;
177 GLint clip_planes_location
;
180 struct glsl_hs_program
182 struct list shader_entry
;
186 struct glsl_ds_program
188 struct list shader_entry
;
191 GLint pos_fixup_location
;
194 struct glsl_gs_program
196 struct list shader_entry
;
199 GLint pos_fixup_location
;
202 struct glsl_ps_program
204 struct list shader_entry
;
206 GLint uniform_f_locations
[WINED3D_MAX_PS_CONSTS_F
];
207 GLint uniform_i_locations
[WINED3D_MAX_CONSTS_I
];
208 GLint uniform_b_locations
[WINED3D_MAX_CONSTS_B
];
209 GLint bumpenv_mat_location
[MAX_TEXTURES
];
210 GLint bumpenv_lum_scale_location
[MAX_TEXTURES
];
211 GLint bumpenv_lum_offset_location
[MAX_TEXTURES
];
212 GLint tss_constant_location
[MAX_TEXTURES
];
213 GLint tex_factor_location
;
214 GLint specular_enable_location
;
215 GLint fog_color_location
;
216 GLint fog_density_location
;
217 GLint fog_end_location
;
218 GLint fog_scale_location
;
219 GLint alpha_test_ref_location
;
220 GLint ycorrection_location
;
221 GLint np2_fixup_location
;
222 GLint color_key_location
;
223 const struct ps_np2fixup_info
*np2_fixup_info
;
226 struct glsl_cs_program
228 struct list shader_entry
;
232 /* Struct to maintain data about a linked GLSL program */
233 struct glsl_shader_prog_link
235 struct wine_rb_entry program_lookup_entry
;
236 struct glsl_vs_program vs
;
237 struct glsl_hs_program hs
;
238 struct glsl_ds_program ds
;
239 struct glsl_gs_program gs
;
240 struct glsl_ps_program ps
;
241 struct glsl_cs_program cs
;
243 DWORD constant_update_mask
;
244 UINT constant_version
;
247 struct glsl_program_key
257 struct shader_glsl_ctx_priv
{
258 const struct vs_compile_args
*cur_vs_args
;
259 const struct ds_compile_args
*cur_ds_args
;
260 const struct ps_compile_args
*cur_ps_args
;
261 struct ps_np2fixup_info
*cur_np2fixup_info
;
262 struct wined3d_string_buffer_list
*string_buffers
;
265 struct glsl_context_data
267 struct glsl_shader_prog_link
*glsl_program
;
268 GLenum vertex_color_clamp
;
271 struct glsl_ps_compiled_shader
273 struct ps_compile_args args
;
274 struct ps_np2fixup_info np2fixup
;
278 struct glsl_vs_compiled_shader
280 struct vs_compile_args args
;
284 struct glsl_hs_compiled_shader
289 struct glsl_ds_compiled_shader
291 struct ds_compile_args args
;
295 struct glsl_gs_compiled_shader
297 struct gs_compile_args args
;
301 struct glsl_cs_compiled_shader
306 struct glsl_shader_private
310 struct glsl_vs_compiled_shader
*vs
;
311 struct glsl_hs_compiled_shader
*hs
;
312 struct glsl_ds_compiled_shader
*ds
;
313 struct glsl_gs_compiled_shader
*gs
;
314 struct glsl_ps_compiled_shader
*ps
;
315 struct glsl_cs_compiled_shader
*cs
;
317 unsigned int num_gl_shaders
, shader_array_size
;
320 struct glsl_ffp_vertex_shader
322 struct wined3d_ffp_vs_desc desc
;
324 struct list linked_programs
;
327 struct glsl_ffp_fragment_shader
329 struct ffp_frag_desc entry
;
331 struct list linked_programs
;
334 struct glsl_ffp_destroy_ctx
336 struct shader_glsl_priv
*priv
;
337 const struct wined3d_gl_info
*gl_info
;
340 static void shader_glsl_generate_shader_epilogue(const struct wined3d_shader_context
*ctx
);
342 static const char *debug_gl_shader_type(GLenum type
)
346 #define WINED3D_TO_STR(u) case u: return #u
347 WINED3D_TO_STR(GL_VERTEX_SHADER
);
348 WINED3D_TO_STR(GL_TESS_CONTROL_SHADER
);
349 WINED3D_TO_STR(GL_TESS_EVALUATION_SHADER
);
350 WINED3D_TO_STR(GL_GEOMETRY_SHADER
);
351 WINED3D_TO_STR(GL_FRAGMENT_SHADER
);
352 WINED3D_TO_STR(GL_COMPUTE_SHADER
);
353 #undef WINED3D_TO_STR
355 return wine_dbg_sprintf("UNKNOWN(%#x)", type
);
359 static const char *shader_glsl_get_prefix(enum wined3d_shader_type type
)
363 case WINED3D_SHADER_TYPE_VERTEX
:
366 case WINED3D_SHADER_TYPE_HULL
:
369 case WINED3D_SHADER_TYPE_DOMAIN
:
372 case WINED3D_SHADER_TYPE_GEOMETRY
:
375 case WINED3D_SHADER_TYPE_PIXEL
:
378 case WINED3D_SHADER_TYPE_COMPUTE
:
382 FIXME("Unhandled shader type %#x.\n", type
);
387 static unsigned int shader_glsl_get_version(const struct wined3d_gl_info
*gl_info
)
389 if (gl_info
->glsl_version
>= MAKEDWORD_VERSION(4, 40))
391 else if (gl_info
->glsl_version
>= MAKEDWORD_VERSION(1, 50))
393 else if (gl_info
->glsl_version
>= MAKEDWORD_VERSION(1, 30))
399 static void shader_glsl_add_version_declaration(struct wined3d_string_buffer
*buffer
,
400 const struct wined3d_gl_info
*gl_info
)
402 shader_addline(buffer
, "#version %u\n", shader_glsl_get_version(gl_info
));
405 static void shader_glsl_append_imm_vec4(struct wined3d_string_buffer
*buffer
, const float *values
)
409 wined3d_ftoa(values
[0], str
[0]);
410 wined3d_ftoa(values
[1], str
[1]);
411 wined3d_ftoa(values
[2], str
[2]);
412 wined3d_ftoa(values
[3], str
[3]);
413 shader_addline(buffer
, "vec4(%s, %s, %s, %s)", str
[0], str
[1], str
[2], str
[3]);
416 static void shader_glsl_append_imm_ivec(struct wined3d_string_buffer
*buffer
,
417 const int *values
, unsigned int size
)
421 if (!size
|| size
> 4)
423 ERR("Invalid vector size %u.\n", size
);
428 shader_addline(buffer
, "ivec%u(", size
);
430 for (i
= 0; i
< size
; ++i
)
431 shader_addline(buffer
, i
? ", %#x" : "%#x", values
[i
]);
434 shader_addline(buffer
, ")");
437 static const char *get_info_log_line(const char **ptr
)
442 if (!(q
= strstr(p
, "\n")))
444 if (!*p
) return NULL
;
453 /* Context activation is done by the caller. */
454 void print_glsl_info_log(const struct wined3d_gl_info
*gl_info
, GLuint id
, BOOL program
)
459 if (!WARN_ON(d3d_shader
) && !FIXME_ON(d3d_shader
))
463 GL_EXTCALL(glGetProgramiv(id
, GL_INFO_LOG_LENGTH
, &length
));
465 GL_EXTCALL(glGetShaderiv(id
, GL_INFO_LOG_LENGTH
, &length
));
467 /* A size of 1 is just a null-terminated string, so the log should be bigger than
468 * that if there are errors. */
471 const char *ptr
, *line
;
473 log
= HeapAlloc(GetProcessHeap(), 0, length
);
474 /* The info log is supposed to be zero-terminated, but at least some
475 * versions of fglrx don't terminate the string properly. The reported
476 * length does include the terminator, so explicitly set it to zero
480 GL_EXTCALL(glGetProgramInfoLog(id
, length
, NULL
, log
));
482 GL_EXTCALL(glGetShaderInfoLog(id
, length
, NULL
, log
));
485 if (gl_info
->quirks
& WINED3D_QUIRK_INFO_LOG_SPAM
)
487 WARN("Info log received from GLSL shader #%u:\n", id
);
488 while ((line
= get_info_log_line(&ptr
))) WARN(" %.*s", (int)(ptr
- line
), line
);
492 FIXME("Info log received from GLSL shader #%u:\n", id
);
493 while ((line
= get_info_log_line(&ptr
))) FIXME(" %.*s", (int)(ptr
- line
), line
);
495 HeapFree(GetProcessHeap(), 0, log
);
499 /* Context activation is done by the caller. */
500 static void shader_glsl_compile(const struct wined3d_gl_info
*gl_info
, GLuint shader
, const char *src
)
502 const char *ptr
, *line
;
504 TRACE("Compiling shader object %u.\n", shader
);
506 if (TRACE_ON(d3d_shader
))
509 while ((line
= get_info_log_line(&ptr
))) TRACE_(d3d_shader
)(" %.*s", (int)(ptr
- line
), line
);
512 GL_EXTCALL(glShaderSource(shader
, 1, &src
, NULL
));
513 checkGLcall("glShaderSource");
514 GL_EXTCALL(glCompileShader(shader
));
515 checkGLcall("glCompileShader");
516 print_glsl_info_log(gl_info
, shader
, FALSE
);
519 /* Context activation is done by the caller. */
520 static void shader_glsl_dump_program_source(const struct wined3d_gl_info
*gl_info
, GLuint program
)
522 GLint i
, shader_count
, source_size
= -1;
526 GL_EXTCALL(glGetProgramiv(program
, GL_ATTACHED_SHADERS
, &shader_count
));
527 if (!(shaders
= wined3d_calloc(shader_count
, sizeof(*shaders
))))
529 ERR("Failed to allocate shader array memory.\n");
533 GL_EXTCALL(glGetAttachedShaders(program
, shader_count
, NULL
, shaders
));
534 for (i
= 0; i
< shader_count
; ++i
)
536 const char *ptr
, *line
;
539 GL_EXTCALL(glGetShaderiv(shaders
[i
], GL_SHADER_SOURCE_LENGTH
, &tmp
));
541 if (source_size
< tmp
)
543 HeapFree(GetProcessHeap(), 0, source
);
545 source
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, tmp
);
548 ERR("Failed to allocate %d bytes for shader source.\n", tmp
);
549 HeapFree(GetProcessHeap(), 0, shaders
);
555 FIXME("Shader %u:\n", shaders
[i
]);
556 GL_EXTCALL(glGetShaderiv(shaders
[i
], GL_SHADER_TYPE
, &tmp
));
557 FIXME(" GL_SHADER_TYPE: %s.\n", debug_gl_shader_type(tmp
));
558 GL_EXTCALL(glGetShaderiv(shaders
[i
], GL_COMPILE_STATUS
, &tmp
));
559 FIXME(" GL_COMPILE_STATUS: %d.\n", tmp
);
563 GL_EXTCALL(glGetShaderSource(shaders
[i
], source_size
, NULL
, source
));
564 while ((line
= get_info_log_line(&ptr
))) FIXME(" %.*s", (int)(ptr
- line
), line
);
568 HeapFree(GetProcessHeap(), 0, source
);
569 HeapFree(GetProcessHeap(), 0, shaders
);
572 /* Context activation is done by the caller. */
573 void shader_glsl_validate_link(const struct wined3d_gl_info
*gl_info
, GLuint program
)
577 if (!TRACE_ON(d3d_shader
) && !FIXME_ON(d3d_shader
))
580 GL_EXTCALL(glGetProgramiv(program
, GL_LINK_STATUS
, &tmp
));
583 FIXME("Program %u link status invalid.\n", program
);
584 shader_glsl_dump_program_source(gl_info
, program
);
587 print_glsl_info_log(gl_info
, program
, TRUE
);
590 static BOOL
shader_glsl_use_layout_qualifier(const struct wined3d_gl_info
*gl_info
)
592 /* Layout qualifiers were introduced in GLSL 1.40. The Nvidia Legacy GPU
593 * driver (series 340.xx) doesn't parse layout qualifiers in older GLSL
595 return shader_glsl_get_version(gl_info
) >= 140;
598 static BOOL
shader_glsl_use_layout_binding_qualifier(const struct wined3d_gl_info
*gl_info
)
600 return gl_info
->supported
[ARB_SHADING_LANGUAGE_420PACK
] && shader_glsl_use_layout_qualifier(gl_info
);
603 static void shader_glsl_init_uniform_block_bindings(const struct wined3d_gl_info
*gl_info
,
604 struct shader_glsl_priv
*priv
, GLuint program_id
,
605 const struct wined3d_shader_reg_maps
*reg_maps
)
607 const char *prefix
= shader_glsl_get_prefix(reg_maps
->shader_version
.type
);
608 struct wined3d_string_buffer
*name
;
609 unsigned int i
, base
, count
;
612 if (shader_glsl_use_layout_binding_qualifier(gl_info
))
615 name
= string_buffer_get(&priv
->string_buffers
);
616 wined3d_gl_limits_get_uniform_block_range(&gl_info
->limits
, reg_maps
->shader_version
.type
, &base
, &count
);
617 for (i
= 0; i
< count
; ++i
)
619 if (!reg_maps
->cb_sizes
[i
])
622 string_buffer_sprintf(name
, "block_%s_cb%u", prefix
, i
);
623 block_idx
= GL_EXTCALL(glGetUniformBlockIndex(program_id
, name
->buffer
));
624 GL_EXTCALL(glUniformBlockBinding(program_id
, block_idx
, base
+ i
));
626 checkGLcall("glUniformBlockBinding");
627 string_buffer_release(&priv
->string_buffers
, name
);
630 /* Context activation is done by the caller. */
631 static void shader_glsl_load_samplers_range(const struct wined3d_gl_info
*gl_info
,
632 struct shader_glsl_priv
*priv
, GLuint program_id
, const char *prefix
,
633 unsigned int base
, unsigned int count
, const DWORD
*tex_unit_map
)
635 struct wined3d_string_buffer
*sampler_name
= string_buffer_get(&priv
->string_buffers
);
636 unsigned int i
, mapped_unit
;
639 for (i
= 0; i
< count
; ++i
)
641 string_buffer_sprintf(sampler_name
, "%s_sampler%u", prefix
, i
);
642 name_loc
= GL_EXTCALL(glGetUniformLocation(program_id
, sampler_name
->buffer
));
646 mapped_unit
= tex_unit_map
? tex_unit_map
[base
+ i
] : base
+ i
;
647 if (mapped_unit
== WINED3D_UNMAPPED_STAGE
|| mapped_unit
>= gl_info
->limits
.combined_samplers
)
649 ERR("Trying to load sampler %s on unsupported unit %u.\n", sampler_name
->buffer
, mapped_unit
);
653 TRACE("Loading sampler %s on unit %u.\n", sampler_name
->buffer
, mapped_unit
);
654 GL_EXTCALL(glUniform1i(name_loc
, mapped_unit
));
656 checkGLcall("Load sampler bindings");
657 string_buffer_release(&priv
->string_buffers
, sampler_name
);
660 static unsigned int shader_glsl_map_tex_unit(const struct wined3d_context
*context
,
661 const struct wined3d_shader_version
*shader_version
, unsigned int sampler_idx
)
663 const DWORD
*tex_unit_map
;
664 unsigned int base
, count
;
666 tex_unit_map
= context_get_tex_unit_mapping(context
, shader_version
, &base
, &count
);
667 if (sampler_idx
>= count
)
668 return WINED3D_UNMAPPED_STAGE
;
670 return base
+ sampler_idx
;
671 return tex_unit_map
[base
+ sampler_idx
];
674 static void shader_glsl_append_sampler_binding_qualifier(struct wined3d_string_buffer
*buffer
,
675 const struct wined3d_context
*context
, const struct wined3d_shader_version
*shader_version
,
676 unsigned int sampler_idx
)
678 unsigned int mapped_unit
= shader_glsl_map_tex_unit(context
, shader_version
, sampler_idx
);
679 if (mapped_unit
!= WINED3D_UNMAPPED_STAGE
)
680 shader_addline(buffer
, "layout(binding = %u)\n", mapped_unit
);
682 ERR("Unmapped sampler %u.\n", sampler_idx
);
685 /* Context activation is done by the caller. */
686 static void shader_glsl_load_samplers(const struct wined3d_context
*context
,
687 struct shader_glsl_priv
*priv
, GLuint program_id
, const struct wined3d_shader_reg_maps
*reg_maps
)
689 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
690 const struct wined3d_shader_version
*shader_version
;
691 const DWORD
*tex_unit_map
;
692 unsigned int base
, count
;
695 if (shader_glsl_use_layout_binding_qualifier(gl_info
))
698 shader_version
= reg_maps
? ®_maps
->shader_version
: NULL
;
699 prefix
= shader_glsl_get_prefix(shader_version
? shader_version
->type
: WINED3D_SHADER_TYPE_PIXEL
);
700 tex_unit_map
= context_get_tex_unit_mapping(context
, shader_version
, &base
, &count
);
701 shader_glsl_load_samplers_range(gl_info
, priv
, program_id
, prefix
, base
, count
, tex_unit_map
);
704 static void shader_glsl_load_icb(const struct wined3d_gl_info
*gl_info
, struct shader_glsl_priv
*priv
,
705 GLuint program_id
, const struct wined3d_shader_reg_maps
*reg_maps
)
707 const struct wined3d_shader_immediate_constant_buffer
*icb
= reg_maps
->icb
;
711 struct wined3d_string_buffer
*icb_name
= string_buffer_get(&priv
->string_buffers
);
712 const char *prefix
= shader_glsl_get_prefix(reg_maps
->shader_version
.type
);
715 string_buffer_sprintf(icb_name
, "%s_icb", prefix
);
716 icb_location
= GL_EXTCALL(glGetUniformLocation(program_id
, icb_name
->buffer
));
717 GL_EXTCALL(glUniform4fv(icb_location
, icb
->vec4_count
, (const GLfloat
*)icb
->data
));
718 checkGLcall("Load immediate constant buffer");
720 string_buffer_release(&priv
->string_buffers
, icb_name
);
724 /* Context activation is done by the caller. */
725 static void shader_glsl_load_images(const struct wined3d_gl_info
*gl_info
, struct shader_glsl_priv
*priv
,
726 GLuint program_id
, const struct wined3d_shader_reg_maps
*reg_maps
)
728 const char *prefix
= shader_glsl_get_prefix(reg_maps
->shader_version
.type
);
729 struct wined3d_string_buffer
*name
;
733 if (shader_glsl_use_layout_binding_qualifier(gl_info
))
736 name
= string_buffer_get(&priv
->string_buffers
);
737 for (i
= 0; i
< MAX_UNORDERED_ACCESS_VIEWS
; ++i
)
739 if (!reg_maps
->uav_resource_info
[i
].type
)
742 string_buffer_sprintf(name
, "%s_image%u", prefix
, i
);
743 location
= GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
747 TRACE("Loading image %s on unit %u.\n", name
->buffer
, i
);
748 GL_EXTCALL(glUniform1i(location
, i
));
750 checkGLcall("Load image bindings");
751 string_buffer_release(&priv
->string_buffers
, name
);
754 /* Context activation is done by the caller. */
755 static void shader_glsl_load_program_resources(const struct wined3d_context
*context
,
756 struct shader_glsl_priv
*priv
, GLuint program_id
, const struct wined3d_shader
*shader
)
758 const struct wined3d_shader_reg_maps
*reg_maps
= &shader
->reg_maps
;
760 shader_glsl_init_uniform_block_bindings(context
->gl_info
, priv
, program_id
, reg_maps
);
761 shader_glsl_load_icb(context
->gl_info
, priv
, program_id
, reg_maps
);
762 /* Texture unit mapping is set up to be the same each time the shader
763 * program is used so we can hardcode the sampler uniform values. */
764 shader_glsl_load_samplers(context
, priv
, program_id
, reg_maps
);
767 static void append_transform_feedback_varying(const char **varyings
, unsigned int *varying_count
,
768 char **strings
, unsigned int *strings_length
, struct wined3d_string_buffer
*buffer
)
770 if (varyings
&& *strings
)
772 char *ptr
= *strings
;
774 varyings
[*varying_count
] = ptr
;
776 memcpy(ptr
, buffer
->buffer
, buffer
->content_size
+ 1);
777 ptr
+= buffer
->content_size
+ 1;
782 *strings_length
+= buffer
->content_size
+ 1;
786 static void append_transform_feedback_skip_components(const char **varyings
,
787 unsigned int *varying_count
, char **strings
, unsigned int *strings_length
,
788 struct wined3d_string_buffer
*buffer
, unsigned int component_count
)
792 for (j
= 0; j
< component_count
/ 4; ++j
)
794 string_buffer_sprintf(buffer
, "gl_SkipComponents4");
795 append_transform_feedback_varying(varyings
, varying_count
, strings
, strings_length
, buffer
);
797 if (component_count
% 4)
799 string_buffer_sprintf(buffer
, "gl_SkipComponents%u", component_count
% 4);
800 append_transform_feedback_varying(varyings
, varying_count
, strings
, strings_length
, buffer
);
804 static void shader_glsl_generate_transform_feedback_varyings(const struct wined3d_stream_output_desc
*so_desc
,
805 struct wined3d_string_buffer
*buffer
, const char **varyings
, unsigned int *varying_count
,
806 char *strings
, unsigned int *strings_length
, GLenum buffer_mode
)
808 unsigned int i
, buffer_idx
, count
, length
, highest_output_slot
, stride
;
811 highest_output_slot
= 0;
812 for (buffer_idx
= 0; buffer_idx
< WINED3D_MAX_STREAM_OUTPUT_BUFFERS
; ++buffer_idx
)
816 for (i
= 0; i
< so_desc
->element_count
; ++i
)
818 const struct wined3d_stream_output_element
*e
= &so_desc
->elements
[i
];
820 highest_output_slot
= max(highest_output_slot
, e
->output_slot
);
821 if (e
->output_slot
!= buffer_idx
)
826 FIXME("Unhandled stream %u.\n", e
->stream_idx
);
830 stride
+= e
->component_count
;
832 if (e
->register_idx
== WINED3D_STREAM_OUTPUT_GAP
)
834 append_transform_feedback_skip_components(varyings
, &count
,
835 &strings
, &length
, buffer
, e
->component_count
);
839 if (e
->component_idx
|| e
->component_count
!= 4)
841 FIXME("Unsupported component range %u-%u.\n", e
->component_idx
, e
->component_count
);
842 append_transform_feedback_skip_components(varyings
, &count
,
843 &strings
, &length
, buffer
, e
->component_count
);
847 string_buffer_sprintf(buffer
, "shader_in_out.reg%u", e
->register_idx
);
848 append_transform_feedback_varying(varyings
, &count
, &strings
, &length
, buffer
);
851 if (buffer_idx
< so_desc
->buffer_stride_count
852 && stride
< so_desc
->buffer_strides
[buffer_idx
] / 4)
854 unsigned int component_count
= so_desc
->buffer_strides
[buffer_idx
] / 4 - stride
;
855 append_transform_feedback_skip_components(varyings
, &count
,
856 &strings
, &length
, buffer
, component_count
);
859 if (highest_output_slot
<= buffer_idx
)
862 if (buffer_mode
== GL_INTERLEAVED_ATTRIBS
)
864 string_buffer_sprintf(buffer
, "gl_NextBuffer");
865 append_transform_feedback_varying(varyings
, &count
, &strings
, &length
, buffer
);
870 *varying_count
= count
;
872 *strings_length
= length
;
875 static void shader_glsl_init_transform_feedback(const struct wined3d_context
*context
,
876 struct shader_glsl_priv
*priv
, GLuint program_id
, const struct wined3d_shader
*shader
)
878 const struct wined3d_stream_output_desc
*so_desc
= &shader
->u
.gs
.so_desc
;
879 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
880 struct wined3d_string_buffer
*buffer
;
881 unsigned int i
, count
, length
;
882 const char **varyings
;
886 if (!so_desc
->element_count
)
889 if (gl_info
->supported
[ARB_TRANSFORM_FEEDBACK3
])
891 mode
= GL_INTERLEAVED_ATTRIBS
;
895 unsigned int element_count
[WINED3D_MAX_STREAM_OUTPUT_BUFFERS
] = {0};
897 for (i
= 0; i
< so_desc
->element_count
; ++i
)
899 if (so_desc
->elements
[i
].register_idx
== WINED3D_STREAM_OUTPUT_GAP
)
901 FIXME("ARB_transform_feedback3 is needed for stream output gaps.\n");
904 ++element_count
[so_desc
->elements
[i
].output_slot
];
907 if (element_count
[0] == so_desc
->element_count
)
909 mode
= GL_INTERLEAVED_ATTRIBS
;
913 mode
= GL_SEPARATE_ATTRIBS
;
914 for (i
= 0; i
< ARRAY_SIZE(element_count
); ++i
)
916 if (element_count
[i
] != 1)
919 for (; i
< ARRAY_SIZE(element_count
); ++i
)
921 if (element_count
[i
])
923 FIXME("Only single element per buffer is allowed in separate mode.\n");
930 buffer
= string_buffer_get(&priv
->string_buffers
);
932 shader_glsl_generate_transform_feedback_varyings(so_desc
, buffer
, NULL
, &count
, NULL
, &length
, mode
);
934 if (!(varyings
= wined3d_calloc(count
, sizeof(*varyings
))))
936 ERR("Out of memory.\n");
937 string_buffer_release(&priv
->string_buffers
, buffer
);
940 if (!(strings
= wined3d_calloc(length
, sizeof(*strings
))))
942 ERR("Out of memory.\n");
943 HeapFree(GetProcessHeap(), 0, varyings
);
944 string_buffer_release(&priv
->string_buffers
, buffer
);
948 shader_glsl_generate_transform_feedback_varyings(so_desc
, buffer
, varyings
, NULL
, strings
, NULL
, mode
);
949 GL_EXTCALL(glTransformFeedbackVaryings(program_id
, count
, varyings
, mode
));
950 checkGLcall("glTransformFeedbackVaryings");
952 HeapFree(GetProcessHeap(), 0, varyings
);
953 HeapFree(GetProcessHeap(), 0, strings
);
954 string_buffer_release(&priv
->string_buffers
, buffer
);
957 /* Context activation is done by the caller. */
958 static inline void walk_constant_heap(const struct wined3d_gl_info
*gl_info
, const struct wined3d_vec4
*constants
,
959 const GLint
*constant_locations
, const struct constant_heap
*heap
, unsigned char *stack
, DWORD version
)
961 unsigned int start
= ~0U, end
= 0;
963 unsigned int heap_idx
= 1;
966 if (heap
->entries
[heap_idx
].version
<= version
) return;
968 idx
= heap
->entries
[heap_idx
].idx
;
969 if (constant_locations
[idx
] != -1)
971 stack
[stack_idx
] = HEAP_NODE_TRAVERSE_LEFT
;
973 while (stack_idx
>= 0)
975 /* Note that we fall through to the next case statement. */
976 switch(stack
[stack_idx
])
978 case HEAP_NODE_TRAVERSE_LEFT
:
980 unsigned int left_idx
= heap_idx
<< 1;
981 if (left_idx
< heap
->size
&& heap
->entries
[left_idx
].version
> version
)
984 idx
= heap
->entries
[heap_idx
].idx
;
985 if (constant_locations
[idx
] != -1)
993 stack
[stack_idx
++] = HEAP_NODE_TRAVERSE_RIGHT
;
994 stack
[stack_idx
] = HEAP_NODE_TRAVERSE_LEFT
;
999 case HEAP_NODE_TRAVERSE_RIGHT
:
1001 unsigned int right_idx
= (heap_idx
<< 1) + 1;
1002 if (right_idx
< heap
->size
&& heap
->entries
[right_idx
].version
> version
)
1004 heap_idx
= right_idx
;
1005 idx
= heap
->entries
[heap_idx
].idx
;
1006 if (constant_locations
[idx
] != -1)
1014 stack
[stack_idx
++] = HEAP_NODE_POP
;
1015 stack
[stack_idx
] = HEAP_NODE_TRAVERSE_LEFT
;
1027 GL_EXTCALL(glUniform4fv(constant_locations
[start
], end
- start
+ 1, &constants
[start
].x
));
1028 checkGLcall("walk_constant_heap()");
1031 /* Context activation is done by the caller. */
1032 static inline void apply_clamped_constant(const struct wined3d_gl_info
*gl_info
,
1033 GLint location
, const struct wined3d_vec4
*data
)
1035 GLfloat clamped_constant
[4];
1037 if (location
== -1) return;
1039 clamped_constant
[0] = data
->x
< -1.0f
? -1.0f
: data
->x
> 1.0f
? 1.0f
: data
->x
;
1040 clamped_constant
[1] = data
->y
< -1.0f
? -1.0f
: data
->y
> 1.0f
? 1.0f
: data
->y
;
1041 clamped_constant
[2] = data
->z
< -1.0f
? -1.0f
: data
->z
> 1.0f
? 1.0f
: data
->z
;
1042 clamped_constant
[3] = data
->w
< -1.0f
? -1.0f
: data
->w
> 1.0f
? 1.0f
: data
->w
;
1044 GL_EXTCALL(glUniform4fv(location
, 1, clamped_constant
));
1047 /* Context activation is done by the caller. */
1048 static inline void walk_constant_heap_clamped(const struct wined3d_gl_info
*gl_info
,
1049 const struct wined3d_vec4
*constants
, const GLint
*constant_locations
,
1050 const struct constant_heap
*heap
, unsigned char *stack
, DWORD version
)
1053 unsigned int heap_idx
= 1;
1056 if (heap
->entries
[heap_idx
].version
<= version
) return;
1058 idx
= heap
->entries
[heap_idx
].idx
;
1059 apply_clamped_constant(gl_info
, constant_locations
[idx
], &constants
[idx
]);
1060 stack
[stack_idx
] = HEAP_NODE_TRAVERSE_LEFT
;
1062 while (stack_idx
>= 0)
1064 /* Note that we fall through to the next case statement. */
1065 switch(stack
[stack_idx
])
1067 case HEAP_NODE_TRAVERSE_LEFT
:
1069 unsigned int left_idx
= heap_idx
<< 1;
1070 if (left_idx
< heap
->size
&& heap
->entries
[left_idx
].version
> version
)
1072 heap_idx
= left_idx
;
1073 idx
= heap
->entries
[heap_idx
].idx
;
1074 apply_clamped_constant(gl_info
, constant_locations
[idx
], &constants
[idx
]);
1076 stack
[stack_idx
++] = HEAP_NODE_TRAVERSE_RIGHT
;
1077 stack
[stack_idx
] = HEAP_NODE_TRAVERSE_LEFT
;
1082 case HEAP_NODE_TRAVERSE_RIGHT
:
1084 unsigned int right_idx
= (heap_idx
<< 1) + 1;
1085 if (right_idx
< heap
->size
&& heap
->entries
[right_idx
].version
> version
)
1087 heap_idx
= right_idx
;
1088 idx
= heap
->entries
[heap_idx
].idx
;
1089 apply_clamped_constant(gl_info
, constant_locations
[idx
], &constants
[idx
]);
1091 stack
[stack_idx
++] = HEAP_NODE_POP
;
1092 stack
[stack_idx
] = HEAP_NODE_TRAVERSE_LEFT
;
1103 checkGLcall("walk_constant_heap_clamped()");
1106 /* Context activation is done by the caller. */
1107 static void shader_glsl_load_constants_f(const struct wined3d_shader
*shader
, const struct wined3d_gl_info
*gl_info
,
1108 const struct wined3d_vec4
*constants
, const GLint
*constant_locations
, const struct constant_heap
*heap
,
1109 unsigned char *stack
, unsigned int version
)
1111 const struct wined3d_shader_lconst
*lconst
;
1113 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
1114 if (shader
->reg_maps
.shader_version
.major
== 1
1115 && shader
->reg_maps
.shader_version
.type
== WINED3D_SHADER_TYPE_PIXEL
)
1116 walk_constant_heap_clamped(gl_info
, constants
, constant_locations
, heap
, stack
, version
);
1118 walk_constant_heap(gl_info
, constants
, constant_locations
, heap
, stack
, version
);
1120 if (!shader
->load_local_constsF
)
1122 TRACE("No need to load local float constants for this shader.\n");
1126 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
1127 LIST_FOR_EACH_ENTRY(lconst
, &shader
->constantsF
, struct wined3d_shader_lconst
, entry
)
1129 GL_EXTCALL(glUniform4fv(constant_locations
[lconst
->idx
], 1, (const GLfloat
*)lconst
->value
));
1131 checkGLcall("glUniform4fv()");
1134 /* Context activation is done by the caller. */
1135 static void shader_glsl_load_constants_i(const struct wined3d_shader
*shader
, const struct wined3d_gl_info
*gl_info
,
1136 const struct wined3d_ivec4
*constants
, const GLint locations
[WINED3D_MAX_CONSTS_I
], WORD constants_set
)
1141 for (i
= 0; constants_set
; constants_set
>>= 1, ++i
)
1143 if (!(constants_set
& 1)) continue;
1145 /* We found this uniform name in the program - go ahead and send the data */
1146 GL_EXTCALL(glUniform4iv(locations
[i
], 1, &constants
[i
].x
));
1149 /* Load immediate constants */
1150 ptr
= list_head(&shader
->constantsI
);
1153 const struct wined3d_shader_lconst
*lconst
= LIST_ENTRY(ptr
, const struct wined3d_shader_lconst
, entry
);
1154 unsigned int idx
= lconst
->idx
;
1155 const GLint
*values
= (const GLint
*)lconst
->value
;
1157 /* We found this uniform name in the program - go ahead and send the data */
1158 GL_EXTCALL(glUniform4iv(locations
[idx
], 1, values
));
1159 ptr
= list_next(&shader
->constantsI
, ptr
);
1161 checkGLcall("glUniform4iv()");
1164 /* Context activation is done by the caller. */
1165 static void shader_glsl_load_constantsB(const struct wined3d_shader
*shader
, const struct wined3d_gl_info
*gl_info
,
1166 const GLint locations
[WINED3D_MAX_CONSTS_B
], const BOOL
*constants
, WORD constants_set
)
1171 for (i
= 0; constants_set
; constants_set
>>= 1, ++i
)
1173 if (!(constants_set
& 1)) continue;
1175 GL_EXTCALL(glUniform1iv(locations
[i
], 1, &constants
[i
]));
1178 /* Load immediate constants */
1179 ptr
= list_head(&shader
->constantsB
);
1182 const struct wined3d_shader_lconst
*lconst
= LIST_ENTRY(ptr
, const struct wined3d_shader_lconst
, entry
);
1183 unsigned int idx
= lconst
->idx
;
1184 const GLint
*values
= (const GLint
*)lconst
->value
;
1186 GL_EXTCALL(glUniform1iv(locations
[idx
], 1, values
));
1187 ptr
= list_next(&shader
->constantsB
, ptr
);
1189 checkGLcall("glUniform1iv()");
1192 static void reset_program_constant_version(struct wine_rb_entry
*entry
, void *context
)
1194 WINE_RB_ENTRY_VALUE(entry
, struct glsl_shader_prog_link
, program_lookup_entry
)->constant_version
= 0;
1197 /* Context activation is done by the caller (state handler). */
1198 static void shader_glsl_load_np2fixup_constants(const struct glsl_ps_program
*ps
,
1199 const struct wined3d_gl_info
*gl_info
, const struct wined3d_state
*state
)
1205 np2fixup_constants
[MAX_FRAGMENT_SAMPLERS
];
1206 UINT fixup
= ps
->np2_fixup_info
->active
;
1209 for (i
= 0; fixup
; fixup
>>= 1, ++i
)
1211 const struct wined3d_texture
*tex
= state
->textures
[i
];
1212 unsigned char idx
= ps
->np2_fixup_info
->idx
[i
];
1216 ERR("Nonexistent texture is flagged for NP2 texcoord fixup.\n");
1220 np2fixup_constants
[idx
].sx
= tex
->pow2_matrix
[0];
1221 np2fixup_constants
[idx
].sy
= tex
->pow2_matrix
[5];
1224 GL_EXTCALL(glUniform4fv(ps
->np2_fixup_location
, ps
->np2_fixup_info
->num_consts
, &np2fixup_constants
[0].sx
));
1227 /* Taken and adapted from Mesa. */
1228 static BOOL
invert_matrix_3d(struct wined3d_matrix
*out
, const struct wined3d_matrix
*in
)
1230 float pos
, neg
, t
, det
;
1231 struct wined3d_matrix temp
;
1233 /* Calculate the determinant of upper left 3x3 submatrix and
1234 * determine if the matrix is singular. */
1236 t
= in
->_11
* in
->_22
* in
->_33
;
1242 t
= in
->_21
* in
->_32
* in
->_13
;
1247 t
= in
->_31
* in
->_12
* in
->_23
;
1253 t
= -in
->_31
* in
->_22
* in
->_13
;
1258 t
= -in
->_21
* in
->_12
* in
->_33
;
1264 t
= -in
->_11
* in
->_32
* in
->_23
;
1272 if (fabsf(det
) < 1e-25f
)
1276 temp
._11
= (in
->_22
* in
->_33
- in
->_32
* in
->_23
) * det
;
1277 temp
._12
= -(in
->_12
* in
->_33
- in
->_32
* in
->_13
) * det
;
1278 temp
._13
= (in
->_12
* in
->_23
- in
->_22
* in
->_13
) * det
;
1279 temp
._21
= -(in
->_21
* in
->_33
- in
->_31
* in
->_23
) * det
;
1280 temp
._22
= (in
->_11
* in
->_33
- in
->_31
* in
->_13
) * det
;
1281 temp
._23
= -(in
->_11
* in
->_23
- in
->_21
* in
->_13
) * det
;
1282 temp
._31
= (in
->_21
* in
->_32
- in
->_31
* in
->_22
) * det
;
1283 temp
._32
= -(in
->_11
* in
->_32
- in
->_31
* in
->_12
) * det
;
1284 temp
._33
= (in
->_11
* in
->_22
- in
->_21
* in
->_12
) * det
;
1290 static void swap_rows(float **a
, float **b
)
1298 static BOOL
invert_matrix(struct wined3d_matrix
*out
, struct wined3d_matrix
*m
)
1301 float m0
, m1
, m2
, m3
, s
;
1302 float *r0
, *r1
, *r2
, *r3
;
1314 r0
[5] = r0
[6] = r0
[7] = 0.0f
;
1321 r1
[4] = r1
[6] = r1
[7] = 0.0f
;
1328 r2
[4] = r2
[5] = r2
[7] = 0.0f
;
1335 r3
[4] = r3
[5] = r3
[6] = 0.0f
;
1337 /* Choose pivot - or die. */
1338 if (fabsf(r3
[0]) > fabsf(r2
[0]))
1339 swap_rows(&r3
, &r2
);
1340 if (fabsf(r2
[0]) > fabsf(r1
[0]))
1341 swap_rows(&r2
, &r1
);
1342 if (fabsf(r1
[0]) > fabsf(r0
[0]))
1343 swap_rows(&r1
, &r0
);
1347 /* Eliminate first variable. */
1348 m1
= r1
[0] / r0
[0]; m2
= r2
[0] / r0
[0]; m3
= r3
[0] / r0
[0];
1349 s
= r0
[1]; r1
[1] -= m1
* s
; r2
[1] -= m2
* s
; r3
[1] -= m3
* s
;
1350 s
= r0
[2]; r1
[2] -= m1
* s
; r2
[2] -= m2
* s
; r3
[2] -= m3
* s
;
1351 s
= r0
[3]; r1
[3] -= m1
* s
; r2
[3] -= m2
* s
; r3
[3] -= m3
* s
;
1381 /* Choose pivot - or die. */
1382 if (fabsf(r3
[1]) > fabsf(r2
[1]))
1383 swap_rows(&r3
, &r2
);
1384 if (fabsf(r2
[1]) > fabsf(r1
[1]))
1385 swap_rows(&r2
, &r1
);
1389 /* Eliminate second variable. */
1390 m2
= r2
[1] / r1
[1]; m3
= r3
[1] / r1
[1];
1391 r2
[2] -= m2
* r1
[2]; r3
[2] -= m3
* r1
[2];
1392 r2
[3] -= m2
* r1
[3]; r3
[3] -= m3
* r1
[3];
1418 /* Choose pivot - or die. */
1419 if (fabsf(r3
[2]) > fabsf(r2
[2]))
1420 swap_rows(&r3
, &r2
);
1424 /* Eliminate third variable. */
1426 r3
[3] -= m3
* r2
[3];
1427 r3
[4] -= m3
* r2
[4];
1428 r3
[5] -= m3
* r2
[5];
1429 r3
[6] -= m3
* r2
[6];
1430 r3
[7] -= m3
* r2
[7];
1436 /* Back substitute row 3. */
1443 /* Back substitute row 2. */
1446 r2
[4] = s
* (r2
[4] - r3
[4] * m2
);
1447 r2
[5] = s
* (r2
[5] - r3
[5] * m2
);
1448 r2
[6] = s
* (r2
[6] - r3
[6] * m2
);
1449 r2
[7] = s
* (r2
[7] - r3
[7] * m2
);
1451 r1
[4] -= r3
[4] * m1
;
1452 r1
[5] -= r3
[5] * m1
;
1453 r1
[6] -= r3
[6] * m1
;
1454 r1
[7] -= r3
[7] * m1
;
1456 r0
[4] -= r3
[4] * m0
;
1457 r0
[5] -= r3
[5] * m0
;
1458 r0
[6] -= r3
[6] * m0
;
1459 r0
[7] -= r3
[7] * m0
;
1461 /* Back substitute row 1. */
1464 r1
[4] = s
* (r1
[4] - r2
[4] * m1
);
1465 r1
[5] = s
* (r1
[5] - r2
[5] * m1
);
1466 r1
[6] = s
* (r1
[6] - r2
[6] * m1
);
1467 r1
[7] = s
* (r1
[7] - r2
[7] * m1
);
1469 r0
[4] -= r2
[4] * m0
;
1470 r0
[5] -= r2
[5] * m0
;
1471 r0
[6] -= r2
[6] * m0
;
1472 r0
[7] -= r2
[7] * m0
;
1474 /* Back substitute row 0. */
1477 r0
[4] = s
* (r0
[4] - r1
[4] * m0
);
1478 r0
[5] = s
* (r0
[5] - r1
[5] * m0
);
1479 r0
[6] = s
* (r0
[6] - r1
[6] * m0
);
1480 r0
[7] = s
* (r0
[7] - r1
[7] * m0
);
1502 static void shader_glsl_ffp_vertex_normalmatrix_uniform(const struct wined3d_context
*context
,
1503 const struct wined3d_state
*state
, struct glsl_shader_prog_link
*prog
)
1505 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1507 struct wined3d_matrix mv
;
1510 if (prog
->vs
.normal_matrix_location
== -1)
1513 get_modelview_matrix(context
, state
, 0, &mv
);
1514 if (context
->d3d_info
->wined3d_creation_flags
& WINED3D_LEGACY_FFP_LIGHTING
)
1515 invert_matrix_3d(&mv
, &mv
);
1517 invert_matrix(&mv
, &mv
);
1518 /* Tests show that singular modelview matrices are used unchanged as normal
1519 * matrices on D3D3 and older. There seems to be no clearly consistent
1520 * behavior on newer D3D versions so always follow older ddraw behavior. */
1521 for (i
= 0; i
< 3; ++i
)
1522 for (j
= 0; j
< 3; ++j
)
1523 mat
[i
* 3 + j
] = (&mv
._11
)[j
* 4 + i
];
1525 GL_EXTCALL(glUniformMatrix3fv(prog
->vs
.normal_matrix_location
, 1, FALSE
, mat
));
1526 checkGLcall("glUniformMatrix3fv");
1529 static void shader_glsl_ffp_vertex_texmatrix_uniform(const struct wined3d_context
*context
,
1530 const struct wined3d_state
*state
, unsigned int tex
, struct glsl_shader_prog_link
*prog
)
1532 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1533 struct wined3d_matrix mat
;
1535 if (tex
>= MAX_TEXTURES
)
1537 if (prog
->vs
.texture_matrix_location
[tex
] == -1)
1540 get_texture_matrix(context
, state
, tex
, &mat
);
1541 GL_EXTCALL(glUniformMatrix4fv(prog
->vs
.texture_matrix_location
[tex
], 1, FALSE
, &mat
._11
));
1542 checkGLcall("glUniformMatrix4fv");
1545 static void shader_glsl_ffp_vertex_material_uniform(const struct wined3d_context
*context
,
1546 const struct wined3d_state
*state
, struct glsl_shader_prog_link
*prog
)
1548 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1550 if (state
->render_states
[WINED3D_RS_SPECULARENABLE
])
1552 GL_EXTCALL(glUniform4fv(prog
->vs
.material_specular_location
, 1, &state
->material
.specular
.r
));
1553 GL_EXTCALL(glUniform1f(prog
->vs
.material_shininess_location
, state
->material
.power
));
1557 static const float black
[] = {0.0f
, 0.0f
, 0.0f
, 0.0f
};
1559 GL_EXTCALL(glUniform4fv(prog
->vs
.material_specular_location
, 1, black
));
1561 GL_EXTCALL(glUniform4fv(prog
->vs
.material_ambient_location
, 1, &state
->material
.ambient
.r
));
1562 GL_EXTCALL(glUniform4fv(prog
->vs
.material_diffuse_location
, 1, &state
->material
.diffuse
.r
));
1563 GL_EXTCALL(glUniform4fv(prog
->vs
.material_emissive_location
, 1, &state
->material
.emissive
.r
));
1564 checkGLcall("setting FFP material uniforms");
1567 static void shader_glsl_ffp_vertex_lightambient_uniform(const struct wined3d_context
*context
,
1568 const struct wined3d_state
*state
, struct glsl_shader_prog_link
*prog
)
1570 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1571 struct wined3d_color color
;
1573 wined3d_color_from_d3dcolor(&color
, state
->render_states
[WINED3D_RS_AMBIENT
]);
1574 GL_EXTCALL(glUniform3fv(prog
->vs
.light_ambient_location
, 1, &color
.r
));
1575 checkGLcall("glUniform3fv");
1578 static void multiply_vector_matrix(struct wined3d_vec4
*dest
, const struct wined3d_vec4
*src1
,
1579 const struct wined3d_matrix
*src2
)
1581 struct wined3d_vec4 temp
;
1583 temp
.x
= (src1
->x
* src2
->_11
) + (src1
->y
* src2
->_21
) + (src1
->z
* src2
->_31
) + (src1
->w
* src2
->_41
);
1584 temp
.y
= (src1
->x
* src2
->_12
) + (src1
->y
* src2
->_22
) + (src1
->z
* src2
->_32
) + (src1
->w
* src2
->_42
);
1585 temp
.z
= (src1
->x
* src2
->_13
) + (src1
->y
* src2
->_23
) + (src1
->z
* src2
->_33
) + (src1
->w
* src2
->_43
);
1586 temp
.w
= (src1
->x
* src2
->_14
) + (src1
->y
* src2
->_24
) + (src1
->z
* src2
->_34
) + (src1
->w
* src2
->_44
);
1591 static void shader_glsl_ffp_vertex_light_uniform(const struct wined3d_context
*context
,
1592 const struct wined3d_state
*state
, unsigned int light
, const struct wined3d_light_info
*light_info
,
1593 struct glsl_shader_prog_link
*prog
)
1595 const struct wined3d_matrix
*view
= &state
->transforms
[WINED3D_TS_VIEW
];
1596 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1597 struct wined3d_vec4 vec4
;
1599 GL_EXTCALL(glUniform4fv(prog
->vs
.light_location
[light
].diffuse
, 1, &light_info
->OriginalParms
.diffuse
.r
));
1600 GL_EXTCALL(glUniform4fv(prog
->vs
.light_location
[light
].specular
, 1, &light_info
->OriginalParms
.specular
.r
));
1601 GL_EXTCALL(glUniform4fv(prog
->vs
.light_location
[light
].ambient
, 1, &light_info
->OriginalParms
.ambient
.r
));
1603 switch (light_info
->OriginalParms
.type
)
1605 case WINED3D_LIGHT_POINT
:
1606 multiply_vector_matrix(&vec4
, &light_info
->position
, view
);
1607 GL_EXTCALL(glUniform4fv(prog
->vs
.light_location
[light
].position
, 1, &vec4
.x
));
1608 GL_EXTCALL(glUniform1f(prog
->vs
.light_location
[light
].range
, light_info
->OriginalParms
.range
));
1609 GL_EXTCALL(glUniform1f(prog
->vs
.light_location
[light
].c_att
, light_info
->OriginalParms
.attenuation0
));
1610 GL_EXTCALL(glUniform1f(prog
->vs
.light_location
[light
].l_att
, light_info
->OriginalParms
.attenuation1
));
1611 GL_EXTCALL(glUniform1f(prog
->vs
.light_location
[light
].q_att
, light_info
->OriginalParms
.attenuation2
));
1614 case WINED3D_LIGHT_SPOT
:
1615 multiply_vector_matrix(&vec4
, &light_info
->position
, view
);
1616 GL_EXTCALL(glUniform4fv(prog
->vs
.light_location
[light
].position
, 1, &vec4
.x
));
1618 multiply_vector_matrix(&vec4
, &light_info
->direction
, view
);
1619 GL_EXTCALL(glUniform3fv(prog
->vs
.light_location
[light
].direction
, 1, &vec4
.x
));
1621 GL_EXTCALL(glUniform1f(prog
->vs
.light_location
[light
].range
, light_info
->OriginalParms
.range
));
1622 GL_EXTCALL(glUniform1f(prog
->vs
.light_location
[light
].falloff
, light_info
->OriginalParms
.falloff
));
1623 GL_EXTCALL(glUniform1f(prog
->vs
.light_location
[light
].c_att
, light_info
->OriginalParms
.attenuation0
));
1624 GL_EXTCALL(glUniform1f(prog
->vs
.light_location
[light
].l_att
, light_info
->OriginalParms
.attenuation1
));
1625 GL_EXTCALL(glUniform1f(prog
->vs
.light_location
[light
].q_att
, light_info
->OriginalParms
.attenuation2
));
1626 GL_EXTCALL(glUniform1f(prog
->vs
.light_location
[light
].cos_htheta
, cosf(light_info
->OriginalParms
.theta
/ 2.0f
)));
1627 GL_EXTCALL(glUniform1f(prog
->vs
.light_location
[light
].cos_hphi
, cosf(light_info
->OriginalParms
.phi
/ 2.0f
)));
1630 case WINED3D_LIGHT_DIRECTIONAL
:
1631 multiply_vector_matrix(&vec4
, &light_info
->direction
, view
);
1632 GL_EXTCALL(glUniform3fv(prog
->vs
.light_location
[light
].direction
, 1, &vec4
.x
));
1635 case WINED3D_LIGHT_PARALLELPOINT
:
1636 multiply_vector_matrix(&vec4
, &light_info
->position
, view
);
1637 GL_EXTCALL(glUniform4fv(prog
->vs
.light_location
[light
].position
, 1, &vec4
.x
));
1641 FIXME("Unrecognized light type %#x.\n", light_info
->OriginalParms
.type
);
1643 checkGLcall("setting FFP lights uniforms");
1646 static void shader_glsl_pointsize_uniform(const struct wined3d_context
*context
,
1647 const struct wined3d_state
*state
, struct glsl_shader_prog_link
*prog
)
1649 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1653 get_pointsize_minmax(context
, state
, &min
, &max
);
1655 GL_EXTCALL(glUniform1f(prog
->vs
.pointsize_min_location
, min
));
1656 checkGLcall("glUniform1f");
1657 GL_EXTCALL(glUniform1f(prog
->vs
.pointsize_max_location
, max
));
1658 checkGLcall("glUniform1f");
1660 get_pointsize(context
, state
, &size
, att
);
1662 GL_EXTCALL(glUniform1f(prog
->vs
.pointsize_location
, size
));
1663 checkGLcall("glUniform1f");
1664 GL_EXTCALL(glUniform1f(prog
->vs
.pointsize_c_att_location
, att
[0]));
1665 checkGLcall("glUniform1f");
1666 GL_EXTCALL(glUniform1f(prog
->vs
.pointsize_l_att_location
, att
[1]));
1667 checkGLcall("glUniform1f");
1668 GL_EXTCALL(glUniform1f(prog
->vs
.pointsize_q_att_location
, att
[2]));
1669 checkGLcall("glUniform1f");
1672 static void shader_glsl_load_fog_uniform(const struct wined3d_context
*context
,
1673 const struct wined3d_state
*state
, struct glsl_shader_prog_link
*prog
)
1675 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1676 struct wined3d_color color
;
1677 float start
, end
, scale
;
1684 wined3d_color_from_d3dcolor(&color
, state
->render_states
[WINED3D_RS_FOGCOLOR
]);
1685 GL_EXTCALL(glUniform4fv(prog
->ps
.fog_color_location
, 1, &color
.r
));
1686 tmpvalue
.d
= state
->render_states
[WINED3D_RS_FOGDENSITY
];
1687 GL_EXTCALL(glUniform1f(prog
->ps
.fog_density_location
, tmpvalue
.f
));
1688 get_fog_start_end(context
, state
, &start
, &end
);
1689 scale
= 1.0f
/ (end
- start
);
1690 GL_EXTCALL(glUniform1f(prog
->ps
.fog_end_location
, end
));
1691 GL_EXTCALL(glUniform1f(prog
->ps
.fog_scale_location
, scale
));
1692 checkGLcall("fog emulation uniforms");
1695 static void shader_glsl_clip_plane_uniform(const struct wined3d_context
*context
,
1696 const struct wined3d_state
*state
, unsigned int index
, struct glsl_shader_prog_link
*prog
)
1698 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1699 struct wined3d_vec4 plane
;
1701 /* Clip planes are affected by the view transform in d3d for FFP draws. */
1703 multiply_vector_matrix(&plane
, &state
->clip_planes
[index
], &state
->transforms
[WINED3D_TS_VIEW
]);
1705 plane
= state
->clip_planes
[index
];
1707 GL_EXTCALL(glUniform4fv(prog
->vs
.clip_planes_location
+ index
, 1, &plane
.x
));
1710 /* Context activation is done by the caller (state handler). */
1711 static void shader_glsl_load_color_key_constant(const struct glsl_ps_program
*ps
,
1712 const struct wined3d_gl_info
*gl_info
, const struct wined3d_state
*state
)
1714 struct wined3d_color float_key
[2];
1715 const struct wined3d_texture
*texture
= state
->textures
[0];
1717 wined3d_format_get_float_color_key(texture
->resource
.format
, &texture
->async
.src_blt_color_key
, float_key
);
1718 GL_EXTCALL(glUniform4fv(ps
->color_key_location
, 2, &float_key
[0].r
));
1721 /* Context activation is done by the caller (state handler). */
1722 static void shader_glsl_load_constants(void *shader_priv
, struct wined3d_context
*context
,
1723 const struct wined3d_state
*state
)
1725 const struct glsl_context_data
*ctx_data
= context
->shader_backend_data
;
1726 const struct wined3d_shader
*vshader
= state
->shader
[WINED3D_SHADER_TYPE_VERTEX
];
1727 const struct wined3d_shader
*pshader
= state
->shader
[WINED3D_SHADER_TYPE_PIXEL
];
1728 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1729 struct shader_glsl_priv
*priv
= shader_priv
;
1730 float position_fixup
[4];
1733 struct glsl_shader_prog_link
*prog
= ctx_data
->glsl_program
;
1734 UINT constant_version
;
1738 /* No GLSL program set - nothing to do. */
1741 constant_version
= prog
->constant_version
;
1742 update_mask
= context
->constant_update_mask
& prog
->constant_update_mask
;
1744 if (update_mask
& WINED3D_SHADER_CONST_VS_F
)
1745 shader_glsl_load_constants_f(vshader
, gl_info
, state
->vs_consts_f
,
1746 prog
->vs
.uniform_f_locations
, &priv
->vconst_heap
, priv
->stack
, constant_version
);
1748 if (update_mask
& WINED3D_SHADER_CONST_VS_I
)
1749 shader_glsl_load_constants_i(vshader
, gl_info
, state
->vs_consts_i
,
1750 prog
->vs
.uniform_i_locations
, vshader
->reg_maps
.integer_constants
);
1752 if (update_mask
& WINED3D_SHADER_CONST_VS_B
)
1753 shader_glsl_load_constantsB(vshader
, gl_info
, prog
->vs
.uniform_b_locations
, state
->vs_consts_b
,
1754 vshader
->reg_maps
.boolean_constants
);
1756 if (update_mask
& WINED3D_SHADER_CONST_VS_CLIP_PLANES
)
1758 for (i
= 0; i
< gl_info
->limits
.user_clip_distances
; ++i
)
1759 shader_glsl_clip_plane_uniform(context
, state
, i
, prog
);
1762 if (update_mask
& WINED3D_SHADER_CONST_VS_POINTSIZE
)
1763 shader_glsl_pointsize_uniform(context
, state
, prog
);
1765 if (update_mask
& WINED3D_SHADER_CONST_POS_FIXUP
)
1767 shader_get_position_fixup(context
, state
, position_fixup
);
1768 if (state
->shader
[WINED3D_SHADER_TYPE_GEOMETRY
])
1769 GL_EXTCALL(glUniform4fv(prog
->gs
.pos_fixup_location
, 1, position_fixup
));
1770 else if (state
->shader
[WINED3D_SHADER_TYPE_DOMAIN
])
1771 GL_EXTCALL(glUniform4fv(prog
->ds
.pos_fixup_location
, 1, position_fixup
));
1773 GL_EXTCALL(glUniform4fv(prog
->vs
.pos_fixup_location
, 1, position_fixup
));
1774 checkGLcall("glUniform4fv");
1777 if (update_mask
& WINED3D_SHADER_CONST_FFP_MODELVIEW
)
1779 struct wined3d_matrix mat
;
1781 get_modelview_matrix(context
, state
, 0, &mat
);
1782 GL_EXTCALL(glUniformMatrix4fv(prog
->vs
.modelview_matrix_location
[0], 1, FALSE
, &mat
._11
));
1783 checkGLcall("glUniformMatrix4fv");
1785 shader_glsl_ffp_vertex_normalmatrix_uniform(context
, state
, prog
);
1788 if (update_mask
& WINED3D_SHADER_CONST_FFP_VERTEXBLEND
)
1790 struct wined3d_matrix mat
;
1792 for (i
= 1; i
< MAX_VERTEX_BLENDS
; ++i
)
1794 if (prog
->vs
.modelview_matrix_location
[i
] == -1)
1797 get_modelview_matrix(context
, state
, i
, &mat
);
1798 GL_EXTCALL(glUniformMatrix4fv(prog
->vs
.modelview_matrix_location
[i
], 1, FALSE
, &mat
._11
));
1799 checkGLcall("glUniformMatrix4fv");
1803 if (update_mask
& WINED3D_SHADER_CONST_FFP_PROJ
)
1805 struct wined3d_matrix projection
;
1807 get_projection_matrix(context
, state
, &projection
);
1808 GL_EXTCALL(glUniformMatrix4fv(prog
->vs
.projection_matrix_location
, 1, FALSE
, &projection
._11
));
1809 checkGLcall("glUniformMatrix4fv");
1812 if (update_mask
& WINED3D_SHADER_CONST_FFP_TEXMATRIX
)
1814 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
1815 shader_glsl_ffp_vertex_texmatrix_uniform(context
, state
, i
, prog
);
1818 if (update_mask
& WINED3D_SHADER_CONST_FFP_MATERIAL
)
1819 shader_glsl_ffp_vertex_material_uniform(context
, state
, prog
);
1821 if (update_mask
& WINED3D_SHADER_CONST_FFP_LIGHTS
)
1823 unsigned int point_idx
, spot_idx
, directional_idx
, parallel_point_idx
;
1824 DWORD point_count
= 0;
1825 DWORD spot_count
= 0;
1826 DWORD directional_count
= 0;
1827 DWORD parallel_point_count
= 0;
1829 for (i
= 0; i
< MAX_ACTIVE_LIGHTS
; ++i
)
1831 if (!state
->lights
[i
])
1834 switch (state
->lights
[i
]->OriginalParms
.type
)
1836 case WINED3D_LIGHT_POINT
:
1839 case WINED3D_LIGHT_SPOT
:
1842 case WINED3D_LIGHT_DIRECTIONAL
:
1843 ++directional_count
;
1845 case WINED3D_LIGHT_PARALLELPOINT
:
1846 ++parallel_point_count
;
1849 FIXME("Unhandled light type %#x.\n", state
->lights
[i
]->OriginalParms
.type
);
1854 spot_idx
= point_idx
+ point_count
;
1855 directional_idx
= spot_idx
+ spot_count
;
1856 parallel_point_idx
= directional_idx
+ directional_count
;
1858 shader_glsl_ffp_vertex_lightambient_uniform(context
, state
, prog
);
1859 for (i
= 0; i
< MAX_ACTIVE_LIGHTS
; ++i
)
1861 const struct wined3d_light_info
*light_info
= state
->lights
[i
];
1867 switch (light_info
->OriginalParms
.type
)
1869 case WINED3D_LIGHT_POINT
:
1872 case WINED3D_LIGHT_SPOT
:
1875 case WINED3D_LIGHT_DIRECTIONAL
:
1876 idx
= directional_idx
++;
1878 case WINED3D_LIGHT_PARALLELPOINT
:
1879 idx
= parallel_point_idx
++;
1882 FIXME("Unhandled light type %#x.\n", light_info
->OriginalParms
.type
);
1885 shader_glsl_ffp_vertex_light_uniform(context
, state
, idx
, light_info
, prog
);
1889 if (update_mask
& WINED3D_SHADER_CONST_PS_F
)
1890 shader_glsl_load_constants_f(pshader
, gl_info
, state
->ps_consts_f
,
1891 prog
->ps
.uniform_f_locations
, &priv
->pconst_heap
, priv
->stack
, constant_version
);
1893 if (update_mask
& WINED3D_SHADER_CONST_PS_I
)
1894 shader_glsl_load_constants_i(pshader
, gl_info
, state
->ps_consts_i
,
1895 prog
->ps
.uniform_i_locations
, pshader
->reg_maps
.integer_constants
);
1897 if (update_mask
& WINED3D_SHADER_CONST_PS_B
)
1898 shader_glsl_load_constantsB(pshader
, gl_info
, prog
->ps
.uniform_b_locations
, state
->ps_consts_b
,
1899 pshader
->reg_maps
.boolean_constants
);
1901 if (update_mask
& WINED3D_SHADER_CONST_PS_BUMP_ENV
)
1903 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
1905 if (prog
->ps
.bumpenv_mat_location
[i
] == -1)
1908 GL_EXTCALL(glUniformMatrix2fv(prog
->ps
.bumpenv_mat_location
[i
], 1, 0,
1909 (const GLfloat
*)&state
->texture_states
[i
][WINED3D_TSS_BUMPENV_MAT00
]));
1911 if (prog
->ps
.bumpenv_lum_scale_location
[i
] != -1)
1913 GL_EXTCALL(glUniform1fv(prog
->ps
.bumpenv_lum_scale_location
[i
], 1,
1914 (const GLfloat
*)&state
->texture_states
[i
][WINED3D_TSS_BUMPENV_LSCALE
]));
1915 GL_EXTCALL(glUniform1fv(prog
->ps
.bumpenv_lum_offset_location
[i
], 1,
1916 (const GLfloat
*)&state
->texture_states
[i
][WINED3D_TSS_BUMPENV_LOFFSET
]));
1920 checkGLcall("bump env uniforms");
1923 if (update_mask
& WINED3D_SHADER_CONST_PS_Y_CORR
)
1925 const struct wined3d_vec4 correction_params
=
1927 /* Position is relative to the framebuffer, not the viewport. */
1928 context
->render_offscreen
? 0.0f
: (float)state
->fb
->render_targets
[0]->height
,
1929 context
->render_offscreen
? 1.0f
: -1.0f
,
1934 GL_EXTCALL(glUniform4fv(prog
->ps
.ycorrection_location
, 1, &correction_params
.x
));
1937 if (update_mask
& WINED3D_SHADER_CONST_PS_NP2_FIXUP
)
1938 shader_glsl_load_np2fixup_constants(&prog
->ps
, gl_info
, state
);
1939 if (update_mask
& WINED3D_SHADER_CONST_FFP_COLOR_KEY
)
1940 shader_glsl_load_color_key_constant(&prog
->ps
, gl_info
, state
);
1942 if (update_mask
& WINED3D_SHADER_CONST_FFP_PS
)
1944 struct wined3d_color color
;
1946 if (prog
->ps
.tex_factor_location
!= -1)
1948 wined3d_color_from_d3dcolor(&color
, state
->render_states
[WINED3D_RS_TEXTUREFACTOR
]);
1949 GL_EXTCALL(glUniform4fv(prog
->ps
.tex_factor_location
, 1, &color
.r
));
1952 if (state
->render_states
[WINED3D_RS_SPECULARENABLE
])
1953 GL_EXTCALL(glUniform4f(prog
->ps
.specular_enable_location
, 1.0f
, 1.0f
, 1.0f
, 0.0f
));
1955 GL_EXTCALL(glUniform4f(prog
->ps
.specular_enable_location
, 0.0f
, 0.0f
, 0.0f
, 0.0f
));
1957 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
1959 if (prog
->ps
.tss_constant_location
[i
] == -1)
1962 wined3d_color_from_d3dcolor(&color
, state
->texture_states
[i
][WINED3D_TSS_CONSTANT
]);
1963 GL_EXTCALL(glUniform4fv(prog
->ps
.tss_constant_location
[i
], 1, &color
.r
));
1966 checkGLcall("fixed function uniforms");
1969 if (update_mask
& WINED3D_SHADER_CONST_PS_FOG
)
1970 shader_glsl_load_fog_uniform(context
, state
, prog
);
1972 if (update_mask
& WINED3D_SHADER_CONST_PS_ALPHA_TEST
)
1974 float ref
= state
->render_states
[WINED3D_RS_ALPHAREF
] / 255.0f
;
1976 GL_EXTCALL(glUniform1f(prog
->ps
.alpha_test_ref_location
, ref
));
1977 checkGLcall("alpha test emulation uniform");
1980 if (priv
->next_constant_version
== UINT_MAX
)
1982 TRACE("Max constant version reached, resetting to 0.\n");
1983 wine_rb_for_each_entry(&priv
->program_lookup
, reset_program_constant_version
, NULL
);
1984 priv
->next_constant_version
= 1;
1988 prog
->constant_version
= priv
->next_constant_version
++;
1992 static void update_heap_entry(struct constant_heap
*heap
, unsigned int idx
, DWORD new_version
)
1994 struct constant_entry
*entries
= heap
->entries
;
1995 unsigned int *positions
= heap
->positions
;
1996 unsigned int heap_idx
, parent_idx
;
1998 if (!heap
->contained
[idx
])
2000 heap_idx
= heap
->size
++;
2001 heap
->contained
[idx
] = TRUE
;
2005 heap_idx
= positions
[idx
];
2008 while (heap_idx
> 1)
2010 parent_idx
= heap_idx
>> 1;
2012 if (new_version
<= entries
[parent_idx
].version
) break;
2014 entries
[heap_idx
] = entries
[parent_idx
];
2015 positions
[entries
[parent_idx
].idx
] = heap_idx
;
2016 heap_idx
= parent_idx
;
2019 entries
[heap_idx
].version
= new_version
;
2020 entries
[heap_idx
].idx
= idx
;
2021 positions
[idx
] = heap_idx
;
2024 static void shader_glsl_update_float_vertex_constants(struct wined3d_device
*device
, UINT start
, UINT count
)
2026 struct shader_glsl_priv
*priv
= device
->shader_priv
;
2027 struct constant_heap
*heap
= &priv
->vconst_heap
;
2030 for (i
= start
; i
< count
+ start
; ++i
)
2032 update_heap_entry(heap
, i
, priv
->next_constant_version
);
2036 static void shader_glsl_update_float_pixel_constants(struct wined3d_device
*device
, UINT start
, UINT count
)
2038 struct shader_glsl_priv
*priv
= device
->shader_priv
;
2039 struct constant_heap
*heap
= &priv
->pconst_heap
;
2042 for (i
= start
; i
< count
+ start
; ++i
)
2044 update_heap_entry(heap
, i
, priv
->next_constant_version
);
2048 static unsigned int vec4_varyings(DWORD shader_major
, const struct wined3d_gl_info
*gl_info
)
2050 unsigned int ret
= gl_info
->limits
.glsl_varyings
/ 4;
2051 /* 4.0 shaders do not write clip coords because d3d10 does not support user clipplanes */
2052 if(shader_major
> 3) return ret
;
2054 /* 3.0 shaders may need an extra varying for the clip coord on some cards(mostly dx10 ones) */
2055 if (gl_info
->quirks
& WINED3D_QUIRK_GLSL_CLIP_VARYING
) ret
-= 1;
2059 static BOOL
needs_legacy_glsl_syntax(const struct wined3d_gl_info
*gl_info
)
2061 return gl_info
->glsl_version
< MAKEDWORD_VERSION(1, 30);
2064 static BOOL
shader_glsl_use_explicit_attrib_location(const struct wined3d_gl_info
*gl_info
)
2066 return gl_info
->supported
[ARB_EXPLICIT_ATTRIB_LOCATION
]
2067 && shader_glsl_use_layout_qualifier(gl_info
) && !needs_legacy_glsl_syntax(gl_info
);
2070 static BOOL
shader_glsl_use_interface_blocks(const struct wined3d_gl_info
*gl_info
)
2072 return shader_glsl_get_version(gl_info
) >= 150;
2075 static const char *get_attribute_keyword(const struct wined3d_gl_info
*gl_info
)
2077 return needs_legacy_glsl_syntax(gl_info
) ? "attribute" : "in";
2080 static void PRINTF_ATTR(4, 5) declare_in_varying(const struct wined3d_gl_info
*gl_info
,
2081 struct wined3d_string_buffer
*buffer
, BOOL flat
, const char *format
, ...)
2086 shader_addline(buffer
, "%s%s ", flat
? "flat " : "",
2087 needs_legacy_glsl_syntax(gl_info
) ? "varying" : "in");
2090 va_start(args
, format
);
2091 ret
= shader_vaddline(buffer
, format
, args
);
2095 if (!string_buffer_resize(buffer
, ret
))
2100 static void PRINTF_ATTR(4, 5) declare_out_varying(const struct wined3d_gl_info
*gl_info
,
2101 struct wined3d_string_buffer
*buffer
, BOOL flat
, const char *format
, ...)
2106 shader_addline(buffer
, "%s%s ", flat
? "flat " : "",
2107 needs_legacy_glsl_syntax(gl_info
) ? "varying" : "out");
2110 va_start(args
, format
);
2111 ret
= shader_vaddline(buffer
, format
, args
);
2115 if (!string_buffer_resize(buffer
, ret
))
2120 static const char *shader_glsl_shader_input_name(const struct wined3d_gl_info
*gl_info
)
2122 return shader_glsl_use_interface_blocks(gl_info
) ? "shader_in.reg" : "ps_link";
2125 static const char *shader_glsl_shader_output_name(const struct wined3d_gl_info
*gl_info
)
2127 return shader_glsl_use_interface_blocks(gl_info
) ? "shader_out.reg" : "ps_link";
2130 static const char *shader_glsl_interpolation_qualifiers(enum wined3d_shader_interpolation_mode mode
)
2134 case WINED3DSIM_CONSTANT
:
2136 case WINED3DSIM_LINEAR_NOPERSPECTIVE
:
2137 return "noperspective";
2139 FIXME("Unhandled interpolation mode %#x.\n", mode
);
2140 case WINED3DSIM_NONE
:
2141 case WINED3DSIM_LINEAR
:
2146 static enum wined3d_shader_interpolation_mode
wined3d_extract_interpolation_mode(
2147 const DWORD
*packed_interpolation_mode
, unsigned int register_idx
)
2149 return wined3d_extract_bits(packed_interpolation_mode
,
2150 register_idx
* WINED3D_PACKED_INTERPOLATION_BIT_COUNT
, WINED3D_PACKED_INTERPOLATION_BIT_COUNT
);
2153 static void shader_glsl_declare_shader_inputs(const struct wined3d_gl_info
*gl_info
,
2154 struct wined3d_string_buffer
*buffer
, unsigned int element_count
,
2155 const DWORD
*interpolation_mode
, BOOL unroll
)
2157 enum wined3d_shader_interpolation_mode mode
;
2160 if (shader_glsl_use_interface_blocks(gl_info
))
2164 shader_addline(buffer
, "in shader_in_out {\n");
2165 for (i
= 0; i
< element_count
; ++i
)
2167 mode
= wined3d_extract_interpolation_mode(interpolation_mode
, i
);
2168 shader_addline(buffer
, "%s vec4 reg%u;\n", shader_glsl_interpolation_qualifiers(mode
), i
);
2170 shader_addline(buffer
, "} shader_in;\n");
2174 shader_addline(buffer
, "in shader_in_out { vec4 reg[%u]; } shader_in;\n", element_count
);
2179 declare_in_varying(gl_info
, buffer
, FALSE
, "vec4 ps_link[%u];\n", element_count
);
2183 static void shader_glsl_declare_shader_outputs(const struct wined3d_gl_info
*gl_info
,
2184 struct wined3d_string_buffer
*buffer
, unsigned int element_count
, BOOL rasterizer_setup
,
2185 const DWORD
*interpolation_mode
)
2187 enum wined3d_shader_interpolation_mode mode
;
2190 if (shader_glsl_use_interface_blocks(gl_info
))
2192 if (rasterizer_setup
)
2194 shader_addline(buffer
, "out shader_in_out {\n");
2195 for (i
= 0; i
< element_count
; ++i
)
2197 const char *interpolation_qualifiers
= "";
2198 if (needs_interpolation_qualifiers_for_shader_outputs(gl_info
))
2200 mode
= wined3d_extract_interpolation_mode(interpolation_mode
, i
);
2201 interpolation_qualifiers
= shader_glsl_interpolation_qualifiers(mode
);
2203 shader_addline(buffer
, "%s vec4 reg%u;\n", interpolation_qualifiers
, i
);
2205 shader_addline(buffer
, "} shader_out;\n");
2209 shader_addline(buffer
, "out shader_in_out { vec4 reg[%u]; } shader_out;\n", element_count
);
2214 declare_out_varying(gl_info
, buffer
, FALSE
, "vec4 ps_link[%u];\n", element_count
);
2218 static const char *get_fragment_output(const struct wined3d_gl_info
*gl_info
)
2220 return needs_legacy_glsl_syntax(gl_info
) ? "gl_FragData" : "ps_out";
2223 static const char *glsl_primitive_type_from_d3d(enum wined3d_primitive_type primitive_type
)
2225 switch (primitive_type
)
2227 case WINED3D_PT_POINTLIST
:
2230 case WINED3D_PT_LINELIST
:
2233 case WINED3D_PT_LINESTRIP
:
2234 return "line_strip";
2236 case WINED3D_PT_TRIANGLELIST
:
2239 case WINED3D_PT_TRIANGLESTRIP
:
2240 return "triangle_strip";
2242 case WINED3D_PT_LINELIST_ADJ
:
2243 return "lines_adjacency";
2245 case WINED3D_PT_TRIANGLELIST_ADJ
:
2246 return "triangles_adjacency";
2249 FIXME("Unhandled primitive type %s.\n", debug_d3dprimitivetype(primitive_type
));
2254 static BOOL
glsl_is_color_reg_read(const struct wined3d_shader
*shader
, unsigned int idx
)
2256 const struct wined3d_shader_signature
*input_signature
= &shader
->input_signature
;
2257 const struct wined3d_shader_reg_maps
*reg_maps
= &shader
->reg_maps
;
2258 DWORD input_reg_used
= shader
->u
.ps
.input_reg_used
;
2261 if (reg_maps
->shader_version
.major
< 3)
2262 return input_reg_used
& (1u << idx
);
2264 for (i
= 0; i
< input_signature
->element_count
; ++i
)
2266 const struct wined3d_shader_signature_element
*input
= &input_signature
->elements
[i
];
2268 if (!(reg_maps
->input_registers
& (1u << input
->register_idx
)))
2271 if (shader_match_semantic(input
->semantic_name
, WINED3D_DECL_USAGE_COLOR
)
2272 && input
->semantic_idx
== idx
)
2273 return input_reg_used
& (1u << input
->register_idx
);
2278 static BOOL
glsl_is_shadow_sampler(const struct wined3d_shader
*shader
,
2279 const struct ps_compile_args
*ps_args
, unsigned int resource_idx
, unsigned int sampler_idx
)
2281 const struct wined3d_shader_version
*version
= &shader
->reg_maps
.shader_version
;
2283 if (version
->major
>= 4)
2284 return shader
->reg_maps
.sampler_comparison_mode
& (1u << sampler_idx
);
2286 return version
->type
== WINED3D_SHADER_TYPE_PIXEL
&& (ps_args
->shadow
& (1u << resource_idx
));
2289 static void shader_glsl_declare_typed_vertex_attribute(struct wined3d_string_buffer
*buffer
,
2290 const struct wined3d_gl_info
*gl_info
, const char *vector_type
, const char *scalar_type
,
2293 shader_addline(buffer
, "%s %s4 vs_in_%s%u;\n",
2294 get_attribute_keyword(gl_info
), vector_type
, scalar_type
, index
);
2295 shader_addline(buffer
, "vec4 vs_in%u = %sBitsToFloat(vs_in_%s%u);\n",
2296 index
, scalar_type
, scalar_type
, index
);
2299 static void shader_glsl_declare_generic_vertex_attribute(struct wined3d_string_buffer
*buffer
,
2300 const struct wined3d_gl_info
*gl_info
, const struct wined3d_shader_signature_element
*e
)
2302 unsigned int index
= e
->register_idx
;
2304 if (e
->sysval_semantic
== WINED3D_SV_VERTEX_ID
)
2306 shader_addline(buffer
, "vec4 vs_in%u = vec4(intBitsToFloat(gl_VertexID), 0.0, 0.0, 0.0);\n",
2310 if (e
->sysval_semantic
== WINED3D_SV_INSTANCE_ID
)
2312 shader_addline(buffer
, "vec4 vs_in%u = vec4(intBitsToFloat(gl_InstanceID), 0.0, 0.0, 0.0);\n",
2316 if (e
->sysval_semantic
&& e
->sysval_semantic
!= WINED3D_SV_POSITION
)
2317 FIXME("Unhandled sysval semantic %#x.\n", e
->sysval_semantic
);
2319 if (shader_glsl_use_explicit_attrib_location(gl_info
))
2320 shader_addline(buffer
, "layout(location = %u) ", index
);
2322 switch (e
->component_type
)
2324 case WINED3D_TYPE_UINT
:
2325 shader_glsl_declare_typed_vertex_attribute(buffer
, gl_info
, "uvec", "uint", index
);
2327 case WINED3D_TYPE_INT
:
2328 shader_glsl_declare_typed_vertex_attribute(buffer
, gl_info
, "ivec", "int", index
);
2332 FIXME("Unhandled type %#x.\n", e
->component_type
);
2334 case WINED3D_TYPE_UNKNOWN
:
2335 case WINED3D_TYPE_FLOAT
:
2336 shader_addline(buffer
, "%s vec4 vs_in%u;\n", get_attribute_keyword(gl_info
), index
);
2341 /** Generate the variable & register declarations for the GLSL output target */
2342 static void shader_generate_glsl_declarations(const struct wined3d_context
*context
,
2343 struct wined3d_string_buffer
*buffer
, const struct wined3d_shader
*shader
,
2344 const struct wined3d_shader_reg_maps
*reg_maps
, const struct shader_glsl_ctx_priv
*ctx_priv
)
2346 const struct wined3d_shader_version
*version
= ®_maps
->shader_version
;
2347 const struct vs_compile_args
*vs_args
= ctx_priv
->cur_vs_args
;
2348 const struct ps_compile_args
*ps_args
= ctx_priv
->cur_ps_args
;
2349 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2350 const struct wined3d_shader_indexable_temp
*idx_temp_reg
;
2351 unsigned int uniform_block_base
, uniform_block_count
;
2352 const struct wined3d_shader_lconst
*lconst
;
2357 prefix
= shader_glsl_get_prefix(version
->type
);
2359 /* Prototype the subroutines */
2360 for (i
= 0, map
= reg_maps
->labels
; map
; map
>>= 1, ++i
)
2362 if (map
& 1) shader_addline(buffer
, "void subroutine%u();\n", i
);
2365 /* Declare the constants (aka uniforms) */
2366 if (shader
->limits
->constant_float
> 0)
2368 unsigned max_constantsF
;
2370 /* Unless the shader uses indirect addressing, always declare the
2371 * maximum array size and ignore that we need some uniforms privately.
2372 * E.g. if GL supports 256 uniforms, and we need 2 for the pos fixup
2373 * and immediate values, still declare VC[256]. If the shader needs
2374 * more uniforms than we have it won't work in any case. If it uses
2375 * less, the compiler will figure out which uniforms are really used
2376 * and strip them out. This allows a shader to use c255 on a dx9 card,
2377 * as long as it doesn't also use all the other constants.
2379 * If the shader uses indirect addressing the compiler must assume
2380 * that all declared uniforms are used. In this case, declare only the
2381 * amount that we're assured to have.
2383 * Thus we run into problems in these two cases:
2384 * 1) The shader really uses more uniforms than supported.
2385 * 2) The shader uses indirect addressing, less constants than
2386 * supported, but uses a constant index > #supported consts. */
2387 if (version
->type
== WINED3D_SHADER_TYPE_PIXEL
)
2389 /* No indirect addressing here. */
2390 max_constantsF
= gl_info
->limits
.glsl_ps_float_constants
;
2394 if (reg_maps
->usesrelconstF
)
2396 /* Subtract the other potential uniforms from the max
2397 * available (bools, ints, and 1 row of projection matrix).
2398 * Subtract another uniform for immediate values, which have
2399 * to be loaded via uniform by the driver as well. The shader
2400 * code only uses 0.5, 2.0, 1.0, 128 and -128 in vertex
2401 * shader code, so one vec4 should be enough. (Unfortunately
2402 * the Nvidia driver doesn't store 128 and -128 in one float).
2404 * Writing gl_ClipVertex requires one uniform for each
2405 * clipplane as well. */
2406 max_constantsF
= gl_info
->limits
.glsl_vs_float_constants
- 3;
2407 if (vs_args
->clip_enabled
)
2408 max_constantsF
-= gl_info
->limits
.user_clip_distances
;
2409 max_constantsF
-= wined3d_popcount(reg_maps
->integer_constants
);
2410 /* Strictly speaking a bool only uses one scalar, but the nvidia(Linux) compiler doesn't pack them properly,
2411 * so each scalar requires a full vec4. We could work around this by packing the booleans ourselves, but
2412 * for now take this into account when calculating the number of available constants
2414 max_constantsF
-= wined3d_popcount(reg_maps
->boolean_constants
);
2415 /* Set by driver quirks in directx.c */
2416 max_constantsF
-= gl_info
->reserved_glsl_constants
;
2418 if (max_constantsF
< shader
->limits
->constant_float
)
2420 static unsigned int once
;
2423 ERR_(winediag
)("The hardware does not support enough uniform components to run this shader,"
2424 " it may not render correctly.\n");
2426 WARN("The hardware does not support enough uniform components to run this shader.\n");
2431 max_constantsF
= gl_info
->limits
.glsl_vs_float_constants
;
2434 max_constantsF
= min(shader
->limits
->constant_float
, max_constantsF
);
2435 shader_addline(buffer
, "uniform vec4 %s_c[%u];\n", prefix
, max_constantsF
);
2438 /* Always declare the full set of constants, the compiler can remove the
2439 * unused ones because d3d doesn't (yet) support indirect int and bool
2440 * constant addressing. This avoids problems if the app uses e.g. i0 and i9. */
2441 if (shader
->limits
->constant_int
> 0 && reg_maps
->integer_constants
)
2442 shader_addline(buffer
, "uniform ivec4 %s_i[%u];\n", prefix
, shader
->limits
->constant_int
);
2444 if (shader
->limits
->constant_bool
> 0 && reg_maps
->boolean_constants
)
2445 shader_addline(buffer
, "uniform bool %s_b[%u];\n", prefix
, shader
->limits
->constant_bool
);
2447 /* Declare immediate constant buffer */
2449 shader_addline(buffer
, "uniform vec4 %s_icb[%u];\n", prefix
, reg_maps
->icb
->vec4_count
);
2451 /* Declare constant buffers */
2452 wined3d_gl_limits_get_uniform_block_range(&gl_info
->limits
, version
->type
,
2453 &uniform_block_base
, &uniform_block_count
);
2454 for (i
= 0; i
< min(uniform_block_count
, WINED3D_MAX_CBS
); ++i
)
2456 if (reg_maps
->cb_sizes
[i
])
2458 shader_addline(buffer
, "layout(std140");
2459 if (shader_glsl_use_layout_binding_qualifier(gl_info
))
2460 shader_addline(buffer
, ", binding = %u", uniform_block_base
+ i
);
2461 shader_addline(buffer
, ") uniform block_%s_cb%u { vec4 %s_cb%u[%u]; };\n",
2462 prefix
, i
, prefix
, i
, reg_maps
->cb_sizes
[i
]);
2466 /* Declare texture samplers */
2467 for (i
= 0; i
< reg_maps
->sampler_map
.count
; ++i
)
2469 struct wined3d_shader_sampler_map_entry
*entry
;
2470 const char *sampler_type_prefix
, *sampler_type
;
2471 BOOL shadow_sampler
, tex_rect
;
2473 entry
= ®_maps
->sampler_map
.entries
[i
];
2475 if (entry
->resource_idx
>= ARRAY_SIZE(reg_maps
->resource_info
))
2477 ERR("Invalid resource index %u.\n", entry
->resource_idx
);
2481 switch (reg_maps
->resource_info
[entry
->resource_idx
].data_type
)
2483 case WINED3D_DATA_FLOAT
:
2484 case WINED3D_DATA_UNORM
:
2485 case WINED3D_DATA_SNORM
:
2486 sampler_type_prefix
= "";
2489 case WINED3D_DATA_INT
:
2490 sampler_type_prefix
= "i";
2493 case WINED3D_DATA_UINT
:
2494 sampler_type_prefix
= "u";
2498 sampler_type_prefix
= "";
2499 ERR("Unhandled resource data type %#x.\n", reg_maps
->resource_info
[i
].data_type
);
2503 shadow_sampler
= glsl_is_shadow_sampler(shader
, ps_args
, entry
->resource_idx
, entry
->sampler_idx
);
2504 switch (reg_maps
->resource_info
[entry
->resource_idx
].type
)
2506 case WINED3D_SHADER_RESOURCE_BUFFER
:
2507 sampler_type
= "samplerBuffer";
2510 case WINED3D_SHADER_RESOURCE_TEXTURE_1D
:
2512 sampler_type
= "sampler1DShadow";
2514 sampler_type
= "sampler1D";
2517 case WINED3D_SHADER_RESOURCE_TEXTURE_2D
:
2518 tex_rect
= version
->type
== WINED3D_SHADER_TYPE_PIXEL
2519 && (ps_args
->np2_fixup
& (1u << entry
->resource_idx
))
2520 && gl_info
->supported
[ARB_TEXTURE_RECTANGLE
];
2524 sampler_type
= "sampler2DRectShadow";
2526 sampler_type
= "sampler2DShadow";
2531 sampler_type
= "sampler2DRect";
2533 sampler_type
= "sampler2D";
2537 case WINED3D_SHADER_RESOURCE_TEXTURE_3D
:
2539 FIXME("Unsupported 3D shadow sampler.\n");
2540 sampler_type
= "sampler3D";
2543 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE
:
2545 sampler_type
= "samplerCubeShadow";
2547 sampler_type
= "samplerCube";
2550 case WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY
:
2552 sampler_type
= "sampler2DArrayShadow";
2554 sampler_type
= "sampler2DArray";
2557 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBEARRAY
:
2559 sampler_type
= "samplerCubeArrayShadow";
2561 sampler_type
= "samplerCubeArray";
2565 sampler_type
= "unsupported_sampler";
2566 FIXME("Unhandled resource type %#x.\n", reg_maps
->resource_info
[entry
->resource_idx
].type
);
2570 if (shader_glsl_use_layout_binding_qualifier(gl_info
))
2571 shader_glsl_append_sampler_binding_qualifier(buffer
, context
, version
, entry
->bind_idx
);
2572 shader_addline(buffer
, "uniform %s%s %s_sampler%u;\n",
2573 sampler_type_prefix
, sampler_type
, prefix
, entry
->bind_idx
);
2576 /* Declare images */
2577 for (i
= 0; i
< ARRAY_SIZE(reg_maps
->uav_resource_info
); ++i
)
2579 const char *image_type_prefix
, *image_type
, *read_format
;
2581 if (!reg_maps
->uav_resource_info
[i
].type
)
2584 switch (reg_maps
->uav_resource_info
[i
].data_type
)
2586 case WINED3D_DATA_FLOAT
:
2587 case WINED3D_DATA_UNORM
:
2588 case WINED3D_DATA_SNORM
:
2589 image_type_prefix
= "";
2590 read_format
= "r32f";
2593 case WINED3D_DATA_INT
:
2594 image_type_prefix
= "i";
2595 read_format
= "r32i";
2598 case WINED3D_DATA_UINT
:
2599 image_type_prefix
= "u";
2600 read_format
= "r32ui";
2604 image_type_prefix
= "";
2606 ERR("Unhandled resource data type %#x.\n", reg_maps
->uav_resource_info
[i
].data_type
);
2610 switch (reg_maps
->uav_resource_info
[i
].type
)
2612 case WINED3D_SHADER_RESOURCE_BUFFER
:
2613 image_type
= "imageBuffer";
2616 case WINED3D_SHADER_RESOURCE_TEXTURE_2D
:
2617 image_type
= "image2D";
2620 case WINED3D_SHADER_RESOURCE_TEXTURE_3D
:
2621 image_type
= "image3D";
2624 case WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY
:
2625 image_type
= "image2DArray";
2629 image_type
= "unsupported_image";
2630 FIXME("Unhandled resource type %#x.\n", reg_maps
->uav_resource_info
[i
].type
);
2634 if (shader_glsl_use_layout_binding_qualifier(gl_info
))
2635 shader_addline(buffer
, "layout(binding = %u)\n", i
);
2636 if (reg_maps
->uav_read_mask
& (1u << i
))
2637 shader_addline(buffer
, "layout(%s) uniform %s%s %s_image%u;\n",
2638 read_format
, image_type_prefix
, image_type
, prefix
, i
);
2640 shader_addline(buffer
, "writeonly uniform %s%s %s_image%u;\n",
2641 image_type_prefix
, image_type
, prefix
, i
);
2643 if (reg_maps
->uav_counter_mask
& (1u << i
))
2644 shader_addline(buffer
, "layout(binding = %u) uniform atomic_uint %s_counter%u;\n",
2648 /* Declare address variables */
2649 for (i
= 0, map
= reg_maps
->address
; map
; map
>>= 1, ++i
)
2651 if (map
& 1) shader_addline(buffer
, "ivec4 A%u;\n", i
);
2654 /* Declare output register temporaries */
2655 if (shader
->limits
->packed_output
)
2656 shader_addline(buffer
, "vec4 %s_out[%u];\n", prefix
, shader
->limits
->packed_output
);
2658 /* Declare temporary variables */
2659 if (reg_maps
->temporary_count
)
2661 for (i
= 0; i
< reg_maps
->temporary_count
; ++i
)
2662 shader_addline(buffer
, "vec4 R%u;\n", i
);
2664 else if (version
->major
< 4)
2666 for (i
= 0, map
= reg_maps
->temporary
; map
; map
>>= 1, ++i
)
2669 shader_addline(buffer
, "vec4 R%u;\n", i
);
2673 /* Declare indexable temporary variables */
2674 LIST_FOR_EACH_ENTRY(idx_temp_reg
, ®_maps
->indexable_temps
, struct wined3d_shader_indexable_temp
, entry
)
2676 if (idx_temp_reg
->component_count
!= 4)
2677 FIXME("Ignoring component count %u.\n", idx_temp_reg
->component_count
);
2678 shader_addline(buffer
, "vec4 X%u[%u];\n", idx_temp_reg
->register_idx
, idx_temp_reg
->register_size
);
2681 /* Declare loop registers aLx */
2682 if (version
->major
< 4)
2684 for (i
= 0; i
< reg_maps
->loop_depth
; ++i
)
2686 shader_addline(buffer
, "int aL%u;\n", i
);
2687 shader_addline(buffer
, "int tmpInt%u;\n", i
);
2691 /* Temporary variables for matrix operations */
2692 shader_addline(buffer
, "vec4 tmp0;\n");
2693 shader_addline(buffer
, "vec4 tmp1;\n");
2695 if (!shader
->load_local_constsF
)
2697 LIST_FOR_EACH_ENTRY(lconst
, &shader
->constantsF
, struct wined3d_shader_lconst
, entry
)
2699 shader_addline(buffer
, "const vec4 %s_lc%u = ", prefix
, lconst
->idx
);
2700 shader_glsl_append_imm_vec4(buffer
, (const float *)lconst
->value
);
2701 shader_addline(buffer
, ";\n");
2706 /*****************************************************************************
2707 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
2709 * For more information, see http://wiki.winehq.org/DirectX-Shaders
2710 ****************************************************************************/
2713 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction
*ins
,
2714 const struct wined3d_shader_src_param
*wined3d_src
, DWORD mask
, struct glsl_src_param
*glsl_src
);
2716 /** Used for opcode modifiers - They multiply the result by the specified amount */
2717 static const char * const shift_glsl_tab
[] = {
2719 "2.0 * ", /* 1 (x2) */
2720 "4.0 * ", /* 2 (x4) */
2721 "8.0 * ", /* 3 (x8) */
2722 "16.0 * ", /* 4 (x16) */
2723 "32.0 * ", /* 5 (x32) */
2730 "0.0625 * ", /* 12 (d16) */
2731 "0.125 * ", /* 13 (d8) */
2732 "0.25 * ", /* 14 (d4) */
2733 "0.5 * " /* 15 (d2) */
2736 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
2737 static void shader_glsl_gen_modifier(enum wined3d_shader_src_modifier src_modifier
,
2738 const char *in_reg
, const char *in_regswizzle
, char *out_str
)
2740 switch (src_modifier
)
2742 case WINED3DSPSM_DZ
: /* Need to handle this in the instructions itself (texld & texcrd). */
2743 case WINED3DSPSM_DW
:
2744 case WINED3DSPSM_NONE
:
2745 sprintf(out_str
, "%s%s", in_reg
, in_regswizzle
);
2747 case WINED3DSPSM_NEG
:
2748 sprintf(out_str
, "-%s%s", in_reg
, in_regswizzle
);
2750 case WINED3DSPSM_NOT
:
2751 sprintf(out_str
, "!%s%s", in_reg
, in_regswizzle
);
2753 case WINED3DSPSM_BIAS
:
2754 sprintf(out_str
, "(%s%s - vec4(0.5)%s)", in_reg
, in_regswizzle
, in_regswizzle
);
2756 case WINED3DSPSM_BIASNEG
:
2757 sprintf(out_str
, "-(%s%s - vec4(0.5)%s)", in_reg
, in_regswizzle
, in_regswizzle
);
2759 case WINED3DSPSM_SIGN
:
2760 sprintf(out_str
, "(2.0 * (%s%s - 0.5))", in_reg
, in_regswizzle
);
2762 case WINED3DSPSM_SIGNNEG
:
2763 sprintf(out_str
, "-(2.0 * (%s%s - 0.5))", in_reg
, in_regswizzle
);
2765 case WINED3DSPSM_COMP
:
2766 sprintf(out_str
, "(1.0 - %s%s)", in_reg
, in_regswizzle
);
2768 case WINED3DSPSM_X2
:
2769 sprintf(out_str
, "(2.0 * %s%s)", in_reg
, in_regswizzle
);
2771 case WINED3DSPSM_X2NEG
:
2772 sprintf(out_str
, "-(2.0 * %s%s)", in_reg
, in_regswizzle
);
2774 case WINED3DSPSM_ABS
:
2775 sprintf(out_str
, "abs(%s%s)", in_reg
, in_regswizzle
);
2777 case WINED3DSPSM_ABSNEG
:
2778 sprintf(out_str
, "-abs(%s%s)", in_reg
, in_regswizzle
);
2781 FIXME("Unhandled modifier %u\n", src_modifier
);
2782 sprintf(out_str
, "%s%s", in_reg
, in_regswizzle
);
2786 static void shader_glsl_fixup_scalar_register_variable(char *register_name
,
2787 const char *glsl_variable
, const struct wined3d_gl_info
*gl_info
)
2789 /* The ARB_shading_language_420pack extension allows swizzle operations on
2791 if (gl_info
->supported
[ARB_SHADING_LANGUAGE_420PACK
])
2792 sprintf(register_name
, "%s", glsl_variable
);
2794 sprintf(register_name
, "ivec2(%s, 0)", glsl_variable
);
2797 /** Writes the GLSL variable name that corresponds to the register that the
2798 * DX opcode parameter is trying to access */
2799 static void shader_glsl_get_register_name(const struct wined3d_shader_register
*reg
,
2800 enum wined3d_data_type data_type
, char *register_name
, BOOL
*is_color
,
2801 const struct wined3d_shader_instruction
*ins
)
2803 /* oPos, oFog and oPts in D3D */
2804 static const char * const hwrastout_reg_names
[] = {"vs_out[10]", "vs_out[11].x", "vs_out[11].y"};
2806 const struct wined3d_shader
*shader
= ins
->ctx
->shader
;
2807 const struct wined3d_shader_reg_maps
*reg_maps
= ins
->ctx
->reg_maps
;
2808 const struct wined3d_shader_version
*version
= ®_maps
->shader_version
;
2809 const struct wined3d_gl_info
*gl_info
= ins
->ctx
->gl_info
;
2810 const char *prefix
= shader_glsl_get_prefix(version
->type
);
2811 struct glsl_src_param rel_param0
, rel_param1
;
2812 char imm_str
[4][17];
2814 if (reg
->idx
[0].offset
!= ~0U && reg
->idx
[0].rel_addr
)
2815 shader_glsl_add_src_param(ins
, reg
->idx
[0].rel_addr
, WINED3DSP_WRITEMASK_0
, &rel_param0
);
2816 if (reg
->idx
[1].offset
!= ~0U && reg
->idx
[1].rel_addr
)
2817 shader_glsl_add_src_param(ins
, reg
->idx
[1].rel_addr
, WINED3DSP_WRITEMASK_0
, &rel_param1
);
2822 case WINED3DSPR_TEMP
:
2823 sprintf(register_name
, "R%u", reg
->idx
[0].offset
);
2826 case WINED3DSPR_INPUT
:
2827 case WINED3DSPR_INCONTROLPOINT
:
2828 if (version
->type
== WINED3D_SHADER_TYPE_VERTEX
)
2830 struct shader_glsl_ctx_priv
*priv
= ins
->ctx
->backend_data
;
2832 if (reg
->idx
[0].rel_addr
)
2833 FIXME("VS3+ input registers relative addressing.\n");
2834 if (priv
->cur_vs_args
->swizzle_map
& (1u << reg
->idx
[0].offset
))
2836 sprintf(register_name
, "%s_in%u", prefix
, reg
->idx
[0].offset
);
2840 if (version
->type
== WINED3D_SHADER_TYPE_HULL
2841 || version
->type
== WINED3D_SHADER_TYPE_DOMAIN
2842 || version
->type
== WINED3D_SHADER_TYPE_GEOMETRY
)
2844 if (reg
->idx
[0].rel_addr
)
2846 if (reg
->idx
[1].rel_addr
)
2847 sprintf(register_name
, "shader_in[%s + %u].reg[%s + %u]",
2848 rel_param0
.param_str
, reg
->idx
[0].offset
,
2849 rel_param1
.param_str
, reg
->idx
[1].offset
);
2851 sprintf(register_name
, "shader_in[%s + %u].reg[%u]",
2852 rel_param0
.param_str
, reg
->idx
[0].offset
,
2853 reg
->idx
[1].offset
);
2855 else if (reg
->idx
[1].rel_addr
)
2856 sprintf(register_name
, "shader_in[%u].reg[%s + %u]", reg
->idx
[0].offset
,
2857 rel_param1
.param_str
, reg
->idx
[1].offset
);
2859 sprintf(register_name
, "shader_in[%u].reg[%u]",
2860 reg
->idx
[0].offset
, reg
->idx
[1].offset
);
2864 /* pixel shaders >= 3.0 */
2865 if (version
->major
>= 3)
2867 DWORD idx
= shader
->u
.ps
.input_reg_map
[reg
->idx
[0].offset
];
2868 unsigned int in_count
= vec4_varyings(version
->major
, gl_info
);
2870 if (reg
->idx
[0].rel_addr
)
2872 /* Removing a + 0 would be an obvious optimization, but
2873 * OS X doesn't see the NOP operation there. */
2876 if (needs_legacy_glsl_syntax(gl_info
)
2877 && shader
->u
.ps
.declared_in_count
> in_count
)
2879 sprintf(register_name
,
2880 "((%s + %u) > %u ? (%s + %u) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s + %u])",
2881 rel_param0
.param_str
, idx
, in_count
- 1, rel_param0
.param_str
, idx
, in_count
,
2882 prefix
, rel_param0
.param_str
, idx
);
2886 sprintf(register_name
, "%s_in[%s + %u]", prefix
, rel_param0
.param_str
, idx
);
2891 if (needs_legacy_glsl_syntax(gl_info
)
2892 && shader
->u
.ps
.declared_in_count
> in_count
)
2894 sprintf(register_name
, "((%s) > %u ? (%s) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s])",
2895 rel_param0
.param_str
, in_count
- 1, rel_param0
.param_str
, in_count
,
2896 prefix
, rel_param0
.param_str
);
2900 sprintf(register_name
, "%s_in[%s]", prefix
, rel_param0
.param_str
);
2906 if (idx
== in_count
) sprintf(register_name
, "gl_Color");
2907 else if (idx
== in_count
+ 1) sprintf(register_name
, "gl_SecondaryColor");
2908 else sprintf(register_name
, "%s_in[%u]", prefix
, idx
);
2913 if (!reg
->idx
[0].offset
)
2914 strcpy(register_name
, "ffp_varying_diffuse");
2916 strcpy(register_name
, "ffp_varying_specular");
2921 case WINED3DSPR_CONST
:
2923 /* Relative addressing */
2924 if (reg
->idx
[0].rel_addr
)
2926 if (wined3d_settings
.check_float_constants
)
2927 sprintf(register_name
, "(%s + %u >= 0 && %s + %u < %u ? %s_c[%s + %u] : vec4(0.0))",
2928 rel_param0
.param_str
, reg
->idx
[0].offset
,
2929 rel_param0
.param_str
, reg
->idx
[0].offset
, shader
->limits
->constant_float
,
2930 prefix
, rel_param0
.param_str
, reg
->idx
[0].offset
);
2931 else if (reg
->idx
[0].offset
)
2932 sprintf(register_name
, "%s_c[%s + %u]", prefix
, rel_param0
.param_str
, reg
->idx
[0].offset
);
2934 sprintf(register_name
, "%s_c[%s]", prefix
, rel_param0
.param_str
);
2938 if (shader_constant_is_local(shader
, reg
->idx
[0].offset
))
2939 sprintf(register_name
, "%s_lc%u", prefix
, reg
->idx
[0].offset
);
2941 sprintf(register_name
, "%s_c[%u]", prefix
, reg
->idx
[0].offset
);
2946 case WINED3DSPR_CONSTINT
:
2947 sprintf(register_name
, "%s_i[%u]", prefix
, reg
->idx
[0].offset
);
2950 case WINED3DSPR_CONSTBOOL
:
2951 sprintf(register_name
, "%s_b[%u]", prefix
, reg
->idx
[0].offset
);
2954 case WINED3DSPR_TEXTURE
: /* case WINED3DSPR_ADDR: */
2955 if (version
->type
== WINED3D_SHADER_TYPE_PIXEL
)
2956 sprintf(register_name
, "T%u", reg
->idx
[0].offset
);
2958 sprintf(register_name
, "A%u", reg
->idx
[0].offset
);
2961 case WINED3DSPR_LOOP
:
2962 sprintf(register_name
, "aL%u", ins
->ctx
->state
->current_loop_reg
- 1);
2965 case WINED3DSPR_SAMPLER
:
2966 sprintf(register_name
, "%s_sampler%u", prefix
, reg
->idx
[0].offset
);
2969 case WINED3DSPR_COLOROUT
:
2970 if (reg
->idx
[0].offset
>= gl_info
->limits
.buffers
)
2971 WARN("Write to render target %u, only %d supported.\n",
2972 reg
->idx
[0].offset
, gl_info
->limits
.buffers
);
2974 sprintf(register_name
, "%s[%u]", get_fragment_output(gl_info
), reg
->idx
[0].offset
);
2977 case WINED3DSPR_RASTOUT
:
2978 sprintf(register_name
, "%s", hwrastout_reg_names
[reg
->idx
[0].offset
]);
2981 case WINED3DSPR_DEPTHOUT
:
2982 case WINED3DSPR_DEPTHOUTGE
:
2983 case WINED3DSPR_DEPTHOUTLE
:
2984 sprintf(register_name
, "gl_FragDepth");
2987 case WINED3DSPR_ATTROUT
:
2988 if (!reg
->idx
[0].offset
)
2989 sprintf(register_name
, "%s_out[8]", prefix
);
2991 sprintf(register_name
, "%s_out[9]", prefix
);
2994 case WINED3DSPR_TEXCRDOUT
:
2995 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
2996 if (reg
->idx
[0].rel_addr
)
2997 sprintf(register_name
, "%s_out[%s + %u]",
2998 prefix
, rel_param0
.param_str
, reg
->idx
[0].offset
);
3000 sprintf(register_name
, "%s_out[%u]", prefix
, reg
->idx
[0].offset
);
3003 case WINED3DSPR_MISCTYPE
:
3004 if (!reg
->idx
[0].offset
)
3007 sprintf(register_name
, "vpos");
3009 else if (reg
->idx
[0].offset
== 1)
3011 /* Note that gl_FrontFacing is a bool, while vFace is
3012 * a float for which the sign determines front/back */
3013 sprintf(register_name
, "(gl_FrontFacing ? 1.0 : -1.0)");
3017 FIXME("Unhandled misctype register %u.\n", reg
->idx
[0].offset
);
3018 sprintf(register_name
, "unrecognized_register");
3022 case WINED3DSPR_IMMCONST
:
3023 switch (reg
->immconst_type
)
3025 case WINED3D_IMMCONST_SCALAR
:
3028 case WINED3D_DATA_FLOAT
:
3029 if (gl_info
->supported
[ARB_SHADER_BIT_ENCODING
])
3030 sprintf(register_name
, "uintBitsToFloat(%#xu)", reg
->u
.immconst_data
[0]);
3032 wined3d_ftoa(*(const float *)reg
->u
.immconst_data
, register_name
);
3034 case WINED3D_DATA_INT
:
3035 sprintf(register_name
, "%#x", reg
->u
.immconst_data
[0]);
3037 case WINED3D_DATA_RESOURCE
:
3038 case WINED3D_DATA_SAMPLER
:
3039 case WINED3D_DATA_UINT
:
3040 sprintf(register_name
, "%#xu", reg
->u
.immconst_data
[0]);
3043 sprintf(register_name
, "<unhandled data type %#x>", data_type
);
3048 case WINED3D_IMMCONST_VEC4
:
3051 case WINED3D_DATA_FLOAT
:
3052 if (gl_info
->supported
[ARB_SHADER_BIT_ENCODING
])
3054 sprintf(register_name
, "uintBitsToFloat(uvec4(%#xu, %#xu, %#xu, %#xu))",
3055 reg
->u
.immconst_data
[0], reg
->u
.immconst_data
[1],
3056 reg
->u
.immconst_data
[2], reg
->u
.immconst_data
[3]);
3060 wined3d_ftoa(*(const float *)®
->u
.immconst_data
[0], imm_str
[0]);
3061 wined3d_ftoa(*(const float *)®
->u
.immconst_data
[1], imm_str
[1]);
3062 wined3d_ftoa(*(const float *)®
->u
.immconst_data
[2], imm_str
[2]);
3063 wined3d_ftoa(*(const float *)®
->u
.immconst_data
[3], imm_str
[3]);
3064 sprintf(register_name
, "vec4(%s, %s, %s, %s)",
3065 imm_str
[0], imm_str
[1], imm_str
[2], imm_str
[3]);
3068 case WINED3D_DATA_INT
:
3069 sprintf(register_name
, "ivec4(%#x, %#x, %#x, %#x)",
3070 reg
->u
.immconst_data
[0], reg
->u
.immconst_data
[1],
3071 reg
->u
.immconst_data
[2], reg
->u
.immconst_data
[3]);
3073 case WINED3D_DATA_RESOURCE
:
3074 case WINED3D_DATA_SAMPLER
:
3075 case WINED3D_DATA_UINT
:
3076 sprintf(register_name
, "uvec4(%#xu, %#xu, %#xu, %#xu)",
3077 reg
->u
.immconst_data
[0], reg
->u
.immconst_data
[1],
3078 reg
->u
.immconst_data
[2], reg
->u
.immconst_data
[3]);
3081 sprintf(register_name
, "<unhandled data type %#x>", data_type
);
3087 FIXME("Unhandled immconst type %#x\n", reg
->immconst_type
);
3088 sprintf(register_name
, "<unhandled_immconst_type %#x>", reg
->immconst_type
);
3092 case WINED3DSPR_CONSTBUFFER
:
3093 if (reg
->idx
[1].rel_addr
)
3094 sprintf(register_name
, "%s_cb%u[%s + %u]",
3095 prefix
, reg
->idx
[0].offset
, rel_param1
.param_str
, reg
->idx
[1].offset
);
3097 sprintf(register_name
, "%s_cb%u[%u]", prefix
, reg
->idx
[0].offset
, reg
->idx
[1].offset
);
3100 case WINED3DSPR_IMMCONSTBUFFER
:
3101 if (reg
->idx
[0].rel_addr
)
3102 sprintf(register_name
, "%s_icb[%s + %u]", prefix
, rel_param0
.param_str
, reg
->idx
[0].offset
);
3104 sprintf(register_name
, "%s_icb[%u]", prefix
, reg
->idx
[0].offset
);
3107 case WINED3DSPR_PRIMID
:
3108 if (version
->type
== WINED3D_SHADER_TYPE_GEOMETRY
)
3109 sprintf(register_name
, "gl_PrimitiveIDIn");
3111 sprintf(register_name
, "gl_PrimitiveID");
3114 case WINED3DSPR_IDXTEMP
:
3115 if (reg
->idx
[1].rel_addr
)
3116 sprintf(register_name
, "X%u[%s + %u]", reg
->idx
[0].offset
, rel_param1
.param_str
, reg
->idx
[1].offset
);
3118 sprintf(register_name
, "X%u[%u]", reg
->idx
[0].offset
, reg
->idx
[1].offset
);
3121 case WINED3DSPR_LOCALTHREADINDEX
:
3122 shader_glsl_fixup_scalar_register_variable(register_name
,
3123 "int(gl_LocalInvocationIndex)", gl_info
);
3126 case WINED3DSPR_GSINSTID
:
3127 case WINED3DSPR_OUTPOINTID
:
3128 shader_glsl_fixup_scalar_register_variable(register_name
,
3129 "gl_InvocationID", gl_info
);
3132 case WINED3DSPR_THREADID
:
3133 sprintf(register_name
, "ivec3(gl_GlobalInvocationID)");
3136 case WINED3DSPR_THREADGROUPID
:
3137 sprintf(register_name
, "ivec3(gl_WorkGroupID)");
3140 case WINED3DSPR_LOCALTHREADID
:
3141 sprintf(register_name
, "ivec3(gl_LocalInvocationID)");
3144 case WINED3DSPR_FORKINSTID
:
3145 case WINED3DSPR_JOININSTID
:
3146 shader_glsl_fixup_scalar_register_variable(register_name
,
3147 "phase_instance_id", gl_info
);
3150 case WINED3DSPR_TESSCOORD
:
3151 sprintf(register_name
, "gl_TessCoord");
3154 case WINED3DSPR_OUTCONTROLPOINT
:
3155 if (reg
->idx
[0].rel_addr
)
3157 if (reg
->idx
[1].rel_addr
)
3158 sprintf(register_name
, "shader_out[%s + %u].reg[%s + %u]",
3159 rel_param0
.param_str
, reg
->idx
[0].offset
,
3160 rel_param1
.param_str
, reg
->idx
[1].offset
);
3162 sprintf(register_name
, "shader_out[%s + %u].reg[%u]",
3163 rel_param0
.param_str
, reg
->idx
[0].offset
,
3164 reg
->idx
[1].offset
);
3166 else if (reg
->idx
[1].rel_addr
)
3168 sprintf(register_name
, "shader_out[%u].reg[%s + %u]",
3169 reg
->idx
[0].offset
, rel_param1
.param_str
,
3170 reg
->idx
[1].offset
);
3174 sprintf(register_name
, "shader_out[%u].reg[%u]",
3175 reg
->idx
[0].offset
, reg
->idx
[1].offset
);
3179 case WINED3DSPR_PATCHCONST
:
3180 if (version
->type
== WINED3D_SHADER_TYPE_HULL
)
3181 sprintf(register_name
, "hs_out[%u]", reg
->idx
[0].offset
);
3183 sprintf(register_name
, "vpc[%u]", reg
->idx
[0].offset
);
3187 FIXME("Unhandled register type %#x.\n", reg
->type
);
3188 sprintf(register_name
, "unrecognized_register");
3193 static void shader_glsl_write_mask_to_str(DWORD write_mask
, char *str
)
3196 if (write_mask
& WINED3DSP_WRITEMASK_0
) *str
++ = 'x';
3197 if (write_mask
& WINED3DSP_WRITEMASK_1
) *str
++ = 'y';
3198 if (write_mask
& WINED3DSP_WRITEMASK_2
) *str
++ = 'z';
3199 if (write_mask
& WINED3DSP_WRITEMASK_3
) *str
++ = 'w';
3203 /* Get the GLSL write mask for the destination register */
3204 static DWORD
shader_glsl_get_write_mask(const struct wined3d_shader_dst_param
*param
, char *write_mask
)
3206 DWORD mask
= param
->write_mask
;
3208 if (shader_is_scalar(¶m
->reg
))
3210 mask
= WINED3DSP_WRITEMASK_0
;
3215 shader_glsl_write_mask_to_str(mask
, write_mask
);
3221 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask
)
3223 unsigned int size
= 0;
3225 if (write_mask
& WINED3DSP_WRITEMASK_0
) ++size
;
3226 if (write_mask
& WINED3DSP_WRITEMASK_1
) ++size
;
3227 if (write_mask
& WINED3DSP_WRITEMASK_2
) ++size
;
3228 if (write_mask
& WINED3DSP_WRITEMASK_3
) ++size
;
3233 static unsigned int shader_glsl_swizzle_get_component(DWORD swizzle
,
3234 unsigned int component_idx
)
3236 /* swizzle bits fields: wwzzyyxx */
3237 return (swizzle
>> (2 * component_idx
)) & 0x3;
3240 static void shader_glsl_swizzle_to_str(DWORD swizzle
, BOOL fixup
, DWORD mask
, char *str
)
3242 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
3243 * but addressed as "rgba". To fix this we need to swap the register's x
3244 * and z components. */
3245 const char *swizzle_chars
= fixup
? "zyxw" : "xyzw";
3249 for (i
= 0; i
< 4; ++i
)
3251 if (mask
& (WINED3DSP_WRITEMASK_0
<< i
))
3252 *str
++ = swizzle_chars
[shader_glsl_swizzle_get_component(swizzle
, i
)];
3257 static void shader_glsl_get_swizzle(const struct wined3d_shader_src_param
*param
,
3258 BOOL fixup
, DWORD mask
, char *swizzle_str
)
3260 if (shader_is_scalar(¶m
->reg
))
3261 *swizzle_str
= '\0';
3263 shader_glsl_swizzle_to_str(param
->swizzle
, fixup
, mask
, swizzle_str
);
3266 static void shader_glsl_sprintf_cast(struct wined3d_string_buffer
*dst_param
, const char *src_param
,
3267 enum wined3d_data_type dst_data_type
, enum wined3d_data_type src_data_type
)
3269 if (dst_data_type
== src_data_type
)
3271 string_buffer_sprintf(dst_param
, "%s", src_param
);
3275 if (src_data_type
== WINED3D_DATA_FLOAT
)
3277 switch (dst_data_type
)
3279 case WINED3D_DATA_INT
:
3280 string_buffer_sprintf(dst_param
, "floatBitsToInt(%s)", src_param
);
3282 case WINED3D_DATA_RESOURCE
:
3283 case WINED3D_DATA_SAMPLER
:
3284 case WINED3D_DATA_UINT
:
3285 string_buffer_sprintf(dst_param
, "floatBitsToUint(%s)", src_param
);
3292 if (src_data_type
== WINED3D_DATA_UINT
&& dst_data_type
== WINED3D_DATA_FLOAT
)
3294 string_buffer_sprintf(dst_param
, "uintBitsToFloat(%s)", src_param
);
3298 if (src_data_type
== WINED3D_DATA_INT
&& dst_data_type
== WINED3D_DATA_FLOAT
)
3300 string_buffer_sprintf(dst_param
, "intBitsToFloat(%s)", src_param
);
3304 FIXME("Unhandled cast from %#x to %#x.\n", src_data_type
, dst_data_type
);
3305 string_buffer_sprintf(dst_param
, "%s", src_param
);
3308 /* From a given parameter token, generate the corresponding GLSL string.
3309 * Also, return the actual register name and swizzle in case the
3310 * caller needs this information as well. */
3311 static void shader_glsl_add_src_param_ext(const struct wined3d_shader_instruction
*ins
,
3312 const struct wined3d_shader_src_param
*wined3d_src
, DWORD mask
, struct glsl_src_param
*glsl_src
,
3313 enum wined3d_data_type data_type
)
3315 struct shader_glsl_ctx_priv
*priv
= ins
->ctx
->backend_data
;
3316 struct wined3d_string_buffer
*reg_name
= string_buffer_get(priv
->string_buffers
);
3317 enum wined3d_data_type param_data_type
;
3318 BOOL is_color
= FALSE
;
3319 char swizzle_str
[6];
3321 glsl_src
->reg_name
[0] = '\0';
3322 glsl_src
->param_str
[0] = '\0';
3323 swizzle_str
[0] = '\0';
3325 shader_glsl_get_register_name(&wined3d_src
->reg
, data_type
, glsl_src
->reg_name
, &is_color
, ins
);
3326 shader_glsl_get_swizzle(wined3d_src
, is_color
, mask
, swizzle_str
);
3328 switch (wined3d_src
->reg
.type
)
3330 case WINED3DSPR_IMMCONST
:
3331 param_data_type
= data_type
;
3333 case WINED3DSPR_FORKINSTID
:
3334 case WINED3DSPR_GSINSTID
:
3335 case WINED3DSPR_JOININSTID
:
3336 case WINED3DSPR_LOCALTHREADID
:
3337 case WINED3DSPR_LOCALTHREADINDEX
:
3338 case WINED3DSPR_OUTPOINTID
:
3339 case WINED3DSPR_PRIMID
:
3340 case WINED3DSPR_THREADGROUPID
:
3341 case WINED3DSPR_THREADID
:
3342 param_data_type
= WINED3D_DATA_INT
;
3345 param_data_type
= WINED3D_DATA_FLOAT
;
3349 shader_glsl_sprintf_cast(reg_name
, glsl_src
->reg_name
, data_type
, param_data_type
);
3350 shader_glsl_gen_modifier(wined3d_src
->modifiers
, reg_name
->buffer
, swizzle_str
, glsl_src
->param_str
);
3352 string_buffer_release(priv
->string_buffers
, reg_name
);
3355 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction
*ins
,
3356 const struct wined3d_shader_src_param
*wined3d_src
, DWORD mask
, struct glsl_src_param
*glsl_src
)
3358 shader_glsl_add_src_param_ext(ins
, wined3d_src
, mask
, glsl_src
, wined3d_src
->reg
.data_type
);
3361 /* From a given parameter token, generate the corresponding GLSL string.
3362 * Also, return the actual register name and swizzle in case the
3363 * caller needs this information as well. */
3364 static DWORD
shader_glsl_add_dst_param(const struct wined3d_shader_instruction
*ins
,
3365 const struct wined3d_shader_dst_param
*wined3d_dst
, struct glsl_dst_param
*glsl_dst
)
3367 BOOL is_color
= FALSE
;
3369 glsl_dst
->mask_str
[0] = '\0';
3370 glsl_dst
->reg_name
[0] = '\0';
3372 shader_glsl_get_register_name(&wined3d_dst
->reg
, wined3d_dst
->reg
.data_type
,
3373 glsl_dst
->reg_name
, &is_color
, ins
);
3374 return shader_glsl_get_write_mask(wined3d_dst
, glsl_dst
->mask_str
);
3377 /* Append the destination part of the instruction to the buffer, return the effective write mask */
3378 static DWORD
shader_glsl_append_dst_ext(struct wined3d_string_buffer
*buffer
,
3379 const struct wined3d_shader_instruction
*ins
, const struct wined3d_shader_dst_param
*dst
,
3380 enum wined3d_data_type data_type
)
3382 struct glsl_dst_param glsl_dst
;
3385 if ((mask
= shader_glsl_add_dst_param(ins
, dst
, &glsl_dst
)))
3389 case WINED3D_DATA_FLOAT
:
3390 shader_addline(buffer
, "%s%s = %s(",
3391 glsl_dst
.reg_name
, glsl_dst
.mask_str
, shift_glsl_tab
[dst
->shift
]);
3393 case WINED3D_DATA_INT
:
3394 shader_addline(buffer
, "%s%s = %sintBitsToFloat(",
3395 glsl_dst
.reg_name
, glsl_dst
.mask_str
, shift_glsl_tab
[dst
->shift
]);
3397 case WINED3D_DATA_RESOURCE
:
3398 case WINED3D_DATA_SAMPLER
:
3399 case WINED3D_DATA_UINT
:
3400 shader_addline(buffer
, "%s%s = %suintBitsToFloat(",
3401 glsl_dst
.reg_name
, glsl_dst
.mask_str
, shift_glsl_tab
[dst
->shift
]);
3404 FIXME("Unhandled data type %#x.\n", data_type
);
3405 shader_addline(buffer
, "%s%s = %s(",
3406 glsl_dst
.reg_name
, glsl_dst
.mask_str
, shift_glsl_tab
[dst
->shift
]);
3414 /* Append the destination part of the instruction to the buffer, return the effective write mask */
3415 static DWORD
shader_glsl_append_dst(struct wined3d_string_buffer
*buffer
, const struct wined3d_shader_instruction
*ins
)
3417 return shader_glsl_append_dst_ext(buffer
, ins
, &ins
->dst
[0], ins
->dst
[0].reg
.data_type
);
3420 /** Process GLSL instruction modifiers */
3421 static void shader_glsl_add_instruction_modifiers(const struct wined3d_shader_instruction
*ins
)
3423 struct glsl_dst_param dst_param
;
3426 if (!ins
->dst_count
) return;
3428 modifiers
= ins
->dst
[0].modifiers
;
3429 if (!modifiers
) return;
3431 shader_glsl_add_dst_param(ins
, &ins
->dst
[0], &dst_param
);
3433 if (modifiers
& WINED3DSPDM_SATURATE
)
3435 /* _SAT means to clamp the value of the register to between 0 and 1 */
3436 shader_addline(ins
->ctx
->buffer
, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param
.reg_name
,
3437 dst_param
.mask_str
, dst_param
.reg_name
, dst_param
.mask_str
);
3440 if (modifiers
& WINED3DSPDM_MSAMPCENTROID
)
3442 FIXME("_centroid modifier not handled\n");
3445 if (modifiers
& WINED3DSPDM_PARTIALPRECISION
)
3447 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
3451 static const char *shader_glsl_get_rel_op(enum wined3d_shader_rel_op op
)
3455 case WINED3D_SHADER_REL_OP_GT
: return ">";
3456 case WINED3D_SHADER_REL_OP_EQ
: return "==";
3457 case WINED3D_SHADER_REL_OP_GE
: return ">=";
3458 case WINED3D_SHADER_REL_OP_LT
: return "<";
3459 case WINED3D_SHADER_REL_OP_NE
: return "!=";
3460 case WINED3D_SHADER_REL_OP_LE
: return "<=";
3462 FIXME("Unrecognized operator %#x.\n", op
);
3467 static BOOL
shader_glsl_has_core_grad(const struct wined3d_gl_info
*gl_info
)
3469 return shader_glsl_get_version(gl_info
) >= 130 || gl_info
->supported
[EXT_GPU_SHADER4
];
3472 static void shader_glsl_get_coord_size(enum wined3d_shader_resource_type resource_type
,
3473 unsigned int *coord_size
, unsigned int *deriv_size
)
3475 const BOOL is_array
= resource_type
== WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY
3476 || resource_type
== WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY
;
3478 *coord_size
= resource_type_info
[resource_type
].coord_size
;
3479 *deriv_size
= *coord_size
;
3484 static void shader_glsl_get_sample_function(const struct wined3d_shader_context
*ctx
,
3485 DWORD resource_idx
, DWORD sampler_idx
, DWORD flags
, struct glsl_sample_function
*sample_function
)
3487 enum wined3d_shader_resource_type resource_type
= ctx
->reg_maps
->resource_info
[resource_idx
].type
;
3488 struct shader_glsl_ctx_priv
*priv
= ctx
->backend_data
;
3489 const struct wined3d_gl_info
*gl_info
= ctx
->gl_info
;
3490 BOOL shadow
= glsl_is_shadow_sampler(ctx
->shader
, priv
->cur_ps_args
, resource_idx
, sampler_idx
);
3491 BOOL projected
= flags
& WINED3D_GLSL_SAMPLE_PROJECTED
;
3492 BOOL texrect
= ctx
->reg_maps
->shader_version
.type
== WINED3D_SHADER_TYPE_PIXEL
3493 && priv
->cur_ps_args
->np2_fixup
& (1u << resource_idx
)
3494 && gl_info
->supported
[ARB_TEXTURE_RECTANGLE
];
3495 BOOL lod
= flags
& WINED3D_GLSL_SAMPLE_LOD
;
3496 BOOL grad
= flags
& WINED3D_GLSL_SAMPLE_GRAD
;
3497 BOOL offset
= flags
& WINED3D_GLSL_SAMPLE_OFFSET
;
3498 const char *base
= "texture", *type_part
= "", *suffix
= "";
3499 unsigned int coord_size
, deriv_size
;
3501 sample_function
->data_type
= ctx
->reg_maps
->resource_info
[resource_idx
].data_type
;
3503 if (resource_type
>= ARRAY_SIZE(resource_type_info
))
3505 ERR("Unexpected resource type %#x.\n", resource_type
);
3506 resource_type
= WINED3D_SHADER_RESOURCE_TEXTURE_2D
;
3509 /* Note that there's no such thing as a projected cube texture. */
3510 if (resource_type
== WINED3D_SHADER_RESOURCE_TEXTURE_CUBE
)
3513 if (needs_legacy_glsl_syntax(gl_info
))
3518 type_part
= resource_type_info
[resource_type
].type_part
;
3519 if (resource_type
== WINED3D_SHADER_RESOURCE_TEXTURE_2D
&& texrect
)
3520 type_part
= "2DRect";
3521 if (!type_part
[0] && resource_type
!= WINED3D_SHADER_RESOURCE_TEXTURE_CUBEARRAY
)
3522 FIXME("Unhandled resource type %#x.\n", resource_type
);
3524 if (!lod
&& grad
&& !shader_glsl_has_core_grad(gl_info
))
3526 if (gl_info
->supported
[ARB_SHADER_TEXTURE_LOD
])
3529 FIXME("Unsupported grad function.\n");
3533 if (flags
& WINED3D_GLSL_SAMPLE_LOAD
)
3535 static const DWORD texel_fetch_flags
= WINED3D_GLSL_SAMPLE_LOAD
| WINED3D_GLSL_SAMPLE_OFFSET
;
3536 if (flags
& ~texel_fetch_flags
)
3537 ERR("Unexpected flags %#x for texelFetch.\n", flags
& ~texel_fetch_flags
);
3539 base
= "texelFetch";
3543 sample_function
->name
= string_buffer_get(priv
->string_buffers
);
3544 string_buffer_sprintf(sample_function
->name
, "%s%s%s%s%s%s", base
, type_part
, projected
? "Proj" : "",
3545 lod
? "Lod" : grad
? "Grad" : "", offset
? "Offset" : "", suffix
);
3547 shader_glsl_get_coord_size(resource_type
, &coord_size
, &deriv_size
);
3550 sample_function
->offset_size
= offset
? deriv_size
: 0;
3551 sample_function
->coord_mask
= (1u << coord_size
) - 1;
3552 sample_function
->deriv_mask
= (1u << deriv_size
) - 1;
3553 sample_function
->output_single_component
= shadow
&& !needs_legacy_glsl_syntax(gl_info
);
3556 static void shader_glsl_release_sample_function(const struct wined3d_shader_context
*ctx
,
3557 struct glsl_sample_function
*sample_function
)
3559 const struct shader_glsl_ctx_priv
*priv
= ctx
->backend_data
;
3561 string_buffer_release(priv
->string_buffers
, sample_function
->name
);
3564 static void shader_glsl_append_fixup_arg(char *arguments
, const char *reg_name
,
3565 BOOL sign_fixup
, enum fixup_channel_source channel_source
)
3567 switch(channel_source
)
3569 case CHANNEL_SOURCE_ZERO
:
3570 strcat(arguments
, "0.0");
3573 case CHANNEL_SOURCE_ONE
:
3574 strcat(arguments
, "1.0");
3577 case CHANNEL_SOURCE_X
:
3578 strcat(arguments
, reg_name
);
3579 strcat(arguments
, ".x");
3582 case CHANNEL_SOURCE_Y
:
3583 strcat(arguments
, reg_name
);
3584 strcat(arguments
, ".y");
3587 case CHANNEL_SOURCE_Z
:
3588 strcat(arguments
, reg_name
);
3589 strcat(arguments
, ".z");
3592 case CHANNEL_SOURCE_W
:
3593 strcat(arguments
, reg_name
);
3594 strcat(arguments
, ".w");
3598 FIXME("Unhandled channel source %#x\n", channel_source
);
3599 strcat(arguments
, "undefined");
3603 if (sign_fixup
) strcat(arguments
, " * 2.0 - 1.0");
3606 static void shader_glsl_color_correction_ext(struct wined3d_string_buffer
*buffer
,
3607 const char *reg_name
, DWORD mask
, struct color_fixup_desc fixup
)
3609 unsigned int mask_size
, remaining
;
3610 DWORD fixup_mask
= 0;
3611 char arguments
[256];
3614 if (fixup
.x_sign_fixup
|| fixup
.x_source
!= CHANNEL_SOURCE_X
) fixup_mask
|= WINED3DSP_WRITEMASK_0
;
3615 if (fixup
.y_sign_fixup
|| fixup
.y_source
!= CHANNEL_SOURCE_Y
) fixup_mask
|= WINED3DSP_WRITEMASK_1
;
3616 if (fixup
.z_sign_fixup
|| fixup
.z_source
!= CHANNEL_SOURCE_Z
) fixup_mask
|= WINED3DSP_WRITEMASK_2
;
3617 if (fixup
.w_sign_fixup
|| fixup
.w_source
!= CHANNEL_SOURCE_W
) fixup_mask
|= WINED3DSP_WRITEMASK_3
;
3618 if (!(mask
&= fixup_mask
))
3621 if (is_complex_fixup(fixup
))
3623 enum complex_fixup complex_fixup
= get_complex_fixup(fixup
);
3624 FIXME("Complex fixup (%#x) not supported\n",complex_fixup
);
3628 shader_glsl_write_mask_to_str(mask
, mask_str
);
3629 mask_size
= shader_glsl_get_write_mask_size(mask
);
3631 arguments
[0] = '\0';
3632 remaining
= mask_size
;
3633 if (mask
& WINED3DSP_WRITEMASK_0
)
3635 shader_glsl_append_fixup_arg(arguments
, reg_name
, fixup
.x_sign_fixup
, fixup
.x_source
);
3636 if (--remaining
) strcat(arguments
, ", ");
3638 if (mask
& WINED3DSP_WRITEMASK_1
)
3640 shader_glsl_append_fixup_arg(arguments
, reg_name
, fixup
.y_sign_fixup
, fixup
.y_source
);
3641 if (--remaining
) strcat(arguments
, ", ");
3643 if (mask
& WINED3DSP_WRITEMASK_2
)
3645 shader_glsl_append_fixup_arg(arguments
, reg_name
, fixup
.z_sign_fixup
, fixup
.z_source
);
3646 if (--remaining
) strcat(arguments
, ", ");
3648 if (mask
& WINED3DSP_WRITEMASK_3
)
3650 shader_glsl_append_fixup_arg(arguments
, reg_name
, fixup
.w_sign_fixup
, fixup
.w_source
);
3651 if (--remaining
) strcat(arguments
, ", ");
3655 shader_addline(buffer
, "%s%s = vec%u(%s);\n", reg_name
, mask_str
, mask_size
, arguments
);
3657 shader_addline(buffer
, "%s%s = %s;\n", reg_name
, mask_str
, arguments
);
3660 static void shader_glsl_color_correction(const struct wined3d_shader_instruction
*ins
, struct color_fixup_desc fixup
)
3665 shader_glsl_get_register_name(&ins
->dst
[0].reg
, ins
->dst
[0].reg
.data_type
, reg_name
, &is_color
, ins
);
3666 shader_glsl_color_correction_ext(ins
->ctx
->buffer
, reg_name
, ins
->dst
[0].write_mask
, fixup
);
3669 static void PRINTF_ATTR(9, 10) shader_glsl_gen_sample_code(const struct wined3d_shader_instruction
*ins
,
3670 unsigned int sampler_bind_idx
, const struct glsl_sample_function
*sample_function
, DWORD swizzle
,
3671 const char *dx
, const char *dy
, const char *bias
, const struct wined3d_shader_texel_offset
*offset
,
3672 const char *coord_reg_fmt
, ...)
3674 const struct wined3d_shader_version
*version
= &ins
->ctx
->reg_maps
->shader_version
;
3675 char dst_swizzle
[6];
3676 struct color_fixup_desc fixup
;
3677 BOOL np2_fixup
= FALSE
;
3681 shader_glsl_swizzle_to_str(swizzle
, FALSE
, ins
->dst
[0].write_mask
, dst_swizzle
);
3683 /* If ARB_texture_swizzle is supported we don't need to do anything here.
3684 * We actually rely on it for vertex shaders and SM4+. */
3685 if (version
->type
== WINED3D_SHADER_TYPE_PIXEL
&& version
->major
< 4)
3687 const struct shader_glsl_ctx_priv
*priv
= ins
->ctx
->backend_data
;
3688 fixup
= priv
->cur_ps_args
->color_fixup
[sampler_bind_idx
];
3690 if (priv
->cur_ps_args
->np2_fixup
& (1u << sampler_bind_idx
))
3695 fixup
= COLOR_FIXUP_IDENTITY
;
3698 shader_glsl_append_dst_ext(ins
->ctx
->buffer
, ins
, &ins
->dst
[0], sample_function
->data_type
);
3700 if (sample_function
->output_single_component
)
3701 shader_addline(ins
->ctx
->buffer
, "vec4(");
3703 shader_addline(ins
->ctx
->buffer
, "%s(%s_sampler%u, ",
3704 sample_function
->name
->buffer
, shader_glsl_get_prefix(version
->type
), sampler_bind_idx
);
3708 va_start(args
, coord_reg_fmt
);
3709 ret
= shader_vaddline(ins
->ctx
->buffer
, coord_reg_fmt
, args
);
3713 if (!string_buffer_resize(ins
->ctx
->buffer
, ret
))
3719 const struct shader_glsl_ctx_priv
*priv
= ins
->ctx
->backend_data
;
3720 const unsigned char idx
= priv
->cur_np2fixup_info
->idx
[sampler_bind_idx
];
3722 switch (shader_glsl_get_write_mask_size(sample_function
->coord_mask
))
3725 shader_addline(ins
->ctx
->buffer
, " * ps_samplerNP2Fixup[%u].%s",
3726 idx
>> 1, (idx
% 2) ? "z" : "x");
3729 shader_addline(ins
->ctx
->buffer
, " * ps_samplerNP2Fixup[%u].%s",
3730 idx
>> 1, (idx
% 2) ? "zw" : "xy");
3733 shader_addline(ins
->ctx
->buffer
, " * vec3(ps_samplerNP2Fixup[%u].%s, 1.0)",
3734 idx
>> 1, (idx
% 2) ? "zw" : "xy");
3737 shader_addline(ins
->ctx
->buffer
, " * vec4(ps_samplerNP2Fixup[%u].%s, 1.0, 1.0)",
3738 idx
>> 1, (idx
% 2) ? "zw" : "xy");
3743 shader_addline(ins
->ctx
->buffer
, ", %s, %s", dx
, dy
);
3745 shader_addline(ins
->ctx
->buffer
, ", %s", bias
);
3746 if (sample_function
->offset_size
)
3748 int offset_immdata
[4] = {offset
->u
, offset
->v
, offset
->w
};
3749 shader_addline(ins
->ctx
->buffer
, ", ");
3750 shader_glsl_append_imm_ivec(ins
->ctx
->buffer
, offset_immdata
, sample_function
->offset_size
);
3752 shader_addline(ins
->ctx
->buffer
, ")");
3754 if (sample_function
->output_single_component
)
3755 shader_addline(ins
->ctx
->buffer
, ")");
3757 shader_addline(ins
->ctx
->buffer
, "%s);\n", dst_swizzle
);
3759 if (!is_identity_fixup(fixup
))
3760 shader_glsl_color_correction(ins
, fixup
);
3763 static void shader_glsl_fixup_position(struct wined3d_string_buffer
*buffer
)
3765 /* Write the final position.
3767 * OpenGL coordinates specify the center of the pixel while D3D coords
3768 * specify the corner. The offsets are stored in z and w in
3769 * pos_fixup. pos_fixup.y contains 1.0 or -1.0 to turn the rendering
3770 * upside down for offscreen rendering. pos_fixup.x contains 1.0 to allow
3772 shader_addline(buffer
, "gl_Position.y = gl_Position.y * pos_fixup.y;\n");
3773 shader_addline(buffer
, "gl_Position.xy += pos_fixup.zw * gl_Position.ww;\n");
3775 /* Z coord [0;1]->[-1;1] mapping, see comment in get_projection_matrix()
3778 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However,
3779 * shaders are run before the homogeneous divide, so we have to take the w
3780 * into account: z = ((z / w) * 2 - 1) * w, which is the same as
3782 shader_addline(buffer
, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
3785 /*****************************************************************************
3786 * Begin processing individual instruction opcodes
3787 ****************************************************************************/
3789 static void shader_glsl_binop(const struct wined3d_shader_instruction
*ins
)
3791 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
3792 struct glsl_src_param src0_param
;
3793 struct glsl_src_param src1_param
;
3797 /* Determine the GLSL operator to use based on the opcode */
3798 switch (ins
->handler_idx
)
3800 case WINED3DSIH_ADD
: op
= "+"; break;
3801 case WINED3DSIH_AND
: op
= "&"; break;
3802 case WINED3DSIH_DIV
: op
= "/"; break;
3803 case WINED3DSIH_IADD
: op
= "+"; break;
3804 case WINED3DSIH_ISHL
: op
= "<<"; break;
3805 case WINED3DSIH_ISHR
: op
= ">>"; break;
3806 case WINED3DSIH_MUL
: op
= "*"; break;
3807 case WINED3DSIH_OR
: op
= "|"; break;
3808 case WINED3DSIH_SUB
: op
= "-"; break;
3809 case WINED3DSIH_USHR
: op
= ">>"; break;
3810 case WINED3DSIH_XOR
: op
= "^"; break;
3812 op
= "<unhandled operator>";
3813 FIXME("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins
->handler_idx
));
3817 write_mask
= shader_glsl_append_dst(buffer
, ins
);
3818 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
3819 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
3820 shader_addline(buffer
, "%s %s %s);\n", src0_param
.param_str
, op
, src1_param
.param_str
);
3823 static void shader_glsl_relop(const struct wined3d_shader_instruction
*ins
)
3825 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
3826 struct glsl_src_param src0_param
;
3827 struct glsl_src_param src1_param
;
3828 unsigned int mask_size
;
3832 write_mask
= shader_glsl_append_dst(buffer
, ins
);
3833 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
3834 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
3835 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
3839 switch (ins
->handler_idx
)
3841 case WINED3DSIH_EQ
: op
= "equal"; break;
3842 case WINED3DSIH_IEQ
: op
= "equal"; break;
3843 case WINED3DSIH_GE
: op
= "greaterThanEqual"; break;
3844 case WINED3DSIH_IGE
: op
= "greaterThanEqual"; break;
3845 case WINED3DSIH_UGE
: op
= "greaterThanEqual"; break;
3846 case WINED3DSIH_LT
: op
= "lessThan"; break;
3847 case WINED3DSIH_ILT
: op
= "lessThan"; break;
3848 case WINED3DSIH_ULT
: op
= "lessThan"; break;
3849 case WINED3DSIH_NE
: op
= "notEqual"; break;
3850 case WINED3DSIH_INE
: op
= "notEqual"; break;
3852 op
= "<unhandled operator>";
3853 ERR("Unhandled opcode %#x.\n", ins
->handler_idx
);
3857 shader_addline(buffer
, "uvec%u(%s(%s, %s)) * 0xffffffffu);\n",
3858 mask_size
, op
, src0_param
.param_str
, src1_param
.param_str
);
3862 switch (ins
->handler_idx
)
3864 case WINED3DSIH_EQ
: op
= "=="; break;
3865 case WINED3DSIH_IEQ
: op
= "=="; break;
3866 case WINED3DSIH_GE
: op
= ">="; break;
3867 case WINED3DSIH_IGE
: op
= ">="; break;
3868 case WINED3DSIH_UGE
: op
= ">="; break;
3869 case WINED3DSIH_LT
: op
= "<"; break;
3870 case WINED3DSIH_ILT
: op
= "<"; break;
3871 case WINED3DSIH_ULT
: op
= "<"; break;
3872 case WINED3DSIH_NE
: op
= "!="; break;
3873 case WINED3DSIH_INE
: op
= "!="; break;
3875 op
= "<unhandled operator>";
3876 ERR("Unhandled opcode %#x.\n", ins
->handler_idx
);
3880 shader_addline(buffer
, "%s %s %s ? 0xffffffffu : 0u);\n",
3881 src0_param
.param_str
, op
, src1_param
.param_str
);
3885 static void shader_glsl_unary_op(const struct wined3d_shader_instruction
*ins
)
3887 struct glsl_src_param src_param
;
3891 switch (ins
->handler_idx
)
3893 case WINED3DSIH_INEG
: op
= "-"; break;
3894 case WINED3DSIH_NOT
: op
= "~"; break;
3896 op
= "<unhandled operator>";
3897 ERR("Unhandled opcode %s.\n",
3898 debug_d3dshaderinstructionhandler(ins
->handler_idx
));
3902 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
3903 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src_param
);
3904 shader_addline(ins
->ctx
->buffer
, "%s%s);\n", op
, src_param
.param_str
);
3907 static void shader_glsl_mul_extended(const struct wined3d_shader_instruction
*ins
)
3909 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
3910 struct glsl_src_param src0_param
;
3911 struct glsl_src_param src1_param
;
3914 /* If we have ARB_gpu_shader5, we can use imulExtended() / umulExtended().
3915 * If not, we can emulate it. */
3916 if (ins
->dst
[0].reg
.type
!= WINED3DSPR_NULL
)
3917 FIXME("64-bit integer multiplies not implemented.\n");
3919 if (ins
->dst
[1].reg
.type
!= WINED3DSPR_NULL
)
3921 write_mask
= shader_glsl_append_dst_ext(buffer
, ins
, &ins
->dst
[1], ins
->dst
[1].reg
.data_type
);
3922 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
3923 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
3925 shader_addline(ins
->ctx
->buffer
, "%s * %s);\n",
3926 src0_param
.param_str
, src1_param
.param_str
);
3930 static void shader_glsl_udiv(const struct wined3d_shader_instruction
*ins
)
3932 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
3933 struct glsl_src_param src0_param
, src1_param
;
3936 if (ins
->dst
[0].reg
.type
!= WINED3DSPR_NULL
)
3938 if (ins
->dst
[1].reg
.type
!= WINED3DSPR_NULL
)
3942 write_mask
= shader_glsl_get_write_mask(&ins
->dst
[0], dst_mask
);
3943 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
3944 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
3945 shader_addline(buffer
, "tmp0%s = uintBitsToFloat(%s / %s);\n",
3946 dst_mask
, src0_param
.param_str
, src1_param
.param_str
);
3948 write_mask
= shader_glsl_append_dst_ext(buffer
, ins
, &ins
->dst
[1], ins
->dst
[1].reg
.data_type
);
3949 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
3950 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
3951 shader_addline(buffer
, "%s %% %s);\n", src0_param
.param_str
, src1_param
.param_str
);
3953 shader_glsl_append_dst_ext(buffer
, ins
, &ins
->dst
[0], WINED3D_DATA_FLOAT
);
3954 shader_addline(buffer
, "tmp0%s);\n", dst_mask
);
3958 write_mask
= shader_glsl_append_dst_ext(buffer
, ins
, &ins
->dst
[0], ins
->dst
[0].reg
.data_type
);
3959 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
3960 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
3961 shader_addline(buffer
, "%s / %s);\n", src0_param
.param_str
, src1_param
.param_str
);
3964 else if (ins
->dst
[1].reg
.type
!= WINED3DSPR_NULL
)
3966 write_mask
= shader_glsl_append_dst_ext(buffer
, ins
, &ins
->dst
[1], ins
->dst
[1].reg
.data_type
);
3967 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
3968 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
3969 shader_addline(buffer
, "%s %% %s);\n", src0_param
.param_str
, src1_param
.param_str
);
3973 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
3974 static void shader_glsl_mov(const struct wined3d_shader_instruction
*ins
)
3976 const struct wined3d_gl_info
*gl_info
= ins
->ctx
->gl_info
;
3977 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
3978 struct glsl_src_param src0_param
;
3981 write_mask
= shader_glsl_append_dst(buffer
, ins
);
3982 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
3984 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
3985 * shader versions WINED3DSIO_MOVA is used for this. */
3986 if (ins
->ctx
->reg_maps
->shader_version
.major
== 1
3987 && ins
->ctx
->reg_maps
->shader_version
.type
== WINED3D_SHADER_TYPE_VERTEX
3988 && ins
->dst
[0].reg
.type
== WINED3DSPR_ADDR
)
3990 /* This is a simple floor() */
3991 unsigned int mask_size
= shader_glsl_get_write_mask_size(write_mask
);
3992 if (mask_size
> 1) {
3993 shader_addline(buffer
, "ivec%d(floor(%s)));\n", mask_size
, src0_param
.param_str
);
3995 shader_addline(buffer
, "int(floor(%s)));\n", src0_param
.param_str
);
3998 else if (ins
->handler_idx
== WINED3DSIH_MOVA
)
4000 unsigned int mask_size
= shader_glsl_get_write_mask_size(write_mask
);
4002 if (shader_glsl_get_version(gl_info
) >= 130 || gl_info
->supported
[EXT_GPU_SHADER4
])
4005 shader_addline(buffer
, "ivec%d(round(%s)));\n", mask_size
, src0_param
.param_str
);
4007 shader_addline(buffer
, "int(round(%s)));\n", src0_param
.param_str
);
4012 shader_addline(buffer
, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n",
4013 mask_size
, src0_param
.param_str
, mask_size
, src0_param
.param_str
);
4015 shader_addline(buffer
, "int(floor(abs(%s) + 0.5) * sign(%s)));\n",
4016 src0_param
.param_str
, src0_param
.param_str
);
4021 shader_addline(buffer
, "%s);\n", src0_param
.param_str
);
4025 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
4026 static void shader_glsl_dot(const struct wined3d_shader_instruction
*ins
)
4028 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
4029 struct glsl_src_param src0_param
;
4030 struct glsl_src_param src1_param
;
4031 DWORD dst_write_mask
, src_write_mask
;
4032 unsigned int dst_size
;
4034 dst_write_mask
= shader_glsl_append_dst(buffer
, ins
);
4035 dst_size
= shader_glsl_get_write_mask_size(dst_write_mask
);
4037 /* dp4 works on vec4, dp3 on vec3, etc. */
4038 if (ins
->handler_idx
== WINED3DSIH_DP4
)
4039 src_write_mask
= WINED3DSP_WRITEMASK_ALL
;
4040 else if (ins
->handler_idx
== WINED3DSIH_DP3
)
4041 src_write_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
4043 src_write_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
;
4045 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_write_mask
, &src0_param
);
4046 shader_glsl_add_src_param(ins
, &ins
->src
[1], src_write_mask
, &src1_param
);
4049 shader_addline(buffer
, "vec%d(dot(%s, %s)));\n", dst_size
, src0_param
.param_str
, src1_param
.param_str
);
4051 shader_addline(buffer
, "dot(%s, %s));\n", src0_param
.param_str
, src1_param
.param_str
);
4055 /* Note that this instruction has some restrictions. The destination write mask
4056 * can't contain the w component, and the source swizzles have to be .xyzw */
4057 static void shader_glsl_cross(const struct wined3d_shader_instruction
*ins
)
4059 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
4060 struct glsl_src_param src0_param
;
4061 struct glsl_src_param src1_param
;
4064 shader_glsl_get_write_mask(&ins
->dst
[0], dst_mask
);
4065 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
4066 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
4067 shader_glsl_add_src_param(ins
, &ins
->src
[1], src_mask
, &src1_param
);
4068 shader_addline(ins
->ctx
->buffer
, "cross(%s, %s)%s);\n", src0_param
.param_str
, src1_param
.param_str
, dst_mask
);
4071 static void shader_glsl_cut(const struct wined3d_shader_instruction
*ins
)
4073 unsigned int stream
= ins
->handler_idx
== WINED3DSIH_CUT
? 0 : ins
->src
[0].reg
.idx
[0].offset
;
4076 shader_addline(ins
->ctx
->buffer
, "EndPrimitive();\n");
4078 FIXME("Unhandled primitive stream %u.\n", stream
);
4081 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
4082 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
4083 * GLSL uses the value as-is. */
4084 static void shader_glsl_pow(const struct wined3d_shader_instruction
*ins
)
4086 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
4087 struct glsl_src_param src0_param
;
4088 struct glsl_src_param src1_param
;
4089 DWORD dst_write_mask
;
4090 unsigned int dst_size
;
4092 dst_write_mask
= shader_glsl_append_dst(buffer
, ins
);
4093 dst_size
= shader_glsl_get_write_mask_size(dst_write_mask
);
4095 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
4096 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_0
, &src1_param
);
4100 shader_addline(buffer
, "vec%u(%s == 0.0 ? 1.0 : pow(abs(%s), %s)));\n",
4101 dst_size
, src1_param
.param_str
, src0_param
.param_str
, src1_param
.param_str
);
4105 shader_addline(buffer
, "%s == 0.0 ? 1.0 : pow(abs(%s), %s));\n",
4106 src1_param
.param_str
, src0_param
.param_str
, src1_param
.param_str
);
4110 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
4111 static void shader_glsl_map2gl(const struct wined3d_shader_instruction
*ins
)
4113 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
4114 struct glsl_src_param src_param
;
4115 const char *instruction
;
4119 /* Determine the GLSL function to use based on the opcode */
4120 /* TODO: Possibly make this a table for faster lookups */
4121 switch (ins
->handler_idx
)
4123 case WINED3DSIH_ABS
: instruction
= "abs"; break;
4124 case WINED3DSIH_BFREV
: instruction
= "bitfieldReverse"; break;
4125 case WINED3DSIH_COUNTBITS
: instruction
= "bitCount"; break;
4126 case WINED3DSIH_DSX
: instruction
= "dFdx"; break;
4127 case WINED3DSIH_DSX_COARSE
: instruction
= "dFdxCoarse"; break;
4128 case WINED3DSIH_DSX_FINE
: instruction
= "dFdxFine"; break;
4129 case WINED3DSIH_DSY
: instruction
= "ycorrection.y * dFdy"; break;
4130 case WINED3DSIH_DSY_COARSE
: instruction
= "ycorrection.y * dFdyCoarse"; break;
4131 case WINED3DSIH_DSY_FINE
: instruction
= "ycorrection.y * dFdyFine"; break;
4132 case WINED3DSIH_FIRSTBIT_HI
: instruction
= "findMSB"; break;
4133 case WINED3DSIH_FIRSTBIT_LO
: instruction
= "findLSB"; break;
4134 case WINED3DSIH_FIRSTBIT_SHI
: instruction
= "findMSB"; break;
4135 case WINED3DSIH_FRC
: instruction
= "fract"; break;
4136 case WINED3DSIH_IMAX
: instruction
= "max"; break;
4137 case WINED3DSIH_IMIN
: instruction
= "min"; break;
4138 case WINED3DSIH_MAX
: instruction
= "max"; break;
4139 case WINED3DSIH_MIN
: instruction
= "min"; break;
4140 case WINED3DSIH_ROUND_NE
: instruction
= "roundEven"; break;
4141 case WINED3DSIH_ROUND_NI
: instruction
= "floor"; break;
4142 case WINED3DSIH_ROUND_PI
: instruction
= "ceil"; break;
4143 case WINED3DSIH_ROUND_Z
: instruction
= "trunc"; break;
4144 case WINED3DSIH_SQRT
: instruction
= "sqrt"; break;
4145 case WINED3DSIH_UMAX
: instruction
= "max"; break;
4146 case WINED3DSIH_UMIN
: instruction
= "min"; break;
4147 default: instruction
= "";
4148 ERR("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins
->handler_idx
));
4152 write_mask
= shader_glsl_append_dst(buffer
, ins
);
4154 /* In D3D bits are numbered from the most significant bit. */
4155 if (ins
->handler_idx
== WINED3DSIH_FIRSTBIT_HI
|| ins
->handler_idx
== WINED3DSIH_FIRSTBIT_SHI
)
4156 shader_addline(buffer
, "31 - ");
4157 shader_addline(buffer
, "%s(", instruction
);
4161 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src_param
);
4162 shader_addline(buffer
, "%s", src_param
.param_str
);
4163 for (i
= 1; i
< ins
->src_count
; ++i
)
4165 shader_glsl_add_src_param(ins
, &ins
->src
[i
], write_mask
, &src_param
);
4166 shader_addline(buffer
, ", %s", src_param
.param_str
);
4170 shader_addline(buffer
, "));\n");
4173 static void shader_glsl_float16(const struct wined3d_shader_instruction
*ins
)
4175 struct wined3d_shader_dst_param dst
;
4176 struct glsl_src_param src
;
4181 fmt
= ins
->handler_idx
== WINED3DSIH_F16TOF32
4182 ? "unpackHalf2x16(%s).x);\n" : "packHalf2x16(vec2(%s, 0.0)));\n";
4185 for (i
= 0; i
< 4; ++i
)
4187 dst
.write_mask
= ins
->dst
[0].write_mask
& (WINED3DSP_WRITEMASK_0
<< i
);
4188 if (!(write_mask
= shader_glsl_append_dst_ext(ins
->ctx
->buffer
, ins
,
4189 &dst
, dst
.reg
.data_type
)))
4192 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src
);
4193 shader_addline(ins
->ctx
->buffer
, fmt
, src
.param_str
);
4197 static void shader_glsl_bitwise_op(const struct wined3d_shader_instruction
*ins
)
4199 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
4200 struct wined3d_shader_dst_param dst
;
4201 struct glsl_src_param src
[4];
4202 const char *instruction
;
4203 BOOL tmp_dst
= FALSE
;
4208 switch (ins
->handler_idx
)
4210 case WINED3DSIH_BFI
: instruction
= "bitfieldInsert"; break;
4211 case WINED3DSIH_IBFE
: instruction
= "bitfieldExtract"; break;
4212 case WINED3DSIH_UBFE
: instruction
= "bitfieldExtract"; break;
4214 ERR("Unhandled opcode %#x.\n", ins
->handler_idx
);
4218 for (i
= 0; i
< ins
->src_count
; ++i
)
4220 if (ins
->dst
[0].reg
.idx
[0].offset
== ins
->src
[i
].reg
.idx
[0].offset
4221 && ins
->dst
[0].reg
.type
== ins
->src
[i
].reg
.type
)
4226 for (i
= 0; i
< 4; ++i
)
4228 dst
.write_mask
= ins
->dst
[0].write_mask
& (WINED3DSP_WRITEMASK_0
<< i
);
4229 if (tmp_dst
&& (write_mask
= shader_glsl_get_write_mask(&dst
, mask_char
)))
4230 shader_addline(buffer
, "tmp0%s = %sBitsToFloat(", mask_char
,
4231 dst
.reg
.data_type
== WINED3D_DATA_INT
? "int" : "uint");
4232 else if (!(write_mask
= shader_glsl_append_dst_ext(buffer
, ins
, &dst
, dst
.reg
.data_type
)))
4235 for (j
= 0; j
< ins
->src_count
; ++j
)
4236 shader_glsl_add_src_param(ins
, &ins
->src
[j
], write_mask
, &src
[j
]);
4237 shader_addline(buffer
, "%s(", instruction
);
4238 for (j
= 0; j
< ins
->src_count
- 2; ++j
)
4239 shader_addline(buffer
, "%s, ", src
[ins
->src_count
- j
- 1].param_str
);
4240 shader_addline(buffer
, "%s & 0x1f, %s & 0x1f));\n", src
[1].param_str
, src
[0].param_str
);
4245 shader_glsl_append_dst_ext(buffer
, ins
, &ins
->dst
[0], WINED3D_DATA_FLOAT
);
4246 shader_glsl_get_write_mask(&ins
->dst
[0], mask_char
);
4247 shader_addline(buffer
, "tmp0%s);\n", mask_char
);
4251 static void shader_glsl_nop(const struct wined3d_shader_instruction
*ins
) {}
4253 static void shader_glsl_nrm(const struct wined3d_shader_instruction
*ins
)
4255 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
4256 struct glsl_src_param src_param
;
4257 unsigned int mask_size
;
4261 write_mask
= shader_glsl_get_write_mask(ins
->dst
, dst_mask
);
4262 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
4263 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src_param
);
4265 shader_addline(buffer
, "tmp0.x = dot(%s, %s);\n",
4266 src_param
.param_str
, src_param
.param_str
);
4267 shader_glsl_append_dst(buffer
, ins
);
4271 shader_addline(buffer
, "tmp0.x == 0.0 ? vec%u(0.0) : (%s * inversesqrt(tmp0.x)));\n",
4272 mask_size
, src_param
.param_str
);
4276 shader_addline(buffer
, "tmp0.x == 0.0 ? 0.0 : (%s * inversesqrt(tmp0.x)));\n",
4277 src_param
.param_str
);
4281 static void shader_glsl_scalar_op(const struct wined3d_shader_instruction
*ins
)
4283 DWORD shader_version
= WINED3D_SHADER_VERSION(ins
->ctx
->reg_maps
->shader_version
.major
,
4284 ins
->ctx
->reg_maps
->shader_version
.minor
);
4285 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
4286 struct glsl_src_param src0_param
;
4287 const char *prefix
, *suffix
;
4288 unsigned int dst_size
;
4289 DWORD dst_write_mask
;
4291 dst_write_mask
= shader_glsl_append_dst(buffer
, ins
);
4292 dst_size
= shader_glsl_get_write_mask_size(dst_write_mask
);
4294 if (shader_version
< WINED3D_SHADER_VERSION(4, 0))
4295 dst_write_mask
= WINED3DSP_WRITEMASK_3
;
4297 shader_glsl_add_src_param(ins
, &ins
->src
[0], dst_write_mask
, &src0_param
);
4299 switch (ins
->handler_idx
)
4301 case WINED3DSIH_EXP
:
4302 case WINED3DSIH_EXPP
:
4307 case WINED3DSIH_LOG
:
4308 case WINED3DSIH_LOGP
:
4309 prefix
= "log2(abs(";
4313 case WINED3DSIH_RCP
:
4318 case WINED3DSIH_RSQ
:
4319 prefix
= "inversesqrt(abs(";
4326 FIXME("Unhandled instruction %#x.\n", ins
->handler_idx
);
4330 if (dst_size
> 1 && shader_version
< WINED3D_SHADER_VERSION(4, 0))
4331 shader_addline(buffer
, "vec%u(%s%s%s));\n", dst_size
, prefix
, src0_param
.param_str
, suffix
);
4333 shader_addline(buffer
, "%s%s%s);\n", prefix
, src0_param
.param_str
, suffix
);
4336 /** Process the WINED3DSIO_EXPP instruction in GLSL:
4337 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
4338 * dst.x = 2^(floor(src))
4339 * dst.y = src - floor(src)
4340 * dst.z = 2^src (partial precision is allowed, but optional)
4342 * For 2.0 shaders, just do this (honoring writemask and swizzle):
4343 * dst = 2^src; (partial precision is allowed, but optional)
4345 static void shader_glsl_expp(const struct wined3d_shader_instruction
*ins
)
4347 if (ins
->ctx
->reg_maps
->shader_version
.major
< 2)
4349 struct glsl_src_param src_param
;
4352 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_3
, &src_param
);
4354 shader_addline(ins
->ctx
->buffer
, "tmp0.x = exp2(floor(%s));\n", src_param
.param_str
);
4355 shader_addline(ins
->ctx
->buffer
, "tmp0.y = %s - floor(%s);\n", src_param
.param_str
, src_param
.param_str
);
4356 shader_addline(ins
->ctx
->buffer
, "tmp0.z = exp2(%s);\n", src_param
.param_str
);
4357 shader_addline(ins
->ctx
->buffer
, "tmp0.w = 1.0;\n");
4359 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
4360 shader_glsl_get_write_mask(&ins
->dst
[0], dst_mask
);
4361 shader_addline(ins
->ctx
->buffer
, "tmp0%s);\n", dst_mask
);
4365 shader_glsl_scalar_op(ins
);
4368 static void shader_glsl_cast(const struct wined3d_shader_instruction
*ins
,
4369 const char *vector_constructor
, const char *scalar_constructor
)
4371 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
4372 struct glsl_src_param src_param
;
4373 unsigned int mask_size
;
4376 write_mask
= shader_glsl_append_dst(buffer
, ins
);
4377 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
4378 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src_param
);
4381 shader_addline(buffer
, "%s%u(%s));\n", vector_constructor
, mask_size
, src_param
.param_str
);
4383 shader_addline(buffer
, "%s(%s));\n", scalar_constructor
, src_param
.param_str
);
4386 static void shader_glsl_to_int(const struct wined3d_shader_instruction
*ins
)
4388 shader_glsl_cast(ins
, "ivec", "int");
4391 static void shader_glsl_to_uint(const struct wined3d_shader_instruction
*ins
)
4393 shader_glsl_cast(ins
, "uvec", "uint");
4396 static void shader_glsl_to_float(const struct wined3d_shader_instruction
*ins
)
4398 shader_glsl_cast(ins
, "vec", "float");
4401 /** Process signed comparison opcodes in GLSL. */
4402 static void shader_glsl_compare(const struct wined3d_shader_instruction
*ins
)
4404 struct glsl_src_param src0_param
;
4405 struct glsl_src_param src1_param
;
4407 unsigned int mask_size
;
4409 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
4410 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
4411 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
4412 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
4414 if (mask_size
> 1) {
4415 const char *compare
;
4417 switch(ins
->handler_idx
)
4419 case WINED3DSIH_SLT
: compare
= "lessThan"; break;
4420 case WINED3DSIH_SGE
: compare
= "greaterThanEqual"; break;
4421 default: compare
= "";
4422 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins
->handler_idx
));
4425 shader_addline(ins
->ctx
->buffer
, "vec%d(%s(%s, %s)));\n", mask_size
, compare
,
4426 src0_param
.param_str
, src1_param
.param_str
);
4428 switch(ins
->handler_idx
)
4430 case WINED3DSIH_SLT
:
4431 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
4432 * to return 0.0 but step returns 1.0 because step is not < x
4433 * An alternative is a bvec compare padded with an unused second component.
4434 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
4435 * issue. Playing with not() is not possible either because not() does not accept
4438 shader_addline(ins
->ctx
->buffer
, "(%s < %s) ? 1.0 : 0.0);\n",
4439 src0_param
.param_str
, src1_param
.param_str
);
4441 case WINED3DSIH_SGE
:
4442 /* Here we can use the step() function and safe a conditional */
4443 shader_addline(ins
->ctx
->buffer
, "step(%s, %s));\n", src1_param
.param_str
, src0_param
.param_str
);
4446 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins
->handler_idx
));
4452 static void shader_glsl_swapc(const struct wined3d_shader_instruction
*ins
)
4454 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
4455 struct wined3d_shader_dst_param dst
[2];
4456 struct glsl_src_param src
[3];
4457 unsigned int i
, j
, k
;
4462 for (i
= 0; i
< ins
->dst_count
; ++i
)
4465 for (j
= 0; j
< ins
->src_count
; ++j
)
4467 if (ins
->dst
[i
].reg
.idx
[0].offset
== ins
->src
[j
].reg
.idx
[0].offset
4468 && ins
->dst
[i
].reg
.type
== ins
->src
[j
].reg
.type
)
4473 dst
[0] = ins
->dst
[0];
4474 dst
[1] = ins
->dst
[1];
4475 for (i
= 0; i
< 4; ++i
)
4477 for (j
= 0; j
< ARRAY_SIZE(dst
); ++j
)
4479 dst
[j
].write_mask
= ins
->dst
[j
].write_mask
& (WINED3DSP_WRITEMASK_0
<< i
);
4480 if (tmp_dst
[j
] && (write_mask
= shader_glsl_get_write_mask(&dst
[j
], mask_char
)))
4481 shader_addline(buffer
, "tmp%u%s = (", j
, mask_char
);
4482 else if (!(write_mask
= shader_glsl_append_dst_ext(buffer
, ins
, &dst
[j
], dst
[j
].reg
.data_type
)))
4485 for (k
= 0; k
< ARRAY_SIZE(src
); ++k
)
4486 shader_glsl_add_src_param(ins
, &ins
->src
[k
], write_mask
, &src
[k
]);
4488 shader_addline(buffer
, "%sbool(%s) ? %s : %s);\n", !j
? "!" : "",
4489 src
[0].param_str
, src
[1].param_str
, src
[2].param_str
);
4493 for (i
= 0; i
< ARRAY_SIZE(tmp_dst
); ++i
)
4497 shader_glsl_get_write_mask(&ins
->dst
[i
], mask_char
);
4498 shader_glsl_append_dst_ext(buffer
, ins
, &ins
->dst
[i
], ins
->dst
[i
].reg
.data_type
);
4499 shader_addline(buffer
, "tmp%u%s);\n", i
, mask_char
);
4504 static void shader_glsl_conditional_move(const struct wined3d_shader_instruction
*ins
)
4506 const char *condition_prefix
, *condition_suffix
;
4507 struct wined3d_shader_dst_param dst
;
4508 struct glsl_src_param src0_param
;
4509 struct glsl_src_param src1_param
;
4510 struct glsl_src_param src2_param
;
4511 BOOL temp_destination
= FALSE
;
4512 DWORD cmp_channel
= 0;
4517 switch (ins
->handler_idx
)
4519 case WINED3DSIH_CMP
:
4520 condition_prefix
= "";
4521 condition_suffix
= " >= 0.0";
4524 case WINED3DSIH_CND
:
4525 condition_prefix
= "";
4526 condition_suffix
= " > 0.5";
4529 case WINED3DSIH_MOVC
:
4530 condition_prefix
= "bool(";
4531 condition_suffix
= ")";
4535 FIXME("Unhandled instruction %#x.\n", ins
->handler_idx
);
4536 condition_prefix
= "<unhandled prefix>";
4537 condition_suffix
= "<unhandled suffix>";
4541 if (shader_is_scalar(&ins
->dst
[0].reg
) || shader_is_scalar(&ins
->src
[0].reg
))
4543 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
4544 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
4545 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
4546 shader_glsl_add_src_param(ins
, &ins
->src
[2], write_mask
, &src2_param
);
4548 shader_addline(ins
->ctx
->buffer
, "%s%s%s ? %s : %s);\n",
4549 condition_prefix
, src0_param
.param_str
, condition_suffix
,
4550 src1_param
.param_str
, src2_param
.param_str
);
4556 /* Splitting the instruction up in multiple lines imposes a problem:
4557 * The first lines may overwrite source parameters of the following lines.
4558 * Deal with that by using a temporary destination register if needed. */
4559 if ((ins
->src
[0].reg
.idx
[0].offset
== dst
.reg
.idx
[0].offset
4560 && ins
->src
[0].reg
.type
== dst
.reg
.type
)
4561 || (ins
->src
[1].reg
.idx
[0].offset
== dst
.reg
.idx
[0].offset
4562 && ins
->src
[1].reg
.type
== dst
.reg
.type
)
4563 || (ins
->src
[2].reg
.idx
[0].offset
== dst
.reg
.idx
[0].offset
4564 && ins
->src
[2].reg
.type
== dst
.reg
.type
))
4565 temp_destination
= TRUE
;
4567 /* Cycle through all source0 channels. */
4568 for (i
= 0; i
< 4; ++i
)
4571 /* Find the destination channels which use the current source0 channel. */
4572 for (j
= 0; j
< 4; ++j
)
4574 if (shader_glsl_swizzle_get_component(ins
->src
[0].swizzle
, j
) == i
)
4576 write_mask
|= WINED3DSP_WRITEMASK_0
<< j
;
4577 cmp_channel
= WINED3DSP_WRITEMASK_0
<< j
;
4580 dst
.write_mask
= ins
->dst
[0].write_mask
& write_mask
;
4582 if (temp_destination
)
4584 if (!(write_mask
= shader_glsl_get_write_mask(&dst
, mask_char
)))
4586 shader_addline(ins
->ctx
->buffer
, "tmp0%s = (", mask_char
);
4588 else if (!(write_mask
= shader_glsl_append_dst_ext(ins
->ctx
->buffer
, ins
, &dst
, dst
.reg
.data_type
)))
4591 shader_glsl_add_src_param(ins
, &ins
->src
[0], cmp_channel
, &src0_param
);
4592 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
4593 shader_glsl_add_src_param(ins
, &ins
->src
[2], write_mask
, &src2_param
);
4595 shader_addline(ins
->ctx
->buffer
, "%s%s%s ? %s : %s);\n",
4596 condition_prefix
, src0_param
.param_str
, condition_suffix
,
4597 src1_param
.param_str
, src2_param
.param_str
);
4600 if (temp_destination
)
4602 shader_glsl_get_write_mask(&ins
->dst
[0], mask_char
);
4603 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
4604 shader_addline(ins
->ctx
->buffer
, "tmp0%s);\n", mask_char
);
4608 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
4609 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
4610 * the compare is done per component of src0. */
4611 static void shader_glsl_cnd(const struct wined3d_shader_instruction
*ins
)
4613 struct glsl_src_param src0_param
;
4614 struct glsl_src_param src1_param
;
4615 struct glsl_src_param src2_param
;
4617 DWORD shader_version
= WINED3D_SHADER_VERSION(ins
->ctx
->reg_maps
->shader_version
.major
,
4618 ins
->ctx
->reg_maps
->shader_version
.minor
);
4620 if (shader_version
< WINED3D_SHADER_VERSION(1, 4))
4622 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
4623 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
4624 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
4625 shader_glsl_add_src_param(ins
, &ins
->src
[2], write_mask
, &src2_param
);
4627 if (ins
->coissue
&& ins
->dst
->write_mask
!= WINED3DSP_WRITEMASK_3
)
4628 shader_addline(ins
->ctx
->buffer
, "%s /* COISSUE! */);\n", src1_param
.param_str
);
4630 shader_addline(ins
->ctx
->buffer
, "%s > 0.5 ? %s : %s);\n",
4631 src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
4635 shader_glsl_conditional_move(ins
);
4638 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
4639 static void shader_glsl_mad(const struct wined3d_shader_instruction
*ins
)
4641 struct glsl_src_param src0_param
;
4642 struct glsl_src_param src1_param
;
4643 struct glsl_src_param src2_param
;
4646 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
4647 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
4648 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
4649 shader_glsl_add_src_param(ins
, &ins
->src
[2], write_mask
, &src2_param
);
4650 shader_addline(ins
->ctx
->buffer
, "(%s * %s) + %s);\n",
4651 src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
4654 /* Handles transforming all WINED3DSIO_M?x? opcodes for
4655 Vertex shaders to GLSL codes */
4656 static void shader_glsl_mnxn(const struct wined3d_shader_instruction
*ins
)
4659 int nComponents
= 0;
4660 struct wined3d_shader_dst_param tmp_dst
= {{0}};
4661 struct wined3d_shader_src_param tmp_src
[2] = {{{0}}};
4662 struct wined3d_shader_instruction tmp_ins
;
4664 memset(&tmp_ins
, 0, sizeof(tmp_ins
));
4666 /* Set constants for the temporary argument */
4667 tmp_ins
.ctx
= ins
->ctx
;
4668 tmp_ins
.dst_count
= 1;
4669 tmp_ins
.dst
= &tmp_dst
;
4670 tmp_ins
.src_count
= 2;
4671 tmp_ins
.src
= tmp_src
;
4673 switch(ins
->handler_idx
)
4675 case WINED3DSIH_M4x4
:
4677 tmp_ins
.handler_idx
= WINED3DSIH_DP4
;
4679 case WINED3DSIH_M4x3
:
4681 tmp_ins
.handler_idx
= WINED3DSIH_DP4
;
4683 case WINED3DSIH_M3x4
:
4685 tmp_ins
.handler_idx
= WINED3DSIH_DP3
;
4687 case WINED3DSIH_M3x3
:
4689 tmp_ins
.handler_idx
= WINED3DSIH_DP3
;
4691 case WINED3DSIH_M3x2
:
4693 tmp_ins
.handler_idx
= WINED3DSIH_DP3
;
4699 tmp_dst
= ins
->dst
[0];
4700 tmp_src
[0] = ins
->src
[0];
4701 tmp_src
[1] = ins
->src
[1];
4702 for (i
= 0; i
< nComponents
; ++i
)
4704 tmp_dst
.write_mask
= WINED3DSP_WRITEMASK_0
<< i
;
4705 shader_glsl_dot(&tmp_ins
);
4706 ++tmp_src
[1].reg
.idx
[0].offset
;
4711 The LRP instruction performs a component-wise linear interpolation
4712 between the second and third operands using the first operand as the
4713 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
4714 This is equivalent to mix(src2, src1, src0);
4716 static void shader_glsl_lrp(const struct wined3d_shader_instruction
*ins
)
4718 struct glsl_src_param src0_param
;
4719 struct glsl_src_param src1_param
;
4720 struct glsl_src_param src2_param
;
4723 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
4725 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
4726 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
4727 shader_glsl_add_src_param(ins
, &ins
->src
[2], write_mask
, &src2_param
);
4729 shader_addline(ins
->ctx
->buffer
, "mix(%s, %s, %s));\n",
4730 src2_param
.param_str
, src1_param
.param_str
, src0_param
.param_str
);
4733 /** Process the WINED3DSIO_LIT instruction in GLSL:
4734 * dst.x = dst.w = 1.0
4735 * dst.y = (src0.x > 0) ? src0.x
4736 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
4737 * where src.w is clamped at +- 128
4739 static void shader_glsl_lit(const struct wined3d_shader_instruction
*ins
)
4741 struct glsl_src_param src0_param
;
4742 struct glsl_src_param src1_param
;
4743 struct glsl_src_param src3_param
;
4746 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
4747 shader_glsl_get_write_mask(&ins
->dst
[0], dst_mask
);
4749 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
4750 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_1
, &src1_param
);
4751 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_3
, &src3_param
);
4753 /* The sdk specifies the instruction like this
4755 * if(src.x > 0.0) dst.y = src.x
4757 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
4760 * (where power = src.w clamped between -128 and 128)
4762 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
4763 * dst.x = 1.0 ... No further explanation needed
4764 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
4765 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
4766 * dst.w = 1.0. ... Nothing fancy.
4768 * So we still have one conditional in there. So do this:
4769 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
4771 * 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),
4772 * 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.
4773 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to.
4775 * Unfortunately pow(0.0 ^ 0.0) returns NaN on most GPUs, but lit with src.y = 0 and src.w = 0 returns
4776 * a non-NaN value in dst.z. What we return doesn't matter, as long as it is not NaN. Return 0, which is
4777 * what all Windows HW drivers and GL_ARB_vertex_program's LIT do.
4779 shader_addline(ins
->ctx
->buffer
,
4780 "vec4(1.0, max(%s, 0.0), %s == 0.0 ? 0.0 : "
4781 "pow(max(0.0, %s) * step(0.0, %s), clamp(%s, -128.0, 128.0)), 1.0)%s);\n",
4782 src0_param
.param_str
, src3_param
.param_str
, src1_param
.param_str
,
4783 src0_param
.param_str
, src3_param
.param_str
, dst_mask
);
4786 /** Process the WINED3DSIO_DST instruction in GLSL:
4788 * dst.y = src0.x * src0.y
4792 static void shader_glsl_dst(const struct wined3d_shader_instruction
*ins
)
4794 struct glsl_src_param src0y_param
;
4795 struct glsl_src_param src0z_param
;
4796 struct glsl_src_param src1y_param
;
4797 struct glsl_src_param src1w_param
;
4800 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
4801 shader_glsl_get_write_mask(&ins
->dst
[0], dst_mask
);
4803 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_1
, &src0y_param
);
4804 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_2
, &src0z_param
);
4805 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_1
, &src1y_param
);
4806 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_3
, &src1w_param
);
4808 shader_addline(ins
->ctx
->buffer
, "vec4(1.0, %s * %s, %s, %s))%s;\n",
4809 src0y_param
.param_str
, src1y_param
.param_str
, src0z_param
.param_str
, src1w_param
.param_str
, dst_mask
);
4812 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
4813 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
4814 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
4816 * dst.x = cos(src0.?)
4817 * dst.y = sin(src0.?)
4821 static void shader_glsl_sincos(const struct wined3d_shader_instruction
*ins
)
4823 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
4824 struct glsl_src_param src0_param
;
4827 if (ins
->ctx
->reg_maps
->shader_version
.major
< 4)
4829 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
4831 write_mask
= shader_glsl_append_dst(buffer
, ins
);
4834 case WINED3DSP_WRITEMASK_0
:
4835 shader_addline(buffer
, "cos(%s));\n", src0_param
.param_str
);
4838 case WINED3DSP_WRITEMASK_1
:
4839 shader_addline(buffer
, "sin(%s));\n", src0_param
.param_str
);
4842 case (WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
):
4843 shader_addline(buffer
, "vec2(cos(%s), sin(%s)));\n",
4844 src0_param
.param_str
, src0_param
.param_str
);
4848 ERR("Write mask should be .x, .y or .xy\n");
4855 if (ins
->dst
[0].reg
.type
!= WINED3DSPR_NULL
)
4858 if (ins
->dst
[1].reg
.type
!= WINED3DSPR_NULL
)
4862 write_mask
= shader_glsl_get_write_mask(&ins
->dst
[0], dst_mask
);
4863 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
4864 shader_addline(buffer
, "tmp0%s = sin(%s);\n", dst_mask
, src0_param
.param_str
);
4866 write_mask
= shader_glsl_append_dst_ext(buffer
, ins
, &ins
->dst
[1], ins
->dst
[1].reg
.data_type
);
4867 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
4868 shader_addline(buffer
, "cos(%s));\n", src0_param
.param_str
);
4870 shader_glsl_append_dst_ext(buffer
, ins
, &ins
->dst
[0], ins
->dst
[0].reg
.data_type
);
4871 shader_addline(buffer
, "tmp0%s);\n", dst_mask
);
4875 write_mask
= shader_glsl_append_dst_ext(buffer
, ins
, &ins
->dst
[0], ins
->dst
[0].reg
.data_type
);
4876 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
4877 shader_addline(buffer
, "sin(%s));\n", src0_param
.param_str
);
4880 else if (ins
->dst
[1].reg
.type
!= WINED3DSPR_NULL
)
4882 write_mask
= shader_glsl_append_dst_ext(buffer
, ins
, &ins
->dst
[1], ins
->dst
[1].reg
.data_type
);
4883 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
4884 shader_addline(buffer
, "cos(%s));\n", src0_param
.param_str
);
4888 /* sgn in vs_2_0 has 2 extra parameters(registers for temporary storage) which we don't use
4889 * here. But those extra parameters require a dedicated function for sgn, since map2gl would
4890 * generate invalid code
4892 static void shader_glsl_sgn(const struct wined3d_shader_instruction
*ins
)
4894 struct glsl_src_param src0_param
;
4897 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
4898 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
4900 shader_addline(ins
->ctx
->buffer
, "sign(%s));\n", src0_param
.param_str
);
4903 /** Process the WINED3DSIO_LOOP instruction in GLSL:
4904 * Start a for() loop where src1.y is the initial value of aL,
4905 * increment aL by src1.z for a total of src1.x iterations.
4906 * Need to use a temporary variable for this operation.
4908 /* FIXME: I don't think nested loops will work correctly this way. */
4909 static void shader_glsl_loop(const struct wined3d_shader_instruction
*ins
)
4911 struct wined3d_shader_parser_state
*state
= ins
->ctx
->state
;
4912 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
4913 const struct wined3d_shader
*shader
= ins
->ctx
->shader
;
4914 const struct wined3d_shader_lconst
*constant
;
4915 struct glsl_src_param src1_param
;
4916 const DWORD
*control_values
= NULL
;
4918 if (ins
->ctx
->reg_maps
->shader_version
.major
< 4)
4920 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_ALL
, &src1_param
);
4922 /* Try to hardcode the loop control parameters if possible. Direct3D 9
4923 * class hardware doesn't support real varying indexing, but Microsoft
4924 * designed this feature for Shader model 2.x+. If the loop control is
4925 * known at compile time, the GLSL compiler can unroll the loop, and
4926 * replace indirect addressing with direct addressing. */
4927 if (ins
->src
[1].reg
.type
== WINED3DSPR_CONSTINT
)
4929 LIST_FOR_EACH_ENTRY(constant
, &shader
->constantsI
, struct wined3d_shader_lconst
, entry
)
4931 if (constant
->idx
== ins
->src
[1].reg
.idx
[0].offset
)
4933 control_values
= constant
->value
;
4941 struct wined3d_shader_loop_control loop_control
;
4942 loop_control
.count
= control_values
[0];
4943 loop_control
.start
= control_values
[1];
4944 loop_control
.step
= (int)control_values
[2];
4946 if (loop_control
.step
> 0)
4948 shader_addline(buffer
, "for (aL%u = %u; aL%u < (%u * %d + %u); aL%u += %d)\n{\n",
4949 state
->current_loop_depth
, loop_control
.start
,
4950 state
->current_loop_depth
, loop_control
.count
, loop_control
.step
, loop_control
.start
,
4951 state
->current_loop_depth
, loop_control
.step
);
4953 else if (loop_control
.step
< 0)
4955 shader_addline(buffer
, "for (aL%u = %u; aL%u > (%u * %d + %u); aL%u += %d)\n{\n",
4956 state
->current_loop_depth
, loop_control
.start
,
4957 state
->current_loop_depth
, loop_control
.count
, loop_control
.step
, loop_control
.start
,
4958 state
->current_loop_depth
, loop_control
.step
);
4962 shader_addline(buffer
, "for (aL%u = %u, tmpInt%u = 0; tmpInt%u < %u; tmpInt%u++)\n{\n",
4963 state
->current_loop_depth
, loop_control
.start
, state
->current_loop_depth
,
4964 state
->current_loop_depth
, loop_control
.count
,
4965 state
->current_loop_depth
);
4970 shader_addline(buffer
, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z)\n{\n",
4971 state
->current_loop_depth
, state
->current_loop_reg
,
4972 src1_param
.reg_name
, state
->current_loop_depth
, src1_param
.reg_name
,
4973 state
->current_loop_depth
, state
->current_loop_reg
, src1_param
.reg_name
);
4976 ++state
->current_loop_reg
;
4980 shader_addline(buffer
, "for (;;)\n{\n");
4983 ++state
->current_loop_depth
;
4986 static void shader_glsl_end(const struct wined3d_shader_instruction
*ins
)
4988 struct wined3d_shader_parser_state
*state
= ins
->ctx
->state
;
4990 shader_addline(ins
->ctx
->buffer
, "}\n");
4992 if (ins
->handler_idx
== WINED3DSIH_ENDLOOP
)
4994 --state
->current_loop_depth
;
4995 --state
->current_loop_reg
;
4998 if (ins
->handler_idx
== WINED3DSIH_ENDREP
)
5000 --state
->current_loop_depth
;
5004 static void shader_glsl_rep(const struct wined3d_shader_instruction
*ins
)
5006 struct wined3d_shader_parser_state
*state
= ins
->ctx
->state
;
5007 const struct wined3d_shader
*shader
= ins
->ctx
->shader
;
5008 const struct wined3d_shader_lconst
*constant
;
5009 struct glsl_src_param src0_param
;
5010 const DWORD
*control_values
= NULL
;
5012 /* Try to hardcode local values to help the GLSL compiler to unroll and optimize the loop */
5013 if (ins
->src
[0].reg
.type
== WINED3DSPR_CONSTINT
)
5015 LIST_FOR_EACH_ENTRY(constant
, &shader
->constantsI
, struct wined3d_shader_lconst
, entry
)
5017 if (constant
->idx
== ins
->src
[0].reg
.idx
[0].offset
)
5019 control_values
= constant
->value
;
5027 shader_addline(ins
->ctx
->buffer
, "for (tmpInt%d = 0; tmpInt%d < %d; tmpInt%d++) {\n",
5028 state
->current_loop_depth
, state
->current_loop_depth
,
5029 control_values
[0], state
->current_loop_depth
);
5033 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
5034 shader_addline(ins
->ctx
->buffer
, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
5035 state
->current_loop_depth
, state
->current_loop_depth
,
5036 src0_param
.param_str
, state
->current_loop_depth
);
5039 ++state
->current_loop_depth
;
5042 static void shader_glsl_switch(const struct wined3d_shader_instruction
*ins
)
5044 struct glsl_src_param src0_param
;
5046 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
5047 shader_addline(ins
->ctx
->buffer
, "switch (%s)\n{\n", src0_param
.param_str
);
5050 static void shader_glsl_case(const struct wined3d_shader_instruction
*ins
)
5052 struct glsl_src_param src0_param
;
5054 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
5055 shader_addline(ins
->ctx
->buffer
, "case %s:\n", src0_param
.param_str
);
5058 static void shader_glsl_default(const struct wined3d_shader_instruction
*ins
)
5060 shader_addline(ins
->ctx
->buffer
, "default:\n");
5063 static void shader_glsl_if(const struct wined3d_shader_instruction
*ins
)
5065 const char *condition
= (ins
->flags
== WINED3D_SHADER_CONDITIONAL_OP_NZ
) ? "bool" : "!bool";
5066 struct glsl_src_param src0_param
;
5068 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
5069 shader_addline(ins
->ctx
->buffer
, "if (%s(%s)) {\n", condition
, src0_param
.param_str
);
5072 static void shader_glsl_ifc(const struct wined3d_shader_instruction
*ins
)
5074 struct glsl_src_param src0_param
;
5075 struct glsl_src_param src1_param
;
5077 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
5078 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_0
, &src1_param
);
5080 shader_addline(ins
->ctx
->buffer
, "if (%s %s %s) {\n",
5081 src0_param
.param_str
, shader_glsl_get_rel_op(ins
->flags
), src1_param
.param_str
);
5084 static void shader_glsl_else(const struct wined3d_shader_instruction
*ins
)
5086 shader_addline(ins
->ctx
->buffer
, "} else {\n");
5089 static void shader_glsl_emit(const struct wined3d_shader_instruction
*ins
)
5091 unsigned int stream
= ins
->handler_idx
== WINED3DSIH_EMIT
? 0 : ins
->src
[0].reg
.idx
[0].offset
;
5093 shader_addline(ins
->ctx
->buffer
, "setup_gs_output(gs_out);\n");
5094 if (!ins
->ctx
->gl_info
->supported
[ARB_CLIP_CONTROL
])
5095 shader_glsl_fixup_position(ins
->ctx
->buffer
);
5098 shader_addline(ins
->ctx
->buffer
, "EmitVertex();\n");
5100 FIXME("Unhandled primitive stream %u.\n", stream
);
5103 static void shader_glsl_break(const struct wined3d_shader_instruction
*ins
)
5105 shader_addline(ins
->ctx
->buffer
, "break;\n");
5108 /* FIXME: According to MSDN the compare is done per component. */
5109 static void shader_glsl_breakc(const struct wined3d_shader_instruction
*ins
)
5111 struct glsl_src_param src0_param
;
5112 struct glsl_src_param src1_param
;
5114 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
5115 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_0
, &src1_param
);
5117 shader_addline(ins
->ctx
->buffer
, "if (%s %s %s) break;\n",
5118 src0_param
.param_str
, shader_glsl_get_rel_op(ins
->flags
), src1_param
.param_str
);
5121 static void shader_glsl_conditional_op(const struct wined3d_shader_instruction
*ins
)
5123 const char *condition
= (ins
->flags
== WINED3D_SHADER_CONDITIONAL_OP_NZ
) ? "bool" : "!bool";
5124 struct glsl_src_param src_param
;
5127 switch (ins
->handler_idx
)
5129 case WINED3DSIH_BREAKP
: op
= "break"; break;
5130 case WINED3DSIH_CONTINUEP
: op
= "continue"; break;
5131 case WINED3DSIH_RETP
: op
= "return"; break;
5133 ERR("Unhandled opcode %#x.\n", ins
->handler_idx
);
5137 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src_param
);
5138 shader_addline(ins
->ctx
->buffer
, "if (%s(%s)) %s;\n", condition
, src_param
.param_str
, op
);
5141 static void shader_glsl_continue(const struct wined3d_shader_instruction
*ins
)
5143 shader_addline(ins
->ctx
->buffer
, "continue;\n");
5146 static void shader_glsl_label(const struct wined3d_shader_instruction
*ins
)
5148 shader_addline(ins
->ctx
->buffer
, "}\n");
5149 shader_addline(ins
->ctx
->buffer
, "void subroutine%u()\n{\n", ins
->src
[0].reg
.idx
[0].offset
);
5151 /* Subroutines appear at the end of the shader. */
5152 ins
->ctx
->state
->in_subroutine
= TRUE
;
5155 static void shader_glsl_call(const struct wined3d_shader_instruction
*ins
)
5157 shader_addline(ins
->ctx
->buffer
, "subroutine%u();\n", ins
->src
[0].reg
.idx
[0].offset
);
5160 static void shader_glsl_callnz(const struct wined3d_shader_instruction
*ins
)
5162 struct glsl_src_param src1_param
;
5164 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_0
, &src1_param
);
5165 shader_addline(ins
->ctx
->buffer
, "if (%s) subroutine%u();\n",
5166 src1_param
.param_str
, ins
->src
[0].reg
.idx
[0].offset
);
5169 static void shader_glsl_ret(const struct wined3d_shader_instruction
*ins
)
5171 const struct wined3d_shader_version
*version
= &ins
->ctx
->shader
->reg_maps
.shader_version
;
5173 if (version
->major
>= 4 && !ins
->ctx
->state
->in_subroutine
)
5175 shader_glsl_generate_shader_epilogue(ins
->ctx
);
5176 shader_addline(ins
->ctx
->buffer
, "return;\n");
5180 static void shader_glsl_tex(const struct wined3d_shader_instruction
*ins
)
5182 DWORD shader_version
= WINED3D_SHADER_VERSION(ins
->ctx
->reg_maps
->shader_version
.major
,
5183 ins
->ctx
->reg_maps
->shader_version
.minor
);
5184 struct glsl_sample_function sample_function
;
5185 DWORD sample_flags
= 0;
5187 DWORD mask
= 0, swizzle
;
5188 const struct shader_glsl_ctx_priv
*priv
= ins
->ctx
->backend_data
;
5190 /* 1.0-1.4: Use destination register as sampler source.
5191 * 2.0+: Use provided sampler source. */
5192 if (shader_version
< WINED3D_SHADER_VERSION(2,0))
5193 resource_idx
= ins
->dst
[0].reg
.idx
[0].offset
;
5195 resource_idx
= ins
->src
[1].reg
.idx
[0].offset
;
5197 if (shader_version
< WINED3D_SHADER_VERSION(1,4))
5199 DWORD flags
= (priv
->cur_ps_args
->tex_transform
>> resource_idx
* WINED3D_PSARGS_TEXTRANSFORM_SHIFT
)
5200 & WINED3D_PSARGS_TEXTRANSFORM_MASK
;
5201 enum wined3d_shader_resource_type resource_type
= ins
->ctx
->reg_maps
->resource_info
[resource_idx
].type
;
5203 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
5204 if (flags
& WINED3D_PSARGS_PROJECTED
&& resource_type
!= WINED3D_SHADER_RESOURCE_TEXTURE_CUBE
)
5206 sample_flags
|= WINED3D_GLSL_SAMPLE_PROJECTED
;
5207 switch (flags
& ~WINED3D_PSARGS_PROJECTED
)
5209 case WINED3D_TTFF_COUNT1
:
5210 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
5212 case WINED3D_TTFF_COUNT2
:
5213 mask
= WINED3DSP_WRITEMASK_1
;
5215 case WINED3D_TTFF_COUNT3
:
5216 mask
= WINED3DSP_WRITEMASK_2
;
5218 case WINED3D_TTFF_COUNT4
:
5219 case WINED3D_TTFF_DISABLE
:
5220 mask
= WINED3DSP_WRITEMASK_3
;
5225 else if (shader_version
< WINED3D_SHADER_VERSION(2,0))
5227 enum wined3d_shader_src_modifier src_mod
= ins
->src
[0].modifiers
;
5229 if (src_mod
== WINED3DSPSM_DZ
) {
5230 sample_flags
|= WINED3D_GLSL_SAMPLE_PROJECTED
;
5231 mask
= WINED3DSP_WRITEMASK_2
;
5232 } else if (src_mod
== WINED3DSPSM_DW
) {
5233 sample_flags
|= WINED3D_GLSL_SAMPLE_PROJECTED
;
5234 mask
= WINED3DSP_WRITEMASK_3
;
5239 if ((ins
->flags
& WINED3DSI_TEXLD_PROJECT
)
5240 && ins
->ctx
->reg_maps
->resource_info
[resource_idx
].type
!= WINED3D_SHADER_RESOURCE_TEXTURE_CUBE
)
5242 /* ps 2.0 texldp instruction always divides by the fourth component. */
5243 sample_flags
|= WINED3D_GLSL_SAMPLE_PROJECTED
;
5244 mask
= WINED3DSP_WRITEMASK_3
;
5248 shader_glsl_get_sample_function(ins
->ctx
, resource_idx
, resource_idx
, sample_flags
, &sample_function
);
5249 mask
|= sample_function
.coord_mask
;
5250 sample_function
.coord_mask
= mask
;
5252 if (shader_version
< WINED3D_SHADER_VERSION(2,0)) swizzle
= WINED3DSP_NOSWIZZLE
;
5253 else swizzle
= ins
->src
[1].swizzle
;
5255 /* 1.0-1.3: Use destination register as coordinate source.
5256 1.4+: Use provided coordinate source register. */
5257 if (shader_version
< WINED3D_SHADER_VERSION(1,4))
5260 shader_glsl_write_mask_to_str(mask
, coord_mask
);
5261 shader_glsl_gen_sample_code(ins
, resource_idx
, &sample_function
, swizzle
, NULL
, NULL
, NULL
, NULL
,
5262 "T%u%s", resource_idx
, coord_mask
);
5266 struct glsl_src_param coord_param
;
5267 shader_glsl_add_src_param(ins
, &ins
->src
[0], mask
, &coord_param
);
5268 if (ins
->flags
& WINED3DSI_TEXLD_BIAS
)
5270 struct glsl_src_param bias
;
5271 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_3
, &bias
);
5272 shader_glsl_gen_sample_code(ins
, resource_idx
, &sample_function
, swizzle
, NULL
, NULL
, bias
.param_str
,
5273 NULL
, "%s", coord_param
.param_str
);
5275 shader_glsl_gen_sample_code(ins
, resource_idx
, &sample_function
, swizzle
, NULL
, NULL
, NULL
, NULL
,
5276 "%s", coord_param
.param_str
);
5279 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
5282 static void shader_glsl_texldd(const struct wined3d_shader_instruction
*ins
)
5284 const struct wined3d_gl_info
*gl_info
= ins
->ctx
->gl_info
;
5285 struct glsl_src_param coord_param
, dx_param
, dy_param
;
5286 struct glsl_sample_function sample_function
;
5288 DWORD swizzle
= ins
->src
[1].swizzle
;
5290 if (!shader_glsl_has_core_grad(gl_info
) && !gl_info
->supported
[ARB_SHADER_TEXTURE_LOD
])
5292 FIXME("texldd used, but not supported by hardware. Falling back to regular tex.\n");
5293 shader_glsl_tex(ins
);
5297 sampler_idx
= ins
->src
[1].reg
.idx
[0].offset
;
5299 shader_glsl_get_sample_function(ins
->ctx
, sampler_idx
, sampler_idx
, WINED3D_GLSL_SAMPLE_GRAD
, &sample_function
);
5300 shader_glsl_add_src_param(ins
, &ins
->src
[0], sample_function
.coord_mask
, &coord_param
);
5301 shader_glsl_add_src_param(ins
, &ins
->src
[2], sample_function
.deriv_mask
, &dx_param
);
5302 shader_glsl_add_src_param(ins
, &ins
->src
[3], sample_function
.deriv_mask
, &dy_param
);
5304 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, swizzle
, dx_param
.param_str
, dy_param
.param_str
,
5305 NULL
, NULL
, "%s", coord_param
.param_str
);
5306 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
5309 static void shader_glsl_texldl(const struct wined3d_shader_instruction
*ins
)
5311 const struct wined3d_shader_version
*shader_version
= &ins
->ctx
->reg_maps
->shader_version
;
5312 const struct wined3d_gl_info
*gl_info
= ins
->ctx
->gl_info
;
5313 struct glsl_src_param coord_param
, lod_param
;
5314 struct glsl_sample_function sample_function
;
5315 DWORD swizzle
= ins
->src
[1].swizzle
;
5318 sampler_idx
= ins
->src
[1].reg
.idx
[0].offset
;
5320 shader_glsl_get_sample_function(ins
->ctx
, sampler_idx
, sampler_idx
, WINED3D_GLSL_SAMPLE_LOD
, &sample_function
);
5321 shader_glsl_add_src_param(ins
, &ins
->src
[0], sample_function
.coord_mask
, &coord_param
);
5323 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_3
, &lod_param
);
5325 if (shader_version
->type
== WINED3D_SHADER_TYPE_PIXEL
&& !shader_glsl_has_core_grad(gl_info
)
5326 && !gl_info
->supported
[ARB_SHADER_TEXTURE_LOD
])
5328 /* Plain GLSL only supports Lod sampling functions in vertex shaders.
5329 * However, the NVIDIA drivers allow them in fragment shaders as well,
5330 * even without the appropriate extension. */
5331 WARN("Using %s in fragment shader.\n", sample_function
.name
->buffer
);
5333 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, swizzle
, NULL
, NULL
, lod_param
.param_str
, NULL
,
5334 "%s", coord_param
.param_str
);
5335 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
5338 static unsigned int shader_glsl_find_sampler(const struct wined3d_shader_sampler_map
*sampler_map
,
5339 unsigned int resource_idx
, unsigned int sampler_idx
)
5341 struct wined3d_shader_sampler_map_entry
*entries
= sampler_map
->entries
;
5344 for (i
= 0; i
< sampler_map
->count
; ++i
)
5346 if (entries
[i
].resource_idx
== resource_idx
&& entries
[i
].sampler_idx
== sampler_idx
)
5347 return entries
[i
].bind_idx
;
5350 ERR("No GLSL sampler found for resource %u / sampler %u.\n", resource_idx
, sampler_idx
);
5355 static void shader_glsl_atomic(const struct wined3d_shader_instruction
*ins
)
5357 const BOOL is_imm_instruction
= WINED3DSIH_IMM_ATOMIC_AND
<= ins
->handler_idx
5358 && ins
->handler_idx
<= WINED3DSIH_IMM_ATOMIC_XOR
;
5359 const struct wined3d_shader_reg_maps
*reg_maps
= ins
->ctx
->reg_maps
;
5360 const struct wined3d_shader_version
*version
= ®_maps
->shader_version
;
5361 struct shader_glsl_ctx_priv
*priv
= ins
->ctx
->backend_data
;
5362 struct glsl_src_param structure_idx
, offset
, data
, data2
;
5363 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
5364 enum wined3d_shader_resource_type resource_type
;
5365 struct wined3d_string_buffer
*address
;
5366 enum wined3d_data_type data_type
;
5367 unsigned int resource_idx
, stride
;
5368 const char *op
, *resource
;
5372 resource_idx
= ins
->dst
[is_imm_instruction
].reg
.idx
[0].offset
;
5373 is_tgsm
= ins
->dst
[is_imm_instruction
].reg
.type
== WINED3DSPR_GROUPSHAREDMEM
;
5376 if (resource_idx
>= reg_maps
->tgsm_count
)
5378 ERR("Invalid TGSM index %u.\n", resource_idx
);
5382 data_type
= WINED3D_DATA_UINT
;
5384 stride
= reg_maps
->tgsm
[resource_idx
].stride
;
5388 if (resource_idx
>= ARRAY_SIZE(reg_maps
->uav_resource_info
))
5390 ERR("Invalid UAV index %u.\n", resource_idx
);
5393 resource_type
= reg_maps
->uav_resource_info
[resource_idx
].type
;
5394 if (resource_type
>= ARRAY_SIZE(resource_type_info
))
5396 ERR("Unexpected resource type %#x.\n", resource_type
);
5400 data_type
= reg_maps
->uav_resource_info
[resource_idx
].data_type
;
5401 coord_mask
= (1u << resource_type_info
[resource_type
].coord_size
) - 1;
5402 stride
= reg_maps
->uav_resource_info
[resource_idx
].stride
;
5405 switch (ins
->handler_idx
)
5407 case WINED3DSIH_ATOMIC_AND
:
5408 case WINED3DSIH_IMM_ATOMIC_AND
:
5412 op
= "imageAtomicAnd";
5414 case WINED3DSIH_ATOMIC_CMP_STORE
:
5415 case WINED3DSIH_IMM_ATOMIC_CMP_EXCH
:
5417 op
= "atomicCompSwap";
5419 op
= "imageAtomicCompSwap";
5421 case WINED3DSIH_ATOMIC_IADD
:
5422 case WINED3DSIH_IMM_ATOMIC_IADD
:
5426 op
= "imageAtomicAdd";
5428 case WINED3DSIH_ATOMIC_IMAX
:
5429 case WINED3DSIH_IMM_ATOMIC_IMAX
:
5433 op
= "imageAtomicMax";
5434 if (data_type
!= WINED3D_DATA_INT
)
5436 FIXME("Unhandled opcode %#x for unsigned integers.\n", ins
->handler_idx
);
5440 case WINED3DSIH_ATOMIC_IMIN
:
5441 case WINED3DSIH_IMM_ATOMIC_IMIN
:
5445 op
= "imageAtomicMin";
5446 if (data_type
!= WINED3D_DATA_INT
)
5448 FIXME("Unhandled opcode %#x for unsigned integers.\n", ins
->handler_idx
);
5452 case WINED3DSIH_ATOMIC_OR
:
5453 case WINED3DSIH_IMM_ATOMIC_OR
:
5457 op
= "imageAtomicOr";
5459 case WINED3DSIH_ATOMIC_UMAX
:
5460 case WINED3DSIH_IMM_ATOMIC_UMAX
:
5464 op
= "imageAtomicMax";
5465 if (data_type
!= WINED3D_DATA_UINT
)
5467 FIXME("Unhandled opcode %#x for signed integers.\n", ins
->handler_idx
);
5471 case WINED3DSIH_ATOMIC_UMIN
:
5472 case WINED3DSIH_IMM_ATOMIC_UMIN
:
5476 op
= "imageAtomicMin";
5477 if (data_type
!= WINED3D_DATA_UINT
)
5479 FIXME("Unhandled opcode %#x for signed integers.\n", ins
->handler_idx
);
5483 case WINED3DSIH_ATOMIC_XOR
:
5484 case WINED3DSIH_IMM_ATOMIC_XOR
:
5488 op
= "imageAtomicXor";
5490 case WINED3DSIH_IMM_ATOMIC_EXCH
:
5492 op
= "atomicExchange";
5494 op
= "imageAtomicExchange";
5497 ERR("Unhandled opcode %#x.\n", ins
->handler_idx
);
5501 address
= string_buffer_get(priv
->string_buffers
);
5504 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &structure_idx
);
5505 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_1
, &offset
);
5506 string_buffer_sprintf(address
, "%s * %u + %s / 4", structure_idx
.param_str
, stride
, offset
.param_str
);
5510 shader_glsl_add_src_param(ins
, &ins
->src
[0], coord_mask
, &offset
);
5511 string_buffer_sprintf(address
, "%s", offset
.param_str
);
5512 if (is_tgsm
|| (reg_maps
->uav_resource_info
[resource_idx
].flags
& WINED3D_VIEW_BUFFER_RAW
))
5513 shader_addline(address
, "/ 4");
5516 if (is_imm_instruction
)
5517 shader_glsl_append_dst_ext(ins
->ctx
->buffer
, ins
, &ins
->dst
[0], data_type
);
5520 shader_addline(buffer
, "%s(%s_%s%u[%s], ",
5521 op
, shader_glsl_get_prefix(version
->type
), resource
, resource_idx
, address
->buffer
);
5523 shader_addline(buffer
, "%s(%s_%s%u, %s, ",
5524 op
, shader_glsl_get_prefix(version
->type
), resource
, resource_idx
, address
->buffer
);
5526 shader_glsl_add_src_param_ext(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_0
, &data
, data_type
);
5527 shader_addline(buffer
, "%s", data
.param_str
);
5528 if (ins
->src_count
>= 3)
5530 shader_glsl_add_src_param_ext(ins
, &ins
->src
[2], WINED3DSP_WRITEMASK_0
, &data2
, data_type
);
5531 shader_addline(buffer
, ", %s", data2
.param_str
);
5534 if (is_imm_instruction
)
5535 shader_addline(buffer
, ")");
5536 shader_addline(buffer
, ");\n");
5538 string_buffer_release(priv
->string_buffers
, address
);
5541 static void shader_glsl_uav_counter(const struct wined3d_shader_instruction
*ins
)
5543 const char *prefix
= shader_glsl_get_prefix(ins
->ctx
->reg_maps
->shader_version
.type
);
5546 if (ins
->handler_idx
== WINED3DSIH_IMM_ATOMIC_ALLOC
)
5547 op
= "atomicCounterIncrement";
5549 op
= "atomicCounterDecrement";
5551 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
5552 shader_addline(ins
->ctx
->buffer
, "%s(%s_counter%u));\n", op
, prefix
, ins
->src
[0].reg
.idx
[0].offset
);
5555 static void shader_glsl_ld_uav(const struct wined3d_shader_instruction
*ins
)
5557 const struct wined3d_shader_reg_maps
*reg_maps
= ins
->ctx
->reg_maps
;
5558 const struct wined3d_shader_version
*version
= ®_maps
->shader_version
;
5559 enum wined3d_shader_resource_type resource_type
;
5560 struct glsl_src_param image_coord_param
;
5561 enum wined3d_data_type data_type
;
5562 DWORD coord_mask
, write_mask
;
5563 unsigned int uav_idx
;
5564 char dst_swizzle
[6];
5566 uav_idx
= ins
->src
[1].reg
.idx
[0].offset
;
5567 if (uav_idx
>= ARRAY_SIZE(reg_maps
->uav_resource_info
))
5569 ERR("Invalid UAV index %u.\n", uav_idx
);
5572 resource_type
= reg_maps
->uav_resource_info
[uav_idx
].type
;
5573 if (resource_type
>= ARRAY_SIZE(resource_type_info
))
5575 ERR("Unexpected resource type %#x.\n", resource_type
);
5576 resource_type
= WINED3D_SHADER_RESOURCE_TEXTURE_2D
;
5578 data_type
= reg_maps
->uav_resource_info
[uav_idx
].data_type
;
5579 coord_mask
= (1u << resource_type_info
[resource_type
].coord_size
) - 1;
5581 write_mask
= shader_glsl_append_dst_ext(ins
->ctx
->buffer
, ins
, &ins
->dst
[0], data_type
);
5582 shader_glsl_get_swizzle(&ins
->src
[1], FALSE
, write_mask
, dst_swizzle
);
5584 shader_glsl_add_src_param(ins
, &ins
->src
[0], coord_mask
, &image_coord_param
);
5585 shader_addline(ins
->ctx
->buffer
, "imageLoad(%s_image%u, %s)%s);\n",
5586 shader_glsl_get_prefix(version
->type
), uav_idx
, image_coord_param
.param_str
, dst_swizzle
);
5589 static void shader_glsl_ld_raw_structured(const struct wined3d_shader_instruction
*ins
)
5591 const char *prefix
= shader_glsl_get_prefix(ins
->ctx
->reg_maps
->shader_version
.type
);
5592 const struct wined3d_shader_src_param
*src
= &ins
->src
[ins
->src_count
- 1];
5593 unsigned int i
, swizzle
, resource_idx
, bind_idx
, stride
, src_idx
= 0;
5594 const struct wined3d_shader_reg_maps
*reg_maps
= ins
->ctx
->reg_maps
;
5595 struct shader_glsl_ctx_priv
*priv
= ins
->ctx
->backend_data
;
5596 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
5597 struct glsl_src_param structure_idx
, offset
;
5598 struct wined3d_string_buffer
*address
;
5599 struct wined3d_shader_dst_param dst
;
5600 const char *function
, *resource
;
5602 resource_idx
= src
->reg
.idx
[0].offset
;
5603 if (src
->reg
.type
== WINED3DSPR_RESOURCE
)
5605 if (resource_idx
>= ARRAY_SIZE(reg_maps
->resource_info
))
5607 ERR("Invalid resource index %u.\n", resource_idx
);
5610 stride
= reg_maps
->resource_info
[resource_idx
].stride
;
5611 bind_idx
= shader_glsl_find_sampler(®_maps
->sampler_map
, resource_idx
, WINED3D_SAMPLER_DEFAULT
);
5612 function
= "texelFetch";
5613 resource
= "sampler";
5615 else if (src
->reg
.type
== WINED3DSPR_UAV
)
5617 if (resource_idx
>= ARRAY_SIZE(reg_maps
->uav_resource_info
))
5619 ERR("Invalid UAV index %u.\n", resource_idx
);
5622 stride
= reg_maps
->uav_resource_info
[resource_idx
].stride
;
5623 bind_idx
= resource_idx
;
5624 function
= "imageLoad";
5629 if (resource_idx
>= reg_maps
->tgsm_count
)
5631 ERR("Invalid TGSM index %u.\n", resource_idx
);
5634 stride
= reg_maps
->tgsm
[resource_idx
].stride
;
5635 bind_idx
= resource_idx
;
5640 address
= string_buffer_get(priv
->string_buffers
);
5641 if (ins
->handler_idx
== WINED3DSIH_LD_STRUCTURED
)
5643 shader_glsl_add_src_param(ins
, &ins
->src
[src_idx
++], WINED3DSP_WRITEMASK_0
, &structure_idx
);
5644 shader_addline(address
, "%s * %u + ", structure_idx
.param_str
, stride
);
5646 shader_glsl_add_src_param(ins
, &ins
->src
[src_idx
++], WINED3DSP_WRITEMASK_0
, &offset
);
5647 shader_addline(address
, "%s / 4", offset
.param_str
);
5650 if (shader_glsl_get_write_mask_size(dst
.write_mask
) > 1)
5652 /* The instruction is split into multiple lines. The first lines may
5653 * overwrite source parameters of the following lines. */
5654 shader_addline(buffer
, "tmp0.x = intBitsToFloat(%s);\n", address
->buffer
);
5655 string_buffer_sprintf(address
, "floatBitsToInt(tmp0.x)");
5658 for (i
= 0; i
< 4; ++i
)
5660 dst
.write_mask
= ins
->dst
[0].write_mask
& (WINED3DSP_WRITEMASK_0
<< i
);
5661 if (!shader_glsl_append_dst_ext(ins
->ctx
->buffer
, ins
, &dst
, dst
.reg
.data_type
))
5664 swizzle
= shader_glsl_swizzle_get_component(src
->swizzle
, i
);
5666 shader_addline(buffer
, "%s(%s_%s%u, %s + %u).x);\n",
5667 function
, prefix
, resource
, bind_idx
, address
->buffer
, swizzle
);
5669 shader_addline(buffer
, "%s_%s%u[%s + %u]);\n",
5670 prefix
, resource
, bind_idx
, address
->buffer
, swizzle
);
5673 string_buffer_release(priv
->string_buffers
, address
);
5676 static void shader_glsl_store_uav(const struct wined3d_shader_instruction
*ins
)
5678 const struct wined3d_shader_reg_maps
*reg_maps
= ins
->ctx
->reg_maps
;
5679 const struct wined3d_shader_version
*version
= ®_maps
->shader_version
;
5680 struct glsl_src_param image_coord_param
, image_data_param
;
5681 enum wined3d_shader_resource_type resource_type
;
5682 enum wined3d_data_type data_type
;
5683 unsigned int uav_idx
;
5686 uav_idx
= ins
->dst
[0].reg
.idx
[0].offset
;
5687 if (uav_idx
>= ARRAY_SIZE(reg_maps
->uav_resource_info
))
5689 ERR("Invalid UAV index %u.\n", uav_idx
);
5692 resource_type
= reg_maps
->uav_resource_info
[uav_idx
].type
;
5693 if (resource_type
>= ARRAY_SIZE(resource_type_info
))
5695 ERR("Unexpected resource type %#x.\n", resource_type
);
5698 data_type
= reg_maps
->uav_resource_info
[uav_idx
].data_type
;
5699 coord_mask
= (1u << resource_type_info
[resource_type
].coord_size
) - 1;
5701 shader_glsl_add_src_param(ins
, &ins
->src
[0], coord_mask
, &image_coord_param
);
5702 shader_glsl_add_src_param_ext(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_ALL
, &image_data_param
, data_type
);
5703 shader_addline(ins
->ctx
->buffer
, "imageStore(%s_image%u, %s, %s);\n",
5704 shader_glsl_get_prefix(version
->type
), uav_idx
,
5705 image_coord_param
.param_str
, image_data_param
.param_str
);
5708 static void shader_glsl_store_raw_structured(const struct wined3d_shader_instruction
*ins
)
5710 const char *prefix
= shader_glsl_get_prefix(ins
->ctx
->reg_maps
->shader_version
.type
);
5711 const struct wined3d_shader_reg_maps
*reg_maps
= ins
->ctx
->reg_maps
;
5712 struct shader_glsl_ctx_priv
*priv
= ins
->ctx
->backend_data
;
5713 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
5714 struct glsl_src_param structure_idx
, offset
, data
;
5715 unsigned int i
, resource_idx
, stride
, src_idx
= 0;
5716 struct wined3d_string_buffer
*address
;
5720 resource_idx
= ins
->dst
[0].reg
.idx
[0].offset
;
5721 is_tgsm
= ins
->dst
[0].reg
.type
== WINED3DSPR_GROUPSHAREDMEM
;
5724 if (resource_idx
>= reg_maps
->tgsm_count
)
5726 ERR("Invalid TGSM index %u.\n", resource_idx
);
5729 stride
= reg_maps
->tgsm
[resource_idx
].stride
;
5733 if (resource_idx
>= ARRAY_SIZE(reg_maps
->uav_resource_info
))
5735 ERR("Invalid UAV index %u.\n", resource_idx
);
5738 stride
= reg_maps
->uav_resource_info
[resource_idx
].stride
;
5741 address
= string_buffer_get(priv
->string_buffers
);
5742 if (ins
->handler_idx
== WINED3DSIH_STORE_STRUCTURED
)
5744 shader_glsl_add_src_param(ins
, &ins
->src
[src_idx
++], WINED3DSP_WRITEMASK_0
, &structure_idx
);
5745 shader_addline(address
, "%s * %u + ", structure_idx
.param_str
, stride
);
5747 shader_glsl_add_src_param(ins
, &ins
->src
[src_idx
++], WINED3DSP_WRITEMASK_0
, &offset
);
5748 shader_addline(address
, "%s / 4", offset
.param_str
);
5750 for (i
= 0; i
< 4; ++i
)
5752 if (!(write_mask
= ins
->dst
[0].write_mask
& (WINED3DSP_WRITEMASK_0
<< i
)))
5755 shader_glsl_add_src_param(ins
, &ins
->src
[src_idx
], write_mask
, &data
);
5758 shader_addline(buffer
, "%s_g%u[%s + %u] = %s;\n",
5759 prefix
, resource_idx
, address
->buffer
, i
, data
.param_str
);
5761 shader_addline(buffer
, "imageStore(%s_image%u, %s + %u, uvec4(%s, 0, 0, 0));\n",
5762 prefix
, resource_idx
, address
->buffer
, i
, data
.param_str
);
5765 string_buffer_release(priv
->string_buffers
, address
);
5768 static void shader_glsl_sync(const struct wined3d_shader_instruction
*ins
)
5770 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
5771 unsigned int sync_flags
= ins
->flags
;
5773 if (sync_flags
& WINED3DSSF_THREAD_GROUP
)
5775 shader_addline(buffer
, "barrier();\n");
5776 sync_flags
&= ~(WINED3DSSF_THREAD_GROUP
| WINED3DSSF_GROUP_SHARED_MEMORY
);
5779 if (sync_flags
& WINED3DSSF_GROUP_SHARED_MEMORY
)
5781 shader_addline(buffer
, "memoryBarrierShared();\n");
5782 sync_flags
&= ~WINED3DSSF_GROUP_SHARED_MEMORY
;
5786 FIXME("Unhandled sync flags %#x.\n", sync_flags
);
5789 static const struct wined3d_shader_resource_info
*shader_glsl_get_resource_info(
5790 const struct wined3d_shader_instruction
*ins
, const struct wined3d_shader_register
*reg
)
5792 const struct wined3d_shader_reg_maps
*reg_maps
= ins
->ctx
->reg_maps
;
5793 unsigned int idx
= reg
->idx
[0].offset
;
5795 if (reg
->type
== WINED3DSPR_RESOURCE
)
5797 if (idx
>= ARRAY_SIZE(reg_maps
->resource_info
))
5799 ERR("Invalid resource index %u.\n", idx
);
5802 return ®_maps
->resource_info
[idx
];
5805 if (reg
->type
== WINED3DSPR_UAV
)
5807 if (idx
>= ARRAY_SIZE(reg_maps
->uav_resource_info
))
5809 ERR("Invalid UAV index %u.\n", idx
);
5812 return ®_maps
->uav_resource_info
[idx
];
5815 FIXME("Unhandled register type %#x.\n", reg
->type
);
5819 static void shader_glsl_bufinfo(const struct wined3d_shader_instruction
*ins
)
5821 const char *prefix
= shader_glsl_get_prefix(ins
->ctx
->reg_maps
->shader_version
.type
);
5822 const struct wined3d_shader_resource_info
*resource_info
;
5823 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
5824 unsigned int resource_idx
;
5825 char dst_swizzle
[6];
5828 write_mask
= shader_glsl_append_dst(buffer
, ins
);
5829 shader_glsl_get_swizzle(&ins
->src
[0], FALSE
, write_mask
, dst_swizzle
);
5831 if (!(resource_info
= shader_glsl_get_resource_info(ins
, &ins
->src
[0].reg
)))
5833 resource_idx
= ins
->src
[0].reg
.idx
[0].offset
;
5835 shader_addline(buffer
, "ivec2(");
5836 if (ins
->src
[0].reg
.type
== WINED3DSPR_RESOURCE
)
5838 unsigned int bind_idx
= shader_glsl_find_sampler(&ins
->ctx
->reg_maps
->sampler_map
,
5839 resource_idx
, WINED3D_SAMPLER_DEFAULT
);
5840 shader_addline(buffer
, "textureSize(%s_sampler%u)", prefix
, bind_idx
);
5844 shader_addline(buffer
, "imageSize(%s_image%u)", prefix
, resource_idx
);
5846 if (resource_info
->stride
)
5847 shader_addline(buffer
, " / %u", resource_info
->stride
);
5848 else if (resource_info
->flags
& WINED3D_VIEW_BUFFER_RAW
)
5849 shader_addline(buffer
, " * 4");
5850 shader_addline(buffer
, ", %u)%s);\n", resource_info
->stride
, dst_swizzle
);
5853 static void shader_glsl_resinfo(const struct wined3d_shader_instruction
*ins
)
5855 const struct wined3d_shader_version
*version
= &ins
->ctx
->reg_maps
->shader_version
;
5856 const struct wined3d_gl_info
*gl_info
= ins
->ctx
->gl_info
;
5857 enum wined3d_shader_resource_type resource_type
;
5858 enum wined3d_shader_register_type reg_type
;
5859 unsigned int resource_idx
, bind_idx
, i
;
5860 enum wined3d_data_type dst_data_type
;
5861 struct glsl_src_param lod_param
;
5862 char dst_swizzle
[6];
5865 dst_data_type
= ins
->dst
[0].reg
.data_type
;
5866 if (ins
->flags
== WINED3DSI_RESINFO_UINT
)
5867 dst_data_type
= WINED3D_DATA_UINT
;
5868 else if (ins
->flags
)
5869 FIXME("Unhandled flags %#x.\n", ins
->flags
);
5871 write_mask
= shader_glsl_append_dst_ext(ins
->ctx
->buffer
, ins
, &ins
->dst
[0], dst_data_type
);
5872 shader_glsl_get_swizzle(&ins
->src
[1], FALSE
, write_mask
, dst_swizzle
);
5874 reg_type
= ins
->src
[1].reg
.type
;
5875 resource_idx
= ins
->src
[1].reg
.idx
[0].offset
;
5876 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &lod_param
);
5877 if (reg_type
== WINED3DSPR_RESOURCE
)
5879 resource_type
= ins
->ctx
->reg_maps
->resource_info
[resource_idx
].type
;
5880 bind_idx
= shader_glsl_find_sampler(&ins
->ctx
->reg_maps
->sampler_map
,
5881 resource_idx
, WINED3D_SAMPLER_DEFAULT
);
5885 resource_type
= ins
->ctx
->reg_maps
->uav_resource_info
[resource_idx
].type
;
5886 bind_idx
= resource_idx
;
5889 if (resource_type
>= ARRAY_SIZE(resource_type_info
))
5891 ERR("Unexpected resource type %#x.\n", resource_type
);
5892 resource_type
= WINED3D_SHADER_RESOURCE_TEXTURE_2D
;
5895 if (dst_data_type
== WINED3D_DATA_UINT
)
5896 shader_addline(ins
->ctx
->buffer
, "uvec4(");
5898 shader_addline(ins
->ctx
->buffer
, "vec4(");
5900 if (reg_type
== WINED3DSPR_RESOURCE
)
5902 shader_addline(ins
->ctx
->buffer
, "textureSize(%s_sampler%u, %s), ",
5903 shader_glsl_get_prefix(version
->type
), bind_idx
, lod_param
.param_str
);
5905 for (i
= 0; i
< 3 - resource_type_info
[resource_type
].resinfo_size
; ++i
)
5906 shader_addline(ins
->ctx
->buffer
, "0, ");
5908 if (gl_info
->supported
[ARB_TEXTURE_QUERY_LEVELS
])
5910 shader_addline(ins
->ctx
->buffer
, "textureQueryLevels(%s_sampler%u)",
5911 shader_glsl_get_prefix(version
->type
), bind_idx
);
5915 FIXME("textureQueryLevels is not supported, returning 1 mipmap level.\n");
5916 shader_addline(ins
->ctx
->buffer
, "1");
5921 shader_addline(ins
->ctx
->buffer
, "imageSize(%s_image%u), ",
5922 shader_glsl_get_prefix(version
->type
), bind_idx
);
5924 for (i
= 0; i
< 3 - resource_type_info
[resource_type
].resinfo_size
; ++i
)
5925 shader_addline(ins
->ctx
->buffer
, "0, ");
5927 /* For UAVs the returned miplevel count is always 1. */
5928 shader_addline(ins
->ctx
->buffer
, "1");
5931 shader_addline(ins
->ctx
->buffer
, ")%s);\n", dst_swizzle
);
5934 /* FIXME: The current implementation does not handle multisample textures correctly. */
5935 static void shader_glsl_ld(const struct wined3d_shader_instruction
*ins
)
5937 const struct wined3d_shader_reg_maps
*reg_maps
= ins
->ctx
->reg_maps
;
5938 unsigned int resource_idx
, sampler_idx
, sampler_bind_idx
;
5939 struct glsl_src_param coord_param
, lod_param
;
5940 struct glsl_sample_function sample_function
;
5941 DWORD flags
= WINED3D_GLSL_SAMPLE_LOAD
;
5944 if (wined3d_shader_instruction_has_texel_offset(ins
))
5945 flags
|= WINED3D_GLSL_SAMPLE_OFFSET
;
5947 resource_idx
= ins
->src
[1].reg
.idx
[0].offset
;
5948 sampler_idx
= WINED3D_SAMPLER_DEFAULT
;
5950 if (resource_idx
>= ARRAY_SIZE(reg_maps
->resource_info
))
5952 ERR("Invalid resource index %u.\n", resource_idx
);
5955 has_lod_param
= reg_maps
->resource_info
[resource_idx
].type
!= WINED3D_SHADER_RESOURCE_BUFFER
;
5957 shader_glsl_get_sample_function(ins
->ctx
, resource_idx
, sampler_idx
, flags
, &sample_function
);
5958 shader_glsl_add_src_param(ins
, &ins
->src
[0], sample_function
.coord_mask
, &coord_param
);
5959 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_3
, &lod_param
);
5960 sampler_bind_idx
= shader_glsl_find_sampler(®_maps
->sampler_map
, resource_idx
, sampler_idx
);
5961 shader_glsl_gen_sample_code(ins
, sampler_bind_idx
, &sample_function
, ins
->src
[1].swizzle
,
5962 NULL
, NULL
, has_lod_param
? lod_param
.param_str
: NULL
, &ins
->texel_offset
,
5963 "%s", coord_param
.param_str
);
5964 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
5967 static void shader_glsl_sample(const struct wined3d_shader_instruction
*ins
)
5969 const char *lod_param_str
= NULL
, *dx_param_str
= NULL
, *dy_param_str
= NULL
;
5970 struct glsl_src_param coord_param
, lod_param
, dx_param
, dy_param
;
5971 unsigned int resource_idx
, sampler_idx
, sampler_bind_idx
;
5972 struct glsl_sample_function sample_function
;
5975 if (ins
->handler_idx
== WINED3DSIH_SAMPLE_GRAD
)
5976 flags
|= WINED3D_GLSL_SAMPLE_GRAD
;
5977 if (ins
->handler_idx
== WINED3DSIH_SAMPLE_LOD
)
5978 flags
|= WINED3D_GLSL_SAMPLE_LOD
;
5979 if (wined3d_shader_instruction_has_texel_offset(ins
))
5980 flags
|= WINED3D_GLSL_SAMPLE_OFFSET
;
5982 resource_idx
= ins
->src
[1].reg
.idx
[0].offset
;
5983 sampler_idx
= ins
->src
[2].reg
.idx
[0].offset
;
5985 shader_glsl_get_sample_function(ins
->ctx
, resource_idx
, sampler_idx
, flags
, &sample_function
);
5986 shader_glsl_add_src_param(ins
, &ins
->src
[0], sample_function
.coord_mask
, &coord_param
);
5988 switch (ins
->handler_idx
)
5990 case WINED3DSIH_SAMPLE
:
5992 case WINED3DSIH_SAMPLE_B
:
5993 shader_glsl_add_src_param(ins
, &ins
->src
[3], WINED3DSP_WRITEMASK_0
, &lod_param
);
5994 lod_param_str
= lod_param
.param_str
;
5996 case WINED3DSIH_SAMPLE_GRAD
:
5997 shader_glsl_add_src_param(ins
, &ins
->src
[3], sample_function
.deriv_mask
, &dx_param
);
5998 shader_glsl_add_src_param(ins
, &ins
->src
[4], sample_function
.deriv_mask
, &dy_param
);
5999 dx_param_str
= dx_param
.param_str
;
6000 dy_param_str
= dy_param
.param_str
;
6002 case WINED3DSIH_SAMPLE_LOD
:
6003 shader_glsl_add_src_param(ins
, &ins
->src
[3], WINED3DSP_WRITEMASK_0
, &lod_param
);
6004 lod_param_str
= lod_param
.param_str
;
6007 ERR("Unhandled opcode %s.\n", debug_d3dshaderinstructionhandler(ins
->handler_idx
));
6011 sampler_bind_idx
= shader_glsl_find_sampler(&ins
->ctx
->reg_maps
->sampler_map
, resource_idx
, sampler_idx
);
6012 shader_glsl_gen_sample_code(ins
, sampler_bind_idx
, &sample_function
, ins
->src
[1].swizzle
,
6013 dx_param_str
, dy_param_str
, lod_param_str
, &ins
->texel_offset
, "%s", coord_param
.param_str
);
6014 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
6017 static void shader_glsl_sample_c(const struct wined3d_shader_instruction
*ins
)
6019 unsigned int resource_idx
, sampler_idx
, sampler_bind_idx
;
6020 struct glsl_src_param coord_param
, compare_param
;
6021 struct glsl_sample_function sample_function
;
6022 const char *lod_param
= NULL
;
6023 unsigned int coord_size
;
6026 if (ins
->handler_idx
== WINED3DSIH_SAMPLE_C_LZ
)
6029 flags
|= WINED3D_GLSL_SAMPLE_LOD
;
6032 if (wined3d_shader_instruction_has_texel_offset(ins
))
6033 flags
|= WINED3D_GLSL_SAMPLE_OFFSET
;
6035 resource_idx
= ins
->src
[1].reg
.idx
[0].offset
;
6036 sampler_idx
= ins
->src
[2].reg
.idx
[0].offset
;
6038 shader_glsl_get_sample_function(ins
->ctx
, resource_idx
, sampler_idx
, flags
, &sample_function
);
6039 coord_size
= shader_glsl_get_write_mask_size(sample_function
.coord_mask
);
6040 shader_glsl_add_src_param(ins
, &ins
->src
[0], sample_function
.coord_mask
>> 1, &coord_param
);
6041 shader_glsl_add_src_param(ins
, &ins
->src
[3], WINED3DSP_WRITEMASK_0
, &compare_param
);
6042 sampler_bind_idx
= shader_glsl_find_sampler(&ins
->ctx
->reg_maps
->sampler_map
, resource_idx
, sampler_idx
);
6043 shader_glsl_gen_sample_code(ins
, sampler_bind_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
,
6044 NULL
, NULL
, lod_param
, &ins
->texel_offset
, "vec%u(%s, %s)",
6045 coord_size
, coord_param
.param_str
, compare_param
.param_str
);
6046 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
6049 static void shader_glsl_gather4(const struct wined3d_shader_instruction
*ins
)
6051 unsigned int resource_param_idx
, resource_idx
, sampler_idx
, sampler_bind_idx
, component_idx
;
6052 const struct wined3d_shader_reg_maps
*reg_maps
= ins
->ctx
->reg_maps
;
6053 const char *prefix
= shader_glsl_get_prefix(reg_maps
->shader_version
.type
);
6054 struct glsl_src_param coord_param
, compare_param
, offset_param
;
6055 const struct wined3d_gl_info
*gl_info
= ins
->ctx
->gl_info
;
6056 const struct wined3d_shader_resource_info
*resource_info
;
6057 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
6058 unsigned int coord_size
, offset_size
;
6059 char dst_swizzle
[6];
6062 if (!gl_info
->supported
[ARB_TEXTURE_GATHER
])
6064 FIXME("OpenGL implementation does not support textureGather.\n");
6068 has_offset
= ins
->handler_idx
== WINED3DSIH_GATHER4_PO
6069 || ins
->handler_idx
== WINED3DSIH_GATHER4_PO_C
6070 || wined3d_shader_instruction_has_texel_offset(ins
);
6072 resource_param_idx
=
6073 (ins
->handler_idx
== WINED3DSIH_GATHER4_PO
|| ins
->handler_idx
== WINED3DSIH_GATHER4_PO_C
) ? 2 : 1;
6074 resource_idx
= ins
->src
[resource_param_idx
].reg
.idx
[0].offset
;
6075 sampler_idx
= ins
->src
[resource_param_idx
+ 1].reg
.idx
[0].offset
;
6076 component_idx
= shader_glsl_swizzle_get_component(ins
->src
[resource_param_idx
+ 1].swizzle
, 0);
6077 sampler_bind_idx
= shader_glsl_find_sampler(®_maps
->sampler_map
, resource_idx
, sampler_idx
);
6079 if (!(resource_info
= shader_glsl_get_resource_info(ins
, &ins
->src
[resource_param_idx
].reg
)))
6082 if (resource_info
->type
>= ARRAY_SIZE(resource_type_info
))
6084 ERR("Unexpected resource type %#x.\n", resource_info
->type
);
6087 shader_glsl_get_coord_size(resource_info
->type
, &coord_size
, &offset_size
);
6089 shader_glsl_swizzle_to_str(ins
->src
[resource_param_idx
].swizzle
, FALSE
, ins
->dst
[0].write_mask
, dst_swizzle
);
6090 shader_glsl_append_dst_ext(buffer
, ins
, &ins
->dst
[0], resource_info
->data_type
);
6092 shader_glsl_add_src_param(ins
, &ins
->src
[0], (1u << coord_size
) - 1, &coord_param
);
6094 shader_addline(buffer
, "textureGather%s(%s_sampler%u, %s",
6095 has_offset
? "Offset" : "", prefix
, sampler_bind_idx
, coord_param
.param_str
);
6096 if (ins
->handler_idx
== WINED3DSIH_GATHER4_C
|| ins
->handler_idx
== WINED3DSIH_GATHER4_PO_C
)
6098 shader_glsl_add_src_param(ins
, &ins
->src
[resource_param_idx
+ 2], WINED3DSP_WRITEMASK_0
, &compare_param
);
6099 shader_addline(buffer
, ", %s", compare_param
.param_str
);
6101 if (ins
->handler_idx
== WINED3DSIH_GATHER4_PO
|| ins
->handler_idx
== WINED3DSIH_GATHER4_PO_C
)
6103 shader_glsl_add_src_param(ins
, &ins
->src
[1], (1u << offset_size
) - 1, &offset_param
);
6104 shader_addline(buffer
, ", %s", offset_param
.param_str
);
6106 else if (has_offset
)
6108 int offset_immdata
[4] = {ins
->texel_offset
.u
, ins
->texel_offset
.v
, ins
->texel_offset
.w
};
6109 shader_addline(buffer
, ", ");
6110 shader_glsl_append_imm_ivec(buffer
, offset_immdata
, offset_size
);
6113 shader_addline(buffer
, ", %u", component_idx
);
6115 shader_addline(buffer
, ")%s);\n", dst_swizzle
);
6118 static void shader_glsl_texcoord(const struct wined3d_shader_instruction
*ins
)
6120 /* FIXME: Make this work for more than just 2D textures */
6121 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
6122 DWORD write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
6124 if (!(ins
->ctx
->reg_maps
->shader_version
.major
== 1 && ins
->ctx
->reg_maps
->shader_version
.minor
== 4))
6128 shader_glsl_get_write_mask(&ins
->dst
[0], dst_mask
);
6129 shader_addline(buffer
, "clamp(ffp_texcoord[%u], 0.0, 1.0)%s);\n",
6130 ins
->dst
[0].reg
.idx
[0].offset
, dst_mask
);
6134 enum wined3d_shader_src_modifier src_mod
= ins
->src
[0].modifiers
;
6135 DWORD reg
= ins
->src
[0].reg
.idx
[0].offset
;
6136 char dst_swizzle
[6];
6138 shader_glsl_get_swizzle(&ins
->src
[0], FALSE
, write_mask
, dst_swizzle
);
6140 if (src_mod
== WINED3DSPSM_DZ
|| src_mod
== WINED3DSPSM_DW
)
6142 unsigned int mask_size
= shader_glsl_get_write_mask_size(write_mask
);
6143 struct glsl_src_param div_param
;
6144 DWORD src_writemask
= src_mod
== WINED3DSPSM_DZ
? WINED3DSP_WRITEMASK_2
: WINED3DSP_WRITEMASK_3
;
6146 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_writemask
, &div_param
);
6149 shader_addline(buffer
, "ffp_texcoord[%u]%s / vec%d(%s));\n", reg
, dst_swizzle
, mask_size
, div_param
.param_str
);
6151 shader_addline(buffer
, "ffp_texcoord[%u]%s / %s);\n", reg
, dst_swizzle
, div_param
.param_str
);
6155 shader_addline(buffer
, "ffp_texcoord[%u]%s);\n", reg
, dst_swizzle
);
6160 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
6161 * Take a 3-component dot product of the TexCoord[dstreg] and src,
6162 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
6163 static void shader_glsl_texdp3tex(const struct wined3d_shader_instruction
*ins
)
6165 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
6166 DWORD sampler_idx
= ins
->dst
[0].reg
.idx
[0].offset
;
6167 struct glsl_sample_function sample_function
;
6168 struct glsl_src_param src0_param
;
6171 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
6173 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
6174 * scalar, and projected sampling would require 4.
6176 * It is a dependent read - not valid with conditional NP2 textures
6178 shader_glsl_get_sample_function(ins
->ctx
, sampler_idx
, sampler_idx
, 0, &sample_function
);
6179 mask_size
= shader_glsl_get_write_mask_size(sample_function
.coord_mask
);
6184 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
,
6185 NULL
, "dot(ffp_texcoord[%u].xyz, %s)", sampler_idx
, src0_param
.param_str
);
6189 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
,
6190 NULL
, "vec2(dot(ffp_texcoord[%u].xyz, %s), 0.0)", sampler_idx
, src0_param
.param_str
);
6194 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
,
6195 NULL
, "vec3(dot(ffp_texcoord[%u].xyz, %s), 0.0, 0.0)", sampler_idx
, src0_param
.param_str
);
6199 FIXME("Unexpected mask size %u\n", mask_size
);
6202 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
6205 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
6206 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
6207 static void shader_glsl_texdp3(const struct wined3d_shader_instruction
*ins
)
6209 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
6210 DWORD dstreg
= ins
->dst
[0].reg
.idx
[0].offset
;
6211 struct glsl_src_param src0_param
;
6213 unsigned int mask_size
;
6215 dst_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
6216 mask_size
= shader_glsl_get_write_mask_size(dst_mask
);
6217 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
6219 if (mask_size
> 1) {
6220 shader_addline(ins
->ctx
->buffer
, "vec%d(dot(T%u.xyz, %s)));\n", mask_size
, dstreg
, src0_param
.param_str
);
6222 shader_addline(ins
->ctx
->buffer
, "dot(T%u.xyz, %s));\n", dstreg
, src0_param
.param_str
);
6226 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
6227 * Calculate the depth as dst.x / dst.y */
6228 static void shader_glsl_texdepth(const struct wined3d_shader_instruction
*ins
)
6230 struct glsl_dst_param dst_param
;
6232 shader_glsl_add_dst_param(ins
, &ins
->dst
[0], &dst_param
);
6234 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
6235 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
6236 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
6237 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
6240 shader_addline(ins
->ctx
->buffer
, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n",
6241 dst_param
.reg_name
, dst_param
.reg_name
);
6244 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
6245 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
6246 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
6247 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
6249 static void shader_glsl_texm3x2depth(const struct wined3d_shader_instruction
*ins
)
6251 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
6252 DWORD dstreg
= ins
->dst
[0].reg
.idx
[0].offset
;
6253 struct glsl_src_param src0_param
;
6255 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
6257 shader_addline(ins
->ctx
->buffer
, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg
, src0_param
.param_str
);
6258 shader_addline(ins
->ctx
->buffer
, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
6261 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
6262 * Calculate the 1st of a 2-row matrix multiplication. */
6263 static void shader_glsl_texm3x2pad(const struct wined3d_shader_instruction
*ins
)
6265 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
6266 DWORD reg
= ins
->dst
[0].reg
.idx
[0].offset
;
6267 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
6268 struct glsl_src_param src0_param
;
6270 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
6271 shader_addline(buffer
, "tmp0.x = dot(T%u.xyz, %s);\n", reg
, src0_param
.param_str
);
6274 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
6275 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
6276 static void shader_glsl_texm3x3pad(const struct wined3d_shader_instruction
*ins
)
6278 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
6279 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
6280 struct wined3d_shader_tex_mx
*tex_mx
= ins
->ctx
->tex_mx
;
6281 DWORD reg
= ins
->dst
[0].reg
.idx
[0].offset
;
6282 struct glsl_src_param src0_param
;
6284 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
6285 shader_addline(buffer
, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + tex_mx
->current_row
, reg
, src0_param
.param_str
);
6286 tex_mx
->texcoord_w
[tex_mx
->current_row
++] = reg
;
6289 static void shader_glsl_texm3x2tex(const struct wined3d_shader_instruction
*ins
)
6291 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
6292 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
6293 struct glsl_sample_function sample_function
;
6294 DWORD reg
= ins
->dst
[0].reg
.idx
[0].offset
;
6295 struct glsl_src_param src0_param
;
6297 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
6298 shader_addline(buffer
, "tmp0.y = dot(T%u.xyz, %s);\n", reg
, src0_param
.param_str
);
6300 shader_glsl_get_sample_function(ins
->ctx
, reg
, reg
, 0, &sample_function
);
6302 /* Sample the texture using the calculated coordinates */
6303 shader_glsl_gen_sample_code(ins
, reg
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
, NULL
, "tmp0.xy");
6304 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
6307 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
6308 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
6309 static void shader_glsl_texm3x3tex(const struct wined3d_shader_instruction
*ins
)
6311 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
6312 struct wined3d_shader_tex_mx
*tex_mx
= ins
->ctx
->tex_mx
;
6313 struct glsl_sample_function sample_function
;
6314 DWORD reg
= ins
->dst
[0].reg
.idx
[0].offset
;
6315 struct glsl_src_param src0_param
;
6317 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
6318 shader_addline(ins
->ctx
->buffer
, "tmp0.z = dot(T%u.xyz, %s);\n", reg
, src0_param
.param_str
);
6320 /* Dependent read, not valid with conditional NP2 */
6321 shader_glsl_get_sample_function(ins
->ctx
, reg
, reg
, 0, &sample_function
);
6323 /* Sample the texture using the calculated coordinates */
6324 shader_glsl_gen_sample_code(ins
, reg
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
, NULL
, "tmp0.xyz");
6325 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
6327 tex_mx
->current_row
= 0;
6330 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
6331 * Perform the 3rd row of a 3x3 matrix multiply */
6332 static void shader_glsl_texm3x3(const struct wined3d_shader_instruction
*ins
)
6334 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
6335 struct wined3d_shader_tex_mx
*tex_mx
= ins
->ctx
->tex_mx
;
6336 DWORD reg
= ins
->dst
[0].reg
.idx
[0].offset
;
6337 struct glsl_src_param src0_param
;
6340 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
6342 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
6343 shader_glsl_get_write_mask(&ins
->dst
[0], dst_mask
);
6344 shader_addline(ins
->ctx
->buffer
, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg
, src0_param
.param_str
, dst_mask
);
6346 tex_mx
->current_row
= 0;
6349 /* Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
6350 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
6351 static void shader_glsl_texm3x3spec(const struct wined3d_shader_instruction
*ins
)
6353 struct glsl_src_param src0_param
;
6354 struct glsl_src_param src1_param
;
6355 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
6356 struct wined3d_shader_tex_mx
*tex_mx
= ins
->ctx
->tex_mx
;
6357 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
6358 struct glsl_sample_function sample_function
;
6359 DWORD reg
= ins
->dst
[0].reg
.idx
[0].offset
;
6362 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
6363 shader_glsl_add_src_param(ins
, &ins
->src
[1], src_mask
, &src1_param
);
6365 /* Perform the last matrix multiply operation */
6366 shader_addline(buffer
, "tmp0.z = dot(T%u.xyz, %s);\n", reg
, src0_param
.param_str
);
6367 /* Reflection calculation */
6368 shader_addline(buffer
, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param
.param_str
);
6370 /* Dependent read, not valid with conditional NP2 */
6371 shader_glsl_get_sample_function(ins
->ctx
, reg
, reg
, 0, &sample_function
);
6372 shader_glsl_write_mask_to_str(sample_function
.coord_mask
, coord_mask
);
6374 /* Sample the texture */
6375 shader_glsl_gen_sample_code(ins
, reg
, &sample_function
, WINED3DSP_NOSWIZZLE
,
6376 NULL
, NULL
, NULL
, NULL
, "tmp0%s", coord_mask
);
6377 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
6379 tex_mx
->current_row
= 0;
6382 /* Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
6383 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
6384 static void shader_glsl_texm3x3vspec(const struct wined3d_shader_instruction
*ins
)
6386 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
6387 struct wined3d_shader_tex_mx
*tex_mx
= ins
->ctx
->tex_mx
;
6388 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
6389 struct glsl_sample_function sample_function
;
6390 DWORD reg
= ins
->dst
[0].reg
.idx
[0].offset
;
6391 struct glsl_src_param src0_param
;
6394 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
6396 /* Perform the last matrix multiply operation */
6397 shader_addline(buffer
, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg
, src0_param
.param_str
);
6399 /* Construct the eye-ray vector from w coordinates */
6400 shader_addline(buffer
, "tmp1.xyz = normalize(vec3(ffp_texcoord[%u].w, ffp_texcoord[%u].w, ffp_texcoord[%u].w));\n",
6401 tex_mx
->texcoord_w
[0], tex_mx
->texcoord_w
[1], reg
);
6402 shader_addline(buffer
, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
6404 /* Dependent read, not valid with conditional NP2 */
6405 shader_glsl_get_sample_function(ins
->ctx
, reg
, reg
, 0, &sample_function
);
6406 shader_glsl_write_mask_to_str(sample_function
.coord_mask
, coord_mask
);
6408 /* Sample the texture using the calculated coordinates */
6409 shader_glsl_gen_sample_code(ins
, reg
, &sample_function
, WINED3DSP_NOSWIZZLE
,
6410 NULL
, NULL
, NULL
, NULL
, "tmp0%s", coord_mask
);
6411 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
6413 tex_mx
->current_row
= 0;
6416 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
6417 * Apply a fake bump map transform.
6418 * texbem is pshader <= 1.3 only, this saves a few version checks
6420 static void shader_glsl_texbem(const struct wined3d_shader_instruction
*ins
)
6422 const struct shader_glsl_ctx_priv
*priv
= ins
->ctx
->backend_data
;
6423 struct glsl_sample_function sample_function
;
6424 struct glsl_src_param coord_param
;
6430 sampler_idx
= ins
->dst
[0].reg
.idx
[0].offset
;
6431 flags
= (priv
->cur_ps_args
->tex_transform
>> sampler_idx
* WINED3D_PSARGS_TEXTRANSFORM_SHIFT
)
6432 & WINED3D_PSARGS_TEXTRANSFORM_MASK
;
6434 /* Dependent read, not valid with conditional NP2 */
6435 shader_glsl_get_sample_function(ins
->ctx
, sampler_idx
, sampler_idx
, 0, &sample_function
);
6436 mask
= sample_function
.coord_mask
;
6438 shader_glsl_write_mask_to_str(mask
, coord_mask
);
6440 /* With projected textures, texbem only divides the static texture coord,
6441 * not the displacement, so we can't let GL handle this. */
6442 if (flags
& WINED3D_PSARGS_PROJECTED
)
6445 char coord_div_mask
[3];
6446 switch (flags
& ~WINED3D_PSARGS_PROJECTED
)
6448 case WINED3D_TTFF_COUNT1
:
6449 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
6451 case WINED3D_TTFF_COUNT2
:
6452 div_mask
= WINED3DSP_WRITEMASK_1
;
6454 case WINED3D_TTFF_COUNT3
:
6455 div_mask
= WINED3DSP_WRITEMASK_2
;
6457 case WINED3D_TTFF_COUNT4
:
6458 case WINED3D_TTFF_DISABLE
:
6459 div_mask
= WINED3DSP_WRITEMASK_3
;
6462 shader_glsl_write_mask_to_str(div_mask
, coord_div_mask
);
6463 shader_addline(ins
->ctx
->buffer
, "T%u%s /= T%u%s;\n", sampler_idx
, coord_mask
, sampler_idx
, coord_div_mask
);
6466 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
, &coord_param
);
6468 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
, NULL
,
6469 "T%u%s + vec4(bumpenv_mat%u * %s, 0.0, 0.0)%s", sampler_idx
, coord_mask
, sampler_idx
,
6470 coord_param
.param_str
, coord_mask
);
6472 if (ins
->handler_idx
== WINED3DSIH_TEXBEML
)
6474 struct glsl_src_param luminance_param
;
6475 struct glsl_dst_param dst_param
;
6477 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_2
, &luminance_param
);
6478 shader_glsl_add_dst_param(ins
, &ins
->dst
[0], &dst_param
);
6480 shader_addline(ins
->ctx
->buffer
, "%s%s *= (%s * bumpenv_lum_scale%u + bumpenv_lum_offset%u);\n",
6481 dst_param
.reg_name
, dst_param
.mask_str
,
6482 luminance_param
.param_str
, sampler_idx
, sampler_idx
);
6484 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
6487 static void shader_glsl_bem(const struct wined3d_shader_instruction
*ins
)
6489 DWORD sampler_idx
= ins
->dst
[0].reg
.idx
[0].offset
;
6490 struct glsl_src_param src0_param
, src1_param
;
6492 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
, &src0_param
);
6493 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
, &src1_param
);
6495 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
6496 shader_addline(ins
->ctx
->buffer
, "%s + bumpenv_mat%u * %s);\n",
6497 src0_param
.param_str
, sampler_idx
, src1_param
.param_str
);
6500 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
6501 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
6502 static void shader_glsl_texreg2ar(const struct wined3d_shader_instruction
*ins
)
6504 DWORD sampler_idx
= ins
->dst
[0].reg
.idx
[0].offset
;
6505 struct glsl_sample_function sample_function
;
6506 struct glsl_src_param src0_param
;
6508 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_ALL
, &src0_param
);
6510 shader_glsl_get_sample_function(ins
->ctx
, sampler_idx
, sampler_idx
, 0, &sample_function
);
6511 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
, NULL
,
6512 "%s.wx", src0_param
.reg_name
);
6513 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
6516 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
6517 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
6518 static void shader_glsl_texreg2gb(const struct wined3d_shader_instruction
*ins
)
6520 DWORD sampler_idx
= ins
->dst
[0].reg
.idx
[0].offset
;
6521 struct glsl_sample_function sample_function
;
6522 struct glsl_src_param src0_param
;
6524 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_ALL
, &src0_param
);
6526 shader_glsl_get_sample_function(ins
->ctx
, sampler_idx
, sampler_idx
, 0, &sample_function
);
6527 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
, NULL
,
6528 "%s.yz", src0_param
.reg_name
);
6529 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
6532 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
6533 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
6534 static void shader_glsl_texreg2rgb(const struct wined3d_shader_instruction
*ins
)
6536 DWORD sampler_idx
= ins
->dst
[0].reg
.idx
[0].offset
;
6537 struct glsl_sample_function sample_function
;
6538 struct glsl_src_param src0_param
;
6540 /* Dependent read, not valid with conditional NP2 */
6541 shader_glsl_get_sample_function(ins
->ctx
, sampler_idx
, sampler_idx
, 0, &sample_function
);
6542 shader_glsl_add_src_param(ins
, &ins
->src
[0], sample_function
.coord_mask
, &src0_param
);
6544 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
, NULL
,
6545 "%s", src0_param
.param_str
);
6546 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
6549 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
6550 * If any of the first 3 components are < 0, discard this pixel */
6551 static void shader_glsl_texkill(const struct wined3d_shader_instruction
*ins
)
6553 if (ins
->ctx
->reg_maps
->shader_version
.major
>= 4)
6555 struct glsl_src_param src_param
;
6557 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src_param
);
6558 shader_addline(ins
->ctx
->buffer
, "if (bool(%s)) discard;\n", src_param
.param_str
);
6562 struct glsl_dst_param dst_param
;
6564 /* The argument is a destination parameter, and no writemasks are allowed */
6565 shader_glsl_add_dst_param(ins
, &ins
->dst
[0], &dst_param
);
6567 /* 2.0 shaders compare all 4 components in texkill. */
6568 if (ins
->ctx
->reg_maps
->shader_version
.major
>= 2)
6569 shader_addline(ins
->ctx
->buffer
, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param
.reg_name
);
6570 /* 1.x shaders only compare the first 3 components, probably due to
6571 * the nature of the texkill instruction as a tex* instruction, and
6572 * phase, which kills all .w components. Even if all 4 components are
6573 * defined, only the first 3 are used. */
6575 shader_addline(ins
->ctx
->buffer
, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param
.reg_name
);
6579 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
6580 * dst = dot2(src0, src1) + src2 */
6581 static void shader_glsl_dp2add(const struct wined3d_shader_instruction
*ins
)
6583 struct glsl_src_param src0_param
;
6584 struct glsl_src_param src1_param
;
6585 struct glsl_src_param src2_param
;
6587 unsigned int mask_size
;
6589 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
6590 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
6592 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
, &src0_param
);
6593 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
, &src1_param
);
6594 shader_glsl_add_src_param(ins
, &ins
->src
[2], WINED3DSP_WRITEMASK_0
, &src2_param
);
6596 if (mask_size
> 1) {
6597 shader_addline(ins
->ctx
->buffer
, "vec%d(dot(%s, %s) + %s));\n",
6598 mask_size
, src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
6600 shader_addline(ins
->ctx
->buffer
, "dot(%s, %s) + %s);\n",
6601 src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
6605 static void shader_glsl_input_pack(const struct wined3d_shader
*shader
, struct wined3d_string_buffer
*buffer
,
6606 const struct wined3d_shader_signature
*input_signature
,
6607 const struct wined3d_shader_reg_maps
*reg_maps
,
6608 const struct ps_compile_args
*args
, const struct wined3d_gl_info
*gl_info
, BOOL unroll
)
6612 for (i
= 0; i
< input_signature
->element_count
; ++i
)
6614 const struct wined3d_shader_signature_element
*input
= &input_signature
->elements
[i
];
6615 const char *semantic_name
;
6620 if (!(reg_maps
->input_registers
& (1u << input
->register_idx
)))
6623 semantic_name
= input
->semantic_name
;
6624 semantic_idx
= input
->semantic_idx
;
6625 shader_glsl_write_mask_to_str(input
->mask
, reg_mask
);
6627 if (args
->vp_mode
== vertexshader
)
6629 if (input
->sysval_semantic
== WINED3D_SV_POSITION
&& !semantic_idx
)
6631 shader_addline(buffer
, "ps_in[%u]%s = vpos%s;\n",
6632 shader
->u
.ps
.input_reg_map
[input
->register_idx
], reg_mask
, reg_mask
);
6634 else if (args
->pointsprite
&& shader_match_semantic(semantic_name
, WINED3D_DECL_USAGE_TEXCOORD
))
6636 shader_addline(buffer
, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", input
->register_idx
);
6638 else if (input
->sysval_semantic
== WINED3D_SV_IS_FRONT_FACE
)
6640 shader_addline(buffer
, "ps_in[%u]%s = uintBitsToFloat(gl_FrontFacing ? 0xffffffffu : 0u);\n",
6641 input
->register_idx
, reg_mask
);
6643 else if (input
->sysval_semantic
== WINED3D_SV_RENDER_TARGET_ARRAY_INDEX
&& !semantic_idx
)
6645 if (gl_info
->supported
[ARB_FRAGMENT_LAYER_VIEWPORT
])
6646 shader_addline(buffer
, "ps_in[%u]%s = intBitsToFloat(gl_Layer);\n",
6647 input
->register_idx
, reg_mask
);
6649 FIXME("ARB_fragment_layer_viewport is not supported.\n");
6653 if (input
->sysval_semantic
)
6654 FIXME("Unhandled sysval semantic %#x.\n", input
->sysval_semantic
);
6655 shader_addline(buffer
, unroll
? "ps_in[%u]%s = %s%u%s;\n" : "ps_in[%u]%s = %s[%u]%s;\n",
6656 shader
->u
.ps
.input_reg_map
[input
->register_idx
], reg_mask
,
6657 shader_glsl_shader_input_name(gl_info
),
6658 shader
->u
.ps
.input_reg_map
[input
->register_idx
], reg_mask
);
6661 else if (shader_match_semantic(semantic_name
, WINED3D_DECL_USAGE_TEXCOORD
))
6663 if (args
->pointsprite
)
6664 shader_addline(buffer
, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n",
6665 shader
->u
.ps
.input_reg_map
[input
->register_idx
]);
6666 else if (args
->vp_mode
== pretransformed
&& args
->texcoords_initialized
& (1u << semantic_idx
))
6667 shader_addline(buffer
, "ps_in[%u]%s = %s[%u]%s;\n",
6668 shader
->u
.ps
.input_reg_map
[input
->register_idx
], reg_mask
,
6669 needs_legacy_glsl_syntax(gl_info
)
6670 ? "gl_TexCoord" : "ffp_varying_texcoord", semantic_idx
, reg_mask
);
6672 shader_addline(buffer
, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
6673 shader
->u
.ps
.input_reg_map
[input
->register_idx
], reg_mask
, reg_mask
);
6675 else if (shader_match_semantic(semantic_name
, WINED3D_DECL_USAGE_COLOR
))
6678 shader_addline(buffer
, "ps_in[%u]%s = vec4(ffp_varying_diffuse)%s;\n",
6679 shader
->u
.ps
.input_reg_map
[input
->register_idx
], reg_mask
, reg_mask
);
6680 else if (semantic_idx
== 1)
6681 shader_addline(buffer
, "ps_in[%u]%s = vec4(ffp_varying_specular)%s;\n",
6682 shader
->u
.ps
.input_reg_map
[input
->register_idx
], reg_mask
, reg_mask
);
6684 shader_addline(buffer
, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
6685 shader
->u
.ps
.input_reg_map
[input
->register_idx
], reg_mask
, reg_mask
);
6689 shader_addline(buffer
, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
6690 shader
->u
.ps
.input_reg_map
[input
->register_idx
], reg_mask
, reg_mask
);
6695 static void add_glsl_program_entry(struct shader_glsl_priv
*priv
, struct glsl_shader_prog_link
*entry
)
6697 struct glsl_program_key key
;
6699 key
.vs_id
= entry
->vs
.id
;
6700 key
.hs_id
= entry
->hs
.id
;
6701 key
.ds_id
= entry
->ds
.id
;
6702 key
.gs_id
= entry
->gs
.id
;
6703 key
.ps_id
= entry
->ps
.id
;
6704 key
.cs_id
= entry
->cs
.id
;
6706 if (wine_rb_put(&priv
->program_lookup
, &key
, &entry
->program_lookup_entry
) == -1)
6708 ERR("Failed to insert program entry.\n");
6712 static struct glsl_shader_prog_link
*get_glsl_program_entry(const struct shader_glsl_priv
*priv
,
6713 const struct glsl_program_key
*key
)
6715 struct wine_rb_entry
*entry
;
6717 entry
= wine_rb_get(&priv
->program_lookup
, key
);
6718 return entry
? WINE_RB_ENTRY_VALUE(entry
, struct glsl_shader_prog_link
, program_lookup_entry
) : NULL
;
6721 /* Context activation is done by the caller. */
6722 static void delete_glsl_program_entry(struct shader_glsl_priv
*priv
, const struct wined3d_gl_info
*gl_info
,
6723 struct glsl_shader_prog_link
*entry
)
6725 wine_rb_remove(&priv
->program_lookup
, &entry
->program_lookup_entry
);
6727 GL_EXTCALL(glDeleteProgram(entry
->id
));
6729 list_remove(&entry
->vs
.shader_entry
);
6731 list_remove(&entry
->hs
.shader_entry
);
6733 list_remove(&entry
->ds
.shader_entry
);
6735 list_remove(&entry
->gs
.shader_entry
);
6737 list_remove(&entry
->ps
.shader_entry
);
6739 list_remove(&entry
->cs
.shader_entry
);
6740 HeapFree(GetProcessHeap(), 0, entry
);
6743 static void shader_glsl_setup_vs3_output(struct shader_glsl_priv
*priv
,
6744 const struct wined3d_gl_info
*gl_info
, const DWORD
*map
,
6745 const struct wined3d_shader_signature
*input_signature
,
6746 const struct wined3d_shader_reg_maps
*reg_maps_in
,
6747 const struct wined3d_shader_signature
*output_signature
,
6748 const struct wined3d_shader_reg_maps
*reg_maps_out
)
6750 struct wined3d_string_buffer
*destination
= string_buffer_get(&priv
->string_buffers
);
6751 const char *out_array_name
= shader_glsl_shader_output_name(gl_info
);
6752 struct wined3d_string_buffer
*buffer
= &priv
->shader_buffer
;
6753 unsigned int in_count
= vec4_varyings(3, gl_info
);
6754 unsigned int max_varyings
= needs_legacy_glsl_syntax(gl_info
) ? in_count
+ 2 : in_count
;
6755 DWORD in_idx
, *set
= NULL
;
6759 set
= wined3d_calloc(max_varyings
, sizeof(*set
));
6761 for (i
= 0; i
< input_signature
->element_count
; ++i
)
6763 const struct wined3d_shader_signature_element
*input
= &input_signature
->elements
[i
];
6765 if (!(reg_maps_in
->input_registers
& (1u << input
->register_idx
)))
6768 in_idx
= map
[input
->register_idx
];
6769 /* Declared, but not read register */
6772 if (in_idx
>= max_varyings
)
6774 FIXME("More input varyings declared than supported, expect issues.\n");
6778 if (in_idx
== in_count
)
6779 string_buffer_sprintf(destination
, "gl_FrontColor");
6780 else if (in_idx
== in_count
+ 1)
6781 string_buffer_sprintf(destination
, "gl_FrontSecondaryColor");
6783 string_buffer_sprintf(destination
, "%s[%u]", out_array_name
, in_idx
);
6788 for (j
= 0; j
< output_signature
->element_count
; ++j
)
6790 const struct wined3d_shader_signature_element
*output
= &output_signature
->elements
[j
];
6793 if (!(reg_maps_out
->output_registers
& (1u << output
->register_idx
))
6794 || input
->semantic_idx
!= output
->semantic_idx
6795 || strcmp(input
->semantic_name
, output
->semantic_name
)
6796 || !(mask
= input
->mask
& output
->mask
))
6799 if (set
[in_idx
] == ~0u)
6801 set
[in_idx
] |= mask
& reg_maps_out
->u
.output_registers_mask
[output
->register_idx
];
6802 shader_glsl_write_mask_to_str(mask
, reg_mask
);
6804 shader_addline(buffer
, "%s%s = outputs[%u]%s;\n",
6805 destination
->buffer
, reg_mask
, output
->register_idx
, reg_mask
);
6809 for (i
= 0; i
< max_varyings
; ++i
)
6813 if (!set
[i
] || set
[i
] == WINED3DSP_WRITEMASK_ALL
)
6820 if (!(set
[i
] & WINED3DSP_WRITEMASK_0
))
6821 reg_mask
[size
++] = 'x';
6822 if (!(set
[i
] & WINED3DSP_WRITEMASK_1
))
6823 reg_mask
[size
++] = 'y';
6824 if (!(set
[i
] & WINED3DSP_WRITEMASK_2
))
6825 reg_mask
[size
++] = 'z';
6826 if (!(set
[i
] & WINED3DSP_WRITEMASK_3
))
6827 reg_mask
[size
++] = 'w';
6828 reg_mask
[size
] = '\0';
6831 string_buffer_sprintf(destination
, "gl_FrontColor");
6832 else if (i
== in_count
+ 1)
6833 string_buffer_sprintf(destination
, "gl_FrontSecondaryColor");
6835 string_buffer_sprintf(destination
, "%s[%u]", out_array_name
, i
);
6838 shader_addline(buffer
, "%s.%s = 0.0;\n", destination
->buffer
, reg_mask
);
6840 shader_addline(buffer
, "%s.%s = vec%u(0.0);\n", destination
->buffer
, reg_mask
, size
);
6843 HeapFree(GetProcessHeap(), 0, set
);
6844 string_buffer_release(&priv
->string_buffers
, destination
);
6847 static void shader_glsl_setup_sm4_shader_output(struct shader_glsl_priv
*priv
,
6848 unsigned int input_count
, const struct wined3d_shader_signature
*output_signature
,
6849 const struct wined3d_shader_reg_maps
*reg_maps_out
, const char *output_variable_name
,
6850 BOOL rasterizer_setup
)
6852 struct wined3d_string_buffer
*buffer
= &priv
->shader_buffer
;
6856 for (i
= 0; i
< output_signature
->element_count
; ++i
)
6858 const struct wined3d_shader_signature_element
*output
= &output_signature
->elements
[i
];
6860 if (!(reg_maps_out
->output_registers
& (1u << output
->register_idx
)))
6863 if (output
->stream_idx
)
6866 if (output
->register_idx
>= input_count
)
6869 shader_glsl_write_mask_to_str(output
->mask
, reg_mask
);
6871 shader_addline(buffer
,
6872 rasterizer_setup
? "%s.reg%u%s = outputs[%u]%s;\n" : "%s.reg[%u]%s = outputs[%u]%s;\n",
6873 output_variable_name
, output
->register_idx
, reg_mask
, output
->register_idx
, reg_mask
);
6877 static void shader_glsl_setup_sm3_rasterizer_input(struct shader_glsl_priv
*priv
,
6878 const struct wined3d_gl_info
*gl_info
, const DWORD
*map
,
6879 const struct wined3d_shader_signature
*input_signature
,
6880 const struct wined3d_shader_reg_maps
*reg_maps_in
, unsigned int input_count
,
6881 const struct wined3d_shader_signature
*output_signature
,
6882 const struct wined3d_shader_reg_maps
*reg_maps_out
, BOOL per_vertex_point_size
)
6884 struct wined3d_string_buffer
*buffer
= &priv
->shader_buffer
;
6885 const char *semantic_name
;
6890 /* First, sort out position and point size system values. */
6891 for (i
= 0; i
< output_signature
->element_count
; ++i
)
6893 const struct wined3d_shader_signature_element
*output
= &output_signature
->elements
[i
];
6895 if (!(reg_maps_out
->output_registers
& (1u << output
->register_idx
)))
6898 if (output
->stream_idx
)
6901 semantic_name
= output
->semantic_name
;
6902 semantic_idx
= output
->semantic_idx
;
6903 shader_glsl_write_mask_to_str(output
->mask
, reg_mask
);
6905 if (output
->sysval_semantic
== WINED3D_SV_POSITION
&& !semantic_idx
)
6907 shader_addline(buffer
, "gl_Position%s = outputs[%u]%s;\n",
6908 reg_mask
, output
->register_idx
, reg_mask
);
6910 else if (shader_match_semantic(semantic_name
, WINED3D_DECL_USAGE_PSIZE
) && per_vertex_point_size
)
6912 shader_addline(buffer
, "gl_PointSize = clamp(outputs[%u].%c, "
6913 "ffp_point.size_min, ffp_point.size_max);\n", output
->register_idx
, reg_mask
[1]);
6915 else if (output
->sysval_semantic
== WINED3D_SV_RENDER_TARGET_ARRAY_INDEX
&& !semantic_idx
)
6917 shader_addline(buffer
, "gl_Layer = floatBitsToInt(outputs[%u])%s;\n",
6918 output
->register_idx
, reg_mask
);
6920 else if (output
->sysval_semantic
)
6922 FIXME("Unhandled sysval semantic %#x.\n", output
->sysval_semantic
);
6926 /* Then, setup the pixel shader input. */
6927 if (reg_maps_out
->shader_version
.major
< 4)
6928 shader_glsl_setup_vs3_output(priv
, gl_info
, map
, input_signature
, reg_maps_in
,
6929 output_signature
, reg_maps_out
);
6931 shader_glsl_setup_sm4_shader_output(priv
, input_count
, output_signature
, reg_maps_out
, "shader_out", TRUE
);
6934 /* Context activation is done by the caller. */
6935 static GLuint
shader_glsl_generate_vs3_rasterizer_input_setup(struct shader_glsl_priv
*priv
,
6936 const struct wined3d_shader
*vs
, const struct wined3d_shader
*ps
,
6937 BOOL per_vertex_point_size
, BOOL flatshading
, const struct wined3d_gl_info
*gl_info
)
6939 const BOOL legacy_syntax
= needs_legacy_glsl_syntax(gl_info
);
6940 DWORD ps_major
= ps
? ps
->reg_maps
.shader_version
.major
: 0;
6941 struct wined3d_string_buffer
*buffer
= &priv
->shader_buffer
;
6942 const char *semantic_name
;
6948 string_buffer_clear(buffer
);
6950 shader_glsl_add_version_declaration(buffer
, gl_info
);
6952 if (per_vertex_point_size
)
6954 shader_addline(buffer
, "uniform struct\n{\n");
6955 shader_addline(buffer
, " float size_min;\n");
6956 shader_addline(buffer
, " float size_max;\n");
6957 shader_addline(buffer
, "} ffp_point;\n");
6962 DWORD colors_written_mask
[2] = {0};
6963 DWORD texcoords_written_mask
[MAX_TEXTURES
] = {0};
6967 declare_out_varying(gl_info
, buffer
, flatshading
, "vec4 ffp_varying_diffuse;\n");
6968 declare_out_varying(gl_info
, buffer
, flatshading
, "vec4 ffp_varying_specular;\n");
6969 declare_out_varying(gl_info
, buffer
, FALSE
, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES
);
6970 declare_out_varying(gl_info
, buffer
, FALSE
, "float ffp_varying_fogcoord;\n");
6973 shader_addline(buffer
, "void setup_vs_output(in vec4 outputs[%u])\n{\n", vs
->limits
->packed_output
);
6975 for (i
= 0; i
< vs
->output_signature
.element_count
; ++i
)
6977 const struct wined3d_shader_signature_element
*output
= &vs
->output_signature
.elements
[i
];
6980 if (!(vs
->reg_maps
.output_registers
& (1u << output
->register_idx
)))
6983 semantic_name
= output
->semantic_name
;
6984 semantic_idx
= output
->semantic_idx
;
6985 write_mask
= output
->mask
;
6986 shader_glsl_write_mask_to_str(write_mask
, reg_mask
);
6988 if (shader_match_semantic(semantic_name
, WINED3D_DECL_USAGE_COLOR
) && semantic_idx
< 2)
6991 shader_addline(buffer
, "gl_Front%sColor%s = outputs[%u]%s;\n",
6992 semantic_idx
? "Secondary" : "", reg_mask
, output
->register_idx
, reg_mask
);
6994 shader_addline(buffer
, "ffp_varying_%s%s = clamp(outputs[%u]%s, 0.0, 1.0);\n",
6995 semantic_idx
? "specular" : "diffuse", reg_mask
, output
->register_idx
, reg_mask
);
6997 colors_written_mask
[semantic_idx
] = write_mask
;
6999 else if (shader_match_semantic(semantic_name
, WINED3D_DECL_USAGE_POSITION
) && !semantic_idx
)
7001 shader_addline(buffer
, "gl_Position%s = outputs[%u]%s;\n",
7002 reg_mask
, output
->register_idx
, reg_mask
);
7004 else if (shader_match_semantic(semantic_name
, WINED3D_DECL_USAGE_TEXCOORD
))
7006 if (semantic_idx
< MAX_TEXTURES
)
7008 shader_addline(buffer
, "%s[%u]%s = outputs[%u]%s;\n",
7009 legacy_syntax
? "gl_TexCoord" : "ffp_varying_texcoord",
7010 semantic_idx
, reg_mask
, output
->register_idx
, reg_mask
);
7011 texcoords_written_mask
[semantic_idx
] = write_mask
;
7014 else if (shader_match_semantic(semantic_name
, WINED3D_DECL_USAGE_PSIZE
) && per_vertex_point_size
)
7016 shader_addline(buffer
, "gl_PointSize = clamp(outputs[%u].%c, "
7017 "ffp_point.size_min, ffp_point.size_max);\n", output
->register_idx
, reg_mask
[1]);
7019 else if (shader_match_semantic(semantic_name
, WINED3D_DECL_USAGE_FOG
))
7021 shader_addline(buffer
, "%s = clamp(outputs[%u].%c, 0.0, 1.0);\n",
7022 legacy_syntax
? "gl_FogFragCoord" : "ffp_varying_fogcoord",
7023 output
->register_idx
, reg_mask
[1]);
7027 for (i
= 0; i
< 2; ++i
)
7029 if (colors_written_mask
[i
] != WINED3DSP_WRITEMASK_ALL
)
7031 shader_glsl_write_mask_to_str(~colors_written_mask
[i
] & WINED3DSP_WRITEMASK_ALL
, reg_mask
);
7033 shader_addline(buffer
, "%s%s = vec4(1.0)%s;\n",
7034 legacy_syntax
? "gl_FrontColor" : "ffp_varying_diffuse",
7035 reg_mask
, reg_mask
);
7037 shader_addline(buffer
, "%s%s = vec4(0.0)%s;\n",
7038 legacy_syntax
? "gl_FrontSecondaryColor" : "ffp_varying_specular",
7039 reg_mask
, reg_mask
);
7042 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
7044 if (ps
&& !(ps
->reg_maps
.texcoord
& (1u << i
)))
7047 if (texcoords_written_mask
[i
] != WINED3DSP_WRITEMASK_ALL
)
7049 if (gl_info
->limits
.glsl_varyings
< wined3d_max_compat_varyings(gl_info
)
7050 && !texcoords_written_mask
[i
])
7053 shader_glsl_write_mask_to_str(~texcoords_written_mask
[i
] & WINED3DSP_WRITEMASK_ALL
, reg_mask
);
7054 shader_addline(buffer
, "%s[%u]%s = vec4(0.0)%s;\n",
7055 legacy_syntax
? "gl_TexCoord" : "ffp_varying_texcoord", i
, reg_mask
, reg_mask
);
7061 unsigned int in_count
= min(vec4_varyings(ps_major
, gl_info
), ps
->limits
->packed_input
);
7063 shader_glsl_declare_shader_outputs(gl_info
, buffer
, in_count
, FALSE
, NULL
);
7064 shader_addline(buffer
, "void setup_vs_output(in vec4 outputs[%u])\n{\n", vs
->limits
->packed_output
);
7065 shader_glsl_setup_sm3_rasterizer_input(priv
, gl_info
, ps
->u
.ps
.input_reg_map
, &ps
->input_signature
,
7066 &ps
->reg_maps
, 0, &vs
->output_signature
, &vs
->reg_maps
, per_vertex_point_size
);
7069 shader_addline(buffer
, "}\n");
7071 ret
= GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER
));
7072 checkGLcall("glCreateShader(GL_VERTEX_SHADER)");
7073 shader_glsl_compile(gl_info
, ret
, buffer
->buffer
);
7078 static void shader_glsl_generate_sm4_output_setup(struct shader_glsl_priv
*priv
,
7079 const struct wined3d_shader
*shader
, unsigned int input_count
,
7080 const struct wined3d_gl_info
*gl_info
, BOOL rasterizer_setup
, const DWORD
*interpolation_mode
)
7082 const char *prefix
= shader_glsl_get_prefix(shader
->reg_maps
.shader_version
.type
);
7083 struct wined3d_string_buffer
*buffer
= &priv
->shader_buffer
;
7085 if (rasterizer_setup
)
7086 input_count
= min(vec4_varyings(4, gl_info
), input_count
);
7089 shader_glsl_declare_shader_outputs(gl_info
, buffer
, input_count
, rasterizer_setup
, interpolation_mode
);
7091 shader_addline(buffer
, "void setup_%s_output(in vec4 outputs[%u])\n{\n",
7092 prefix
, shader
->limits
->packed_output
);
7094 if (rasterizer_setup
)
7095 shader_glsl_setup_sm3_rasterizer_input(priv
, gl_info
, NULL
, NULL
,
7096 NULL
, input_count
, &shader
->output_signature
, &shader
->reg_maps
, FALSE
);
7098 shader_glsl_setup_sm4_shader_output(priv
, input_count
, &shader
->output_signature
,
7099 &shader
->reg_maps
, "shader_out", rasterizer_setup
);
7101 shader_addline(buffer
, "}\n");
7104 static void shader_glsl_generate_patch_constant_name(struct wined3d_string_buffer
*buffer
,
7105 const struct wined3d_shader_signature_element
*constant
, unsigned int *user_constant_idx
,
7106 const char *reg_mask
)
7108 if (!constant
->sysval_semantic
)
7110 shader_addline(buffer
, "user_patch_constant[%u]%s", (*user_constant_idx
)++, reg_mask
);
7114 switch (constant
->sysval_semantic
)
7116 case WINED3D_SV_TESS_FACTOR_QUADEDGE
:
7117 case WINED3D_SV_TESS_FACTOR_TRIEDGE
:
7118 case WINED3D_SV_TESS_FACTOR_LINEDET
:
7119 case WINED3D_SV_TESS_FACTOR_LINEDEN
:
7120 shader_addline(buffer
, "gl_TessLevelOuter[%u]", constant
->semantic_idx
);
7122 case WINED3D_SV_TESS_FACTOR_QUADINT
:
7123 case WINED3D_SV_TESS_FACTOR_TRIINT
:
7124 shader_addline(buffer
, "gl_TessLevelInner[%u]", constant
->semantic_idx
);
7127 FIXME("Unhandled sysval semantic %#x.\n", constant
->sysval_semantic
);
7128 shader_addline(buffer
, "vec4(0.0)%s", reg_mask
);
7132 static void shader_glsl_generate_patch_constant_setup(struct wined3d_string_buffer
*buffer
,
7133 const struct wined3d_shader_signature
*signature
, BOOL input_setup
)
7135 unsigned int i
, register_count
, user_constant_index
, user_constant_count
;
7137 register_count
= user_constant_count
= 0;
7138 for (i
= 0; i
< signature
->element_count
; ++i
)
7140 const struct wined3d_shader_signature_element
*constant
= &signature
->elements
[i
];
7141 register_count
= max(constant
->register_idx
+ 1, register_count
);
7142 if (!constant
->sysval_semantic
)
7143 ++user_constant_count
;
7146 if (user_constant_count
)
7147 shader_addline(buffer
, "patch %s vec4 user_patch_constant[%u];\n",
7148 input_setup
? "in" : "out", user_constant_count
);
7150 shader_addline(buffer
, "vec4 vpc[%u];\n", register_count
);
7152 shader_addline(buffer
, "void setup_patch_constant_%s()\n{\n", input_setup
? "input" : "output");
7153 for (i
= 0, user_constant_index
= 0; i
< signature
->element_count
; ++i
)
7155 const struct wined3d_shader_signature_element
*constant
= &signature
->elements
[i
];
7158 shader_glsl_write_mask_to_str(constant
->mask
, reg_mask
);
7161 shader_addline(buffer
, "vpc[%u]%s", constant
->register_idx
, reg_mask
);
7163 shader_glsl_generate_patch_constant_name(buffer
, constant
, &user_constant_index
, reg_mask
);
7165 shader_addline(buffer
, " = ");
7168 shader_glsl_generate_patch_constant_name(buffer
, constant
, &user_constant_index
, reg_mask
);
7170 shader_addline(buffer
, "hs_out[%u]%s", constant
->register_idx
, reg_mask
);
7172 shader_addline(buffer
, ";\n");
7174 shader_addline(buffer
, "}\n");
7177 static void shader_glsl_generate_srgb_write_correction(struct wined3d_string_buffer
*buffer
,
7178 const struct wined3d_gl_info
*gl_info
)
7180 const char *output
= get_fragment_output(gl_info
);
7182 shader_addline(buffer
, "tmp0.xyz = pow(%s[0].xyz, vec3(srgb_const0.x));\n", output
);
7183 shader_addline(buffer
, "tmp0.xyz = tmp0.xyz * vec3(srgb_const0.y) - vec3(srgb_const0.z);\n");
7184 shader_addline(buffer
, "tmp1.xyz = %s[0].xyz * vec3(srgb_const0.w);\n", output
);
7185 shader_addline(buffer
, "bvec3 srgb_compare = lessThan(%s[0].xyz, vec3(srgb_const1.x));\n", output
);
7186 shader_addline(buffer
, "%s[0].xyz = mix(tmp0.xyz, tmp1.xyz, vec3(srgb_compare));\n", output
);
7187 shader_addline(buffer
, "%s[0] = clamp(%s[0], 0.0, 1.0);\n", output
, output
);
7190 static void shader_glsl_generate_fog_code(struct wined3d_string_buffer
*buffer
,
7191 const struct wined3d_gl_info
*gl_info
, enum wined3d_ffp_ps_fog_mode mode
)
7193 const char *output
= get_fragment_output(gl_info
);
7197 case WINED3D_FFP_PS_FOG_OFF
:
7200 case WINED3D_FFP_PS_FOG_LINEAR
:
7201 shader_addline(buffer
, "float fog = (ffp_fog.end - ffp_varying_fogcoord) * ffp_fog.scale;\n");
7204 case WINED3D_FFP_PS_FOG_EXP
:
7205 shader_addline(buffer
, "float fog = exp(-ffp_fog.density * ffp_varying_fogcoord);\n");
7208 case WINED3D_FFP_PS_FOG_EXP2
:
7209 shader_addline(buffer
, "float fog = exp(-ffp_fog.density * ffp_fog.density"
7210 " * ffp_varying_fogcoord * ffp_varying_fogcoord);\n");
7214 ERR("Invalid fog mode %#x.\n", mode
);
7218 shader_addline(buffer
, "%s[0].xyz = mix(ffp_fog.color.xyz, %s[0].xyz, clamp(fog, 0.0, 1.0));\n",
7222 static void shader_glsl_generate_alpha_test(struct wined3d_string_buffer
*buffer
,
7223 const struct wined3d_gl_info
*gl_info
, enum wined3d_cmp_func alpha_func
)
7225 /* alpha_func is the PASS condition, not the DISCARD condition. Instead of
7226 * flipping all the operators here, just negate the comparison below. */
7227 static const char * const comparison_operator
[] =
7229 "", /* WINED3D_CMP_NEVER */
7230 "<", /* WINED3D_CMP_LESS */
7231 "==", /* WINED3D_CMP_EQUAL */
7232 "<=", /* WINED3D_CMP_LESSEQUAL */
7233 ">", /* WINED3D_CMP_GREATER */
7234 "!=", /* WINED3D_CMP_NOTEQUAL */
7235 ">=", /* WINED3D_CMP_GREATEREQUAL */
7236 "" /* WINED3D_CMP_ALWAYS */
7239 if (alpha_func
== WINED3D_CMP_ALWAYS
)
7242 if (alpha_func
!= WINED3D_CMP_NEVER
)
7243 shader_addline(buffer
, "if (!(%s[0].a %s alpha_test_ref))\n",
7244 get_fragment_output(gl_info
), comparison_operator
[alpha_func
- WINED3D_CMP_NEVER
]);
7245 shader_addline(buffer
, " discard;\n");
7248 static void shader_glsl_enable_extensions(struct wined3d_string_buffer
*buffer
,
7249 const struct wined3d_gl_info
*gl_info
)
7251 if (gl_info
->supported
[ARB_GPU_SHADER5
])
7252 shader_addline(buffer
, "#extension GL_ARB_gpu_shader5 : enable\n");
7253 if (gl_info
->supported
[ARB_SHADER_ATOMIC_COUNTERS
])
7254 shader_addline(buffer
, "#extension GL_ARB_shader_atomic_counters : enable\n");
7255 if (gl_info
->supported
[ARB_SHADER_BIT_ENCODING
])
7256 shader_addline(buffer
, "#extension GL_ARB_shader_bit_encoding : enable\n");
7257 if (gl_info
->supported
[ARB_SHADER_IMAGE_LOAD_STORE
])
7258 shader_addline(buffer
, "#extension GL_ARB_shader_image_load_store : enable\n");
7259 if (gl_info
->supported
[ARB_SHADER_IMAGE_SIZE
])
7260 shader_addline(buffer
, "#extension GL_ARB_shader_image_size : enable\n");
7261 if (gl_info
->supported
[ARB_SHADER_STORAGE_BUFFER_OBJECT
])
7262 shader_addline(buffer
, "#extension GL_ARB_shader_storage_buffer_object : enable\n");
7263 if (gl_info
->supported
[ARB_SHADING_LANGUAGE_420PACK
])
7264 shader_addline(buffer
, "#extension GL_ARB_shading_language_420pack : enable\n");
7265 if (gl_info
->supported
[ARB_SHADING_LANGUAGE_PACKING
])
7266 shader_addline(buffer
, "#extension GL_ARB_shading_language_packing : enable\n");
7267 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP_ARRAY
])
7268 shader_addline(buffer
, "#extension GL_ARB_texture_cube_map_array : enable\n");
7269 if (gl_info
->supported
[ARB_TEXTURE_GATHER
])
7270 shader_addline(buffer
, "#extension GL_ARB_texture_gather : enable\n");
7271 if (gl_info
->supported
[ARB_TEXTURE_QUERY_LEVELS
])
7272 shader_addline(buffer
, "#extension GL_ARB_texture_query_levels : enable\n");
7273 if (gl_info
->supported
[ARB_UNIFORM_BUFFER_OBJECT
])
7274 shader_addline(buffer
, "#extension GL_ARB_uniform_buffer_object : enable\n");
7275 if (gl_info
->supported
[EXT_GPU_SHADER4
])
7276 shader_addline(buffer
, "#extension GL_EXT_gpu_shader4 : enable\n");
7277 if (gl_info
->supported
[EXT_TEXTURE_ARRAY
])
7278 shader_addline(buffer
, "#extension GL_EXT_texture_array : enable\n");
7281 static void shader_glsl_generate_ps_epilogue(const struct wined3d_gl_info
*gl_info
,
7282 struct wined3d_string_buffer
*buffer
, const struct wined3d_shader
*shader
,
7283 const struct ps_compile_args
*args
)
7285 const struct wined3d_shader_reg_maps
*reg_maps
= &shader
->reg_maps
;
7287 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly. */
7288 if (reg_maps
->shader_version
.major
< 2)
7289 shader_addline(buffer
, "%s[0] = R0;\n", get_fragment_output(gl_info
));
7291 if (args
->srgb_correction
)
7292 shader_glsl_generate_srgb_write_correction(buffer
, gl_info
);
7294 /* SM < 3 does not replace the fog stage. */
7295 if (reg_maps
->shader_version
.major
< 3)
7296 shader_glsl_generate_fog_code(buffer
, gl_info
, args
->fog
);
7298 shader_glsl_generate_alpha_test(buffer
, gl_info
, args
->alpha_test_func
+ 1);
7301 /* Context activation is done by the caller. */
7302 static GLuint
shader_glsl_generate_pshader(const struct wined3d_context
*context
,
7303 struct wined3d_string_buffer
*buffer
, struct wined3d_string_buffer_list
*string_buffers
,
7304 const struct wined3d_shader
*shader
,
7305 const struct ps_compile_args
*args
, struct ps_np2fixup_info
*np2fixup_info
)
7307 const struct wined3d_shader_reg_maps
*reg_maps
= &shader
->reg_maps
;
7308 const struct wined3d_shader_version
*version
= ®_maps
->shader_version
;
7309 const char *prefix
= shader_glsl_get_prefix(version
->type
);
7310 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
7311 const BOOL legacy_syntax
= needs_legacy_glsl_syntax(gl_info
);
7312 unsigned int i
, extra_constants_needed
= 0;
7313 struct shader_glsl_ctx_priv priv_ctx
;
7317 memset(&priv_ctx
, 0, sizeof(priv_ctx
));
7318 priv_ctx
.cur_ps_args
= args
;
7319 priv_ctx
.cur_np2fixup_info
= np2fixup_info
;
7320 priv_ctx
.string_buffers
= string_buffers
;
7322 shader_glsl_add_version_declaration(buffer
, gl_info
);
7324 shader_glsl_enable_extensions(buffer
, gl_info
);
7325 if (gl_info
->supported
[ARB_CONSERVATIVE_DEPTH
])
7326 shader_addline(buffer
, "#extension GL_ARB_conservative_depth : enable\n");
7327 if (gl_info
->supported
[ARB_DERIVATIVE_CONTROL
])
7328 shader_addline(buffer
, "#extension GL_ARB_derivative_control : enable\n");
7329 if (shader_glsl_use_explicit_attrib_location(gl_info
))
7330 shader_addline(buffer
, "#extension GL_ARB_explicit_attrib_location : enable\n");
7331 if (gl_info
->supported
[ARB_FRAGMENT_COORD_CONVENTIONS
])
7332 shader_addline(buffer
, "#extension GL_ARB_fragment_coord_conventions : enable\n");
7333 if (gl_info
->supported
[ARB_FRAGMENT_LAYER_VIEWPORT
])
7334 shader_addline(buffer
, "#extension GL_ARB_fragment_layer_viewport : enable\n");
7335 if (gl_info
->supported
[ARB_SHADER_TEXTURE_LOD
])
7336 shader_addline(buffer
, "#extension GL_ARB_shader_texture_lod : enable\n");
7337 /* The spec says that it doesn't have to be explicitly enabled, but the
7338 * nvidia drivers write a warning if we don't do so. */
7339 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
7340 shader_addline(buffer
, "#extension GL_ARB_texture_rectangle : enable\n");
7342 /* Base Declarations */
7343 shader_generate_glsl_declarations(context
, buffer
, shader
, reg_maps
, &priv_ctx
);
7345 if (gl_info
->supported
[ARB_CONSERVATIVE_DEPTH
])
7347 if (shader
->u
.ps
.depth_output
== WINED3DSPR_DEPTHOUTGE
)
7348 shader_addline(buffer
, "layout (depth_greater) out float gl_FragDepth;\n");
7349 else if (shader
->u
.ps
.depth_output
== WINED3DSPR_DEPTHOUTLE
)
7350 shader_addline(buffer
, "layout (depth_less) out float gl_FragDepth;\n");
7353 /* Declare uniforms for NP2 texcoord fixup:
7354 * This is NOT done inside the loop that declares the texture samplers
7355 * since the NP2 fixup code is currently only used for the GeforceFX
7356 * series and when forcing the ARB_npot extension off. Modern cards just
7357 * skip the code anyway, so put it inside a separate loop. */
7358 if (args
->np2_fixup
)
7360 struct ps_np2fixup_info
*fixup
= priv_ctx
.cur_np2fixup_info
;
7361 unsigned int cur
= 0;
7363 /* NP2/RECT textures in OpenGL use texcoords in the range [0,width]x[0,height]
7364 * while D3D has them in the (normalized) [0,1]x[0,1] range.
7365 * samplerNP2Fixup stores texture dimensions and is updated through
7366 * shader_glsl_load_np2fixup_constants when the sampler changes. */
7368 for (i
= 0; i
< shader
->limits
->sampler
; ++i
)
7370 if (!reg_maps
->resource_info
[i
].type
|| !(args
->np2_fixup
& (1u << i
)))
7373 if (reg_maps
->resource_info
[i
].type
!= WINED3D_SHADER_RESOURCE_TEXTURE_2D
)
7375 FIXME("Non-2D texture is flagged for NP2 texcoord fixup.\n");
7379 fixup
->idx
[i
] = cur
++;
7382 fixup
->num_consts
= (cur
+ 1) >> 1;
7383 fixup
->active
= args
->np2_fixup
;
7384 shader_addline(buffer
, "uniform vec4 %s_samplerNP2Fixup[%u];\n", prefix
, fixup
->num_consts
);
7387 if (version
->major
< 3 || args
->vp_mode
!= vertexshader
)
7389 shader_addline(buffer
, "uniform struct\n{\n");
7390 shader_addline(buffer
, " vec4 color;\n");
7391 shader_addline(buffer
, " float density;\n");
7392 shader_addline(buffer
, " float end;\n");
7393 shader_addline(buffer
, " float scale;\n");
7394 shader_addline(buffer
, "} ffp_fog;\n");
7396 if (needs_legacy_glsl_syntax(gl_info
))
7398 if (glsl_is_color_reg_read(shader
, 0))
7399 shader_addline(buffer
, "vec4 ffp_varying_diffuse;\n");
7400 if (glsl_is_color_reg_read(shader
, 1))
7401 shader_addline(buffer
, "vec4 ffp_varying_specular;\n");
7402 shader_addline(buffer
, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES
);
7403 shader_addline(buffer
, "float ffp_varying_fogcoord;\n");
7407 if (glsl_is_color_reg_read(shader
, 0))
7408 declare_in_varying(gl_info
, buffer
, args
->flatshading
, "vec4 ffp_varying_diffuse;\n");
7409 if (glsl_is_color_reg_read(shader
, 1))
7410 declare_in_varying(gl_info
, buffer
, args
->flatshading
, "vec4 ffp_varying_specular;\n");
7411 declare_in_varying(gl_info
, buffer
, FALSE
, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES
);
7412 shader_addline(buffer
, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES
);
7413 declare_in_varying(gl_info
, buffer
, FALSE
, "float ffp_varying_fogcoord;\n");
7417 if (version
->major
>= 3)
7419 unsigned int in_count
= min(vec4_varyings(version
->major
, gl_info
), shader
->limits
->packed_input
);
7421 if (args
->vp_mode
== vertexshader
&& reg_maps
->input_registers
)
7422 shader_glsl_declare_shader_inputs(gl_info
, buffer
, in_count
,
7423 shader
->u
.ps
.interpolation_mode
, version
->major
>= 4);
7424 shader_addline(buffer
, "vec4 %s_in[%u];\n", prefix
, in_count
);
7427 for (i
= 0, map
= reg_maps
->bumpmat
; map
; map
>>= 1, ++i
)
7432 shader_addline(buffer
, "uniform mat2 bumpenv_mat%u;\n", i
);
7434 if (reg_maps
->luminanceparams
& (1u << i
))
7436 shader_addline(buffer
, "uniform float bumpenv_lum_scale%u;\n", i
);
7437 shader_addline(buffer
, "uniform float bumpenv_lum_offset%u;\n", i
);
7438 extra_constants_needed
++;
7441 extra_constants_needed
++;
7444 if (args
->srgb_correction
)
7446 shader_addline(buffer
, "const vec4 srgb_const0 = ");
7447 shader_glsl_append_imm_vec4(buffer
, wined3d_srgb_const0
);
7448 shader_addline(buffer
, ";\n");
7449 shader_addline(buffer
, "const vec4 srgb_const1 = ");
7450 shader_glsl_append_imm_vec4(buffer
, wined3d_srgb_const1
);
7451 shader_addline(buffer
, ";\n");
7453 if (reg_maps
->vpos
|| reg_maps
->usesdsy
)
7455 if (reg_maps
->usesdsy
|| !gl_info
->supported
[ARB_FRAGMENT_COORD_CONVENTIONS
])
7457 ++extra_constants_needed
;
7458 shader_addline(buffer
, "uniform vec4 ycorrection;\n");
7462 if (gl_info
->supported
[ARB_FRAGMENT_COORD_CONVENTIONS
])
7464 if (context
->d3d_info
->wined3d_creation_flags
& WINED3D_PIXEL_CENTER_INTEGER
)
7465 shader_addline(buffer
, "layout(%spixel_center_integer) in vec4 gl_FragCoord;\n",
7466 args
->render_offscreen
? "" : "origin_upper_left, ");
7467 else if (!args
->render_offscreen
)
7468 shader_addline(buffer
, "layout(origin_upper_left) in vec4 gl_FragCoord;\n");
7470 shader_addline(buffer
, "vec4 vpos;\n");
7474 if (args
->alpha_test_func
+ 1 != WINED3D_CMP_ALWAYS
)
7475 shader_addline(buffer
, "uniform float alpha_test_ref;\n");
7477 if (!needs_legacy_glsl_syntax(gl_info
))
7479 if (shader_glsl_use_explicit_attrib_location(gl_info
))
7480 shader_addline(buffer
, "layout(location = 0) ");
7481 shader_addline(buffer
, "out vec4 ps_out[%u];\n", gl_info
->limits
.buffers
);
7484 if (shader
->limits
->constant_float
+ extra_constants_needed
>= gl_info
->limits
.glsl_ps_float_constants
)
7485 FIXME("Insufficient uniforms to run this shader.\n");
7487 if (shader
->u
.ps
.force_early_depth_stencil
)
7488 shader_addline(buffer
, "layout(early_fragment_tests) in;\n");
7490 shader_addline(buffer
, "void main()\n{\n");
7492 /* Direct3D applications expect integer vPos values, while OpenGL drivers
7493 * add approximately 0.5. This causes off-by-one problems as spotted by
7494 * the vPos d3d9 visual test. Unfortunately ATI cards do not add exactly
7495 * 0.5, but rather something like 0.49999999 or 0.50000001, which still
7496 * causes precision troubles when we just subtract 0.5.
7498 * To deal with that, just floor() the position. This will eliminate the
7499 * fraction on all cards.
7501 * TODO: Test how this behaves with multisampling.
7503 * An advantage of floor is that it works even if the driver doesn't add
7504 * 0.5. It is somewhat questionable if 1.5, 2.5, ... are the proper values
7505 * to return in gl_FragCoord, even though coordinates specify the pixel
7506 * centers instead of the pixel corners. This code will behave correctly
7507 * on drivers that returns integer values. */
7510 if (gl_info
->supported
[ARB_FRAGMENT_COORD_CONVENTIONS
])
7511 shader_addline(buffer
, "vpos = gl_FragCoord;\n");
7512 else if (context
->d3d_info
->wined3d_creation_flags
& WINED3D_PIXEL_CENTER_INTEGER
)
7513 shader_addline(buffer
,
7514 "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
7516 shader_addline(buffer
,
7517 "vpos = vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1);\n");
7520 if (reg_maps
->shader_version
.major
< 3 || args
->vp_mode
!= vertexshader
)
7523 WORD map
= reg_maps
->texcoord
;
7527 if (glsl_is_color_reg_read(shader
, 0))
7528 shader_addline(buffer
, "ffp_varying_diffuse = gl_Color;\n");
7529 if (glsl_is_color_reg_read(shader
, 1))
7530 shader_addline(buffer
, "ffp_varying_specular = gl_SecondaryColor;\n");
7533 for (i
= 0; map
; map
>>= 1, ++i
)
7537 if (args
->pointsprite
)
7538 shader_addline(buffer
, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", i
);
7539 else if (args
->texcoords_initialized
& (1u << i
))
7540 shader_addline(buffer
, "ffp_texcoord[%u] = %s[%u];\n", i
,
7541 legacy_syntax
? "gl_TexCoord" : "ffp_varying_texcoord", i
);
7543 shader_addline(buffer
, "ffp_texcoord[%u] = vec4(0.0);\n", i
);
7544 shader_addline(buffer
, "vec4 T%u = ffp_texcoord[%u];\n", i
, i
);
7549 shader_addline(buffer
, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
7552 /* Pack 3.0 inputs */
7553 if (reg_maps
->shader_version
.major
>= 3)
7554 shader_glsl_input_pack(shader
, buffer
, &shader
->input_signature
, reg_maps
, args
, gl_info
,
7555 reg_maps
->shader_version
.major
>= 4);
7557 /* Base Shader Body */
7558 if (FAILED(shader_generate_code(shader
, buffer
, reg_maps
, &priv_ctx
, NULL
, NULL
)))
7561 /* In SM4+ the shader epilogue is generated by the "ret" instruction. */
7562 if (reg_maps
->shader_version
.major
< 4)
7563 shader_glsl_generate_ps_epilogue(gl_info
, buffer
, shader
, args
);
7565 shader_addline(buffer
, "}\n");
7567 shader_id
= GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER
));
7568 TRACE("Compiling shader object %u.\n", shader_id
);
7569 shader_glsl_compile(gl_info
, shader_id
, buffer
->buffer
);
7574 static void shader_glsl_generate_vs_epilogue(const struct wined3d_gl_info
*gl_info
,
7575 struct wined3d_string_buffer
*buffer
, const struct wined3d_shader
*shader
,
7576 const struct vs_compile_args
*args
)
7578 const struct wined3d_shader_reg_maps
*reg_maps
= &shader
->reg_maps
;
7579 const BOOL legacy_syntax
= needs_legacy_glsl_syntax(gl_info
);
7582 /* Unpack outputs. */
7583 shader_addline(buffer
, "setup_vs_output(vs_out);\n");
7585 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
7586 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
7587 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
7588 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0).
7590 if (reg_maps
->shader_version
.major
< 3)
7592 if (args
->fog_src
== VS_FOG_Z
)
7593 shader_addline(buffer
, "%s = gl_Position.z;\n",
7594 legacy_syntax
? "gl_FogFragCoord" : "ffp_varying_fogcoord");
7595 else if (!reg_maps
->fog
)
7596 shader_addline(buffer
, "%s = 0.0;\n",
7597 legacy_syntax
? "gl_FogFragCoord" : "ffp_varying_fogcoord");
7600 /* We always store the clipplanes without y inversion. */
7601 if (args
->clip_enabled
)
7604 shader_addline(buffer
, "gl_ClipVertex = gl_Position;\n");
7606 for (i
= 0; i
< gl_info
->limits
.user_clip_distances
; ++i
)
7607 shader_addline(buffer
, "gl_ClipDistance[%u] = dot(gl_Position, clip_planes[%u]);\n", i
, i
);
7610 if (args
->point_size
&& !args
->per_vertex_point_size
)
7611 shader_addline(buffer
, "gl_PointSize = clamp(ffp_point.size, ffp_point.size_min, ffp_point.size_max);\n");
7613 if (args
->next_shader_type
== WINED3D_SHADER_TYPE_PIXEL
&& !gl_info
->supported
[ARB_CLIP_CONTROL
])
7614 shader_glsl_fixup_position(buffer
);
7617 /* Context activation is done by the caller. */
7618 static GLuint
shader_glsl_generate_vshader(const struct wined3d_context
*context
,
7619 struct shader_glsl_priv
*priv
, const struct wined3d_shader
*shader
, const struct vs_compile_args
*args
)
7621 struct wined3d_string_buffer_list
*string_buffers
= &priv
->string_buffers
;
7622 const struct wined3d_shader_reg_maps
*reg_maps
= &shader
->reg_maps
;
7623 const struct wined3d_shader_version
*version
= ®_maps
->shader_version
;
7624 struct wined3d_string_buffer
*buffer
= &priv
->shader_buffer
;
7625 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
7626 struct shader_glsl_ctx_priv priv_ctx
;
7630 memset(&priv_ctx
, 0, sizeof(priv_ctx
));
7631 priv_ctx
.cur_vs_args
= args
;
7632 priv_ctx
.string_buffers
= string_buffers
;
7634 shader_glsl_add_version_declaration(buffer
, gl_info
);
7636 shader_glsl_enable_extensions(buffer
, gl_info
);
7637 if (gl_info
->supported
[ARB_DRAW_INSTANCED
])
7638 shader_addline(buffer
, "#extension GL_ARB_draw_instanced : enable\n");
7639 if (shader_glsl_use_explicit_attrib_location(gl_info
))
7640 shader_addline(buffer
, "#extension GL_ARB_explicit_attrib_location : enable\n");
7642 /* Base Declarations */
7643 shader_generate_glsl_declarations(context
, buffer
, shader
, reg_maps
, &priv_ctx
);
7645 for (i
= 0; i
< shader
->input_signature
.element_count
; ++i
)
7646 shader_glsl_declare_generic_vertex_attribute(buffer
, gl_info
, &shader
->input_signature
.elements
[i
]);
7648 if (args
->point_size
&& !args
->per_vertex_point_size
)
7650 shader_addline(buffer
, "uniform struct\n{\n");
7651 shader_addline(buffer
, " float size;\n");
7652 shader_addline(buffer
, " float size_min;\n");
7653 shader_addline(buffer
, " float size_max;\n");
7654 shader_addline(buffer
, "} ffp_point;\n");
7657 if (!needs_legacy_glsl_syntax(gl_info
))
7659 if (args
->clip_enabled
)
7660 shader_addline(buffer
, "uniform vec4 clip_planes[%u];\n", gl_info
->limits
.user_clip_distances
);
7662 if (version
->major
< 3)
7664 declare_out_varying(gl_info
, buffer
, args
->flatshading
, "vec4 ffp_varying_diffuse;\n");
7665 declare_out_varying(gl_info
, buffer
, args
->flatshading
, "vec4 ffp_varying_specular;\n");
7666 declare_out_varying(gl_info
, buffer
, FALSE
, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES
);
7667 declare_out_varying(gl_info
, buffer
, FALSE
, "float ffp_varying_fogcoord;\n");
7671 if (version
->major
< 4)
7672 shader_addline(buffer
, "void setup_vs_output(in vec4[%u]);\n", shader
->limits
->packed_output
);
7674 if (args
->next_shader_type
== WINED3D_SHADER_TYPE_PIXEL
&& !gl_info
->supported
[ARB_CLIP_CONTROL
])
7675 shader_addline(buffer
, "uniform vec4 pos_fixup;\n");
7677 if (reg_maps
->shader_version
.major
>= 4)
7678 shader_glsl_generate_sm4_output_setup(priv
, shader
, args
->next_shader_input_count
,
7679 gl_info
, args
->next_shader_type
== WINED3D_SHADER_TYPE_PIXEL
, args
->interpolation_mode
);
7681 shader_addline(buffer
, "void main()\n{\n");
7683 /* Base Shader Body */
7684 if (FAILED(shader_generate_code(shader
, buffer
, reg_maps
, &priv_ctx
, NULL
, NULL
)))
7687 /* In SM4+ the shader epilogue is generated by the "ret" instruction. */
7688 if (reg_maps
->shader_version
.major
< 4)
7689 shader_glsl_generate_vs_epilogue(gl_info
, buffer
, shader
, args
);
7691 shader_addline(buffer
, "}\n");
7693 shader_id
= GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER
));
7694 TRACE("Compiling shader object %u.\n", shader_id
);
7695 shader_glsl_compile(gl_info
, shader_id
, buffer
->buffer
);
7700 static void shader_glsl_generate_default_control_point_phase(const struct wined3d_shader
*shader
,
7701 struct wined3d_string_buffer
*buffer
, const struct wined3d_shader_reg_maps
*reg_maps
)
7703 const struct wined3d_shader_signature
*output_signature
= &shader
->output_signature
;
7707 for (i
= 0; i
< output_signature
->element_count
; ++i
)
7709 const struct wined3d_shader_signature_element
*output
= &output_signature
->elements
[i
];
7711 shader_glsl_write_mask_to_str(output
->mask
, reg_mask
);
7712 shader_addline(buffer
, "shader_out[gl_InvocationID].reg[%u]%s = shader_in[gl_InvocationID].reg[%u]%s;\n",
7713 output
->register_idx
, reg_mask
, output
->register_idx
, reg_mask
);
7717 static HRESULT
shader_glsl_generate_shader_phase(const struct wined3d_shader
*shader
,
7718 struct wined3d_string_buffer
*buffer
, const struct wined3d_shader_reg_maps
*reg_maps
,
7719 struct shader_glsl_ctx_priv
*priv_ctx
, const struct wined3d_shader_phase
*phase
,
7720 const char *phase_name
, unsigned phase_idx
)
7725 shader_addline(buffer
, "void hs_%s_phase%u(%s)\n{\n",
7726 phase_name
, phase_idx
, phase
->instance_count
? "int phase_instance_id" : "");
7727 for (i
= 0; i
< phase
->temporary_count
; ++i
)
7728 shader_addline(buffer
, "vec4 R%u;\n", i
);
7729 hr
= shader_generate_code(shader
, buffer
, reg_maps
, priv_ctx
, phase
->start
, phase
->end
);
7730 shader_addline(buffer
, "}\n");
7734 static void shader_glsl_generate_shader_phase_invocation(struct wined3d_string_buffer
*buffer
,
7735 const struct wined3d_shader_phase
*phase
, const char *phase_name
, unsigned int phase_idx
)
7737 if (phase
->instance_count
)
7739 shader_addline(buffer
, "for (int i = 0; i < %u; ++i)\n{\n", phase
->instance_count
);
7740 shader_addline(buffer
, "hs_%s_phase%u(i);\n", phase_name
, phase_idx
);
7741 shader_addline(buffer
, "}\n");
7745 shader_addline(buffer
, "hs_%s_phase%u();\n", phase_name
, phase_idx
);
7749 static GLuint
shader_glsl_generate_hull_shader(const struct wined3d_context
*context
,
7750 struct shader_glsl_priv
*priv
, const struct wined3d_shader
*shader
)
7752 struct wined3d_string_buffer_list
*string_buffers
= &priv
->string_buffers
;
7753 const struct wined3d_shader_reg_maps
*reg_maps
= &shader
->reg_maps
;
7754 struct wined3d_string_buffer
*buffer
= &priv
->shader_buffer
;
7755 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
7756 const struct wined3d_hull_shader
*hs
= &shader
->u
.hs
;
7757 const struct wined3d_shader_phase
*phase
;
7758 struct shader_glsl_ctx_priv priv_ctx
;
7762 memset(&priv_ctx
, 0, sizeof(priv_ctx
));
7763 priv_ctx
.string_buffers
= string_buffers
;
7765 shader_glsl_add_version_declaration(buffer
, gl_info
);
7767 shader_glsl_enable_extensions(buffer
, gl_info
);
7768 shader_addline(buffer
, "#extension GL_ARB_tessellation_shader : enable\n");
7770 shader_generate_glsl_declarations(context
, buffer
, shader
, reg_maps
, &priv_ctx
);
7772 shader_addline(buffer
, "layout(vertices = %u) out;\n", hs
->output_vertex_count
);
7774 shader_addline(buffer
, "in shader_in_out { vec4 reg[%u]; } shader_in[];\n", shader
->limits
->packed_input
);
7775 shader_addline(buffer
, "out shader_in_out { vec4 reg[%u]; } shader_out[];\n", shader
->limits
->packed_output
);
7777 shader_glsl_generate_patch_constant_setup(buffer
, &shader
->patch_constant_signature
, FALSE
);
7779 if (hs
->phases
.control_point
)
7781 shader_addline(buffer
, "void setup_hs_output(in vec4 outputs[%u])\n{\n",
7782 shader
->limits
->packed_output
);
7783 shader_glsl_setup_sm4_shader_output(priv
, shader
->limits
->packed_output
, &shader
->output_signature
,
7784 &shader
->reg_maps
, "shader_out[gl_InvocationID]", FALSE
);
7785 shader_addline(buffer
, "}\n");
7788 shader_addline(buffer
, "void hs_control_point_phase()\n{\n");
7789 if ((phase
= hs
->phases
.control_point
))
7791 for (i
= 0; i
< phase
->temporary_count
; ++i
)
7792 shader_addline(buffer
, "vec4 R%u;\n", i
);
7793 if (FAILED(shader_generate_code(shader
, buffer
, reg_maps
, &priv_ctx
, phase
->start
, phase
->end
)))
7795 shader_addline(buffer
, "setup_hs_output(hs_out);\n");
7799 shader_glsl_generate_default_control_point_phase(shader
, buffer
, reg_maps
);
7801 shader_addline(buffer
, "}\n");
7803 for (i
= 0; i
< hs
->phases
.fork_count
; ++i
)
7805 if (FAILED(shader_glsl_generate_shader_phase(shader
, buffer
, reg_maps
, &priv_ctx
,
7806 &hs
->phases
.fork
[i
], "fork", i
)))
7810 for (i
= 0; i
< hs
->phases
.join_count
; ++i
)
7812 if (FAILED(shader_glsl_generate_shader_phase(shader
, buffer
, reg_maps
, &priv_ctx
,
7813 &hs
->phases
.join
[i
], "join", i
)))
7817 shader_addline(buffer
, "void main()\n{\n");
7818 shader_addline(buffer
, "hs_control_point_phase();\n");
7820 shader_addline(buffer
, "barrier();\n");
7821 for (i
= 0; i
< hs
->phases
.fork_count
; ++i
)
7822 shader_glsl_generate_shader_phase_invocation(buffer
, &hs
->phases
.fork
[i
], "fork", i
);
7823 for (i
= 0; i
< hs
->phases
.join_count
; ++i
)
7824 shader_glsl_generate_shader_phase_invocation(buffer
, &hs
->phases
.join
[i
], "join", i
);
7825 shader_addline(buffer
, "setup_patch_constant_output();\n");
7826 shader_addline(buffer
, "}\n");
7828 shader_id
= GL_EXTCALL(glCreateShader(GL_TESS_CONTROL_SHADER
));
7829 TRACE("Compiling shader object %u.\n", shader_id
);
7830 shader_glsl_compile(gl_info
, shader_id
, buffer
->buffer
);
7835 static void shader_glsl_generate_ds_epilogue(const struct wined3d_gl_info
*gl_info
,
7836 struct wined3d_string_buffer
*buffer
, const struct wined3d_shader
*shader
,
7837 const struct ds_compile_args
*args
)
7839 shader_addline(buffer
, "setup_ds_output(ds_out);\n");
7841 if (args
->next_shader_type
== WINED3D_SHADER_TYPE_PIXEL
&& !gl_info
->supported
[ARB_CLIP_CONTROL
])
7842 shader_glsl_fixup_position(buffer
);
7845 static GLuint
shader_glsl_generate_domain_shader(const struct wined3d_context
*context
,
7846 struct shader_glsl_priv
*priv
, const struct wined3d_shader
*shader
, const struct ds_compile_args
*args
)
7848 struct wined3d_string_buffer_list
*string_buffers
= &priv
->string_buffers
;
7849 const struct wined3d_shader_reg_maps
*reg_maps
= &shader
->reg_maps
;
7850 struct wined3d_string_buffer
*buffer
= &priv
->shader_buffer
;
7851 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
7852 struct shader_glsl_ctx_priv priv_ctx
;
7855 memset(&priv_ctx
, 0, sizeof(priv_ctx
));
7856 priv_ctx
.cur_ds_args
= args
;
7857 priv_ctx
.string_buffers
= string_buffers
;
7859 shader_glsl_add_version_declaration(buffer
, gl_info
);
7861 shader_glsl_enable_extensions(buffer
, gl_info
);
7862 shader_addline(buffer
, "#extension GL_ARB_tessellation_shader : enable\n");
7864 shader_generate_glsl_declarations(context
, buffer
, shader
, reg_maps
, &priv_ctx
);
7866 shader_addline(buffer
, "layout(");
7867 switch (shader
->u
.ds
.tessellator_domain
)
7869 case WINED3D_TESSELLATOR_DOMAIN_LINE
:
7870 shader_addline(buffer
, "isolines");
7872 case WINED3D_TESSELLATOR_DOMAIN_QUAD
:
7873 shader_addline(buffer
, "quads");
7875 case WINED3D_TESSELLATOR_DOMAIN_TRIANGLE
:
7876 shader_addline(buffer
, "triangles");
7879 switch (args
->tessellator_output_primitive
)
7881 case WINED3D_TESSELLATOR_OUTPUT_TRIANGLE_CW
:
7882 if (args
->render_offscreen
)
7883 shader_addline(buffer
, ", ccw");
7885 shader_addline(buffer
, ", cw");
7887 case WINED3D_TESSELLATOR_OUTPUT_TRIANGLE_CCW
:
7888 if (args
->render_offscreen
)
7889 shader_addline(buffer
, ", cw");
7891 shader_addline(buffer
, ", ccw");
7893 case WINED3D_TESSELLATOR_OUTPUT_POINT
:
7894 shader_addline(buffer
, ", point_mode");
7896 case WINED3D_TESSELLATOR_OUTPUT_LINE
:
7899 switch (args
->tessellator_partitioning
)
7901 case WINED3D_TESSELLATOR_PARTITIONING_FRACTIONAL_ODD
:
7902 shader_addline(buffer
, ", fractional_odd_spacing");
7904 case WINED3D_TESSELLATOR_PARTITIONING_FRACTIONAL_EVEN
:
7905 shader_addline(buffer
, ", fractional_even_spacing");
7907 case WINED3D_TESSELLATOR_PARTITIONING_INTEGER
:
7908 case WINED3D_TESSELLATOR_PARTITIONING_POW2
:
7909 shader_addline(buffer
, ", equal_spacing");
7912 shader_addline(buffer
, ") in;\n");
7914 shader_addline(buffer
, "in shader_in_out { vec4 reg[%u]; } shader_in[];\n", shader
->limits
->packed_input
);
7916 if (args
->next_shader_type
== WINED3D_SHADER_TYPE_PIXEL
&& !gl_info
->supported
[ARB_CLIP_CONTROL
])
7917 shader_addline(buffer
, "uniform vec4 pos_fixup;\n");
7919 shader_glsl_generate_sm4_output_setup(priv
, shader
, args
->output_count
, gl_info
,
7920 args
->next_shader_type
== WINED3D_SHADER_TYPE_PIXEL
, args
->interpolation_mode
);
7921 shader_glsl_generate_patch_constant_setup(buffer
, &shader
->patch_constant_signature
, TRUE
);
7923 shader_addline(buffer
, "void main()\n{\n");
7924 shader_addline(buffer
, "setup_patch_constant_input();\n");
7926 if (FAILED(shader_generate_code(shader
, buffer
, reg_maps
, &priv_ctx
, NULL
, NULL
)))
7929 shader_addline(buffer
, "}\n");
7931 shader_id
= GL_EXTCALL(glCreateShader(GL_TESS_EVALUATION_SHADER
));
7932 TRACE("Compiling shader object %u.\n", shader_id
);
7933 shader_glsl_compile(gl_info
, shader_id
, buffer
->buffer
);
7938 /* Context activation is done by the caller. */
7939 static GLuint
shader_glsl_generate_geometry_shader(const struct wined3d_context
*context
,
7940 struct shader_glsl_priv
*priv
, const struct wined3d_shader
*shader
, const struct gs_compile_args
*args
)
7942 struct wined3d_string_buffer_list
*string_buffers
= &priv
->string_buffers
;
7943 const struct wined3d_shader_reg_maps
*reg_maps
= &shader
->reg_maps
;
7944 struct wined3d_string_buffer
*buffer
= &priv
->shader_buffer
;
7945 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
7946 struct shader_glsl_ctx_priv priv_ctx
;
7949 memset(&priv_ctx
, 0, sizeof(priv_ctx
));
7950 priv_ctx
.string_buffers
= string_buffers
;
7952 shader_glsl_add_version_declaration(buffer
, gl_info
);
7954 shader_glsl_enable_extensions(buffer
, gl_info
);
7956 shader_generate_glsl_declarations(context
, buffer
, shader
, reg_maps
, &priv_ctx
);
7958 shader_addline(buffer
, "layout(%s", glsl_primitive_type_from_d3d(shader
->u
.gs
.input_type
));
7959 if (shader
->u
.gs
.instance_count
> 1)
7960 shader_addline(buffer
, ", invocations = %u", shader
->u
.gs
.instance_count
);
7961 shader_addline(buffer
, ") in;\n");
7962 shader_addline(buffer
, "layout(%s, max_vertices = %u) out;\n",
7963 glsl_primitive_type_from_d3d(shader
->u
.gs
.output_type
), shader
->u
.gs
.vertices_out
);
7964 shader_addline(buffer
, "in shader_in_out { vec4 reg[%u]; } shader_in[];\n", shader
->limits
->packed_input
);
7966 if (!gl_info
->supported
[ARB_CLIP_CONTROL
])
7967 shader_addline(buffer
, "uniform vec4 pos_fixup;\n");
7969 shader_glsl_generate_sm4_output_setup(priv
, shader
, args
->output_count
,
7970 gl_info
, TRUE
, args
->interpolation_mode
);
7971 shader_addline(buffer
, "void main()\n{\n");
7972 if (FAILED(shader_generate_code(shader
, buffer
, reg_maps
, &priv_ctx
, NULL
, NULL
)))
7974 shader_addline(buffer
, "}\n");
7976 shader_id
= GL_EXTCALL(glCreateShader(GL_GEOMETRY_SHADER
));
7977 TRACE("Compiling shader object %u.\n", shader_id
);
7978 shader_glsl_compile(gl_info
, shader_id
, buffer
->buffer
);
7983 static void shader_glsl_generate_shader_epilogue(const struct wined3d_shader_context
*ctx
)
7985 const struct shader_glsl_ctx_priv
*priv
= ctx
->backend_data
;
7986 const struct wined3d_gl_info
*gl_info
= ctx
->gl_info
;
7987 struct wined3d_string_buffer
*buffer
= ctx
->buffer
;
7988 const struct wined3d_shader
*shader
= ctx
->shader
;
7990 switch (shader
->reg_maps
.shader_version
.type
)
7992 case WINED3D_SHADER_TYPE_PIXEL
:
7993 shader_glsl_generate_ps_epilogue(gl_info
, buffer
, shader
, priv
->cur_ps_args
);
7995 case WINED3D_SHADER_TYPE_VERTEX
:
7996 shader_glsl_generate_vs_epilogue(gl_info
, buffer
, shader
, priv
->cur_vs_args
);
7998 case WINED3D_SHADER_TYPE_DOMAIN
:
7999 shader_glsl_generate_ds_epilogue(gl_info
, buffer
, shader
, priv
->cur_ds_args
);
8001 case WINED3D_SHADER_TYPE_GEOMETRY
:
8002 case WINED3D_SHADER_TYPE_COMPUTE
:
8005 FIXME("Unhandled shader type %#x.\n", shader
->reg_maps
.shader_version
.type
);
8010 /* Context activation is done by the caller. */
8011 static GLuint
shader_glsl_generate_compute_shader(const struct wined3d_context
*context
,
8012 struct wined3d_string_buffer
*buffer
, struct wined3d_string_buffer_list
*string_buffers
,
8013 const struct wined3d_shader
*shader
)
8015 const struct wined3d_shader_thread_group_size
*thread_group_size
= &shader
->u
.cs
.thread_group_size
;
8016 const struct wined3d_shader_reg_maps
*reg_maps
= &shader
->reg_maps
;
8017 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
8018 struct shader_glsl_ctx_priv priv_ctx
;
8022 memset(&priv_ctx
, 0, sizeof(priv_ctx
));
8023 priv_ctx
.string_buffers
= string_buffers
;
8025 shader_glsl_add_version_declaration(buffer
, gl_info
);
8027 shader_glsl_enable_extensions(buffer
, gl_info
);
8028 shader_addline(buffer
, "#extension GL_ARB_compute_shader : enable\n");
8030 shader_generate_glsl_declarations(context
, buffer
, shader
, reg_maps
, &priv_ctx
);
8032 for (i
= 0; i
< reg_maps
->tgsm_count
; ++i
)
8034 if (reg_maps
->tgsm
[i
].size
)
8035 shader_addline(buffer
, "shared uint cs_g%u[%u];\n", i
, reg_maps
->tgsm
[i
].size
);
8038 shader_addline(buffer
, "layout(local_size_x = %u, local_size_y = %u, local_size_z = %u) in;\n",
8039 thread_group_size
->x
, thread_group_size
->y
, thread_group_size
->z
);
8041 shader_addline(buffer
, "void main()\n{\n");
8042 shader_generate_code(shader
, buffer
, reg_maps
, &priv_ctx
, NULL
, NULL
);
8043 shader_addline(buffer
, "}\n");
8045 shader_id
= GL_EXTCALL(glCreateShader(GL_COMPUTE_SHADER
));
8046 TRACE("Compiling shader object %u.\n", shader_id
);
8047 shader_glsl_compile(gl_info
, shader_id
, buffer
->buffer
);
8052 static GLuint
find_glsl_pshader(const struct wined3d_context
*context
,
8053 struct wined3d_string_buffer
*buffer
, struct wined3d_string_buffer_list
*string_buffers
,
8054 struct wined3d_shader
*shader
,
8055 const struct ps_compile_args
*args
, const struct ps_np2fixup_info
**np2fixup_info
)
8057 struct glsl_ps_compiled_shader
*gl_shaders
, *new_array
;
8058 struct glsl_shader_private
*shader_data
;
8059 struct ps_np2fixup_info
*np2fixup
;
8064 if (!shader
->backend_data
)
8066 shader
->backend_data
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*shader_data
));
8067 if (!shader
->backend_data
)
8069 ERR("Failed to allocate backend data.\n");
8073 shader_data
= shader
->backend_data
;
8074 gl_shaders
= shader_data
->gl_shaders
.ps
;
8076 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
8077 * so a linear search is more performant than a hashmap or a binary search
8078 * (cache coherency etc)
8080 for (i
= 0; i
< shader_data
->num_gl_shaders
; ++i
)
8082 if (!memcmp(&gl_shaders
[i
].args
, args
, sizeof(*args
)))
8084 if (args
->np2_fixup
)
8085 *np2fixup_info
= &gl_shaders
[i
].np2fixup
;
8086 return gl_shaders
[i
].id
;
8090 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader
);
8091 if(shader_data
->shader_array_size
== shader_data
->num_gl_shaders
) {
8092 if (shader_data
->num_gl_shaders
)
8094 new_size
= shader_data
->shader_array_size
+ max(1, shader_data
->shader_array_size
/ 2);
8095 new_array
= HeapReAlloc(GetProcessHeap(), 0, shader_data
->gl_shaders
.ps
,
8096 new_size
* sizeof(*gl_shaders
));
8100 new_array
= HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders
));
8105 ERR("Out of memory\n");
8108 shader_data
->gl_shaders
.ps
= new_array
;
8109 shader_data
->shader_array_size
= new_size
;
8110 gl_shaders
= new_array
;
8113 gl_shaders
[shader_data
->num_gl_shaders
].args
= *args
;
8115 np2fixup
= &gl_shaders
[shader_data
->num_gl_shaders
].np2fixup
;
8116 memset(np2fixup
, 0, sizeof(*np2fixup
));
8117 *np2fixup_info
= args
->np2_fixup
? np2fixup
: NULL
;
8119 pixelshader_update_resource_types(shader
, args
->tex_types
);
8121 string_buffer_clear(buffer
);
8122 ret
= shader_glsl_generate_pshader(context
, buffer
, string_buffers
, shader
, args
, np2fixup
);
8123 gl_shaders
[shader_data
->num_gl_shaders
++].id
= ret
;
8128 static inline BOOL
vs_args_equal(const struct vs_compile_args
*stored
, const struct vs_compile_args
*new,
8129 const DWORD use_map
)
8131 if((stored
->swizzle_map
& use_map
) != new->swizzle_map
) return FALSE
;
8132 if((stored
->clip_enabled
) != new->clip_enabled
) return FALSE
;
8133 if (stored
->point_size
!= new->point_size
)
8135 if (stored
->per_vertex_point_size
!= new->per_vertex_point_size
)
8137 if (stored
->flatshading
!= new->flatshading
)
8139 if (stored
->next_shader_type
!= new->next_shader_type
)
8141 if (stored
->next_shader_input_count
!= new->next_shader_input_count
)
8143 return stored
->fog_src
== new->fog_src
;
8146 static GLuint
find_glsl_vshader(const struct wined3d_context
*context
, struct shader_glsl_priv
*priv
,
8147 struct wined3d_shader
*shader
, const struct vs_compile_args
*args
)
8151 DWORD use_map
= context
->stream_info
.use_map
;
8152 struct glsl_vs_compiled_shader
*gl_shaders
, *new_array
;
8153 struct glsl_shader_private
*shader_data
;
8156 if (!shader
->backend_data
)
8158 shader
->backend_data
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*shader_data
));
8159 if (!shader
->backend_data
)
8161 ERR("Failed to allocate backend data.\n");
8165 shader_data
= shader
->backend_data
;
8166 gl_shaders
= shader_data
->gl_shaders
.vs
;
8168 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
8169 * so a linear search is more performant than a hashmap or a binary search
8170 * (cache coherency etc)
8172 for (i
= 0; i
< shader_data
->num_gl_shaders
; ++i
)
8174 if (vs_args_equal(&gl_shaders
[i
].args
, args
, use_map
))
8175 return gl_shaders
[i
].id
;
8178 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader
);
8180 if(shader_data
->shader_array_size
== shader_data
->num_gl_shaders
) {
8181 if (shader_data
->num_gl_shaders
)
8183 new_size
= shader_data
->shader_array_size
+ max(1, shader_data
->shader_array_size
/ 2);
8184 new_array
= HeapReAlloc(GetProcessHeap(), 0, shader_data
->gl_shaders
.vs
,
8185 new_size
* sizeof(*gl_shaders
));
8189 new_array
= HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders
));
8194 ERR("Out of memory\n");
8197 shader_data
->gl_shaders
.vs
= new_array
;
8198 shader_data
->shader_array_size
= new_size
;
8199 gl_shaders
= new_array
;
8202 gl_shaders
[shader_data
->num_gl_shaders
].args
= *args
;
8204 string_buffer_clear(&priv
->shader_buffer
);
8205 ret
= shader_glsl_generate_vshader(context
, priv
, shader
, args
);
8206 gl_shaders
[shader_data
->num_gl_shaders
++].id
= ret
;
8211 static GLuint
find_glsl_hull_shader(const struct wined3d_context
*context
,
8212 struct shader_glsl_priv
*priv
, struct wined3d_shader
*shader
)
8214 struct glsl_hs_compiled_shader
*gl_shaders
, *new_array
;
8215 struct glsl_shader_private
*shader_data
;
8216 unsigned int new_size
;
8219 if (!shader
->backend_data
)
8221 if (!(shader
->backend_data
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*shader_data
))))
8223 ERR("Failed to allocate backend data.\n");
8227 shader_data
= shader
->backend_data
;
8228 gl_shaders
= shader_data
->gl_shaders
.hs
;
8230 if (shader_data
->num_gl_shaders
> 0)
8232 assert(shader_data
->num_gl_shaders
== 1);
8233 return gl_shaders
[0].id
;
8236 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader
);
8238 assert(!shader_data
->gl_shaders
.hs
);
8239 new_array
= HeapAlloc(GetProcessHeap(), 0, sizeof(*new_array
));
8243 ERR("Failed to allocate GL shaders array.\n");
8246 shader_data
->gl_shaders
.hs
= new_array
;
8247 shader_data
->shader_array_size
= new_size
;
8248 gl_shaders
= new_array
;
8250 string_buffer_clear(&priv
->shader_buffer
);
8251 ret
= shader_glsl_generate_hull_shader(context
, priv
, shader
);
8252 gl_shaders
[shader_data
->num_gl_shaders
++].id
= ret
;
8257 static GLuint
find_glsl_domain_shader(const struct wined3d_context
*context
,
8258 struct shader_glsl_priv
*priv
, struct wined3d_shader
*shader
, const struct ds_compile_args
*args
)
8260 struct glsl_ds_compiled_shader
*gl_shaders
, *new_array
;
8261 struct glsl_shader_private
*shader_data
;
8262 unsigned int i
, new_size
;
8265 if (!shader
->backend_data
)
8267 if (!(shader
->backend_data
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*shader_data
))))
8269 ERR("Failed to allocate backend data.\n");
8273 shader_data
= shader
->backend_data
;
8274 gl_shaders
= shader_data
->gl_shaders
.ds
;
8276 for (i
= 0; i
< shader_data
->num_gl_shaders
; ++i
)
8278 if (!memcmp(&gl_shaders
[i
].args
, args
, sizeof(*args
)))
8279 return gl_shaders
[i
].id
;
8282 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader
);
8284 if (shader_data
->num_gl_shaders
)
8286 new_size
= shader_data
->shader_array_size
+ 1;
8287 new_array
= HeapReAlloc(GetProcessHeap(), 0, shader_data
->gl_shaders
.ds
,
8288 new_size
* sizeof(*new_array
));
8292 new_array
= HeapAlloc(GetProcessHeap(), 0, sizeof(*new_array
));
8298 ERR("Failed to allocate GL shaders array.\n");
8301 shader_data
->gl_shaders
.ds
= new_array
;
8302 shader_data
->shader_array_size
= new_size
;
8303 gl_shaders
= new_array
;
8305 string_buffer_clear(&priv
->shader_buffer
);
8306 ret
= shader_glsl_generate_domain_shader(context
, priv
, shader
, args
);
8307 gl_shaders
[shader_data
->num_gl_shaders
].args
= *args
;
8308 gl_shaders
[shader_data
->num_gl_shaders
++].id
= ret
;
8313 static GLuint
find_glsl_geometry_shader(const struct wined3d_context
*context
,
8314 struct shader_glsl_priv
*priv
, struct wined3d_shader
*shader
, const struct gs_compile_args
*args
)
8316 struct glsl_gs_compiled_shader
*gl_shaders
, *new_array
;
8317 struct glsl_shader_private
*shader_data
;
8318 unsigned int i
, new_size
;
8321 if (!shader
->backend_data
)
8323 if (!(shader
->backend_data
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*shader_data
))))
8325 ERR("Failed to allocate backend data.\n");
8329 shader_data
= shader
->backend_data
;
8330 gl_shaders
= shader_data
->gl_shaders
.gs
;
8332 for (i
= 0; i
< shader_data
->num_gl_shaders
; ++i
)
8334 if (!memcmp(&gl_shaders
[i
].args
, args
, sizeof(*args
)))
8335 return gl_shaders
[i
].id
;
8338 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader
);
8340 if (shader_data
->num_gl_shaders
)
8342 new_size
= shader_data
->shader_array_size
+ 1;
8343 new_array
= HeapReAlloc(GetProcessHeap(), 0, shader_data
->gl_shaders
.gs
,
8344 new_size
* sizeof(*new_array
));
8348 new_array
= HeapAlloc(GetProcessHeap(), 0, sizeof(*new_array
));
8354 ERR("Failed to allocate GL shaders array.\n");
8357 shader_data
->gl_shaders
.gs
= new_array
;
8358 shader_data
->shader_array_size
= new_size
;
8359 gl_shaders
= new_array
;
8361 string_buffer_clear(&priv
->shader_buffer
);
8362 ret
= shader_glsl_generate_geometry_shader(context
, priv
, shader
, args
);
8363 gl_shaders
[shader_data
->num_gl_shaders
].args
= *args
;
8364 gl_shaders
[shader_data
->num_gl_shaders
++].id
= ret
;
8369 static const char *shader_glsl_ffp_mcs(enum wined3d_material_color_source mcs
, const char *material
)
8373 case WINED3D_MCS_MATERIAL
:
8375 case WINED3D_MCS_COLOR1
:
8376 return "ffp_attrib_diffuse";
8377 case WINED3D_MCS_COLOR2
:
8378 return "ffp_attrib_specular";
8380 ERR("Invalid material color source %#x.\n", mcs
);
8385 static void shader_glsl_ffp_vertex_lighting_footer(struct wined3d_string_buffer
*buffer
,
8386 const struct wined3d_ffp_vs_settings
*settings
, unsigned int idx
)
8388 shader_addline(buffer
, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
8389 " * ffp_light[%u].diffuse.xyz * att;\n", idx
);
8390 if (settings
->localviewer
)
8391 shader_addline(buffer
, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
8393 shader_addline(buffer
, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
8394 shader_addline(buffer
, "if (dot(dir, normal) > 0.0 && t > 0.0) specular +="
8395 " pow(t, ffp_material.shininess) * ffp_light[%u].specular * att;\n", idx
);
8398 static void shader_glsl_ffp_vertex_lighting(struct wined3d_string_buffer
*buffer
,
8399 const struct wined3d_ffp_vs_settings
*settings
, BOOL legacy_lighting
)
8401 const char *diffuse
, *specular
, *emissive
, *ambient
;
8402 unsigned int i
, idx
;
8404 if (!settings
->lighting
)
8406 shader_addline(buffer
, "ffp_varying_diffuse = ffp_attrib_diffuse;\n");
8407 shader_addline(buffer
, "ffp_varying_specular = ffp_attrib_specular;\n");
8411 shader_addline(buffer
, "vec3 ambient = ffp_light_ambient;\n");
8412 shader_addline(buffer
, "vec3 diffuse = vec3(0.0);\n");
8413 shader_addline(buffer
, "vec4 specular = vec4(0.0);\n");
8414 shader_addline(buffer
, "vec3 dir, dst;\n");
8415 shader_addline(buffer
, "float att, t;\n");
8417 ambient
= shader_glsl_ffp_mcs(settings
->ambient_source
, "ffp_material.ambient");
8418 diffuse
= shader_glsl_ffp_mcs(settings
->diffuse_source
, "ffp_material.diffuse");
8419 specular
= shader_glsl_ffp_mcs(settings
->specular_source
, "ffp_material.specular");
8420 emissive
= shader_glsl_ffp_mcs(settings
->emissive_source
, "ffp_material.emissive");
8423 for (i
= 0; i
< settings
->point_light_count
; ++i
, ++idx
)
8425 shader_addline(buffer
, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", idx
);
8426 shader_addline(buffer
, "dst.z = dot(dir, dir);\n");
8427 shader_addline(buffer
, "dst.y = sqrt(dst.z);\n");
8428 shader_addline(buffer
, "dst.x = 1.0;\n");
8429 if (legacy_lighting
)
8431 shader_addline(buffer
, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", idx
, idx
);
8432 shader_addline(buffer
, "dst.z = dst.y * dst.y;\n");
8433 shader_addline(buffer
, "if (dst.y > 0.0)\n{\n");
8437 shader_addline(buffer
, "if (dst.y <= ffp_light[%u].range)\n{\n", idx
);
8439 shader_addline(buffer
, "att = dot(dst.xyz, vec3(ffp_light[%u].c_att,"
8440 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n", idx
, idx
, idx
);
8441 if (!legacy_lighting
)
8442 shader_addline(buffer
, "att = 1.0 / att;\n");
8443 shader_addline(buffer
, "ambient += ffp_light[%u].ambient.xyz * att;\n", idx
);
8444 if (!settings
->normal
)
8446 shader_addline(buffer
, "}\n");
8449 shader_addline(buffer
, "dir = normalize(dir);\n");
8450 shader_glsl_ffp_vertex_lighting_footer(buffer
, settings
, idx
);
8451 shader_addline(buffer
, "}\n");
8454 for (i
= 0; i
< settings
->spot_light_count
; ++i
, ++idx
)
8456 shader_addline(buffer
, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", idx
);
8457 shader_addline(buffer
, "dst.z = dot(dir, dir);\n");
8458 shader_addline(buffer
, "dst.y = sqrt(dst.z);\n");
8459 shader_addline(buffer
, "dst.x = 1.0;\n");
8460 if (legacy_lighting
)
8462 shader_addline(buffer
, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", idx
, idx
);
8463 shader_addline(buffer
, "dst.z = dst.y * dst.y;\n");
8464 shader_addline(buffer
, "if (dst.y > 0.0)\n{\n");
8468 shader_addline(buffer
, "if (dst.y <= ffp_light[%u].range)\n{\n", idx
);
8470 shader_addline(buffer
, "dir = normalize(dir);\n");
8471 shader_addline(buffer
, "t = dot(-dir, normalize(ffp_light[%u].direction));\n", idx
);
8472 shader_addline(buffer
, "if (t > ffp_light[%u].cos_htheta) att = 1.0;\n", idx
);
8473 shader_addline(buffer
, "else if (t <= ffp_light[%u].cos_hphi) att = 0.0;\n", idx
);
8474 shader_addline(buffer
, "else att = pow((t - ffp_light[%u].cos_hphi)"
8475 " / (ffp_light[%u].cos_htheta - ffp_light[%u].cos_hphi), ffp_light[%u].falloff);\n",
8476 idx
, idx
, idx
, idx
);
8477 if (legacy_lighting
)
8478 shader_addline(buffer
, "att *= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
8479 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
8482 shader_addline(buffer
, "att /= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
8483 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
8485 shader_addline(buffer
, "ambient += ffp_light[%u].ambient.xyz * att;\n", idx
);
8486 if (!settings
->normal
)
8488 shader_addline(buffer
, "}\n");
8491 shader_glsl_ffp_vertex_lighting_footer(buffer
, settings
, idx
);
8492 shader_addline(buffer
, "}\n");
8495 for (i
= 0; i
< settings
->directional_light_count
; ++i
, ++idx
)
8497 shader_addline(buffer
, "ambient += ffp_light[%u].ambient.xyz;\n", idx
);
8498 if (!settings
->normal
)
8500 shader_addline(buffer
, "att = 1.0;\n");
8501 shader_addline(buffer
, "dir = normalize(ffp_light[%u].direction.xyz);\n", idx
);
8502 shader_glsl_ffp_vertex_lighting_footer(buffer
, settings
, idx
);
8505 for (i
= 0; i
< settings
->parallel_point_light_count
; ++i
, ++idx
)
8507 shader_addline(buffer
, "ambient += ffp_light[%u].ambient.xyz;\n", idx
);
8508 if (!settings
->normal
)
8510 shader_addline(buffer
, "att = 1.0;\n");
8511 shader_addline(buffer
, "dir = normalize(ffp_light[%u].position.xyz);\n", idx
);
8512 shader_glsl_ffp_vertex_lighting_footer(buffer
, settings
, idx
);
8515 shader_addline(buffer
, "ffp_varying_diffuse.xyz = %s.xyz * ambient + %s.xyz * diffuse + %s.xyz;\n",
8516 ambient
, diffuse
, emissive
);
8517 shader_addline(buffer
, "ffp_varying_diffuse.w = %s.w;\n", diffuse
);
8518 shader_addline(buffer
, "ffp_varying_specular = %s * specular;\n", specular
);
8521 /* Context activation is done by the caller. */
8522 static GLuint
shader_glsl_generate_ffp_vertex_shader(struct shader_glsl_priv
*priv
,
8523 const struct wined3d_ffp_vs_settings
*settings
, const struct wined3d_gl_info
*gl_info
)
8525 static const struct attrib_info
8528 const char name
[24];
8532 {"vec4", "ffp_attrib_position"}, /* WINED3D_FFP_POSITION */
8533 {"vec4", "ffp_attrib_blendweight"}, /* WINED3D_FFP_BLENDWEIGHT */
8534 /* TODO: Indexed vertex blending */
8535 {"float", ""}, /* WINED3D_FFP_BLENDINDICES */
8536 {"vec3", "ffp_attrib_normal"}, /* WINED3D_FFP_NORMAL */
8537 {"float", "ffp_attrib_psize"}, /* WINED3D_FFP_PSIZE */
8538 {"vec4", "ffp_attrib_diffuse"}, /* WINED3D_FFP_DIFFUSE */
8539 {"vec4", "ffp_attrib_specular"}, /* WINED3D_FFP_SPECULAR */
8541 const BOOL legacy_syntax
= needs_legacy_glsl_syntax(gl_info
);
8542 struct wined3d_string_buffer
*buffer
= &priv
->shader_buffer
;
8543 BOOL output_legacy_fogcoord
= legacy_syntax
;
8544 BOOL legacy_lighting
= priv
->legacy_lighting
;
8548 string_buffer_clear(buffer
);
8550 shader_glsl_add_version_declaration(buffer
, gl_info
);
8552 if (shader_glsl_use_explicit_attrib_location(gl_info
))
8553 shader_addline(buffer
, "#extension GL_ARB_explicit_attrib_location : enable\n");
8555 for (i
= 0; i
< WINED3D_FFP_ATTRIBS_COUNT
; ++i
)
8557 const char *type
= i
< ARRAY_SIZE(attrib_info
) ? attrib_info
[i
].type
: "vec4";
8559 if (shader_glsl_use_explicit_attrib_location(gl_info
))
8560 shader_addline(buffer
, "layout(location = %u) ", i
);
8561 shader_addline(buffer
, "%s %s vs_in%u;\n", get_attribute_keyword(gl_info
), type
, i
);
8563 shader_addline(buffer
, "\n");
8565 shader_addline(buffer
, "uniform mat4 ffp_modelview_matrix[%u];\n", MAX_VERTEX_BLENDS
);
8566 shader_addline(buffer
, "uniform mat4 ffp_projection_matrix;\n");
8567 shader_addline(buffer
, "uniform mat3 ffp_normal_matrix;\n");
8568 shader_addline(buffer
, "uniform mat4 ffp_texture_matrix[%u];\n", MAX_TEXTURES
);
8570 shader_addline(buffer
, "uniform struct\n{\n");
8571 shader_addline(buffer
, " vec4 emissive;\n");
8572 shader_addline(buffer
, " vec4 ambient;\n");
8573 shader_addline(buffer
, " vec4 diffuse;\n");
8574 shader_addline(buffer
, " vec4 specular;\n");
8575 shader_addline(buffer
, " float shininess;\n");
8576 shader_addline(buffer
, "} ffp_material;\n");
8578 shader_addline(buffer
, "uniform vec3 ffp_light_ambient;\n");
8579 shader_addline(buffer
, "uniform struct\n{\n");
8580 shader_addline(buffer
, " vec4 diffuse;\n");
8581 shader_addline(buffer
, " vec4 specular;\n");
8582 shader_addline(buffer
, " vec4 ambient;\n");
8583 shader_addline(buffer
, " vec4 position;\n");
8584 shader_addline(buffer
, " vec3 direction;\n");
8585 shader_addline(buffer
, " float range;\n");
8586 shader_addline(buffer
, " float falloff;\n");
8587 shader_addline(buffer
, " float c_att;\n");
8588 shader_addline(buffer
, " float l_att;\n");
8589 shader_addline(buffer
, " float q_att;\n");
8590 shader_addline(buffer
, " float cos_htheta;\n");
8591 shader_addline(buffer
, " float cos_hphi;\n");
8592 shader_addline(buffer
, "} ffp_light[%u];\n", MAX_ACTIVE_LIGHTS
);
8594 if (settings
->point_size
)
8596 shader_addline(buffer
, "uniform struct\n{\n");
8597 shader_addline(buffer
, " float size;\n");
8598 shader_addline(buffer
, " float size_min;\n");
8599 shader_addline(buffer
, " float size_max;\n");
8600 shader_addline(buffer
, " float c_att;\n");
8601 shader_addline(buffer
, " float l_att;\n");
8602 shader_addline(buffer
, " float q_att;\n");
8603 shader_addline(buffer
, "} ffp_point;\n");
8608 shader_addline(buffer
, "vec4 ffp_varying_diffuse;\n");
8609 shader_addline(buffer
, "vec4 ffp_varying_specular;\n");
8610 shader_addline(buffer
, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES
);
8611 shader_addline(buffer
, "float ffp_varying_fogcoord;\n");
8615 if (settings
->clipping
)
8616 shader_addline(buffer
, "uniform vec4 clip_planes[%u];\n", gl_info
->limits
.user_clip_distances
);
8618 declare_out_varying(gl_info
, buffer
, settings
->flatshading
, "vec4 ffp_varying_diffuse;\n");
8619 declare_out_varying(gl_info
, buffer
, settings
->flatshading
, "vec4 ffp_varying_specular;\n");
8620 declare_out_varying(gl_info
, buffer
, FALSE
, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES
);
8621 declare_out_varying(gl_info
, buffer
, FALSE
, "float ffp_varying_fogcoord;\n");
8624 shader_addline(buffer
, "\nvoid main()\n{\n");
8625 shader_addline(buffer
, "float m;\n");
8626 shader_addline(buffer
, "vec3 r;\n");
8628 for (i
= 0; i
< ARRAY_SIZE(attrib_info
); ++i
)
8630 if (attrib_info
[i
].name
[0])
8631 shader_addline(buffer
, "%s %s = vs_in%u%s;\n", attrib_info
[i
].type
, attrib_info
[i
].name
,
8632 i
, settings
->swizzle_map
& (1u << i
) ? ".zyxw" : "");
8634 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
8636 unsigned int coord_idx
= settings
->texgen
[i
] & 0x0000ffff;
8637 if ((settings
->texgen
[i
] & 0xffff0000) == WINED3DTSS_TCI_PASSTHRU
8638 && settings
->texcoords
& (1u << i
))
8639 shader_addline(buffer
, "vec4 ffp_attrib_texcoord%u = vs_in%u;\n", i
, coord_idx
+ WINED3D_FFP_TEXCOORD0
);
8642 shader_addline(buffer
, "ffp_attrib_blendweight[%u] = 1.0;\n", settings
->vertexblends
);
8644 if (settings
->transformed
)
8646 shader_addline(buffer
, "vec4 ec_pos = vec4(ffp_attrib_position.xyz, 1.0);\n");
8647 shader_addline(buffer
, "gl_Position = ffp_projection_matrix * ec_pos;\n");
8648 shader_addline(buffer
, "if (ffp_attrib_position.w != 0.0) gl_Position /= ffp_attrib_position.w;\n");
8652 for (i
= 0; i
< settings
->vertexblends
; ++i
)
8653 shader_addline(buffer
, "ffp_attrib_blendweight[%u] -= ffp_attrib_blendweight[%u];\n", settings
->vertexblends
, i
);
8655 shader_addline(buffer
, "vec4 ec_pos = vec4(0.0);\n");
8656 for (i
= 0; i
< settings
->vertexblends
+ 1; ++i
)
8657 shader_addline(buffer
, "ec_pos += ffp_attrib_blendweight[%u] * (ffp_modelview_matrix[%u] * ffp_attrib_position);\n", i
, i
);
8659 shader_addline(buffer
, "gl_Position = ffp_projection_matrix * ec_pos;\n");
8660 if (settings
->clipping
)
8663 shader_addline(buffer
, "gl_ClipVertex = ec_pos;\n");
8665 for (i
= 0; i
< gl_info
->limits
.user_clip_distances
; ++i
)
8666 shader_addline(buffer
, "gl_ClipDistance[%u] = dot(ec_pos, clip_planes[%u]);\n", i
, i
);
8668 shader_addline(buffer
, "ec_pos /= ec_pos.w;\n");
8671 shader_addline(buffer
, "vec3 normal = vec3(0.0);\n");
8672 if (settings
->normal
)
8674 if (!settings
->vertexblends
)
8676 shader_addline(buffer
, "normal = ffp_normal_matrix * ffp_attrib_normal;\n");
8680 for (i
= 0; i
< settings
->vertexblends
+ 1; ++i
)
8681 shader_addline(buffer
, "normal += ffp_attrib_blendweight[%u] * (mat3(ffp_modelview_matrix[%u]) * ffp_attrib_normal);\n", i
, i
);
8684 if (settings
->normalize
)
8685 shader_addline(buffer
, "normal = normalize(normal);\n");
8688 shader_glsl_ffp_vertex_lighting(buffer
, settings
, legacy_lighting
);
8691 shader_addline(buffer
, "gl_FrontColor = ffp_varying_diffuse;\n");
8692 shader_addline(buffer
, "gl_FrontSecondaryColor = ffp_varying_specular;\n");
8696 shader_addline(buffer
, "ffp_varying_diffuse = clamp(ffp_varying_diffuse, 0.0, 1.0);\n");
8697 shader_addline(buffer
, "ffp_varying_specular = clamp(ffp_varying_specular, 0.0, 1.0);\n");
8700 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
8702 BOOL output_legacy_texcoord
= legacy_syntax
;
8704 switch (settings
->texgen
[i
] & 0xffff0000)
8706 case WINED3DTSS_TCI_PASSTHRU
:
8707 if (settings
->texcoords
& (1u << i
))
8708 shader_addline(buffer
, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ffp_attrib_texcoord%u;\n",
8710 else if (gl_info
->limits
.glsl_varyings
>= wined3d_max_compat_varyings(gl_info
))
8711 shader_addline(buffer
, "ffp_varying_texcoord[%u] = vec4(0.0);\n", i
);
8713 output_legacy_texcoord
= FALSE
;
8716 case WINED3DTSS_TCI_CAMERASPACENORMAL
:
8717 shader_addline(buffer
, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * vec4(normal, 1.0);\n", i
, i
);
8720 case WINED3DTSS_TCI_CAMERASPACEPOSITION
:
8721 shader_addline(buffer
, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ec_pos;\n", i
, i
);
8724 case WINED3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR
:
8725 shader_addline(buffer
, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
8726 " * vec4(reflect(normalize(ec_pos.xyz), normal), 1.0);\n", i
, i
);
8729 case WINED3DTSS_TCI_SPHEREMAP
:
8730 shader_addline(buffer
, "r = reflect(normalize(ec_pos.xyz), normal);\n");
8731 shader_addline(buffer
, "m = 2.0 * length(vec3(r.x, r.y, r.z + 1.0));\n");
8732 shader_addline(buffer
, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
8733 " * vec4(r.x / m + 0.5, r.y / m + 0.5, 0.0, 1.0);\n", i
, i
);
8737 ERR("Unhandled texgen %#x.\n", settings
->texgen
[i
]);
8740 if (output_legacy_texcoord
)
8741 shader_addline(buffer
, "gl_TexCoord[%u] = ffp_varying_texcoord[%u];\n", i
, i
);
8744 switch (settings
->fog_mode
)
8746 case WINED3D_FFP_VS_FOG_OFF
:
8747 output_legacy_fogcoord
= FALSE
;
8750 case WINED3D_FFP_VS_FOG_FOGCOORD
:
8751 shader_addline(buffer
, "ffp_varying_fogcoord = ffp_attrib_specular.w * 255.0;\n");
8754 case WINED3D_FFP_VS_FOG_RANGE
:
8755 shader_addline(buffer
, "ffp_varying_fogcoord = length(ec_pos.xyz);\n");
8758 case WINED3D_FFP_VS_FOG_DEPTH
:
8759 if (settings
->ortho_fog
)
8761 if (gl_info
->supported
[ARB_CLIP_CONTROL
])
8762 shader_addline(buffer
, "ffp_varying_fogcoord = gl_Position.z;\n");
8764 /* Need to undo the [0.0 - 1.0] -> [-1.0 - 1.0] transformation from D3D to GL coordinates. */
8765 shader_addline(buffer
, "ffp_varying_fogcoord = gl_Position.z * 0.5 + 0.5;\n");
8767 else if (settings
->transformed
)
8769 shader_addline(buffer
, "ffp_varying_fogcoord = ec_pos.z;\n");
8773 shader_addline(buffer
, "ffp_varying_fogcoord = abs(ec_pos.z);\n");
8778 ERR("Unhandled fog mode %#x.\n", settings
->fog_mode
);
8781 if (output_legacy_fogcoord
)
8782 shader_addline(buffer
, "gl_FogFragCoord = ffp_varying_fogcoord;\n");
8784 if (settings
->point_size
)
8786 shader_addline(buffer
, "gl_PointSize = %s / sqrt(ffp_point.c_att"
8787 " + ffp_point.l_att * length(ec_pos.xyz)"
8788 " + ffp_point.q_att * dot(ec_pos.xyz, ec_pos.xyz));\n",
8789 settings
->per_vertex_point_size
? "ffp_attrib_psize" : "ffp_point.size");
8790 shader_addline(buffer
, "gl_PointSize = clamp(gl_PointSize, ffp_point.size_min, ffp_point.size_max);\n");
8793 shader_addline(buffer
, "}\n");
8795 shader_obj
= GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER
));
8796 shader_glsl_compile(gl_info
, shader_obj
, buffer
->buffer
);
8801 static const char *shader_glsl_get_ffp_fragment_op_arg(struct wined3d_string_buffer
*buffer
,
8802 DWORD argnum
, unsigned int stage
, DWORD arg
)
8806 if (arg
== ARG_UNUSED
)
8807 return "<unused arg>";
8809 switch (arg
& WINED3DTA_SELECTMASK
)
8811 case WINED3DTA_DIFFUSE
:
8812 ret
= "ffp_varying_diffuse";
8815 case WINED3DTA_CURRENT
:
8819 case WINED3DTA_TEXTURE
:
8822 case 0: ret
= "tex0"; break;
8823 case 1: ret
= "tex1"; break;
8824 case 2: ret
= "tex2"; break;
8825 case 3: ret
= "tex3"; break;
8826 case 4: ret
= "tex4"; break;
8827 case 5: ret
= "tex5"; break;
8828 case 6: ret
= "tex6"; break;
8829 case 7: ret
= "tex7"; break;
8831 ret
= "<invalid texture>";
8836 case WINED3DTA_TFACTOR
:
8840 case WINED3DTA_SPECULAR
:
8841 ret
= "ffp_varying_specular";
8844 case WINED3DTA_TEMP
:
8848 case WINED3DTA_CONSTANT
:
8851 case 0: ret
= "tss_const0"; break;
8852 case 1: ret
= "tss_const1"; break;
8853 case 2: ret
= "tss_const2"; break;
8854 case 3: ret
= "tss_const3"; break;
8855 case 4: ret
= "tss_const4"; break;
8856 case 5: ret
= "tss_const5"; break;
8857 case 6: ret
= "tss_const6"; break;
8858 case 7: ret
= "tss_const7"; break;
8860 ret
= "<invalid constant>";
8866 return "<unhandled arg>";
8869 if (arg
& WINED3DTA_COMPLEMENT
)
8871 shader_addline(buffer
, "arg%u = vec4(1.0) - %s;\n", argnum
, ret
);
8874 else if (argnum
== 1)
8876 else if (argnum
== 2)
8880 if (arg
& WINED3DTA_ALPHAREPLICATE
)
8882 shader_addline(buffer
, "arg%u = vec4(%s.w);\n", argnum
, ret
);
8885 else if (argnum
== 1)
8887 else if (argnum
== 2)
8894 static void shader_glsl_ffp_fragment_op(struct wined3d_string_buffer
*buffer
, unsigned int stage
, BOOL color
,
8895 BOOL alpha
, DWORD dst
, DWORD op
, DWORD dw_arg0
, DWORD dw_arg1
, DWORD dw_arg2
)
8897 const char *dstmask
, *dstreg
, *arg0
, *arg1
, *arg2
;
8907 dstreg
= "temp_reg";
8911 arg0
= shader_glsl_get_ffp_fragment_op_arg(buffer
, 0, stage
, dw_arg0
);
8912 arg1
= shader_glsl_get_ffp_fragment_op_arg(buffer
, 1, stage
, dw_arg1
);
8913 arg2
= shader_glsl_get_ffp_fragment_op_arg(buffer
, 2, stage
, dw_arg2
);
8917 case WINED3D_TOP_DISABLE
:
8920 case WINED3D_TOP_SELECT_ARG1
:
8921 shader_addline(buffer
, "%s%s = %s%s;\n", dstreg
, dstmask
, arg1
, dstmask
);
8924 case WINED3D_TOP_SELECT_ARG2
:
8925 shader_addline(buffer
, "%s%s = %s%s;\n", dstreg
, dstmask
, arg2
, dstmask
);
8928 case WINED3D_TOP_MODULATE
:
8929 shader_addline(buffer
, "%s%s = %s%s * %s%s;\n", dstreg
, dstmask
, arg1
, dstmask
, arg2
, dstmask
);
8932 case WINED3D_TOP_MODULATE_4X
:
8933 shader_addline(buffer
, "%s%s = clamp(%s%s * %s%s * 4.0, 0.0, 1.0);\n",
8934 dstreg
, dstmask
, arg1
, dstmask
, arg2
, dstmask
);
8937 case WINED3D_TOP_MODULATE_2X
:
8938 shader_addline(buffer
, "%s%s = clamp(%s%s * %s%s * 2.0, 0.0, 1.0);\n",
8939 dstreg
, dstmask
, arg1
, dstmask
, arg2
, dstmask
);
8942 case WINED3D_TOP_ADD
:
8943 shader_addline(buffer
, "%s%s = clamp(%s%s + %s%s, 0.0, 1.0);\n",
8944 dstreg
, dstmask
, arg1
, dstmask
, arg2
, dstmask
);
8947 case WINED3D_TOP_ADD_SIGNED
:
8948 shader_addline(buffer
, "%s%s = clamp(%s%s + (%s - vec4(0.5))%s, 0.0, 1.0);\n",
8949 dstreg
, dstmask
, arg1
, dstmask
, arg2
, dstmask
);
8952 case WINED3D_TOP_ADD_SIGNED_2X
:
8953 shader_addline(buffer
, "%s%s = clamp((%s%s + (%s - vec4(0.5))%s) * 2.0, 0.0, 1.0);\n",
8954 dstreg
, dstmask
, arg1
, dstmask
, arg2
, dstmask
);
8957 case WINED3D_TOP_SUBTRACT
:
8958 shader_addline(buffer
, "%s%s = clamp(%s%s - %s%s, 0.0, 1.0);\n",
8959 dstreg
, dstmask
, arg1
, dstmask
, arg2
, dstmask
);
8962 case WINED3D_TOP_ADD_SMOOTH
:
8963 shader_addline(buffer
, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s%s, 0.0, 1.0);\n",
8964 dstreg
, dstmask
, arg1
, dstmask
, arg2
, dstmask
, arg1
, dstmask
);
8967 case WINED3D_TOP_BLEND_DIFFUSE_ALPHA
:
8968 arg0
= shader_glsl_get_ffp_fragment_op_arg(buffer
, 0, stage
, WINED3DTA_DIFFUSE
);
8969 shader_addline(buffer
, "%s%s = mix(%s%s, %s%s, %s.w);\n",
8970 dstreg
, dstmask
, arg2
, dstmask
, arg1
, dstmask
, arg0
);
8973 case WINED3D_TOP_BLEND_TEXTURE_ALPHA
:
8974 arg0
= shader_glsl_get_ffp_fragment_op_arg(buffer
, 0, stage
, WINED3DTA_TEXTURE
);
8975 shader_addline(buffer
, "%s%s = mix(%s%s, %s%s, %s.w);\n",
8976 dstreg
, dstmask
, arg2
, dstmask
, arg1
, dstmask
, arg0
);
8979 case WINED3D_TOP_BLEND_FACTOR_ALPHA
:
8980 arg0
= shader_glsl_get_ffp_fragment_op_arg(buffer
, 0, stage
, WINED3DTA_TFACTOR
);
8981 shader_addline(buffer
, "%s%s = mix(%s%s, %s%s, %s.w);\n",
8982 dstreg
, dstmask
, arg2
, dstmask
, arg1
, dstmask
, arg0
);
8985 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM
:
8986 arg0
= shader_glsl_get_ffp_fragment_op_arg(buffer
, 0, stage
, WINED3DTA_TEXTURE
);
8987 shader_addline(buffer
, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
8988 dstreg
, dstmask
, arg2
, dstmask
, arg0
, arg1
, dstmask
);
8991 case WINED3D_TOP_BLEND_CURRENT_ALPHA
:
8992 arg0
= shader_glsl_get_ffp_fragment_op_arg(buffer
, 0, stage
, WINED3DTA_CURRENT
);
8993 shader_addline(buffer
, "%s%s = mix(%s%s, %s%s, %s.w);\n",
8994 dstreg
, dstmask
, arg2
, dstmask
, arg1
, dstmask
, arg0
);
8997 case WINED3D_TOP_MODULATE_ALPHA_ADD_COLOR
:
8998 shader_addline(buffer
, "%s%s = clamp(%s%s * %s.w + %s%s, 0.0, 1.0);\n",
8999 dstreg
, dstmask
, arg2
, dstmask
, arg1
, arg1
, dstmask
);
9002 case WINED3D_TOP_MODULATE_COLOR_ADD_ALPHA
:
9003 shader_addline(buffer
, "%s%s = clamp(%s%s * %s%s + %s.w, 0.0, 1.0);\n",
9004 dstreg
, dstmask
, arg1
, dstmask
, arg2
, dstmask
, arg1
);
9007 case WINED3D_TOP_MODULATE_INVALPHA_ADD_COLOR
:
9008 shader_addline(buffer
, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
9009 dstreg
, dstmask
, arg2
, dstmask
, arg1
, arg1
, dstmask
);
9011 case WINED3D_TOP_MODULATE_INVCOLOR_ADD_ALPHA
:
9012 shader_addline(buffer
, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s.w, 0.0, 1.0);\n",
9013 dstreg
, dstmask
, arg1
, dstmask
, arg2
, dstmask
, arg1
);
9016 case WINED3D_TOP_BUMPENVMAP
:
9017 case WINED3D_TOP_BUMPENVMAP_LUMINANCE
:
9018 /* These are handled in the first pass, nothing to do. */
9021 case WINED3D_TOP_DOTPRODUCT3
:
9022 shader_addline(buffer
, "%s%s = vec4(clamp(dot(%s.xyz - 0.5, %s.xyz - 0.5) * 4.0, 0.0, 1.0))%s;\n",
9023 dstreg
, dstmask
, arg1
, arg2
, dstmask
);
9026 case WINED3D_TOP_MULTIPLY_ADD
:
9027 shader_addline(buffer
, "%s%s = clamp(%s%s * %s%s + %s%s, 0.0, 1.0);\n",
9028 dstreg
, dstmask
, arg1
, dstmask
, arg2
, dstmask
, arg0
, dstmask
);
9031 case WINED3D_TOP_LERP
:
9032 /* MSDN isn't quite right here. */
9033 shader_addline(buffer
, "%s%s = mix(%s%s, %s%s, %s%s);\n",
9034 dstreg
, dstmask
, arg2
, dstmask
, arg1
, dstmask
, arg0
, dstmask
);
9038 FIXME("Unhandled operation %#x.\n", op
);
9043 /* Context activation is done by the caller. */
9044 static GLuint
shader_glsl_generate_ffp_fragment_shader(struct shader_glsl_priv
*priv
,
9045 const struct ffp_frag_settings
*settings
, const struct wined3d_context
*context
)
9047 struct wined3d_string_buffer
*tex_reg_name
= string_buffer_get(&priv
->string_buffers
);
9048 enum wined3d_cmp_func alpha_test_func
= settings
->alpha_test_func
+ 1;
9049 struct wined3d_string_buffer
*buffer
= &priv
->shader_buffer
;
9050 BYTE lum_map
= 0, bump_map
= 0, tex_map
= 0, tss_const_map
= 0;
9051 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
9052 const BOOL legacy_syntax
= needs_legacy_glsl_syntax(gl_info
);
9053 BOOL tempreg_used
= FALSE
, tfactor_used
= FALSE
;
9054 UINT lowest_disabled_stage
;
9056 DWORD arg0
, arg1
, arg2
;
9059 string_buffer_clear(buffer
);
9061 /* Find out which textures are read */
9062 for (stage
= 0; stage
< MAX_TEXTURES
; ++stage
)
9064 if (settings
->op
[stage
].cop
== WINED3D_TOP_DISABLE
)
9067 arg0
= settings
->op
[stage
].carg0
& WINED3DTA_SELECTMASK
;
9068 arg1
= settings
->op
[stage
].carg1
& WINED3DTA_SELECTMASK
;
9069 arg2
= settings
->op
[stage
].carg2
& WINED3DTA_SELECTMASK
;
9071 if (arg0
== WINED3DTA_TEXTURE
|| arg1
== WINED3DTA_TEXTURE
|| arg2
== WINED3DTA_TEXTURE
9072 || (stage
== 0 && settings
->color_key_enabled
))
9073 tex_map
|= 1u << stage
;
9074 if (arg0
== WINED3DTA_TFACTOR
|| arg1
== WINED3DTA_TFACTOR
|| arg2
== WINED3DTA_TFACTOR
)
9075 tfactor_used
= TRUE
;
9076 if (arg0
== WINED3DTA_TEMP
|| arg1
== WINED3DTA_TEMP
|| arg2
== WINED3DTA_TEMP
)
9077 tempreg_used
= TRUE
;
9078 if (settings
->op
[stage
].dst
== tempreg
)
9079 tempreg_used
= TRUE
;
9080 if (arg0
== WINED3DTA_CONSTANT
|| arg1
== WINED3DTA_CONSTANT
|| arg2
== WINED3DTA_CONSTANT
)
9081 tss_const_map
|= 1u << stage
;
9083 switch (settings
->op
[stage
].cop
)
9085 case WINED3D_TOP_BUMPENVMAP_LUMINANCE
:
9086 lum_map
|= 1u << stage
;
9088 case WINED3D_TOP_BUMPENVMAP
:
9089 bump_map
|= 1u << stage
;
9091 case WINED3D_TOP_BLEND_TEXTURE_ALPHA
:
9092 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM
:
9093 tex_map
|= 1u << stage
;
9096 case WINED3D_TOP_BLEND_FACTOR_ALPHA
:
9097 tfactor_used
= TRUE
;
9104 if (settings
->op
[stage
].aop
== WINED3D_TOP_DISABLE
)
9107 arg0
= settings
->op
[stage
].aarg0
& WINED3DTA_SELECTMASK
;
9108 arg1
= settings
->op
[stage
].aarg1
& WINED3DTA_SELECTMASK
;
9109 arg2
= settings
->op
[stage
].aarg2
& WINED3DTA_SELECTMASK
;
9111 if (arg0
== WINED3DTA_TEXTURE
|| arg1
== WINED3DTA_TEXTURE
|| arg2
== WINED3DTA_TEXTURE
)
9112 tex_map
|= 1u << stage
;
9113 if (arg0
== WINED3DTA_TFACTOR
|| arg1
== WINED3DTA_TFACTOR
|| arg2
== WINED3DTA_TFACTOR
)
9114 tfactor_used
= TRUE
;
9115 if (arg0
== WINED3DTA_TEMP
|| arg1
== WINED3DTA_TEMP
|| arg2
== WINED3DTA_TEMP
)
9116 tempreg_used
= TRUE
;
9117 if (arg0
== WINED3DTA_CONSTANT
|| arg1
== WINED3DTA_CONSTANT
|| arg2
== WINED3DTA_CONSTANT
)
9118 tss_const_map
|= 1u << stage
;
9120 lowest_disabled_stage
= stage
;
9122 shader_glsl_add_version_declaration(buffer
, gl_info
);
9124 if (shader_glsl_use_explicit_attrib_location(gl_info
))
9125 shader_addline(buffer
, "#extension GL_ARB_explicit_attrib_location : enable\n");
9126 if (gl_info
->supported
[ARB_SHADING_LANGUAGE_420PACK
])
9127 shader_addline(buffer
, "#extension GL_ARB_shading_language_420pack : enable\n");
9128 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
9129 shader_addline(buffer
, "#extension GL_ARB_texture_rectangle : enable\n");
9131 if (!needs_legacy_glsl_syntax(gl_info
))
9133 if (shader_glsl_use_explicit_attrib_location(gl_info
))
9134 shader_addline(buffer
, "layout(location = 0) ");
9135 shader_addline(buffer
, "out vec4 ps_out[1];\n");
9138 shader_addline(buffer
, "vec4 tmp0, tmp1;\n");
9139 shader_addline(buffer
, "vec4 ret;\n");
9140 if (tempreg_used
|| settings
->sRGB_write
)
9141 shader_addline(buffer
, "vec4 temp_reg = vec4(0.0);\n");
9142 shader_addline(buffer
, "vec4 arg0, arg1, arg2;\n");
9144 for (stage
= 0; stage
< MAX_TEXTURES
; ++stage
)
9146 const char *sampler_type
;
9148 if (tss_const_map
& (1u << stage
))
9149 shader_addline(buffer
, "uniform vec4 tss_const%u;\n", stage
);
9151 if (!(tex_map
& (1u << stage
)))
9154 switch (settings
->op
[stage
].tex_type
)
9156 case WINED3D_GL_RES_TYPE_TEX_1D
:
9157 sampler_type
= "1D";
9159 case WINED3D_GL_RES_TYPE_TEX_2D
:
9160 sampler_type
= "2D";
9162 case WINED3D_GL_RES_TYPE_TEX_3D
:
9163 sampler_type
= "3D";
9165 case WINED3D_GL_RES_TYPE_TEX_CUBE
:
9166 sampler_type
= "Cube";
9168 case WINED3D_GL_RES_TYPE_TEX_RECT
:
9169 sampler_type
= "2DRect";
9172 FIXME("Unhandled sampler type %#x.\n", settings
->op
[stage
].tex_type
);
9173 sampler_type
= NULL
;
9178 if (shader_glsl_use_layout_binding_qualifier(gl_info
))
9179 shader_glsl_append_sampler_binding_qualifier(buffer
, context
, NULL
, stage
);
9180 shader_addline(buffer
, "uniform sampler%s ps_sampler%u;\n", sampler_type
, stage
);
9183 shader_addline(buffer
, "vec4 tex%u;\n", stage
);
9185 if (!(bump_map
& (1u << stage
)))
9187 shader_addline(buffer
, "uniform mat2 bumpenv_mat%u;\n", stage
);
9189 if (!(lum_map
& (1u << stage
)))
9191 shader_addline(buffer
, "uniform float bumpenv_lum_scale%u;\n", stage
);
9192 shader_addline(buffer
, "uniform float bumpenv_lum_offset%u;\n", stage
);
9195 shader_addline(buffer
, "uniform vec4 tex_factor;\n");
9196 if (settings
->color_key_enabled
)
9197 shader_addline(buffer
, "uniform vec4 color_key[2];\n");
9198 shader_addline(buffer
, "uniform vec4 specular_enable;\n");
9200 if (settings
->sRGB_write
)
9202 shader_addline(buffer
, "const vec4 srgb_const0 = ");
9203 shader_glsl_append_imm_vec4(buffer
, wined3d_srgb_const0
);
9204 shader_addline(buffer
, ";\n");
9205 shader_addline(buffer
, "const vec4 srgb_const1 = ");
9206 shader_glsl_append_imm_vec4(buffer
, wined3d_srgb_const1
);
9207 shader_addline(buffer
, ";\n");
9210 shader_addline(buffer
, "uniform struct\n{\n");
9211 shader_addline(buffer
, " vec4 color;\n");
9212 shader_addline(buffer
, " float density;\n");
9213 shader_addline(buffer
, " float end;\n");
9214 shader_addline(buffer
, " float scale;\n");
9215 shader_addline(buffer
, "} ffp_fog;\n");
9217 if (alpha_test_func
!= WINED3D_CMP_ALWAYS
)
9218 shader_addline(buffer
, "uniform float alpha_test_ref;\n");
9222 shader_addline(buffer
, "vec4 ffp_varying_diffuse;\n");
9223 shader_addline(buffer
, "vec4 ffp_varying_specular;\n");
9224 shader_addline(buffer
, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES
);
9225 shader_addline(buffer
, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES
);
9226 shader_addline(buffer
, "float ffp_varying_fogcoord;\n");
9230 declare_in_varying(gl_info
, buffer
, settings
->flatshading
, "vec4 ffp_varying_diffuse;\n");
9231 declare_in_varying(gl_info
, buffer
, settings
->flatshading
, "vec4 ffp_varying_specular;\n");
9232 declare_in_varying(gl_info
, buffer
, FALSE
, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES
);
9233 shader_addline(buffer
, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES
);
9234 declare_in_varying(gl_info
, buffer
, FALSE
, "float ffp_varying_fogcoord;\n");
9237 shader_addline(buffer
, "void main()\n{\n");
9241 shader_addline(buffer
, "ffp_varying_diffuse = gl_Color;\n");
9242 shader_addline(buffer
, "ffp_varying_specular = gl_SecondaryColor;\n");
9245 for (stage
= 0; stage
< MAX_TEXTURES
; ++stage
)
9247 if (tex_map
& (1u << stage
))
9249 if (settings
->pointsprite
)
9250 shader_addline(buffer
, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", stage
);
9251 else if (settings
->texcoords_initialized
& (1u << stage
))
9252 shader_addline(buffer
, "ffp_texcoord[%u] = %s[%u];\n",
9253 stage
, legacy_syntax
? "gl_TexCoord" : "ffp_varying_texcoord", stage
);
9255 shader_addline(buffer
, "ffp_texcoord[%u] = vec4(0.0);\n", stage
);
9259 if (legacy_syntax
&& settings
->fog
!= WINED3D_FFP_PS_FOG_OFF
)
9260 shader_addline(buffer
, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
9262 if (lowest_disabled_stage
< 7 && settings
->emul_clipplanes
)
9263 shader_addline(buffer
, "if (any(lessThan(ffp_texcoord[7], vec4(0.0)))) discard;\n");
9265 /* Generate texture sampling instructions */
9266 for (stage
= 0; stage
< MAX_TEXTURES
&& settings
->op
[stage
].cop
!= WINED3D_TOP_DISABLE
; ++stage
)
9268 const char *texture_function
, *coord_mask
;
9271 if (!(tex_map
& (1u << stage
)))
9274 if (settings
->op
[stage
].projected
== proj_none
)
9278 else if (settings
->op
[stage
].projected
== proj_count4
9279 || settings
->op
[stage
].projected
== proj_count3
)
9285 FIXME("Unexpected projection mode %d\n", settings
->op
[stage
].projected
);
9289 if (settings
->op
[stage
].tex_type
== WINED3D_GL_RES_TYPE_TEX_CUBE
)
9292 switch (settings
->op
[stage
].tex_type
)
9294 case WINED3D_GL_RES_TYPE_TEX_1D
:
9297 texture_function
= "texture1DProj";
9302 texture_function
= "texture1D";
9306 case WINED3D_GL_RES_TYPE_TEX_2D
:
9309 texture_function
= "texture2DProj";
9314 texture_function
= "texture2D";
9318 case WINED3D_GL_RES_TYPE_TEX_3D
:
9321 texture_function
= "texture3DProj";
9322 coord_mask
= "xyzw";
9326 texture_function
= "texture3D";
9330 case WINED3D_GL_RES_TYPE_TEX_CUBE
:
9331 texture_function
= "textureCube";
9334 case WINED3D_GL_RES_TYPE_TEX_RECT
:
9337 texture_function
= "texture2DRectProj";
9342 texture_function
= "texture2DRect";
9347 FIXME("Unhandled texture type %#x.\n", settings
->op
[stage
].tex_type
);
9348 texture_function
= "";
9349 coord_mask
= "xyzw";
9353 texture_function
= proj
? "textureProj" : "texture";
9356 && (settings
->op
[stage
- 1].cop
== WINED3D_TOP_BUMPENVMAP
9357 || settings
->op
[stage
- 1].cop
== WINED3D_TOP_BUMPENVMAP_LUMINANCE
))
9359 shader_addline(buffer
, "ret.xy = bumpenv_mat%u * tex%u.xy;\n", stage
- 1, stage
- 1);
9361 /* With projective textures, texbem only divides the static
9362 * texture coord, not the displacement, so multiply the
9363 * displacement with the dividing parameter before passing it to
9365 if (settings
->op
[stage
].projected
!= proj_none
)
9367 if (settings
->op
[stage
].projected
== proj_count4
)
9369 shader_addline(buffer
, "ret.xy = (ret.xy * ffp_texcoord[%u].w) + ffp_texcoord[%u].xy;\n",
9371 shader_addline(buffer
, "ret.zw = ffp_texcoord[%u].ww;\n", stage
);
9375 shader_addline(buffer
, "ret.xy = (ret.xy * ffp_texcoord[%u].z) + ffp_texcoord[%u].xy;\n",
9377 shader_addline(buffer
, "ret.zw = ffp_texcoord[%u].zz;\n", stage
);
9382 shader_addline(buffer
, "ret = ffp_texcoord[%u] + ret.xyxy;\n", stage
);
9385 shader_addline(buffer
, "tex%u = %s(ps_sampler%u, ret.%s);\n",
9386 stage
, texture_function
, stage
, coord_mask
);
9388 if (settings
->op
[stage
- 1].cop
== WINED3D_TOP_BUMPENVMAP_LUMINANCE
)
9389 shader_addline(buffer
, "tex%u *= clamp(tex%u.z * bumpenv_lum_scale%u + bumpenv_lum_offset%u, 0.0, 1.0);\n",
9390 stage
, stage
- 1, stage
- 1, stage
- 1);
9392 else if (settings
->op
[stage
].projected
== proj_count3
)
9394 shader_addline(buffer
, "tex%u = %s(ps_sampler%u, ffp_texcoord[%u].xyz);\n",
9395 stage
, texture_function
, stage
, stage
);
9399 shader_addline(buffer
, "tex%u = %s(ps_sampler%u, ffp_texcoord[%u].%s);\n",
9400 stage
, texture_function
, stage
, stage
, coord_mask
);
9403 string_buffer_sprintf(tex_reg_name
, "tex%u", stage
);
9404 shader_glsl_color_correction_ext(buffer
, tex_reg_name
->buffer
, WINED3DSP_WRITEMASK_ALL
,
9405 settings
->op
[stage
].color_fixup
);
9408 if (settings
->color_key_enabled
)
9410 shader_addline(buffer
, "if (all(greaterThanEqual(tex0, color_key[0])) && all(lessThan(tex0, color_key[1])))\n");
9411 shader_addline(buffer
, " discard;\n");
9414 shader_addline(buffer
, "ret = ffp_varying_diffuse;\n");
9416 /* Generate the main shader */
9417 for (stage
= 0; stage
< MAX_TEXTURES
; ++stage
)
9421 if (settings
->op
[stage
].cop
== WINED3D_TOP_DISABLE
)
9424 if (settings
->op
[stage
].cop
== WINED3D_TOP_SELECT_ARG1
9425 && settings
->op
[stage
].aop
== WINED3D_TOP_SELECT_ARG1
)
9426 op_equal
= settings
->op
[stage
].carg1
== settings
->op
[stage
].aarg1
;
9427 else if (settings
->op
[stage
].cop
== WINED3D_TOP_SELECT_ARG1
9428 && settings
->op
[stage
].aop
== WINED3D_TOP_SELECT_ARG2
)
9429 op_equal
= settings
->op
[stage
].carg1
== settings
->op
[stage
].aarg2
;
9430 else if (settings
->op
[stage
].cop
== WINED3D_TOP_SELECT_ARG2
9431 && settings
->op
[stage
].aop
== WINED3D_TOP_SELECT_ARG1
)
9432 op_equal
= settings
->op
[stage
].carg2
== settings
->op
[stage
].aarg1
;
9433 else if (settings
->op
[stage
].cop
== WINED3D_TOP_SELECT_ARG2
9434 && settings
->op
[stage
].aop
== WINED3D_TOP_SELECT_ARG2
)
9435 op_equal
= settings
->op
[stage
].carg2
== settings
->op
[stage
].aarg2
;
9437 op_equal
= settings
->op
[stage
].aop
== settings
->op
[stage
].cop
9438 && settings
->op
[stage
].carg0
== settings
->op
[stage
].aarg0
9439 && settings
->op
[stage
].carg1
== settings
->op
[stage
].aarg1
9440 && settings
->op
[stage
].carg2
== settings
->op
[stage
].aarg2
;
9442 if (settings
->op
[stage
].aop
== WINED3D_TOP_DISABLE
)
9444 shader_glsl_ffp_fragment_op(buffer
, stage
, TRUE
, FALSE
, settings
->op
[stage
].dst
,
9445 settings
->op
[stage
].cop
, settings
->op
[stage
].carg0
,
9446 settings
->op
[stage
].carg1
, settings
->op
[stage
].carg2
);
9450 shader_glsl_ffp_fragment_op(buffer
, stage
, TRUE
, TRUE
, settings
->op
[stage
].dst
,
9451 settings
->op
[stage
].cop
, settings
->op
[stage
].carg0
,
9452 settings
->op
[stage
].carg1
, settings
->op
[stage
].carg2
);
9454 else if (settings
->op
[stage
].cop
!= WINED3D_TOP_BUMPENVMAP
9455 && settings
->op
[stage
].cop
!= WINED3D_TOP_BUMPENVMAP_LUMINANCE
)
9457 shader_glsl_ffp_fragment_op(buffer
, stage
, TRUE
, FALSE
, settings
->op
[stage
].dst
,
9458 settings
->op
[stage
].cop
, settings
->op
[stage
].carg0
,
9459 settings
->op
[stage
].carg1
, settings
->op
[stage
].carg2
);
9460 shader_glsl_ffp_fragment_op(buffer
, stage
, FALSE
, TRUE
, settings
->op
[stage
].dst
,
9461 settings
->op
[stage
].aop
, settings
->op
[stage
].aarg0
,
9462 settings
->op
[stage
].aarg1
, settings
->op
[stage
].aarg2
);
9466 shader_addline(buffer
, "%s[0] = ffp_varying_specular * specular_enable + ret;\n",
9467 get_fragment_output(gl_info
));
9469 if (settings
->sRGB_write
)
9470 shader_glsl_generate_srgb_write_correction(buffer
, gl_info
);
9472 shader_glsl_generate_fog_code(buffer
, gl_info
, settings
->fog
);
9474 shader_glsl_generate_alpha_test(buffer
, gl_info
, alpha_test_func
);
9476 shader_addline(buffer
, "}\n");
9478 shader_id
= GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER
));
9479 shader_glsl_compile(gl_info
, shader_id
, buffer
->buffer
);
9481 string_buffer_release(&priv
->string_buffers
, tex_reg_name
);
9485 static struct glsl_ffp_vertex_shader
*shader_glsl_find_ffp_vertex_shader(struct shader_glsl_priv
*priv
,
9486 const struct wined3d_gl_info
*gl_info
, const struct wined3d_ffp_vs_settings
*settings
)
9488 struct glsl_ffp_vertex_shader
*shader
;
9489 const struct wine_rb_entry
*entry
;
9491 if ((entry
= wine_rb_get(&priv
->ffp_vertex_shaders
, settings
)))
9492 return WINE_RB_ENTRY_VALUE(entry
, struct glsl_ffp_vertex_shader
, desc
.entry
);
9494 if (!(shader
= HeapAlloc(GetProcessHeap(), 0, sizeof(*shader
))))
9497 shader
->desc
.settings
= *settings
;
9498 shader
->id
= shader_glsl_generate_ffp_vertex_shader(priv
, settings
, gl_info
);
9499 list_init(&shader
->linked_programs
);
9500 if (wine_rb_put(&priv
->ffp_vertex_shaders
, &shader
->desc
.settings
, &shader
->desc
.entry
) == -1)
9501 ERR("Failed to insert ffp vertex shader.\n");
9506 static struct glsl_ffp_fragment_shader
*shader_glsl_find_ffp_fragment_shader(struct shader_glsl_priv
*priv
,
9507 const struct ffp_frag_settings
*args
, const struct wined3d_context
*context
)
9509 struct glsl_ffp_fragment_shader
*glsl_desc
;
9510 const struct ffp_frag_desc
*desc
;
9512 if ((desc
= find_ffp_frag_shader(&priv
->ffp_fragment_shaders
, args
)))
9513 return CONTAINING_RECORD(desc
, struct glsl_ffp_fragment_shader
, entry
);
9515 if (!(glsl_desc
= HeapAlloc(GetProcessHeap(), 0, sizeof(*glsl_desc
))))
9518 glsl_desc
->entry
.settings
= *args
;
9519 glsl_desc
->id
= shader_glsl_generate_ffp_fragment_shader(priv
, args
, context
);
9520 list_init(&glsl_desc
->linked_programs
);
9521 add_ffp_frag_shader(&priv
->ffp_fragment_shaders
, &glsl_desc
->entry
);
9527 static void shader_glsl_init_vs_uniform_locations(const struct wined3d_gl_info
*gl_info
,
9528 struct shader_glsl_priv
*priv
, GLuint program_id
, struct glsl_vs_program
*vs
, unsigned int vs_c_count
)
9531 struct wined3d_string_buffer
*name
= string_buffer_get(&priv
->string_buffers
);
9533 for (i
= 0; i
< vs_c_count
; ++i
)
9535 string_buffer_sprintf(name
, "vs_c[%u]", i
);
9536 vs
->uniform_f_locations
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
9538 memset(&vs
->uniform_f_locations
[vs_c_count
], 0xff, (WINED3D_MAX_VS_CONSTS_F
- vs_c_count
) * sizeof(GLuint
));
9540 for (i
= 0; i
< WINED3D_MAX_CONSTS_I
; ++i
)
9542 string_buffer_sprintf(name
, "vs_i[%u]", i
);
9543 vs
->uniform_i_locations
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
9546 for (i
= 0; i
< WINED3D_MAX_CONSTS_B
; ++i
)
9548 string_buffer_sprintf(name
, "vs_b[%u]", i
);
9549 vs
->uniform_b_locations
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
9552 vs
->pos_fixup_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "pos_fixup"));
9554 for (i
= 0; i
< MAX_VERTEX_BLENDS
; ++i
)
9556 string_buffer_sprintf(name
, "ffp_modelview_matrix[%u]", i
);
9557 vs
->modelview_matrix_location
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
9559 vs
->projection_matrix_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_projection_matrix"));
9560 vs
->normal_matrix_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_normal_matrix"));
9561 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
9563 string_buffer_sprintf(name
, "ffp_texture_matrix[%u]", i
);
9564 vs
->texture_matrix_location
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
9566 vs
->material_ambient_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_material.ambient"));
9567 vs
->material_diffuse_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_material.diffuse"));
9568 vs
->material_specular_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_material.specular"));
9569 vs
->material_emissive_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_material.emissive"));
9570 vs
->material_shininess_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_material.shininess"));
9571 vs
->light_ambient_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_light_ambient"));
9572 for (i
= 0; i
< MAX_ACTIVE_LIGHTS
; ++i
)
9574 string_buffer_sprintf(name
, "ffp_light[%u].diffuse", i
);
9575 vs
->light_location
[i
].diffuse
= GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
9576 string_buffer_sprintf(name
, "ffp_light[%u].specular", i
);
9577 vs
->light_location
[i
].specular
= GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
9578 string_buffer_sprintf(name
, "ffp_light[%u].ambient", i
);
9579 vs
->light_location
[i
].ambient
= GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
9580 string_buffer_sprintf(name
, "ffp_light[%u].position", i
);
9581 vs
->light_location
[i
].position
= GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
9582 string_buffer_sprintf(name
, "ffp_light[%u].direction", i
);
9583 vs
->light_location
[i
].direction
= GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
9584 string_buffer_sprintf(name
, "ffp_light[%u].range", i
);
9585 vs
->light_location
[i
].range
= GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
9586 string_buffer_sprintf(name
, "ffp_light[%u].falloff", i
);
9587 vs
->light_location
[i
].falloff
= GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
9588 string_buffer_sprintf(name
, "ffp_light[%u].c_att", i
);
9589 vs
->light_location
[i
].c_att
= GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
9590 string_buffer_sprintf(name
, "ffp_light[%u].l_att", i
);
9591 vs
->light_location
[i
].l_att
= GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
9592 string_buffer_sprintf(name
, "ffp_light[%u].q_att", i
);
9593 vs
->light_location
[i
].q_att
= GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
9594 string_buffer_sprintf(name
, "ffp_light[%u].cos_htheta", i
);
9595 vs
->light_location
[i
].cos_htheta
= GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
9596 string_buffer_sprintf(name
, "ffp_light[%u].cos_hphi", i
);
9597 vs
->light_location
[i
].cos_hphi
= GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
9599 vs
->pointsize_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_point.size"));
9600 vs
->pointsize_min_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_point.size_min"));
9601 vs
->pointsize_max_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_point.size_max"));
9602 vs
->pointsize_c_att_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_point.c_att"));
9603 vs
->pointsize_l_att_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_point.l_att"));
9604 vs
->pointsize_q_att_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_point.q_att"));
9605 vs
->clip_planes_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "clip_planes"));
9607 string_buffer_release(&priv
->string_buffers
, name
);
9610 static void shader_glsl_init_ds_uniform_locations(const struct wined3d_gl_info
*gl_info
,
9611 struct shader_glsl_priv
*priv
, GLuint program_id
, struct glsl_ds_program
*ds
)
9613 ds
->pos_fixup_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "pos_fixup"));
9616 static void shader_glsl_init_gs_uniform_locations(const struct wined3d_gl_info
*gl_info
,
9617 struct shader_glsl_priv
*priv
, GLuint program_id
, struct glsl_gs_program
*gs
)
9619 gs
->pos_fixup_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "pos_fixup"));
9622 static void shader_glsl_init_ps_uniform_locations(const struct wined3d_gl_info
*gl_info
,
9623 struct shader_glsl_priv
*priv
, GLuint program_id
, struct glsl_ps_program
*ps
, unsigned int ps_c_count
)
9626 struct wined3d_string_buffer
*name
= string_buffer_get(&priv
->string_buffers
);
9628 for (i
= 0; i
< ps_c_count
; ++i
)
9630 string_buffer_sprintf(name
, "ps_c[%u]", i
);
9631 ps
->uniform_f_locations
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
9633 memset(&ps
->uniform_f_locations
[ps_c_count
], 0xff, (WINED3D_MAX_PS_CONSTS_F
- ps_c_count
) * sizeof(GLuint
));
9635 for (i
= 0; i
< WINED3D_MAX_CONSTS_I
; ++i
)
9637 string_buffer_sprintf(name
, "ps_i[%u]", i
);
9638 ps
->uniform_i_locations
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
9641 for (i
= 0; i
< WINED3D_MAX_CONSTS_B
; ++i
)
9643 string_buffer_sprintf(name
, "ps_b[%u]", i
);
9644 ps
->uniform_b_locations
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
9647 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
9649 string_buffer_sprintf(name
, "bumpenv_mat%u", i
);
9650 ps
->bumpenv_mat_location
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
9651 string_buffer_sprintf(name
, "bumpenv_lum_scale%u", i
);
9652 ps
->bumpenv_lum_scale_location
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
9653 string_buffer_sprintf(name
, "bumpenv_lum_offset%u", i
);
9654 ps
->bumpenv_lum_offset_location
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
9655 string_buffer_sprintf(name
, "tss_const%u", i
);
9656 ps
->tss_constant_location
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
9659 ps
->tex_factor_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "tex_factor"));
9660 ps
->specular_enable_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "specular_enable"));
9662 ps
->fog_color_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_fog.color"));
9663 ps
->fog_density_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_fog.density"));
9664 ps
->fog_end_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_fog.end"));
9665 ps
->fog_scale_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_fog.scale"));
9667 ps
->alpha_test_ref_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "alpha_test_ref"));
9669 ps
->np2_fixup_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ps_samplerNP2Fixup"));
9670 ps
->ycorrection_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ycorrection"));
9671 ps
->color_key_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "color_key"));
9673 string_buffer_release(&priv
->string_buffers
, name
);
9676 static HRESULT
shader_glsl_compile_compute_shader(struct shader_glsl_priv
*priv
,
9677 const struct wined3d_context
*context
, struct wined3d_shader
*shader
)
9679 struct glsl_context_data
*ctx_data
= context
->shader_backend_data
;
9680 struct wined3d_string_buffer
*buffer
= &priv
->shader_buffer
;
9681 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
9682 struct glsl_cs_compiled_shader
*gl_shaders
;
9683 struct glsl_shader_private
*shader_data
;
9684 struct glsl_shader_prog_link
*entry
;
9685 GLuint shader_id
, program_id
;
9687 if (!(entry
= HeapAlloc(GetProcessHeap(), 0, sizeof(*entry
))))
9689 ERR("Out of memory.\n");
9690 return E_OUTOFMEMORY
;
9693 if (!(shader
->backend_data
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*shader_data
))))
9695 ERR("Failed to allocate backend data.\n");
9696 HeapFree(GetProcessHeap(), 0, entry
);
9697 return E_OUTOFMEMORY
;
9699 shader_data
= shader
->backend_data
;
9700 gl_shaders
= shader_data
->gl_shaders
.cs
;
9702 if (!(shader_data
->gl_shaders
.cs
= HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders
))))
9704 ERR("Failed to allocate GL shader array.\n");
9705 HeapFree(GetProcessHeap(), 0, entry
);
9706 HeapFree(GetProcessHeap(), 0, shader
->backend_data
);
9707 shader
->backend_data
= NULL
;
9708 return E_OUTOFMEMORY
;
9710 shader_data
->shader_array_size
= 1;
9711 gl_shaders
= shader_data
->gl_shaders
.cs
;
9713 TRACE("Compiling compute shader %p.\n", shader
);
9715 string_buffer_clear(buffer
);
9716 shader_id
= shader_glsl_generate_compute_shader(context
, buffer
, &priv
->string_buffers
, shader
);
9717 gl_shaders
[shader_data
->num_gl_shaders
++].id
= shader_id
;
9719 program_id
= GL_EXTCALL(glCreateProgram());
9720 TRACE("Created new GLSL shader program %u.\n", program_id
);
9722 entry
->id
= program_id
;
9728 entry
->cs
.id
= shader_id
;
9729 entry
->constant_version
= 0;
9730 entry
->ps
.np2_fixup_info
= NULL
;
9731 add_glsl_program_entry(priv
, entry
);
9733 TRACE("Attaching GLSL shader object %u to program %u.\n", shader_id
, program_id
);
9734 GL_EXTCALL(glAttachShader(program_id
, shader_id
));
9735 checkGLcall("glAttachShader");
9737 list_add_head(&shader
->linked_programs
, &entry
->cs
.shader_entry
);
9739 TRACE("Linking GLSL shader program %u.\n", program_id
);
9740 GL_EXTCALL(glLinkProgram(program_id
));
9741 shader_glsl_validate_link(gl_info
, program_id
);
9743 GL_EXTCALL(glUseProgram(program_id
));
9744 checkGLcall("glUseProgram");
9745 shader_glsl_load_program_resources(context
, priv
, program_id
, shader
);
9746 shader_glsl_load_images(gl_info
, priv
, program_id
, &shader
->reg_maps
);
9748 entry
->constant_update_mask
= 0;
9750 GL_EXTCALL(glUseProgram(ctx_data
->glsl_program
? ctx_data
->glsl_program
->id
: 0));
9751 checkGLcall("glUseProgram");
9755 static GLuint
find_glsl_compute_shader(const struct wined3d_context
*context
,
9756 struct shader_glsl_priv
*priv
, struct wined3d_shader
*shader
)
9758 struct glsl_shader_private
*shader_data
;
9760 if (!shader
->backend_data
)
9762 WARN("Failed to find GLSL program for compute shader %p.\n", shader
);
9763 if (FAILED(shader_glsl_compile_compute_shader(priv
, context
, shader
)))
9765 ERR("Failed to compile compute shader %p.\n", shader
);
9769 shader_data
= shader
->backend_data
;
9770 return shader_data
->gl_shaders
.cs
[0].id
;
9773 /* Context activation is done by the caller. */
9774 static void set_glsl_compute_shader_program(const struct wined3d_context
*context
,
9775 const struct wined3d_state
*state
, struct shader_glsl_priv
*priv
, struct glsl_context_data
*ctx_data
)
9777 struct glsl_shader_prog_link
*entry
;
9778 struct wined3d_shader
*shader
;
9779 struct glsl_program_key key
;
9782 if (!(context
->shader_update_mask
& (1u << WINED3D_SHADER_TYPE_COMPUTE
)))
9785 if (!(shader
= state
->shader
[WINED3D_SHADER_TYPE_COMPUTE
]))
9787 WARN("Compute shader is NULL.\n");
9788 ctx_data
->glsl_program
= NULL
;
9792 cs_id
= find_glsl_compute_shader(context
, priv
, shader
);
9793 memset(&key
, 0, sizeof(key
));
9795 if (!(entry
= get_glsl_program_entry(priv
, &key
)))
9796 ERR("Failed to find GLSL program for compute shader %p.\n", shader
);
9797 ctx_data
->glsl_program
= entry
;
9800 /* Context activation is done by the caller. */
9801 static void set_glsl_shader_program(const struct wined3d_context
*context
, const struct wined3d_state
*state
,
9802 struct shader_glsl_priv
*priv
, struct glsl_context_data
*ctx_data
)
9804 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
9805 const struct wined3d_d3d_info
*d3d_info
= context
->d3d_info
;
9806 const struct ps_np2fixup_info
*np2fixup_info
= NULL
;
9807 struct glsl_shader_prog_link
*entry
= NULL
;
9808 struct wined3d_shader
*hshader
, *dshader
, *gshader
;
9809 struct wined3d_shader
*vshader
= NULL
;
9810 struct wined3d_shader
*pshader
= NULL
;
9811 GLuint reorder_shader_id
= 0;
9812 struct glsl_program_key key
;
9820 struct list
*ps_list
, *vs_list
;
9822 struct wined3d_string_buffer
*tmp_name
;
9824 if (!(context
->shader_update_mask
& (1u << WINED3D_SHADER_TYPE_VERTEX
)) && ctx_data
->glsl_program
)
9826 vs_id
= ctx_data
->glsl_program
->vs
.id
;
9827 vs_list
= &ctx_data
->glsl_program
->vs
.shader_entry
;
9830 vshader
= state
->shader
[WINED3D_SHADER_TYPE_VERTEX
];
9832 else if (use_vs(state
))
9834 struct vs_compile_args vs_compile_args
;
9836 vshader
= state
->shader
[WINED3D_SHADER_TYPE_VERTEX
];
9838 find_vs_compile_args(state
, vshader
, context
->stream_info
.swizzle_map
, &vs_compile_args
, context
);
9839 vs_id
= find_glsl_vshader(context
, priv
, vshader
, &vs_compile_args
);
9840 vs_list
= &vshader
->linked_programs
;
9842 else if (priv
->vertex_pipe
== &glsl_vertex_pipe
)
9844 struct glsl_ffp_vertex_shader
*ffp_shader
;
9845 struct wined3d_ffp_vs_settings settings
;
9847 wined3d_ffp_get_vs_settings(context
, state
, &settings
);
9848 ffp_shader
= shader_glsl_find_ffp_vertex_shader(priv
, gl_info
, &settings
);
9849 vs_id
= ffp_shader
->id
;
9850 vs_list
= &ffp_shader
->linked_programs
;
9853 hshader
= state
->shader
[WINED3D_SHADER_TYPE_HULL
];
9854 if (!(context
->shader_update_mask
& (1u << WINED3D_SHADER_TYPE_HULL
)) && ctx_data
->glsl_program
)
9855 hs_id
= ctx_data
->glsl_program
->hs
.id
;
9857 hs_id
= find_glsl_hull_shader(context
, priv
, hshader
);
9859 dshader
= state
->shader
[WINED3D_SHADER_TYPE_DOMAIN
];
9860 if (!(context
->shader_update_mask
& (1u << WINED3D_SHADER_TYPE_DOMAIN
)) && ctx_data
->glsl_program
)
9862 ds_id
= ctx_data
->glsl_program
->ds
.id
;
9866 struct ds_compile_args args
;
9868 find_ds_compile_args(state
, dshader
, &args
, context
);
9869 ds_id
= find_glsl_domain_shader(context
, priv
, dshader
, &args
);
9872 gshader
= state
->shader
[WINED3D_SHADER_TYPE_GEOMETRY
];
9873 if (!(context
->shader_update_mask
& (1u << WINED3D_SHADER_TYPE_GEOMETRY
)) && ctx_data
->glsl_program
)
9875 gs_id
= ctx_data
->glsl_program
->gs
.id
;
9879 struct gs_compile_args args
;
9881 find_gs_compile_args(state
, gshader
, &args
, context
);
9882 gs_id
= find_glsl_geometry_shader(context
, priv
, gshader
, &args
);
9885 if (!(context
->shader_update_mask
& (1u << WINED3D_SHADER_TYPE_PIXEL
)) && ctx_data
->glsl_program
)
9887 ps_id
= ctx_data
->glsl_program
->ps
.id
;
9888 ps_list
= &ctx_data
->glsl_program
->ps
.shader_entry
;
9891 pshader
= state
->shader
[WINED3D_SHADER_TYPE_PIXEL
];
9893 else if (use_ps(state
))
9895 struct ps_compile_args ps_compile_args
;
9896 pshader
= state
->shader
[WINED3D_SHADER_TYPE_PIXEL
];
9897 find_ps_compile_args(state
, pshader
, context
->stream_info
.position_transformed
, &ps_compile_args
, context
);
9898 ps_id
= find_glsl_pshader(context
, &priv
->shader_buffer
, &priv
->string_buffers
,
9899 pshader
, &ps_compile_args
, &np2fixup_info
);
9900 ps_list
= &pshader
->linked_programs
;
9902 else if (priv
->fragment_pipe
== &glsl_fragment_pipe
9903 && !(vshader
&& vshader
->reg_maps
.shader_version
.major
>= 4))
9905 struct glsl_ffp_fragment_shader
*ffp_shader
;
9906 struct ffp_frag_settings settings
;
9908 gen_ffp_frag_op(context
, state
, &settings
, FALSE
);
9909 ffp_shader
= shader_glsl_find_ffp_fragment_shader(priv
, &settings
, context
);
9910 ps_id
= ffp_shader
->id
;
9911 ps_list
= &ffp_shader
->linked_programs
;
9920 if ((!vs_id
&& !hs_id
&& !ds_id
&& !gs_id
&& !ps_id
) || (entry
= get_glsl_program_entry(priv
, &key
)))
9922 ctx_data
->glsl_program
= entry
;
9926 /* If we get to this point, then no matching program exists, so we create one */
9927 program_id
= GL_EXTCALL(glCreateProgram());
9928 TRACE("Created new GLSL shader program %u.\n", program_id
);
9930 /* Create the entry */
9931 entry
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link
));
9932 entry
->id
= program_id
;
9933 entry
->vs
.id
= vs_id
;
9934 entry
->hs
.id
= hs_id
;
9935 entry
->ds
.id
= ds_id
;
9936 entry
->gs
.id
= gs_id
;
9937 entry
->ps
.id
= ps_id
;
9939 entry
->constant_version
= 0;
9940 entry
->ps
.np2_fixup_info
= np2fixup_info
;
9941 /* Add the hash table entry */
9942 add_glsl_program_entry(priv
, entry
);
9944 /* Set the current program */
9945 ctx_data
->glsl_program
= entry
;
9947 /* Attach GLSL vshader */
9950 TRACE("Attaching GLSL shader object %u to program %u.\n", vs_id
, program_id
);
9951 GL_EXTCALL(glAttachShader(program_id
, vs_id
));
9952 checkGLcall("glAttachShader");
9954 list_add_head(vs_list
, &entry
->vs
.shader_entry
);
9959 attribs_map
= vshader
->reg_maps
.input_registers
;
9960 if (vshader
->reg_maps
.shader_version
.major
< 4)
9962 reorder_shader_id
= shader_glsl_generate_vs3_rasterizer_input_setup(priv
, vshader
, pshader
,
9963 state
->gl_primitive_type
== GL_POINTS
&& vshader
->reg_maps
.point_size
,
9964 d3d_info
->emulated_flatshading
9965 && state
->render_states
[WINED3D_RS_SHADEMODE
] == WINED3D_SHADE_FLAT
, gl_info
);
9966 TRACE("Attaching GLSL shader object %u to program %u.\n", reorder_shader_id
, program_id
);
9967 GL_EXTCALL(glAttachShader(program_id
, reorder_shader_id
));
9968 checkGLcall("glAttachShader");
9969 /* Flag the reorder function for deletion, it will be freed
9970 * automatically when the program is destroyed. */
9971 GL_EXTCALL(glDeleteShader(reorder_shader_id
));
9976 attribs_map
= (1u << WINED3D_FFP_ATTRIBS_COUNT
) - 1;
9979 if (!shader_glsl_use_explicit_attrib_location(gl_info
))
9981 /* Bind vertex attributes to a corresponding index number to match
9982 * the same index numbers as ARB_vertex_programs (makes loading
9983 * vertex attributes simpler). With this method, we can use the
9984 * exact same code to load the attributes later for both ARB and
9987 * We have to do this here because we need to know the Program ID
9988 * in order to make the bindings work, and it has to be done prior
9989 * to linking the GLSL program. */
9990 tmp_name
= string_buffer_get(&priv
->string_buffers
);
9991 for (i
= 0; attribs_map
; attribs_map
>>= 1, ++i
)
9993 if (!(attribs_map
& 1))
9996 string_buffer_sprintf(tmp_name
, "vs_in%u", i
);
9997 GL_EXTCALL(glBindAttribLocation(program_id
, i
, tmp_name
->buffer
));
9998 if (vshader
&& vshader
->reg_maps
.shader_version
.major
>= 4)
10000 string_buffer_sprintf(tmp_name
, "vs_in_uint%u", i
);
10001 GL_EXTCALL(glBindAttribLocation(program_id
, i
, tmp_name
->buffer
));
10002 string_buffer_sprintf(tmp_name
, "vs_in_int%u", i
);
10003 GL_EXTCALL(glBindAttribLocation(program_id
, i
, tmp_name
->buffer
));
10006 checkGLcall("glBindAttribLocation");
10007 string_buffer_release(&priv
->string_buffers
, tmp_name
);
10009 if (!needs_legacy_glsl_syntax(gl_info
))
10011 GL_EXTCALL(glBindFragDataLocation(program_id
, 0, "ps_out"));
10012 checkGLcall("glBindFragDataLocation");
10018 TRACE("Attaching GLSL tessellation control shader object %u to program %u.\n", hs_id
, program_id
);
10019 GL_EXTCALL(glAttachShader(program_id
, hs_id
));
10020 checkGLcall("glAttachShader");
10022 list_add_head(&hshader
->linked_programs
, &entry
->hs
.shader_entry
);
10027 TRACE("Attaching GLSL tessellation evaluation shader object %u to program %u.\n", ds_id
, program_id
);
10028 GL_EXTCALL(glAttachShader(program_id
, ds_id
));
10029 checkGLcall("glAttachShader");
10031 list_add_head(&dshader
->linked_programs
, &entry
->ds
.shader_entry
);
10036 TRACE("Attaching GLSL geometry shader object %u to program %u.\n", gs_id
, program_id
);
10037 GL_EXTCALL(glAttachShader(program_id
, gs_id
));
10038 checkGLcall("glAttachShader");
10040 shader_glsl_init_transform_feedback(context
, priv
, program_id
, gshader
);
10042 list_add_head(&gshader
->linked_programs
, &entry
->gs
.shader_entry
);
10045 /* Attach GLSL pshader */
10048 TRACE("Attaching GLSL shader object %u to program %u.\n", ps_id
, program_id
);
10049 GL_EXTCALL(glAttachShader(program_id
, ps_id
));
10050 checkGLcall("glAttachShader");
10052 list_add_head(ps_list
, &entry
->ps
.shader_entry
);
10055 /* Link the program */
10056 TRACE("Linking GLSL shader program %u.\n", program_id
);
10057 GL_EXTCALL(glLinkProgram(program_id
));
10058 shader_glsl_validate_link(gl_info
, program_id
);
10060 shader_glsl_init_vs_uniform_locations(gl_info
, priv
, program_id
, &entry
->vs
,
10061 vshader
? vshader
->limits
->constant_float
: 0);
10062 shader_glsl_init_ds_uniform_locations(gl_info
, priv
, program_id
, &entry
->ds
);
10063 shader_glsl_init_gs_uniform_locations(gl_info
, priv
, program_id
, &entry
->gs
);
10064 shader_glsl_init_ps_uniform_locations(gl_info
, priv
, program_id
, &entry
->ps
,
10065 pshader
? pshader
->limits
->constant_float
: 0);
10066 checkGLcall("Find glsl program uniform locations");
10068 if (needs_legacy_glsl_syntax(gl_info
))
10070 if (pshader
&& pshader
->reg_maps
.shader_version
.major
>= 3
10071 && pshader
->u
.ps
.declared_in_count
> vec4_varyings(3, gl_info
))
10073 TRACE("Shader %d needs vertex color clamping disabled.\n", program_id
);
10074 entry
->vs
.vertex_color_clamp
= GL_FALSE
;
10078 entry
->vs
.vertex_color_clamp
= GL_FIXED_ONLY_ARB
;
10083 /* With core profile we never change vertex_color_clamp from
10084 * GL_FIXED_ONLY_MODE (which is also the initial value) so we never call
10085 * glClampColorARB(). */
10086 entry
->vs
.vertex_color_clamp
= GL_FIXED_ONLY_ARB
;
10089 /* Set the shader to allow uniform loading on it */
10090 GL_EXTCALL(glUseProgram(program_id
));
10091 checkGLcall("glUseProgram");
10093 entry
->constant_update_mask
= 0;
10096 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_VS_F
;
10097 if (vshader
->reg_maps
.integer_constants
)
10098 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_VS_I
;
10099 if (vshader
->reg_maps
.boolean_constants
)
10100 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_VS_B
;
10101 if (entry
->vs
.pos_fixup_location
!= -1)
10102 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_POS_FIXUP
;
10104 shader_glsl_load_program_resources(context
, priv
, program_id
, vshader
);
10108 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_MODELVIEW
10109 | WINED3D_SHADER_CONST_FFP_PROJ
;
10111 for (i
= 1; i
< MAX_VERTEX_BLENDS
; ++i
)
10113 if (entry
->vs
.modelview_matrix_location
[i
] != -1)
10115 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_VERTEXBLEND
;
10120 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
10122 if (entry
->vs
.texture_matrix_location
[i
] != -1)
10124 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_TEXMATRIX
;
10128 if (entry
->vs
.material_ambient_location
!= -1 || entry
->vs
.material_diffuse_location
!= -1
10129 || entry
->vs
.material_specular_location
!= -1
10130 || entry
->vs
.material_emissive_location
!= -1
10131 || entry
->vs
.material_shininess_location
!= -1)
10132 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_MATERIAL
;
10133 if (entry
->vs
.light_ambient_location
!= -1)
10134 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_LIGHTS
;
10136 if (entry
->vs
.clip_planes_location
!= -1)
10137 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_VS_CLIP_PLANES
;
10138 if (entry
->vs
.pointsize_min_location
!= -1)
10139 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_VS_POINTSIZE
;
10142 shader_glsl_load_program_resources(context
, priv
, program_id
, hshader
);
10146 if (entry
->ds
.pos_fixup_location
!= -1)
10147 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_POS_FIXUP
;
10149 shader_glsl_load_program_resources(context
, priv
, program_id
, dshader
);
10154 if (entry
->gs
.pos_fixup_location
!= -1)
10155 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_POS_FIXUP
;
10157 shader_glsl_load_program_resources(context
, priv
, program_id
, gshader
);
10164 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_PS_F
;
10165 if (pshader
->reg_maps
.integer_constants
)
10166 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_PS_I
;
10167 if (pshader
->reg_maps
.boolean_constants
)
10168 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_PS_B
;
10169 if (entry
->ps
.ycorrection_location
!= -1)
10170 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_PS_Y_CORR
;
10172 shader_glsl_load_program_resources(context
, priv
, program_id
, pshader
);
10173 shader_glsl_load_images(gl_info
, priv
, program_id
, &pshader
->reg_maps
);
10177 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_PS
;
10179 shader_glsl_load_samplers(context
, priv
, program_id
, NULL
);
10182 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
10184 if (entry
->ps
.bumpenv_mat_location
[i
] != -1)
10186 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_PS_BUMP_ENV
;
10191 if (entry
->ps
.fog_color_location
!= -1)
10192 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_PS_FOG
;
10193 if (entry
->ps
.alpha_test_ref_location
!= -1)
10194 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_PS_ALPHA_TEST
;
10195 if (entry
->ps
.np2_fixup_location
!= -1)
10196 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_PS_NP2_FIXUP
;
10197 if (entry
->ps
.color_key_location
!= -1)
10198 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_COLOR_KEY
;
10202 static void shader_glsl_precompile(void *shader_priv
, struct wined3d_shader
*shader
)
10204 struct wined3d_device
*device
= shader
->device
;
10205 struct wined3d_context
*context
;
10207 if (shader
->reg_maps
.shader_version
.type
== WINED3D_SHADER_TYPE_COMPUTE
)
10209 context
= context_acquire(device
, NULL
, 0);
10210 shader_glsl_compile_compute_shader(shader_priv
, context
, shader
);
10211 context_release(context
);
10215 /* Context activation is done by the caller. */
10216 static void shader_glsl_select(void *shader_priv
, struct wined3d_context
*context
,
10217 const struct wined3d_state
*state
)
10219 struct glsl_context_data
*ctx_data
= context
->shader_backend_data
;
10220 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
10221 struct shader_glsl_priv
*priv
= shader_priv
;
10222 GLenum current_vertex_color_clamp
;
10223 GLuint program_id
, prev_id
;
10225 priv
->vertex_pipe
->vp_enable(gl_info
, !use_vs(state
));
10226 priv
->fragment_pipe
->enable_extension(gl_info
, !use_ps(state
));
10228 prev_id
= ctx_data
->glsl_program
? ctx_data
->glsl_program
->id
: 0;
10230 set_glsl_shader_program(context
, state
, priv
, ctx_data
);
10232 if (ctx_data
->glsl_program
)
10234 program_id
= ctx_data
->glsl_program
->id
;
10235 current_vertex_color_clamp
= ctx_data
->glsl_program
->vs
.vertex_color_clamp
;
10240 current_vertex_color_clamp
= GL_FIXED_ONLY_ARB
;
10243 if (ctx_data
->vertex_color_clamp
!= current_vertex_color_clamp
)
10245 ctx_data
->vertex_color_clamp
= current_vertex_color_clamp
;
10246 if (gl_info
->supported
[ARB_COLOR_BUFFER_FLOAT
])
10248 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB
, current_vertex_color_clamp
));
10249 checkGLcall("glClampColorARB");
10253 FIXME("Vertex color clamp needs to be changed, but extension not supported.\n");
10257 TRACE("Using GLSL program %u.\n", program_id
);
10259 if (prev_id
!= program_id
)
10261 GL_EXTCALL(glUseProgram(program_id
));
10262 checkGLcall("glUseProgram");
10265 context
->constant_update_mask
|= ctx_data
->glsl_program
->constant_update_mask
;
10268 context
->shader_update_mask
|= (1u << WINED3D_SHADER_TYPE_COMPUTE
);
10271 /* Context activation is done by the caller. */
10272 static void shader_glsl_select_compute(void *shader_priv
, struct wined3d_context
*context
,
10273 const struct wined3d_state
*state
)
10275 struct glsl_context_data
*ctx_data
= context
->shader_backend_data
;
10276 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
10277 struct shader_glsl_priv
*priv
= shader_priv
;
10278 GLuint program_id
, prev_id
;
10280 prev_id
= ctx_data
->glsl_program
? ctx_data
->glsl_program
->id
: 0;
10281 set_glsl_compute_shader_program(context
, state
, priv
, ctx_data
);
10282 program_id
= ctx_data
->glsl_program
? ctx_data
->glsl_program
->id
: 0;
10284 TRACE("Using GLSL program %u.\n", program_id
);
10286 if (prev_id
!= program_id
)
10288 GL_EXTCALL(glUseProgram(program_id
));
10289 checkGLcall("glUseProgram");
10292 context
->shader_update_mask
|= (1u << WINED3D_SHADER_TYPE_PIXEL
)
10293 | (1u << WINED3D_SHADER_TYPE_VERTEX
)
10294 | (1u << WINED3D_SHADER_TYPE_GEOMETRY
)
10295 | (1u << WINED3D_SHADER_TYPE_HULL
)
10296 | (1u << WINED3D_SHADER_TYPE_DOMAIN
);
10299 /* "context" is not necessarily the currently active context. */
10300 static void shader_glsl_invalidate_current_program(struct wined3d_context
*context
)
10302 struct glsl_context_data
*ctx_data
= context
->shader_backend_data
;
10304 ctx_data
->glsl_program
= NULL
;
10305 context
->shader_update_mask
= (1u << WINED3D_SHADER_TYPE_PIXEL
)
10306 | (1u << WINED3D_SHADER_TYPE_VERTEX
)
10307 | (1u << WINED3D_SHADER_TYPE_GEOMETRY
)
10308 | (1u << WINED3D_SHADER_TYPE_HULL
)
10309 | (1u << WINED3D_SHADER_TYPE_DOMAIN
)
10310 | (1u << WINED3D_SHADER_TYPE_COMPUTE
);
10313 /* Context activation is done by the caller. */
10314 static void shader_glsl_disable(void *shader_priv
, struct wined3d_context
*context
)
10316 struct glsl_context_data
*ctx_data
= context
->shader_backend_data
;
10317 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
10318 struct shader_glsl_priv
*priv
= shader_priv
;
10320 shader_glsl_invalidate_current_program(context
);
10321 GL_EXTCALL(glUseProgram(0));
10322 checkGLcall("glUseProgram");
10324 priv
->vertex_pipe
->vp_enable(gl_info
, FALSE
);
10325 priv
->fragment_pipe
->enable_extension(gl_info
, FALSE
);
10327 if (needs_legacy_glsl_syntax(gl_info
) && gl_info
->supported
[ARB_COLOR_BUFFER_FLOAT
])
10329 ctx_data
->vertex_color_clamp
= GL_FIXED_ONLY_ARB
;
10330 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB
, GL_FIXED_ONLY_ARB
));
10331 checkGLcall("glClampColorARB");
10335 static void shader_glsl_invalidate_contexts_program(struct wined3d_device
*device
,
10336 const struct glsl_shader_prog_link
*program
)
10338 const struct glsl_context_data
*ctx_data
;
10339 struct wined3d_context
*context
;
10342 for (i
= 0; i
< device
->context_count
; ++i
)
10344 context
= device
->contexts
[i
];
10345 ctx_data
= context
->shader_backend_data
;
10347 if (ctx_data
->glsl_program
== program
)
10348 shader_glsl_invalidate_current_program(context
);
10352 static void shader_glsl_destroy(struct wined3d_shader
*shader
)
10354 struct glsl_shader_private
*shader_data
= shader
->backend_data
;
10355 struct wined3d_device
*device
= shader
->device
;
10356 struct shader_glsl_priv
*priv
= device
->shader_priv
;
10357 const struct wined3d_gl_info
*gl_info
;
10358 const struct list
*linked_programs
;
10359 struct wined3d_context
*context
;
10361 if (!shader_data
|| !shader_data
->num_gl_shaders
)
10363 HeapFree(GetProcessHeap(), 0, shader_data
);
10364 shader
->backend_data
= NULL
;
10368 context
= context_acquire(device
, NULL
, 0);
10369 gl_info
= context
->gl_info
;
10371 TRACE("Deleting linked programs.\n");
10372 linked_programs
= &shader
->linked_programs
;
10373 if (linked_programs
->next
)
10375 struct glsl_shader_prog_link
*entry
, *entry2
;
10378 switch (shader
->reg_maps
.shader_version
.type
)
10380 case WINED3D_SHADER_TYPE_PIXEL
:
10382 struct glsl_ps_compiled_shader
*gl_shaders
= shader_data
->gl_shaders
.ps
;
10384 for (i
= 0; i
< shader_data
->num_gl_shaders
; ++i
)
10386 TRACE("Deleting pixel shader %u.\n", gl_shaders
[i
].id
);
10387 GL_EXTCALL(glDeleteShader(gl_shaders
[i
].id
));
10388 checkGLcall("glDeleteShader");
10390 HeapFree(GetProcessHeap(), 0, shader_data
->gl_shaders
.ps
);
10392 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, linked_programs
,
10393 struct glsl_shader_prog_link
, ps
.shader_entry
)
10395 shader_glsl_invalidate_contexts_program(device
, entry
);
10396 delete_glsl_program_entry(priv
, gl_info
, entry
);
10402 case WINED3D_SHADER_TYPE_VERTEX
:
10404 struct glsl_vs_compiled_shader
*gl_shaders
= shader_data
->gl_shaders
.vs
;
10406 for (i
= 0; i
< shader_data
->num_gl_shaders
; ++i
)
10408 TRACE("Deleting vertex shader %u.\n", gl_shaders
[i
].id
);
10409 GL_EXTCALL(glDeleteShader(gl_shaders
[i
].id
));
10410 checkGLcall("glDeleteShader");
10412 HeapFree(GetProcessHeap(), 0, shader_data
->gl_shaders
.vs
);
10414 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, linked_programs
,
10415 struct glsl_shader_prog_link
, vs
.shader_entry
)
10417 shader_glsl_invalidate_contexts_program(device
, entry
);
10418 delete_glsl_program_entry(priv
, gl_info
, entry
);
10424 case WINED3D_SHADER_TYPE_HULL
:
10426 struct glsl_hs_compiled_shader
*gl_shaders
= shader_data
->gl_shaders
.hs
;
10428 for (i
= 0; i
< shader_data
->num_gl_shaders
; ++i
)
10430 TRACE("Deleting hull shader %u.\n", gl_shaders
[i
].id
);
10431 GL_EXTCALL(glDeleteShader(gl_shaders
[i
].id
));
10432 checkGLcall("glDeleteShader");
10434 HeapFree(GetProcessHeap(), 0, shader_data
->gl_shaders
.hs
);
10436 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, linked_programs
,
10437 struct glsl_shader_prog_link
, hs
.shader_entry
)
10439 shader_glsl_invalidate_contexts_program(device
, entry
);
10440 delete_glsl_program_entry(priv
, gl_info
, entry
);
10446 case WINED3D_SHADER_TYPE_DOMAIN
:
10448 struct glsl_ds_compiled_shader
*gl_shaders
= shader_data
->gl_shaders
.ds
;
10450 for (i
= 0; i
< shader_data
->num_gl_shaders
; ++i
)
10452 TRACE("Deleting domain shader %u.\n", gl_shaders
[i
].id
);
10453 GL_EXTCALL(glDeleteShader(gl_shaders
[i
].id
));
10454 checkGLcall("glDeleteShader");
10456 HeapFree(GetProcessHeap(), 0, shader_data
->gl_shaders
.ds
);
10458 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, linked_programs
,
10459 struct glsl_shader_prog_link
, ds
.shader_entry
)
10461 shader_glsl_invalidate_contexts_program(device
, entry
);
10462 delete_glsl_program_entry(priv
, gl_info
, entry
);
10468 case WINED3D_SHADER_TYPE_GEOMETRY
:
10470 struct glsl_gs_compiled_shader
*gl_shaders
= shader_data
->gl_shaders
.gs
;
10472 for (i
= 0; i
< shader_data
->num_gl_shaders
; ++i
)
10474 TRACE("Deleting geometry shader %u.\n", gl_shaders
[i
].id
);
10475 GL_EXTCALL(glDeleteShader(gl_shaders
[i
].id
));
10476 checkGLcall("glDeleteShader");
10478 HeapFree(GetProcessHeap(), 0, shader_data
->gl_shaders
.gs
);
10480 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, linked_programs
,
10481 struct glsl_shader_prog_link
, gs
.shader_entry
)
10483 shader_glsl_invalidate_contexts_program(device
, entry
);
10484 delete_glsl_program_entry(priv
, gl_info
, entry
);
10490 case WINED3D_SHADER_TYPE_COMPUTE
:
10492 struct glsl_cs_compiled_shader
*gl_shaders
= shader_data
->gl_shaders
.cs
;
10494 for (i
= 0; i
< shader_data
->num_gl_shaders
; ++i
)
10496 TRACE("Deleting compute shader %u.\n", gl_shaders
[i
].id
);
10497 GL_EXTCALL(glDeleteShader(gl_shaders
[i
].id
));
10498 checkGLcall("glDeleteShader");
10500 HeapFree(GetProcessHeap(), 0, shader_data
->gl_shaders
.cs
);
10502 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, linked_programs
,
10503 struct glsl_shader_prog_link
, cs
.shader_entry
)
10505 shader_glsl_invalidate_contexts_program(device
, entry
);
10506 delete_glsl_program_entry(priv
, gl_info
, entry
);
10513 ERR("Unhandled shader type %#x.\n", shader
->reg_maps
.shader_version
.type
);
10518 HeapFree(GetProcessHeap(), 0, shader
->backend_data
);
10519 shader
->backend_data
= NULL
;
10521 context_release(context
);
10524 static int glsl_program_key_compare(const void *key
, const struct wine_rb_entry
*entry
)
10526 const struct glsl_program_key
*k
= key
;
10527 const struct glsl_shader_prog_link
*prog
= WINE_RB_ENTRY_VALUE(entry
,
10528 const struct glsl_shader_prog_link
, program_lookup_entry
);
10530 if (k
->vs_id
> prog
->vs
.id
) return 1;
10531 else if (k
->vs_id
< prog
->vs
.id
) return -1;
10533 if (k
->gs_id
> prog
->gs
.id
) return 1;
10534 else if (k
->gs_id
< prog
->gs
.id
) return -1;
10536 if (k
->ps_id
> prog
->ps
.id
) return 1;
10537 else if (k
->ps_id
< prog
->ps
.id
) return -1;
10539 if (k
->hs_id
> prog
->hs
.id
) return 1;
10540 else if (k
->hs_id
< prog
->hs
.id
) return -1;
10542 if (k
->ds_id
> prog
->ds
.id
) return 1;
10543 else if (k
->ds_id
< prog
->ds
.id
) return -1;
10545 if (k
->cs_id
> prog
->cs
.id
) return 1;
10546 else if (k
->cs_id
< prog
->cs
.id
) return -1;
10551 static BOOL
constant_heap_init(struct constant_heap
*heap
, unsigned int constant_count
)
10553 SIZE_T size
= (constant_count
+ 1) * sizeof(*heap
->entries
)
10554 + constant_count
* sizeof(*heap
->contained
)
10555 + constant_count
* sizeof(*heap
->positions
);
10556 void *mem
= HeapAlloc(GetProcessHeap(), 0, size
);
10560 ERR("Failed to allocate memory\n");
10564 heap
->entries
= mem
;
10565 heap
->entries
[1].version
= 0;
10566 heap
->contained
= (BOOL
*)(heap
->entries
+ constant_count
+ 1);
10567 memset(heap
->contained
, 0, constant_count
* sizeof(*heap
->contained
));
10568 heap
->positions
= (unsigned int *)(heap
->contained
+ constant_count
);
10574 static void constant_heap_free(struct constant_heap
*heap
)
10576 HeapFree(GetProcessHeap(), 0, heap
->entries
);
10579 static HRESULT
shader_glsl_alloc(struct wined3d_device
*device
, const struct wined3d_vertex_pipe_ops
*vertex_pipe
,
10580 const struct fragment_pipeline
*fragment_pipe
)
10582 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
10583 struct shader_glsl_priv
*priv
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(struct shader_glsl_priv
));
10584 SIZE_T stack_size
= wined3d_log2i(max(WINED3D_MAX_VS_CONSTS_F
, WINED3D_MAX_PS_CONSTS_F
)) + 1;
10585 struct fragment_caps fragment_caps
;
10586 void *vertex_priv
, *fragment_priv
;
10588 string_buffer_list_init(&priv
->string_buffers
);
10590 if (!(vertex_priv
= vertex_pipe
->vp_alloc(&glsl_shader_backend
, priv
)))
10592 ERR("Failed to initialize vertex pipe.\n");
10593 HeapFree(GetProcessHeap(), 0, priv
);
10597 if (!(fragment_priv
= fragment_pipe
->alloc_private(&glsl_shader_backend
, priv
)))
10599 ERR("Failed to initialize fragment pipe.\n");
10600 vertex_pipe
->vp_free(device
);
10601 HeapFree(GetProcessHeap(), 0, priv
);
10605 if (!string_buffer_init(&priv
->shader_buffer
))
10607 ERR("Failed to initialize shader buffer.\n");
10611 if (!(priv
->stack
= wined3d_calloc(stack_size
, sizeof(*priv
->stack
))))
10613 ERR("Failed to allocate memory.\n");
10617 if (!constant_heap_init(&priv
->vconst_heap
, WINED3D_MAX_VS_CONSTS_F
))
10619 ERR("Failed to initialize vertex shader constant heap\n");
10623 if (!constant_heap_init(&priv
->pconst_heap
, WINED3D_MAX_PS_CONSTS_F
))
10625 ERR("Failed to initialize pixel shader constant heap\n");
10629 wine_rb_init(&priv
->program_lookup
, glsl_program_key_compare
);
10631 priv
->next_constant_version
= 1;
10632 priv
->vertex_pipe
= vertex_pipe
;
10633 priv
->fragment_pipe
= fragment_pipe
;
10634 fragment_pipe
->get_caps(gl_info
, &fragment_caps
);
10635 priv
->ffp_proj_control
= fragment_caps
.wined3d_caps
& WINED3D_FRAGMENT_CAP_PROJ_CONTROL
;
10636 priv
->legacy_lighting
= device
->wined3d
->flags
& WINED3D_LEGACY_FFP_LIGHTING
;
10638 device
->vertex_priv
= vertex_priv
;
10639 device
->fragment_priv
= fragment_priv
;
10640 device
->shader_priv
= priv
;
10645 constant_heap_free(&priv
->pconst_heap
);
10646 constant_heap_free(&priv
->vconst_heap
);
10647 HeapFree(GetProcessHeap(), 0, priv
->stack
);
10648 string_buffer_free(&priv
->shader_buffer
);
10649 fragment_pipe
->free_private(device
);
10650 vertex_pipe
->vp_free(device
);
10651 HeapFree(GetProcessHeap(), 0, priv
);
10652 return E_OUTOFMEMORY
;
10655 /* Context activation is done by the caller. */
10656 static void shader_glsl_free(struct wined3d_device
*device
)
10658 struct shader_glsl_priv
*priv
= device
->shader_priv
;
10660 wine_rb_destroy(&priv
->program_lookup
, NULL
, NULL
);
10661 constant_heap_free(&priv
->pconst_heap
);
10662 constant_heap_free(&priv
->vconst_heap
);
10663 HeapFree(GetProcessHeap(), 0, priv
->stack
);
10664 string_buffer_list_cleanup(&priv
->string_buffers
);
10665 string_buffer_free(&priv
->shader_buffer
);
10666 priv
->fragment_pipe
->free_private(device
);
10667 priv
->vertex_pipe
->vp_free(device
);
10669 HeapFree(GetProcessHeap(), 0, device
->shader_priv
);
10670 device
->shader_priv
= NULL
;
10673 static BOOL
shader_glsl_allocate_context_data(struct wined3d_context
*context
)
10675 struct glsl_context_data
*ctx_data
;
10676 if (!(ctx_data
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*ctx_data
))))
10678 ctx_data
->vertex_color_clamp
= GL_FIXED_ONLY_ARB
;
10679 context
->shader_backend_data
= ctx_data
;
10683 static void shader_glsl_free_context_data(struct wined3d_context
*context
)
10685 HeapFree(GetProcessHeap(), 0, context
->shader_backend_data
);
10688 static void shader_glsl_init_context_state(struct wined3d_context
*context
)
10690 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
10692 gl_info
->gl_ops
.gl
.p_glEnable(GL_PROGRAM_POINT_SIZE
);
10693 checkGLcall("GL_PROGRAM_POINT_SIZE");
10696 static unsigned int shader_glsl_get_shader_model(const struct wined3d_gl_info
*gl_info
)
10698 BOOL shader_model_4
= gl_info
->glsl_version
>= MAKEDWORD_VERSION(1, 50)
10699 && gl_info
->supported
[WINED3D_GL_VERSION_3_2
]
10700 && gl_info
->supported
[ARB_SAMPLER_OBJECTS
]
10701 && gl_info
->supported
[ARB_SHADER_BIT_ENCODING
]
10702 && gl_info
->supported
[ARB_TEXTURE_SWIZZLE
];
10705 && gl_info
->supported
[ARB_COMPUTE_SHADER
]
10706 && gl_info
->supported
[ARB_DERIVATIVE_CONTROL
]
10707 && gl_info
->supported
[ARB_GPU_SHADER5
]
10708 && gl_info
->supported
[ARB_SHADER_ATOMIC_COUNTERS
]
10709 && gl_info
->supported
[ARB_SHADER_IMAGE_LOAD_STORE
]
10710 && gl_info
->supported
[ARB_SHADER_IMAGE_SIZE
]
10711 && gl_info
->supported
[ARB_SHADING_LANGUAGE_PACKING
]
10712 && gl_info
->supported
[ARB_TESSELLATION_SHADER
]
10713 && gl_info
->supported
[ARB_TEXTURE_GATHER
]
10714 && gl_info
->supported
[ARB_TRANSFORM_FEEDBACK3
])
10717 if (shader_model_4
)
10720 /* Support for texldd and texldl instructions in pixel shaders is required
10722 if (shader_glsl_has_core_grad(gl_info
) || gl_info
->supported
[ARB_SHADER_TEXTURE_LOD
])
10728 static void shader_glsl_get_caps(const struct wined3d_gl_info
*gl_info
, struct shader_caps
*caps
)
10730 unsigned int shader_model
= shader_glsl_get_shader_model(gl_info
);
10732 TRACE("Shader model %u.\n", shader_model
);
10734 caps
->vs_version
= min(wined3d_settings
.max_sm_vs
, shader_model
);
10735 caps
->hs_version
= min(wined3d_settings
.max_sm_hs
, shader_model
);
10736 caps
->ds_version
= min(wined3d_settings
.max_sm_ds
, shader_model
);
10737 caps
->gs_version
= min(wined3d_settings
.max_sm_gs
, shader_model
);
10738 caps
->ps_version
= min(wined3d_settings
.max_sm_ps
, shader_model
);
10739 caps
->cs_version
= min(wined3d_settings
.max_sm_cs
, shader_model
);
10741 caps
->vs_version
= gl_info
->supported
[ARB_VERTEX_SHADER
] ? caps
->vs_version
: 0;
10742 caps
->ps_version
= gl_info
->supported
[ARB_FRAGMENT_SHADER
] ? caps
->ps_version
: 0;
10744 caps
->vs_uniform_count
= min(WINED3D_MAX_VS_CONSTS_F
, gl_info
->limits
.glsl_vs_float_constants
);
10745 caps
->ps_uniform_count
= min(WINED3D_MAX_PS_CONSTS_F
, gl_info
->limits
.glsl_ps_float_constants
);
10746 caps
->varying_count
= gl_info
->limits
.glsl_varyings
;
10748 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
10749 * Direct3D minimum requirement.
10751 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
10752 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
10754 * The problem is that the refrast clamps temporary results in the shader to
10755 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
10756 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
10757 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
10758 * offer a way to query this.
10760 if (shader_model
>= 4)
10761 caps
->ps_1x_max_value
= FLT_MAX
;
10763 caps
->ps_1x_max_value
= 1024.0f
;
10765 /* Ideally we'd only set caps like sRGB writes here if supported by both
10766 * the shader backend and the fragment pipe, but we can get called before
10767 * shader_glsl_alloc(). */
10768 caps
->wined3d_caps
= WINED3D_SHADER_CAP_VS_CLIPPING
10769 | WINED3D_SHADER_CAP_SRGB_WRITE
;
10772 static BOOL
shader_glsl_color_fixup_supported(struct color_fixup_desc fixup
)
10774 /* We support everything except YUV conversions. */
10775 return !is_complex_fixup(fixup
);
10778 static const SHADER_HANDLER shader_glsl_instruction_handler_table
[WINED3DSIH_TABLE_SIZE
] =
10780 /* WINED3DSIH_ABS */ shader_glsl_map2gl
,
10781 /* WINED3DSIH_ADD */ shader_glsl_binop
,
10782 /* WINED3DSIH_AND */ shader_glsl_binop
,
10783 /* WINED3DSIH_ATOMIC_AND */ shader_glsl_atomic
,
10784 /* WINED3DSIH_ATOMIC_CMP_STORE */ shader_glsl_atomic
,
10785 /* WINED3DSIH_ATOMIC_IADD */ shader_glsl_atomic
,
10786 /* WINED3DSIH_ATOMIC_IMAX */ shader_glsl_atomic
,
10787 /* WINED3DSIH_ATOMIC_IMIN */ shader_glsl_atomic
,
10788 /* WINED3DSIH_ATOMIC_OR */ shader_glsl_atomic
,
10789 /* WINED3DSIH_ATOMIC_UMAX */ shader_glsl_atomic
,
10790 /* WINED3DSIH_ATOMIC_UMIN */ shader_glsl_atomic
,
10791 /* WINED3DSIH_ATOMIC_XOR */ shader_glsl_atomic
,
10792 /* WINED3DSIH_BEM */ shader_glsl_bem
,
10793 /* WINED3DSIH_BFI */ shader_glsl_bitwise_op
,
10794 /* WINED3DSIH_BFREV */ shader_glsl_map2gl
,
10795 /* WINED3DSIH_BREAK */ shader_glsl_break
,
10796 /* WINED3DSIH_BREAKC */ shader_glsl_breakc
,
10797 /* WINED3DSIH_BREAKP */ shader_glsl_conditional_op
,
10798 /* WINED3DSIH_BUFINFO */ shader_glsl_bufinfo
,
10799 /* WINED3DSIH_CALL */ shader_glsl_call
,
10800 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz
,
10801 /* WINED3DSIH_CASE */ shader_glsl_case
,
10802 /* WINED3DSIH_CMP */ shader_glsl_conditional_move
,
10803 /* WINED3DSIH_CND */ shader_glsl_cnd
,
10804 /* WINED3DSIH_CONTINUE */ shader_glsl_continue
,
10805 /* WINED3DSIH_CONTINUEP */ shader_glsl_conditional_op
,
10806 /* WINED3DSIH_COUNTBITS */ shader_glsl_map2gl
,
10807 /* WINED3DSIH_CRS */ shader_glsl_cross
,
10808 /* WINED3DSIH_CUT */ shader_glsl_cut
,
10809 /* WINED3DSIH_CUT_STREAM */ shader_glsl_cut
,
10810 /* WINED3DSIH_DCL */ shader_glsl_nop
,
10811 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ shader_glsl_nop
,
10812 /* WINED3DSIH_DCL_FUNCTION_BODY */ NULL
,
10813 /* WINED3DSIH_DCL_FUNCTION_TABLE */ NULL
,
10814 /* WINED3DSIH_DCL_GLOBAL_FLAGS */ shader_glsl_nop
,
10815 /* WINED3DSIH_DCL_GS_INSTANCES */ shader_glsl_nop
,
10816 /* WINED3DSIH_DCL_HS_FORK_PHASE_INSTANCE_COUNT */ shader_glsl_nop
,
10817 /* WINED3DSIH_DCL_HS_JOIN_PHASE_INSTANCE_COUNT */ shader_glsl_nop
,
10818 /* WINED3DSIH_DCL_HS_MAX_TESSFACTOR */ shader_glsl_nop
,
10819 /* WINED3DSIH_DCL_IMMEDIATE_CONSTANT_BUFFER */ shader_glsl_nop
,
10820 /* WINED3DSIH_DCL_INDEX_RANGE */ shader_glsl_nop
,
10821 /* WINED3DSIH_DCL_INDEXABLE_TEMP */ shader_glsl_nop
,
10822 /* WINED3DSIH_DCL_INPUT */ shader_glsl_nop
,
10823 /* WINED3DSIH_DCL_INPUT_CONTROL_POINT_COUNT */ shader_glsl_nop
,
10824 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ shader_glsl_nop
,
10825 /* WINED3DSIH_DCL_INPUT_PS */ shader_glsl_nop
,
10826 /* WINED3DSIH_DCL_INPUT_PS_SGV */ NULL
,
10827 /* WINED3DSIH_DCL_INPUT_PS_SIV */ NULL
,
10828 /* WINED3DSIH_DCL_INPUT_SGV */ shader_glsl_nop
,
10829 /* WINED3DSIH_DCL_INPUT_SIV */ shader_glsl_nop
,
10830 /* WINED3DSIH_DCL_INTERFACE */ NULL
,
10831 /* WINED3DSIH_DCL_OUTPUT */ shader_glsl_nop
,
10832 /* WINED3DSIH_DCL_OUTPUT_CONTROL_POINT_COUNT */ shader_glsl_nop
,
10833 /* WINED3DSIH_DCL_OUTPUT_SIV */ shader_glsl_nop
,
10834 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ shader_glsl_nop
,
10835 /* WINED3DSIH_DCL_RESOURCE_RAW */ shader_glsl_nop
,
10836 /* WINED3DSIH_DCL_RESOURCE_STRUCTURED */ shader_glsl_nop
,
10837 /* WINED3DSIH_DCL_SAMPLER */ shader_glsl_nop
,
10838 /* WINED3DSIH_DCL_STREAM */ NULL
,
10839 /* WINED3DSIH_DCL_TEMPS */ shader_glsl_nop
,
10840 /* WINED3DSIH_DCL_TESSELLATOR_DOMAIN */ shader_glsl_nop
,
10841 /* WINED3DSIH_DCL_TESSELLATOR_OUTPUT_PRIMITIVE */ shader_glsl_nop
,
10842 /* WINED3DSIH_DCL_TESSELLATOR_PARTITIONING */ shader_glsl_nop
,
10843 /* WINED3DSIH_DCL_TGSM_RAW */ shader_glsl_nop
,
10844 /* WINED3DSIH_DCL_TGSM_STRUCTURED */ shader_glsl_nop
,
10845 /* WINED3DSIH_DCL_THREAD_GROUP */ shader_glsl_nop
,
10846 /* WINED3DSIH_DCL_UAV_RAW */ shader_glsl_nop
,
10847 /* WINED3DSIH_DCL_UAV_STRUCTURED */ shader_glsl_nop
,
10848 /* WINED3DSIH_DCL_UAV_TYPED */ shader_glsl_nop
,
10849 /* WINED3DSIH_DCL_VERTICES_OUT */ shader_glsl_nop
,
10850 /* WINED3DSIH_DEF */ shader_glsl_nop
,
10851 /* WINED3DSIH_DEFAULT */ shader_glsl_default
,
10852 /* WINED3DSIH_DEFB */ shader_glsl_nop
,
10853 /* WINED3DSIH_DEFI */ shader_glsl_nop
,
10854 /* WINED3DSIH_DIV */ shader_glsl_binop
,
10855 /* WINED3DSIH_DP2 */ shader_glsl_dot
,
10856 /* WINED3DSIH_DP2ADD */ shader_glsl_dp2add
,
10857 /* WINED3DSIH_DP3 */ shader_glsl_dot
,
10858 /* WINED3DSIH_DP4 */ shader_glsl_dot
,
10859 /* WINED3DSIH_DST */ shader_glsl_dst
,
10860 /* WINED3DSIH_DSX */ shader_glsl_map2gl
,
10861 /* WINED3DSIH_DSX_COARSE */ shader_glsl_map2gl
,
10862 /* WINED3DSIH_DSX_FINE */ shader_glsl_map2gl
,
10863 /* WINED3DSIH_DSY */ shader_glsl_map2gl
,
10864 /* WINED3DSIH_DSY_COARSE */ shader_glsl_map2gl
,
10865 /* WINED3DSIH_DSY_FINE */ shader_glsl_map2gl
,
10866 /* WINED3DSIH_ELSE */ shader_glsl_else
,
10867 /* WINED3DSIH_EMIT */ shader_glsl_emit
,
10868 /* WINED3DSIH_EMIT_STREAM */ shader_glsl_emit
,
10869 /* WINED3DSIH_ENDIF */ shader_glsl_end
,
10870 /* WINED3DSIH_ENDLOOP */ shader_glsl_end
,
10871 /* WINED3DSIH_ENDREP */ shader_glsl_end
,
10872 /* WINED3DSIH_ENDSWITCH */ shader_glsl_end
,
10873 /* WINED3DSIH_EQ */ shader_glsl_relop
,
10874 /* WINED3DSIH_EXP */ shader_glsl_scalar_op
,
10875 /* WINED3DSIH_EXPP */ shader_glsl_expp
,
10876 /* WINED3DSIH_F16TOF32 */ shader_glsl_float16
,
10877 /* WINED3DSIH_F32TOF16 */ shader_glsl_float16
,
10878 /* WINED3DSIH_FCALL */ NULL
,
10879 /* WINED3DSIH_FIRSTBIT_HI */ shader_glsl_map2gl
,
10880 /* WINED3DSIH_FIRSTBIT_LO */ shader_glsl_map2gl
,
10881 /* WINED3DSIH_FIRSTBIT_SHI */ shader_glsl_map2gl
,
10882 /* WINED3DSIH_FRC */ shader_glsl_map2gl
,
10883 /* WINED3DSIH_FTOI */ shader_glsl_to_int
,
10884 /* WINED3DSIH_FTOU */ shader_glsl_to_uint
,
10885 /* WINED3DSIH_GATHER4 */ shader_glsl_gather4
,
10886 /* WINED3DSIH_GATHER4_C */ shader_glsl_gather4
,
10887 /* WINED3DSIH_GATHER4_PO */ shader_glsl_gather4
,
10888 /* WINED3DSIH_GATHER4_PO_C */ shader_glsl_gather4
,
10889 /* WINED3DSIH_GE */ shader_glsl_relop
,
10890 /* WINED3DSIH_HS_CONTROL_POINT_PHASE */ shader_glsl_nop
,
10891 /* WINED3DSIH_HS_DECLS */ shader_glsl_nop
,
10892 /* WINED3DSIH_HS_FORK_PHASE */ shader_glsl_nop
,
10893 /* WINED3DSIH_HS_JOIN_PHASE */ shader_glsl_nop
,
10894 /* WINED3DSIH_IADD */ shader_glsl_binop
,
10895 /* WINED3DSIH_IBFE */ shader_glsl_bitwise_op
,
10896 /* WINED3DSIH_IEQ */ shader_glsl_relop
,
10897 /* WINED3DSIH_IF */ shader_glsl_if
,
10898 /* WINED3DSIH_IFC */ shader_glsl_ifc
,
10899 /* WINED3DSIH_IGE */ shader_glsl_relop
,
10900 /* WINED3DSIH_ILT */ shader_glsl_relop
,
10901 /* WINED3DSIH_IMAD */ shader_glsl_mad
,
10902 /* WINED3DSIH_IMAX */ shader_glsl_map2gl
,
10903 /* WINED3DSIH_IMIN */ shader_glsl_map2gl
,
10904 /* WINED3DSIH_IMM_ATOMIC_ALLOC */ shader_glsl_uav_counter
,
10905 /* WINED3DSIH_IMM_ATOMIC_AND */ shader_glsl_atomic
,
10906 /* WINED3DSIH_IMM_ATOMIC_CMP_EXCH */ shader_glsl_atomic
,
10907 /* WINED3DSIH_IMM_ATOMIC_CONSUME */ shader_glsl_uav_counter
,
10908 /* WINED3DSIH_IMM_ATOMIC_EXCH */ shader_glsl_atomic
,
10909 /* WINED3DSIH_IMM_ATOMIC_IADD */ shader_glsl_atomic
,
10910 /* WINED3DSIH_IMM_ATOMIC_IMAX */ shader_glsl_atomic
,
10911 /* WINED3DSIH_IMM_ATOMIC_IMIN */ shader_glsl_atomic
,
10912 /* WINED3DSIH_IMM_ATOMIC_OR */ shader_glsl_atomic
,
10913 /* WINED3DSIH_IMM_ATOMIC_UMAX */ shader_glsl_atomic
,
10914 /* WINED3DSIH_IMM_ATOMIC_UMIN */ shader_glsl_atomic
,
10915 /* WINED3DSIH_IMM_ATOMIC_XOR */ shader_glsl_atomic
,
10916 /* WINED3DSIH_IMUL */ shader_glsl_mul_extended
,
10917 /* WINED3DSIH_INE */ shader_glsl_relop
,
10918 /* WINED3DSIH_INEG */ shader_glsl_unary_op
,
10919 /* WINED3DSIH_ISHL */ shader_glsl_binop
,
10920 /* WINED3DSIH_ISHR */ shader_glsl_binop
,
10921 /* WINED3DSIH_ITOF */ shader_glsl_to_float
,
10922 /* WINED3DSIH_LABEL */ shader_glsl_label
,
10923 /* WINED3DSIH_LD */ shader_glsl_ld
,
10924 /* WINED3DSIH_LD2DMS */ NULL
,
10925 /* WINED3DSIH_LD_RAW */ shader_glsl_ld_raw_structured
,
10926 /* WINED3DSIH_LD_STRUCTURED */ shader_glsl_ld_raw_structured
,
10927 /* WINED3DSIH_LD_UAV_TYPED */ shader_glsl_ld_uav
,
10928 /* WINED3DSIH_LIT */ shader_glsl_lit
,
10929 /* WINED3DSIH_LOD */ NULL
,
10930 /* WINED3DSIH_LOG */ shader_glsl_scalar_op
,
10931 /* WINED3DSIH_LOGP */ shader_glsl_scalar_op
,
10932 /* WINED3DSIH_LOOP */ shader_glsl_loop
,
10933 /* WINED3DSIH_LRP */ shader_glsl_lrp
,
10934 /* WINED3DSIH_LT */ shader_glsl_relop
,
10935 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn
,
10936 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn
,
10937 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn
,
10938 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn
,
10939 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn
,
10940 /* WINED3DSIH_MAD */ shader_glsl_mad
,
10941 /* WINED3DSIH_MAX */ shader_glsl_map2gl
,
10942 /* WINED3DSIH_MIN */ shader_glsl_map2gl
,
10943 /* WINED3DSIH_MOV */ shader_glsl_mov
,
10944 /* WINED3DSIH_MOVA */ shader_glsl_mov
,
10945 /* WINED3DSIH_MOVC */ shader_glsl_conditional_move
,
10946 /* WINED3DSIH_MUL */ shader_glsl_binop
,
10947 /* WINED3DSIH_NE */ shader_glsl_relop
,
10948 /* WINED3DSIH_NOP */ shader_glsl_nop
,
10949 /* WINED3DSIH_NOT */ shader_glsl_unary_op
,
10950 /* WINED3DSIH_NRM */ shader_glsl_nrm
,
10951 /* WINED3DSIH_OR */ shader_glsl_binop
,
10952 /* WINED3DSIH_PHASE */ shader_glsl_nop
,
10953 /* WINED3DSIH_POW */ shader_glsl_pow
,
10954 /* WINED3DSIH_RCP */ shader_glsl_scalar_op
,
10955 /* WINED3DSIH_REP */ shader_glsl_rep
,
10956 /* WINED3DSIH_RESINFO */ shader_glsl_resinfo
,
10957 /* WINED3DSIH_RET */ shader_glsl_ret
,
10958 /* WINED3DSIH_RETP */ shader_glsl_conditional_op
,
10959 /* WINED3DSIH_ROUND_NE */ shader_glsl_map2gl
,
10960 /* WINED3DSIH_ROUND_NI */ shader_glsl_map2gl
,
10961 /* WINED3DSIH_ROUND_PI */ shader_glsl_map2gl
,
10962 /* WINED3DSIH_ROUND_Z */ shader_glsl_map2gl
,
10963 /* WINED3DSIH_RSQ */ shader_glsl_scalar_op
,
10964 /* WINED3DSIH_SAMPLE */ shader_glsl_sample
,
10965 /* WINED3DSIH_SAMPLE_B */ shader_glsl_sample
,
10966 /* WINED3DSIH_SAMPLE_C */ shader_glsl_sample_c
,
10967 /* WINED3DSIH_SAMPLE_C_LZ */ shader_glsl_sample_c
,
10968 /* WINED3DSIH_SAMPLE_GRAD */ shader_glsl_sample
,
10969 /* WINED3DSIH_SAMPLE_INFO */ NULL
,
10970 /* WINED3DSIH_SAMPLE_LOD */ shader_glsl_sample
,
10971 /* WINED3DSIH_SAMPLE_POS */ NULL
,
10972 /* WINED3DSIH_SETP */ NULL
,
10973 /* WINED3DSIH_SGE */ shader_glsl_compare
,
10974 /* WINED3DSIH_SGN */ shader_glsl_sgn
,
10975 /* WINED3DSIH_SINCOS */ shader_glsl_sincos
,
10976 /* WINED3DSIH_SLT */ shader_glsl_compare
,
10977 /* WINED3DSIH_SQRT */ shader_glsl_map2gl
,
10978 /* WINED3DSIH_STORE_RAW */ shader_glsl_store_raw_structured
,
10979 /* WINED3DSIH_STORE_STRUCTURED */ shader_glsl_store_raw_structured
,
10980 /* WINED3DSIH_STORE_UAV_TYPED */ shader_glsl_store_uav
,
10981 /* WINED3DSIH_SUB */ shader_glsl_binop
,
10982 /* WINED3DSIH_SWAPC */ shader_glsl_swapc
,
10983 /* WINED3DSIH_SWITCH */ shader_glsl_switch
,
10984 /* WINED3DSIH_SYNC */ shader_glsl_sync
,
10985 /* WINED3DSIH_TEX */ shader_glsl_tex
,
10986 /* WINED3DSIH_TEXBEM */ shader_glsl_texbem
,
10987 /* WINED3DSIH_TEXBEML */ shader_glsl_texbem
,
10988 /* WINED3DSIH_TEXCOORD */ shader_glsl_texcoord
,
10989 /* WINED3DSIH_TEXDEPTH */ shader_glsl_texdepth
,
10990 /* WINED3DSIH_TEXDP3 */ shader_glsl_texdp3
,
10991 /* WINED3DSIH_TEXDP3TEX */ shader_glsl_texdp3tex
,
10992 /* WINED3DSIH_TEXKILL */ shader_glsl_texkill
,
10993 /* WINED3DSIH_TEXLDD */ shader_glsl_texldd
,
10994 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl
,
10995 /* WINED3DSIH_TEXM3x2DEPTH */ shader_glsl_texm3x2depth
,
10996 /* WINED3DSIH_TEXM3x2PAD */ shader_glsl_texm3x2pad
,
10997 /* WINED3DSIH_TEXM3x2TEX */ shader_glsl_texm3x2tex
,
10998 /* WINED3DSIH_TEXM3x3 */ shader_glsl_texm3x3
,
10999 /* WINED3DSIH_TEXM3x3DIFF */ NULL
,
11000 /* WINED3DSIH_TEXM3x3PAD */ shader_glsl_texm3x3pad
,
11001 /* WINED3DSIH_TEXM3x3SPEC */ shader_glsl_texm3x3spec
,
11002 /* WINED3DSIH_TEXM3x3TEX */ shader_glsl_texm3x3tex
,
11003 /* WINED3DSIH_TEXM3x3VSPEC */ shader_glsl_texm3x3vspec
,
11004 /* WINED3DSIH_TEXREG2AR */ shader_glsl_texreg2ar
,
11005 /* WINED3DSIH_TEXREG2GB */ shader_glsl_texreg2gb
,
11006 /* WINED3DSIH_TEXREG2RGB */ shader_glsl_texreg2rgb
,
11007 /* WINED3DSIH_UBFE */ shader_glsl_bitwise_op
,
11008 /* WINED3DSIH_UDIV */ shader_glsl_udiv
,
11009 /* WINED3DSIH_UGE */ shader_glsl_relop
,
11010 /* WINED3DSIH_ULT */ shader_glsl_relop
,
11011 /* WINED3DSIH_UMAX */ shader_glsl_map2gl
,
11012 /* WINED3DSIH_UMIN */ shader_glsl_map2gl
,
11013 /* WINED3DSIH_UMUL */ shader_glsl_mul_extended
,
11014 /* WINED3DSIH_USHR */ shader_glsl_binop
,
11015 /* WINED3DSIH_UTOF */ shader_glsl_to_float
,
11016 /* WINED3DSIH_XOR */ shader_glsl_binop
,
11019 static void shader_glsl_handle_instruction(const struct wined3d_shader_instruction
*ins
) {
11020 SHADER_HANDLER hw_fct
;
11022 /* Select handler */
11023 hw_fct
= shader_glsl_instruction_handler_table
[ins
->handler_idx
];
11025 /* Unhandled opcode */
11028 FIXME("Backend can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins
->handler_idx
));
11033 shader_glsl_add_instruction_modifiers(ins
);
11036 static BOOL
shader_glsl_has_ffp_proj_control(void *shader_priv
)
11038 struct shader_glsl_priv
*priv
= shader_priv
;
11040 return priv
->ffp_proj_control
;
11043 const struct wined3d_shader_backend_ops glsl_shader_backend
=
11045 shader_glsl_handle_instruction
,
11046 shader_glsl_precompile
,
11047 shader_glsl_select
,
11048 shader_glsl_select_compute
,
11049 shader_glsl_disable
,
11050 shader_glsl_update_float_vertex_constants
,
11051 shader_glsl_update_float_pixel_constants
,
11052 shader_glsl_load_constants
,
11053 shader_glsl_destroy
,
11056 shader_glsl_allocate_context_data
,
11057 shader_glsl_free_context_data
,
11058 shader_glsl_init_context_state
,
11059 shader_glsl_get_caps
,
11060 shader_glsl_color_fixup_supported
,
11061 shader_glsl_has_ffp_proj_control
,
11064 static void glsl_vertex_pipe_vp_enable(const struct wined3d_gl_info
*gl_info
, BOOL enable
) {}
11066 static void glsl_vertex_pipe_vp_get_caps(const struct wined3d_gl_info
*gl_info
, struct wined3d_vertex_caps
*caps
)
11068 caps
->xyzrhw
= TRUE
;
11069 caps
->emulated_flatshading
= !needs_legacy_glsl_syntax(gl_info
);
11070 caps
->ffp_generic_attributes
= TRUE
;
11071 caps
->max_active_lights
= MAX_ACTIVE_LIGHTS
;
11072 caps
->max_vertex_blend_matrices
= MAX_VERTEX_BLENDS
;
11073 caps
->max_vertex_blend_matrix_index
= 0;
11074 caps
->vertex_processing_caps
= WINED3DVTXPCAPS_TEXGEN
11075 | WINED3DVTXPCAPS_MATERIALSOURCE7
11076 | WINED3DVTXPCAPS_VERTEXFOG
11077 | WINED3DVTXPCAPS_DIRECTIONALLIGHTS
11078 | WINED3DVTXPCAPS_POSITIONALLIGHTS
11079 | WINED3DVTXPCAPS_LOCALVIEWER
11080 | WINED3DVTXPCAPS_TEXGEN_SPHEREMAP
;
11081 caps
->fvf_caps
= WINED3DFVFCAPS_PSIZE
| 8; /* 8 texture coordinates. */
11082 caps
->max_user_clip_planes
= gl_info
->limits
.user_clip_distances
;
11083 caps
->raster_caps
= WINED3DPRASTERCAPS_FOGRANGE
;
11086 static DWORD
glsl_vertex_pipe_vp_get_emul_mask(const struct wined3d_gl_info
*gl_info
)
11088 if (gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
])
11089 return GL_EXT_EMUL_ARB_MULTITEXTURE
;
11093 static void *glsl_vertex_pipe_vp_alloc(const struct wined3d_shader_backend_ops
*shader_backend
, void *shader_priv
)
11095 struct shader_glsl_priv
*priv
;
11097 if (shader_backend
== &glsl_shader_backend
)
11099 priv
= shader_priv
;
11100 wine_rb_init(&priv
->ffp_vertex_shaders
, wined3d_ffp_vertex_program_key_compare
);
11104 FIXME("GLSL vertex pipe without GLSL shader backend not implemented.\n");
11109 static void shader_glsl_free_ffp_vertex_shader(struct wine_rb_entry
*entry
, void *context
)
11111 struct glsl_ffp_vertex_shader
*shader
= WINE_RB_ENTRY_VALUE(entry
,
11112 struct glsl_ffp_vertex_shader
, desc
.entry
);
11113 struct glsl_shader_prog_link
*program
, *program2
;
11114 struct glsl_ffp_destroy_ctx
*ctx
= context
;
11116 LIST_FOR_EACH_ENTRY_SAFE(program
, program2
, &shader
->linked_programs
,
11117 struct glsl_shader_prog_link
, vs
.shader_entry
)
11119 delete_glsl_program_entry(ctx
->priv
, ctx
->gl_info
, program
);
11121 ctx
->gl_info
->gl_ops
.ext
.p_glDeleteShader(shader
->id
);
11122 HeapFree(GetProcessHeap(), 0, shader
);
11125 /* Context activation is done by the caller. */
11126 static void glsl_vertex_pipe_vp_free(struct wined3d_device
*device
)
11128 struct shader_glsl_priv
*priv
= device
->vertex_priv
;
11129 struct glsl_ffp_destroy_ctx ctx
;
11132 ctx
.gl_info
= &device
->adapter
->gl_info
;
11133 wine_rb_destroy(&priv
->ffp_vertex_shaders
, shader_glsl_free_ffp_vertex_shader
, &ctx
);
11136 static void glsl_vertex_pipe_nop(struct wined3d_context
*context
,
11137 const struct wined3d_state
*state
, DWORD state_id
) {}
11139 static void glsl_vertex_pipe_shader(struct wined3d_context
*context
,
11140 const struct wined3d_state
*state
, DWORD state_id
)
11142 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_VERTEX
;
11145 static void glsl_vertex_pipe_vdecl(struct wined3d_context
*context
,
11146 const struct wined3d_state
*state
, DWORD state_id
)
11148 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
11149 BOOL normal
= !!(context
->stream_info
.use_map
& (1u << WINED3D_FFP_NORMAL
));
11150 const BOOL legacy_clip_planes
= needs_legacy_glsl_syntax(gl_info
);
11151 BOOL transformed
= context
->stream_info
.position_transformed
;
11152 BOOL wasrhw
= context
->last_was_rhw
;
11155 context
->last_was_rhw
= transformed
;
11157 /* If the vertex declaration contains a transformed position attribute,
11158 * the draw uses the fixed function vertex pipeline regardless of any
11159 * vertex shader set by the application. */
11160 if (transformed
!= wasrhw
11161 || context
->stream_info
.swizzle_map
!= context
->last_swizzle_map
)
11162 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_VERTEX
;
11164 context
->last_swizzle_map
= context
->stream_info
.swizzle_map
;
11166 if (!use_vs(state
))
11168 if (context
->last_was_vshader
)
11170 if (legacy_clip_planes
)
11171 for (i
= 0; i
< gl_info
->limits
.user_clip_distances
; ++i
)
11172 clipplane(context
, state
, STATE_CLIPPLANE(i
));
11174 context
->constant_update_mask
|= WINED3D_SHADER_CONST_VS_CLIP_PLANES
;
11177 context
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_TEXMATRIX
;
11179 /* Because of settings->texcoords, we have to regenerate the vertex
11180 * shader on a vdecl change if there aren't enough varyings to just
11181 * always output all the texture coordinates. */
11182 if (gl_info
->limits
.glsl_varyings
< wined3d_max_compat_varyings(gl_info
)
11183 || normal
!= context
->last_was_normal
)
11184 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_VERTEX
;
11187 && state
->shader
[WINED3D_SHADER_TYPE_PIXEL
]->reg_maps
.shader_version
.major
== 1
11188 && state
->shader
[WINED3D_SHADER_TYPE_PIXEL
]->reg_maps
.shader_version
.minor
<= 3)
11189 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_PIXEL
;
11193 if (!context
->last_was_vshader
)
11195 /* Vertex shader clipping ignores the view matrix. Update all clip planes. */
11196 if (legacy_clip_planes
)
11197 for (i
= 0; i
< gl_info
->limits
.user_clip_distances
; ++i
)
11198 clipplane(context
, state
, STATE_CLIPPLANE(i
));
11200 context
->constant_update_mask
|= WINED3D_SHADER_CONST_VS_CLIP_PLANES
;
11204 context
->last_was_vshader
= use_vs(state
);
11205 context
->last_was_normal
= normal
;
11208 static void glsl_vertex_pipe_vs(struct wined3d_context
*context
,
11209 const struct wined3d_state
*state
, DWORD state_id
)
11211 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_VERTEX
;
11212 /* Different vertex shaders potentially require a different vertex attributes setup. */
11213 if (!isStateDirty(context
, STATE_VDECL
))
11214 context_apply_state(context
, state
, STATE_VDECL
);
11217 static void glsl_vertex_pipe_hs(struct wined3d_context
*context
,
11218 const struct wined3d_state
*state
, DWORD state_id
)
11220 /* In Direct3D tessellator options (e.g. output primitive type, primitive
11221 * winding) are defined in Hull Shaders, while in GLSL those are
11222 * specified in Tessellation Evaluation Shaders. */
11223 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_DOMAIN
;
11225 if (state
->shader
[WINED3D_SHADER_TYPE_VERTEX
])
11226 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_VERTEX
;
11229 static void glsl_vertex_pipe_geometry_shader(struct wined3d_context
*context
,
11230 const struct wined3d_state
*state
, DWORD state_id
)
11232 if (state
->shader
[WINED3D_SHADER_TYPE_DOMAIN
])
11233 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_DOMAIN
;
11234 else if (state
->shader
[WINED3D_SHADER_TYPE_VERTEX
]
11235 && state
->shader
[WINED3D_SHADER_TYPE_VERTEX
]->reg_maps
.shader_version
.major
>= 4)
11236 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_VERTEX
;
11239 static void glsl_vertex_pipe_pixel_shader(struct wined3d_context
*context
,
11240 const struct wined3d_state
*state
, DWORD state_id
)
11242 if (state
->shader
[WINED3D_SHADER_TYPE_GEOMETRY
])
11243 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_GEOMETRY
;
11244 else if (state
->shader
[WINED3D_SHADER_TYPE_DOMAIN
])
11245 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_DOMAIN
;
11246 else if (state
->shader
[WINED3D_SHADER_TYPE_VERTEX
]
11247 && state
->shader
[WINED3D_SHADER_TYPE_VERTEX
]->reg_maps
.shader_version
.major
>= 4)
11248 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_VERTEX
;
11251 static void glsl_vertex_pipe_world(struct wined3d_context
*context
,
11252 const struct wined3d_state
*state
, DWORD state_id
)
11254 context
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_MODELVIEW
;
11257 static void glsl_vertex_pipe_vertexblend(struct wined3d_context
*context
,
11258 const struct wined3d_state
*state
, DWORD state_id
)
11260 context
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_VERTEXBLEND
;
11263 static void glsl_vertex_pipe_view(struct wined3d_context
*context
, const struct wined3d_state
*state
, DWORD state_id
)
11265 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
11268 context
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_MODELVIEW
11269 | WINED3D_SHADER_CONST_FFP_LIGHTS
11270 | WINED3D_SHADER_CONST_FFP_VERTEXBLEND
;
11272 if (needs_legacy_glsl_syntax(gl_info
))
11274 for (k
= 0; k
< gl_info
->limits
.user_clip_distances
; ++k
)
11276 if (!isStateDirty(context
, STATE_CLIPPLANE(k
)))
11277 clipplane(context
, state
, STATE_CLIPPLANE(k
));
11282 context
->constant_update_mask
|= WINED3D_SHADER_CONST_VS_CLIP_PLANES
;
11286 static void glsl_vertex_pipe_projection(struct wined3d_context
*context
,
11287 const struct wined3d_state
*state
, DWORD state_id
)
11289 /* Table fog behavior depends on the projection matrix. */
11290 if (state
->render_states
[WINED3D_RS_FOGENABLE
]
11291 && state
->render_states
[WINED3D_RS_FOGTABLEMODE
] != WINED3D_FOG_NONE
)
11292 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_VERTEX
;
11293 context
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_PROJ
;
11296 static void glsl_vertex_pipe_viewport(struct wined3d_context
*context
,
11297 const struct wined3d_state
*state
, DWORD state_id
)
11299 if (!isStateDirty(context
, STATE_TRANSFORM(WINED3D_TS_PROJECTION
)))
11300 glsl_vertex_pipe_projection(context
, state
, STATE_TRANSFORM(WINED3D_TS_PROJECTION
));
11301 if (!isStateDirty(context
, STATE_RENDER(WINED3D_RS_POINTSCALEENABLE
))
11302 && state
->render_states
[WINED3D_RS_POINTSCALEENABLE
])
11303 context
->constant_update_mask
|= WINED3D_SHADER_CONST_VS_POINTSIZE
;
11304 context
->constant_update_mask
|= WINED3D_SHADER_CONST_POS_FIXUP
;
11307 static void glsl_vertex_pipe_texmatrix(struct wined3d_context
*context
,
11308 const struct wined3d_state
*state
, DWORD state_id
)
11310 context
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_TEXMATRIX
;
11313 static void glsl_vertex_pipe_texmatrix_np2(struct wined3d_context
*context
,
11314 const struct wined3d_state
*state
, DWORD state_id
)
11316 DWORD sampler
= state_id
- STATE_SAMPLER(0);
11317 const struct wined3d_texture
*texture
= state
->textures
[sampler
];
11323 if (sampler
>= MAX_TEXTURES
)
11326 if ((np2
= !(texture
->flags
& WINED3D_TEXTURE_POW2_MAT_IDENT
))
11327 || context
->lastWasPow2Texture
& (1u << sampler
))
11330 context
->lastWasPow2Texture
|= 1u << sampler
;
11332 context
->lastWasPow2Texture
&= ~(1u << sampler
);
11334 context
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_TEXMATRIX
;
11338 static void glsl_vertex_pipe_material(struct wined3d_context
*context
,
11339 const struct wined3d_state
*state
, DWORD state_id
)
11341 context
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_MATERIAL
;
11344 static void glsl_vertex_pipe_light(struct wined3d_context
*context
,
11345 const struct wined3d_state
*state
, DWORD state_id
)
11347 context
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_LIGHTS
;
11350 static void glsl_vertex_pipe_pointsize(struct wined3d_context
*context
,
11351 const struct wined3d_state
*state
, DWORD state_id
)
11353 context
->constant_update_mask
|= WINED3D_SHADER_CONST_VS_POINTSIZE
;
11356 static void glsl_vertex_pipe_pointscale(struct wined3d_context
*context
,
11357 const struct wined3d_state
*state
, DWORD state_id
)
11359 if (!use_vs(state
))
11360 context
->constant_update_mask
|= WINED3D_SHADER_CONST_VS_POINTSIZE
;
11363 static void glsl_vertex_pointsprite_core(struct wined3d_context
*context
,
11364 const struct wined3d_state
*state
, DWORD state_id
)
11366 static unsigned int once
;
11368 if (state
->gl_primitive_type
== GL_POINTS
&& !state
->render_states
[WINED3D_RS_POINTSPRITEENABLE
] && !once
++)
11369 FIXME("Non-point sprite points not supported in core profile.\n");
11372 static void glsl_vertex_pipe_shademode(struct wined3d_context
*context
,
11373 const struct wined3d_state
*state
, DWORD state_id
)
11375 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_VERTEX
;
11378 static void glsl_vertex_pipe_clip_plane(struct wined3d_context
*context
,
11379 const struct wined3d_state
*state
, DWORD state_id
)
11381 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
11382 UINT index
= state_id
- STATE_CLIPPLANE(0);
11384 if (index
>= gl_info
->limits
.user_clip_distances
)
11387 context
->constant_update_mask
|= WINED3D_SHADER_CONST_VS_CLIP_PLANES
;
11390 static const struct StateEntryTemplate glsl_vertex_pipe_vp_states
[] =
11392 {STATE_VDECL
, {STATE_VDECL
, glsl_vertex_pipe_vdecl
}, WINED3D_GL_EXT_NONE
},
11393 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), glsl_vertex_pipe_vs
}, WINED3D_GL_EXT_NONE
},
11394 {STATE_SHADER(WINED3D_SHADER_TYPE_HULL
), {STATE_SHADER(WINED3D_SHADER_TYPE_HULL
), glsl_vertex_pipe_hs
}, WINED3D_GL_EXT_NONE
},
11395 {STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY
), {STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY
), glsl_vertex_pipe_geometry_shader
}, WINED3D_GL_EXT_NONE
},
11396 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), glsl_vertex_pipe_pixel_shader
}, WINED3D_GL_EXT_NONE
},
11397 {STATE_MATERIAL
, {STATE_RENDER(WINED3D_RS_SPECULARENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
11398 {STATE_RENDER(WINED3D_RS_SPECULARENABLE
), {STATE_RENDER(WINED3D_RS_SPECULARENABLE
), glsl_vertex_pipe_material
}, WINED3D_GL_EXT_NONE
},
11400 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), glsl_vertex_pipe_clip_plane
}, WINED3D_GLSL_130
},
11401 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), clipplane
}, WINED3D_GL_EXT_NONE
},
11402 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), glsl_vertex_pipe_clip_plane
}, WINED3D_GLSL_130
},
11403 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), clipplane
}, WINED3D_GL_EXT_NONE
},
11404 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), glsl_vertex_pipe_clip_plane
}, WINED3D_GLSL_130
},
11405 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), clipplane
}, WINED3D_GL_EXT_NONE
},
11406 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), glsl_vertex_pipe_clip_plane
}, WINED3D_GLSL_130
},
11407 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), clipplane
}, WINED3D_GL_EXT_NONE
},
11408 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), glsl_vertex_pipe_clip_plane
}, WINED3D_GLSL_130
},
11409 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), clipplane
}, WINED3D_GL_EXT_NONE
},
11410 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), glsl_vertex_pipe_clip_plane
}, WINED3D_GLSL_130
},
11411 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), clipplane
}, WINED3D_GL_EXT_NONE
},
11412 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), glsl_vertex_pipe_clip_plane
}, WINED3D_GLSL_130
},
11413 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), clipplane
}, WINED3D_GL_EXT_NONE
},
11414 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), glsl_vertex_pipe_clip_plane
}, WINED3D_GLSL_130
},
11415 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), clipplane
}, WINED3D_GL_EXT_NONE
},
11416 {STATE_CLIPPLANE(8), {STATE_CLIPPLANE(8), glsl_vertex_pipe_clip_plane
}, WINED3D_GLSL_130
},
11417 {STATE_CLIPPLANE(8), {STATE_CLIPPLANE(8), clipplane
}, WINED3D_GL_EXT_NONE
},
11418 {STATE_CLIPPLANE(9), {STATE_CLIPPLANE(9), glsl_vertex_pipe_clip_plane
}, WINED3D_GLSL_130
},
11419 {STATE_CLIPPLANE(9), {STATE_CLIPPLANE(9), clipplane
}, WINED3D_GL_EXT_NONE
},
11420 {STATE_CLIPPLANE(10), {STATE_CLIPPLANE(10), glsl_vertex_pipe_clip_plane
}, WINED3D_GLSL_130
},
11421 {STATE_CLIPPLANE(10), {STATE_CLIPPLANE(10), clipplane
}, WINED3D_GL_EXT_NONE
},
11422 {STATE_CLIPPLANE(11), {STATE_CLIPPLANE(11), glsl_vertex_pipe_clip_plane
}, WINED3D_GLSL_130
},
11423 {STATE_CLIPPLANE(11), {STATE_CLIPPLANE(11), clipplane
}, WINED3D_GL_EXT_NONE
},
11424 {STATE_CLIPPLANE(12), {STATE_CLIPPLANE(12), glsl_vertex_pipe_clip_plane
}, WINED3D_GLSL_130
},
11425 {STATE_CLIPPLANE(12), {STATE_CLIPPLANE(12), clipplane
}, WINED3D_GL_EXT_NONE
},
11426 {STATE_CLIPPLANE(13), {STATE_CLIPPLANE(13), glsl_vertex_pipe_clip_plane
}, WINED3D_GLSL_130
},
11427 {STATE_CLIPPLANE(13), {STATE_CLIPPLANE(13), clipplane
}, WINED3D_GL_EXT_NONE
},
11428 {STATE_CLIPPLANE(14), {STATE_CLIPPLANE(14), glsl_vertex_pipe_clip_plane
}, WINED3D_GLSL_130
},
11429 {STATE_CLIPPLANE(14), {STATE_CLIPPLANE(14), clipplane
}, WINED3D_GL_EXT_NONE
},
11430 {STATE_CLIPPLANE(15), {STATE_CLIPPLANE(15), glsl_vertex_pipe_clip_plane
}, WINED3D_GLSL_130
},
11431 {STATE_CLIPPLANE(15), {STATE_CLIPPLANE(15), clipplane
}, WINED3D_GL_EXT_NONE
},
11432 {STATE_CLIPPLANE(16), {STATE_CLIPPLANE(16), glsl_vertex_pipe_clip_plane
}, WINED3D_GLSL_130
},
11433 {STATE_CLIPPLANE(16), {STATE_CLIPPLANE(16), clipplane
}, WINED3D_GL_EXT_NONE
},
11434 {STATE_CLIPPLANE(17), {STATE_CLIPPLANE(17), glsl_vertex_pipe_clip_plane
}, WINED3D_GLSL_130
},
11435 {STATE_CLIPPLANE(17), {STATE_CLIPPLANE(17), clipplane
}, WINED3D_GL_EXT_NONE
},
11436 {STATE_CLIPPLANE(18), {STATE_CLIPPLANE(18), glsl_vertex_pipe_clip_plane
}, WINED3D_GLSL_130
},
11437 {STATE_CLIPPLANE(18), {STATE_CLIPPLANE(18), clipplane
}, WINED3D_GL_EXT_NONE
},
11438 {STATE_CLIPPLANE(19), {STATE_CLIPPLANE(19), glsl_vertex_pipe_clip_plane
}, WINED3D_GLSL_130
},
11439 {STATE_CLIPPLANE(19), {STATE_CLIPPLANE(19), clipplane
}, WINED3D_GL_EXT_NONE
},
11440 {STATE_CLIPPLANE(20), {STATE_CLIPPLANE(20), glsl_vertex_pipe_clip_plane
}, WINED3D_GLSL_130
},
11441 {STATE_CLIPPLANE(20), {STATE_CLIPPLANE(20), clipplane
}, WINED3D_GL_EXT_NONE
},
11442 {STATE_CLIPPLANE(21), {STATE_CLIPPLANE(21), glsl_vertex_pipe_clip_plane
}, WINED3D_GLSL_130
},
11443 {STATE_CLIPPLANE(21), {STATE_CLIPPLANE(21), clipplane
}, WINED3D_GL_EXT_NONE
},
11444 {STATE_CLIPPLANE(22), {STATE_CLIPPLANE(22), glsl_vertex_pipe_clip_plane
}, WINED3D_GLSL_130
},
11445 {STATE_CLIPPLANE(22), {STATE_CLIPPLANE(22), clipplane
}, WINED3D_GL_EXT_NONE
},
11446 {STATE_CLIPPLANE(23), {STATE_CLIPPLANE(23), glsl_vertex_pipe_clip_plane
}, WINED3D_GLSL_130
},
11447 {STATE_CLIPPLANE(23), {STATE_CLIPPLANE(23), clipplane
}, WINED3D_GL_EXT_NONE
},
11448 {STATE_CLIPPLANE(24), {STATE_CLIPPLANE(24), glsl_vertex_pipe_clip_plane
}, WINED3D_GLSL_130
},
11449 {STATE_CLIPPLANE(24), {STATE_CLIPPLANE(24), clipplane
}, WINED3D_GL_EXT_NONE
},
11450 {STATE_CLIPPLANE(25), {STATE_CLIPPLANE(25), glsl_vertex_pipe_clip_plane
}, WINED3D_GLSL_130
},
11451 {STATE_CLIPPLANE(25), {STATE_CLIPPLANE(25), clipplane
}, WINED3D_GL_EXT_NONE
},
11452 {STATE_CLIPPLANE(26), {STATE_CLIPPLANE(26), glsl_vertex_pipe_clip_plane
}, WINED3D_GLSL_130
},
11453 {STATE_CLIPPLANE(26), {STATE_CLIPPLANE(26), clipplane
}, WINED3D_GL_EXT_NONE
},
11454 {STATE_CLIPPLANE(27), {STATE_CLIPPLANE(27), glsl_vertex_pipe_clip_plane
}, WINED3D_GLSL_130
},
11455 {STATE_CLIPPLANE(27), {STATE_CLIPPLANE(27), clipplane
}, WINED3D_GL_EXT_NONE
},
11456 {STATE_CLIPPLANE(28), {STATE_CLIPPLANE(28), glsl_vertex_pipe_clip_plane
}, WINED3D_GLSL_130
},
11457 {STATE_CLIPPLANE(28), {STATE_CLIPPLANE(28), clipplane
}, WINED3D_GL_EXT_NONE
},
11458 {STATE_CLIPPLANE(29), {STATE_CLIPPLANE(29), glsl_vertex_pipe_clip_plane
}, WINED3D_GLSL_130
},
11459 {STATE_CLIPPLANE(29), {STATE_CLIPPLANE(29), clipplane
}, WINED3D_GL_EXT_NONE
},
11460 {STATE_CLIPPLANE(30), {STATE_CLIPPLANE(30), glsl_vertex_pipe_clip_plane
}, WINED3D_GLSL_130
},
11461 {STATE_CLIPPLANE(30), {STATE_CLIPPLANE(30), clipplane
}, WINED3D_GL_EXT_NONE
},
11462 {STATE_CLIPPLANE(31), {STATE_CLIPPLANE(31), glsl_vertex_pipe_clip_plane
}, WINED3D_GLSL_130
},
11463 {STATE_CLIPPLANE(31), {STATE_CLIPPLANE(31), clipplane
}, WINED3D_GL_EXT_NONE
},
11465 {STATE_LIGHT_TYPE
, {STATE_RENDER(WINED3D_RS_FOGENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
11466 {STATE_ACTIVELIGHT(0), {STATE_ACTIVELIGHT(0), glsl_vertex_pipe_light
}, WINED3D_GL_EXT_NONE
},
11467 {STATE_ACTIVELIGHT(1), {STATE_ACTIVELIGHT(1), glsl_vertex_pipe_light
}, WINED3D_GL_EXT_NONE
},
11468 {STATE_ACTIVELIGHT(2), {STATE_ACTIVELIGHT(2), glsl_vertex_pipe_light
}, WINED3D_GL_EXT_NONE
},
11469 {STATE_ACTIVELIGHT(3), {STATE_ACTIVELIGHT(3), glsl_vertex_pipe_light
}, WINED3D_GL_EXT_NONE
},
11470 {STATE_ACTIVELIGHT(4), {STATE_ACTIVELIGHT(4), glsl_vertex_pipe_light
}, WINED3D_GL_EXT_NONE
},
11471 {STATE_ACTIVELIGHT(5), {STATE_ACTIVELIGHT(5), glsl_vertex_pipe_light
}, WINED3D_GL_EXT_NONE
},
11472 {STATE_ACTIVELIGHT(6), {STATE_ACTIVELIGHT(6), glsl_vertex_pipe_light
}, WINED3D_GL_EXT_NONE
},
11473 {STATE_ACTIVELIGHT(7), {STATE_ACTIVELIGHT(7), glsl_vertex_pipe_light
}, WINED3D_GL_EXT_NONE
},
11475 {STATE_VIEWPORT
, {STATE_VIEWPORT
, glsl_vertex_pipe_viewport
}, WINED3D_GL_EXT_NONE
},
11476 /* Transform states */
11477 {STATE_TRANSFORM(WINED3D_TS_VIEW
), {STATE_TRANSFORM(WINED3D_TS_VIEW
), glsl_vertex_pipe_view
}, WINED3D_GL_EXT_NONE
},
11478 {STATE_TRANSFORM(WINED3D_TS_PROJECTION
), {STATE_TRANSFORM(WINED3D_TS_PROJECTION
), glsl_vertex_pipe_projection
}, WINED3D_GL_EXT_NONE
},
11479 {STATE_TRANSFORM(WINED3D_TS_TEXTURE0
), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), NULL
}, WINED3D_GL_EXT_NONE
},
11480 {STATE_TRANSFORM(WINED3D_TS_TEXTURE1
), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), NULL
}, WINED3D_GL_EXT_NONE
},
11481 {STATE_TRANSFORM(WINED3D_TS_TEXTURE2
), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), NULL
}, WINED3D_GL_EXT_NONE
},
11482 {STATE_TRANSFORM(WINED3D_TS_TEXTURE3
), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), NULL
}, WINED3D_GL_EXT_NONE
},
11483 {STATE_TRANSFORM(WINED3D_TS_TEXTURE4
), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), NULL
}, WINED3D_GL_EXT_NONE
},
11484 {STATE_TRANSFORM(WINED3D_TS_TEXTURE5
), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), NULL
}, WINED3D_GL_EXT_NONE
},
11485 {STATE_TRANSFORM(WINED3D_TS_TEXTURE6
), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), NULL
}, WINED3D_GL_EXT_NONE
},
11486 {STATE_TRANSFORM(WINED3D_TS_TEXTURE7
), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), NULL
}, WINED3D_GL_EXT_NONE
},
11487 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), glsl_vertex_pipe_world
}, WINED3D_GL_EXT_NONE
},
11488 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), glsl_vertex_pipe_vertexblend
}, WINED3D_GL_EXT_NONE
},
11489 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), glsl_vertex_pipe_vertexblend
}, WINED3D_GL_EXT_NONE
},
11490 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), glsl_vertex_pipe_vertexblend
}, WINED3D_GL_EXT_NONE
},
11491 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), glsl_vertex_pipe_texmatrix
}, WINED3D_GL_EXT_NONE
},
11492 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), glsl_vertex_pipe_texmatrix
}, WINED3D_GL_EXT_NONE
},
11493 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), glsl_vertex_pipe_texmatrix
}, WINED3D_GL_EXT_NONE
},
11494 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), glsl_vertex_pipe_texmatrix
}, WINED3D_GL_EXT_NONE
},
11495 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), glsl_vertex_pipe_texmatrix
}, WINED3D_GL_EXT_NONE
},
11496 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), glsl_vertex_pipe_texmatrix
}, WINED3D_GL_EXT_NONE
},
11497 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), glsl_vertex_pipe_texmatrix
}, WINED3D_GL_EXT_NONE
},
11498 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), glsl_vertex_pipe_texmatrix
}, WINED3D_GL_EXT_NONE
},
11499 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXCOORD_INDEX
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
11500 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXCOORD_INDEX
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
11501 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXCOORD_INDEX
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
11502 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXCOORD_INDEX
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
11503 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXCOORD_INDEX
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
11504 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXCOORD_INDEX
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
11505 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXCOORD_INDEX
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
11506 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXCOORD_INDEX
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
11508 {STATE_RENDER(WINED3D_RS_FOGENABLE
), {STATE_RENDER(WINED3D_RS_FOGENABLE
), glsl_vertex_pipe_shader
}, WINED3D_GL_EXT_NONE
},
11509 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE
), {STATE_RENDER(WINED3D_RS_FOGENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
11510 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE
), {STATE_RENDER(WINED3D_RS_FOGENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
11511 {STATE_RENDER(WINED3D_RS_RANGEFOGENABLE
), {STATE_RENDER(WINED3D_RS_FOGENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
11512 {STATE_RENDER(WINED3D_RS_CLIPPING
), {STATE_RENDER(WINED3D_RS_CLIPPING
), state_clipping
}, WINED3D_GL_EXT_NONE
},
11513 {STATE_RENDER(WINED3D_RS_CLIPPLANEENABLE
), {STATE_RENDER(WINED3D_RS_CLIPPING
), NULL
}, WINED3D_GL_EXT_NONE
},
11514 {STATE_RENDER(WINED3D_RS_LIGHTING
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
11515 {STATE_RENDER(WINED3D_RS_AMBIENT
), {STATE_RENDER(WINED3D_RS_AMBIENT
), glsl_vertex_pipe_light
}, WINED3D_GL_EXT_NONE
},
11516 {STATE_RENDER(WINED3D_RS_COLORVERTEX
), {STATE_RENDER(WINED3D_RS_COLORVERTEX
), glsl_vertex_pipe_shader
}, WINED3D_GL_EXT_NONE
},
11517 {STATE_RENDER(WINED3D_RS_LOCALVIEWER
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
11518 {STATE_RENDER(WINED3D_RS_NORMALIZENORMALS
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
11519 {STATE_RENDER(WINED3D_RS_DIFFUSEMATERIALSOURCE
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
11520 {STATE_RENDER(WINED3D_RS_SPECULARMATERIALSOURCE
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
11521 {STATE_RENDER(WINED3D_RS_AMBIENTMATERIALSOURCE
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
11522 {STATE_RENDER(WINED3D_RS_EMISSIVEMATERIALSOURCE
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
11523 {STATE_RENDER(WINED3D_RS_VERTEXBLEND
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
11524 {STATE_RENDER(WINED3D_RS_POINTSIZE
), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN
), NULL
}, WINED3D_GL_EXT_NONE
},
11525 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN
), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN
), glsl_vertex_pipe_pointsize
}, WINED3D_GL_EXT_NONE
},
11526 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE
), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE
), state_pointsprite
}, ARB_POINT_SPRITE
},
11527 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE
), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE
), state_pointsprite_w
}, WINED3D_GL_LEGACY_CONTEXT
},
11528 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE
), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE
), glsl_vertex_pointsprite_core
}, WINED3D_GL_EXT_NONE
},
11529 {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE
), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE
), glsl_vertex_pipe_pointscale
}, WINED3D_GL_EXT_NONE
},
11530 {STATE_RENDER(WINED3D_RS_POINTSCALE_A
), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
11531 {STATE_RENDER(WINED3D_RS_POINTSCALE_B
), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
11532 {STATE_RENDER(WINED3D_RS_POINTSCALE_C
), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
11533 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX
), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN
), NULL
}, WINED3D_GL_EXT_NONE
},
11534 {STATE_RENDER(WINED3D_RS_TWEENFACTOR
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
11535 {STATE_RENDER(WINED3D_RS_INDEXEDVERTEXBLENDENABLE
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
11536 /* NP2 texture matrix fixups. They are not needed if
11537 * GL_ARB_texture_non_power_of_two is supported. Otherwise, register
11538 * glsl_vertex_pipe_texmatrix(), which takes care of updating the texture
11540 {STATE_SAMPLER(0), {0, NULL
}, ARB_TEXTURE_NON_POWER_OF_TWO
},
11541 {STATE_SAMPLER(0), {0, NULL
}, WINED3D_GL_NORMALIZED_TEXRECT
},
11542 {STATE_SAMPLER(0), {STATE_SAMPLER(0), glsl_vertex_pipe_texmatrix_np2
}, WINED3D_GL_EXT_NONE
},
11543 {STATE_SAMPLER(1), {0, NULL
}, ARB_TEXTURE_NON_POWER_OF_TWO
},
11544 {STATE_SAMPLER(1), {0, NULL
}, WINED3D_GL_NORMALIZED_TEXRECT
},
11545 {STATE_SAMPLER(1), {STATE_SAMPLER(1), glsl_vertex_pipe_texmatrix_np2
}, WINED3D_GL_EXT_NONE
},
11546 {STATE_SAMPLER(2), {0, NULL
}, ARB_TEXTURE_NON_POWER_OF_TWO
},
11547 {STATE_SAMPLER(2), {0, NULL
}, WINED3D_GL_NORMALIZED_TEXRECT
},
11548 {STATE_SAMPLER(2), {STATE_SAMPLER(2), glsl_vertex_pipe_texmatrix_np2
}, WINED3D_GL_EXT_NONE
},
11549 {STATE_SAMPLER(3), {0, NULL
}, ARB_TEXTURE_NON_POWER_OF_TWO
},
11550 {STATE_SAMPLER(3), {0, NULL
}, WINED3D_GL_NORMALIZED_TEXRECT
},
11551 {STATE_SAMPLER(3), {STATE_SAMPLER(3), glsl_vertex_pipe_texmatrix_np2
}, WINED3D_GL_EXT_NONE
},
11552 {STATE_SAMPLER(4), {0, NULL
}, ARB_TEXTURE_NON_POWER_OF_TWO
},
11553 {STATE_SAMPLER(4), {0, NULL
}, WINED3D_GL_NORMALIZED_TEXRECT
},
11554 {STATE_SAMPLER(4), {STATE_SAMPLER(4), glsl_vertex_pipe_texmatrix_np2
}, WINED3D_GL_EXT_NONE
},
11555 {STATE_SAMPLER(5), {0, NULL
}, ARB_TEXTURE_NON_POWER_OF_TWO
},
11556 {STATE_SAMPLER(5), {0, NULL
}, WINED3D_GL_NORMALIZED_TEXRECT
},
11557 {STATE_SAMPLER(5), {STATE_SAMPLER(5), glsl_vertex_pipe_texmatrix_np2
}, WINED3D_GL_EXT_NONE
},
11558 {STATE_SAMPLER(6), {0, NULL
}, ARB_TEXTURE_NON_POWER_OF_TWO
},
11559 {STATE_SAMPLER(6), {0, NULL
}, WINED3D_GL_NORMALIZED_TEXRECT
},
11560 {STATE_SAMPLER(6), {STATE_SAMPLER(6), glsl_vertex_pipe_texmatrix_np2
}, WINED3D_GL_EXT_NONE
},
11561 {STATE_SAMPLER(7), {0, NULL
}, ARB_TEXTURE_NON_POWER_OF_TWO
},
11562 {STATE_SAMPLER(7), {0, NULL
}, WINED3D_GL_NORMALIZED_TEXRECT
},
11563 {STATE_SAMPLER(7), {STATE_SAMPLER(7), glsl_vertex_pipe_texmatrix_np2
}, WINED3D_GL_EXT_NONE
},
11564 {STATE_POINT_ENABLE
, {STATE_POINT_ENABLE
, glsl_vertex_pipe_shader
}, WINED3D_GL_EXT_NONE
},
11565 {STATE_RENDER(WINED3D_RS_SHADEMODE
), {STATE_RENDER(WINED3D_RS_SHADEMODE
), glsl_vertex_pipe_shademode
}, WINED3D_GLSL_130
},
11566 {STATE_RENDER(WINED3D_RS_SHADEMODE
), {STATE_RENDER(WINED3D_RS_SHADEMODE
), glsl_vertex_pipe_nop
}, WINED3D_GL_EXT_NONE
},
11567 {0 /* Terminate */, {0, NULL
}, WINED3D_GL_EXT_NONE
},
11571 * - Implement vertex tweening. */
11572 const struct wined3d_vertex_pipe_ops glsl_vertex_pipe
=
11574 glsl_vertex_pipe_vp_enable
,
11575 glsl_vertex_pipe_vp_get_caps
,
11576 glsl_vertex_pipe_vp_get_emul_mask
,
11577 glsl_vertex_pipe_vp_alloc
,
11578 glsl_vertex_pipe_vp_free
,
11579 glsl_vertex_pipe_vp_states
,
11582 static void glsl_fragment_pipe_enable(const struct wined3d_gl_info
*gl_info
, BOOL enable
)
11584 /* Nothing to do. */
11587 static void glsl_fragment_pipe_get_caps(const struct wined3d_gl_info
*gl_info
, struct fragment_caps
*caps
)
11589 caps
->wined3d_caps
= WINED3D_FRAGMENT_CAP_PROJ_CONTROL
11590 | WINED3D_FRAGMENT_CAP_SRGB_WRITE
11591 | WINED3D_FRAGMENT_CAP_COLOR_KEY
;
11592 caps
->PrimitiveMiscCaps
= WINED3DPMISCCAPS_TSSARGTEMP
11593 | WINED3DPMISCCAPS_PERSTAGECONSTANT
;
11594 caps
->TextureOpCaps
= WINED3DTEXOPCAPS_DISABLE
11595 | WINED3DTEXOPCAPS_SELECTARG1
11596 | WINED3DTEXOPCAPS_SELECTARG2
11597 | WINED3DTEXOPCAPS_MODULATE4X
11598 | WINED3DTEXOPCAPS_MODULATE2X
11599 | WINED3DTEXOPCAPS_MODULATE
11600 | WINED3DTEXOPCAPS_ADDSIGNED2X
11601 | WINED3DTEXOPCAPS_ADDSIGNED
11602 | WINED3DTEXOPCAPS_ADD
11603 | WINED3DTEXOPCAPS_SUBTRACT
11604 | WINED3DTEXOPCAPS_ADDSMOOTH
11605 | WINED3DTEXOPCAPS_BLENDCURRENTALPHA
11606 | WINED3DTEXOPCAPS_BLENDFACTORALPHA
11607 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHA
11608 | WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA
11609 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM
11610 | WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR
11611 | WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA
11612 | WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA
11613 | WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR
11614 | WINED3DTEXOPCAPS_DOTPRODUCT3
11615 | WINED3DTEXOPCAPS_MULTIPLYADD
11616 | WINED3DTEXOPCAPS_LERP
11617 | WINED3DTEXOPCAPS_BUMPENVMAP
11618 | WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE
;
11619 caps
->MaxTextureBlendStages
= MAX_TEXTURES
;
11620 caps
->MaxSimultaneousTextures
= min(gl_info
->limits
.samplers
[WINED3D_SHADER_TYPE_PIXEL
], MAX_TEXTURES
);
11623 static DWORD
glsl_fragment_pipe_get_emul_mask(const struct wined3d_gl_info
*gl_info
)
11625 if (gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
])
11626 return GL_EXT_EMUL_ARB_MULTITEXTURE
;
11630 static void *glsl_fragment_pipe_alloc(const struct wined3d_shader_backend_ops
*shader_backend
, void *shader_priv
)
11632 struct shader_glsl_priv
*priv
;
11634 if (shader_backend
== &glsl_shader_backend
)
11636 priv
= shader_priv
;
11637 wine_rb_init(&priv
->ffp_fragment_shaders
, wined3d_ffp_frag_program_key_compare
);
11641 FIXME("GLSL fragment pipe without GLSL shader backend not implemented.\n");
11646 static void shader_glsl_free_ffp_fragment_shader(struct wine_rb_entry
*entry
, void *context
)
11648 struct glsl_ffp_fragment_shader
*shader
= WINE_RB_ENTRY_VALUE(entry
,
11649 struct glsl_ffp_fragment_shader
, entry
.entry
);
11650 struct glsl_shader_prog_link
*program
, *program2
;
11651 struct glsl_ffp_destroy_ctx
*ctx
= context
;
11653 LIST_FOR_EACH_ENTRY_SAFE(program
, program2
, &shader
->linked_programs
,
11654 struct glsl_shader_prog_link
, ps
.shader_entry
)
11656 delete_glsl_program_entry(ctx
->priv
, ctx
->gl_info
, program
);
11658 ctx
->gl_info
->gl_ops
.ext
.p_glDeleteShader(shader
->id
);
11659 HeapFree(GetProcessHeap(), 0, shader
);
11662 /* Context activation is done by the caller. */
11663 static void glsl_fragment_pipe_free(struct wined3d_device
*device
)
11665 struct shader_glsl_priv
*priv
= device
->fragment_priv
;
11666 struct glsl_ffp_destroy_ctx ctx
;
11669 ctx
.gl_info
= &device
->adapter
->gl_info
;
11670 wine_rb_destroy(&priv
->ffp_fragment_shaders
, shader_glsl_free_ffp_fragment_shader
, &ctx
);
11673 static void glsl_fragment_pipe_shader(struct wined3d_context
*context
,
11674 const struct wined3d_state
*state
, DWORD state_id
)
11676 context
->last_was_pshader
= use_ps(state
);
11678 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_PIXEL
;
11681 static void glsl_fragment_pipe_fogparams(struct wined3d_context
*context
,
11682 const struct wined3d_state
*state
, DWORD state_id
)
11684 context
->constant_update_mask
|= WINED3D_SHADER_CONST_PS_FOG
;
11687 static void glsl_fragment_pipe_fog(struct wined3d_context
*context
,
11688 const struct wined3d_state
*state
, DWORD state_id
)
11690 BOOL use_vshader
= use_vs(state
);
11691 enum fogsource new_source
;
11692 DWORD fogstart
= state
->render_states
[WINED3D_RS_FOGSTART
];
11693 DWORD fogend
= state
->render_states
[WINED3D_RS_FOGEND
];
11695 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_PIXEL
;
11697 if (!state
->render_states
[WINED3D_RS_FOGENABLE
])
11700 if (state
->render_states
[WINED3D_RS_FOGTABLEMODE
] == WINED3D_FOG_NONE
)
11703 new_source
= FOGSOURCE_VS
;
11704 else if (state
->render_states
[WINED3D_RS_FOGVERTEXMODE
] == WINED3D_FOG_NONE
|| context
->stream_info
.position_transformed
)
11705 new_source
= FOGSOURCE_COORD
;
11707 new_source
= FOGSOURCE_FFP
;
11711 new_source
= FOGSOURCE_FFP
;
11714 if (new_source
!= context
->fog_source
|| fogstart
== fogend
)
11716 context
->fog_source
= new_source
;
11717 context
->constant_update_mask
|= WINED3D_SHADER_CONST_PS_FOG
;
11721 static void glsl_fragment_pipe_vdecl(struct wined3d_context
*context
,
11722 const struct wined3d_state
*state
, DWORD state_id
)
11724 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
11725 if (context
->gl_info
->limits
.glsl_varyings
< wined3d_max_compat_varyings(context
->gl_info
))
11726 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_PIXEL
;
11728 if (!isStateDirty(context
, STATE_RENDER(WINED3D_RS_FOGENABLE
)))
11729 glsl_fragment_pipe_fog(context
, state
, state_id
);
11732 static void glsl_fragment_pipe_vs(struct wined3d_context
*context
,
11733 const struct wined3d_state
*state
, DWORD state_id
)
11735 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
11736 if (context
->gl_info
->limits
.glsl_varyings
< wined3d_max_compat_varyings(context
->gl_info
))
11737 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_PIXEL
;
11740 static void glsl_fragment_pipe_tex_transform(struct wined3d_context
*context
,
11741 const struct wined3d_state
*state
, DWORD state_id
)
11743 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_PIXEL
;
11746 static void glsl_fragment_pipe_invalidate_constants(struct wined3d_context
*context
,
11747 const struct wined3d_state
*state
, DWORD state_id
)
11749 context
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_PS
;
11752 static void glsl_fragment_pipe_alpha_test_func(struct wined3d_context
*context
,
11753 const struct wined3d_state
*state
, DWORD state_id
)
11755 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
11756 GLint func
= wined3d_gl_compare_func(state
->render_states
[WINED3D_RS_ALPHAFUNC
]);
11757 float ref
= state
->render_states
[WINED3D_RS_ALPHAREF
] / 255.0f
;
11761 gl_info
->gl_ops
.gl
.p_glAlphaFunc(func
, ref
);
11762 checkGLcall("glAlphaFunc");
11766 static void glsl_fragment_pipe_core_alpha_test(struct wined3d_context
*context
,
11767 const struct wined3d_state
*state
, DWORD state_id
)
11769 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_PIXEL
;
11772 static void glsl_fragment_pipe_alpha_test(struct wined3d_context
*context
,
11773 const struct wined3d_state
*state
, DWORD state_id
)
11775 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
11777 if (state
->render_states
[WINED3D_RS_ALPHATESTENABLE
])
11779 gl_info
->gl_ops
.gl
.p_glEnable(GL_ALPHA_TEST
);
11780 checkGLcall("glEnable(GL_ALPHA_TEST)");
11784 gl_info
->gl_ops
.gl
.p_glDisable(GL_ALPHA_TEST
);
11785 checkGLcall("glDisable(GL_ALPHA_TEST)");
11789 static void glsl_fragment_pipe_core_alpha_test_ref(struct wined3d_context
*context
,
11790 const struct wined3d_state
*state
, DWORD state_id
)
11792 context
->constant_update_mask
|= WINED3D_SHADER_CONST_PS_ALPHA_TEST
;
11795 static void glsl_fragment_pipe_color_key(struct wined3d_context
*context
,
11796 const struct wined3d_state
*state
, DWORD state_id
)
11798 context
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_COLOR_KEY
;
11801 static void glsl_fragment_pipe_shademode(struct wined3d_context
*context
,
11802 const struct wined3d_state
*state
, DWORD state_id
)
11804 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_PIXEL
;
11807 static const struct StateEntryTemplate glsl_fragment_pipe_state_template
[] =
11809 {STATE_VDECL
, {STATE_VDECL
, glsl_fragment_pipe_vdecl
}, WINED3D_GL_EXT_NONE
},
11810 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), glsl_fragment_pipe_vs
}, WINED3D_GL_EXT_NONE
},
11811 {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR
), {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR
), glsl_fragment_pipe_invalidate_constants
}, WINED3D_GL_EXT_NONE
},
11812 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
11813 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
11814 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
11815 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
11816 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
11817 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
11818 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
11819 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
11820 {STATE_TEXTURESTAGE(0, WINED3D_TSS_RESULT_ARG
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
11821 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
11822 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
11823 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
11824 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
11825 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
11826 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
11827 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
11828 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
11829 {STATE_TEXTURESTAGE(1, WINED3D_TSS_RESULT_ARG
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
11830 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
11831 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
11832 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
11833 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
11834 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
11835 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
11836 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
11837 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
11838 {STATE_TEXTURESTAGE(2, WINED3D_TSS_RESULT_ARG
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
11839 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
11840 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
11841 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
11842 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
11843 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
11844 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
11845 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
11846 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
11847 {STATE_TEXTURESTAGE(3, WINED3D_TSS_RESULT_ARG
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
11848 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
11849 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
11850 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
11851 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
11852 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
11853 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
11854 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
11855 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
11856 {STATE_TEXTURESTAGE(4, WINED3D_TSS_RESULT_ARG
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
11857 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
11858 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
11859 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
11860 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
11861 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
11862 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
11863 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
11864 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
11865 {STATE_TEXTURESTAGE(5, WINED3D_TSS_RESULT_ARG
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
11866 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
11867 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
11868 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
11869 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
11870 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
11871 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
11872 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
11873 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
11874 {STATE_TEXTURESTAGE(6, WINED3D_TSS_RESULT_ARG
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
11875 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
11876 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
11877 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
11878 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
11879 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
11880 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
11881 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
11882 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
11883 {STATE_TEXTURESTAGE(7, WINED3D_TSS_RESULT_ARG
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
11884 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), glsl_fragment_pipe_shader
}, WINED3D_GL_EXT_NONE
},
11885 {STATE_RENDER(WINED3D_RS_ALPHAFUNC
), {STATE_RENDER(WINED3D_RS_ALPHAFUNC
), glsl_fragment_pipe_alpha_test_func
}, WINED3D_GL_LEGACY_CONTEXT
},
11886 {STATE_RENDER(WINED3D_RS_ALPHAFUNC
), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
11887 {STATE_RENDER(WINED3D_RS_ALPHAREF
), {STATE_RENDER(WINED3D_RS_ALPHAFUNC
), NULL
}, WINED3D_GL_LEGACY_CONTEXT
},
11888 {STATE_RENDER(WINED3D_RS_ALPHAREF
), {STATE_RENDER(WINED3D_RS_ALPHAREF
), glsl_fragment_pipe_core_alpha_test_ref
}, WINED3D_GL_EXT_NONE
},
11889 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE
), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE
), glsl_fragment_pipe_alpha_test
}, WINED3D_GL_LEGACY_CONTEXT
},
11890 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE
), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE
), glsl_fragment_pipe_core_alpha_test
}, WINED3D_GL_EXT_NONE
},
11891 {STATE_RENDER(WINED3D_RS_COLORKEYENABLE
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
11892 {STATE_COLOR_KEY
, { STATE_COLOR_KEY
, glsl_fragment_pipe_color_key
}, WINED3D_GL_EXT_NONE
},
11893 {STATE_RENDER(WINED3D_RS_FOGENABLE
), {STATE_RENDER(WINED3D_RS_FOGENABLE
), glsl_fragment_pipe_fog
}, WINED3D_GL_EXT_NONE
},
11894 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE
), {STATE_RENDER(WINED3D_RS_FOGENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
11895 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE
), {STATE_RENDER(WINED3D_RS_FOGENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
11896 {STATE_RENDER(WINED3D_RS_FOGSTART
), {STATE_RENDER(WINED3D_RS_FOGSTART
), glsl_fragment_pipe_fogparams
}, WINED3D_GL_EXT_NONE
},
11897 {STATE_RENDER(WINED3D_RS_FOGEND
), {STATE_RENDER(WINED3D_RS_FOGSTART
), NULL
}, WINED3D_GL_EXT_NONE
},
11898 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE
), {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE
), state_srgbwrite
}, ARB_FRAMEBUFFER_SRGB
},
11899 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
11900 {STATE_RENDER(WINED3D_RS_FOGCOLOR
), {STATE_RENDER(WINED3D_RS_FOGCOLOR
), glsl_fragment_pipe_fogparams
}, WINED3D_GL_EXT_NONE
},
11901 {STATE_RENDER(WINED3D_RS_FOGDENSITY
), {STATE_RENDER(WINED3D_RS_FOGDENSITY
), glsl_fragment_pipe_fogparams
}, WINED3D_GL_EXT_NONE
},
11902 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE
), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE
), glsl_fragment_pipe_shader
}, ARB_POINT_SPRITE
},
11903 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE
), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE
), glsl_fragment_pipe_shader
}, WINED3D_GL_VERSION_2_0
},
11904 {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
},
11905 {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
},
11906 {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
},
11907 {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
},
11908 {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
},
11909 {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
},
11910 {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
},
11911 {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
},
11912 {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT
), {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT
), glsl_fragment_pipe_invalidate_constants
}, WINED3D_GL_EXT_NONE
},
11913 {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT
), {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT
), glsl_fragment_pipe_invalidate_constants
}, WINED3D_GL_EXT_NONE
},
11914 {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT
), {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT
), glsl_fragment_pipe_invalidate_constants
}, WINED3D_GL_EXT_NONE
},
11915 {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT
), {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT
), glsl_fragment_pipe_invalidate_constants
}, WINED3D_GL_EXT_NONE
},
11916 {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT
), {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT
), glsl_fragment_pipe_invalidate_constants
}, WINED3D_GL_EXT_NONE
},
11917 {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT
), {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT
), glsl_fragment_pipe_invalidate_constants
}, WINED3D_GL_EXT_NONE
},
11918 {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT
), {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT
), glsl_fragment_pipe_invalidate_constants
}, WINED3D_GL_EXT_NONE
},
11919 {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT
), {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT
), glsl_fragment_pipe_invalidate_constants
}, WINED3D_GL_EXT_NONE
},
11920 {STATE_RENDER(WINED3D_RS_SPECULARENABLE
), {STATE_RENDER(WINED3D_RS_SPECULARENABLE
), glsl_fragment_pipe_invalidate_constants
}, WINED3D_GL_EXT_NONE
},
11921 {STATE_POINT_ENABLE
, {STATE_POINT_ENABLE
, glsl_fragment_pipe_shader
}, WINED3D_GL_EXT_NONE
},
11922 {STATE_RENDER(WINED3D_RS_SHADEMODE
), {STATE_RENDER(WINED3D_RS_SHADEMODE
), glsl_fragment_pipe_shademode
}, WINED3D_GLSL_130
},
11923 {STATE_RENDER(WINED3D_RS_SHADEMODE
), {STATE_RENDER(WINED3D_RS_SHADEMODE
), state_shademode
}, WINED3D_GL_EXT_NONE
},
11924 {0 /* Terminate */, {0, 0 }, WINED3D_GL_EXT_NONE
},
11927 static BOOL
glsl_fragment_pipe_alloc_context_data(struct wined3d_context
*context
)
11932 static void glsl_fragment_pipe_free_context_data(struct wined3d_context
*context
)
11936 const struct fragment_pipeline glsl_fragment_pipe
=
11938 glsl_fragment_pipe_enable
,
11939 glsl_fragment_pipe_get_caps
,
11940 glsl_fragment_pipe_get_emul_mask
,
11941 glsl_fragment_pipe_alloc
,
11942 glsl_fragment_pipe_free
,
11943 glsl_fragment_pipe_alloc_context_data
,
11944 glsl_fragment_pipe_free_context_data
,
11945 shader_glsl_color_fixup_supported
,
11946 glsl_fragment_pipe_state_template
,