Merge branch 'glsl-to-tgsi'
[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 "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_program.h"
45 #include "st_mesa_to_tgsi.h"
46 #include "st_cb_program.h"
47 #include "st_glsl_to_tgsi.h"
48
49
50
51 /**
52 * Called via ctx->Driver.BindProgram() to bind an ARB vertex or
53 * fragment program.
54 */
55 static void
56 st_bind_program(struct gl_context *ctx, GLenum target, struct gl_program *prog)
57 {
58 struct st_context *st = st_context(ctx);
59
60 switch (target) {
61 case GL_VERTEX_PROGRAM_ARB:
62 st->dirty.st |= ST_NEW_VERTEX_PROGRAM;
63 break;
64 case GL_FRAGMENT_PROGRAM_ARB:
65 st->dirty.st |= ST_NEW_FRAGMENT_PROGRAM;
66 break;
67 case MESA_GEOMETRY_PROGRAM:
68 st->dirty.st |= ST_NEW_GEOMETRY_PROGRAM;
69 break;
70 }
71 }
72
73
74 /**
75 * Called via ctx->Driver.UseProgram() to bind a linked GLSL program
76 * (vertex shader + fragment shader).
77 */
78 static void
79 st_use_program(struct gl_context *ctx, struct gl_shader_program *shProg)
80 {
81 struct st_context *st = st_context(ctx);
82
83 st->dirty.st |= ST_NEW_FRAGMENT_PROGRAM;
84 st->dirty.st |= ST_NEW_VERTEX_PROGRAM;
85 st->dirty.st |= ST_NEW_GEOMETRY_PROGRAM;
86 }
87
88
89 /**
90 * Called via ctx->Driver.NewProgram() to allocate a new vertex or
91 * fragment program.
92 */
93 static struct gl_program *
94 st_new_program(struct gl_context *ctx, GLenum target, GLuint id)
95 {
96 switch (target) {
97 case GL_VERTEX_PROGRAM_ARB: {
98 struct st_vertex_program *prog = ST_CALLOC_STRUCT(st_vertex_program);
99 return _mesa_init_vertex_program(ctx, &prog->Base, target, id);
100 }
101
102 case GL_FRAGMENT_PROGRAM_ARB:
103 case GL_FRAGMENT_PROGRAM_NV: {
104 struct st_fragment_program *prog = ST_CALLOC_STRUCT(st_fragment_program);
105 return _mesa_init_fragment_program(ctx, &prog->Base, target, id);
106 }
107
108 case MESA_GEOMETRY_PROGRAM: {
109 struct st_geometry_program *prog = ST_CALLOC_STRUCT(st_geometry_program);
110 return _mesa_init_geometry_program(ctx, &prog->Base, target, id);
111 }
112
113 default:
114 assert(0);
115 return NULL;
116 }
117 }
118
119
120 /**
121 * Called via ctx->Driver.DeleteProgram()
122 */
123 static void
124 st_delete_program(struct gl_context *ctx, struct gl_program *prog)
125 {
126 struct st_context *st = st_context(ctx);
127
128 switch( prog->Target ) {
129 case GL_VERTEX_PROGRAM_ARB:
130 {
131 struct st_vertex_program *stvp = (struct st_vertex_program *) prog;
132 st_release_vp_variants( st, stvp );
133
134 if (stvp->glsl_to_tgsi)
135 free_glsl_to_tgsi_visitor(stvp->glsl_to_tgsi);
136 }
137 break;
138 case MESA_GEOMETRY_PROGRAM:
139 {
140 struct st_geometry_program *stgp =
141 (struct st_geometry_program *) prog;
142
143 st_release_gp_variants(st, stgp);
144
145 if (stgp->glsl_to_tgsi)
146 free_glsl_to_tgsi_visitor(stgp->glsl_to_tgsi);
147
148 if (stgp->tgsi.tokens) {
149 st_free_tokens((void *) stgp->tgsi.tokens);
150 stgp->tgsi.tokens = NULL;
151 }
152 }
153 break;
154 case GL_FRAGMENT_PROGRAM_ARB:
155 {
156 struct st_fragment_program *stfp =
157 (struct st_fragment_program *) prog;
158
159 st_release_fp_variants(st, stfp);
160
161 if (stfp->glsl_to_tgsi)
162 free_glsl_to_tgsi_visitor(stfp->glsl_to_tgsi);
163
164 if (stfp->tgsi.tokens) {
165 st_free_tokens(stfp->tgsi.tokens);
166 stfp->tgsi.tokens = NULL;
167 }
168 }
169 break;
170 default:
171 assert(0); /* problem */
172 }
173
174 /* delete base class */
175 _mesa_delete_program( ctx, prog );
176 }
177
178
179 /**
180 * Called via ctx->Driver.IsProgramNative()
181 */
182 static GLboolean
183 st_is_program_native(struct gl_context *ctx,
184 GLenum target,
185 struct gl_program *prog)
186 {
187 return GL_TRUE;
188 }
189
190
191 /**
192 * Called via ctx->Driver.ProgramStringNotify()
193 * Called when the program's text/code is changed. We have to free
194 * all shader variants and corresponding gallium shaders when this happens.
195 */
196 static GLboolean
197 st_program_string_notify( struct gl_context *ctx,
198 GLenum target,
199 struct gl_program *prog )
200 {
201 struct st_context *st = st_context(ctx);
202
203 if (target == GL_FRAGMENT_PROGRAM_ARB) {
204 struct st_fragment_program *stfp = (struct st_fragment_program *) prog;
205
206 st_release_fp_variants(st, stfp);
207
208 if (stfp->tgsi.tokens) {
209 st_free_tokens(stfp->tgsi.tokens);
210 stfp->tgsi.tokens = NULL;
211 }
212
213 if (st->fp == stfp)
214 st->dirty.st |= ST_NEW_FRAGMENT_PROGRAM;
215 }
216 else if (target == MESA_GEOMETRY_PROGRAM) {
217 struct st_geometry_program *stgp = (struct st_geometry_program *) prog;
218
219 st_release_gp_variants(st, stgp);
220
221 if (stgp->tgsi.tokens) {
222 st_free_tokens((void *) stgp->tgsi.tokens);
223 stgp->tgsi.tokens = NULL;
224 }
225
226 if (st->gp == stgp)
227 st->dirty.st |= ST_NEW_GEOMETRY_PROGRAM;
228 }
229 else if (target == GL_VERTEX_PROGRAM_ARB) {
230 struct st_vertex_program *stvp = (struct st_vertex_program *) prog;
231
232 st_release_vp_variants( st, stvp );
233
234 if (st->vp == stvp)
235 st->dirty.st |= ST_NEW_VERTEX_PROGRAM;
236 }
237
238 /* XXX check if program is legal, within limits */
239 return GL_TRUE;
240 }
241
242
243 /**
244 * Plug in the program and shader-related device driver functions.
245 */
246 void
247 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
256 functions->NewShader = st_new_shader;
257 functions->NewShaderProgram = st_new_shader_program;
258 functions->LinkShader = st_link_shader;
259 }