1 /**************************************************************************
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
29 * State validation for vertex/fragment shaders.
30 * Note that we have to delay most vertex/fragment shader translation
31 * until rendering time since the linkage between the vertex outputs and
32 * fragment inputs can vary depending on the pairing of shaders.
38 #include "main/imports.h"
39 #include "main/mtypes.h"
40 #include "main/macros.h"
41 #include "shader/program.h"
43 #include "pipe/p_context.h"
44 #include "pipe/p_shader_tokens.h"
46 #include "util/u_simple_shaders.h"
48 #include "cso_cache/cso_context.h"
50 #include "st_context.h"
52 #include "st_program.h"
53 #include "st_atom_shader.h"
54 #include "st_mesa_to_tgsi.h"
59 * Translate fragment program if needed.
62 translate_fp(struct st_context
*st
,
63 struct st_fragment_program
*stfp
)
65 const GLbitfield fragInputsRead
= stfp
->Base
.Base
.InputsRead
;
67 if (!stfp
->state
.tokens
) {
68 GLuint inAttr
, numIn
= 0;
70 for (inAttr
= 0; inAttr
< FRAG_ATTRIB_MAX
; inAttr
++) {
71 if (fragInputsRead
& (1 << inAttr
)) {
72 stfp
->input_to_slot
[inAttr
] = numIn
;
76 stfp
->input_to_slot
[inAttr
] = -1;
80 stfp
->num_input_slots
= numIn
;
82 assert(stfp
->Base
.Base
.NumInstructions
> 1);
84 st_translate_fragment_program(st
, stfp
, stfp
->input_to_slot
);
91 * Find a translated vertex program that corresponds to stvp and
92 * has outputs matched to stfp's inputs.
93 * This performs vertex and fragment translation (to TGSI) when needed.
95 static struct st_vp_varient
*
96 find_translated_vp(struct st_context
*st
,
97 struct st_vertex_program
*stvp
)
99 struct st_vp_varient
*vpv
;
100 struct st_vp_varient_key key
;
102 /* Nothing in our key yet. This will change:
104 memset(&key
, 0, sizeof key
);
106 /* When this is true, we will add an extra input to the vertex
107 * shader translation (for edgeflags), an extra output with
108 * edgeflag semantics, and extend the vertex shader to pass through
109 * the input to the output. We'll need to use similar logic to set
110 * up the extra vertex_element input for edgeflags.
111 * _NEW_POLYGON, ST_NEW_EDGEFLAGS_DATA
113 key
.passthrough_edgeflags
= (st
->vertdata_edgeflags
&& (
114 st
->ctx
->Polygon
.FrontMode
!= GL_FILL
||
115 st
->ctx
->Polygon
.BackMode
!= GL_FILL
));
118 /* Do we need to throw away old translations after a change in the
121 if (stvp
->serialNo
!= stvp
->lastSerialNo
) {
122 /* These may have changed if the program string changed.
124 st_prepare_vertex_program( st
, stvp
);
126 /* We are now up-to-date:
128 stvp
->lastSerialNo
= stvp
->serialNo
;
131 /* See if we've got a translated vertex program whose outputs match
132 * the fragment program's inputs.
134 for (vpv
= stvp
->varients
; vpv
; vpv
= vpv
->next
) {
135 if (memcmp(&vpv
->key
, &key
, sizeof key
) == 0) {
140 /* No? Perform new translation here. */
142 vpv
= st_translate_vertex_program(st
, stvp
, &key
);
146 vpv
->next
= stvp
->varients
;
147 stvp
->varients
= vpv
;
155 * Return pointer to a pass-through fragment shader.
156 * This shader is used when a texture is missing/incomplete.
159 get_passthrough_fs(struct st_context
*st
)
161 if (!st
->passthrough_fs
) {
163 util_make_fragment_passthrough_shader(st
->pipe
);
166 return st
->passthrough_fs
;
171 * Update fragment program state/atom. This involves translating the
172 * Mesa fragment program into a gallium fragment program and binding it.
175 update_fp( struct st_context
*st
)
177 struct st_fragment_program
*stfp
;
179 assert(st
->ctx
->FragmentProgram
._Current
);
180 stfp
= st_fragment_program(st
->ctx
->FragmentProgram
._Current
);
181 assert(stfp
->Base
.Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
);
183 translate_fp(st
, stfp
);
185 st_reference_fragprog(st
, &st
->fp
, stfp
);
187 if (st
->missing_textures
) {
188 /* use a pass-through frag shader that uses no textures */
189 void *fs
= get_passthrough_fs(st
);
190 cso_set_fragment_shader_handle(st
->cso_context
, fs
);
193 cso_set_fragment_shader_handle(st
->cso_context
, stfp
->driver_shader
);
198 const struct st_tracked_state st_update_fp
= {
199 "st_update_fp", /* name */
202 ST_NEW_FRAGMENT_PROGRAM
/* st */
204 update_fp
/* update */
210 * Update vertex program state/atom. This involves translating the
211 * Mesa vertex program into a gallium fragment program and binding it.
214 update_vp( struct st_context
*st
)
216 struct st_vertex_program
*stvp
;
218 /* find active shader and params -- Should be covered by
219 * ST_NEW_VERTEX_PROGRAM
221 assert(st
->ctx
->VertexProgram
._Current
);
222 stvp
= st_vertex_program(st
->ctx
->VertexProgram
._Current
);
223 assert(stvp
->Base
.Base
.Target
== GL_VERTEX_PROGRAM_ARB
);
225 st
->vp_varient
= find_translated_vp(st
, stvp
);
227 st_reference_vertprog(st
, &st
->vp
, stvp
);
229 cso_set_vertex_shader_handle(st
->cso_context
,
230 st
->vp_varient
->driver_shader
);
232 st
->vertex_result_to_slot
= stvp
->result_to_output
;
236 const struct st_tracked_state st_update_vp
= {
237 "st_update_vp", /* name */
239 _NEW_POLYGON
, /* mesa */
240 ST_NEW_VERTEX_PROGRAM
| ST_NEW_EDGEFLAGS_DATA
/* st */
242 update_vp
/* update */