st/mesa/r200/i915/i965: use rzalloc() to create gl_program
[mesa.git] / src / mesa / state_tracker / st_cb_program.c
1 /**************************************************************************
2 *
3 * Copyright 2003 VMware, Inc.
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 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.
25 *
26 **************************************************************************/
27
28 /*
29 * Authors:
30 * Keith Whitwell <keithw@vmware.com>
31 */
32
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"
39
40 #include "cso_cache/cso_context.h"
41 #include "draw/draw_context.h"
42
43 #include "st_context.h"
44 #include "st_debug.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"
49 #include "st_atifs_to_tgsi.h"
50
51
52 /**
53 * Called via ctx->Driver.NewProgram() to allocate a new vertex or
54 * fragment program.
55 */
56 static struct gl_program *
57 st_new_program(struct gl_context *ctx, GLenum target, GLuint id)
58 {
59 switch (target) {
60 case GL_VERTEX_PROGRAM_ARB: {
61 struct st_vertex_program *prog = rzalloc(NULL,
62 struct st_vertex_program);
63 return _mesa_init_gl_program(&prog->Base, target, id);
64 }
65 case GL_FRAGMENT_PROGRAM_ARB: {
66 struct st_fragment_program *prog = rzalloc(NULL,
67 struct st_fragment_program);
68 return _mesa_init_gl_program(&prog->Base, target, id);
69 }
70 case GL_GEOMETRY_PROGRAM_NV: {
71 struct st_geometry_program *prog = rzalloc(NULL,
72 struct st_geometry_program);
73 return _mesa_init_gl_program(&prog->Base, target, id);
74 }
75 case GL_TESS_CONTROL_PROGRAM_NV: {
76 struct st_tessctrl_program *prog = rzalloc(NULL,
77 struct st_tessctrl_program);
78 return _mesa_init_gl_program(&prog->Base, target, id);
79 }
80 case GL_TESS_EVALUATION_PROGRAM_NV: {
81 struct st_tesseval_program *prog = rzalloc(NULL,
82 struct st_tesseval_program);
83 return _mesa_init_gl_program(&prog->Base, target, id);
84 }
85 case GL_COMPUTE_PROGRAM_NV: {
86 struct st_compute_program *prog = rzalloc(NULL,
87 struct st_compute_program);
88 return _mesa_init_gl_program(&prog->Base, target, id);
89 }
90 default:
91 assert(0);
92 return NULL;
93 }
94 }
95
96
97 /**
98 * Called via ctx->Driver.DeleteProgram()
99 */
100 static void
101 st_delete_program(struct gl_context *ctx, struct gl_program *prog)
102 {
103 struct st_context *st = st_context(ctx);
104
105 switch( prog->Target ) {
106 case GL_VERTEX_PROGRAM_ARB:
107 {
108 struct st_vertex_program *stvp = (struct st_vertex_program *) prog;
109 st_release_vp_variants( st, stvp );
110
111 if (stvp->glsl_to_tgsi)
112 free_glsl_to_tgsi_visitor(stvp->glsl_to_tgsi);
113 }
114 break;
115 case GL_GEOMETRY_PROGRAM_NV:
116 {
117 struct st_geometry_program *stgp =
118 (struct st_geometry_program *) prog;
119
120 st_release_basic_variants(st, stgp->Base.Target, &stgp->variants,
121 &stgp->tgsi);
122
123 if (stgp->glsl_to_tgsi)
124 free_glsl_to_tgsi_visitor(stgp->glsl_to_tgsi);
125 }
126 break;
127 case GL_FRAGMENT_PROGRAM_ARB:
128 {
129 struct st_fragment_program *stfp =
130 (struct st_fragment_program *) prog;
131
132 st_release_fp_variants(st, stfp);
133
134 if (stfp->glsl_to_tgsi)
135 free_glsl_to_tgsi_visitor(stfp->glsl_to_tgsi);
136 }
137 break;
138 case GL_TESS_CONTROL_PROGRAM_NV:
139 {
140 struct st_tessctrl_program *sttcp =
141 (struct st_tessctrl_program *) prog;
142
143 st_release_basic_variants(st, sttcp->Base.Target, &sttcp->variants,
144 &sttcp->tgsi);
145
146 if (sttcp->glsl_to_tgsi)
147 free_glsl_to_tgsi_visitor(sttcp->glsl_to_tgsi);
148 }
149 break;
150 case GL_TESS_EVALUATION_PROGRAM_NV:
151 {
152 struct st_tesseval_program *sttep =
153 (struct st_tesseval_program *) prog;
154
155 st_release_basic_variants(st, sttep->Base.Target,
156 &sttep->variants, &sttep->tgsi);
157
158 if (sttep->glsl_to_tgsi)
159 free_glsl_to_tgsi_visitor(sttep->glsl_to_tgsi);
160 }
161 break;
162 case GL_COMPUTE_PROGRAM_NV:
163 {
164 struct st_compute_program *stcp =
165 (struct st_compute_program *) prog;
166
167 st_release_cp_variants(st, stcp);
168
169 if (stcp->glsl_to_tgsi)
170 free_glsl_to_tgsi_visitor(stcp->glsl_to_tgsi);
171 }
172 break;
173 default:
174 assert(0); /* problem */
175 }
176
177 /* delete base class */
178 _mesa_delete_program( ctx, prog );
179 }
180
181
182 /**
183 * Called via ctx->Driver.ProgramStringNotify()
184 * Called when the program's text/code is changed. We have to free
185 * all shader variants and corresponding gallium shaders when this happens.
186 */
187 static GLboolean
188 st_program_string_notify( struct gl_context *ctx,
189 GLenum target,
190 struct gl_program *prog )
191 {
192 struct st_context *st = st_context(ctx);
193 gl_shader_stage stage = _mesa_program_enum_to_shader_stage(target);
194
195 if (target == GL_FRAGMENT_PROGRAM_ARB) {
196 struct st_fragment_program *stfp = (struct st_fragment_program *) prog;
197
198 st_release_fp_variants(st, stfp);
199 if (!st_translate_fragment_program(st, stfp))
200 return false;
201
202 if (st->fp == stfp)
203 st->dirty |= stfp->affected_states;
204 }
205 else if (target == GL_GEOMETRY_PROGRAM_NV) {
206 struct st_geometry_program *stgp = (struct st_geometry_program *) prog;
207
208 st_release_basic_variants(st, stgp->Base.Target, &stgp->variants,
209 &stgp->tgsi);
210 if (!st_translate_geometry_program(st, stgp))
211 return false;
212
213 if (st->gp == stgp)
214 st->dirty |= stgp->affected_states;
215 }
216 else if (target == GL_VERTEX_PROGRAM_ARB) {
217 struct st_vertex_program *stvp = (struct st_vertex_program *) prog;
218
219 st_release_vp_variants(st, stvp);
220 if (!st_translate_vertex_program(st, stvp))
221 return false;
222
223 if (st->vp == stvp)
224 st->dirty |= ST_NEW_VERTEX_PROGRAM(st, stvp);
225 }
226 else if (target == GL_TESS_CONTROL_PROGRAM_NV) {
227 struct st_tessctrl_program *sttcp =
228 (struct st_tessctrl_program *) prog;
229
230 st_release_basic_variants(st, sttcp->Base.Target, &sttcp->variants,
231 &sttcp->tgsi);
232 if (!st_translate_tessctrl_program(st, sttcp))
233 return false;
234
235 if (st->tcp == sttcp)
236 st->dirty |= sttcp->affected_states;
237 }
238 else if (target == GL_TESS_EVALUATION_PROGRAM_NV) {
239 struct st_tesseval_program *sttep =
240 (struct st_tesseval_program *) prog;
241
242 st_release_basic_variants(st, sttep->Base.Target, &sttep->variants,
243 &sttep->tgsi);
244 if (!st_translate_tesseval_program(st, sttep))
245 return false;
246
247 if (st->tep == sttep)
248 st->dirty |= sttep->affected_states;
249 }
250 else if (target == GL_COMPUTE_PROGRAM_NV) {
251 struct st_compute_program *stcp =
252 (struct st_compute_program *) prog;
253
254 st_release_cp_variants(st, stcp);
255 if (!st_translate_compute_program(st, stcp))
256 return false;
257
258 if (st->cp == stcp)
259 st->dirty |= stcp->affected_states;
260 }
261 else if (target == GL_FRAGMENT_SHADER_ATI) {
262 assert(prog);
263
264 struct st_fragment_program *stfp = (struct st_fragment_program *) prog;
265 assert(stfp->ati_fs);
266 assert(stfp->ati_fs->Program == prog);
267
268 st_init_atifs_prog(ctx, prog);
269
270 st_release_fp_variants(st, stfp);
271 if (!st_translate_fragment_program(st, stfp))
272 return false;
273
274 if (st->fp == stfp)
275 st->dirty |= stfp->affected_states;
276 }
277
278 if (ST_DEBUG & DEBUG_PRECOMPILE ||
279 st->shader_has_one_variant[stage])
280 st_precompile_shader_variant(st, prog);
281
282 return GL_TRUE;
283 }
284
285 /**
286 * Called via ctx->Driver.NewATIfs()
287 * Called in glEndFragmentShaderATI()
288 */
289 static struct gl_program *
290 st_new_ati_fs(struct gl_context *ctx, struct ati_fragment_shader *curProg)
291 {
292 struct gl_program *prog = ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB,
293 curProg->Id);
294 struct st_fragment_program *stfp = (struct st_fragment_program *)prog;
295 stfp->ati_fs = curProg;
296 return prog;
297 }
298
299 /**
300 * Plug in the program and shader-related device driver functions.
301 */
302 void
303 st_init_program_functions(struct dd_function_table *functions)
304 {
305 functions->NewProgram = st_new_program;
306 functions->DeleteProgram = st_delete_program;
307 functions->ProgramStringNotify = st_program_string_notify;
308 functions->NewATIfs = st_new_ati_fs;
309
310 functions->LinkShader = st_link_shader;
311 }