st/mesa: merge st_fragment_program into 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_TESS_CONTROL_PROGRAM_NV:
68 case GL_TESS_EVALUATION_PROGRAM_NV:
69 case GL_GEOMETRY_PROGRAM_NV:
70 case GL_FRAGMENT_PROGRAM_ARB:
71 case GL_COMPUTE_PROGRAM_NV: {
72 struct st_common_program *prog = rzalloc(NULL,
73 struct st_common_program);
74 return _mesa_init_gl_program(&prog->Base, target, id, is_arb_asm);
75 }
76 default:
77 assert(0);
78 return NULL;
79 }
80 }
81
82
83 /**
84 * Called via ctx->Driver.DeleteProgram()
85 */
86 static void
87 st_delete_program(struct gl_context *ctx, struct gl_program *prog)
88 {
89 struct st_context *st = st_context(ctx);
90
91 switch( prog->Target ) {
92 case GL_VERTEX_PROGRAM_ARB:
93 {
94 struct st_vertex_program *stvp = (struct st_vertex_program *) prog;
95 st_release_vp_variants( st, stvp );
96
97 if (stvp->glsl_to_tgsi)
98 free_glsl_to_tgsi_visitor(stvp->glsl_to_tgsi);
99 }
100 break;
101 case GL_TESS_CONTROL_PROGRAM_NV:
102 case GL_TESS_EVALUATION_PROGRAM_NV:
103 case GL_GEOMETRY_PROGRAM_NV:
104 case GL_FRAGMENT_PROGRAM_ARB:
105 case GL_COMPUTE_PROGRAM_NV:
106 {
107 struct st_common_program *p = st_common_program(prog);
108
109 if (prog->Target == GL_FRAGMENT_PROGRAM_ARB)
110 st_release_fp_variants(st, p);
111 else
112 st_release_common_variants(st, p);
113
114 if (p->glsl_to_tgsi)
115 free_glsl_to_tgsi_visitor(p->glsl_to_tgsi);
116 }
117 break;
118 default:
119 assert(0); /* problem */
120 }
121
122 /* delete base class */
123 _mesa_delete_program( ctx, prog );
124 }
125
126
127 /**
128 * Called via ctx->Driver.ProgramStringNotify()
129 * Called when the program's text/code is changed. We have to free
130 * all shader variants and corresponding gallium shaders when this happens.
131 */
132 static GLboolean
133 st_program_string_notify( struct gl_context *ctx,
134 GLenum target,
135 struct gl_program *prog )
136 {
137 struct st_context *st = st_context(ctx);
138 gl_shader_stage stage = _mesa_program_enum_to_shader_stage(target);
139
140 if (target == GL_FRAGMENT_PROGRAM_ARB ||
141 target == GL_FRAGMENT_SHADER_ATI) {
142 struct st_common_program *stfp = (struct st_common_program *) prog;
143
144 if (target == GL_FRAGMENT_SHADER_ATI) {
145 assert(stfp->ati_fs);
146 assert(stfp->ati_fs->Program == prog);
147
148 st_init_atifs_prog(ctx, prog);
149 }
150
151 st_release_fp_variants(st, stfp);
152 if (!stfp->shader_program && /* not GLSL->NIR */
153 !st_translate_fragment_program(st, stfp))
154 return false;
155
156 if (st->fp == stfp)
157 st->dirty |= stfp->affected_states;
158 } else if (target == GL_VERTEX_PROGRAM_ARB) {
159 struct st_vertex_program *stvp = (struct st_vertex_program *) prog;
160
161 st_release_vp_variants(st, stvp);
162 if (!stvp->shader_program && /* not GLSL->NIR */
163 !st_translate_vertex_program(st, stvp))
164 return false;
165
166 if (st->vp == stvp)
167 st->dirty |= ST_NEW_VERTEX_PROGRAM(st, stvp);
168 } else {
169 struct st_common_program *stcp = st_common_program(prog);
170
171 st_release_common_variants(st, stcp);
172 if (!stcp->shader_program && /* not GLSL->NIR */
173 !st_translate_common_program(st, stcp))
174 return false;
175
176 if ((prog->info.stage == MESA_SHADER_TESS_CTRL && st->tcp == stcp) ||
177 (prog->info.stage == MESA_SHADER_TESS_EVAL && st->tep == stcp) ||
178 (prog->info.stage == MESA_SHADER_GEOMETRY && st->gp == stcp) ||
179 (prog->info.stage == MESA_SHADER_COMPUTE && st->cp == stcp))
180 st->dirty |= stcp->affected_states;
181 }
182
183 if (ST_DEBUG & DEBUG_PRECOMPILE ||
184 st->shader_has_one_variant[stage])
185 st_precompile_shader_variant(st, prog);
186
187 return GL_TRUE;
188 }
189
190 /**
191 * Called via ctx->Driver.NewATIfs()
192 * Called in glEndFragmentShaderATI()
193 */
194 static struct gl_program *
195 st_new_ati_fs(struct gl_context *ctx, struct ati_fragment_shader *curProg)
196 {
197 struct gl_program *prog = ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB,
198 curProg->Id, true);
199 struct st_common_program *stfp = (struct st_common_program *)prog;
200 stfp->ati_fs = curProg;
201 return prog;
202 }
203
204 static void
205 st_max_shader_compiler_threads(struct gl_context *ctx, unsigned count)
206 {
207 struct pipe_screen *screen = st_context(ctx)->pipe->screen;
208
209 if (screen->set_max_shader_compiler_threads)
210 screen->set_max_shader_compiler_threads(screen, count);
211 }
212
213 static bool
214 st_get_shader_program_completion_status(struct gl_context *ctx,
215 struct gl_shader_program *shprog)
216 {
217 struct pipe_screen *screen = st_context(ctx)->pipe->screen;
218
219 if (!screen->is_parallel_shader_compilation_finished)
220 return true;
221
222 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
223 struct gl_linked_shader *linked = shprog->_LinkedShaders[i];
224 void *sh = NULL;
225
226 if (!linked || !linked->Program)
227 continue;
228
229 switch (i) {
230 case MESA_SHADER_VERTEX:
231 if (st_vertex_program(linked->Program)->variants)
232 sh = st_vertex_program(linked->Program)->variants->driver_shader;
233 break;
234 case MESA_SHADER_FRAGMENT:
235 if (st_common_program(linked->Program)->fp_variants)
236 sh = st_common_program(linked->Program)->fp_variants->driver_shader;
237 break;
238 case MESA_SHADER_TESS_CTRL:
239 case MESA_SHADER_TESS_EVAL:
240 case MESA_SHADER_GEOMETRY:
241 case MESA_SHADER_COMPUTE:
242 if (st_common_program(linked->Program)->variants)
243 sh = st_common_program(linked->Program)->variants->driver_shader;
244 break;
245 }
246
247 unsigned type = pipe_shader_type_from_mesa(i);
248
249 if (sh &&
250 !screen->is_parallel_shader_compilation_finished(screen, sh, type))
251 return false;
252 }
253 return true;
254 }
255
256 /**
257 * Plug in the program and shader-related device driver functions.
258 */
259 void
260 st_init_program_functions(struct dd_function_table *functions)
261 {
262 functions->NewProgram = st_new_program;
263 functions->DeleteProgram = st_delete_program;
264 functions->ProgramStringNotify = st_program_string_notify;
265 functions->NewATIfs = st_new_ati_fs;
266 functions->LinkShader = st_link_shader;
267 functions->SetMaxShaderCompilerThreads = st_max_shader_compiler_threads;
268 functions->GetShaderProgramCompletionStatus =
269 st_get_shader_program_completion_status;
270 }