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 "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"
44 #include "st_program.h"
45 #include "st_mesa_to_tgsi.h"
46 #include "st_cb_program.h"
51 * Called via ctx->Driver.BindProgram() to bind an ARB vertex or
55 st_bind_program(struct gl_context *ctx, GLenum target, struct gl_program *prog)
57 struct st_context *st = st_context(ctx);
60 case GL_VERTEX_PROGRAM_ARB:
61 st->dirty.st |= ST_NEW_VERTEX_PROGRAM;
63 case GL_FRAGMENT_PROGRAM_ARB:
64 st->dirty.st |= ST_NEW_FRAGMENT_PROGRAM;
66 case MESA_GEOMETRY_PROGRAM:
67 st->dirty.st |= ST_NEW_GEOMETRY_PROGRAM;
74 * Called via ctx->Driver.UseProgram() to bind a linked GLSL program
75 * (vertex shader + fragment shader).
78 st_use_program(struct gl_context *ctx, struct gl_shader_program *shProg)
80 struct st_context *st = st_context(ctx);
82 st->dirty.st |= ST_NEW_FRAGMENT_PROGRAM;
83 st->dirty.st |= ST_NEW_VERTEX_PROGRAM;
84 st->dirty.st |= ST_NEW_GEOMETRY_PROGRAM;
89 * Called via ctx->Driver.NewProgram() to allocate a new vertex or
92 static struct gl_program *
93 st_new_program(struct gl_context *ctx, GLenum target, GLuint id)
96 case GL_VERTEX_PROGRAM_ARB: {
97 struct st_vertex_program *prog = ST_CALLOC_STRUCT(st_vertex_program);
98 return _mesa_init_vertex_program(ctx, &prog->Base, target, id);
101 case GL_FRAGMENT_PROGRAM_ARB:
102 case GL_FRAGMENT_PROGRAM_NV: {
103 struct st_fragment_program *prog = ST_CALLOC_STRUCT(st_fragment_program);
104 return _mesa_init_fragment_program(ctx, &prog->Base, target, id);
107 case MESA_GEOMETRY_PROGRAM: {
108 struct st_geometry_program *prog = ST_CALLOC_STRUCT(st_geometry_program);
109 return _mesa_init_geometry_program(ctx, &prog->Base, target, id);
120 * Called via ctx->Driver.DeleteProgram()
123 st_delete_program(struct gl_context *ctx, struct gl_program *prog)
125 struct st_context *st = st_context(ctx);
127 switch( prog->Target ) {
128 case GL_VERTEX_PROGRAM_ARB:
130 struct st_vertex_program *stvp = (struct st_vertex_program *) prog;
131 st_release_vp_variants( st, stvp );
134 case MESA_GEOMETRY_PROGRAM:
136 struct st_geometry_program *stgp =
137 (struct st_geometry_program *) prog;
139 st_release_gp_variants(st, stgp);
141 if (stgp->tgsi.tokens) {
142 st_free_tokens((void *) stgp->tgsi.tokens);
143 stgp->tgsi.tokens = NULL;
147 case GL_FRAGMENT_PROGRAM_ARB:
149 struct st_fragment_program *stfp =
150 (struct st_fragment_program *) prog;
152 st_release_fp_variants(st, stfp);
154 if (stfp->tgsi.tokens) {
155 st_free_tokens(stfp->tgsi.tokens);
156 stfp->tgsi.tokens = NULL;
161 assert(0); /* problem */
164 /* delete base class */
165 _mesa_delete_program( ctx, prog );
170 * Called via ctx->Driver.IsProgramNative()
173 st_is_program_native(struct gl_context *ctx,
175 struct gl_program *prog)
182 * Called via ctx->Driver.ProgramStringNotify()
183 * Called when the program's text/code is changed. We have to free
184 * all shader variants and corresponding gallium shaders when this happens.
187 st_program_string_notify( struct gl_context *ctx,
189 struct gl_program *prog )
191 struct st_context *st = st_context(ctx);
193 if (target == GL_FRAGMENT_PROGRAM_ARB) {
194 struct st_fragment_program *stfp = (struct st_fragment_program *) prog;
196 st_release_fp_variants(st, stfp);
198 if (stfp->tgsi.tokens) {
199 st_free_tokens(stfp->tgsi.tokens);
200 stfp->tgsi.tokens = NULL;
204 st->dirty.st |= ST_NEW_FRAGMENT_PROGRAM;
206 else if (target == MESA_GEOMETRY_PROGRAM) {
207 struct st_geometry_program *stgp = (struct st_geometry_program *) prog;
209 st_release_gp_variants(st, stgp);
211 if (stgp->tgsi.tokens) {
212 st_free_tokens((void *) stgp->tgsi.tokens);
213 stgp->tgsi.tokens = NULL;
217 st->dirty.st |= ST_NEW_GEOMETRY_PROGRAM;
219 else if (target == GL_VERTEX_PROGRAM_ARB) {
220 struct st_vertex_program *stvp = (struct st_vertex_program *) prog;
222 st_release_vp_variants( st, stvp );
225 st->dirty.st |= ST_NEW_VERTEX_PROGRAM;
228 /* XXX check if program is legal, within limits */
234 * Plug in the program and shader-related device driver functions.
237 st_init_program_functions(struct dd_function_table *functions)
239 functions->BindProgram = st_bind_program;
240 functions->UseProgram = st_use_program;
241 functions->NewProgram = st_new_program;
242 functions->DeleteProgram = st_delete_program;
243 functions->IsProgramNative = st_is_program_native;
244 functions->ProgramStringNotify = st_program_string_notify;