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"
53 * Called via ctx->Driver.NewProgram() to allocate a new vertex or
56 static struct gl_program
*
57 st_new_program(struct gl_context
*ctx
, GLenum target
, GLuint id
)
60 case GL_VERTEX_PROGRAM_ARB
: {
61 struct st_vertex_program
*prog
= ST_CALLOC_STRUCT(st_vertex_program
);
62 return _mesa_init_gl_program(&prog
->Base
, target
, id
);
64 case GL_FRAGMENT_PROGRAM_ARB
: {
65 struct st_fragment_program
*prog
= ST_CALLOC_STRUCT(st_fragment_program
);
66 return _mesa_init_gl_program(&prog
->Base
, target
, id
);
68 case GL_GEOMETRY_PROGRAM_NV
: {
69 struct st_geometry_program
*prog
= ST_CALLOC_STRUCT(st_geometry_program
);
70 return _mesa_init_gl_program(&prog
->Base
, target
, id
);
72 case GL_TESS_CONTROL_PROGRAM_NV
: {
73 struct st_tessctrl_program
*prog
= ST_CALLOC_STRUCT(st_tessctrl_program
);
74 return _mesa_init_gl_program(&prog
->Base
, target
, id
);
76 case GL_TESS_EVALUATION_PROGRAM_NV
: {
77 struct st_tesseval_program
*prog
= ST_CALLOC_STRUCT(st_tesseval_program
);
78 return _mesa_init_gl_program(&prog
->Base
, target
, id
);
80 case GL_COMPUTE_PROGRAM_NV
: {
81 struct st_compute_program
*prog
= ST_CALLOC_STRUCT(st_compute_program
);
82 return _mesa_init_gl_program(&prog
->Base
, target
, id
);
92 * Called via ctx->Driver.DeleteProgram()
95 st_delete_program(struct gl_context
*ctx
, struct gl_program
*prog
)
97 struct st_context
*st
= st_context(ctx
);
99 switch( prog
->Target
) {
100 case GL_VERTEX_PROGRAM_ARB
:
102 struct st_vertex_program
*stvp
= (struct st_vertex_program
*) prog
;
103 st_release_vp_variants( st
, stvp
);
105 if (stvp
->glsl_to_tgsi
)
106 free_glsl_to_tgsi_visitor(stvp
->glsl_to_tgsi
);
109 case GL_GEOMETRY_PROGRAM_NV
:
111 struct st_geometry_program
*stgp
=
112 (struct st_geometry_program
*) prog
;
114 st_release_basic_variants(st
, stgp
->Base
.Target
, &stgp
->variants
,
117 if (stgp
->glsl_to_tgsi
)
118 free_glsl_to_tgsi_visitor(stgp
->glsl_to_tgsi
);
121 case GL_FRAGMENT_PROGRAM_ARB
:
123 struct st_fragment_program
*stfp
=
124 (struct st_fragment_program
*) prog
;
126 st_release_fp_variants(st
, stfp
);
128 if (stfp
->glsl_to_tgsi
)
129 free_glsl_to_tgsi_visitor(stfp
->glsl_to_tgsi
);
132 case GL_TESS_CONTROL_PROGRAM_NV
:
134 struct st_tessctrl_program
*sttcp
=
135 (struct st_tessctrl_program
*) prog
;
137 st_release_basic_variants(st
, sttcp
->Base
.Target
, &sttcp
->variants
,
140 if (sttcp
->glsl_to_tgsi
)
141 free_glsl_to_tgsi_visitor(sttcp
->glsl_to_tgsi
);
144 case GL_TESS_EVALUATION_PROGRAM_NV
:
146 struct st_tesseval_program
*sttep
=
147 (struct st_tesseval_program
*) prog
;
149 st_release_basic_variants(st
, sttep
->Base
.Target
,
150 &sttep
->variants
, &sttep
->tgsi
);
152 if (sttep
->glsl_to_tgsi
)
153 free_glsl_to_tgsi_visitor(sttep
->glsl_to_tgsi
);
156 case GL_COMPUTE_PROGRAM_NV
:
158 struct st_compute_program
*stcp
=
159 (struct st_compute_program
*) prog
;
161 st_release_cp_variants(st
, stcp
);
163 if (stcp
->glsl_to_tgsi
)
164 free_glsl_to_tgsi_visitor(stcp
->glsl_to_tgsi
);
168 assert(0); /* problem */
171 /* delete base class */
172 _mesa_delete_program( ctx
, prog
);
177 * Called via ctx->Driver.ProgramStringNotify()
178 * Called when the program's text/code is changed. We have to free
179 * all shader variants and corresponding gallium shaders when this happens.
182 st_program_string_notify( struct gl_context
*ctx
,
184 struct gl_program
*prog
)
186 struct st_context
*st
= st_context(ctx
);
187 gl_shader_stage stage
= _mesa_program_enum_to_shader_stage(target
);
189 if (target
== GL_FRAGMENT_PROGRAM_ARB
) {
190 struct st_fragment_program
*stfp
= (struct st_fragment_program
*) prog
;
192 st_release_fp_variants(st
, stfp
);
193 if (!st_translate_fragment_program(st
, stfp
))
197 st
->dirty
|= stfp
->affected_states
;
199 else if (target
== GL_GEOMETRY_PROGRAM_NV
) {
200 struct st_geometry_program
*stgp
= (struct st_geometry_program
*) prog
;
202 st_release_basic_variants(st
, stgp
->Base
.Target
, &stgp
->variants
,
204 if (!st_translate_geometry_program(st
, stgp
))
208 st
->dirty
|= stgp
->affected_states
;
210 else if (target
== GL_VERTEX_PROGRAM_ARB
) {
211 struct st_vertex_program
*stvp
= (struct st_vertex_program
*) prog
;
213 st_release_vp_variants(st
, stvp
);
214 if (!st_translate_vertex_program(st
, stvp
))
218 st
->dirty
|= ST_NEW_VERTEX_PROGRAM(st
, stvp
);
220 else if (target
== GL_TESS_CONTROL_PROGRAM_NV
) {
221 struct st_tessctrl_program
*sttcp
=
222 (struct st_tessctrl_program
*) prog
;
224 st_release_basic_variants(st
, sttcp
->Base
.Target
, &sttcp
->variants
,
226 if (!st_translate_tessctrl_program(st
, sttcp
))
229 if (st
->tcp
== sttcp
)
230 st
->dirty
|= sttcp
->affected_states
;
232 else if (target
== GL_TESS_EVALUATION_PROGRAM_NV
) {
233 struct st_tesseval_program
*sttep
=
234 (struct st_tesseval_program
*) prog
;
236 st_release_basic_variants(st
, sttep
->Base
.Target
, &sttep
->variants
,
238 if (!st_translate_tesseval_program(st
, sttep
))
241 if (st
->tep
== sttep
)
242 st
->dirty
|= sttep
->affected_states
;
244 else if (target
== GL_COMPUTE_PROGRAM_NV
) {
245 struct st_compute_program
*stcp
=
246 (struct st_compute_program
*) prog
;
248 st_release_cp_variants(st
, stcp
);
249 if (!st_translate_compute_program(st
, stcp
))
253 st
->dirty
|= stcp
->affected_states
;
255 else if (target
== GL_FRAGMENT_SHADER_ATI
) {
258 struct st_fragment_program
*stfp
= (struct st_fragment_program
*) prog
;
259 assert(stfp
->ati_fs
);
260 assert(stfp
->ati_fs
->Program
== prog
);
262 st_init_atifs_prog(ctx
, prog
);
264 st_release_fp_variants(st
, stfp
);
265 if (!st_translate_fragment_program(st
, stfp
))
269 st
->dirty
|= stfp
->affected_states
;
272 if (ST_DEBUG
& DEBUG_PRECOMPILE
||
273 st
->shader_has_one_variant
[stage
])
274 st_precompile_shader_variant(st
, prog
);
280 * Called via ctx->Driver.NewATIfs()
281 * Called in glEndFragmentShaderATI()
283 static struct gl_program
*
284 st_new_ati_fs(struct gl_context
*ctx
, struct ati_fragment_shader
*curProg
)
286 struct gl_program
*prog
= ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
,
288 struct st_fragment_program
*stfp
= (struct st_fragment_program
*)prog
;
289 stfp
->ati_fs
= curProg
;
294 * Plug in the program and shader-related device driver functions.
297 st_init_program_functions(struct dd_function_table
*functions
)
299 functions
->NewProgram
= st_new_program
;
300 functions
->DeleteProgram
= st_delete_program
;
301 functions
->ProgramStringNotify
= st_program_string_notify
;
302 functions
->NewATIfs
= st_new_ati_fs
;
304 functions
->LinkShader
= st_link_shader
;