Merge branch '7.8' into master
[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 "shader/prog_instruction.h"
37 #include "shader/prog_parameter.h"
38 #include "shader/program.h"
39 #include "shader/shader_api.h"
40
41 #include "cso_cache/cso_context.h"
42 #include "draw/draw_context.h"
43
44 #include "st_context.h"
45 #include "st_program.h"
46 #include "st_mesa_to_tgsi.h"
47 #include "st_cb_program.h"
48
49
50 static GLuint SerialNo = 1;
51
52
53 /**
54 * Called via ctx->Driver.BindProgram() to bind an ARB vertex or
55 * fragment program.
56 */
57 static void st_bind_program( GLcontext *ctx,
58 GLenum target,
59 struct gl_program *prog )
60 {
61 struct st_context *st = st_context(ctx);
62
63 switch (target) {
64 case GL_VERTEX_PROGRAM_ARB:
65 st->dirty.st |= ST_NEW_VERTEX_PROGRAM;
66 break;
67 case GL_FRAGMENT_PROGRAM_ARB:
68 st->dirty.st |= ST_NEW_FRAGMENT_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 st_use_program( GLcontext *ctx,
79 GLuint program )
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
86 _mesa_use_program(ctx, 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( GLcontext *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 default:
124 assert(0);
125 return NULL;
126 }
127 }
128
129
130 void
131 st_delete_program(GLcontext *ctx, struct gl_program *prog)
132 {
133 struct st_context *st = st_context(ctx);
134
135 switch( prog->Target ) {
136 case GL_VERTEX_PROGRAM_ARB:
137 {
138 struct st_vertex_program *stvp = (struct st_vertex_program *) prog;
139 st_vp_release_varients( st, stvp );
140 }
141 break;
142 case GL_FRAGMENT_PROGRAM_ARB:
143 {
144 struct st_fragment_program *stfp = (struct st_fragment_program *) prog;
145
146 if (stfp->driver_shader) {
147 cso_delete_fragment_shader(st->cso_context, stfp->driver_shader);
148 stfp->driver_shader = NULL;
149 }
150
151 if (stfp->tgsi.tokens) {
152 st_free_tokens(stfp->tgsi.tokens);
153 stfp->tgsi.tokens = NULL;
154 }
155
156 if (stfp->bitmap_program) {
157 struct gl_program *prg = &stfp->bitmap_program->Base.Base;
158 _mesa_reference_program(ctx, &prg, NULL);
159 stfp->bitmap_program = NULL;
160 }
161 }
162 break;
163 default:
164 assert(0); /* problem */
165 }
166
167 /* delete base class */
168 _mesa_delete_program( ctx, prog );
169 }
170
171
172 static GLboolean st_is_program_native( GLcontext *ctx,
173 GLenum target,
174 struct gl_program *prog )
175 {
176 return GL_TRUE;
177 }
178
179
180 static GLboolean st_program_string_notify( GLcontext *ctx,
181 GLenum target,
182 struct gl_program *prog )
183 {
184 struct st_context *st = st_context(ctx);
185
186 if (target == GL_FRAGMENT_PROGRAM_ARB) {
187 struct st_fragment_program *stfp = (struct st_fragment_program *) prog;
188
189 stfp->serialNo++;
190
191 if (stfp->driver_shader) {
192 cso_delete_fragment_shader(st->cso_context, stfp->driver_shader);
193 stfp->driver_shader = NULL;
194 }
195
196 if (stfp->tgsi.tokens) {
197 st_free_tokens(stfp->tgsi.tokens);
198 stfp->tgsi.tokens = NULL;
199 }
200
201 if (st->fp == stfp)
202 st->dirty.st |= ST_NEW_FRAGMENT_PROGRAM;
203 }
204 else if (target == GL_VERTEX_PROGRAM_ARB) {
205 struct st_vertex_program *stvp = (struct st_vertex_program *) prog;
206
207 stvp->serialNo++;
208
209 st_vp_release_varients( st, stvp );
210
211 if (st->vp == stvp)
212 st->dirty.st |= ST_NEW_VERTEX_PROGRAM;
213 }
214
215 /* XXX check if program is legal, within limits */
216 return GL_TRUE;
217 }
218
219
220
221 void st_init_program_functions(struct dd_function_table *functions)
222 {
223 functions->BindProgram = st_bind_program;
224 functions->UseProgram = st_use_program;
225 functions->NewProgram = st_new_program;
226 functions->DeleteProgram = st_delete_program;
227 functions->IsProgramNative = st_is_program_native;
228 functions->ProgramStringNotify = st_program_string_notify;
229 }