Merge branch 'gallium-0.1' into gallium-tex-surfaces
[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_cb_program.h"
49
50
51 static GLuint SerialNo = 1;
52
53
54 /**
55 * Called via ctx->Driver.BindProgram() to bind an ARB vertex or
56 * fragment program.
57 */
58 static void st_bind_program( GLcontext *ctx,
59 GLenum target,
60 struct gl_program *prog )
61 {
62 struct st_context *st = st_context(ctx);
63
64 switch (target) {
65 case GL_VERTEX_PROGRAM_ARB:
66 st->dirty.st |= ST_NEW_VERTEX_PROGRAM;
67 break;
68 case GL_FRAGMENT_PROGRAM_ARB:
69 st->dirty.st |= ST_NEW_FRAGMENT_PROGRAM;
70 break;
71 }
72 }
73
74
75 /**
76 * Called via ctx->Driver.UseProgram() to bind a linked GLSL program
77 * (vertex shader + fragment shader).
78 */
79 static void st_use_program( GLcontext *ctx,
80 GLuint program )
81 {
82 struct st_context *st = st_context(ctx);
83
84 st->dirty.st |= ST_NEW_FRAGMENT_PROGRAM;
85 st->dirty.st |= ST_NEW_VERTEX_PROGRAM;
86
87 _mesa_use_program(ctx, program);
88 }
89
90
91
92 /**
93 * Called via ctx->Driver.NewProgram() to allocate a new vertex or
94 * fragment program.
95 */
96 static struct gl_program *st_new_program( GLcontext *ctx,
97 GLenum target,
98 GLuint id )
99 {
100 switch (target) {
101 case GL_VERTEX_PROGRAM_ARB: {
102 struct st_vertex_program *prog = CALLOC_STRUCT(st_vertex_program);
103
104 prog->serialNo = SerialNo++;
105
106 return _mesa_init_vertex_program( ctx,
107 &prog->Base,
108 target,
109 id );
110 }
111
112 case GL_FRAGMENT_PROGRAM_ARB:
113 case GL_FRAGMENT_PROGRAM_NV: {
114 struct st_fragment_program *prog = CALLOC_STRUCT(st_fragment_program);
115
116 prog->serialNo = SerialNo++;
117
118 return _mesa_init_fragment_program( ctx,
119 &prog->Base,
120 target,
121 id );
122 }
123
124 default:
125 assert(0);
126 return NULL;
127 }
128 }
129
130
131 void
132 st_delete_program(GLcontext *ctx, struct gl_program *prog)
133 {
134 struct st_context *st = st_context(ctx);
135
136 switch( prog->Target ) {
137 case GL_VERTEX_PROGRAM_ARB:
138 {
139 struct st_vertex_program *stvp = (struct st_vertex_program *) prog;
140
141 if (stvp->driver_shader) {
142 cso_delete_vertex_shader(st->cso_context, stvp->driver_shader);
143 stvp->driver_shader = NULL;
144 }
145
146 if (stvp->state.tokens) {
147 FREE((void *) stvp->state.tokens);
148 stvp->state.tokens = NULL;
149 }
150 }
151 break;
152 case GL_FRAGMENT_PROGRAM_ARB:
153 {
154 struct st_fragment_program *stfp = (struct st_fragment_program *) prog;
155
156 if (stfp->driver_shader) {
157 cso_delete_fragment_shader(st->cso_context, stfp->driver_shader);
158 stfp->driver_shader = NULL;
159 }
160
161 if (stfp->state.tokens) {
162 FREE((void *) stfp->state.tokens);
163 stfp->state.tokens = NULL;
164 }
165
166 if (stfp->bitmap_program) {
167 struct gl_program *prg = &stfp->bitmap_program->Base.Base;
168 _mesa_reference_program(ctx, &prg, NULL);
169 stfp->bitmap_program = NULL;
170 }
171
172 st_free_translated_vertex_programs(st, stfp->vertex_programs);
173 }
174 break;
175 default:
176 assert(0); /* problem */
177 }
178
179 /* delete base class */
180 _mesa_delete_program( ctx, prog );
181 }
182
183
184 static GLboolean st_is_program_native( GLcontext *ctx,
185 GLenum target,
186 struct gl_program *prog )
187 {
188 return GL_TRUE;
189 }
190
191
192 static void st_program_string_notify( GLcontext *ctx,
193 GLenum target,
194 struct gl_program *prog )
195 {
196 struct st_context *st = st_context(ctx);
197
198 if (target == GL_FRAGMENT_PROGRAM_ARB) {
199 struct st_fragment_program *stfp = (struct st_fragment_program *) prog;
200
201 stfp->serialNo++;
202
203 if (stfp->driver_shader) {
204 cso_delete_fragment_shader(st->cso_context, stfp->driver_shader);
205 stfp->driver_shader = NULL;
206 }
207
208 if (stfp->state.tokens) {
209 FREE((void *) stfp->state.tokens);
210 stfp->state.tokens = NULL;
211 }
212
213 stfp->param_state = stfp->Base.Base.Parameters->StateFlags;
214
215 if (st->fp == stfp)
216 st->dirty.st |= ST_NEW_FRAGMENT_PROGRAM;
217 }
218 else if (target == GL_VERTEX_PROGRAM_ARB) {
219 struct st_vertex_program *stvp = (struct st_vertex_program *) prog;
220
221 stvp->serialNo++;
222
223 if (stvp->driver_shader) {
224 cso_delete_vertex_shader(st->cso_context, stvp->driver_shader);
225 stvp->driver_shader = NULL;
226 }
227
228 if (stvp->draw_shader) {
229 draw_delete_vertex_shader(st->draw, stvp->draw_shader);
230 stvp->draw_shader = NULL;
231 }
232
233 if (stvp->state.tokens) {
234 FREE((void *) stvp->state.tokens);
235 stvp->state.tokens = NULL;
236 }
237
238 stvp->param_state = stvp->Base.Base.Parameters->StateFlags;
239
240 if (st->vp == stvp)
241 st->dirty.st |= ST_NEW_VERTEX_PROGRAM;
242 }
243 }
244
245
246
247 void st_init_program_functions(struct dd_function_table *functions)
248 {
249 functions->BindProgram = st_bind_program;
250 functions->UseProgram = st_use_program;
251 functions->NewProgram = st_new_program;
252 functions->DeleteProgram = st_delete_program;
253 functions->IsProgramNative = st_is_program_native;
254 functions->ProgramStringNotify = st_program_string_notify;
255 }