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"
53 * Called via ctx->Driver.BindProgram() to bind an ARB vertex or
57 st_bind_program(struct gl_context
*ctx
, GLenum target
, struct gl_program
*prog
)
59 struct st_context
*st
= st_context(ctx
);
62 case GL_VERTEX_PROGRAM_ARB
:
63 st
->dirty
.st
|= ST_NEW_VERTEX_PROGRAM
;
65 case GL_FRAGMENT_PROGRAM_ARB
:
66 st
->dirty
.st
|= ST_NEW_FRAGMENT_PROGRAM
;
68 case GL_GEOMETRY_PROGRAM_NV
:
69 st
->dirty
.st
|= ST_NEW_GEOMETRY_PROGRAM
;
71 case GL_TESS_CONTROL_PROGRAM_NV
:
72 st
->dirty
.st
|= ST_NEW_TESSCTRL_PROGRAM
;
74 case GL_TESS_EVALUATION_PROGRAM_NV
:
75 st
->dirty
.st
|= ST_NEW_TESSEVAL_PROGRAM
;
82 * Called via ctx->Driver.UseProgram() to bind a linked GLSL program
83 * (vertex shader + fragment shader).
86 st_use_program(struct gl_context
*ctx
, struct gl_shader_program
*shProg
)
88 struct st_context
*st
= st_context(ctx
);
90 st
->dirty
.st
|= ST_NEW_FRAGMENT_PROGRAM
;
91 st
->dirty
.st
|= ST_NEW_VERTEX_PROGRAM
;
92 st
->dirty
.st
|= ST_NEW_GEOMETRY_PROGRAM
;
93 st
->dirty
.st
|= ST_NEW_TESSCTRL_PROGRAM
;
94 st
->dirty
.st
|= ST_NEW_TESSEVAL_PROGRAM
;
99 * Called via ctx->Driver.NewProgram() to allocate a new vertex or
102 static struct gl_program
*
103 st_new_program(struct gl_context
*ctx
, GLenum target
, GLuint id
)
106 case GL_VERTEX_PROGRAM_ARB
: {
107 struct st_vertex_program
*prog
= ST_CALLOC_STRUCT(st_vertex_program
);
108 return _mesa_init_gl_program(&prog
->Base
.Base
, target
, id
);
110 case GL_FRAGMENT_PROGRAM_ARB
: {
111 struct st_fragment_program
*prog
= ST_CALLOC_STRUCT(st_fragment_program
);
112 return _mesa_init_gl_program(&prog
->Base
.Base
, target
, id
);
114 case GL_GEOMETRY_PROGRAM_NV
: {
115 struct st_geometry_program
*prog
= ST_CALLOC_STRUCT(st_geometry_program
);
116 return _mesa_init_gl_program(&prog
->Base
.Base
, target
, id
);
118 case GL_TESS_CONTROL_PROGRAM_NV
: {
119 struct st_tessctrl_program
*prog
= ST_CALLOC_STRUCT(st_tessctrl_program
);
120 return _mesa_init_gl_program(&prog
->Base
.Base
, target
, id
);
122 case GL_TESS_EVALUATION_PROGRAM_NV
: {
123 struct st_tesseval_program
*prog
= ST_CALLOC_STRUCT(st_tesseval_program
);
124 return _mesa_init_gl_program(&prog
->Base
.Base
, target
, id
);
134 * Called via ctx->Driver.DeleteProgram()
137 st_delete_program(struct gl_context
*ctx
, struct gl_program
*prog
)
139 struct st_context
*st
= st_context(ctx
);
141 switch( prog
->Target
) {
142 case GL_VERTEX_PROGRAM_ARB
:
144 struct st_vertex_program
*stvp
= (struct st_vertex_program
*) prog
;
145 st_release_vp_variants( st
, stvp
);
147 if (stvp
->glsl_to_tgsi
)
148 free_glsl_to_tgsi_visitor(stvp
->glsl_to_tgsi
);
151 case GL_GEOMETRY_PROGRAM_NV
:
153 struct st_geometry_program
*stgp
=
154 (struct st_geometry_program
*) prog
;
156 st_release_basic_variants(st
, stgp
->Base
.Base
.Target
,
157 &stgp
->variants
, &stgp
->tgsi
);
159 if (stgp
->glsl_to_tgsi
)
160 free_glsl_to_tgsi_visitor(stgp
->glsl_to_tgsi
);
163 case GL_FRAGMENT_PROGRAM_ARB
:
165 struct st_fragment_program
*stfp
=
166 (struct st_fragment_program
*) prog
;
168 st_release_fp_variants(st
, stfp
);
170 if (stfp
->glsl_to_tgsi
)
171 free_glsl_to_tgsi_visitor(stfp
->glsl_to_tgsi
);
174 case GL_TESS_CONTROL_PROGRAM_NV
:
176 struct st_tessctrl_program
*sttcp
=
177 (struct st_tessctrl_program
*) prog
;
179 st_release_basic_variants(st
, sttcp
->Base
.Base
.Target
,
180 &sttcp
->variants
, &sttcp
->tgsi
);
182 if (sttcp
->glsl_to_tgsi
)
183 free_glsl_to_tgsi_visitor(sttcp
->glsl_to_tgsi
);
186 case GL_TESS_EVALUATION_PROGRAM_NV
:
188 struct st_tesseval_program
*sttep
=
189 (struct st_tesseval_program
*) prog
;
191 st_release_basic_variants(st
, sttep
->Base
.Base
.Target
,
192 &sttep
->variants
, &sttep
->tgsi
);
194 if (sttep
->glsl_to_tgsi
)
195 free_glsl_to_tgsi_visitor(sttep
->glsl_to_tgsi
);
199 assert(0); /* problem */
202 /* delete base class */
203 _mesa_delete_program( ctx
, prog
);
208 * Called via ctx->Driver.ProgramStringNotify()
209 * Called when the program's text/code is changed. We have to free
210 * all shader variants and corresponding gallium shaders when this happens.
213 st_program_string_notify( struct gl_context
*ctx
,
215 struct gl_program
*prog
)
217 struct st_context
*st
= st_context(ctx
);
218 gl_shader_stage stage
= _mesa_program_enum_to_shader_stage(target
);
220 if (target
== GL_FRAGMENT_PROGRAM_ARB
) {
221 struct st_fragment_program
*stfp
= (struct st_fragment_program
*) prog
;
223 st_release_fp_variants(st
, stfp
);
224 if (!st_translate_fragment_program(st
, stfp
))
228 st
->dirty
.st
|= ST_NEW_FRAGMENT_PROGRAM
;
230 else if (target
== GL_GEOMETRY_PROGRAM_NV
) {
231 struct st_geometry_program
*stgp
= (struct st_geometry_program
*) prog
;
233 st_release_basic_variants(st
, stgp
->Base
.Base
.Target
,
234 &stgp
->variants
, &stgp
->tgsi
);
235 if (!st_translate_geometry_program(st
, stgp
))
239 st
->dirty
.st
|= ST_NEW_GEOMETRY_PROGRAM
;
241 else if (target
== GL_VERTEX_PROGRAM_ARB
) {
242 struct st_vertex_program
*stvp
= (struct st_vertex_program
*) prog
;
244 st_release_vp_variants(st
, stvp
);
245 if (!st_translate_vertex_program(st
, stvp
))
249 st
->dirty
.st
|= ST_NEW_VERTEX_PROGRAM
;
251 else if (target
== GL_TESS_CONTROL_PROGRAM_NV
) {
252 struct st_tessctrl_program
*sttcp
=
253 (struct st_tessctrl_program
*) prog
;
255 st_release_basic_variants(st
, sttcp
->Base
.Base
.Target
,
256 &sttcp
->variants
, &sttcp
->tgsi
);
257 if (!st_translate_tessctrl_program(st
, sttcp
))
260 if (st
->tcp
== sttcp
)
261 st
->dirty
.st
|= ST_NEW_TESSCTRL_PROGRAM
;
263 else if (target
== GL_TESS_EVALUATION_PROGRAM_NV
) {
264 struct st_tesseval_program
*sttep
=
265 (struct st_tesseval_program
*) prog
;
267 st_release_basic_variants(st
, sttep
->Base
.Base
.Target
,
268 &sttep
->variants
, &sttep
->tgsi
);
269 if (!st_translate_tesseval_program(st
, sttep
))
272 if (st
->tep
== sttep
)
273 st
->dirty
.st
|= ST_NEW_TESSEVAL_PROGRAM
;
276 if (ST_DEBUG
& DEBUG_PRECOMPILE
||
277 st
->shader_has_one_variant
[stage
])
278 st_precompile_shader_variant(st
, prog
);
285 * Plug in the program and shader-related device driver functions.
288 st_init_program_functions(struct dd_function_table
*functions
)
290 functions
->BindProgram
= st_bind_program
;
291 functions
->UseProgram
= st_use_program
;
292 functions
->NewProgram
= st_new_program
;
293 functions
->DeleteProgram
= st_delete_program
;
294 functions
->ProgramStringNotify
= st_program_string_notify
;
296 functions
->LinkShader
= st_link_shader
;