2eb9c13d929f91a822e59516042d8255241a886b
[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_program *prog = rzalloc(NULL,
64 struct st_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_program *prog = rzalloc(NULL,
73 struct st_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 struct st_program *stp = st_program(prog);
91
92 if (prog->Target == GL_VERTEX_PROGRAM_ARB)
93 st_release_vp_variants(st, stp);
94 if (prog->Target == GL_FRAGMENT_PROGRAM_ARB)
95 st_release_fp_variants(st, stp);
96 else
97 st_release_common_variants(st, stp);
98
99 if (stp->glsl_to_tgsi)
100 free_glsl_to_tgsi_visitor(stp->glsl_to_tgsi);
101
102 /* delete base class */
103 _mesa_delete_program( ctx, prog );
104 }
105
106 /**
107 * Called via ctx->Driver.ProgramStringNotify()
108 * Called when the program's text/code is changed. We have to free
109 * all shader variants and corresponding gallium shaders when this happens.
110 */
111 static GLboolean
112 st_program_string_notify( struct gl_context *ctx,
113 GLenum target,
114 struct gl_program *prog )
115 {
116 struct st_context *st = st_context(ctx);
117 struct st_program *stp = (struct st_program *) prog;
118
119 if (target == GL_FRAGMENT_PROGRAM_ARB ||
120 target == GL_FRAGMENT_SHADER_ATI) {
121 if (target == GL_FRAGMENT_SHADER_ATI) {
122 assert(stp->ati_fs);
123 assert(stp->ati_fs->Program == prog);
124
125 st_init_atifs_prog(ctx, prog);
126 }
127
128 st_release_fp_variants(st, stp);
129 if (!stp->shader_program && /* not GLSL->NIR */
130 !st_translate_fragment_program(st, stp))
131 return false;
132 } else if (target == GL_VERTEX_PROGRAM_ARB) {
133 st_release_vp_variants(st, stp);
134 if (!stp->shader_program && /* not GLSL->NIR */
135 !st_translate_vertex_program(st, stp))
136 return false;
137 } else {
138 st_release_common_variants(st, stp);
139 if (!stp->shader_program && /* not GLSL->NIR */
140 !st_translate_common_program(st, stp))
141 return false;
142 }
143
144 st_finalize_program(st, prog);
145 return GL_TRUE;
146 }
147
148 /**
149 * Called via ctx->Driver.NewATIfs()
150 * Called in glEndFragmentShaderATI()
151 */
152 static struct gl_program *
153 st_new_ati_fs(struct gl_context *ctx, struct ati_fragment_shader *curProg)
154 {
155 struct gl_program *prog = ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB,
156 curProg->Id, true);
157 struct st_program *stfp = (struct st_program *)prog;
158 stfp->ati_fs = curProg;
159 return prog;
160 }
161
162 static void
163 st_max_shader_compiler_threads(struct gl_context *ctx, unsigned count)
164 {
165 struct pipe_screen *screen = st_context(ctx)->pipe->screen;
166
167 if (screen->set_max_shader_compiler_threads)
168 screen->set_max_shader_compiler_threads(screen, count);
169 }
170
171 static bool
172 st_get_shader_program_completion_status(struct gl_context *ctx,
173 struct gl_shader_program *shprog)
174 {
175 struct pipe_screen *screen = st_context(ctx)->pipe->screen;
176
177 if (!screen->is_parallel_shader_compilation_finished)
178 return true;
179
180 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
181 struct gl_linked_shader *linked = shprog->_LinkedShaders[i];
182 void *sh = NULL;
183
184 if (!linked || !linked->Program)
185 continue;
186
187 switch (i) {
188 case MESA_SHADER_VERTEX:
189 if (st_program(linked->Program)->vp_variants)
190 sh = st_program(linked->Program)->vp_variants->driver_shader;
191 break;
192 case MESA_SHADER_FRAGMENT:
193 if (st_program(linked->Program)->fp_variants)
194 sh = st_program(linked->Program)->fp_variants->driver_shader;
195 break;
196 case MESA_SHADER_TESS_CTRL:
197 case MESA_SHADER_TESS_EVAL:
198 case MESA_SHADER_GEOMETRY:
199 case MESA_SHADER_COMPUTE:
200 if (st_program(linked->Program)->variants)
201 sh = st_program(linked->Program)->variants->driver_shader;
202 break;
203 }
204
205 unsigned type = pipe_shader_type_from_mesa(i);
206
207 if (sh &&
208 !screen->is_parallel_shader_compilation_finished(screen, sh, type))
209 return false;
210 }
211 return true;
212 }
213
214 /**
215 * Plug in the program and shader-related device driver functions.
216 */
217 void
218 st_init_program_functions(struct dd_function_table *functions)
219 {
220 functions->NewProgram = st_new_program;
221 functions->DeleteProgram = st_delete_program;
222 functions->ProgramStringNotify = st_program_string_notify;
223 functions->NewATIfs = st_new_ati_fs;
224 functions->LinkShader = st_link_shader;
225 functions->SetMaxShaderCompilerThreads = st_max_shader_compiler_threads;
226 functions->GetShaderProgramCompletionStatus =
227 st_get_shader_program_completion_status;
228 }