1 /**************************************************************************
3 * Copyright 2003 VMware, Inc.
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 VMWARE 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 **************************************************************************/
30 * Keith Whitwell <keithw@vmware.com>
33 #include "main/glheader.h"
34 #include "main/macros.h"
35 #include "main/enums.h"
36 #include "main/shaderapi.h"
37 #include "program/prog_instruction.h"
38 #include "program/program.h"
40 #include "cso_cache/cso_context.h"
41 #include "draw/draw_context.h"
43 #include "st_context.h"
45 #include "st_program.h"
46 #include "st_mesa_to_tgsi.h"
47 #include "st_cb_program.h"
48 #include "st_glsl_to_tgsi.h"
49 #include "st_atifs_to_tgsi.h"
54 * Called via ctx->Driver.BindProgram() to bind an ARB vertex or
58 st_bind_program(struct gl_context
*ctx
, GLenum target
, struct gl_program
*prog
)
60 struct st_context
*st
= st_context(ctx
);
63 case GL_VERTEX_PROGRAM_ARB
:
64 st
->dirty
.st
|= ST_NEW_VERTEX_PROGRAM
;
66 case GL_FRAGMENT_PROGRAM_ARB
:
67 st
->dirty
.st
|= ST_NEW_FRAGMENT_PROGRAM
;
69 case GL_GEOMETRY_PROGRAM_NV
:
70 st
->dirty
.st
|= ST_NEW_GEOMETRY_PROGRAM
;
72 case GL_TESS_CONTROL_PROGRAM_NV
:
73 st
->dirty
.st
|= ST_NEW_TESSCTRL_PROGRAM
;
75 case GL_TESS_EVALUATION_PROGRAM_NV
:
76 st
->dirty
.st
|= ST_NEW_TESSEVAL_PROGRAM
;
78 case GL_COMPUTE_PROGRAM_NV
:
79 st
->dirty_cp
.st
|= ST_NEW_COMPUTE_PROGRAM
;
86 * Called via ctx->Driver.UseProgram() to bind a linked GLSL program
87 * (vertex shader + fragment shader).
90 st_use_program(struct gl_context
*ctx
, struct gl_shader_program
*shProg
)
92 struct st_context
*st
= st_context(ctx
);
94 st
->dirty
.st
|= ST_NEW_FRAGMENT_PROGRAM
;
95 st
->dirty
.st
|= ST_NEW_VERTEX_PROGRAM
;
96 st
->dirty
.st
|= ST_NEW_GEOMETRY_PROGRAM
;
97 st
->dirty
.st
|= ST_NEW_TESSCTRL_PROGRAM
;
98 st
->dirty
.st
|= ST_NEW_TESSEVAL_PROGRAM
;
99 st
->dirty_cp
.st
|= ST_NEW_COMPUTE_PROGRAM
;
104 * Called via ctx->Driver.NewProgram() to allocate a new vertex or
107 static struct gl_program
*
108 st_new_program(struct gl_context
*ctx
, GLenum target
, GLuint id
)
111 case GL_VERTEX_PROGRAM_ARB
: {
112 struct st_vertex_program
*prog
= ST_CALLOC_STRUCT(st_vertex_program
);
113 return _mesa_init_gl_program(&prog
->Base
.Base
, target
, id
);
115 case GL_FRAGMENT_PROGRAM_ARB
: {
116 struct st_fragment_program
*prog
= ST_CALLOC_STRUCT(st_fragment_program
);
117 return _mesa_init_gl_program(&prog
->Base
.Base
, target
, id
);
119 case GL_GEOMETRY_PROGRAM_NV
: {
120 struct st_geometry_program
*prog
= ST_CALLOC_STRUCT(st_geometry_program
);
121 return _mesa_init_gl_program(&prog
->Base
.Base
, target
, id
);
123 case GL_TESS_CONTROL_PROGRAM_NV
: {
124 struct st_tessctrl_program
*prog
= ST_CALLOC_STRUCT(st_tessctrl_program
);
125 return _mesa_init_gl_program(&prog
->Base
.Base
, target
, id
);
127 case GL_TESS_EVALUATION_PROGRAM_NV
: {
128 struct st_tesseval_program
*prog
= ST_CALLOC_STRUCT(st_tesseval_program
);
129 return _mesa_init_gl_program(&prog
->Base
.Base
, target
, id
);
131 case GL_COMPUTE_PROGRAM_NV
: {
132 struct st_compute_program
*prog
= ST_CALLOC_STRUCT(st_compute_program
);
133 return _mesa_init_gl_program(&prog
->Base
.Base
, target
, id
);
143 * Called via ctx->Driver.DeleteProgram()
146 st_delete_program(struct gl_context
*ctx
, struct gl_program
*prog
)
148 struct st_context
*st
= st_context(ctx
);
150 switch( prog
->Target
) {
151 case GL_VERTEX_PROGRAM_ARB
:
153 struct st_vertex_program
*stvp
= (struct st_vertex_program
*) prog
;
154 st_release_vp_variants( st
, stvp
);
156 if (stvp
->glsl_to_tgsi
)
157 free_glsl_to_tgsi_visitor(stvp
->glsl_to_tgsi
);
160 case GL_GEOMETRY_PROGRAM_NV
:
162 struct st_geometry_program
*stgp
=
163 (struct st_geometry_program
*) prog
;
165 st_release_basic_variants(st
, stgp
->Base
.Base
.Target
,
166 &stgp
->variants
, &stgp
->tgsi
);
168 if (stgp
->glsl_to_tgsi
)
169 free_glsl_to_tgsi_visitor(stgp
->glsl_to_tgsi
);
172 case GL_FRAGMENT_PROGRAM_ARB
:
174 struct st_fragment_program
*stfp
=
175 (struct st_fragment_program
*) prog
;
177 st_release_fp_variants(st
, stfp
);
179 if (stfp
->glsl_to_tgsi
)
180 free_glsl_to_tgsi_visitor(stfp
->glsl_to_tgsi
);
183 case GL_TESS_CONTROL_PROGRAM_NV
:
185 struct st_tessctrl_program
*sttcp
=
186 (struct st_tessctrl_program
*) prog
;
188 st_release_basic_variants(st
, sttcp
->Base
.Base
.Target
,
189 &sttcp
->variants
, &sttcp
->tgsi
);
191 if (sttcp
->glsl_to_tgsi
)
192 free_glsl_to_tgsi_visitor(sttcp
->glsl_to_tgsi
);
195 case GL_TESS_EVALUATION_PROGRAM_NV
:
197 struct st_tesseval_program
*sttep
=
198 (struct st_tesseval_program
*) prog
;
200 st_release_basic_variants(st
, sttep
->Base
.Base
.Target
,
201 &sttep
->variants
, &sttep
->tgsi
);
203 if (sttep
->glsl_to_tgsi
)
204 free_glsl_to_tgsi_visitor(sttep
->glsl_to_tgsi
);
207 case GL_COMPUTE_PROGRAM_NV
:
209 struct st_compute_program
*stcp
=
210 (struct st_compute_program
*) prog
;
212 st_release_cp_variants(st
, stcp
);
214 if (stcp
->glsl_to_tgsi
)
215 free_glsl_to_tgsi_visitor(stcp
->glsl_to_tgsi
);
219 assert(0); /* problem */
222 /* delete base class */
223 _mesa_delete_program( ctx
, prog
);
228 * Called via ctx->Driver.ProgramStringNotify()
229 * Called when the program's text/code is changed. We have to free
230 * all shader variants and corresponding gallium shaders when this happens.
233 st_program_string_notify( struct gl_context
*ctx
,
235 struct gl_program
*prog
)
237 struct st_context
*st
= st_context(ctx
);
238 gl_shader_stage stage
= _mesa_program_enum_to_shader_stage(target
);
240 if (target
== GL_FRAGMENT_PROGRAM_ARB
) {
241 struct st_fragment_program
*stfp
= (struct st_fragment_program
*) prog
;
243 st_release_fp_variants(st
, stfp
);
244 if (!st_translate_fragment_program(st
, stfp
))
248 st
->dirty
.st
|= ST_NEW_FRAGMENT_PROGRAM
;
250 else if (target
== GL_GEOMETRY_PROGRAM_NV
) {
251 struct st_geometry_program
*stgp
= (struct st_geometry_program
*) prog
;
253 st_release_basic_variants(st
, stgp
->Base
.Base
.Target
,
254 &stgp
->variants
, &stgp
->tgsi
);
255 if (!st_translate_geometry_program(st
, stgp
))
259 st
->dirty
.st
|= ST_NEW_GEOMETRY_PROGRAM
;
261 else if (target
== GL_VERTEX_PROGRAM_ARB
) {
262 struct st_vertex_program
*stvp
= (struct st_vertex_program
*) prog
;
264 st_release_vp_variants(st
, stvp
);
265 if (!st_translate_vertex_program(st
, stvp
))
269 st
->dirty
.st
|= ST_NEW_VERTEX_PROGRAM
;
271 else if (target
== GL_TESS_CONTROL_PROGRAM_NV
) {
272 struct st_tessctrl_program
*sttcp
=
273 (struct st_tessctrl_program
*) prog
;
275 st_release_basic_variants(st
, sttcp
->Base
.Base
.Target
,
276 &sttcp
->variants
, &sttcp
->tgsi
);
277 if (!st_translate_tessctrl_program(st
, sttcp
))
280 if (st
->tcp
== sttcp
)
281 st
->dirty
.st
|= ST_NEW_TESSCTRL_PROGRAM
;
283 else if (target
== GL_TESS_EVALUATION_PROGRAM_NV
) {
284 struct st_tesseval_program
*sttep
=
285 (struct st_tesseval_program
*) prog
;
287 st_release_basic_variants(st
, sttep
->Base
.Base
.Target
,
288 &sttep
->variants
, &sttep
->tgsi
);
289 if (!st_translate_tesseval_program(st
, sttep
))
292 if (st
->tep
== sttep
)
293 st
->dirty
.st
|= ST_NEW_TESSEVAL_PROGRAM
;
295 else if (target
== GL_COMPUTE_PROGRAM_NV
) {
296 struct st_compute_program
*stcp
=
297 (struct st_compute_program
*) prog
;
299 st_release_cp_variants(st
, stcp
);
300 if (!st_translate_compute_program(st
, stcp
))
304 st
->dirty_cp
.st
|= ST_NEW_COMPUTE_PROGRAM
;
306 else if (target
== GL_FRAGMENT_SHADER_ATI
) {
309 struct st_fragment_program
*stfp
= (struct st_fragment_program
*) prog
;
310 assert(stfp
->ati_fs
);
311 assert(stfp
->ati_fs
->Program
== prog
);
313 st_init_atifs_prog(ctx
, prog
);
315 st_release_fp_variants(st
, stfp
);
316 if (!st_translate_fragment_program(st
, stfp
))
320 st
->dirty
.st
|= ST_NEW_FRAGMENT_PROGRAM
;
323 if (ST_DEBUG
& DEBUG_PRECOMPILE
||
324 st
->shader_has_one_variant
[stage
])
325 st_precompile_shader_variant(st
, prog
);
331 * Called via ctx->Driver.NewATIfs()
332 * Called in glEndFragmentShaderATI()
334 static struct gl_program
*
335 st_new_ati_fs(struct gl_context
*ctx
, struct ati_fragment_shader
*curProg
)
337 struct gl_program
*prog
= ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
,
339 struct st_fragment_program
*stfp
= (struct st_fragment_program
*)prog
;
340 stfp
->ati_fs
= curProg
;
345 * Plug in the program and shader-related device driver functions.
348 st_init_program_functions(struct dd_function_table
*functions
)
350 functions
->BindProgram
= st_bind_program
;
351 functions
->UseProgram
= st_use_program
;
352 functions
->NewProgram
= st_new_program
;
353 functions
->DeleteProgram
= st_delete_program
;
354 functions
->ProgramStringNotify
= st_program_string_notify
;
355 functions
->NewATIfs
= st_new_ati_fs
;
357 functions
->LinkShader
= st_link_shader
;