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