program.h revision af69d88d
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
48extern "C" {
49#endif
50
51extern struct gl_program _mesa_DummyProgram;
52
53
54extern void
55_mesa_init_program(struct gl_context *ctx);
56
57extern void
58_mesa_free_program_data(struct gl_context *ctx);
59
60extern void
61_mesa_update_default_objects_program(struct gl_context *ctx);
62
63extern void
64_mesa_set_program_error(struct gl_context *ctx, GLint pos, const char *string);
65
66extern const GLubyte *
67_mesa_find_line_column(const GLubyte *string, const GLubyte *pos,
68                       GLint *line, GLint *col);
69
70
71extern struct gl_program *
72_mesa_init_vertex_program(struct gl_context *ctx,
73                          struct gl_vertex_program *prog,
74                          GLenum target, GLuint id);
75
76extern struct gl_program *
77_mesa_init_fragment_program(struct gl_context *ctx,
78                            struct gl_fragment_program *prog,
79                            GLenum target, GLuint id);
80
81extern struct gl_program *
82_mesa_init_geometry_program(struct gl_context *ctx,
83                            struct gl_geometry_program *prog,
84                            GLenum target, GLuint id);
85
86extern struct gl_program *
87_mesa_init_compute_program(struct gl_context *ctx,
88                           struct gl_compute_program *prog,
89                           GLenum target, GLuint id);
90
91extern struct gl_program *
92_mesa_new_program(struct gl_context *ctx, GLenum target, GLuint id);
93
94extern void
95_mesa_delete_program(struct gl_context *ctx, struct gl_program *prog);
96
97extern struct gl_program *
98_mesa_lookup_program(struct gl_context *ctx, GLuint id);
99
100extern void
101_mesa_reference_program_(struct gl_context *ctx,
102                         struct gl_program **ptr,
103                         struct gl_program *prog);
104
105static inline void
106_mesa_reference_program(struct gl_context *ctx,
107                        struct gl_program **ptr,
108                        struct gl_program *prog)
109{
110   if (*ptr != prog)
111      _mesa_reference_program_(ctx, ptr, prog);
112}
113
114static inline void
115_mesa_reference_vertprog(struct gl_context *ctx,
116                         struct gl_vertex_program **ptr,
117                         struct gl_vertex_program *prog)
118{
119   _mesa_reference_program(ctx, (struct gl_program **) ptr,
120                           (struct gl_program *) prog);
121}
122
123static inline void
124_mesa_reference_fragprog(struct gl_context *ctx,
125                         struct gl_fragment_program **ptr,
126                         struct gl_fragment_program *prog)
127{
128   _mesa_reference_program(ctx, (struct gl_program **) ptr,
129                           (struct gl_program *) prog);
130}
131
132static inline void
133_mesa_reference_geomprog(struct gl_context *ctx,
134                         struct gl_geometry_program **ptr,
135                         struct gl_geometry_program *prog)
136{
137   _mesa_reference_program(ctx, (struct gl_program **) ptr,
138                           (struct gl_program *) prog);
139}
140
141extern struct gl_program *
142_mesa_clone_program(struct gl_context *ctx, const struct gl_program *prog);
143
144static inline struct gl_vertex_program *
145_mesa_clone_vertex_program(struct gl_context *ctx,
146                           const struct gl_vertex_program *prog)
147{
148   return (struct gl_vertex_program *) _mesa_clone_program(ctx, &prog->Base);
149}
150
151static inline struct gl_geometry_program *
152_mesa_clone_geometry_program(struct gl_context *ctx,
153                             const struct gl_geometry_program *prog)
154{
155   return (struct gl_geometry_program *) _mesa_clone_program(ctx, &prog->Base);
156}
157
158static inline struct gl_fragment_program *
159_mesa_clone_fragment_program(struct gl_context *ctx,
160                             const struct gl_fragment_program *prog)
161{
162   return (struct gl_fragment_program *) _mesa_clone_program(ctx, &prog->Base);
163}
164
165
166extern  GLboolean
167_mesa_insert_instructions(struct gl_program *prog, GLuint start, GLuint count);
168
169extern  GLboolean
170_mesa_delete_instructions(struct gl_program *prog, GLuint start, GLuint count);
171
172extern struct gl_program *
173_mesa_combine_programs(struct gl_context *ctx,
174                       const struct gl_program *progA,
175                       const struct gl_program *progB);
176
177extern void
178_mesa_find_used_registers(const struct gl_program *prog,
179                          gl_register_file file,
180                          GLboolean used[], GLuint usedSize);
181
182extern GLint
183_mesa_find_free_register(const GLboolean used[],
184                         GLuint maxRegs, GLuint firstReg);
185
186
187extern GLboolean
188_mesa_valid_register_index(const struct gl_context *ctx,
189                           gl_shader_stage shaderType,
190                           gl_register_file file, GLint index);
191
192extern void
193_mesa_postprocess_program(struct gl_context *ctx, struct gl_program *prog);
194
195extern GLint
196_mesa_get_min_invocations_per_fragment(struct gl_context *ctx,
197                                       const struct gl_fragment_program *prog,
198                                       bool ignore_sample_qualifier);
199
200static inline GLuint
201_mesa_program_enum_to_shader_stage(GLenum v)
202{
203   switch (v) {
204   case GL_VERTEX_PROGRAM_ARB:
205      return MESA_SHADER_VERTEX;
206   case GL_FRAGMENT_PROGRAM_ARB:
207      return MESA_SHADER_FRAGMENT;
208   case GL_GEOMETRY_PROGRAM_NV:
209      return MESA_SHADER_GEOMETRY;
210   case GL_COMPUTE_PROGRAM_NV:
211      return MESA_SHADER_COMPUTE;
212   default:
213      ASSERT(0);
214      return ~0;
215   }
216}
217
218
219static inline GLenum
220_mesa_shader_stage_to_program(unsigned stage)
221{
222   switch (stage) {
223   case MESA_SHADER_VERTEX:
224      return GL_VERTEX_PROGRAM_ARB;
225   case MESA_SHADER_FRAGMENT:
226      return GL_FRAGMENT_PROGRAM_ARB;
227   case MESA_SHADER_GEOMETRY:
228      return GL_GEOMETRY_PROGRAM_NV;
229   case MESA_SHADER_COMPUTE:
230      return GL_COMPUTE_PROGRAM_NV;
231   }
232
233   assert(!"Unexpected shader stage in _mesa_shader_stage_to_program");
234   return GL_VERTEX_PROGRAM_ARB;
235}
236
237
238/* Cast wrappers from gl_program to gl_vertex/geometry/fragment_program */
239
240static inline struct gl_fragment_program *
241gl_fragment_program(struct gl_program *prog)
242{
243   return (struct gl_fragment_program *) prog;
244}
245
246static inline const struct gl_fragment_program *
247gl_fragment_program_const(const struct gl_program *prog)
248{
249   return (const struct gl_fragment_program *) prog;
250}
251
252
253static inline struct gl_vertex_program *
254gl_vertex_program(struct gl_program *prog)
255{
256   return (struct gl_vertex_program *) prog;
257}
258
259static inline const struct gl_vertex_program *
260gl_vertex_program_const(const struct gl_program *prog)
261{
262   return (const struct gl_vertex_program *) prog;
263}
264
265
266static inline struct gl_geometry_program *
267gl_geometry_program(struct gl_program *prog)
268{
269   return (struct gl_geometry_program *) prog;
270}
271
272static inline const struct gl_geometry_program *
273gl_geometry_program_const(const struct gl_program *prog)
274{
275   return (const struct gl_geometry_program *) prog;
276}
277
278
279#ifdef __cplusplus
280} /* extern "C" */
281#endif
282
283#endif /* PROGRAM_H */
284