st/mesa: remove st_compute_program in favor of st_common_program
[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_ir.h"
49 #include "st_atifs_to_tgsi.h"
50 #include "st_util.h"
51
52
53 /**
54 * Called via ctx->Driver.NewProgram() to allocate a new vertex or
55 * fragment program.
56 */
57 static struct gl_program *
58 st_new_program(struct gl_context *ctx, GLenum target, GLuint id,
59 bool is_arb_asm)
60 {
61 switch (target) {
62 case GL_VERTEX_PROGRAM_ARB: {
63 struct st_vertex_program *prog = rzalloc(NULL,
64 struct st_vertex_program);
65 return _mesa_init_gl_program(&prog->Base, target, id, is_arb_asm);
66 }
67 case GL_FRAGMENT_PROGRAM_ARB: {
68 struct st_fragment_program *prog = rzalloc(NULL,
69 struct st_fragment_program);
70 return _mesa_init_gl_program(&prog->Base, target, id, is_arb_asm);
71 }
72 case GL_TESS_CONTROL_PROGRAM_NV:
73 case GL_TESS_EVALUATION_PROGRAM_NV:
74 case GL_GEOMETRY_PROGRAM_NV:
75 case GL_COMPUTE_PROGRAM_NV: {
76 struct st_common_program *prog = rzalloc(NULL,
77 struct st_common_program);
78 return _mesa_init_gl_program(&prog->Base, target, id, is_arb_asm);
79 }
80 default:
81 assert(0);
82 return NULL;
83 }
84 }
85
86
87 /**
88 * Called via ctx->Driver.DeleteProgram()
89 */
90 static void
91 st_delete_program(struct gl_context *ctx, struct gl_program *prog)
92 {
93 struct st_context *st = st_context(ctx);
94
95 switch( prog->Target ) {
96 case GL_VERTEX_PROGRAM_ARB:
97 {
98 struct st_vertex_program *stvp = (struct st_vertex_program *) prog;
99 st_release_vp_variants( st, stvp );
100
101 if (stvp->glsl_to_tgsi)
102 free_glsl_to_tgsi_visitor(stvp->glsl_to_tgsi);
103 }
104 break;
105 case GL_TESS_CONTROL_PROGRAM_NV:
106 case GL_TESS_EVALUATION_PROGRAM_NV:
107 case GL_GEOMETRY_PROGRAM_NV:
108 case GL_COMPUTE_PROGRAM_NV:
109 {
110 struct st_common_program *p = st_common_program(prog);
111
112 st_release_basic_variants(st, p);
113
114 if (p->glsl_to_tgsi)
115 free_glsl_to_tgsi_visitor(p->glsl_to_tgsi);
116 }
117 break;
118 case GL_FRAGMENT_PROGRAM_ARB:
119 {
120 struct st_fragment_program *stfp =
121 (struct st_fragment_program *) prog;
122
123 st_release_fp_variants(st, stfp);
124
125 if (stfp->glsl_to_tgsi)
126 free_glsl_to_tgsi_visitor(stfp->glsl_to_tgsi);
127 }
128 break;
129 default:
130 assert(0); /* problem */
131 }
132
133 /* delete base class */
134 _mesa_delete_program( ctx, prog );
135 }
136
137
138 /**
139 * Called via ctx->Driver.ProgramStringNotify()
140 * Called when the program's text/code is changed. We have to free
141 * all shader variants and corresponding gallium shaders when this happens.
142 */
143 static GLboolean
144 st_program_string_notify( struct gl_context *ctx,
145 GLenum target,
146 struct gl_program *prog )
147 {
148 struct st_context *st = st_context(ctx);
149 gl_shader_stage stage = _mesa_program_enum_to_shader_stage(target);
150
151 if (target == GL_FRAGMENT_PROGRAM_ARB ||
152 target == GL_FRAGMENT_SHADER_ATI) {
153 struct st_fragment_program *stfp = (struct st_fragment_program *) prog;
154
155 if (target == GL_FRAGMENT_SHADER_ATI) {
156 assert(stfp->ati_fs);
157 assert(stfp->ati_fs->Program == prog);
158
159 st_init_atifs_prog(ctx, prog);
160 }
161
162 st_release_fp_variants(st, stfp);
163 if (!st_translate_fragment_program(st, stfp))
164 return false;
165
166 if (st->fp == stfp)
167 st->dirty |= stfp->affected_states;
168 }
169 else if (target == GL_GEOMETRY_PROGRAM_NV) {
170 struct st_common_program *stgp = st_common_program(prog);
171
172 st_release_basic_variants(st, stgp);
173 if (!st_translate_geometry_program(st, stgp))
174 return false;
175
176 if (st->gp == stgp)
177 st->dirty |= stgp->affected_states;
178 }
179 else if (target == GL_VERTEX_PROGRAM_ARB) {
180 struct st_vertex_program *stvp = (struct st_vertex_program *) prog;
181
182 st_release_vp_variants(st, stvp);
183 if (!st_translate_vertex_program(st, stvp))
184 return false;
185
186 if (st->vp == stvp)
187 st->dirty |= ST_NEW_VERTEX_PROGRAM(st, stvp);
188 }
189 else if (target == GL_TESS_CONTROL_PROGRAM_NV) {
190 struct st_common_program *sttcp =
191 st_common_program(prog);
192
193 st_release_basic_variants(st, sttcp);
194 if (!st_translate_tessctrl_program(st, sttcp))
195 return false;
196
197 if (st->tcp == sttcp)
198 st->dirty |= sttcp->affected_states;
199 }
200 else if (target == GL_TESS_EVALUATION_PROGRAM_NV) {
201 struct st_common_program *sttep =
202 st_common_program(prog);
203
204 st_release_basic_variants(st, sttep);
205 if (!st_translate_tesseval_program(st, sttep))
206 return false;
207
208 if (st->tep == sttep)
209 st->dirty |= sttep->affected_states;
210 }
211 else if (target == GL_COMPUTE_PROGRAM_NV) {
212 struct st_common_program *stcp =
213 (struct st_common_program *) prog;
214
215 st_release_basic_variants(st, stcp);
216 if (!st_translate_compute_program(st, stcp))
217 return false;
218
219 if (st->cp == stcp)
220 st->dirty |= stcp->affected_states;
221 }
222
223 if (ST_DEBUG & DEBUG_PRECOMPILE ||
224 st->shader_has_one_variant[stage])
225 st_precompile_shader_variant(st, prog);
226
227 return GL_TRUE;
228 }
229
230 /**
231 * Called via ctx->Driver.NewATIfs()
232 * Called in glEndFragmentShaderATI()
233 */
234 static struct gl_program *
235 st_new_ati_fs(struct gl_context *ctx, struct ati_fragment_shader *curProg)
236 {
237 struct gl_program *prog = ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB,
238 curProg->Id, true);
239 struct st_fragment_program *stfp = (struct st_fragment_program *)prog;
240 stfp->ati_fs = curProg;
241 return prog;
242 }
243
244 static void
245 st_max_shader_compiler_threads(struct gl_context *ctx, unsigned count)
246 {
247 struct pipe_screen *screen = st_context(ctx)->pipe->screen;
248
249 if (screen->set_max_shader_compiler_threads)
250 screen->set_max_shader_compiler_threads(screen, count);
251 }
252
253 static bool
254 st_get_shader_program_completion_status(struct gl_context *ctx,
255 struct gl_shader_program *shprog)
256 {
257 struct pipe_screen *screen = st_context(ctx)->pipe->screen;
258
259 if (!screen->is_parallel_shader_compilation_finished)
260 return true;
261
262 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
263 struct gl_linked_shader *linked = shprog->_LinkedShaders[i];
264 void *sh = NULL;
265
266 if (!linked || !linked->Program)
267 continue;
268
269 switch (i) {
270 case MESA_SHADER_VERTEX:
271 if (st_vertex_program(linked->Program)->variants)
272 sh = st_vertex_program(linked->Program)->variants->driver_shader;
273 break;
274 case MESA_SHADER_FRAGMENT:
275 if (st_fragment_program(linked->Program)->variants)
276 sh = st_fragment_program(linked->Program)->variants->driver_shader;
277 break;
278 case MESA_SHADER_TESS_CTRL:
279 case MESA_SHADER_TESS_EVAL:
280 case MESA_SHADER_GEOMETRY:
281 case MESA_SHADER_COMPUTE:
282 if (st_common_program(linked->Program)->variants)
283 sh = st_common_program(linked->Program)->variants->driver_shader;
284 break;
285 }
286
287 unsigned type = pipe_shader_type_from_mesa(i);
288
289 if (sh &&
290 !screen->is_parallel_shader_compilation_finished(screen, sh, type))
291 return false;
292 }
293 return true;
294 }
295
296 /**
297 * Plug in the program and shader-related device driver functions.
298 */
299 void
300 st_init_program_functions(struct dd_function_table *functions)
301 {
302 functions->NewProgram = st_new_program;
303 functions->DeleteProgram = st_delete_program;
304 functions->ProgramStringNotify = st_program_string_notify;
305 functions->NewATIfs = st_new_ati_fs;
306 functions->LinkShader = st_link_shader;
307 functions->SetMaxShaderCompilerThreads = st_max_shader_compiler_threads;
308 functions->GetShaderProgramCompletionStatus =
309 st_get_shader_program_completion_status;
310 }