1 /**************************************************************************
3 * Copyright 2003 VMware, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
30 * Keith Whitwell <keithw@vmware.com>
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"
40 #include "cso_cache/cso_context.h"
41 #include "draw/draw_context.h"
43 #include "st_context.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"
54 * Called via ctx->Driver.NewProgram() to allocate a new vertex or
57 static struct gl_program
*
58 st_new_program(struct gl_context
*ctx
, GLenum target
, GLuint id
,
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
);
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
);
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
);
88 * Called via ctx->Driver.DeleteProgram()
91 st_delete_program(struct gl_context
*ctx
, struct gl_program
*prog
)
93 struct st_context
*st
= st_context(ctx
);
95 switch( prog
->Target
) {
96 case GL_VERTEX_PROGRAM_ARB
:
98 struct st_vertex_program
*stvp
= (struct st_vertex_program
*) prog
;
99 st_release_vp_variants( st
, stvp
);
101 if (stvp
->glsl_to_tgsi
)
102 free_glsl_to_tgsi_visitor(stvp
->glsl_to_tgsi
);
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
:
110 struct st_common_program
*p
= st_common_program(prog
);
112 st_release_common_variants(st
, p
);
115 free_glsl_to_tgsi_visitor(p
->glsl_to_tgsi
);
118 case GL_FRAGMENT_PROGRAM_ARB
:
120 struct st_fragment_program
*stfp
=
121 (struct st_fragment_program
*) prog
;
123 st_release_fp_variants(st
, stfp
);
125 if (stfp
->glsl_to_tgsi
)
126 free_glsl_to_tgsi_visitor(stfp
->glsl_to_tgsi
);
130 assert(0); /* problem */
133 /* delete base class */
134 _mesa_delete_program( ctx
, prog
);
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.
144 st_program_string_notify( struct gl_context
*ctx
,
146 struct gl_program
*prog
)
148 struct st_context
*st
= st_context(ctx
);
149 gl_shader_stage stage
= _mesa_program_enum_to_shader_stage(target
);
151 if (target
== GL_FRAGMENT_PROGRAM_ARB
||
152 target
== GL_FRAGMENT_SHADER_ATI
) {
153 struct st_fragment_program
*stfp
= (struct st_fragment_program
*) prog
;
155 if (target
== GL_FRAGMENT_SHADER_ATI
) {
156 assert(stfp
->ati_fs
);
157 assert(stfp
->ati_fs
->Program
== prog
);
159 st_init_atifs_prog(ctx
, prog
);
162 st_release_fp_variants(st
, stfp
);
163 if (!stfp
->shader_program
&& /* not GLSL->NIR */
164 !st_translate_fragment_program(st
, stfp
))
168 st
->dirty
|= stfp
->affected_states
;
169 } else if (target
== GL_VERTEX_PROGRAM_ARB
) {
170 struct st_vertex_program
*stvp
= (struct st_vertex_program
*) prog
;
172 st_release_vp_variants(st
, stvp
);
173 if (!stvp
->shader_program
&& /* not GLSL->NIR */
174 !st_translate_vertex_program(st
, stvp
))
178 st
->dirty
|= ST_NEW_VERTEX_PROGRAM(st
, stvp
);
180 struct st_common_program
*stcp
= st_common_program(prog
);
182 st_release_common_variants(st
, stcp
);
183 if (!stcp
->shader_program
&& /* not GLSL->NIR */
184 !st_translate_common_program(st
, stcp
))
187 if ((prog
->info
.stage
== MESA_SHADER_TESS_CTRL
&& st
->tcp
== stcp
) ||
188 (prog
->info
.stage
== MESA_SHADER_TESS_EVAL
&& st
->tep
== stcp
) ||
189 (prog
->info
.stage
== MESA_SHADER_GEOMETRY
&& st
->gp
== stcp
) ||
190 (prog
->info
.stage
== MESA_SHADER_COMPUTE
&& st
->cp
== stcp
))
191 st
->dirty
|= stcp
->affected_states
;
194 if (ST_DEBUG
& DEBUG_PRECOMPILE
||
195 st
->shader_has_one_variant
[stage
])
196 st_precompile_shader_variant(st
, prog
);
202 * Called via ctx->Driver.NewATIfs()
203 * Called in glEndFragmentShaderATI()
205 static struct gl_program
*
206 st_new_ati_fs(struct gl_context
*ctx
, struct ati_fragment_shader
*curProg
)
208 struct gl_program
*prog
= ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
,
210 struct st_fragment_program
*stfp
= (struct st_fragment_program
*)prog
;
211 stfp
->ati_fs
= curProg
;
216 st_max_shader_compiler_threads(struct gl_context
*ctx
, unsigned count
)
218 struct pipe_screen
*screen
= st_context(ctx
)->pipe
->screen
;
220 if (screen
->set_max_shader_compiler_threads
)
221 screen
->set_max_shader_compiler_threads(screen
, count
);
225 st_get_shader_program_completion_status(struct gl_context
*ctx
,
226 struct gl_shader_program
*shprog
)
228 struct pipe_screen
*screen
= st_context(ctx
)->pipe
->screen
;
230 if (!screen
->is_parallel_shader_compilation_finished
)
233 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
234 struct gl_linked_shader
*linked
= shprog
->_LinkedShaders
[i
];
237 if (!linked
|| !linked
->Program
)
241 case MESA_SHADER_VERTEX
:
242 if (st_vertex_program(linked
->Program
)->variants
)
243 sh
= st_vertex_program(linked
->Program
)->variants
->driver_shader
;
245 case MESA_SHADER_FRAGMENT
:
246 if (st_fragment_program(linked
->Program
)->variants
)
247 sh
= st_fragment_program(linked
->Program
)->variants
->driver_shader
;
249 case MESA_SHADER_TESS_CTRL
:
250 case MESA_SHADER_TESS_EVAL
:
251 case MESA_SHADER_GEOMETRY
:
252 case MESA_SHADER_COMPUTE
:
253 if (st_common_program(linked
->Program
)->variants
)
254 sh
= st_common_program(linked
->Program
)->variants
->driver_shader
;
258 unsigned type
= pipe_shader_type_from_mesa(i
);
261 !screen
->is_parallel_shader_compilation_finished(screen
, sh
, type
))
268 * Plug in the program and shader-related device driver functions.
271 st_init_program_functions(struct dd_function_table
*functions
)
273 functions
->NewProgram
= st_new_program
;
274 functions
->DeleteProgram
= st_delete_program
;
275 functions
->ProgramStringNotify
= st_program_string_notify
;
276 functions
->NewATIfs
= st_new_ati_fs
;
277 functions
->LinkShader
= st_link_shader
;
278 functions
->SetMaxShaderCompilerThreads
= st_max_shader_compiler_threads
;
279 functions
->GetShaderProgramCompletionStatus
=
280 st_get_shader_program_completion_status
;