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