2018-07-17 14:46:44 +02:00
|
|
|
/*
|
|
|
|
* ***** BEGIN GPL LICENSE BLOCK *****
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* Contributor(s): Blender Foundation, Clément Foucault
|
|
|
|
*
|
|
|
|
* ***** END GPL LICENSE BLOCK *****
|
|
|
|
*/
|
|
|
|
|
2018-07-18 00:12:21 +02:00
|
|
|
/** \file blender/gpu/intern/gpu_vertex_format.c
|
2018-07-17 14:46:44 +02:00
|
|
|
* \ingroup gpu
|
|
|
|
*
|
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-17 21:11:23 +02:00
|
|
|
#include "GPU_vertex_format.h"
|
|
|
|
#include "gpu_vertex_format_private.h"
|
2017-04-16 11:00:59 -04:00
|
|
|
#include <stddef.h>
|
2016-09-13 02:18:33 -04:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#define PACK_DEBUG 0
|
|
|
|
|
|
|
|
#if PACK_DEBUG
|
|
|
|
#include <stdio.h>
|
|
|
|
#endif
|
|
|
|
|
2018-07-18 23:09:31 +10:00
|
|
|
void GPU_vertformat_clear(GPUVertFormat *format)
|
2018-07-17 14:46:44 +02:00
|
|
|
{
|
2016-09-13 02:18:33 -04:00
|
|
|
#if TRUST_NO_ONE
|
2018-07-18 00:12:21 +02:00
|
|
|
memset(format, 0, sizeof(GPUVertFormat));
|
2016-09-13 02:18:33 -04:00
|
|
|
#else
|
2018-07-08 13:05:41 +02:00
|
|
|
format->attr_len = 0;
|
2016-09-13 02:18:33 -04:00
|
|
|
format->packed = false;
|
2016-10-13 14:49:33 -04:00
|
|
|
format->name_offset = 0;
|
2018-07-08 13:05:41 +02:00
|
|
|
format->name_len = 0;
|
2017-05-17 01:43:49 +02:00
|
|
|
|
2018-07-18 00:12:21 +02:00
|
|
|
for (unsigned i = 0; i < GPU_VERT_ATTR_MAX_LEN; i++) {
|
2018-07-08 13:05:41 +02:00
|
|
|
format->attribs[i].name_len = 0;
|
2016-09-13 02:18:33 -04:00
|
|
|
}
|
2018-07-17 14:46:44 +02:00
|
|
|
#endif
|
|
|
|
}
|
2016-09-13 02:18:33 -04:00
|
|
|
|
2018-07-18 23:09:31 +10:00
|
|
|
void GPU_vertformat_copy(GPUVertFormat *dest, const GPUVertFormat *src)
|
2018-07-17 14:46:44 +02:00
|
|
|
{
|
|
|
|
/* copy regular struct fields */
|
2018-07-18 00:12:21 +02:00
|
|
|
memcpy(dest, src, sizeof(GPUVertFormat));
|
2017-05-11 20:56:53 +10:00
|
|
|
|
2018-07-17 14:46:44 +02:00
|
|
|
for (unsigned i = 0; i < dest->attr_len; i++) {
|
|
|
|
for (unsigned j = 0; j < dest->attribs[i].name_len; j++) {
|
2017-05-13 22:52:29 +02:00
|
|
|
dest->attribs[i].name[j] = (char *)dest + (src->attribs[i].name[j] - ((char *)src));
|
2018-07-17 14:46:44 +02:00
|
|
|
}
|
2016-09-15 21:45:10 +02:00
|
|
|
}
|
2018-07-17 14:46:44 +02:00
|
|
|
}
|
2016-09-15 21:45:10 +02:00
|
|
|
|
2018-07-18 00:12:21 +02:00
|
|
|
static GLenum convert_comp_type_to_gl(GPUVertCompType type)
|
2018-07-17 14:46:44 +02:00
|
|
|
{
|
2017-04-07 16:00:03 -04:00
|
|
|
static const GLenum table[] = {
|
2018-07-18 00:12:21 +02:00
|
|
|
[GPU_COMP_I8] = GL_BYTE,
|
|
|
|
[GPU_COMP_U8] = GL_UNSIGNED_BYTE,
|
|
|
|
[GPU_COMP_I16] = GL_SHORT,
|
|
|
|
[GPU_COMP_U16] = GL_UNSIGNED_SHORT,
|
|
|
|
[GPU_COMP_I32] = GL_INT,
|
|
|
|
[GPU_COMP_U32] = GL_UNSIGNED_INT,
|
2017-04-07 16:00:03 -04:00
|
|
|
|
2018-07-18 00:12:21 +02:00
|
|
|
[GPU_COMP_F32] = GL_FLOAT,
|
2017-04-07 16:00:03 -04:00
|
|
|
|
2018-07-18 00:12:21 +02:00
|
|
|
[GPU_COMP_I10] = GL_INT_2_10_10_10_REV
|
2018-07-17 14:46:44 +02:00
|
|
|
};
|
2017-04-07 16:00:03 -04:00
|
|
|
return table[type];
|
2018-07-17 14:46:44 +02:00
|
|
|
}
|
2017-04-07 16:00:03 -04:00
|
|
|
|
2018-07-18 00:12:21 +02:00
|
|
|
static unsigned comp_sz(GPUVertCompType type)
|
2018-07-17 14:46:44 +02:00
|
|
|
{
|
2016-09-13 02:18:33 -04:00
|
|
|
#if TRUST_NO_ONE
|
2018-07-18 00:12:21 +02:00
|
|
|
assert(type <= GPU_COMP_F32); /* other types have irregular sizes (not bytes) */
|
2016-09-13 02:18:33 -04:00
|
|
|
#endif
|
2018-07-18 23:09:31 +10:00
|
|
|
const GLubyte sizes[] = {1, 1, 2, 2, 4, 4, 4};
|
2017-04-07 16:00:03 -04:00
|
|
|
return sizes[type];
|
2018-07-17 14:46:44 +02:00
|
|
|
}
|
2016-09-13 02:18:33 -04:00
|
|
|
|
2018-07-18 00:12:21 +02:00
|
|
|
static unsigned attrib_sz(const GPUVertAttr *a)
|
2018-07-17 14:46:44 +02:00
|
|
|
{
|
2018-07-18 00:12:21 +02:00
|
|
|
if (a->comp_type == GPU_COMP_I10) {
|
2018-07-17 14:46:44 +02:00
|
|
|
return 4; /* always packed as 10_10_10_2 */
|
2016-09-13 02:18:33 -04:00
|
|
|
}
|
2018-07-17 14:46:44 +02:00
|
|
|
return a->comp_len * comp_sz(a->comp_type);
|
|
|
|
}
|
2016-09-13 02:18:33 -04:00
|
|
|
|
2018-07-18 00:12:21 +02:00
|
|
|
static unsigned attrib_align(const GPUVertAttr *a)
|
2018-07-17 14:46:44 +02:00
|
|
|
{
|
2018-07-18 00:12:21 +02:00
|
|
|
if (a->comp_type == GPU_COMP_I10) {
|
2018-07-17 14:46:44 +02:00
|
|
|
return 4; /* always packed as 10_10_10_2 */
|
|
|
|
}
|
2016-09-13 02:18:33 -04:00
|
|
|
unsigned c = comp_sz(a->comp_type);
|
2018-07-17 14:46:44 +02:00
|
|
|
if (a->comp_len == 3 && c <= 2) {
|
|
|
|
return 4 * c; /* AMD HW can't fetch these well, so pad it out (other vendors too?) */
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return c; /* most fetches are ok if components are naturally aligned */
|
2016-09-13 02:18:33 -04:00
|
|
|
}
|
2018-07-17 14:46:44 +02:00
|
|
|
}
|
2016-09-13 02:18:33 -04:00
|
|
|
|
2018-07-18 23:09:31 +10:00
|
|
|
unsigned vertex_buffer_size(const GPUVertFormat *format, unsigned vertex_len)
|
2018-07-17 14:46:44 +02:00
|
|
|
{
|
2016-09-13 02:18:33 -04:00
|
|
|
#if TRUST_NO_ONE
|
|
|
|
assert(format->packed && format->stride > 0);
|
|
|
|
#endif
|
2018-07-08 13:05:41 +02:00
|
|
|
return format->stride * vertex_len;
|
2018-07-17 14:46:44 +02:00
|
|
|
}
|
2016-09-13 02:18:33 -04:00
|
|
|
|
2018-07-18 23:09:31 +10:00
|
|
|
static const char *copy_attrib_name(GPUVertFormat *format, const char *name)
|
2018-07-17 14:46:44 +02:00
|
|
|
{
|
|
|
|
/* strncpy does 110% of what we need; let's do exactly 100% */
|
2018-07-18 23:09:31 +10:00
|
|
|
char *name_copy = format->names + format->name_offset;
|
2018-07-18 00:12:21 +02:00
|
|
|
unsigned available = GPU_VERT_ATTR_NAMES_BUF_LEN - format->name_offset;
|
2016-10-13 14:49:33 -04:00
|
|
|
bool terminated = false;
|
|
|
|
|
2018-07-17 14:46:44 +02:00
|
|
|
for (unsigned i = 0; i < available; ++i) {
|
2016-10-13 14:49:33 -04:00
|
|
|
const char c = name[i];
|
|
|
|
name_copy[i] = c;
|
2018-07-17 14:46:44 +02:00
|
|
|
if (c == '\0') {
|
2016-10-13 14:49:33 -04:00
|
|
|
terminated = true;
|
|
|
|
format->name_offset += (i + 1);
|
|
|
|
break;
|
|
|
|
}
|
2018-07-17 14:46:44 +02:00
|
|
|
}
|
2016-10-13 14:49:33 -04:00
|
|
|
#if TRUST_NO_ONE
|
|
|
|
assert(terminated);
|
2018-07-18 00:12:21 +02:00
|
|
|
assert(format->name_offset <= GPU_VERT_ATTR_NAMES_BUF_LEN);
|
2017-04-19 15:57:37 +10:00
|
|
|
#else
|
|
|
|
(void)terminated;
|
2016-10-13 14:49:33 -04:00
|
|
|
#endif
|
|
|
|
return name_copy;
|
2018-07-17 14:46:44 +02:00
|
|
|
}
|
2016-10-13 14:49:33 -04:00
|
|
|
|
2018-07-18 23:09:31 +10:00
|
|
|
unsigned GPU_vertformat_attr_add(
|
|
|
|
GPUVertFormat *format, const char *name,
|
|
|
|
GPUVertCompType comp_type, unsigned comp_len, GPUVertFetchMode fetch_mode)
|
2018-07-17 14:46:44 +02:00
|
|
|
{
|
2016-09-13 02:18:33 -04:00
|
|
|
#if TRUST_NO_ONE
|
2018-07-18 00:12:21 +02:00
|
|
|
assert(format->name_len < GPU_VERT_ATTR_MAX_LEN); /* there's room for more */
|
|
|
|
assert(format->attr_len < GPU_VERT_ATTR_MAX_LEN); /* there's room for more */
|
2018-07-17 14:46:44 +02:00
|
|
|
assert(!format->packed); /* packed means frozen/locked */
|
2018-07-08 13:05:41 +02:00
|
|
|
assert((comp_len >= 1 && comp_len <= 4) || comp_len == 8 || comp_len == 12 || comp_len == 16);
|
2018-07-17 14:46:44 +02:00
|
|
|
|
|
|
|
switch (comp_type) {
|
2018-07-18 00:12:21 +02:00
|
|
|
case GPU_COMP_F32:
|
2018-07-17 14:46:44 +02:00
|
|
|
/* float type can only kept as float */
|
2018-07-18 00:12:21 +02:00
|
|
|
assert(fetch_mode == GPU_FETCH_FLOAT);
|
2016-10-08 16:58:06 -04:00
|
|
|
break;
|
2018-07-18 00:12:21 +02:00
|
|
|
case GPU_COMP_I10:
|
2018-07-17 14:46:44 +02:00
|
|
|
/* 10_10_10 format intended for normals (xyz) or colors (rgb)
|
|
|
|
* extra component packed.w can be manually set to { -2, -1, 0, 1 } */
|
2018-07-08 13:05:41 +02:00
|
|
|
assert(comp_len == 3 || comp_len == 4);
|
2018-07-18 00:12:21 +02:00
|
|
|
assert(fetch_mode == GPU_FETCH_INT_TO_FLOAT_UNIT); /* not strictly required, may relax later */
|
2016-11-11 19:39:56 -06:00
|
|
|
break;
|
2016-10-08 16:58:06 -04:00
|
|
|
default:
|
2018-07-17 14:46:44 +02:00
|
|
|
/* integer types can be kept as int or converted/normalized to float */
|
2018-07-18 00:12:21 +02:00
|
|
|
assert(fetch_mode != GPU_FETCH_FLOAT);
|
2018-07-17 14:46:44 +02:00
|
|
|
/* only support float matrices (see Batch_update_program_bindings) */
|
2018-07-08 13:05:41 +02:00
|
|
|
assert(comp_len != 8 && comp_len != 12 && comp_len != 16);
|
2018-07-17 14:46:44 +02:00
|
|
|
}
|
2016-09-13 02:18:33 -04:00
|
|
|
#endif
|
2018-07-17 14:46:44 +02:00
|
|
|
format->name_len++; /* multiname support */
|
2016-09-13 02:18:33 -04:00
|
|
|
|
2018-07-08 13:05:41 +02:00
|
|
|
const unsigned attrib_id = format->attr_len++;
|
2018-07-18 23:09:31 +10:00
|
|
|
GPUVertAttr *attrib = format->attribs + attrib_id;
|
2016-09-13 02:18:33 -04:00
|
|
|
|
2018-07-08 13:05:41 +02:00
|
|
|
attrib->name[attrib->name_len++] = copy_attrib_name(format, name);
|
2016-09-13 02:18:33 -04:00
|
|
|
attrib->comp_type = comp_type;
|
2017-04-07 16:00:03 -04:00
|
|
|
attrib->gl_comp_type = convert_comp_type_to_gl(comp_type);
|
2018-07-18 00:12:21 +02:00
|
|
|
attrib->comp_len = (comp_type == GPU_COMP_I10) ? 4 : comp_len; /* system needs 10_10_10_2 to be 4 or BGRA */
|
2016-09-13 02:18:33 -04:00
|
|
|
attrib->sz = attrib_sz(attrib);
|
2018-07-17 14:46:44 +02:00
|
|
|
attrib->offset = 0; /* offsets & stride are calculated later (during pack) */
|
2016-09-13 02:18:33 -04:00
|
|
|
attrib->fetch_mode = fetch_mode;
|
|
|
|
|
|
|
|
return attrib_id;
|
2018-07-17 14:46:44 +02:00
|
|
|
}
|
2016-09-13 02:18:33 -04:00
|
|
|
|
2018-07-18 23:09:31 +10:00
|
|
|
void GPU_vertformat_alias_add(GPUVertFormat *format, const char *alias)
|
2018-07-17 14:46:44 +02:00
|
|
|
{
|
2018-07-18 23:09:31 +10:00
|
|
|
GPUVertAttr *attrib = format->attribs + (format->attr_len - 1);
|
2017-05-13 22:52:29 +02:00
|
|
|
#if TRUST_NO_ONE
|
2018-07-18 00:12:21 +02:00
|
|
|
assert(format->name_len < GPU_VERT_ATTR_MAX_LEN); /* there's room for more */
|
|
|
|
assert(attrib->name_len < GPU_VERT_ATTR_MAX_NAMES);
|
2017-05-13 22:52:29 +02:00
|
|
|
#endif
|
2018-07-17 14:46:44 +02:00
|
|
|
format->name_len++; /* multiname support */
|
2018-07-08 13:05:41 +02:00
|
|
|
attrib->name[attrib->name_len++] = copy_attrib_name(format, alias);
|
2018-07-17 14:46:44 +02:00
|
|
|
}
|
2017-05-13 22:52:29 +02:00
|
|
|
|
2016-09-13 02:18:33 -04:00
|
|
|
unsigned padding(unsigned offset, unsigned alignment)
|
2018-07-17 14:46:44 +02:00
|
|
|
{
|
2016-09-13 02:18:33 -04:00
|
|
|
const unsigned mod = offset % alignment;
|
|
|
|
return (mod == 0) ? 0 : (alignment - mod);
|
2018-07-17 14:46:44 +02:00
|
|
|
}
|
2016-09-13 02:18:33 -04:00
|
|
|
|
|
|
|
#if PACK_DEBUG
|
|
|
|
static void show_pack(unsigned a_idx, unsigned sz, unsigned pad)
|
2018-07-17 14:46:44 +02:00
|
|
|
{
|
2016-09-13 02:18:33 -04:00
|
|
|
const char c = 'A' + a_idx;
|
2018-07-17 14:46:44 +02:00
|
|
|
for (unsigned i = 0; i < pad; ++i) {
|
2016-09-13 02:18:33 -04:00
|
|
|
putchar('-');
|
2018-07-17 14:46:44 +02:00
|
|
|
}
|
|
|
|
for (unsigned i = 0; i < sz; ++i) {
|
2016-09-13 02:18:33 -04:00
|
|
|
putchar(c);
|
|
|
|
}
|
2018-07-17 14:46:44 +02:00
|
|
|
}
|
2016-09-13 02:18:33 -04:00
|
|
|
#endif
|
|
|
|
|
2018-07-18 23:09:31 +10:00
|
|
|
void VertexFormat_pack(GPUVertFormat *format)
|
2018-07-17 14:46:44 +02:00
|
|
|
{
|
|
|
|
/* For now, attributes are packed in the order they were added,
|
|
|
|
* making sure each attrib is naturally aligned (add padding where necessary)
|
|
|
|
* Later we can implement more efficient packing w/ reordering
|
|
|
|
* (keep attrib ID order, adjust their offsets to reorder in buffer). */
|
2016-09-13 02:18:33 -04:00
|
|
|
|
2018-07-17 14:46:44 +02:00
|
|
|
/* TODO: realloc just enough to hold the final combo string. And just enough to
|
|
|
|
* hold used attribs, not all 16. */
|
2016-09-13 02:18:33 -04:00
|
|
|
|
2018-07-18 23:09:31 +10:00
|
|
|
GPUVertAttr *a0 = format->attribs + 0;
|
2016-09-13 02:18:33 -04:00
|
|
|
a0->offset = 0;
|
|
|
|
unsigned offset = a0->sz;
|
|
|
|
|
|
|
|
#if PACK_DEBUG
|
|
|
|
show_pack(0, a0->sz, 0);
|
|
|
|
#endif
|
|
|
|
|
2018-07-17 14:46:44 +02:00
|
|
|
for (unsigned a_idx = 1; a_idx < format->attr_len; ++a_idx) {
|
2018-07-18 23:09:31 +10:00
|
|
|
GPUVertAttr *a = format->attribs + a_idx;
|
2016-09-13 02:18:33 -04:00
|
|
|
unsigned mid_padding = padding(offset, attrib_align(a));
|
|
|
|
offset += mid_padding;
|
|
|
|
a->offset = offset;
|
|
|
|
offset += a->sz;
|
|
|
|
|
|
|
|
#if PACK_DEBUG
|
|
|
|
show_pack(a_idx, a->sz, mid_padding);
|
|
|
|
#endif
|
2018-07-17 14:46:44 +02:00
|
|
|
}
|
2016-09-13 02:18:33 -04:00
|
|
|
|
|
|
|
unsigned end_padding = padding(offset, attrib_align(a0));
|
|
|
|
|
|
|
|
#if PACK_DEBUG
|
|
|
|
show_pack(0, 0, end_padding);
|
|
|
|
putchar('\n');
|
|
|
|
#endif
|
|
|
|
format->stride = offset + end_padding;
|
|
|
|
format->packed = true;
|
2018-07-17 14:46:44 +02:00
|
|
|
}
|
2016-11-13 20:18:51 -06:00
|
|
|
|
|
|
|
|
2018-07-17 14:46:44 +02:00
|
|
|
/* OpenGL ES packs in a different order as desktop GL but component conversion is the same.
|
2018-07-18 00:12:21 +02:00
|
|
|
* Of the code here, only struct GPUPackedNormal needs to change. */
|
2016-11-13 20:18:51 -06:00
|
|
|
|
|
|
|
#define SIGNED_INT_10_MAX 511
|
|
|
|
#define SIGNED_INT_10_MIN -512
|
|
|
|
|
|
|
|
static int clampi(int x, int min_allowed, int max_allowed)
|
2018-07-17 14:46:44 +02:00
|
|
|
{
|
2016-11-13 20:18:51 -06:00
|
|
|
#if TRUST_NO_ONE
|
|
|
|
assert(min_allowed <= max_allowed);
|
|
|
|
#endif
|
2018-07-17 14:46:44 +02:00
|
|
|
if (x < min_allowed) {
|
2016-11-13 20:18:51 -06:00
|
|
|
return min_allowed;
|
2018-07-17 14:46:44 +02:00
|
|
|
}
|
|
|
|
else if (x > max_allowed) {
|
2016-11-13 20:18:51 -06:00
|
|
|
return max_allowed;
|
2018-07-17 14:46:44 +02:00
|
|
|
}
|
|
|
|
else {
|
2016-11-13 20:18:51 -06:00
|
|
|
return x;
|
|
|
|
}
|
2018-07-17 14:46:44 +02:00
|
|
|
}
|
2016-11-13 20:18:51 -06:00
|
|
|
|
|
|
|
static int quantize(float x)
|
2018-07-17 14:46:44 +02:00
|
|
|
{
|
2016-11-13 20:18:51 -06:00
|
|
|
int qx = x * 511.0f;
|
|
|
|
return clampi(qx, SIGNED_INT_10_MIN, SIGNED_INT_10_MAX);
|
2018-07-17 14:46:44 +02:00
|
|
|
}
|
2016-11-13 20:18:51 -06:00
|
|
|
|
2017-05-21 18:07:23 -04:00
|
|
|
static int convert_i16(short x)
|
2018-07-17 14:46:44 +02:00
|
|
|
{
|
|
|
|
/* 16-bit signed --> 10-bit signed */
|
|
|
|
/* TODO: round? */
|
2017-05-21 18:07:23 -04:00
|
|
|
return x >> 6;
|
2018-07-17 14:46:44 +02:00
|
|
|
}
|
2017-05-21 18:07:23 -04:00
|
|
|
|
2018-07-18 00:12:21 +02:00
|
|
|
GPUPackedNormal GPU_normal_convert_i10_v3(const float data[3])
|
2018-07-17 14:46:44 +02:00
|
|
|
{
|
2018-07-18 00:12:21 +02:00
|
|
|
GPUPackedNormal n = { .x = quantize(data[0]), .y = quantize(data[1]), .z = quantize(data[2]) };
|
2016-11-13 20:18:51 -06:00
|
|
|
return n;
|
2018-07-17 14:46:44 +02:00
|
|
|
}
|
2016-11-13 20:18:51 -06:00
|
|
|
|
2018-07-18 00:12:21 +02:00
|
|
|
GPUPackedNormal GPU_normal_convert_i10_s3(const short data[3])
|
2018-07-17 14:46:44 +02:00
|
|
|
{
|
2018-07-18 00:12:21 +02:00
|
|
|
GPUPackedNormal n = { .x = convert_i16(data[0]), .y = convert_i16(data[1]), .z = convert_i16(data[2]) };
|
2017-05-13 16:38:32 +02:00
|
|
|
return n;
|
2018-07-17 14:46:44 +02:00
|
|
|
}
|