4dc76f19b10abb0814a2af4455aff8fd3a59f5ec
[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 "shader/prog_instruction.h"
37 #include "shader/prog_parameter.h"
38 #include "shader/program.h"
39 #include "shader/programopt.h"
40 #include "shader/shader_api.h"
41
42 #include "cso_cache/cso_cache.h"
43 #include "draw/draw_context.h"
44
45 #include "st_context.h"
46 #include "st_program.h"
47 #include "st_atom_shader.h"
48
49
50 static GLuint SerialNo = 1;
51
52
53 /**
54 * Called via ctx->Driver.BindProgram() to bind an ARB vertex or
55 * fragment program.
56 */
57 static void st_bind_program( GLcontext *ctx,
58 GLenum target,
59 struct gl_program *prog )
60 {
61 struct st_context *st = st_context(ctx);
62
63 switch (target) {
64 case GL_VERTEX_PROGRAM_ARB:
65 st->dirty.st |= ST_NEW_VERTEX_PROGRAM;
66 break;
67 case GL_FRAGMENT_PROGRAM_ARB:
68 st->dirty.st |= ST_NEW_FRAGMENT_PROGRAM;
69 break;
70 }
71 }
72
73
74 /**
75 * Called via ctx->Driver.UseProgram() to bind a linked GLSL program
76 * (vertex shader + fragment shader).
77 */
78 static void st_use_program( GLcontext *ctx,
79 GLuint program )
80 {
81 struct st_context *st = st_context(ctx);
82
83 st->dirty.st |= ST_NEW_FRAGMENT_PROGRAM;
84 st->dirty.st |= ST_NEW_VERTEX_PROGRAM;
85
86 _mesa_use_program(ctx, program);
87 }
88
89
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 = 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 = 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 return _mesa_new_program(ctx, target, id);
121 }
122 }
123
124
125 static void st_delete_program( GLcontext *ctx,
126 struct gl_program *prog )
127 {
128 struct st_context *st = st_context(ctx);
129
130 switch( prog->Target ) {
131 case GL_VERTEX_PROGRAM_ARB:
132 {
133 struct st_vertex_program *stvp = (struct st_vertex_program *) prog;
134 st_remove_vertex_program(st, stvp);
135 }
136 break;
137 case GL_FRAGMENT_PROGRAM_ARB:
138 {
139 struct st_fragment_program *stfp
140 = (struct st_fragment_program *) prog;
141 st_remove_fragment_program(st, stfp);
142 }
143 break;
144 default:
145 assert(0); /* problem */
146 }
147
148 _mesa_delete_program( ctx, prog );
149 }
150
151
152 static GLboolean st_is_program_native( GLcontext *ctx,
153 GLenum target,
154 struct gl_program *prog )
155 {
156 return GL_TRUE;
157 }
158
159
160 static void st_program_string_notify( GLcontext *ctx,
161 GLenum target,
162 struct gl_program *prog )
163 {
164 struct st_context *st = st_context(ctx);
165
166 if (target == GL_FRAGMENT_PROGRAM_ARB) {
167 struct st_fragment_program *stfp = (struct st_fragment_program *) prog;
168
169 stfp->serialNo++;
170
171 #if 0
172 if (stfp->cso) {
173 /* free the TGSI code */
174 // cso_delete(stfp->vs);
175 stfp->cso = NULL;
176 }
177 #endif
178
179 stfp->param_state = stfp->Base.Base.Parameters->StateFlags;
180
181 if (st->fp == stfp)
182 st->dirty.st |= ST_NEW_FRAGMENT_PROGRAM;
183 }
184 else if (target == GL_VERTEX_PROGRAM_ARB) {
185 struct st_vertex_program *stvp = (struct st_vertex_program *) prog;
186
187 stvp->serialNo++;
188
189 #if 0
190 if (stvp->cso) {
191 /* free the CSO data */
192 st->pipe->delete_vs_state(st->pipe, stvp->cso->data);
193 FREE((void *) stvp->cso);
194 stvp->cso = NULL;
195 }
196 #endif
197 if (stvp->draw_shader) {
198 draw_delete_vertex_shader(st->draw, stvp->draw_shader);
199 stvp->draw_shader = NULL;
200 }
201
202 stvp->param_state = stvp->Base.Base.Parameters->StateFlags;
203
204 if (st->vp == stvp)
205 st->dirty.st |= ST_NEW_VERTEX_PROGRAM;
206 }
207 }
208
209
210
211 void st_init_program_functions(struct dd_function_table *functions)
212 {
213 functions->BindProgram = st_bind_program;
214 functions->UseProgram = st_use_program;
215 functions->NewProgram = st_new_program;
216 functions->DeleteProgram = st_delete_program;
217 functions->IsProgramNative = st_is_program_native;
218 functions->ProgramStringNotify = st_program_string_notify;
219 }