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.
40 #include "main/imports.h"
41 #include "main/mtypes.h"
42 #include "main/macros.h"
43 #include "shader/program.h"
45 #include "pipe/p_context.h"
46 #include "pipe/p_shader_tokens.h"
48 #include "util/u_simple_shaders.h"
50 #include "cso_cache/cso_context.h"
52 #include "st_context.h"
54 #include "st_program.h"
55 #include "st_atom_shader.h"
56 #include "st_mesa_to_tgsi.h"
63 * Translate fragment program if needed.
66 translate_fp(struct st_context
*st
,
67 struct st_fragment_program
*stfp
)
69 const GLbitfield fragInputsRead
= stfp
->Base
.Base
.InputsRead
;
71 if (!stfp
->state
.tokens
) {
72 GLuint inAttr
, numIn
= 0;
74 for (inAttr
= 0; inAttr
< FRAG_ATTRIB_MAX
; inAttr
++) {
75 if (fragInputsRead
& (1 << inAttr
)) {
76 stfp
->input_to_slot
[inAttr
] = numIn
;
80 stfp
->input_to_slot
[inAttr
] = -1;
84 stfp
->num_input_slots
= numIn
;
86 assert(stfp
->Base
.Base
.NumInstructions
> 1);
88 st_translate_fragment_program(st
, stfp
, stfp
->input_to_slot
);
95 * Find a translated vertex program that corresponds to stvp and
96 * has outputs matched to stfp's inputs.
97 * This performs vertex and fragment translation (to TGSI) when needed.
99 static struct st_vp_varient
*
100 find_translated_vp(struct st_context
*st
,
101 struct st_vertex_program
*stvp
)
103 struct st_vp_varient
*vpv
;
104 struct st_vp_varient_key key
;
106 /* Nothing in our key yet. This will change:
108 memset(&key
, 0, sizeof key
);
111 /* Do we need to throw away old translations after a change in the
114 if (stvp
->serialNo
!= stvp
->lastSerialNo
) {
115 /* These may have changed if the program string changed.
117 st_prepare_vertex_program( st
, stvp
);
119 /* We are now up-to-date:
121 stvp
->lastSerialNo
= stvp
->serialNo
;
124 /* See if we've got a translated vertex program whose outputs match
125 * the fragment program's inputs.
127 for (vpv
= stvp
->varients
; vpv
; vpv
= vpv
->next
) {
128 if (memcmp(&vpv
->key
, &key
, sizeof key
) == 0) {
133 /* No? Perform new translation here. */
135 vpv
= st_translate_vertex_program(st
, stvp
, &key
);
139 vpv
->next
= stvp
->varients
;
140 stvp
->varients
= vpv
;
150 get_passthrough_fs(struct st_context
*st
)
152 if (!st
->passthrough_fs
) {
154 util_make_fragment_passthrough_shader(st
->pipe
);
157 return st
->passthrough_fs
;
161 update_fp( struct st_context
*st
)
163 struct st_fragment_program
*stfp
;
165 assert(st
->ctx
->FragmentProgram
._Current
);
166 stfp
= st_fragment_program(st
->ctx
->FragmentProgram
._Current
);
167 assert(stfp
->Base
.Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
);
169 translate_fp(st
, stfp
);
171 st_reference_fragprog(st
, &st
->fp
, stfp
);
173 if (st
->missing_textures
) {
174 /* use a pass-through frag shader that uses no textures */
175 void *fs
= get_passthrough_fs(st
);
176 cso_set_fragment_shader_handle(st
->cso_context
, fs
);
179 cso_set_fragment_shader_handle(st
->cso_context
, stfp
->driver_shader
);
183 const struct st_tracked_state st_update_fp
= {
184 "st_update_fp", /* name */
187 ST_NEW_FRAGMENT_PROGRAM
/* st */
189 update_fp
/* update */
196 update_vp( struct st_context
*st
)
198 struct st_vertex_program
*stvp
;
200 /* find active shader and params -- Should be covered by
201 * ST_NEW_VERTEX_PROGRAM
203 assert(st
->ctx
->VertexProgram
._Current
);
204 stvp
= st_vertex_program(st
->ctx
->VertexProgram
._Current
);
205 assert(stvp
->Base
.Base
.Target
== GL_VERTEX_PROGRAM_ARB
);
207 st
->vp_varient
= find_translated_vp(st
, stvp
);
209 st_reference_vertprog(st
, &st
->vp
, stvp
);
211 cso_set_vertex_shader_handle(st
->cso_context
,
212 st
->vp_varient
->driver_shader
);
214 st
->vertex_result_to_slot
= stvp
->result_to_output
;
218 const struct st_tracked_state st_update_vp
= {
219 "st_update_vp", /* name */
222 ST_NEW_VERTEX_PROGRAM
/* st */
224 update_vp
/* update */