Merge branch 'llvm-cliptest-viewport'
[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
48
49 static GLuint SerialNo = 1;
50
51
52 /**
53 * Called via ctx->Driver.BindProgram() to bind an ARB vertex or
54 * fragment program.
55 */
56 static void st_bind_program( struct gl_context *ctx,
57 GLenum target,
58 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 MESA_GEOMETRY_PROGRAM:
70 st->dirty.st |= ST_NEW_GEOMETRY_PROGRAM;
71 break;
72 }
73 }
74
75
76 /**
77 * Called via ctx->Driver.UseProgram() to bind a linked GLSL program
78 * (vertex shader + fragment shader).
79 */
80 static void st_use_program( struct gl_context *ctx, struct gl_shader_program *shProg)
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 st->dirty.st |= ST_NEW_GEOMETRY_PROGRAM;
87 }
88
89
90
91 /**
92 * Called via ctx->Driver.NewProgram() to allocate a new vertex or
93 * fragment program.
94 */
95 static struct gl_program *st_new_program( struct gl_context *ctx,
96 GLenum target,
97 GLuint id )
98 {
99 switch (target) {
100 case GL_VERTEX_PROGRAM_ARB: {
101 struct st_vertex_program *prog = ST_CALLOC_STRUCT(st_vertex_program);
102
103 prog->serialNo = SerialNo++;
104
105 return _mesa_init_vertex_program( ctx,
106 &prog->Base,
107 target,
108 id );
109 }
110
111 case GL_FRAGMENT_PROGRAM_ARB:
112 case GL_FRAGMENT_PROGRAM_NV: {
113 struct st_fragment_program *prog = ST_CALLOC_STRUCT(st_fragment_program);
114
115 prog->serialNo = SerialNo++;
116
117 return _mesa_init_fragment_program( ctx,
118 &prog->Base,
119 target,
120 id );
121 }
122
123 case MESA_GEOMETRY_PROGRAM: {
124 struct st_geometry_program *prog = ST_CALLOC_STRUCT(st_geometry_program);
125
126 prog->serialNo = SerialNo++;
127
128 return _mesa_init_geometry_program( ctx,
129 &prog->Base,
130 target,
131 id );
132 }
133
134 default:
135 assert(0);
136 return NULL;
137 }
138 }
139
140
141 void
142 st_delete_program(struct gl_context *ctx, struct gl_program *prog)
143 {
144 struct st_context *st = st_context(ctx);
145
146 switch( prog->Target ) {
147 case GL_VERTEX_PROGRAM_ARB:
148 {
149 struct st_vertex_program *stvp = (struct st_vertex_program *) prog;
150 st_vp_release_varients( st, stvp );
151 }
152 break;
153 case MESA_GEOMETRY_PROGRAM:
154 {
155 struct st_geometry_program *stgp = (struct st_geometry_program *) prog;
156
157 if (stgp->driver_shader) {
158 cso_delete_geometry_shader(st->cso_context, stgp->driver_shader);
159 stgp->driver_shader = NULL;
160 }
161
162 if (stgp->tgsi.tokens) {
163 st_free_tokens((void *) stgp->tgsi.tokens);
164 stgp->tgsi.tokens = NULL;
165 }
166 }
167 break;
168 case GL_FRAGMENT_PROGRAM_ARB:
169 {
170 struct st_fragment_program *stfp = (struct st_fragment_program *) prog;
171
172 if (stfp->driver_shader) {
173 cso_delete_fragment_shader(st->cso_context, stfp->driver_shader);
174 stfp->driver_shader = NULL;
175 }
176
177 if (stfp->tgsi.tokens) {
178 st_free_tokens(stfp->tgsi.tokens);
179 stfp->tgsi.tokens = NULL;
180 }
181
182 if (stfp->bitmap_program) {
183 struct gl_program *prg = &stfp->bitmap_program->Base.Base;
184 _mesa_reference_program(ctx, &prg, NULL);
185 stfp->bitmap_program = NULL;
186 }
187 }
188 break;
189 default:
190 assert(0); /* problem */
191 }
192
193 /* delete base class */
194 _mesa_delete_program( ctx, prog );
195 }
196
197
198 static GLboolean st_is_program_native( struct gl_context *ctx,
199 GLenum target,
200 struct gl_program *prog )
201 {
202 return GL_TRUE;
203 }
204
205
206 static GLboolean st_program_string_notify( struct gl_context *ctx,
207 GLenum target,
208 struct gl_program *prog )
209 {
210 struct st_context *st = st_context(ctx);
211
212 if (target == GL_FRAGMENT_PROGRAM_ARB) {
213 struct st_fragment_program *stfp = (struct st_fragment_program *) prog;
214
215 stfp->serialNo++;
216
217 if (stfp->driver_shader) {
218 cso_delete_fragment_shader(st->cso_context, stfp->driver_shader);
219 stfp->driver_shader = NULL;
220 }
221
222 if (stfp->tgsi.tokens) {
223 st_free_tokens(stfp->tgsi.tokens);
224 stfp->tgsi.tokens = NULL;
225 }
226
227 if (st->fp == stfp)
228 st->dirty.st |= ST_NEW_FRAGMENT_PROGRAM;
229 }
230 else if (target == MESA_GEOMETRY_PROGRAM) {
231 struct st_geometry_program *stgp = (struct st_geometry_program *) prog;
232
233 stgp->serialNo++;
234
235 if (stgp->driver_shader) {
236 cso_delete_geometry_shader(st->cso_context, stgp->driver_shader);
237 stgp->driver_shader = NULL;
238 }
239
240 if (stgp->tgsi.tokens) {
241 st_free_tokens((void *) stgp->tgsi.tokens);
242 stgp->tgsi.tokens = NULL;
243 }
244
245 if (st->gp == stgp)
246 st->dirty.st |= ST_NEW_GEOMETRY_PROGRAM;
247 }
248 else if (target == GL_VERTEX_PROGRAM_ARB) {
249 struct st_vertex_program *stvp = (struct st_vertex_program *) prog;
250
251 stvp->serialNo++;
252
253 st_vp_release_varients( st, stvp );
254
255 if (st->vp == stvp)
256 st->dirty.st |= ST_NEW_VERTEX_PROGRAM;
257 }
258
259 /* XXX check if program is legal, within limits */
260 return GL_TRUE;
261 }
262
263
264
265 void st_init_program_functions(struct dd_function_table *functions)
266 {
267 functions->BindProgram = st_bind_program;
268 functions->UseProgram = st_use_program;
269 functions->NewProgram = st_new_program;
270 functions->DeleteProgram = st_delete_program;
271 functions->IsProgramNative = st_is_program_native;
272 functions->ProgramStringNotify = st_program_string_notify;
273 }