prog_parameter.h revision 7ec681f3
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 <stdbool.h>
35#include <stdint.h>
36#include "prog_statevars.h"
37
38#include <string.h>
39
40#ifdef __cplusplus
41extern "C" {
42#endif
43
44/**
45 * Names of the various vertex/fragment program register files, etc.
46 *
47 * NOTE: first four tokens must fit into 2 bits (see t_vb_arbprogram.c)
48 * All values should fit in a 4-bit field.
49 *
50 * NOTE: PROGRAM_STATE_VAR, PROGRAM_CONSTANT, and PROGRAM_UNIFORM can all be
51 * considered to be "uniform" variables since they can only be set outside
52 * glBegin/End.  They're also all stored in the same Parameters array.
53 */
54typedef enum
55{
56   PROGRAM_TEMPORARY,   /**< machine->Temporary[] */
57   PROGRAM_ARRAY,       /**< Arrays & Matrixes */
58   PROGRAM_INPUT,       /**< machine->Inputs[] */
59   PROGRAM_OUTPUT,      /**< machine->Outputs[] */
60   PROGRAM_STATE_VAR,   /**< gl_program->Parameters[] */
61   PROGRAM_CONSTANT,    /**< gl_program->Parameters[] */
62   PROGRAM_UNIFORM,     /**< gl_program->Parameters[] */
63   PROGRAM_WRITE_ONLY,  /**< A dummy, write-only register */
64   PROGRAM_ADDRESS,     /**< machine->AddressReg */
65   PROGRAM_SAMPLER,     /**< for shader samplers, compile-time only */
66   PROGRAM_SYSTEM_VALUE,/**< InstanceId, PrimitiveID, etc. */
67   PROGRAM_UNDEFINED,   /**< Invalid/TBD value */
68   PROGRAM_IMMEDIATE,   /**< Immediate value, used by TGSI */
69   PROGRAM_BUFFER,      /**< for shader buffers, compile-time only */
70   PROGRAM_MEMORY,      /**< for shared, global and local memory */
71   PROGRAM_IMAGE,       /**< for shader images, compile-time only */
72   PROGRAM_HW_ATOMIC,   /**< for hw atomic counters, compile-time only */
73   PROGRAM_FILE_MAX
74} gl_register_file;
75
76
77/**
78 * Actual data for constant values of parameters.
79 */
80typedef union gl_constant_value
81{
82   GLfloat f;
83   GLint b;
84   GLint i;
85   GLuint u;
86} gl_constant_value;
87
88
89/**
90 * Program parameter.
91 * Used by shaders/programs for uniforms, constants, varying vars, etc.
92 */
93struct gl_program_parameter
94{
95   const char *Name;        /**< Null-terminated string */
96   gl_register_file Type:5;  /**< PROGRAM_CONSTANT or STATE_VAR */
97
98   /**
99    * We need to keep track of whether the param is padded for use in the
100    * shader cache.
101    */
102   bool Padded:1;
103
104   GLenum16 DataType;         /**< GL_FLOAT, GL_FLOAT_VEC2, etc */
105
106   /**
107    * Number of components (1..4), or more.
108    * If the number of components is greater than 4,
109    * this parameter is part of a larger uniform like a GLSL matrix or array.
110    */
111   GLushort Size;
112   /**
113    * A sequence of STATE_* tokens and integers to identify GL state.
114    */
115   gl_state_index16 StateIndexes[STATE_LENGTH];
116
117   /**
118    * Offset within ParameterValues where this parameter is stored.
119    */
120   unsigned ValueOffset;
121
122   /**
123    * Index of this parameter's uniform storage.
124    */
125   uint32_t UniformStorageIndex;
126
127   /**
128    * Index of the first uniform storage that is associated with the same
129    * variable as this parameter.
130    */
131   uint32_t MainUniformStorageIndex;
132};
133
134
135/**
136 * List of gl_program_parameter instances.
137 */
138struct gl_program_parameter_list
139{
140   unsigned Size;           /**< allocated size of Parameters */
141   unsigned SizeValues;     /**< alllocate size of ParameterValues */
142   GLuint NumParameters;  /**< number of used parameters in array */
143   unsigned NumParameterValues;  /**< number of used parameter values array */
144   struct gl_program_parameter *Parameters; /**< Array [Size] */
145   gl_constant_value *ParameterValues; /**< Array [Size] of gl_constant_value */
146   GLbitfield StateFlags; /**< _NEW_* flags indicating which state changes
147                               might invalidate ParameterValues[] */
148   bool DisallowRealloc;
149
150   /* Parameters are optionally sorted as follows. Uniforms and constants
151    * are first, then state vars. This should be true in all cases except
152    * ir_to_mesa, which adds constants at the end, and ARB_vp with ARL,
153    * which can't sort parameters.
154    */
155   int UniformBytes;
156   int FirstStateVarIndex;
157   int LastStateVarIndex;
158};
159
160
161extern struct gl_program_parameter_list *
162_mesa_new_parameter_list(void);
163
164extern struct gl_program_parameter_list *
165_mesa_new_parameter_list_sized(unsigned size);
166
167extern void
168_mesa_free_parameter_list(struct gl_program_parameter_list *paramList);
169
170extern void
171_mesa_reserve_parameter_storage(struct gl_program_parameter_list *paramList,
172                                unsigned reserve_params,
173                                unsigned reserve_values);
174
175extern void
176_mesa_disallow_parameter_storage_realloc(struct gl_program_parameter_list *paramList);
177
178extern GLint
179_mesa_add_parameter(struct gl_program_parameter_list *paramList,
180                    gl_register_file type, const char *name,
181                    GLuint size, GLenum datatype,
182                    const gl_constant_value *values,
183                    const gl_state_index16 state[STATE_LENGTH],
184                    bool pad_and_align);
185
186extern GLint
187_mesa_add_typed_unnamed_constant(struct gl_program_parameter_list *paramList,
188                           const gl_constant_value *values, GLuint size,
189                           GLenum datatype, GLuint *swizzleOut);
190
191static inline GLint
192_mesa_add_unnamed_constant(struct gl_program_parameter_list *paramList,
193                           const gl_constant_value *values, GLuint size,
194                           GLuint *swizzleOut)
195{
196   return _mesa_add_typed_unnamed_constant(paramList, values, size, GL_NONE,
197                                           swizzleOut);
198}
199
200extern GLint
201_mesa_add_sized_state_reference(struct gl_program_parameter_list *paramList,
202                                const gl_state_index16 stateTokens[STATE_LENGTH],
203                                const unsigned size, bool pad_and_align);
204
205extern GLint
206_mesa_add_state_reference(struct gl_program_parameter_list *paramList,
207                          const gl_state_index16 stateTokens[STATE_LENGTH]);
208
209
210static inline GLint
211_mesa_lookup_parameter_index(const struct gl_program_parameter_list *paramList,
212                             const char *name)
213{
214   if (!paramList)
215      return -1;
216
217   /* name must be null-terminated */
218   for (GLint i = 0; i < (GLint) paramList->NumParameters; i++) {
219      if (paramList->Parameters[i].Name &&
220         strcmp(paramList->Parameters[i].Name, name) == 0)
221         return i;
222   }
223
224   return -1;
225}
226
227static inline bool
228_mesa_gl_datatype_is_64bit(GLenum datatype)
229{
230   switch (datatype) {
231   case GL_DOUBLE:
232   case GL_DOUBLE_VEC2:
233   case GL_DOUBLE_VEC3:
234   case GL_DOUBLE_VEC4:
235   case GL_DOUBLE_MAT2:
236   case GL_DOUBLE_MAT2x3:
237   case GL_DOUBLE_MAT2x4:
238   case GL_DOUBLE_MAT3:
239   case GL_DOUBLE_MAT3x2:
240   case GL_DOUBLE_MAT3x4:
241   case GL_DOUBLE_MAT4:
242   case GL_DOUBLE_MAT4x2:
243   case GL_DOUBLE_MAT4x3:
244   case GL_INT64_ARB:
245   case GL_INT64_VEC2_ARB:
246   case GL_INT64_VEC3_ARB:
247   case GL_INT64_VEC4_ARB:
248   case GL_UNSIGNED_INT64_ARB:
249   case GL_UNSIGNED_INT64_VEC2_ARB:
250   case GL_UNSIGNED_INT64_VEC3_ARB:
251   case GL_UNSIGNED_INT64_VEC4_ARB:
252      return true;
253   default:
254      return false;
255   }
256}
257
258void
259_mesa_recompute_parameter_bounds(struct gl_program_parameter_list *list);
260
261#ifdef __cplusplus
262}
263#endif
264
265#endif /* PROG_PARAMETER_H */
266