Blender  V3.3
GPU_vertex_format.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2016 by Mike Erwin. All rights reserved. */
3 
10 #pragma once
11 
12 #include "BLI_assert.h"
13 #include "BLI_compiler_compat.h"
14 #include "BLI_math_geom.h"
15 #include "GPU_common.h"
16 
17 #ifdef __cplusplus
18 extern "C" {
19 #endif
20 
21 #define GPU_VERT_ATTR_MAX_LEN 16
22 #define GPU_VERT_ATTR_MAX_NAMES 6
23 #define GPU_VERT_ATTR_NAMES_BUF_LEN 256
24 #define GPU_VERT_FORMAT_MAX_NAMES 63 /* More than enough, actual max is ~30. */
25 /* Computed as GPU_VERT_ATTR_NAMES_BUF_LEN / 30 (actual max format name). */
26 #define GPU_MAX_SAFE_ATTR_NAME 12
27 
28 typedef enum {
35 
37 
39  /* Warning! adjust GPUVertAttr if changing. */
41 
42 typedef enum {
45  GPU_FETCH_INT_TO_FLOAT_UNIT, /* 127 (ubyte) -> 0.5 (and so on for other int types) */
46  GPU_FETCH_INT_TO_FLOAT, /* 127 (any int type) -> 127.0 */
47  /* Warning! adjust GPUVertAttr if changing. */
49 
50 typedef struct GPUVertAttr {
51  /* GPUVertFetchMode */
53  /* GPUVertCompType */
55  /* 1 to 4 or 8 or 12 or 16 */
57  /* size in bytes, 1 to 64 */
58  uint size : 7;
59  /* from beginning of vertex, in bytes */
60  uint offset : 11;
61  /* up to GPU_VERT_ATTR_MAX_NAMES */
65 
67  "We use uchar as index inside the name buffer "
68  "so GPU_VERT_ATTR_NAMES_BUF_LEN needs to be "
69  "smaller than GPUVertFormat->name_offset and "
70  "GPUVertAttr->names maximum value");
71 
72 typedef struct GPUVertFormat {
78  uint stride : 11;
80  uint packed : 1;
85 
89 
90 struct GPUShader;
91 
94 void GPU_vertformat_from_shader(GPUVertFormat *format, const struct GPUShader *shader);
95 
97  GPUVertFormat *, const char *name, GPUVertCompType, uint comp_len, GPUVertFetchMode);
98 void GPU_vertformat_alias_add(GPUVertFormat *, const char *alias);
99 
112 
128 
129 int GPU_vertformat_attr_id_get(const GPUVertFormat *, const char *name);
130 
132  const GPUVertAttr *attr,
133  uint n_idx)
134 {
135  return format->names + attr->names[n_idx];
136 }
137 
142 void GPU_vertformat_attr_rename(GPUVertFormat *format, int attr, const char *new_name);
143 
148 void GPU_vertformat_safe_attr_name(const char *attr_name, char *r_safe_name, uint max_len);
149 
150 /* format conversion */
151 
152 typedef struct GPUPackedNormal {
153  int x : 10;
154  int y : 10;
155  int z : 10;
156  int w : 2; /* 0 by default, can manually set to { -2, -1, 0, 1 } */
158 
159 typedef struct GPUNormal {
160  union {
162  short high[3];
163  };
165 
166 /* OpenGL ES packs in a different order as desktop GL but component conversion is the same.
167  * Of the code here, only struct GPUPackedNormal needs to change. */
168 
169 #define SIGNED_INT_10_MAX 511
170 #define SIGNED_INT_10_MIN -512
171 
172 BLI_INLINE int clampi(int x, int min_allowed, int max_allowed)
173 {
174 #if TRUST_NO_ONE
175  assert(min_allowed <= max_allowed);
176 #endif
177  if (x < min_allowed) {
178  return min_allowed;
179  }
180  else if (x > max_allowed) {
181  return max_allowed;
182  }
183  else {
184  return x;
185  }
186 }
187 
189 {
190  int qx = x * 511.0f;
192 }
193 
195 {
196  /* 16-bit signed --> 10-bit signed */
197  /* TODO: round? */
198  return x >> 6;
199 }
200 
202 {
203  GPUPackedNormal n = {
207  };
208  return n;
209 }
210 
212 {
213  GPUPackedNormal n = {
217  };
218  return n;
219 }
220 
222  const float data[3],
223  const bool do_hq_normals)
224 {
225  if (do_hq_normals) {
226  normal_float_to_short_v3(gpu_normal->high, data);
227  }
228  else {
229  gpu_normal->low = GPU_normal_convert_i10_v3(data);
230  }
231 }
232 
233 #ifdef __cplusplus
234 }
235 #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:70
unsigned int uint
Definition: BLI_sys_types.h:67
struct GPUShader GPUShader
Definition: GPU_shader.h:20
#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
SyclQueue void void * src
SyclQueue void * dest
format
Definition: logImageCore.h:38
GPUPackedNormal low
uchar names[GPU_VERT_ATTR_MAX_NAMES]
GPUVertAttr attrs[GPU_VERT_ATTR_MAX_LEN]
char names[GPU_VERT_ATTR_NAMES_BUF_LEN]