Blender  V2.93
GPU_vertex_format.h
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2016 by Mike Erwin.
17  * All rights reserved.
18  */
19 
26 #pragma once
27 
28 #include "BLI_assert.h"
29 #include "BLI_compiler_compat.h"
30 #include "BLI_math_geom.h"
31 #include "GPU_common.h"
32 
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36 
37 #define GPU_VERT_ATTR_MAX_LEN 16
38 #define GPU_VERT_ATTR_MAX_NAMES 6
39 #define GPU_VERT_ATTR_NAMES_BUF_LEN 256
40 #define GPU_VERT_FORMAT_MAX_NAMES 63 /* More than enough, actual max is ~30. */
41 /* Computed as GPU_VERT_ATTR_NAMES_BUF_LEN / 30 (actual max format name). */
42 #define GPU_MAX_SAFE_ATTR_NAME 12
43 
44 typedef enum {
51 
53 
55  /* Warning! adjust GPUVertAttr if changing. */
57 
58 typedef enum {
61  GPU_FETCH_INT_TO_FLOAT_UNIT, /* 127 (ubyte) -> 0.5 (and so on for other int types) */
62  GPU_FETCH_INT_TO_FLOAT, /* 127 (any int type) -> 127.0 */
63  /* Warning! adjust GPUVertAttr if changing. */
65 
66 typedef struct GPUVertAttr {
67  /* GPUVertFetchMode */
69  /* GPUVertCompType */
71  /* 1 to 4 or 8 or 12 or 16 */
73  /* size in bytes, 1 to 64 */
74  uint sz : 7;
75  /* from beginning of vertex, in bytes */
76  uint offset : 11;
77  /* up to GPU_VERT_ATTR_MAX_NAMES */
81 
83  "We use uchar as index inside the name buffer "
84  "so GPU_VERT_ATTR_NAMES_BUF_LEN needs to be "
85  "smaller than GPUVertFormat->name_offset and "
86  "GPUVertAttr->names maximum value");
87 
88 typedef struct GPUVertFormat {
94  uint stride : 11;
96  uint packed : 1;
101 
105 
106 struct GPUShader;
107 
109 void GPU_vertformat_copy(GPUVertFormat *dest, const GPUVertFormat *src);
111 
113  GPUVertFormat *, const char *name, GPUVertCompType, uint comp_len, GPUVertFetchMode);
114 void GPU_vertformat_alias_add(GPUVertFormat *, const char *alias);
115 
117 
119 
120 int GPU_vertformat_attr_id_get(const GPUVertFormat *, const char *name);
121 
123  const GPUVertAttr *attr,
124  uint n_idx)
125 {
126  return format->names + attr->names[n_idx];
127 }
128 
129 /* WARNING: Can only rename using a string with same character count.
130  * WARNING: This removes all other aliases of this attrib */
131 void GPU_vertformat_attr_rename(GPUVertFormat *format, int attr, const char *new_name);
132 
133 void GPU_vertformat_safe_attr_name(const char *attr_name, char *r_safe_name, uint max_len);
134 
135 /* format conversion */
136 
137 typedef struct GPUPackedNormal {
138  int x : 10;
139  int y : 10;
140  int z : 10;
141  int w : 2; /* 0 by default, can manually set to { -2, -1, 0, 1 } */
143 
144 typedef struct GPUNormal {
145  union {
147  short high[3];
148  };
150 
151 /* OpenGL ES packs in a different order as desktop GL but component conversion is the same.
152  * Of the code here, only struct GPUPackedNormal needs to change. */
153 
154 #define SIGNED_INT_10_MAX 511
155 #define SIGNED_INT_10_MIN -512
156 
157 BLI_INLINE int clampi(int x, int min_allowed, int max_allowed)
158 {
159 #if TRUST_NO_ONE
160  assert(min_allowed <= max_allowed);
161 #endif
162  if (x < min_allowed) {
163  return min_allowed;
164  }
165  else if (x > max_allowed) {
166  return max_allowed;
167  }
168  else {
169  return x;
170  }
171 }
172 
174 {
175  int qx = x * 511.0f;
177 }
178 
180 {
181  /* 16-bit signed --> 10-bit signed */
182  /* TODO: round? */
183  return x >> 6;
184 }
185 
187 {
188  GPUPackedNormal n = {
192  };
193  return n;
194 }
195 
197 {
198  GPUPackedNormal n = {
202  };
203  return n;
204 }
205 
207  const float data[3],
208  const bool do_hq_normals)
209 {
210  if (do_hq_normals) {
211  normal_float_to_short_v3(gpu_normal->high, data);
212  }
213  else {
214  gpu_normal->low = GPU_normal_convert_i10_v3(data);
215  }
216 }
217 
218 #ifdef __cplusplus
219 }
220 #endif
#define BLI_INLINE
MINLINE void normal_float_to_short_v3(short r[3], const float n[3])
unsigned char uchar
Definition: BLI_sys_types.h:86
unsigned int uint
Definition: BLI_sys_types.h:83
struct GPUShader GPUShader
Definition: GPU_shader.h:33
#define GPU_VERT_ATTR_MAX_NAMES
void GPU_vertformat_attr_rename(GPUVertFormat *format, int attr, const char *new_name)
void GPU_vertformat_safe_attr_name(const char *attr_name, char *r_safe_name, uint max_len)
struct GPUPackedNormal GPUPackedNormal
GPUVertFetchMode
@ GPU_FETCH_FLOAT
@ GPU_FETCH_INT_TO_FLOAT_UNIT
@ GPU_FETCH_INT
@ GPU_FETCH_INT_TO_FLOAT
void GPU_vertformat_copy(GPUVertFormat *dest, const GPUVertFormat *src)
BLI_INLINE GPUPackedNormal GPU_normal_convert_i10_s3(const short data[3])
BLI_INLINE int gpu_convert_normalized_f32_to_i10(float x)
#define SIGNED_INT_10_MIN
#define GPU_VERT_ATTR_NAMES_BUF_LEN
BLI_INLINE const char * GPU_vertformat_attr_name_get(const GPUVertFormat *format, const GPUVertAttr *attr, uint n_idx)
uint GPU_vertformat_attr_add(GPUVertFormat *, const char *name, GPUVertCompType, uint comp_len, GPUVertFetchMode)
BLI_INLINE int gpu_convert_i16_to_i10(short x)
struct GPUVertAttr GPUVertAttr
BLI_INLINE void GPU_normal_convert_v3(GPUNormal *gpu_normal, const float data[3], const bool do_hq_normals)
struct GPUNormal GPUNormal
void GPU_vertformat_multiload_enable(GPUVertFormat *format, int load_count)
BLI_INLINE int clampi(int x, int min_allowed, int max_allowed)
void GPU_vertformat_clear(GPUVertFormat *)
#define SIGNED_INT_10_MAX
void GPU_vertformat_from_shader(GPUVertFormat *format, const struct GPUShader *shader)
#define GPU_VERT_ATTR_MAX_LEN
BLI_STATIC_ASSERT(GPU_VERT_ATTR_NAMES_BUF_LEN<=256, "We use uchar as index inside the name buffer " "so GPU_VERT_ATTR_NAMES_BUF_LEN needs to be " "smaller than GPUVertFormat->name_offset and " "GPUVertAttr->names maximum value")
BLI_INLINE GPUPackedNormal GPU_normal_convert_i10_v3(const float data[3])
void GPU_vertformat_alias_add(GPUVertFormat *, const char *alias)
int GPU_vertformat_attr_id_get(const GPUVertFormat *, const char *name)
void GPU_vertformat_deinterleave(GPUVertFormat *format)
struct GPUVertFormat GPUVertFormat
GPUVertCompType
@ GPU_COMP_U16
@ GPU_COMP_I10
@ GPU_COMP_F32
@ GPU_COMP_I32
@ GPU_COMP_I8
@ GPU_COMP_U32
@ GPU_COMP_I16
@ GPU_COMP_U8
void KERNEL_FUNCTION_FULL_NAME() shader(KernelGlobals *kg, uint4 *input, float4 *output, int type, int filter, int i, int offset, int sample)
format
Definition: logImageCore.h:47
GPUPackedNormal low
uchar names[GPU_VERT_ATTR_MAX_NAMES]
GPUVertAttr attrs[GPU_VERT_ATTR_MAX_LEN]
char names[GPU_VERT_ATTR_NAMES_BUF_LEN]