st/mesa: implement GL_ATI_fragment_shader
[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 /**
54 * Called via ctx->Driver.BindProgram() to bind an ARB vertex or
55 * fragment program.
56 */
57 static void
58 st_bind_program(struct gl_context *ctx, GLenum target, struct gl_program *prog)
59 {
60 struct st_context *st = st_context(ctx);
61
62 switch (target) {
63 case GL_VERTEX_PROGRAM_ARB:
64 st->dirty.st |= ST_NEW_VERTEX_PROGRAM;
65 break;
66 case GL_FRAGMENT_PROGRAM_ARB:
67 st->dirty.st |= ST_NEW_FRAGMENT_PROGRAM;
68 break;
69 case GL_GEOMETRY_PROGRAM_NV:
70 st->dirty.st |= ST_NEW_GEOMETRY_PROGRAM;
71 break;
72 case GL_TESS_CONTROL_PROGRAM_NV:
73 st->dirty.st |= ST_NEW_TESSCTRL_PROGRAM;
74 break;
75 case GL_TESS_EVALUATION_PROGRAM_NV:
76 st->dirty.st |= ST_NEW_TESSEVAL_PROGRAM;
77 break;
78 case GL_COMPUTE_PROGRAM_NV:
79 st->dirty_cp.st |= ST_NEW_COMPUTE_PROGRAM;
80 break;
81 }
82 }
83
84
85 /**
86 * Called via ctx->Driver.UseProgram() to bind a linked GLSL program
87 * (vertex shader + fragment shader).
88 */
89 static void
90 st_use_program(struct gl_context *ctx, struct gl_shader_program *shProg)
91 {
92 struct st_context *st = st_context(ctx);
93
94 st->dirty.st |= ST_NEW_FRAGMENT_PROGRAM;
95 st->dirty.st |= ST_NEW_VERTEX_PROGRAM;
96 st->dirty.st |= ST_NEW_GEOMETRY_PROGRAM;
97 st->dirty.st |= ST_NEW_TESSCTRL_PROGRAM;
98 st->dirty.st |= ST_NEW_TESSEVAL_PROGRAM;
99 st->dirty_cp.st |= ST_NEW_COMPUTE_PROGRAM;
100 }
101
102
103 /**
104 * Called via ctx->Driver.NewProgram() to allocate a new vertex or
105 * fragment program.
106 */
107 static struct gl_program *
108 st_new_program(struct gl_context *ctx, GLenum target, GLuint id)
109 {
110 switch (target) {
111 case GL_VERTEX_PROGRAM_ARB: {
112 struct st_vertex_program *prog = ST_CALLOC_STRUCT(st_vertex_program);
113 return _mesa_init_gl_program(&prog->Base.Base, target, id);
114 }
115 case GL_FRAGMENT_PROGRAM_ARB: {
116 struct st_fragment_program *prog = ST_CALLOC_STRUCT(st_fragment_program);
117 return _mesa_init_gl_program(&prog->Base.Base, target, id);
118 }
119 case GL_GEOMETRY_PROGRAM_NV: {
120 struct st_geometry_program *prog = ST_CALLOC_STRUCT(st_geometry_program);
121 return _mesa_init_gl_program(&prog->Base.Base, target, id);
122 }
123 case GL_TESS_CONTROL_PROGRAM_NV: {
124 struct st_tessctrl_program *prog = ST_CALLOC_STRUCT(st_tessctrl_program);
125 return _mesa_init_gl_program(&prog->Base.Base, target, id);
126 }
127 case GL_TESS_EVALUATION_PROGRAM_NV: {
128 struct st_tesseval_program *prog = ST_CALLOC_STRUCT(st_tesseval_program);
129 return _mesa_init_gl_program(&prog->Base.Base, target, id);
130 }
131 case GL_COMPUTE_PROGRAM_NV: {
132 struct st_compute_program *prog = ST_CALLOC_STRUCT(st_compute_program);
133 return _mesa_init_gl_program(&prog->Base.Base, target, id);
134 }
135 default:
136 assert(0);
137 return NULL;
138 }
139 }
140
141
142 /**
143 * Called via ctx->Driver.DeleteProgram()
144 */
145 static void
146 st_delete_program(struct gl_context *ctx, struct gl_program *prog)
147 {
148 struct st_context *st = st_context(ctx);
149
150 switch( prog->Target ) {
151 case GL_VERTEX_PROGRAM_ARB:
152 {
153 struct st_vertex_program *stvp = (struct st_vertex_program *) prog;
154 st_release_vp_variants( st, stvp );
155
156 if (stvp->glsl_to_tgsi)
157 free_glsl_to_tgsi_visitor(stvp->glsl_to_tgsi);
158 }
159 break;
160 case GL_GEOMETRY_PROGRAM_NV:
161 {
162 struct st_geometry_program *stgp =
163 (struct st_geometry_program *) prog;
164
165 st_release_basic_variants(st, stgp->Base.Base.Target,
166 &stgp->variants, &stgp->tgsi);
167
168 if (stgp->glsl_to_tgsi)
169 free_glsl_to_tgsi_visitor(stgp->glsl_to_tgsi);
170 }
171 break;
172 case GL_FRAGMENT_PROGRAM_ARB:
173 {
174 struct st_fragment_program *stfp =
175 (struct st_fragment_program *) prog;
176
177 st_release_fp_variants(st, stfp);
178
179 if (stfp->glsl_to_tgsi)
180 free_glsl_to_tgsi_visitor(stfp->glsl_to_tgsi);
181 }
182 break;
183 case GL_TESS_CONTROL_PROGRAM_NV:
184 {
185 struct st_tessctrl_program *sttcp =
186 (struct st_tessctrl_program *) prog;
187
188 st_release_basic_variants(st, sttcp->Base.Base.Target,
189 &sttcp->variants, &sttcp->tgsi);
190
191 if (sttcp->glsl_to_tgsi)
192 free_glsl_to_tgsi_visitor(sttcp->glsl_to_tgsi);
193 }
194 break;
195 case GL_TESS_EVALUATION_PROGRAM_NV:
196 {
197 struct st_tesseval_program *sttep =
198 (struct st_tesseval_program *) prog;
199
200 st_release_basic_variants(st, sttep->Base.Base.Target,
201 &sttep->variants, &sttep->tgsi);
202
203 if (sttep->glsl_to_tgsi)
204 free_glsl_to_tgsi_visitor(sttep->glsl_to_tgsi);
205 }
206 break;
207 case GL_COMPUTE_PROGRAM_NV:
208 {
209 struct st_compute_program *stcp =
210 (struct st_compute_program *) prog;
211
212 st_release_cp_variants(st, stcp);
213
214 if (stcp->glsl_to_tgsi)
215 free_glsl_to_tgsi_visitor(stcp->glsl_to_tgsi);
216 }
217 break;
218 default:
219 assert(0); /* problem */
220 }
221
222 /* delete base class */
223 _mesa_delete_program( ctx, prog );
224 }
225
226
227 /**
228 * Called via ctx->Driver.ProgramStringNotify()
229 * Called when the program's text/code is changed. We have to free
230 * all shader variants and corresponding gallium shaders when this happens.
231 */
232 static GLboolean
233 st_program_string_notify( struct gl_context *ctx,
234 GLenum target,
235 struct gl_program *prog )
236 {
237 struct st_context *st = st_context(ctx);
238 gl_shader_stage stage = _mesa_program_enum_to_shader_stage(target);
239
240 if (target == GL_FRAGMENT_PROGRAM_ARB) {
241 struct st_fragment_program *stfp = (struct st_fragment_program *) prog;
242
243 st_release_fp_variants(st, stfp);
244 if (!st_translate_fragment_program(st, stfp))
245 return false;
246
247 if (st->fp == stfp)
248 st->dirty.st |= ST_NEW_FRAGMENT_PROGRAM;
249 }
250 else if (target == GL_GEOMETRY_PROGRAM_NV) {
251 struct st_geometry_program *stgp = (struct st_geometry_program *) prog;
252
253 st_release_basic_variants(st, stgp->Base.Base.Target,
254 &stgp->variants, &stgp->tgsi);
255 if (!st_translate_geometry_program(st, stgp))
256 return false;
257
258 if (st->gp == stgp)
259 st->dirty.st |= ST_NEW_GEOMETRY_PROGRAM;
260 }
261 else if (target == GL_VERTEX_PROGRAM_ARB) {
262 struct st_vertex_program *stvp = (struct st_vertex_program *) prog;
263
264 st_release_vp_variants(st, stvp);
265 if (!st_translate_vertex_program(st, stvp))
266 return false;
267
268 if (st->vp == stvp)
269 st->dirty.st |= ST_NEW_VERTEX_PROGRAM;
270 }
271 else if (target == GL_TESS_CONTROL_PROGRAM_NV) {
272 struct st_tessctrl_program *sttcp =
273 (struct st_tessctrl_program *) prog;
274
275 st_release_basic_variants(st, sttcp->Base.Base.Target,
276 &sttcp->variants, &sttcp->tgsi);
277 if (!st_translate_tessctrl_program(st, sttcp))
278 return false;
279
280 if (st->tcp == sttcp)
281 st->dirty.st |= ST_NEW_TESSCTRL_PROGRAM;
282 }
283 else if (target == GL_TESS_EVALUATION_PROGRAM_NV) {
284 struct st_tesseval_program *sttep =
285 (struct st_tesseval_program *) prog;
286
287 st_release_basic_variants(st, sttep->Base.Base.Target,
288 &sttep->variants, &sttep->tgsi);
289 if (!st_translate_tesseval_program(st, sttep))
290 return false;
291
292 if (st->tep == sttep)
293 st->dirty.st |= ST_NEW_TESSEVAL_PROGRAM;
294 }
295 else if (target == GL_COMPUTE_PROGRAM_NV) {
296 struct st_compute_program *stcp =
297 (struct st_compute_program *) prog;
298
299 st_release_cp_variants(st, stcp);
300 if (!st_translate_compute_program(st, stcp))
301 return false;
302
303 if (st->cp == stcp)
304 st->dirty_cp.st |= ST_NEW_COMPUTE_PROGRAM;
305 }
306 else if (target == GL_FRAGMENT_SHADER_ATI) {
307 assert(prog);
308
309 struct st_fragment_program *stfp = (struct st_fragment_program *) prog;
310 assert(stfp->ati_fs);
311 assert(stfp->ati_fs->Program == prog);
312
313 st_init_atifs_prog(ctx, prog);
314
315 st_release_fp_variants(st, stfp);
316 if (!st_translate_fragment_program(st, stfp))
317 return false;
318
319 if (st->fp == stfp)
320 st->dirty.st |= ST_NEW_FRAGMENT_PROGRAM;
321 }
322
323 if (ST_DEBUG & DEBUG_PRECOMPILE ||
324 st->shader_has_one_variant[stage])
325 st_precompile_shader_variant(st, prog);
326
327 return GL_TRUE;
328 }
329
330 /**
331 * Called via ctx->Driver.NewATIfs()
332 * Called in glEndFragmentShaderATI()
333 */
334 static struct gl_program *
335 st_new_ati_fs(struct gl_context *ctx, struct ati_fragment_shader *curProg)
336 {
337 struct gl_program *prog = ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB,
338 curProg->Id);
339 struct st_fragment_program *stfp = (struct st_fragment_program *)prog;
340 stfp->ati_fs = curProg;
341 return prog;
342 }
343
344 /**
345 * Plug in the program and shader-related device driver functions.
346 */
347 void
348 st_init_program_functions(struct dd_function_table *functions)
349 {
350 functions->BindProgram = st_bind_program;
351 functions->UseProgram = st_use_program;
352 functions->NewProgram = st_new_program;
353 functions->DeleteProgram = st_delete_program;
354 functions->ProgramStringNotify = st_program_string_notify;
355 functions->NewATIfs = st_new_ati_fs;
356
357 functions->LinkShader = st_link_shader;
358 }