Merge branch 'gallium-drm-driver-drescriptor'
[mesa.git] / src / mesa / state_tracker / st_cb_program.c
1 /**************************************************************************
2 *
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
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 TUNGSTEN GRAPHICS 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 <keith@tungstengraphics.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_program.h"
45 #include "st_mesa_to_tgsi.h"
46 #include "st_cb_program.h"
47
48
49 static GLuint SerialNo = 1;
50
51
52 /**
53 * Called via ctx->Driver.BindProgram() to bind an ARB vertex or
54 * fragment program.
55 */
56 static void st_bind_program( GLcontext *ctx,
57 GLenum target,
58 struct gl_program *prog )
59 {
60 struct st_context *st = st_context(ctx);
61
62 switch (target) {
63 case GL_VERTEX_PROGRAM_ARB:
64 st->dirty.st |= ST_NEW_VERTEX_PROGRAM;
65 break;
66 case GL_FRAGMENT_PROGRAM_ARB:
67 st->dirty.st |= ST_NEW_FRAGMENT_PROGRAM;
68 break;
69 }
70 }
71
72
73 /**
74 * Called via ctx->Driver.UseProgram() to bind a linked GLSL program
75 * (vertex shader + fragment shader).
76 */
77 static void st_use_program( GLcontext *ctx, struct gl_shader_program *shProg)
78 {
79 struct st_context *st = st_context(ctx);
80
81 st->dirty.st |= ST_NEW_FRAGMENT_PROGRAM;
82 st->dirty.st |= ST_NEW_VERTEX_PROGRAM;
83 }
84
85
86
87 /**
88 * Called via ctx->Driver.NewProgram() to allocate a new vertex or
89 * fragment program.
90 */
91 static struct gl_program *st_new_program( GLcontext *ctx,
92 GLenum target,
93 GLuint id )
94 {
95 switch (target) {
96 case GL_VERTEX_PROGRAM_ARB: {
97 struct st_vertex_program *prog = ST_CALLOC_STRUCT(st_vertex_program);
98
99 prog->serialNo = SerialNo++;
100
101 return _mesa_init_vertex_program( ctx,
102 &prog->Base,
103 target,
104 id );
105 }
106
107 case GL_FRAGMENT_PROGRAM_ARB:
108 case GL_FRAGMENT_PROGRAM_NV: {
109 struct st_fragment_program *prog = ST_CALLOC_STRUCT(st_fragment_program);
110
111 prog->serialNo = SerialNo++;
112
113 return _mesa_init_fragment_program( ctx,
114 &prog->Base,
115 target,
116 id );
117 }
118
119 default:
120 assert(0);
121 return NULL;
122 }
123 }
124
125
126 void
127 st_delete_program(GLcontext *ctx, struct gl_program *prog)
128 {
129 struct st_context *st = st_context(ctx);
130
131 switch( prog->Target ) {
132 case GL_VERTEX_PROGRAM_ARB:
133 {
134 struct st_vertex_program *stvp = (struct st_vertex_program *) prog;
135 st_vp_release_varients( st, stvp );
136 }
137 break;
138 case GL_FRAGMENT_PROGRAM_ARB:
139 {
140 struct st_fragment_program *stfp = (struct st_fragment_program *) prog;
141
142 if (stfp->driver_shader) {
143 cso_delete_fragment_shader(st->cso_context, stfp->driver_shader);
144 stfp->driver_shader = NULL;
145 }
146
147 if (stfp->tgsi.tokens) {
148 st_free_tokens(stfp->tgsi.tokens);
149 stfp->tgsi.tokens = NULL;
150 }
151
152 if (stfp->bitmap_program) {
153 struct gl_program *prg = &stfp->bitmap_program->Base.Base;
154 _mesa_reference_program(ctx, &prg, NULL);
155 stfp->bitmap_program = NULL;
156 }
157 }
158 break;
159 default:
160 assert(0); /* problem */
161 }
162
163 /* delete base class */
164 _mesa_delete_program( ctx, prog );
165 }
166
167
168 static GLboolean st_is_program_native( GLcontext *ctx,
169 GLenum target,
170 struct gl_program *prog )
171 {
172 return GL_TRUE;
173 }
174
175
176 static GLboolean st_program_string_notify( GLcontext *ctx,
177 GLenum target,
178 struct gl_program *prog )
179 {
180 struct st_context *st = st_context(ctx);
181
182 if (target == GL_FRAGMENT_PROGRAM_ARB) {
183 struct st_fragment_program *stfp = (struct st_fragment_program *) prog;
184
185 stfp->serialNo++;
186
187 if (stfp->driver_shader) {
188 cso_delete_fragment_shader(st->cso_context, stfp->driver_shader);
189 stfp->driver_shader = NULL;
190 }
191
192 if (stfp->tgsi.tokens) {
193 st_free_tokens(stfp->tgsi.tokens);
194 stfp->tgsi.tokens = NULL;
195 }
196
197 if (st->fp == stfp)
198 st->dirty.st |= ST_NEW_FRAGMENT_PROGRAM;
199 }
200 else if (target == GL_VERTEX_PROGRAM_ARB) {
201 struct st_vertex_program *stvp = (struct st_vertex_program *) prog;
202
203 stvp->serialNo++;
204
205 st_vp_release_varients( st, stvp );
206
207 if (st->vp == stvp)
208 st->dirty.st |= ST_NEW_VERTEX_PROGRAM;
209 }
210
211 /* XXX check if program is legal, within limits */
212 return GL_TRUE;
213 }
214
215
216
217 void st_init_program_functions(struct dd_function_table *functions)
218 {
219 functions->BindProgram = st_bind_program;
220 functions->UseProgram = st_use_program;
221 functions->NewProgram = st_new_program;
222 functions->DeleteProgram = st_delete_program;
223 functions->IsProgramNative = st_is_program_native;
224 functions->ProgramStringNotify = st_program_string_notify;
225 }