26609e964545995a430e4cf72b7d85da21a52eea
[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 "st_context.h"
43 #include "st_program.h"
44 #include "st_atom_shader.h"
45
46 #include "pipe/tgsi/mesa/tgsi_mesa.h"
47
48
49 /**
50 * Called via ctx->Driver.BindProgram() to bind an ARB vertex or
51 * fragment program.
52 */
53 static void st_bind_program( GLcontext *ctx,
54 GLenum target,
55 struct gl_program *prog )
56 {
57 struct st_context *st = st_context(ctx);
58
59 switch (target) {
60 case GL_VERTEX_PROGRAM_ARB:
61 st->dirty.st |= ST_NEW_VERTEX_PROGRAM;
62 break;
63 case GL_FRAGMENT_PROGRAM_ARB:
64 st->dirty.st |= ST_NEW_FRAGMENT_PROGRAM;
65 break;
66 }
67 }
68
69
70 /**
71 * Called via ctx->Driver.UseProgram() to bind a linked GLSL program
72 * (vertex shader + fragment shader).
73 */
74 static void st_use_program( GLcontext *ctx,
75 GLuint program )
76 {
77 struct st_context *st = st_context(ctx);
78
79 st->dirty.st |= ST_NEW_FRAGMENT_PROGRAM;
80 st->dirty.st |= ST_NEW_VERTEX_PROGRAM;
81
82 _mesa_use_program(ctx, program);
83 }
84
85
86
87 static struct gl_program *st_new_program( GLcontext *ctx,
88 GLenum target,
89 GLuint id )
90 {
91 switch (target) {
92 case GL_VERTEX_PROGRAM_ARB: {
93 struct st_vertex_program *prog = CALLOC_STRUCT(st_vertex_program);
94
95 prog->serialNo = 1;
96
97 return _mesa_init_vertex_program( ctx,
98 &prog->Base,
99 target,
100 id );
101 }
102
103 case GL_FRAGMENT_PROGRAM_ARB:
104 case GL_FRAGMENT_PROGRAM_NV: {
105 struct st_fragment_program *prog = CALLOC_STRUCT(st_fragment_program);
106
107 prog->serialNo = 1;
108
109 return _mesa_init_fragment_program( ctx,
110 &prog->Base,
111 target,
112 id );
113 }
114
115 default:
116 return _mesa_new_program(ctx, target, id);
117 }
118 }
119
120
121 static void st_delete_program( GLcontext *ctx,
122 struct gl_program *prog )
123 {
124 struct st_context *st = st_context(ctx);
125
126 switch( prog->Target ) {
127 case GL_VERTEX_PROGRAM_ARB:
128 {
129 struct st_vertex_program *stvp = (struct st_vertex_program *) prog;
130 st_remove_vertex_program(st, stvp);
131 }
132 break;
133 case GL_FRAGMENT_PROGRAM_ARB:
134 {
135 struct st_fragment_program *stfp
136 = (struct st_fragment_program *) prog;
137 st_remove_fragment_program(st, stfp);
138 }
139 break;
140 default:
141 assert(0); /* problem */
142 }
143
144 _mesa_delete_program( ctx, prog );
145 }
146
147
148 static GLboolean st_is_program_native( GLcontext *ctx,
149 GLenum target,
150 struct gl_program *prog )
151 {
152 return GL_TRUE;
153 }
154
155
156 static void st_program_string_notify( GLcontext *ctx,
157 GLenum target,
158 struct gl_program *prog )
159 {
160 struct st_context *st = st_context(ctx);
161
162 if (target == GL_FRAGMENT_PROGRAM_ARB) {
163 struct st_fragment_program *stfp = (struct st_fragment_program *) prog;
164
165 stfp->serialNo++;
166
167 if (stfp->fs) {
168 /* free the TGSI code */
169 // cso_delete(stfp->vs);
170 stfp->fs = NULL;
171 }
172
173 stfp->param_state = stfp->Base.Base.Parameters->StateFlags;
174
175 if (st->fp == stfp)
176 st->dirty.st |= ST_NEW_FRAGMENT_PROGRAM;
177 }
178 else if (target == GL_VERTEX_PROGRAM_ARB) {
179 struct st_vertex_program *stvp = (struct st_vertex_program *) prog;
180
181 stvp->serialNo++;
182
183 if (stvp->vs) {
184 /* free the TGSI code */
185 // cso_delete(stfp->vs);
186 stvp->vs = NULL;
187 }
188
189 stvp->param_state = stvp->Base.Base.Parameters->StateFlags;
190
191 if (st->vp == stvp)
192 st->dirty.st |= ST_NEW_VERTEX_PROGRAM;
193
194 }
195 }
196
197
198
199 void st_init_program_functions(struct dd_function_table *functions)
200 {
201 functions->BindProgram = st_bind_program;
202 functions->UseProgram = st_use_program;
203 functions->NewProgram = st_new_program;
204 functions->DeleteProgram = st_delete_program;
205 functions->IsProgramNative = st_is_program_native;
206 functions->ProgramStringNotify = st_program_string_notify;
207 }