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