mesa: add _mesa_add_sized_state_reference() helper
[mesa.git] / src / mesa / program / prog_parameter.h
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2008 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 prog_parameter.c
27 * Program parameter lists and functions.
28 * \author Brian Paul
29 */
30
31 #ifndef PROG_PARAMETER_H
32 #define PROG_PARAMETER_H
33
34 #include "main/mtypes.h"
35 #include "prog_statevars.h"
36
37 #include <string.h>
38
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42
43
44 /**
45 * Actual data for constant values of parameters.
46 */
47 typedef union gl_constant_value
48 {
49 GLfloat f;
50 GLint b;
51 GLint i;
52 GLuint u;
53 } gl_constant_value;
54
55
56 /**
57 * Program parameter.
58 * Used by shaders/programs for uniforms, constants, varying vars, etc.
59 */
60 struct gl_program_parameter
61 {
62 const char *Name; /**< Null-terminated string */
63 gl_register_file Type:16; /**< PROGRAM_CONSTANT or STATE_VAR */
64 GLenum16 DataType; /**< GL_FLOAT, GL_FLOAT_VEC2, etc */
65 /**
66 * Number of components (1..4), or more.
67 * If the number of components is greater than 4,
68 * this parameter is part of a larger uniform like a GLSL matrix or array.
69 * The next program parameter's Size will be Size-4 of this parameter.
70 */
71 GLushort Size;
72 /**
73 * A sequence of STATE_* tokens and integers to identify GL state.
74 */
75 gl_state_index16 StateIndexes[STATE_LENGTH];
76 };
77
78
79 /**
80 * List of gl_program_parameter instances.
81 */
82 struct gl_program_parameter_list
83 {
84 GLuint Size; /**< allocated size of Parameters, ParameterValues */
85 GLuint NumParameters; /**< number of used parameters in array */
86 unsigned NumParameterValues; /**< number of used parameter values array */
87 struct gl_program_parameter *Parameters; /**< Array [Size] */
88 unsigned *ParameterValueOffset;
89 gl_constant_value *ParameterValues; /**< Array [Size] of gl_constant_value */
90 GLbitfield StateFlags; /**< _NEW_* flags indicating which state changes
91 might invalidate ParameterValues[] */
92 };
93
94
95 extern struct gl_program_parameter_list *
96 _mesa_new_parameter_list(void);
97
98 extern struct gl_program_parameter_list *
99 _mesa_new_parameter_list_sized(unsigned size);
100
101 extern void
102 _mesa_free_parameter_list(struct gl_program_parameter_list *paramList);
103
104 extern void
105 _mesa_reserve_parameter_storage(struct gl_program_parameter_list *paramList,
106 unsigned reserve_slots);
107
108 extern GLint
109 _mesa_add_parameter(struct gl_program_parameter_list *paramList,
110 gl_register_file type, const char *name,
111 GLuint size, GLenum datatype,
112 const gl_constant_value *values,
113 const gl_state_index16 state[STATE_LENGTH],
114 bool pad_and_align);
115
116 extern GLint
117 _mesa_add_typed_unnamed_constant(struct gl_program_parameter_list *paramList,
118 const gl_constant_value values[4], GLuint size,
119 GLenum datatype, GLuint *swizzleOut);
120
121 static inline GLint
122 _mesa_add_unnamed_constant(struct gl_program_parameter_list *paramList,
123 const gl_constant_value values[4], GLuint size,
124 GLuint *swizzleOut)
125 {
126 return _mesa_add_typed_unnamed_constant(paramList, values, size, GL_NONE,
127 swizzleOut);
128 }
129
130 extern GLint
131 _mesa_add_sized_state_reference(struct gl_program_parameter_list *paramList,
132 const gl_state_index16 stateTokens[STATE_LENGTH],
133 const unsigned size, bool pad_and_align);
134
135 extern GLint
136 _mesa_add_state_reference(struct gl_program_parameter_list *paramList,
137 const gl_state_index16 stateTokens[]);
138
139
140 static inline GLint
141 _mesa_lookup_parameter_index(const struct gl_program_parameter_list *paramList,
142 const char *name)
143 {
144 if (!paramList)
145 return -1;
146
147 /* name must be null-terminated */
148 for (GLint i = 0; i < (GLint) paramList->NumParameters; i++) {
149 if (paramList->Parameters[i].Name &&
150 strcmp(paramList->Parameters[i].Name, name) == 0)
151 return i;
152 }
153
154 return -1;
155 }
156
157 #ifdef __cplusplus
158 }
159 #endif
160
161 #endif /* PROG_PARAMETER_H */