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 **************************************************************************/
30 * Keith Whitwell <keith@tungstengraphics.com>
33 #include "main/glheader.h"
34 #include "main/macros.h"
35 #include "main/enums.h"
36 #include "shader/prog_instruction.h"
37 #include "shader/prog_parameter.h"
38 #include "shader/program.h"
39 #include "shader/programopt.h"
40 #include "shader/shader_api.h"
42 #include "cso_cache/cso_context.h"
43 #include "draw/draw_context.h"
45 #include "st_context.h"
46 #include "st_program.h"
47 #include "st_atom_shader.h"
48 #include "st_cb_program.h"
51 static GLuint SerialNo
= 1;
55 * Called via ctx->Driver.BindProgram() to bind an ARB vertex or
58 static void st_bind_program( GLcontext
*ctx
,
60 struct gl_program
*prog
)
62 struct st_context
*st
= st_context(ctx
);
65 case GL_VERTEX_PROGRAM_ARB
:
66 st
->dirty
.st
|= ST_NEW_VERTEX_PROGRAM
;
68 case GL_FRAGMENT_PROGRAM_ARB
:
69 st
->dirty
.st
|= ST_NEW_FRAGMENT_PROGRAM
;
76 * Called via ctx->Driver.UseProgram() to bind a linked GLSL program
77 * (vertex shader + fragment shader).
79 static void st_use_program( GLcontext
*ctx
,
82 struct st_context
*st
= st_context(ctx
);
84 st
->dirty
.st
|= ST_NEW_FRAGMENT_PROGRAM
;
85 st
->dirty
.st
|= ST_NEW_VERTEX_PROGRAM
;
87 _mesa_use_program(ctx
, program
);
93 * Called via ctx->Driver.NewProgram() to allocate a new vertex or
96 static struct gl_program
*st_new_program( GLcontext
*ctx
,
101 case GL_VERTEX_PROGRAM_ARB
: {
102 struct st_vertex_program
*prog
= ST_CALLOC_STRUCT(st_vertex_program
);
104 prog
->serialNo
= SerialNo
++;
106 return _mesa_init_vertex_program( ctx
,
112 case GL_FRAGMENT_PROGRAM_ARB
:
113 case GL_FRAGMENT_PROGRAM_NV
: {
114 struct st_fragment_program
*prog
= ST_CALLOC_STRUCT(st_fragment_program
);
116 prog
->serialNo
= SerialNo
++;
118 return _mesa_init_fragment_program( ctx
,
132 st_delete_program(GLcontext
*ctx
, struct gl_program
*prog
)
134 struct st_context
*st
= st_context(ctx
);
136 switch( prog
->Target
) {
137 case GL_VERTEX_PROGRAM_ARB
:
139 struct st_vertex_program
*stvp
= (struct st_vertex_program
*) prog
;
141 if (stvp
->driver_shader
) {
142 cso_delete_vertex_shader(st
->cso_context
, stvp
->driver_shader
);
143 stvp
->driver_shader
= NULL
;
146 if (stvp
->draw_shader
) {
147 #if FEATURE_feedback || FEATURE_drawpix
148 /* this would only have been allocated for the RasterPos path */
149 draw_delete_vertex_shader(st
->draw
, stvp
->draw_shader
);
150 stvp
->draw_shader
= NULL
;
154 if (stvp
->state
.tokens
) {
155 _mesa_free((void *) stvp
->state
.tokens
);
156 stvp
->state
.tokens
= NULL
;
160 case GL_FRAGMENT_PROGRAM_ARB
:
162 struct st_fragment_program
*stfp
= (struct st_fragment_program
*) prog
;
164 if (stfp
->driver_shader
) {
165 cso_delete_fragment_shader(st
->cso_context
, stfp
->driver_shader
);
166 stfp
->driver_shader
= NULL
;
169 if (stfp
->state
.tokens
) {
170 _mesa_free((void *) stfp
->state
.tokens
);
171 stfp
->state
.tokens
= NULL
;
174 if (stfp
->bitmap_program
) {
175 struct gl_program
*prg
= &stfp
->bitmap_program
->Base
.Base
;
176 _mesa_reference_program(ctx
, &prg
, NULL
);
177 stfp
->bitmap_program
= NULL
;
180 st_free_translated_vertex_programs(st
, stfp
->vertex_programs
);
184 assert(0); /* problem */
187 /* delete base class */
188 _mesa_delete_program( ctx
, prog
);
192 static GLboolean
st_is_program_native( GLcontext
*ctx
,
194 struct gl_program
*prog
)
200 static void st_program_string_notify( GLcontext
*ctx
,
202 struct gl_program
*prog
)
204 struct st_context
*st
= st_context(ctx
);
206 if (target
== GL_FRAGMENT_PROGRAM_ARB
) {
207 struct st_fragment_program
*stfp
= (struct st_fragment_program
*) prog
;
211 if (stfp
->driver_shader
) {
212 cso_delete_fragment_shader(st
->cso_context
, stfp
->driver_shader
);
213 stfp
->driver_shader
= NULL
;
216 if (stfp
->state
.tokens
) {
217 _mesa_free((void *) stfp
->state
.tokens
);
218 stfp
->state
.tokens
= NULL
;
221 stfp
->param_state
= stfp
->Base
.Base
.Parameters
->StateFlags
;
224 st
->dirty
.st
|= ST_NEW_FRAGMENT_PROGRAM
;
226 else if (target
== GL_VERTEX_PROGRAM_ARB
) {
227 struct st_vertex_program
*stvp
= (struct st_vertex_program
*) prog
;
231 if (stvp
->driver_shader
) {
232 cso_delete_vertex_shader(st
->cso_context
, stvp
->driver_shader
);
233 stvp
->driver_shader
= NULL
;
236 if (stvp
->draw_shader
) {
237 #if FEATURE_feedback || FEATURE_drawpix
238 /* this would only have been allocated for the RasterPos path */
239 draw_delete_vertex_shader(st
->draw
, stvp
->draw_shader
);
240 stvp
->draw_shader
= NULL
;
244 if (stvp
->state
.tokens
) {
245 _mesa_free((void *) stvp
->state
.tokens
);
246 stvp
->state
.tokens
= NULL
;
249 stvp
->param_state
= stvp
->Base
.Base
.Parameters
->StateFlags
;
252 st
->dirty
.st
|= ST_NEW_VERTEX_PROGRAM
;
258 void st_init_program_functions(struct dd_function_table
*functions
)
260 functions
->BindProgram
= st_bind_program
;
261 functions
->UseProgram
= st_use_program
;
262 functions
->NewProgram
= st_new_program
;
263 functions
->DeleteProgram
= st_delete_program
;
264 functions
->IsProgramNative
= st_is_program_native
;
265 functions
->ProgramStringNotify
= st_program_string_notify
;