Merge remote-tracking branch 'mesa-public/master' into vulkan
[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 switch (target) {
106 case GL_VERTEX_PROGRAM_ARB: {
107 struct st_vertex_program *prog = ST_CALLOC_STRUCT(st_vertex_program);
108 return _mesa_init_gl_program(&prog->Base.Base, target, id);
109 }
110 case GL_FRAGMENT_PROGRAM_ARB: {
111 struct st_fragment_program *prog = ST_CALLOC_STRUCT(st_fragment_program);
112 return _mesa_init_gl_program(&prog->Base.Base, target, id);
113 }
114 case GL_GEOMETRY_PROGRAM_NV: {
115 struct st_geometry_program *prog = ST_CALLOC_STRUCT(st_geometry_program);
116 return _mesa_init_gl_program(&prog->Base.Base, target, id);
117 }
118 case GL_TESS_CONTROL_PROGRAM_NV: {
119 struct st_tessctrl_program *prog = ST_CALLOC_STRUCT(st_tessctrl_program);
120 return _mesa_init_gl_program(&prog->Base.Base, target, id);
121 }
122 case GL_TESS_EVALUATION_PROGRAM_NV: {
123 struct st_tesseval_program *prog = ST_CALLOC_STRUCT(st_tesseval_program);
124 return _mesa_init_gl_program(&prog->Base.Base, target, id);
125 }
126 default:
127 assert(0);
128 return NULL;
129 }
130 }
131
132
133 /**
134 * Called via ctx->Driver.DeleteProgram()
135 */
136 static void
137 st_delete_program(struct gl_context *ctx, struct gl_program *prog)
138 {
139 struct st_context *st = st_context(ctx);
140
141 switch( prog->Target ) {
142 case GL_VERTEX_PROGRAM_ARB:
143 {
144 struct st_vertex_program *stvp = (struct st_vertex_program *) prog;
145 st_release_vp_variants( st, stvp );
146
147 if (stvp->glsl_to_tgsi)
148 free_glsl_to_tgsi_visitor(stvp->glsl_to_tgsi);
149 }
150 break;
151 case GL_GEOMETRY_PROGRAM_NV:
152 {
153 struct st_geometry_program *stgp =
154 (struct st_geometry_program *) prog;
155
156 st_release_gp_variants(st, stgp);
157
158 if (stgp->glsl_to_tgsi)
159 free_glsl_to_tgsi_visitor(stgp->glsl_to_tgsi);
160 }
161 break;
162 case GL_FRAGMENT_PROGRAM_ARB:
163 {
164 struct st_fragment_program *stfp =
165 (struct st_fragment_program *) prog;
166
167 st_release_fp_variants(st, stfp);
168
169 if (stfp->glsl_to_tgsi)
170 free_glsl_to_tgsi_visitor(stfp->glsl_to_tgsi);
171 }
172 break;
173 case GL_TESS_CONTROL_PROGRAM_NV:
174 {
175 struct st_tessctrl_program *sttcp =
176 (struct st_tessctrl_program *) prog;
177
178 st_release_tcp_variants(st, sttcp);
179
180 if (sttcp->glsl_to_tgsi)
181 free_glsl_to_tgsi_visitor(sttcp->glsl_to_tgsi);
182 }
183 break;
184 case GL_TESS_EVALUATION_PROGRAM_NV:
185 {
186 struct st_tesseval_program *sttep =
187 (struct st_tesseval_program *) prog;
188
189 st_release_tep_variants(st, sttep);
190
191 if (sttep->glsl_to_tgsi)
192 free_glsl_to_tgsi_visitor(sttep->glsl_to_tgsi);
193 }
194 break;
195 default:
196 assert(0); /* problem */
197 }
198
199 /* delete base class */
200 _mesa_delete_program( ctx, prog );
201 }
202
203
204 /**
205 * Called via ctx->Driver.IsProgramNative()
206 */
207 static GLboolean
208 st_is_program_native(struct gl_context *ctx,
209 GLenum target,
210 struct gl_program *prog)
211 {
212 return GL_TRUE;
213 }
214
215
216 /**
217 * Called via ctx->Driver.ProgramStringNotify()
218 * Called when the program's text/code is changed. We have to free
219 * all shader variants and corresponding gallium shaders when this happens.
220 */
221 static GLboolean
222 st_program_string_notify( struct gl_context *ctx,
223 GLenum target,
224 struct gl_program *prog )
225 {
226 struct st_context *st = st_context(ctx);
227
228 if (target == GL_FRAGMENT_PROGRAM_ARB) {
229 struct st_fragment_program *stfp = (struct st_fragment_program *) prog;
230
231 st_release_fp_variants(st, stfp);
232 if (!st_translate_fragment_program(st, stfp))
233 return false;
234
235 if (st->fp == stfp)
236 st->dirty.st |= ST_NEW_FRAGMENT_PROGRAM;
237 }
238 else if (target == GL_GEOMETRY_PROGRAM_NV) {
239 struct st_geometry_program *stgp = (struct st_geometry_program *) prog;
240
241 st_release_gp_variants(st, stgp);
242 if (!st_translate_geometry_program(st, stgp))
243 return false;
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 st_release_vp_variants(st, stvp);
252 if (!st_translate_vertex_program(st, stvp))
253 return false;
254
255 if (st->vp == stvp)
256 st->dirty.st |= ST_NEW_VERTEX_PROGRAM;
257 }
258 else if (target == GL_TESS_CONTROL_PROGRAM_NV) {
259 struct st_tessctrl_program *sttcp =
260 (struct st_tessctrl_program *) prog;
261
262 st_release_tcp_variants(st, sttcp);
263 if (!st_translate_tessctrl_program(st, sttcp))
264 return false;
265
266 if (st->tcp == sttcp)
267 st->dirty.st |= ST_NEW_TESSCTRL_PROGRAM;
268 }
269 else if (target == GL_TESS_EVALUATION_PROGRAM_NV) {
270 struct st_tesseval_program *sttep =
271 (struct st_tesseval_program *) prog;
272
273 st_release_tep_variants(st, sttep);
274 if (!st_translate_tesseval_program(st, sttep))
275 return false;
276
277 if (st->tep == sttep)
278 st->dirty.st |= ST_NEW_TESSEVAL_PROGRAM;
279 }
280
281 if (ST_DEBUG & DEBUG_PRECOMPILE)
282 st_precompile_shader_variant(st, prog);
283
284 /* XXX check if program is legal, within limits */
285 return GL_TRUE;
286 }
287
288
289 /**
290 * Plug in the program and shader-related device driver functions.
291 */
292 void
293 st_init_program_functions(struct dd_function_table *functions)
294 {
295 functions->BindProgram = st_bind_program;
296 functions->UseProgram = st_use_program;
297 functions->NewProgram = st_new_program;
298 functions->DeleteProgram = st_delete_program;
299 functions->IsProgramNative = st_is_program_native;
300 functions->ProgramStringNotify = st_program_string_notify;
301
302 functions->LinkShader = st_link_shader;
303 }