Merge branch 'master' into r300-compiler
[mesa.git] / src / mesa / state_tracker / st_cb_program.c
1 /**************************************************************************
2 *
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28 /*
29 * Authors:
30 * Keith Whitwell <keith@tungstengraphics.com>
31 */
32
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"
41
42 #include "cso_cache/cso_context.h"
43 #include "draw/draw_context.h"
44
45 #include "st_context.h"
46 #include "st_program.h"
47 #include "st_atom_shader.h"
48 #include "st_mesa_to_tgsi.h"
49 #include "st_cb_program.h"
50
51
52 static GLuint SerialNo = 1;
53
54
55 /**
56 * Called via ctx->Driver.BindProgram() to bind an ARB vertex or
57 * fragment program.
58 */
59 static void st_bind_program( GLcontext *ctx,
60 GLenum target,
61 struct gl_program *prog )
62 {
63 struct st_context *st = st_context(ctx);
64
65 switch (target) {
66 case GL_VERTEX_PROGRAM_ARB:
67 st->dirty.st |= ST_NEW_VERTEX_PROGRAM;
68 break;
69 case GL_FRAGMENT_PROGRAM_ARB:
70 st->dirty.st |= ST_NEW_FRAGMENT_PROGRAM;
71 break;
72 }
73 }
74
75
76 /**
77 * Called via ctx->Driver.UseProgram() to bind a linked GLSL program
78 * (vertex shader + fragment shader).
79 */
80 static void st_use_program( GLcontext *ctx,
81 GLuint program )
82 {
83 struct st_context *st = st_context(ctx);
84
85 st->dirty.st |= ST_NEW_FRAGMENT_PROGRAM;
86 st->dirty.st |= ST_NEW_VERTEX_PROGRAM;
87
88 _mesa_use_program(ctx, program);
89 }
90
91
92
93 /**
94 * Called via ctx->Driver.NewProgram() to allocate a new vertex or
95 * fragment program.
96 */
97 static struct gl_program *st_new_program( GLcontext *ctx,
98 GLenum target,
99 GLuint id )
100 {
101 switch (target) {
102 case GL_VERTEX_PROGRAM_ARB: {
103 struct st_vertex_program *prog = ST_CALLOC_STRUCT(st_vertex_program);
104
105 prog->serialNo = SerialNo++;
106
107 return _mesa_init_vertex_program( ctx,
108 &prog->Base,
109 target,
110 id );
111 }
112
113 case GL_FRAGMENT_PROGRAM_ARB:
114 case GL_FRAGMENT_PROGRAM_NV: {
115 struct st_fragment_program *prog = ST_CALLOC_STRUCT(st_fragment_program);
116
117 prog->serialNo = SerialNo++;
118
119 return _mesa_init_fragment_program( ctx,
120 &prog->Base,
121 target,
122 id );
123 }
124
125 default:
126 assert(0);
127 return NULL;
128 }
129 }
130
131
132 void
133 st_delete_program(GLcontext *ctx, struct gl_program *prog)
134 {
135 struct st_context *st = st_context(ctx);
136
137 switch( prog->Target ) {
138 case GL_VERTEX_PROGRAM_ARB:
139 {
140 struct st_vertex_program *stvp = (struct st_vertex_program *) prog;
141
142 if (stvp->driver_shader) {
143 cso_delete_vertex_shader(st->cso_context, stvp->driver_shader);
144 stvp->driver_shader = NULL;
145 }
146
147 if (stvp->draw_shader) {
148 #if FEATURE_feedback || FEATURE_drawpix
149 /* this would only have been allocated for the RasterPos path */
150 draw_delete_vertex_shader(st->draw, stvp->draw_shader);
151 stvp->draw_shader = NULL;
152 #endif
153 }
154
155 if (stvp->state.tokens) {
156 st_free_tokens(stvp->state.tokens);
157 stvp->state.tokens = NULL;
158 }
159 }
160 break;
161 case GL_FRAGMENT_PROGRAM_ARB:
162 {
163 struct st_fragment_program *stfp = (struct st_fragment_program *) prog;
164
165 if (stfp->driver_shader) {
166 cso_delete_fragment_shader(st->cso_context, stfp->driver_shader);
167 stfp->driver_shader = NULL;
168 }
169
170 if (stfp->state.tokens) {
171 st_free_tokens(stfp->state.tokens);
172 stfp->state.tokens = NULL;
173 }
174
175 if (stfp->bitmap_program) {
176 struct gl_program *prg = &stfp->bitmap_program->Base.Base;
177 _mesa_reference_program(ctx, &prg, NULL);
178 stfp->bitmap_program = NULL;
179 }
180
181 st_free_translated_vertex_programs(st, stfp->vertex_programs);
182 }
183 break;
184 default:
185 assert(0); /* problem */
186 }
187
188 /* delete base class */
189 _mesa_delete_program( ctx, prog );
190 }
191
192
193 static GLboolean st_is_program_native( GLcontext *ctx,
194 GLenum target,
195 struct gl_program *prog )
196 {
197 return GL_TRUE;
198 }
199
200
201 static void st_program_string_notify( GLcontext *ctx,
202 GLenum target,
203 struct gl_program *prog )
204 {
205 struct st_context *st = st_context(ctx);
206
207 if (target == GL_FRAGMENT_PROGRAM_ARB) {
208 struct st_fragment_program *stfp = (struct st_fragment_program *) prog;
209
210 stfp->serialNo++;
211
212 if (stfp->driver_shader) {
213 cso_delete_fragment_shader(st->cso_context, stfp->driver_shader);
214 stfp->driver_shader = NULL;
215 }
216
217 if (stfp->state.tokens) {
218 st_free_tokens(stfp->state.tokens);
219 stfp->state.tokens = NULL;
220 }
221
222 stfp->param_state = stfp->Base.Base.Parameters->StateFlags;
223
224 if (st->fp == stfp)
225 st->dirty.st |= ST_NEW_FRAGMENT_PROGRAM;
226 }
227 else if (target == GL_VERTEX_PROGRAM_ARB) {
228 struct st_vertex_program *stvp = (struct st_vertex_program *) prog;
229
230 stvp->serialNo++;
231
232 if (stvp->driver_shader) {
233 cso_delete_vertex_shader(st->cso_context, stvp->driver_shader);
234 stvp->driver_shader = NULL;
235 }
236
237 if (stvp->draw_shader) {
238 #if FEATURE_feedback || FEATURE_drawpix
239 /* this would only have been allocated for the RasterPos path */
240 draw_delete_vertex_shader(st->draw, stvp->draw_shader);
241 stvp->draw_shader = NULL;
242 #endif
243 }
244
245 if (stvp->state.tokens) {
246 st_free_tokens(stvp->state.tokens);
247 stvp->state.tokens = NULL;
248 }
249
250 stvp->param_state = stvp->Base.Base.Parameters->StateFlags;
251
252 if (st->vp == stvp)
253 st->dirty.st |= ST_NEW_VERTEX_PROGRAM;
254 }
255 }
256
257
258
259 void st_init_program_functions(struct dd_function_table *functions)
260 {
261 functions->BindProgram = st_bind_program;
262 functions->UseProgram = st_use_program;
263 functions->NewProgram = st_new_program;
264 functions->DeleteProgram = st_delete_program;
265 functions->IsProgramNative = st_is_program_native;
266 functions->ProgramStringNotify = st_program_string_notify;
267 }