2018-07-17 14:46:44 +02:00
|
|
|
/*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
* The Original Code is Copyright (C) 2016 by Mike Erwin.
|
|
|
|
* All rights reserved.
|
|
|
|
*/
|
2016-09-13 02:18:33 -04:00
|
|
|
|
2019-02-18 08:08:12 +11:00
|
|
|
/** \file
|
|
|
|
* \ingroup gpu
|
2018-07-17 14:46:44 +02:00
|
|
|
*
|
2018-07-18 00:12:21 +02:00
|
|
|
* GPU vertex format
|
2018-07-17 14:46:44 +02:00
|
|
|
*/
|
2016-09-13 02:18:33 -04:00
|
|
|
|
2018-07-18 00:12:21 +02:00
|
|
|
#pragma once
|
2016-09-13 02:18:33 -04:00
|
|
|
|
2019-05-14 13:45:55 +02:00
|
|
|
#include "BLI_assert.h"
|
2020-03-19 09:33:03 +01:00
|
|
|
#include "BLI_compiler_compat.h"
|
2020-12-18 16:06:26 +01:00
|
|
|
#include "BLI_math_geom.h"
|
2020-03-19 09:33:03 +01:00
|
|
|
#include "GPU_common.h"
|
2016-09-13 02:18:33 -04:00
|
|
|
|
2020-03-02 15:28:47 +01:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2018-07-18 00:12:21 +02:00
|
|
|
#define GPU_VERT_ATTR_MAX_LEN 16
|
2019-07-14 16:49:44 +02:00
|
|
|
#define GPU_VERT_ATTR_MAX_NAMES 6
|
2019-08-14 22:18:47 +02:00
|
|
|
#define GPU_VERT_ATTR_NAMES_BUF_LEN 256
|
|
|
|
#define GPU_VERT_FORMAT_MAX_NAMES 63 /* More than enough, actual max is ~30. */
|
|
|
|
/* Computed as GPU_VERT_ATTR_NAMES_BUF_LEN / 30 (actual max format name). */
|
2020-04-03 16:59:34 +11:00
|
|
|
#define GPU_MAX_SAFE_ATTR_NAME 12
|
2016-09-13 02:18:33 -04:00
|
|
|
|
2016-11-11 19:39:56 -06:00
|
|
|
typedef enum {
|
2020-07-27 23:56:43 +02:00
|
|
|
GPU_COMP_I8 = 0,
|
2018-07-18 00:12:21 +02:00
|
|
|
GPU_COMP_U8,
|
|
|
|
GPU_COMP_I16,
|
|
|
|
GPU_COMP_U16,
|
|
|
|
GPU_COMP_I32,
|
|
|
|
GPU_COMP_U32,
|
2016-11-11 19:39:56 -06:00
|
|
|
|
2018-07-18 00:12:21 +02:00
|
|
|
GPU_COMP_F32,
|
2016-11-11 19:39:56 -06:00
|
|
|
|
2019-04-16 16:40:47 +02:00
|
|
|
GPU_COMP_I10,
|
2020-07-27 23:56:43 +02:00
|
|
|
/* Warning! adjust GPUVertAttr if changing. */
|
2018-07-18 00:12:21 +02:00
|
|
|
} GPUVertCompType;
|
2016-11-11 19:39:56 -06:00
|
|
|
|
2016-09-13 02:18:33 -04:00
|
|
|
typedef enum {
|
2020-07-27 23:56:43 +02:00
|
|
|
GPU_FETCH_FLOAT = 0,
|
2018-07-18 00:12:21 +02:00
|
|
|
GPU_FETCH_INT,
|
|
|
|
GPU_FETCH_INT_TO_FLOAT_UNIT, /* 127 (ubyte) -> 0.5 (and so on for other int types) */
|
2019-04-16 16:40:47 +02:00
|
|
|
GPU_FETCH_INT_TO_FLOAT, /* 127 (any int type) -> 127.0 */
|
2020-07-27 23:56:43 +02:00
|
|
|
/* Warning! adjust GPUVertAttr if changing. */
|
2018-07-18 00:12:21 +02:00
|
|
|
} GPUVertFetchMode;
|
|
|
|
|
|
|
|
typedef struct GPUVertAttr {
|
2020-07-27 23:56:43 +02:00
|
|
|
/* GPUVertFetchMode */
|
2019-05-14 16:31:04 +02:00
|
|
|
uint fetch_mode : 2;
|
2020-07-27 23:56:43 +02:00
|
|
|
/* GPUVertCompType */
|
2019-05-14 16:31:04 +02:00
|
|
|
uint comp_type : 3;
|
2019-05-13 23:31:43 +02:00
|
|
|
/* 1 to 4 or 8 or 12 or 16 */
|
|
|
|
uint comp_len : 5;
|
|
|
|
/* size in bytes, 1 to 64 */
|
|
|
|
uint sz : 7;
|
|
|
|
/* from beginning of vertex, in bytes */
|
|
|
|
uint offset : 11;
|
|
|
|
/* up to GPU_VERT_ATTR_MAX_NAMES */
|
|
|
|
uint name_len : 3;
|
|
|
|
uchar names[GPU_VERT_ATTR_MAX_NAMES];
|
2018-07-18 00:12:21 +02:00
|
|
|
} GPUVertAttr;
|
2016-09-13 02:18:33 -04:00
|
|
|
|
2019-05-14 13:45:55 +02:00
|
|
|
BLI_STATIC_ASSERT(GPU_VERT_ATTR_NAMES_BUF_LEN <= 256,
|
|
|
|
"We use uchar as index inside the name buffer "
|
2019-11-25 00:55:11 +11:00
|
|
|
"so GPU_VERT_ATTR_NAMES_BUF_LEN needs to be "
|
2019-05-14 13:45:55 +02:00
|
|
|
"smaller than GPUVertFormat->name_offset and "
|
|
|
|
"GPUVertAttr->names maximum value");
|
|
|
|
|
2018-07-18 00:12:21 +02:00
|
|
|
typedef struct GPUVertFormat {
|
2019-01-29 07:46:25 +11:00
|
|
|
/** 0 to 16 (GPU_VERT_ATTR_MAX_LEN). */
|
2019-05-13 23:31:43 +02:00
|
|
|
uint attr_len : 5;
|
2019-08-14 22:18:47 +02:00
|
|
|
/** Total count of active vertex attribute names. (max GPU_VERT_FORMAT_MAX_NAMES) */
|
|
|
|
uint name_len : 6;
|
2019-05-13 23:31:43 +02:00
|
|
|
/** Stride in bytes, 1 to 1024. */
|
|
|
|
uint stride : 11;
|
|
|
|
/** Has the format been packed. */
|
|
|
|
uint packed : 1;
|
|
|
|
/** Current offset in names[]. */
|
|
|
|
uint name_offset : 8;
|
2020-04-03 16:59:34 +11:00
|
|
|
/** Store each attribute in one contiguous buffer region. */
|
2019-07-14 16:49:44 +02:00
|
|
|
uint deinterleaved : 1;
|
2019-05-13 23:31:43 +02:00
|
|
|
|
2019-01-29 07:46:25 +11:00
|
|
|
GPUVertAttr attrs[GPU_VERT_ATTR_MAX_LEN];
|
2019-05-13 23:31:43 +02:00
|
|
|
char names[GPU_VERT_ATTR_NAMES_BUF_LEN];
|
2018-07-18 00:12:21 +02:00
|
|
|
} GPUVertFormat;
|
2016-09-13 02:18:33 -04:00
|
|
|
|
2020-06-02 12:11:39 +02:00
|
|
|
struct GPUShader;
|
2018-10-09 11:17:29 +02:00
|
|
|
|
2018-07-18 23:09:31 +10:00
|
|
|
void GPU_vertformat_clear(GPUVertFormat *);
|
|
|
|
void GPU_vertformat_copy(GPUVertFormat *dest, const GPUVertFormat *src);
|
2020-06-02 12:11:39 +02:00
|
|
|
void GPU_vertformat_from_shader(GPUVertFormat *format, const struct GPUShader *shader);
|
2016-09-15 21:45:10 +02:00
|
|
|
|
2018-07-18 23:09:31 +10:00
|
|
|
uint GPU_vertformat_attr_add(
|
|
|
|
GPUVertFormat *, const char *name, GPUVertCompType, uint comp_len, GPUVertFetchMode);
|
|
|
|
void GPU_vertformat_alias_add(GPUVertFormat *, const char *alias);
|
2019-05-13 23:31:43 +02:00
|
|
|
|
2020-03-09 16:27:24 +01:00
|
|
|
void GPU_vertformat_multiload_enable(GPUVertFormat *format, int load_count);
|
|
|
|
|
2019-07-14 16:49:44 +02:00
|
|
|
void GPU_vertformat_deinterleave(GPUVertFormat *format);
|
|
|
|
|
2018-10-05 15:10:56 +02:00
|
|
|
int GPU_vertformat_attr_id_get(const GPUVertFormat *, const char *name);
|
2016-09-13 02:18:33 -04:00
|
|
|
|
2019-05-13 23:31:43 +02:00
|
|
|
BLI_INLINE const char *GPU_vertformat_attr_name_get(const GPUVertFormat *format,
|
|
|
|
const GPUVertAttr *attr,
|
|
|
|
uint n_idx)
|
|
|
|
{
|
|
|
|
return format->names + attr->names[n_idx];
|
|
|
|
}
|
2018-12-07 00:58:17 +01:00
|
|
|
|
2020-06-19 17:02:55 +02:00
|
|
|
/* WARNING: Can only rename using a string with same character count.
|
|
|
|
* WARNING: This removes all other aliases of this attrib */
|
|
|
|
void GPU_vertformat_attr_rename(GPUVertFormat *format, int attr, const char *new_name);
|
|
|
|
|
2020-04-03 16:59:34 +11:00
|
|
|
void GPU_vertformat_safe_attr_name(const char *attr_name, char *r_safe_name, uint max_len);
|
2019-08-14 22:18:47 +02:00
|
|
|
|
2018-07-17 14:46:44 +02:00
|
|
|
/* format conversion */
|
2016-11-13 20:18:51 -06:00
|
|
|
|
2018-07-18 00:12:21 +02:00
|
|
|
typedef struct GPUPackedNormal {
|
2016-11-13 20:18:51 -06:00
|
|
|
int x : 10;
|
|
|
|
int y : 10;
|
|
|
|
int z : 10;
|
2018-07-18 23:09:31 +10:00
|
|
|
int w : 2; /* 0 by default, can manually set to { -2, -1, 0, 1 } */
|
2018-07-18 00:12:21 +02:00
|
|
|
} GPUPackedNormal;
|
2016-11-13 20:18:51 -06:00
|
|
|
|
2020-12-18 16:06:26 +01:00
|
|
|
typedef struct GPUNormal {
|
|
|
|
union {
|
|
|
|
GPUPackedNormal low;
|
|
|
|
short high[3];
|
|
|
|
};
|
|
|
|
} GPUNormal;
|
|
|
|
|
2019-07-14 16:49:44 +02:00
|
|
|
/* OpenGL ES packs in a different order as desktop GL but component conversion is the same.
|
|
|
|
* Of the code here, only struct GPUPackedNormal needs to change. */
|
|
|
|
|
|
|
|
#define SIGNED_INT_10_MAX 511
|
|
|
|
#define SIGNED_INT_10_MIN -512
|
|
|
|
|
|
|
|
BLI_INLINE int clampi(int x, int min_allowed, int max_allowed)
|
|
|
|
{
|
|
|
|
#if TRUST_NO_ONE
|
|
|
|
assert(min_allowed <= max_allowed);
|
|
|
|
#endif
|
|
|
|
if (x < min_allowed) {
|
|
|
|
return min_allowed;
|
|
|
|
}
|
|
|
|
else if (x > max_allowed) {
|
|
|
|
return max_allowed;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BLI_INLINE int gpu_convert_normalized_f32_to_i10(float x)
|
|
|
|
{
|
|
|
|
int qx = x * 511.0f;
|
|
|
|
return clampi(qx, SIGNED_INT_10_MIN, SIGNED_INT_10_MAX);
|
|
|
|
}
|
|
|
|
|
|
|
|
BLI_INLINE int gpu_convert_i16_to_i10(short x)
|
|
|
|
{
|
|
|
|
/* 16-bit signed --> 10-bit signed */
|
|
|
|
/* TODO: round? */
|
|
|
|
return x >> 6;
|
|
|
|
}
|
|
|
|
|
|
|
|
BLI_INLINE GPUPackedNormal GPU_normal_convert_i10_v3(const float data[3])
|
|
|
|
{
|
|
|
|
GPUPackedNormal n = {
|
2019-08-14 12:52:17 -06:00
|
|
|
gpu_convert_normalized_f32_to_i10(data[0]),
|
|
|
|
gpu_convert_normalized_f32_to_i10(data[1]),
|
|
|
|
gpu_convert_normalized_f32_to_i10(data[2]),
|
2019-07-14 16:49:44 +02:00
|
|
|
};
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
BLI_INLINE GPUPackedNormal GPU_normal_convert_i10_s3(const short data[3])
|
|
|
|
{
|
|
|
|
GPUPackedNormal n = {
|
2019-08-14 12:52:17 -06:00
|
|
|
gpu_convert_i16_to_i10(data[0]),
|
|
|
|
gpu_convert_i16_to_i10(data[1]),
|
|
|
|
gpu_convert_i16_to_i10(data[2]),
|
2019-07-14 16:49:44 +02:00
|
|
|
};
|
|
|
|
return n;
|
|
|
|
}
|
2018-07-17 14:46:44 +02:00
|
|
|
|
2020-12-18 16:06:26 +01:00
|
|
|
BLI_INLINE void GPU_normal_convert_v3(GPUNormal *gpu_normal,
|
|
|
|
const float data[3],
|
|
|
|
const bool do_hq_normals)
|
|
|
|
{
|
|
|
|
if (do_hq_normals) {
|
|
|
|
normal_float_to_short_v3(gpu_normal->high, data);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
gpu_normal->low = GPU_normal_convert_i10_v3(data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-02 15:28:47 +01:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|