Panda3D
gpuCommand.I
1 /**
2  *
3  * RenderPipeline
4  *
5  * Copyright (c) 2014-2016 tobspr <tobias.springer1@gmail.com>
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy
8  * of this software and associated documentation files (the "Software"), to deal
9  * in the Software without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23  * THE SOFTWARE.
24  *
25  */
26 
27 #include <stdint.h>
28 
29 /**
30  * @brief Appends an integer to the GPUCommand.
31  * @details This adds an integer to the back of the GPUCommand. Depending on the
32  * setting in convert_int_to_float, this will either just convert the int to a
33  * float by casting it, or just do a bitwise copy.
34  *
35  * @param v The integer to append.
36  */
37 inline void GPUCommand::push_int(int v) {
38  push_float(convert_int_to_float(v));
39 }
40 
41 /**
42  * @brief Internal method to convert an integer to float
43  * @details This methods gets called by the GPUCommand::push_int, and manages
44  * storing an integer in a floating point variable. There are two options,
45  * which are documented inside of the method.
46  *
47  * @param v Integer to convert
48  * @return Float-representation of that integer, either casted or binary converted.s
49  */
50 inline float GPUCommand::convert_int_to_float(int v) const {
51 
52  #if !PACK_INT_AS_FLOAT
53  // Just round to float, can cause rounding issues tho
54  return (float)v;
55 
56  #else
57  assert(sizeof(float) == 4); // We really need this for packing! Better
58  // throw an error if the compiler uses more
59  // than 4 bytes.
60  // Simple binary conversion, assuming sizeof(int) == sizeof(float)
61  union { int32_t _int; float _float; } converter = { (int32_t)v };
62  return converter._float;
63  #endif
64 }
65 
66 /**
67  * @brief Appends a float to the GPUCommand.
68  * @details This adds an integer to the back of the GPUCommand. Its used by all
69  * other push_xxx methods, and simply stores the value, then increments the write
70  * pointer. When the amount of floats exceeds the capacity of the GPUCommand,
71  * an error will be printed, and the method returns without doing anything else.
72  *
73  * @param v The float to append.
74  */
75 inline void GPUCommand::push_float(float v) {
76  if (_current_index >= GPU_COMMAND_ENTRIES) {
77  gpucommand_cat.error() << "Out of bounds! Exceeded command size of " << GPU_COMMAND_ENTRIES << std::endl;
78  return;
79  }
80  _data[_current_index++] = v;
81 }
82 
83 /**
84  * @brief Appends a 3-component floating point vector to the GPUCommand.
85  * @details This appends a 3-component floating point vector to the command.
86  * It basically just calls push_float() for every component, in the order
87  * x, y, z, which causes the vector to occupy the space of 3 floats.
88  *
89  * @param v Int-Vector to append.
90  */
91 inline void GPUCommand::push_vec3(const LVecBase3 &v) {
92  push_float(v.get_x());
93  push_float(v.get_y());
94  push_float(v.get_z());
95 }
96 
97 
98 /**
99  * @brief Appends a 3-component integer vector to the GPUCommand.
100  * @details This appends a 3-component integer vector to the command.
101  * It basically just calls push_int() for every component, in the order
102  * x, y, z, which causes the vector to occupy the space of 3 floats.
103  *
104  * @param v Int-Vector to append.
105  */
106 inline void GPUCommand::push_vec3(const LVecBase3i &v) {
107  push_int(v.get_x());
108  push_int(v.get_y());
109  push_int(v.get_z());
110 }
111 
112 /**
113  * @brief Appends a 4-component floating point vector to the GPUCommand.
114  * @details This appends a 4-component floating point vector to the command.
115  * It basically just calls push_float() for every component, in the order
116  * x, y, z, which causes the vector to occupy the space of 3 floats.
117  *
118  * @param v Int-Vector to append.
119  */
120 inline void GPUCommand::push_vec4(const LVecBase4 &v) {
121  push_float(v.get_x());
122  push_float(v.get_y());
123  push_float(v.get_z());
124  push_float(v.get_w());
125 }
126 
127 /**
128  * @brief Appends a 4-component integer vector to the GPUCommand.
129  * @details This appends a 4-component integer vector to the command.
130  * It basically just calls push_int() for every component, in the order
131  * x, y, z, w, which causes the vector to occupy the space of 4 floats.
132  *
133  * @param v Int-Vector to append.
134  */
135 inline void GPUCommand::push_vec4(const LVecBase4i &v) {
136  push_int(v.get_x());
137  push_int(v.get_y());
138  push_int(v.get_z());
139  push_int(v.get_w());
140 }
141 
142 /**
143  * @brief Appends a floating point 3x3 matrix to the GPUCommand.
144  * @details This appends a floating point 3x3 matrix to the GPUCommand, by
145  * pushing all components in row-order to the command. This occupies a space of
146  * 9 floats.
147  *
148  * @param v Matrix to append
149  */
150 inline void GPUCommand::push_mat3(const LMatrix3 &v) {
151  for (size_t i = 0; i < 3; ++i) {
152  for (size_t j = 0; j < 3; ++j) {
153  push_float(v.get_cell(i, j));
154  }
155  }
156 }
157 
158 /**
159  * @brief Appends a floating point 4x4 matrix to the GPUCommand.
160  * @details This appends a floating point 4x4 matrix to the GPUCommand, by
161  * pushing all components in row-order to the command. This occupies a space of
162  * 16 floats.
163  *
164  * @param v Matrix to append
165  */
166 inline void GPUCommand::push_mat4(const LMatrix4 &v) {
167  for (size_t i = 0; i < 4; ++i) {
168  for (size_t j = 0; j < 4; ++j) {
169  push_float(v.get_cell(i, j));
170  }
171  }
172 }
173 
174 /**
175  * @brief Returns whether integers are packed as floats.
176  * @details This returns how integer are packed into the data stream. If the
177  * returned value is true, then integers are packed using their binary
178  * representation converted to floating point format. If the returned value
179  * is false, then integers are packed by simply casting them to float,
180  * e.g. val = (float)i;
181  * @return The integer representation flag
182  */
184  return PACK_INT_AS_FLOAT;
185 }
static bool get_uses_integer_packing()
Returns whether integers are packed as floats.
Definition: gpuCommand.I:183
void push_vec4(const LVecBase4 &v)
Appends a 4-component floating point vector to the GPUCommand.
Definition: gpuCommand.I:120
void push_float(float v)
Appends a float to the GPUCommand.
Definition: gpuCommand.I:75
void push_mat4(const LMatrix4 &v)
Appends a floating point 4x4 matrix to the GPUCommand.
Definition: gpuCommand.I:166
void push_mat3(const LMatrix3 &v)
Appends a floating point 3x3 matrix to the GPUCommand.
Definition: gpuCommand.I:150
void push_vec3(const LVecBase3 &v)
Appends a 3-component floating point vector to the GPUCommand.
Definition: gpuCommand.I:91
void push_int(int v)
RenderPipeline.
Definition: gpuCommand.I:37