program: remove _mesa_init_*_program wrappers
[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
50
51
52 /**
53 * Called via ctx->Driver.BindProgram() to bind an ARB vertex or
54 * fragment program.
55 */
56 static void
57 st_bind_program(struct gl_context *ctx, GLenum target, struct gl_program *prog)
58 {
59 struct st_context *st = st_context(ctx);
60
61 switch (target) {
62 case GL_VERTEX_PROGRAM_ARB:
63 st->dirty.st |= ST_NEW_VERTEX_PROGRAM;
64 break;
65 case GL_FRAGMENT_PROGRAM_ARB:
66 st->dirty.st |= ST_NEW_FRAGMENT_PROGRAM;
67 break;
68 case GL_GEOMETRY_PROGRAM_NV:
69 st->dirty.st |= ST_NEW_GEOMETRY_PROGRAM;
70 break;
71 case GL_TESS_CONTROL_PROGRAM_NV:
72 st->dirty.st |= ST_NEW_TESSCTRL_PROGRAM;
73 break;
74 case GL_TESS_EVALUATION_PROGRAM_NV:
75 st->dirty.st |= ST_NEW_TESSEVAL_PROGRAM;
76 break;
77 }
78 }
79
80
81 /**
82 * Called via ctx->Driver.UseProgram() to bind a linked GLSL program
83 * (vertex shader + fragment shader).
84 */
85 static void
86 st_use_program(struct gl_context *ctx, struct gl_shader_program *shProg)
87 {
88 struct st_context *st = st_context(ctx);
89
90 st->dirty.st |= ST_NEW_FRAGMENT_PROGRAM;
91 st->dirty.st |= ST_NEW_VERTEX_PROGRAM;
92 st->dirty.st |= ST_NEW_GEOMETRY_PROGRAM;
93 st->dirty.st |= ST_NEW_TESSCTRL_PROGRAM;
94 st->dirty.st |= ST_NEW_TESSEVAL_PROGRAM;
95 }
96
97
98 /**
99 * Called via ctx->Driver.NewProgram() to allocate a new vertex or
100 * fragment program.
101 */
102 static struct gl_program *
103 st_new_program(struct gl_context *ctx, GLenum target, GLuint id)
104 {
105 struct gl_program *prog;
106
107 switch (target) {
108 case GL_VERTEX_PROGRAM_ARB:
109 prog = (struct gl_program*)ST_CALLOC_STRUCT(st_vertex_program);
110 break;
111 case GL_FRAGMENT_PROGRAM_ARB:
112 prog = (struct gl_program*)ST_CALLOC_STRUCT(st_fragment_program);
113 break;
114 case GL_GEOMETRY_PROGRAM_NV:
115 prog = (struct gl_program*)ST_CALLOC_STRUCT(st_geometry_program);
116 break;
117 case GL_TESS_CONTROL_PROGRAM_NV:
118 prog = (struct gl_program*)ST_CALLOC_STRUCT(st_tessctrl_program);
119 break;
120 case GL_TESS_EVALUATION_PROGRAM_NV:
121 prog = (struct gl_program*)ST_CALLOC_STRUCT(st_tesseval_program);
122 break;
123 default:
124 assert(0);
125 return NULL;
126 }
127 return _mesa_init_gl_program(prog, target, id);
128 }
129
130
131 /**
132 * Called via ctx->Driver.DeleteProgram()
133 */
134 static void
135 st_delete_program(struct gl_context *ctx, struct gl_program *prog)
136 {
137 struct st_context *st = st_context(ctx);
138
139 switch( prog->Target ) {
140 case GL_VERTEX_PROGRAM_ARB:
141 {
142 struct st_vertex_program *stvp = (struct st_vertex_program *) prog;
143 st_release_vp_variants( st, stvp );
144
145 if (stvp->glsl_to_tgsi)
146 free_glsl_to_tgsi_visitor(stvp->glsl_to_tgsi);
147 }
148 break;
149 case GL_GEOMETRY_PROGRAM_NV:
150 {
151 struct st_geometry_program *stgp =
152 (struct st_geometry_program *) prog;
153
154 st_release_gp_variants(st, stgp);
155
156 if (stgp->glsl_to_tgsi)
157 free_glsl_to_tgsi_visitor(stgp->glsl_to_tgsi);
158 }
159 break;
160 case GL_FRAGMENT_PROGRAM_ARB:
161 {
162 struct st_fragment_program *stfp =
163 (struct st_fragment_program *) prog;
164
165 st_release_fp_variants(st, stfp);
166
167 if (stfp->glsl_to_tgsi)
168 free_glsl_to_tgsi_visitor(stfp->glsl_to_tgsi);
169 }
170 break;
171 case GL_TESS_CONTROL_PROGRAM_NV:
172 {
173 struct st_tessctrl_program *sttcp =
174 (struct st_tessctrl_program *) prog;
175
176 st_release_tcp_variants(st, sttcp);
177
178 if (sttcp->glsl_to_tgsi)
179 free_glsl_to_tgsi_visitor(sttcp->glsl_to_tgsi);
180 }
181 break;
182 case GL_TESS_EVALUATION_PROGRAM_NV:
183 {
184 struct st_tesseval_program *sttep =
185 (struct st_tesseval_program *) prog;
186
187 st_release_tep_variants(st, sttep);
188
189 if (sttep->glsl_to_tgsi)
190 free_glsl_to_tgsi_visitor(sttep->glsl_to_tgsi);
191 }
192 break;
193 default:
194 assert(0); /* problem */
195 }
196
197 /* delete base class */
198 _mesa_delete_program( ctx, prog );
199 }
200
201
202 /**
203 * Called via ctx->Driver.IsProgramNative()
204 */
205 static GLboolean
206 st_is_program_native(struct gl_context *ctx,
207 GLenum target,
208 struct gl_program *prog)
209 {
210 return GL_TRUE;
211 }
212
213
214 /**
215 * Called via ctx->Driver.ProgramStringNotify()
216 * Called when the program's text/code is changed. We have to free
217 * all shader variants and corresponding gallium shaders when this happens.
218 */
219 static GLboolean
220 st_program_string_notify( struct gl_context *ctx,
221 GLenum target,
222 struct gl_program *prog )
223 {
224 struct st_context *st = st_context(ctx);
225
226 if (target == GL_FRAGMENT_PROGRAM_ARB) {
227 struct st_fragment_program *stfp = (struct st_fragment_program *) prog;
228
229 st_release_fp_variants(st, stfp);
230 if (!st_translate_fragment_program(st, stfp))
231 return false;
232
233 if (st->fp == stfp)
234 st->dirty.st |= ST_NEW_FRAGMENT_PROGRAM;
235 }
236 else if (target == GL_GEOMETRY_PROGRAM_NV) {
237 struct st_geometry_program *stgp = (struct st_geometry_program *) prog;
238
239 st_release_gp_variants(st, stgp);
240 if (!st_translate_geometry_program(st, stgp))
241 return false;
242
243 if (st->gp == stgp)
244 st->dirty.st |= ST_NEW_GEOMETRY_PROGRAM;
245 }
246 else if (target == GL_VERTEX_PROGRAM_ARB) {
247 struct st_vertex_program *stvp = (struct st_vertex_program *) prog;
248
249 st_release_vp_variants(st, stvp);
250 if (!st_translate_vertex_program(st, stvp))
251 return false;
252
253 if (st->vp == stvp)
254 st->dirty.st |= ST_NEW_VERTEX_PROGRAM;
255 }
256 else if (target == GL_TESS_CONTROL_PROGRAM_NV) {
257 struct st_tessctrl_program *sttcp =
258 (struct st_tessctrl_program *) prog;
259
260 st_release_tcp_variants(st, sttcp);
261 if (!st_translate_tessctrl_program(st, sttcp))
262 return false;
263
264 if (st->tcp == sttcp)
265 st->dirty.st |= ST_NEW_TESSCTRL_PROGRAM;
266 }
267 else if (target == GL_TESS_EVALUATION_PROGRAM_NV) {
268 struct st_tesseval_program *sttep =
269 (struct st_tesseval_program *) prog;
270
271 st_release_tep_variants(st, sttep);
272 if (!st_translate_tesseval_program(st, sttep))
273 return false;
274
275 if (st->tep == sttep)
276 st->dirty.st |= ST_NEW_TESSEVAL_PROGRAM;
277 }
278
279 if (ST_DEBUG & DEBUG_PRECOMPILE)
280 st_precompile_shader_variant(st, prog);
281
282 /* XXX check if program is legal, within limits */
283 return GL_TRUE;
284 }
285
286
287 /**
288 * Plug in the program and shader-related device driver functions.
289 */
290 void
291 st_init_program_functions(struct dd_function_table *functions)
292 {
293 functions->BindProgram = st_bind_program;
294 functions->UseProgram = st_use_program;
295 functions->NewProgram = st_new_program;
296 functions->DeleteProgram = st_delete_program;
297 functions->IsProgramNative = st_is_program_native;
298 functions->ProgramStringNotify = st_program_string_notify;
299
300 functions->LinkShader = st_link_shader;
301 }