mesa: Add an assertion to _mesa_program_index_to_target().
[mesa.git] / src / mesa / program / program.h
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2007 Brian Paul 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 "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 /**
26 * \file program.c
27 * Vertex and fragment program support functions.
28 * \author Brian Paul
29 */
30
31
32 /**
33 * \mainpage Mesa vertex and fragment program module
34 *
35 * This module or directory contains most of the code for vertex and
36 * fragment programs and shaders, including state management, parsers,
37 * and (some) software routines for executing programs
38 */
39
40 #ifndef PROGRAM_H
41 #define PROGRAM_H
42
43 #include "main/compiler.h"
44 #include "main/mtypes.h"
45
46
47 #ifdef __cplusplus
48 extern "C" {
49 #endif
50
51 extern struct gl_program _mesa_DummyProgram;
52
53
54 extern void
55 _mesa_init_program(struct gl_context *ctx);
56
57 extern void
58 _mesa_free_program_data(struct gl_context *ctx);
59
60 extern void
61 _mesa_update_default_objects_program(struct gl_context *ctx);
62
63 extern void
64 _mesa_set_program_error(struct gl_context *ctx, GLint pos, const char *string);
65
66 extern const GLubyte *
67 _mesa_find_line_column(const GLubyte *string, const GLubyte *pos,
68 GLint *line, GLint *col);
69
70
71 extern struct gl_program *
72 _mesa_init_vertex_program(struct gl_context *ctx,
73 struct gl_vertex_program *prog,
74 GLenum target, GLuint id);
75
76 extern struct gl_program *
77 _mesa_init_fragment_program(struct gl_context *ctx,
78 struct gl_fragment_program *prog,
79 GLenum target, GLuint id);
80
81 extern struct gl_program *
82 _mesa_init_geometry_program(struct gl_context *ctx,
83 struct gl_geometry_program *prog,
84 GLenum target, GLuint id);
85
86 extern struct gl_program *
87 _mesa_new_program(struct gl_context *ctx, GLenum target, GLuint id);
88
89 extern void
90 _mesa_delete_program(struct gl_context *ctx, struct gl_program *prog);
91
92 extern struct gl_program *
93 _mesa_lookup_program(struct gl_context *ctx, GLuint id);
94
95 extern void
96 _mesa_reference_program_(struct gl_context *ctx,
97 struct gl_program **ptr,
98 struct gl_program *prog);
99
100 static inline void
101 _mesa_reference_program(struct gl_context *ctx,
102 struct gl_program **ptr,
103 struct gl_program *prog)
104 {
105 if (*ptr != prog)
106 _mesa_reference_program_(ctx, ptr, prog);
107 }
108
109 static inline void
110 _mesa_reference_vertprog(struct gl_context *ctx,
111 struct gl_vertex_program **ptr,
112 struct gl_vertex_program *prog)
113 {
114 _mesa_reference_program(ctx, (struct gl_program **) ptr,
115 (struct gl_program *) prog);
116 }
117
118 static inline void
119 _mesa_reference_fragprog(struct gl_context *ctx,
120 struct gl_fragment_program **ptr,
121 struct gl_fragment_program *prog)
122 {
123 _mesa_reference_program(ctx, (struct gl_program **) ptr,
124 (struct gl_program *) prog);
125 }
126
127 static inline void
128 _mesa_reference_geomprog(struct gl_context *ctx,
129 struct gl_geometry_program **ptr,
130 struct gl_geometry_program *prog)
131 {
132 _mesa_reference_program(ctx, (struct gl_program **) ptr,
133 (struct gl_program *) prog);
134 }
135
136 extern struct gl_program *
137 _mesa_clone_program(struct gl_context *ctx, const struct gl_program *prog);
138
139 static inline struct gl_vertex_program *
140 _mesa_clone_vertex_program(struct gl_context *ctx,
141 const struct gl_vertex_program *prog)
142 {
143 return (struct gl_vertex_program *) _mesa_clone_program(ctx, &prog->Base);
144 }
145
146 static inline struct gl_geometry_program *
147 _mesa_clone_geometry_program(struct gl_context *ctx,
148 const struct gl_geometry_program *prog)
149 {
150 return (struct gl_geometry_program *) _mesa_clone_program(ctx, &prog->Base);
151 }
152
153 static inline struct gl_fragment_program *
154 _mesa_clone_fragment_program(struct gl_context *ctx,
155 const struct gl_fragment_program *prog)
156 {
157 return (struct gl_fragment_program *) _mesa_clone_program(ctx, &prog->Base);
158 }
159
160
161 extern GLboolean
162 _mesa_insert_instructions(struct gl_program *prog, GLuint start, GLuint count);
163
164 extern GLboolean
165 _mesa_delete_instructions(struct gl_program *prog, GLuint start, GLuint count);
166
167 extern struct gl_program *
168 _mesa_combine_programs(struct gl_context *ctx,
169 const struct gl_program *progA,
170 const struct gl_program *progB);
171
172 extern void
173 _mesa_find_used_registers(const struct gl_program *prog,
174 gl_register_file file,
175 GLboolean used[], GLuint usedSize);
176
177 extern GLint
178 _mesa_find_free_register(const GLboolean used[],
179 GLuint maxRegs, GLuint firstReg);
180
181
182 extern GLboolean
183 _mesa_valid_register_index(const struct gl_context *ctx,
184 gl_shader_type shaderType,
185 gl_register_file file, GLint index);
186
187 extern void
188 _mesa_postprocess_program(struct gl_context *ctx, struct gl_program *prog);
189
190 extern GLint
191 _mesa_get_min_invocations_per_fragment(struct gl_context *ctx,
192 const struct gl_fragment_program *prog);
193
194 static inline GLuint
195 _mesa_program_target_to_index(GLenum v)
196 {
197 switch (v) {
198 case GL_VERTEX_PROGRAM_ARB:
199 return MESA_SHADER_VERTEX;
200 case GL_FRAGMENT_PROGRAM_ARB:
201 return MESA_SHADER_FRAGMENT;
202 case GL_GEOMETRY_PROGRAM_NV:
203 return MESA_SHADER_GEOMETRY;
204 default:
205 ASSERT(0);
206 return ~0;
207 }
208 }
209
210 static inline GLenum
211 _mesa_program_index_to_target(GLuint i)
212 {
213 static const GLenum enums[] = {
214 GL_VERTEX_PROGRAM_ARB,
215 GL_GEOMETRY_PROGRAM_NV,
216 GL_FRAGMENT_PROGRAM_ARB
217 };
218 STATIC_ASSERT(Elements(enums) == MESA_SHADER_TYPES);
219 if(i >= MESA_SHADER_TYPES) {
220 assert(!"Unexpected program index");
221 return 0;
222 } else
223 return enums[i];
224 }
225
226
227 /* Cast wrappers from gl_program to gl_vertex/geometry/fragment_program */
228
229 static inline struct gl_fragment_program *
230 gl_fragment_program(struct gl_program *prog)
231 {
232 return (struct gl_fragment_program *) prog;
233 }
234
235 static inline const struct gl_fragment_program *
236 gl_fragment_program_const(const struct gl_program *prog)
237 {
238 return (const struct gl_fragment_program *) prog;
239 }
240
241
242 static inline struct gl_vertex_program *
243 gl_vertex_program(struct gl_program *prog)
244 {
245 return (struct gl_vertex_program *) prog;
246 }
247
248 static inline const struct gl_vertex_program *
249 gl_vertex_program_const(const struct gl_program *prog)
250 {
251 return (const struct gl_vertex_program *) prog;
252 }
253
254
255 static inline struct gl_geometry_program *
256 gl_geometry_program(struct gl_program *prog)
257 {
258 return (struct gl_geometry_program *) prog;
259 }
260
261 static inline const struct gl_geometry_program *
262 gl_geometry_program_const(const struct gl_program *prog)
263 {
264 return (const struct gl_geometry_program *) prog;
265 }
266
267
268 #ifdef __cplusplus
269 } /* extern "C" */
270 #endif
271
272 #endif /* PROGRAM_H */