GWN: Port to GPU module: Move files to GPU

This does not include all the struct and type renaming. Only files were
renamed.

gwn_batch.c/h was fusioned with GPU_batch.c/h
gwn_immediate.c/h was fusioned with GPU_immediate.c/h
gwn_imm_util.c/h was fusioned with GPU_immediate_util.c/h
This commit is contained in:
2018-07-17 21:11:23 +02:00
parent 6329629bb9
commit 247ad2034d
51 changed files with 1886 additions and 2137 deletions

View File

@@ -34,7 +34,6 @@ add_subdirectory(opencolorio)
add_subdirectory(mikktspace)
add_subdirectory(glew-mx)
add_subdirectory(eigen)
add_subdirectory(gawain)
if(WITH_AUDASPACE)
add_subdirectory(audaspace)

View File

@@ -1,50 +0,0 @@
# WITH_OPENGL limits the visibility of the opengl headers to just gawain and bg_gpu,
# to more easily highlight codepadths in other libraries that need to be refactored,
# bf_intern_gawain is allowed to have opengl regardless of this option.
if(NOT WITH_OPENGL)
add_definitions(-DWITH_OPENGL)
endif()
set(INC
gawain
)
set(INC_SYS
${GLEW_INCLUDE_PATH}
)
set(SRC
src/gwn_attr_binding.c
src/gwn_batch.c
src/gwn_element.c
src/gwn_buffer_id.cpp
src/gwn_immediate.c
src/gwn_imm_util.c
src/gwn_primitive.c
src/gwn_shader_interface.c
src/gwn_vertex_array_id.cpp
src/gwn_vertex_buffer.c
src/gwn_vertex_format.c
gawain/gwn_attr_binding.h
gawain/gwn_attr_binding_private.h
gawain/gwn_batch.h
gawain/gwn_batch_private.h
gawain/gwn_buffer_id.h
gawain/gwn_common.h
gawain/gwn_element.h
gawain/gwn_imm_util.h
gawain/gwn_immediate.h
gawain/gwn_primitive.h
gawain/gwn_primitive_private.h
gawain/gwn_shader_interface.h
gawain/gwn_vertex_array_id.h
gawain/gwn_vertex_buffer.h
gawain/gwn_vertex_format.h
gawain/gwn_vertex_format_private.h
)
add_definitions(${GL_DEFINITIONS})
blender_add_lib(bf_intern_gawain "${SRC}" "${INC}" "${INC_SYS}")

View File

@@ -1,193 +0,0 @@
/*
* ***** 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
*
* ***** END GPL LICENSE BLOCK *****
*/
/** \file blender/gpu/gwn_batch.h
* \ingroup gpu
*
* Gawain geometry batch
* Contains VAOs + VBOs + Shader representing a drawable entity.
*/
#ifndef __GWN_BATCH_H__
#define __GWN_BATCH_H__
#include "gwn_vertex_buffer.h"
#include "gwn_element.h"
#include "gwn_shader_interface.h"
typedef enum {
GWN_BATCH_READY_TO_FORMAT,
GWN_BATCH_READY_TO_BUILD,
GWN_BATCH_BUILDING,
GWN_BATCH_READY_TO_DRAW
} Gwn_BatchPhase;
#define GWN_BATCH_VBO_MAX_LEN 3
#define GWN_BATCH_VAO_STATIC_LEN 3
#define GWN_BATCH_VAO_DYN_ALLOC_COUNT 16
typedef struct Gwn_Batch {
/* geometry */
Gwn_VertBuf* verts[GWN_BATCH_VBO_MAX_LEN]; /* verts[0] is required, others can be NULL */
Gwn_VertBuf* inst; /* instance attribs */
Gwn_IndexBuf* elem; /* NULL if element list not needed */
uint32_t gl_prim_type;
/* cached values (avoid dereferencing later) */
uint32_t vao_id;
uint32_t program;
const struct Gwn_ShaderInterface* interface;
/* book-keeping */
uint owns_flag;
struct Gwn_Context *context; /* used to free all vaos. this implies all vaos were created under the same context. */
Gwn_BatchPhase phase;
bool program_in_use;
/* Vao management: remembers all geometry state (vertex attrib bindings & element buffer)
* for each shader interface. Start with a static number of vaos and fallback to dynamic count
* if necessary. Once a batch goes dynamic it does not go back. */
bool is_dynamic_vao_count;
union {
/* Static handle count */
struct {
const struct Gwn_ShaderInterface* interfaces[GWN_BATCH_VAO_STATIC_LEN];
uint32_t vao_ids[GWN_BATCH_VAO_STATIC_LEN];
} static_vaos;
/* Dynamic handle count */
struct {
uint count;
const struct Gwn_ShaderInterface** interfaces;
uint32_t* vao_ids;
} dynamic_vaos;
};
/* XXX This is the only solution if we want to have some data structure using
* batches as key to identify nodes. We must destroy these nodes with this callback. */
void (*free_callback)(struct Gwn_Batch*, void*);
void* callback_data;
} Gwn_Batch;
enum {
GWN_BATCH_OWNS_VBO = (1 << 0),
/* each vbo index gets bit-shifted */
GWN_BATCH_OWNS_INSTANCES = (1 << 30),
GWN_BATCH_OWNS_INDEX = (1 << 31),
};
Gwn_Batch* GWN_batch_create_ex(Gwn_PrimType, Gwn_VertBuf*, Gwn_IndexBuf*, uint owns_flag);
void GWN_batch_init_ex(Gwn_Batch*, Gwn_PrimType, Gwn_VertBuf*, Gwn_IndexBuf*, uint owns_flag);
Gwn_Batch* GWN_batch_duplicate(Gwn_Batch* batch_src);
#define GWN_batch_create(prim, verts, elem) \
GWN_batch_create_ex(prim, verts, elem, 0)
#define GWN_batch_init(batch, prim, verts, elem) \
GWN_batch_init_ex(batch, prim, verts, elem, 0)
void GWN_batch_discard(Gwn_Batch*); /* verts & elem are not discarded */
void GWN_batch_callback_free_set(Gwn_Batch*, void (*callback)(Gwn_Batch*, void*), void*);
void GWN_batch_instbuf_set(Gwn_Batch*, Gwn_VertBuf*, bool own_vbo); /* Instancing */
int GWN_batch_vertbuf_add_ex(Gwn_Batch*, Gwn_VertBuf*, bool own_vbo);
#define GWN_batch_vertbuf_add(batch, verts) \
GWN_batch_vertbuf_add_ex(batch, verts, false)
void GWN_batch_program_set_no_use(Gwn_Batch*, uint32_t program, const Gwn_ShaderInterface*);
void GWN_batch_program_set(Gwn_Batch*, uint32_t program, const Gwn_ShaderInterface*);
/* Entire batch draws with one shader program, but can be redrawn later with another program. */
/* Vertex shader's inputs must be compatible with the batch's vertex format. */
void GWN_batch_program_use_begin(Gwn_Batch*); /* call before Batch_Uniform (temp hack?) */
void GWN_batch_program_use_end(Gwn_Batch*);
void GWN_batch_uniform_1ui(Gwn_Batch*, const char* name, int value);
void GWN_batch_uniform_1i(Gwn_Batch*, const char* name, int value);
void GWN_batch_uniform_1b(Gwn_Batch*, const char* name, bool value);
void GWN_batch_uniform_1f(Gwn_Batch*, const char* name, float value);
void GWN_batch_uniform_2f(Gwn_Batch*, const char* name, float x, float y);
void GWN_batch_uniform_3f(Gwn_Batch*, const char* name, float x, float y, float z);
void GWN_batch_uniform_4f(Gwn_Batch*, const char* name, float x, float y, float z, float w);
void GWN_batch_uniform_2fv(Gwn_Batch*, const char* name, const float data[2]);
void GWN_batch_uniform_3fv(Gwn_Batch*, const char* name, const float data[3]);
void GWN_batch_uniform_4fv(Gwn_Batch*, const char* name, const float data[4]);
void GWN_batch_uniform_2fv_array(Gwn_Batch*, const char* name, int len, const float *data);
void GWN_batch_uniform_4fv_array(Gwn_Batch*, const char* name, int len, const float *data);
void GWN_batch_uniform_mat4(Gwn_Batch*, const char* name, const float data[4][4]);
void GWN_batch_draw(Gwn_Batch*);
/* This does not bind/unbind shader and does not call GPU_matrix_bind() */
void GWN_batch_draw_range_ex(Gwn_Batch*, int v_first, int v_count, bool force_instance);
/* Does not even need batch */
void GWN_draw_primitive(Gwn_PrimType, int v_count);
#if 0 /* future plans */
/* Can multiple batches share a Gwn_VertBuf? Use ref count? */
/* We often need a batch with its own data, to be created and discarded together. */
/* WithOwn variants reduce number of system allocations. */
typedef struct BatchWithOwnVertexBuffer {
Gwn_Batch batch;
Gwn_VertBuf verts; /* link batch.verts to this */
} BatchWithOwnVertexBuffer;
typedef struct BatchWithOwnElementList {
Gwn_Batch batch;
Gwn_IndexBuf elem; /* link batch.elem to this */
} BatchWithOwnElementList;
typedef struct BatchWithOwnVertexBufferAndElementList {
Gwn_Batch batch;
Gwn_IndexBuf elem; /* link batch.elem to this */
Gwn_VertBuf verts; /* link batch.verts to this */
} BatchWithOwnVertexBufferAndElementList;
Gwn_Batch* create_BatchWithOwnVertexBuffer(Gwn_PrimType, Gwn_VertFormat*, uint v_len, Gwn_IndexBuf*);
Gwn_Batch* create_BatchWithOwnElementList(Gwn_PrimType, Gwn_VertBuf*, uint prim_len);
Gwn_Batch* create_BatchWithOwnVertexBufferAndElementList(Gwn_PrimType, Gwn_VertFormat*, uint v_len, uint prim_len);
/* verts: shared, own */
/* elem: none, shared, own */
Gwn_Batch* create_BatchInGeneral(Gwn_PrimType, VertexBufferStuff, ElementListStuff);
#endif /* future plans */
/* Macros */
#define GWN_BATCH_DISCARD_SAFE(batch) do { \
if (batch != NULL) { \
GWN_batch_discard(batch); \
batch = NULL; \
} \
} while (0)
#endif /* __GWN_BATCH_H__ */

View File

@@ -1,46 +0,0 @@
/*
* ***** 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
*
* ***** END GPL LICENSE BLOCK *****
*/
/** \file blender/gpu/gwn_imm_util.h
* \ingroup gpu
*
* Gawain element list (AKA index buffer)
*/
#ifndef __GWN_IMM_UTIL_H__
#define __GWN_IMM_UTIL_H__
#include <stdlib.h>
/* Draw 2D rectangles (replaces glRect functions) */
/* caller is reponsible for vertex format & shader */
void immRectf(uint pos, float x1, float y1, float x2, float y2);
void immRecti(uint pos, int x1, int y1, int x2, int y2);
/* Same as immRectf/immRecti but does not call immBegin/immEnd. To use with GWN_PRIM_TRIS. */
void immRectf_fast_with_color(uint pos, uint col, float x1, float y1, float x2, float y2, const float color[4]);
void immRecti_fast_with_color(uint pos, uint col, int x1, int y1, int x2, int y2, const float color[4]);
#endif /* __GWN_IMM_UTIL_H__ */

View File

@@ -1,132 +0,0 @@
/*
* ***** 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
*
* ***** END GPL LICENSE BLOCK *****
*/
/** \file blender/gpu/gwn_immediate.h
* \ingroup gpu
*
* Gawain immediate mode work-alike
*/
#ifndef __GWN_IMMEDIATE_H__
#define __GWN_IMMEDIATE_H__
#include "gwn_vertex_format.h"
#include "gwn_primitive.h"
#include "gwn_shader_interface.h"
#include "gwn_batch.h"
Gwn_VertFormat* immVertexFormat(void); /* returns a cleared vertex format, ready for add_attrib. */
void immBindProgram(uint32_t program, const Gwn_ShaderInterface*); /* every immBegin must have a program bound first. */
void immUnbindProgram(void); /* call after your last immEnd, or before binding another program. */
void immBegin(Gwn_PrimType, uint vertex_len); /* must supply exactly vertex_len vertices. */
void immBeginAtMost(Gwn_PrimType, uint max_vertex_len); /* can supply fewer vertices. */
void immEnd(void); /* finishes and draws. */
/* ImmBegin a batch, then use standard immFunctions as usual. */
/* ImmEnd will finalize the batch instead of drawing. */
/* Then you can draw it as many times as you like! Partially replaces the need for display lists. */
Gwn_Batch* immBeginBatch(Gwn_PrimType, uint vertex_len);
Gwn_Batch* immBeginBatchAtMost(Gwn_PrimType, uint vertex_len);
/* Provide attribute values that can change per vertex. */
/* First vertex after immBegin must have all its attributes specified. */
/* Skipped attributes will continue using the previous value for that attrib_id. */
void immAttrib1f(uint attrib_id, float x);
void immAttrib2f(uint attrib_id, float x, float y);
void immAttrib3f(uint attrib_id, float x, float y, float z);
void immAttrib4f(uint attrib_id, float x, float y, float z, float w);
void immAttrib2i(uint attrib_id, int x, int y);
void immAttrib1u(uint attrib_id, uint x);
void immAttrib2s(uint attrib_id, short x, short y);
void immAttrib2fv(uint attrib_id, const float data[2]);
void immAttrib3fv(uint attrib_id, const float data[3]);
void immAttrib4fv(uint attrib_id, const float data[4]);
void immAttrib3ub(uint attrib_id, unsigned char r, unsigned char g, unsigned char b);
void immAttrib4ub(uint attrib_id, unsigned char r, unsigned char g, unsigned char b, unsigned char a);
void immAttrib3ubv(uint attrib_id, const unsigned char data[4]);
void immAttrib4ubv(uint attrib_id, const unsigned char data[4]);
/* Explicitly skip an attribute. */
/* This advanced option kills automatic value copying for this attrib_id. */
void immSkipAttrib(uint attrib_id);
/* Provide one last attribute value & end the current vertex. */
/* This is most often used for 2D or 3D position (similar to glVertex). */
void immVertex2f(uint attrib_id, float x, float y);
void immVertex3f(uint attrib_id, float x, float y, float z);
void immVertex4f(uint attrib_id, float x, float y, float z, float w);
void immVertex2i(uint attrib_id, int x, int y);
void immVertex2s(uint attrib_id, short x, short y);
void immVertex2fv(uint attrib_id, const float data[2]);
void immVertex3fv(uint attrib_id, const float data[3]);
void immVertex2iv(uint attrib_id, const int data[2]);
/* Provide uniform values that don't change for the entire draw call. */
void immUniform1i(const char* name, int x);
void immUniform4iv(const char* name, const int data[4]);
void immUniform1f(const char* name, float x);
void immUniform2f(const char* name, float x, float y);
void immUniform2fv(const char* name, const float data[2]);
void immUniform3f(const char* name, float x, float y, float z);
void immUniform3fv(const char* name, const float data[3]);
void immUniformArray3fv(const char* name, const float *data, int count);
void immUniform4f(const char* name, float x, float y, float z, float w);
void immUniform4fv(const char* name, const float data[4]);
void immUniformArray4fv(const char* bare_name, const float *data, int count);
void immUniformMatrix4fv(const char* name, const float data[4][4]);
/* Convenience functions for setting "uniform vec4 color". */
/* The rgb functions have implicit alpha = 1.0. */
void immUniformColor4f(float r, float g, float b, float a);
void immUniformColor4fv(const float rgba[4]);
void immUniformColor3f(float r, float g, float b);
void immUniformColor3fv(const float rgb[3]);
void immUniformColor3fvAlpha(const float rgb[3], float a);
void immUniformColor3ub(unsigned char r, unsigned char g, unsigned char b);
void immUniformColor4ub(unsigned char r, unsigned char g, unsigned char b, unsigned char a);
void immUniformColor3ubv(const unsigned char rgb[3]);
void immUniformColor3ubvAlpha(const unsigned char rgb[3], unsigned char a);
void immUniformColor4ubv(const unsigned char rgba[4]);
/* These are called by the system -- not part of drawing API. */
void immInit(void);
void immActivate(void);
void immDeactivate(void);
void immDestroy(void);
#endif /* __GWN_IMMEDIATE_H__ */

View File

@@ -1,645 +0,0 @@
/*
* ***** 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
*
* ***** END GPL LICENSE BLOCK *****
*/
/** \file blender/gpu/intern/gwn_batch.c
* \ingroup gpu
*
* Gawain geometry batch
* Contains VAOs + VBOs + Shader representing a drawable entity.
*/
#include "gwn_batch.h"
#include "gwn_batch_private.h"
#include "gwn_buffer_id.h"
#include "gwn_vertex_array_id.h"
#include "gwn_primitive_private.h"
#include <stdlib.h>
#include <string.h>
/* necessary functions from matrix API */
extern void GPU_matrix_bind(const Gwn_ShaderInterface* shaderface);
static void batch_update_program_bindings(Gwn_Batch* batch, uint v_first);
void gwn_batch_vao_cache_clear(Gwn_Batch* batch)
{
if (batch->context == NULL) {
return;
}
if (batch->is_dynamic_vao_count) {
for (int i = 0; i < batch->dynamic_vaos.count; ++i) {
if (batch->dynamic_vaos.vao_ids[i]) {
GWN_vao_free(batch->dynamic_vaos.vao_ids[i], batch->context);
}
if (batch->dynamic_vaos.interfaces[i]) {
GWN_shaderinterface_remove_batch_ref((Gwn_ShaderInterface *)batch->dynamic_vaos.interfaces[i], batch);
}
}
free(batch->dynamic_vaos.interfaces);
free(batch->dynamic_vaos.vao_ids);
}
else {
for (int i = 0; i < GWN_BATCH_VAO_STATIC_LEN; ++i) {
if (batch->static_vaos.vao_ids[i]) {
GWN_vao_free(batch->static_vaos.vao_ids[i], batch->context);
}
if (batch->static_vaos.interfaces[i]) {
GWN_shaderinterface_remove_batch_ref((Gwn_ShaderInterface *)batch->static_vaos.interfaces[i], batch);
}
}
}
batch->is_dynamic_vao_count = false;
for (int i = 0; i < GWN_BATCH_VAO_STATIC_LEN; ++i) {
batch->static_vaos.vao_ids[i] = 0;
batch->static_vaos.interfaces[i] = NULL;
}
gwn_context_remove_batch(batch->context, batch);
batch->context = NULL;
}
Gwn_Batch* GWN_batch_create_ex(
Gwn_PrimType prim_type, Gwn_VertBuf* verts, Gwn_IndexBuf* elem,
uint owns_flag)
{
Gwn_Batch* batch = calloc(1, sizeof(Gwn_Batch));
GWN_batch_init_ex(batch, prim_type, verts, elem, owns_flag);
return batch;
}
void GWN_batch_init_ex(
Gwn_Batch* batch, Gwn_PrimType prim_type, Gwn_VertBuf* verts, Gwn_IndexBuf* elem,
uint owns_flag)
{
#if TRUST_NO_ONE
assert(verts != NULL);
#endif
batch->verts[0] = verts;
for (int v = 1; v < GWN_BATCH_VBO_MAX_LEN; ++v) {
batch->verts[v] = NULL;
}
batch->inst = NULL;
batch->elem = elem;
batch->gl_prim_type = convert_prim_type_to_gl(prim_type);
batch->phase = GWN_BATCH_READY_TO_DRAW;
batch->is_dynamic_vao_count = false;
batch->owns_flag = owns_flag;
batch->free_callback = NULL;
}
/* This will share the VBOs with the new batch. */
Gwn_Batch* GWN_batch_duplicate(Gwn_Batch* batch_src)
{
Gwn_Batch* batch = GWN_batch_create_ex(GWN_PRIM_POINTS, batch_src->verts[0], batch_src->elem, 0);
batch->gl_prim_type = batch_src->gl_prim_type;
for (int v = 1; v < GWN_BATCH_VBO_MAX_LEN; ++v) {
batch->verts[v] = batch_src->verts[v];
}
return batch;
}
void GWN_batch_discard(Gwn_Batch* batch)
{
if (batch->owns_flag & GWN_BATCH_OWNS_INDEX) {
GWN_indexbuf_discard(batch->elem);
}
if (batch->owns_flag & GWN_BATCH_OWNS_INSTANCES) {
GWN_vertbuf_discard(batch->inst);
}
if ((batch->owns_flag & ~GWN_BATCH_OWNS_INDEX) != 0) {
for (int v = 0; v < GWN_BATCH_VBO_MAX_LEN; ++v) {
if (batch->verts[v] == NULL) {
break;
}
if (batch->owns_flag & (1 << v)) {
GWN_vertbuf_discard(batch->verts[v]);
}
}
}
gwn_batch_vao_cache_clear(batch);
if (batch->free_callback) {
batch->free_callback(batch, batch->callback_data);
}
free(batch);
}
void GWN_batch_callback_free_set(Gwn_Batch* batch, void (*callback)(Gwn_Batch*, void*), void* user_data)
{
batch->free_callback = callback;
batch->callback_data = user_data;
}
void GWN_batch_instbuf_set(Gwn_Batch* batch, Gwn_VertBuf* inst, bool own_vbo)
{
#if TRUST_NO_ONE
assert(inst != NULL);
#endif
/* redo the bindings */
gwn_batch_vao_cache_clear(batch);
if (batch->inst != NULL && (batch->owns_flag & GWN_BATCH_OWNS_INSTANCES)) {
GWN_vertbuf_discard(batch->inst);
}
batch->inst = inst;
if (own_vbo) {
batch->owns_flag |= GWN_BATCH_OWNS_INSTANCES;
}
else {
batch->owns_flag &= ~GWN_BATCH_OWNS_INSTANCES;
}
}
/* Returns the index of verts in the batch. */
int GWN_batch_vertbuf_add_ex(
Gwn_Batch* batch, Gwn_VertBuf* verts,
bool own_vbo)
{
/* redo the bindings */
gwn_batch_vao_cache_clear(batch);
for (uint v = 0; v < GWN_BATCH_VBO_MAX_LEN; ++v) {
if (batch->verts[v] == NULL) {
#if TRUST_NO_ONE
/* for now all VertexBuffers must have same vertex_len */
assert(verts->vertex_len == batch->verts[0]->vertex_len);
#endif
batch->verts[v] = verts;
/* TODO: mark dirty so we can keep attrib bindings up-to-date */
if (own_vbo)
batch->owns_flag |= (1 << v);
return v;
}
}
/* we only make it this far if there is no room for another Gwn_VertBuf */
#if TRUST_NO_ONE
assert(false);
#endif
return -1;
}
static GLuint batch_vao_get(Gwn_Batch *batch)
{
/* Search through cache */
if (batch->is_dynamic_vao_count) {
for (int i = 0; i < batch->dynamic_vaos.count; ++i)
if (batch->dynamic_vaos.interfaces[i] == batch->interface)
return batch->dynamic_vaos.vao_ids[i];
}
else {
for (int i = 0; i < GWN_BATCH_VAO_STATIC_LEN; ++i)
if (batch->static_vaos.interfaces[i] == batch->interface)
return batch->static_vaos.vao_ids[i];
}
/* Set context of this batch.
* It will be bound to it until gwn_batch_vao_cache_clear is called.
* Until then it can only be drawn with this context. */
if (batch->context == NULL) {
batch->context = GWN_context_active_get();
gwn_context_add_batch(batch->context, batch);
}
#if TRUST_NO_ONE
else {
/* Make sure you are not trying to draw this batch in another context. */
assert(batch->context == GWN_context_active_get());
}
#endif
/* Cache miss, time to add a new entry! */
GLuint new_vao = 0;
if (!batch->is_dynamic_vao_count) {
int i; /* find first unused slot */
for (i = 0; i < GWN_BATCH_VAO_STATIC_LEN; ++i)
if (batch->static_vaos.vao_ids[i] == 0)
break;
if (i < GWN_BATCH_VAO_STATIC_LEN) {
batch->static_vaos.interfaces[i] = batch->interface;
batch->static_vaos.vao_ids[i] = new_vao = GWN_vao_alloc();
}
else {
/* Not enough place switch to dynamic. */
batch->is_dynamic_vao_count = true;
/* Erase previous entries, they will be added back if drawn again. */
for (int j = 0; j < GWN_BATCH_VAO_STATIC_LEN; ++j) {
GWN_shaderinterface_remove_batch_ref((Gwn_ShaderInterface*)batch->static_vaos.interfaces[j], batch);
GWN_vao_free(batch->static_vaos.vao_ids[j], batch->context);
}
/* Init dynamic arrays and let the branch below set the values. */
batch->dynamic_vaos.count = GWN_BATCH_VAO_DYN_ALLOC_COUNT;
batch->dynamic_vaos.interfaces = calloc(batch->dynamic_vaos.count, sizeof(Gwn_ShaderInterface*));
batch->dynamic_vaos.vao_ids = calloc(batch->dynamic_vaos.count, sizeof(GLuint));
}
}
if (batch->is_dynamic_vao_count) {
int i; /* find first unused slot */
for (i = 0; i < batch->dynamic_vaos.count; ++i)
if (batch->dynamic_vaos.vao_ids[i] == 0)
break;
if (i == batch->dynamic_vaos.count) {
/* Not enough place, realloc the array. */
i = batch->dynamic_vaos.count;
batch->dynamic_vaos.count += GWN_BATCH_VAO_DYN_ALLOC_COUNT;
batch->dynamic_vaos.interfaces = realloc(batch->dynamic_vaos.interfaces, sizeof(Gwn_ShaderInterface*) * batch->dynamic_vaos.count);
batch->dynamic_vaos.vao_ids = realloc(batch->dynamic_vaos.vao_ids, sizeof(GLuint) * batch->dynamic_vaos.count);
memset(batch->dynamic_vaos.interfaces + i, 0, sizeof(Gwn_ShaderInterface*) * GWN_BATCH_VAO_DYN_ALLOC_COUNT);
memset(batch->dynamic_vaos.vao_ids + i, 0, sizeof(GLuint) * GWN_BATCH_VAO_DYN_ALLOC_COUNT);
}
batch->dynamic_vaos.interfaces[i] = batch->interface;
batch->dynamic_vaos.vao_ids[i] = new_vao = GWN_vao_alloc();
}
GWN_shaderinterface_add_batch_ref((Gwn_ShaderInterface*)batch->interface, batch);
#if TRUST_NO_ONE
assert(new_vao != 0);
#endif
/* We just got a fresh VAO we need to initialize it. */
glBindVertexArray(new_vao);
batch_update_program_bindings(batch, 0);
glBindVertexArray(0);
return new_vao;
}
void GWN_batch_program_set_no_use(Gwn_Batch* batch, uint32_t program, const Gwn_ShaderInterface* shaderface)
{
#if TRUST_NO_ONE
assert(glIsProgram(shaderface->program));
assert(batch->program_in_use == 0);
#endif
batch->interface = shaderface;
batch->program = program;
batch->vao_id = batch_vao_get(batch);
}
void GWN_batch_program_set(Gwn_Batch* batch, uint32_t program, const Gwn_ShaderInterface* shaderface)
{
GWN_batch_program_set_no_use(batch, program, shaderface);
GWN_batch_program_use_begin(batch); /* hack! to make Batch_Uniform* simpler */
}
void gwn_batch_remove_interface_ref(Gwn_Batch* batch, const Gwn_ShaderInterface* interface)
{
if (batch->is_dynamic_vao_count) {
for (int i = 0; i < batch->dynamic_vaos.count; ++i) {
if (batch->dynamic_vaos.interfaces[i] == interface) {
GWN_vao_free(batch->dynamic_vaos.vao_ids[i], batch->context);
batch->dynamic_vaos.vao_ids[i] = 0;
batch->dynamic_vaos.interfaces[i] = NULL;
break; /* cannot have duplicates */
}
}
}
else {
int i;
for (i = 0; i < GWN_BATCH_VAO_STATIC_LEN; ++i) {
if (batch->static_vaos.interfaces[i] == interface) {
GWN_vao_free(batch->static_vaos.vao_ids[i], batch->context);
batch->static_vaos.vao_ids[i] = 0;
batch->static_vaos.interfaces[i] = NULL;
break; /* cannot have duplicates */
}
}
}
}
static void create_bindings(
Gwn_VertBuf* verts, const Gwn_ShaderInterface* interface,
uint v_first, const bool use_instancing)
{
const Gwn_VertFormat* format = &verts->format;
const uint attr_len = format->attr_len;
const uint stride = format->stride;
GWN_vertbuf_use(verts);
for (uint a_idx = 0; a_idx < attr_len; ++a_idx) {
const Gwn_VertAttr* a = format->attribs + a_idx;
const GLvoid* pointer = (const GLubyte*)0 + a->offset + v_first * stride;
for (uint n_idx = 0; n_idx < a->name_len; ++n_idx) {
const Gwn_ShaderInput* input = GWN_shaderinterface_attr(interface, a->name[n_idx]);
if (input == NULL) continue;
if (a->comp_len == 16 || a->comp_len == 12 || a->comp_len == 8) {
#if TRUST_NO_ONE
assert(a->fetch_mode == GWN_FETCH_FLOAT);
assert(a->gl_comp_type == GL_FLOAT);
#endif
for (int i = 0; i < a->comp_len / 4; ++i) {
glEnableVertexAttribArray(input->location + i);
glVertexAttribDivisor(input->location + i, (use_instancing) ? 1 : 0);
glVertexAttribPointer(input->location + i, 4, a->gl_comp_type, GL_FALSE, stride,
(const GLubyte*)pointer + i * 16);
}
}
else
{
glEnableVertexAttribArray(input->location);
glVertexAttribDivisor(input->location, (use_instancing) ? 1 : 0);
switch (a->fetch_mode) {
case GWN_FETCH_FLOAT:
case GWN_FETCH_INT_TO_FLOAT:
glVertexAttribPointer(input->location, a->comp_len, a->gl_comp_type, GL_FALSE, stride, pointer);
break;
case GWN_FETCH_INT_TO_FLOAT_UNIT:
glVertexAttribPointer(input->location, a->comp_len, a->gl_comp_type, GL_TRUE, stride, pointer);
break;
case GWN_FETCH_INT:
glVertexAttribIPointer(input->location, a->comp_len, a->gl_comp_type, stride, pointer);
break;
}
}
}
}
}
static void batch_update_program_bindings(Gwn_Batch* batch, uint v_first)
{
for (int v = 0; v < GWN_BATCH_VBO_MAX_LEN && batch->verts[v] != NULL; ++v) {
create_bindings(batch->verts[v], batch->interface, (batch->inst) ? 0 : v_first, false);
}
if (batch->inst) {
create_bindings(batch->inst, batch->interface, v_first, true);
}
if (batch->elem) {
GWN_indexbuf_use(batch->elem);
}
}
void GWN_batch_program_use_begin(Gwn_Batch* batch)
{
/* NOTE: use_program & done_using_program are fragile, depend on staying in sync with
* the GL context's active program. use_program doesn't mark other programs as "not used". */
/* TODO: make not fragile (somehow) */
if (!batch->program_in_use) {
glUseProgram(batch->program);
batch->program_in_use = true;
}
}
void GWN_batch_program_use_end(Gwn_Batch* batch)
{
if (batch->program_in_use) {
#if PROGRAM_NO_OPTI
glUseProgram(0);
#endif
batch->program_in_use = false;
}
}
#if TRUST_NO_ONE
#define GET_UNIFORM const Gwn_ShaderInput* uniform = GWN_shaderinterface_uniform(batch->interface, name); assert(uniform);
#else
#define GET_UNIFORM const Gwn_ShaderInput* uniform = GWN_shaderinterface_uniform(batch->interface, name);
#endif
void GWN_batch_uniform_1ui(Gwn_Batch* batch, const char* name, int value)
{
GET_UNIFORM
glUniform1ui(uniform->location, value);
}
void GWN_batch_uniform_1i(Gwn_Batch* batch, const char* name, int value)
{
GET_UNIFORM
glUniform1i(uniform->location, value);
}
void GWN_batch_uniform_1b(Gwn_Batch* batch, const char* name, bool value)
{
GET_UNIFORM
glUniform1i(uniform->location, value ? GL_TRUE : GL_FALSE);
}
void GWN_batch_uniform_2f(Gwn_Batch* batch, const char* name, float x, float y)
{
GET_UNIFORM
glUniform2f(uniform->location, x, y);
}
void GWN_batch_uniform_3f(Gwn_Batch* batch, const char* name, float x, float y, float z)
{
GET_UNIFORM
glUniform3f(uniform->location, x, y, z);
}
void GWN_batch_uniform_4f(Gwn_Batch* batch, const char* name, float x, float y, float z, float w)
{
GET_UNIFORM
glUniform4f(uniform->location, x, y, z, w);
}
void GWN_batch_uniform_1f(Gwn_Batch* batch, const char* name, float x)
{
GET_UNIFORM
glUniform1f(uniform->location, x);
}
void GWN_batch_uniform_2fv(Gwn_Batch* batch, const char* name, const float data[2])
{
GET_UNIFORM
glUniform2fv(uniform->location, 1, data);
}
void GWN_batch_uniform_3fv(Gwn_Batch* batch, const char* name, const float data[3])
{
GET_UNIFORM
glUniform3fv(uniform->location, 1, data);
}
void GWN_batch_uniform_4fv(Gwn_Batch* batch, const char* name, const float data[4])
{
GET_UNIFORM
glUniform4fv(uniform->location, 1, data);
}
void GWN_batch_uniform_2fv_array(Gwn_Batch* batch, const char* name, const int len, const float *data)
{
GET_UNIFORM
glUniform2fv(uniform->location, len, data);
}
void GWN_batch_uniform_4fv_array(Gwn_Batch* batch, const char* name, const int len, const float *data)
{
GET_UNIFORM
glUniform4fv(uniform->location, len, data);
}
void GWN_batch_uniform_mat4(Gwn_Batch* batch, const char* name, const float data[4][4])
{
GET_UNIFORM
glUniformMatrix4fv(uniform->location, 1, GL_FALSE, (const float *)data);
}
static void primitive_restart_enable(const Gwn_IndexBuf *el)
{
// TODO(fclem) Replace by GL_PRIMITIVE_RESTART_FIXED_INDEX when we have ogl 4.3
glEnable(GL_PRIMITIVE_RESTART);
GLuint restart_index = (GLuint)0xFFFFFFFF;
#if GWN_TRACK_INDEX_RANGE
if (el->index_type == GWN_INDEX_U8)
restart_index = (GLuint)0xFF;
else if (el->index_type == GWN_INDEX_U16)
restart_index = (GLuint)0xFFFF;
#endif
glPrimitiveRestartIndex(restart_index);
}
static void primitive_restart_disable(void)
{
glDisable(GL_PRIMITIVE_RESTART);
}
void GWN_batch_draw(Gwn_Batch* batch)
{
#if TRUST_NO_ONE
assert(batch->phase == GWN_BATCH_READY_TO_DRAW);
assert(batch->verts[0]->vbo_id != 0);
#endif
GWN_batch_program_use_begin(batch);
GPU_matrix_bind(batch->interface); // external call.
GWN_batch_draw_range_ex(batch, 0, 0, false);
GWN_batch_program_use_end(batch);
}
void GWN_batch_draw_range_ex(Gwn_Batch* batch, int v_first, int v_count, bool force_instance)
{
#if TRUST_NO_ONE
assert(!(force_instance && (batch->inst == NULL)) || v_count > 0); // we cannot infer length if force_instance
#endif
const bool do_instance = (force_instance || batch->inst);
// If using offset drawing, use the default VAO and redo bindings.
if (v_first != 0 && (do_instance || batch->elem)) {
glBindVertexArray(GWN_vao_default());
batch_update_program_bindings(batch, v_first);
}
else {
glBindVertexArray(batch->vao_id);
}
if (do_instance) {
/* Infer length if vertex count is not given */
if (v_count == 0) {
v_count = batch->inst->vertex_len;
}
if (batch->elem) {
const Gwn_IndexBuf* el = batch->elem;
if (el->use_prim_restart) {
primitive_restart_enable(el);
}
#if GWN_TRACK_INDEX_RANGE
glDrawElementsInstancedBaseVertex(batch->gl_prim_type,
el->index_len,
el->gl_index_type,
0,
v_count,
el->base_index);
#else
glDrawElementsInstanced(batch->gl_prim_type, el->index_len, GL_UNSIGNED_INT, 0, v_count);
#endif
if (el->use_prim_restart) {
primitive_restart_disable();
}
}
else {
glDrawArraysInstanced(batch->gl_prim_type, 0, batch->verts[0]->vertex_len, v_count);
}
}
else {
/* Infer length if vertex count is not given */
if (v_count == 0) {
v_count = (batch->elem) ? batch->elem->index_len : batch->verts[0]->vertex_len;
}
if (batch->elem) {
const Gwn_IndexBuf* el = batch->elem;
if (el->use_prim_restart) {
primitive_restart_enable(el);
}
#if GWN_TRACK_INDEX_RANGE
if (el->base_index) {
glDrawRangeElementsBaseVertex(batch->gl_prim_type,
el->min_index,
el->max_index,
v_count,
el->gl_index_type,
0,
el->base_index);
}
else {
glDrawRangeElements(batch->gl_prim_type, el->min_index, el->max_index, v_count, el->gl_index_type, 0);
}
#else
glDrawElements(batch->gl_prim_type, v_count, GL_UNSIGNED_INT, 0);
#endif
if (el->use_prim_restart) {
primitive_restart_disable();
}
}
else {
glDrawArrays(batch->gl_prim_type, v_first, v_count);
}
}
/* Performance hog if you are drawing with the same vao multiple time.
* Only activate for debugging. */
// glBindVertexArray(0);
}
/* just draw some vertices and let shader place them where we want. */
void GWN_draw_primitive(Gwn_PrimType prim_type, int v_count)
{
/* we cannot draw without vao ... annoying ... */
glBindVertexArray(GWN_vao_default());
GLenum type = convert_prim_type_to_gl(prim_type);
glDrawArrays(type, 0, v_count);
/* Performance hog if you are drawing with the same vao multiple time.
* Only activate for debugging.*/
// glBindVertexArray(0);
}

View File

@@ -1,100 +0,0 @@
/*
* ***** 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
*
* ***** END GPL LICENSE BLOCK *****
*/
/** \file blender/gpu/intern/gwn_imm_util.c
* \ingroup gpu
*
* Gawain immediate mode drawing utilities
*/
#include "gwn_imm_util.h"
#include "gwn_immediate.h"
#include <stdlib.h>
void immRectf(uint pos, float x1, float y1, float x2, float y2)
{
immBegin(GWN_PRIM_TRI_FAN, 4);
immVertex2f(pos, x1, y1);
immVertex2f(pos, x2, y1);
immVertex2f(pos, x2, y2);
immVertex2f(pos, x1, y2);
immEnd();
}
void immRecti(uint pos, int x1, int y1, int x2, int y2)
{
immBegin(GWN_PRIM_TRI_FAN, 4);
immVertex2i(pos, x1, y1);
immVertex2i(pos, x2, y1);
immVertex2i(pos, x2, y2);
immVertex2i(pos, x1, y2);
immEnd();
}
void immRectf_fast_with_color(uint pos, uint col, float x1, float y1, float x2, float y2, const float color[4])
{
immAttrib4fv(col, color);
immVertex2f(pos, x1, y1);
immAttrib4fv(col, color);
immVertex2f(pos, x2, y1);
immAttrib4fv(col, color);
immVertex2f(pos, x2, y2);
immAttrib4fv(col, color);
immVertex2f(pos, x1, y1);
immAttrib4fv(col, color);
immVertex2f(pos, x2, y2);
immAttrib4fv(col, color);
immVertex2f(pos, x1, y2);
}
void immRecti_fast_with_color(uint pos, uint col, int x1, int y1, int x2, int y2, const float color[4])
{
immAttrib4fv(col, color);
immVertex2i(pos, x1, y1);
immAttrib4fv(col, color);
immVertex2i(pos, x2, y1);
immAttrib4fv(col, color);
immVertex2i(pos, x2, y2);
immAttrib4fv(col, color);
immVertex2i(pos, x1, y1);
immAttrib4fv(col, color);
immVertex2i(pos, x2, y2);
immAttrib4fv(col, color);
immVertex2i(pos, x1, y2);
}
#if 0 /* more complete version in case we want that */
void immRecti_complete(int x1, int y1, int x2, int y2, const float color[4])
{
Gwn_VertFormat *format = immVertexFormat();
uint pos = add_attrib(format, "pos", GWN_COMP_I32, 2, GWN_FETCH_INT_TO_FLOAT);
immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
immUniformColor4fv(color);
immRecti(pos, x1, y1, x2, y2);
immUnbindProgram();
}
#endif

View File

@@ -1,863 +0,0 @@
/*
* ***** 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
*
* ***** END GPL LICENSE BLOCK *****
*/
/** \file blender/gpu/intern/gwn_immediate.c
* \ingroup gpu
*
* Gawain immediate mode work-alike
*/
#include "gwn_immediate.h"
#include "gwn_buffer_id.h"
#include "gwn_attr_binding.h"
#include "gwn_attr_binding_private.h"
#include "gwn_vertex_format_private.h"
#include "gwn_vertex_array_id.h"
#include "gwn_primitive_private.h"
#include <string.h>
#include <stdlib.h>
/* necessary functions from matrix API */
extern void GPU_matrix_bind(const Gwn_ShaderInterface*);
extern bool GPU_matrix_dirty_get(void);
typedef struct {
/* TODO: organize this struct by frequency of change (run-time) */
Gwn_Batch* batch;
Gwn_Context* context;
/* current draw call */
GLubyte* buffer_data;
uint buffer_offset;
uint buffer_bytes_mapped;
uint vertex_len;
bool strict_vertex_len;
Gwn_PrimType prim_type;
Gwn_VertFormat vertex_format;
/* current vertex */
uint vertex_idx;
GLubyte* vertex_data;
uint16_t unassigned_attrib_bits; /* which attributes of current vertex have not been given values? */
GLuint vbo_id;
GLuint vao_id;
GLuint bound_program;
const Gwn_ShaderInterface* shader_interface;
Gwn_AttrBinding attrib_binding;
uint16_t prev_enabled_attrib_bits; /* <-- only affects this VAO, so we're ok */
} Immediate;
/* size of internal buffer -- make this adjustable? */
#define IMM_BUFFER_SIZE (4 * 1024 * 1024)
static bool initialized = false;
static Immediate imm;
void immInit(void)
{
#if TRUST_NO_ONE
assert(!initialized);
#endif
memset(&imm, 0, sizeof(Immediate));
imm.vbo_id = GWN_buf_id_alloc();
glBindBuffer(GL_ARRAY_BUFFER, imm.vbo_id);
glBufferData(GL_ARRAY_BUFFER, IMM_BUFFER_SIZE, NULL, GL_DYNAMIC_DRAW);
imm.prim_type = GWN_PRIM_NONE;
imm.strict_vertex_len = true;
glBindBuffer(GL_ARRAY_BUFFER, 0);
initialized = true;
}
void immActivate(void)
{
#if TRUST_NO_ONE
assert(initialized);
assert(imm.prim_type == GWN_PRIM_NONE); /* make sure we're not between a Begin/End pair */
assert(imm.vao_id == 0);
#endif
imm.vao_id = GWN_vao_alloc();
imm.context = GWN_context_active_get();
}
void immDeactivate(void)
{
#if TRUST_NO_ONE
assert(initialized);
assert(imm.prim_type == GWN_PRIM_NONE); /* make sure we're not between a Begin/End pair */
assert(imm.vao_id != 0);
#endif
GWN_vao_free(imm.vao_id, imm.context);
imm.vao_id = 0;
imm.prev_enabled_attrib_bits = 0;
}
void immDestroy(void)
{
GWN_buf_id_free(imm.vbo_id);
initialized = false;
}
Gwn_VertFormat* immVertexFormat(void)
{
GWN_vertformat_clear(&imm.vertex_format);
return &imm.vertex_format;
}
void immBindProgram(GLuint program, const Gwn_ShaderInterface* shaderface)
{
#if TRUST_NO_ONE
assert(imm.bound_program == 0);
assert(glIsProgram(program));
#endif
imm.bound_program = program;
imm.shader_interface = shaderface;
if (!imm.vertex_format.packed)
VertexFormat_pack(&imm.vertex_format);
glUseProgram(program);
get_attrib_locations(&imm.vertex_format, &imm.attrib_binding, shaderface);
GPU_matrix_bind(shaderface);
}
void immUnbindProgram(void)
{
#if TRUST_NO_ONE
assert(imm.bound_program != 0);
#endif
#if PROGRAM_NO_OPTI
glUseProgram(0);
#endif
imm.bound_program = 0;
}
#if TRUST_NO_ONE
static bool vertex_count_makes_sense_for_primitive(uint vertex_len, Gwn_PrimType prim_type)
{
/* does vertex_len make sense for this primitive type? */
if (vertex_len == 0) {
return false;
}
switch (prim_type) {
case GWN_PRIM_POINTS:
return true;
case GWN_PRIM_LINES:
return vertex_len % 2 == 0;
case GWN_PRIM_LINE_STRIP:
case GWN_PRIM_LINE_LOOP:
return vertex_len >= 2;
case GWN_PRIM_LINE_STRIP_ADJ:
return vertex_len >= 4;
case GWN_PRIM_TRIS:
return vertex_len % 3 == 0;
case GWN_PRIM_TRI_STRIP:
case GWN_PRIM_TRI_FAN:
return vertex_len >= 3;
default:
return false;
}
}
#endif
void immBegin(Gwn_PrimType prim_type, uint vertex_len)
{
#if TRUST_NO_ONE
assert(initialized);
assert(imm.prim_type == GWN_PRIM_NONE); /* make sure we haven't already begun */
assert(vertex_count_makes_sense_for_primitive(vertex_len, prim_type));
#endif
imm.prim_type = prim_type;
imm.vertex_len = vertex_len;
imm.vertex_idx = 0;
imm.unassigned_attrib_bits = imm.attrib_binding.enabled_bits;
/* how many bytes do we need for this draw call? */
const uint bytes_needed = vertex_buffer_size(&imm.vertex_format, vertex_len);
#if TRUST_NO_ONE
assert(bytes_needed <= IMM_BUFFER_SIZE);
#endif
glBindBuffer(GL_ARRAY_BUFFER, imm.vbo_id);
/* does the current buffer have enough room? */
const uint available_bytes = IMM_BUFFER_SIZE - imm.buffer_offset;
/* ensure vertex data is aligned */
const uint pre_padding = padding(imm.buffer_offset, imm.vertex_format.stride); /* might waste a little space, but it's safe */
if ((bytes_needed + pre_padding) <= available_bytes) {
imm.buffer_offset += pre_padding;
}
else {
/* orphan this buffer & start with a fresh one */
/* this method works on all platforms, old & new */
glBufferData(GL_ARRAY_BUFFER, IMM_BUFFER_SIZE, NULL, GL_DYNAMIC_DRAW);
imm.buffer_offset = 0;
}
/* printf("mapping %u to %u\n", imm.buffer_offset, imm.buffer_offset + bytes_needed - 1); */
imm.buffer_data = glMapBufferRange(GL_ARRAY_BUFFER, imm.buffer_offset, bytes_needed,
GL_MAP_WRITE_BIT | GL_MAP_UNSYNCHRONIZED_BIT | (imm.strict_vertex_len ? 0 : GL_MAP_FLUSH_EXPLICIT_BIT));
#if TRUST_NO_ONE
assert(imm.buffer_data != NULL);
#endif
imm.buffer_bytes_mapped = bytes_needed;
imm.vertex_data = imm.buffer_data;
}
void immBeginAtMost(Gwn_PrimType prim_type, uint vertex_len)
{
#if TRUST_NO_ONE
assert(vertex_len > 0);
#endif
imm.strict_vertex_len = false;
immBegin(prim_type, vertex_len);
}
Gwn_Batch* immBeginBatch(Gwn_PrimType prim_type, uint vertex_len)
{
#if TRUST_NO_ONE
assert(initialized);
assert(imm.prim_type == GWN_PRIM_NONE); /* make sure we haven't already begun */
assert(vertex_count_makes_sense_for_primitive(vertex_len, prim_type));
#endif
imm.prim_type = prim_type;
imm.vertex_len = vertex_len;
imm.vertex_idx = 0;
imm.unassigned_attrib_bits = imm.attrib_binding.enabled_bits;
Gwn_VertBuf* verts = GWN_vertbuf_create_with_format(&imm.vertex_format);
GWN_vertbuf_data_alloc(verts, vertex_len);
imm.buffer_bytes_mapped = GWN_vertbuf_size_get(verts);
imm.vertex_data = verts->data;
imm.batch = GWN_batch_create_ex(prim_type, verts, NULL, GWN_BATCH_OWNS_VBO);
imm.batch->phase = GWN_BATCH_BUILDING;
return imm.batch;
}
Gwn_Batch* immBeginBatchAtMost(Gwn_PrimType prim_type, uint vertex_len)
{
imm.strict_vertex_len = false;
return immBeginBatch(prim_type, vertex_len);
}
static void immDrawSetup(void)
{
/* set up VAO -- can be done during Begin or End really */
glBindVertexArray(imm.vao_id);
/* enable/disable vertex attribs as needed */
if (imm.attrib_binding.enabled_bits != imm.prev_enabled_attrib_bits) {
for (uint loc = 0; loc < GWN_VERT_ATTR_MAX_LEN; ++loc) {
bool is_enabled = imm.attrib_binding.enabled_bits & (1 << loc);
bool was_enabled = imm.prev_enabled_attrib_bits & (1 << loc);
if (is_enabled && !was_enabled) {
glEnableVertexAttribArray(loc);
}
else if (was_enabled && !is_enabled) {
glDisableVertexAttribArray(loc);
}
}
imm.prev_enabled_attrib_bits = imm.attrib_binding.enabled_bits;
}
const uint stride = imm.vertex_format.stride;
for (uint a_idx = 0; a_idx < imm.vertex_format.attr_len; ++a_idx) {
const Gwn_VertAttr* a = imm.vertex_format.attribs + a_idx;
const uint offset = imm.buffer_offset + a->offset;
const GLvoid* pointer = (const GLubyte*)0 + offset;
const uint loc = read_attrib_location(&imm.attrib_binding, a_idx);
switch (a->fetch_mode) {
case GWN_FETCH_FLOAT:
case GWN_FETCH_INT_TO_FLOAT:
glVertexAttribPointer(loc, a->comp_len, a->gl_comp_type, GL_FALSE, stride, pointer);
break;
case GWN_FETCH_INT_TO_FLOAT_UNIT:
glVertexAttribPointer(loc, a->comp_len, a->gl_comp_type, GL_TRUE, stride, pointer);
break;
case GWN_FETCH_INT:
glVertexAttribIPointer(loc, a->comp_len, a->gl_comp_type, stride, pointer);
}
}
if (GPU_matrix_dirty_get()) {
GPU_matrix_bind(imm.shader_interface);
}
}
void immEnd(void)
{
#if TRUST_NO_ONE
assert(imm.prim_type != GWN_PRIM_NONE); /* make sure we're between a Begin/End pair */
#endif
uint buffer_bytes_used;
if (imm.strict_vertex_len) {
#if TRUST_NO_ONE
assert(imm.vertex_idx == imm.vertex_len); /* with all vertices defined */
#endif
buffer_bytes_used = imm.buffer_bytes_mapped;
}
else {
#if TRUST_NO_ONE
assert(imm.vertex_idx <= imm.vertex_len);
#endif
if (imm.vertex_idx == imm.vertex_len) {
buffer_bytes_used = imm.buffer_bytes_mapped;
}
else {
#if TRUST_NO_ONE
assert(imm.vertex_idx == 0 || vertex_count_makes_sense_for_primitive(imm.vertex_idx, imm.prim_type));
#endif
imm.vertex_len = imm.vertex_idx;
buffer_bytes_used = vertex_buffer_size(&imm.vertex_format, imm.vertex_len);
/* unused buffer bytes are available to the next immBegin */
}
/* tell OpenGL what range was modified so it doesn't copy the whole mapped range */
glFlushMappedBufferRange(GL_ARRAY_BUFFER, 0, buffer_bytes_used);
}
if (imm.batch) {
if (buffer_bytes_used != imm.buffer_bytes_mapped) {
GWN_vertbuf_data_resize(imm.batch->verts[0], imm.vertex_len);
/* TODO: resize only if vertex count is much smaller */
}
GWN_batch_program_set(imm.batch, imm.bound_program, imm.shader_interface);
imm.batch->phase = GWN_BATCH_READY_TO_DRAW;
imm.batch = NULL; /* don't free, batch belongs to caller */
}
else {
glUnmapBuffer(GL_ARRAY_BUFFER);
if (imm.vertex_len > 0) {
immDrawSetup();
glDrawArrays(convert_prim_type_to_gl(imm.prim_type), 0, imm.vertex_len);
}
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
/* prep for next immBegin */
imm.buffer_offset += buffer_bytes_used;
}
/* prep for next immBegin */
imm.prim_type = GWN_PRIM_NONE;
imm.strict_vertex_len = true;
}
static void setAttribValueBit(uint attrib_id)
{
uint16_t mask = 1 << attrib_id;
#if TRUST_NO_ONE
assert(imm.unassigned_attrib_bits & mask); /* not already set */
#endif
imm.unassigned_attrib_bits &= ~mask;
}
/* --- generic attribute functions --- */
void immAttrib1f(uint attrib_id, float x)
{
Gwn_VertAttr* attrib = imm.vertex_format.attribs + attrib_id;
#if TRUST_NO_ONE
assert(attrib_id < imm.vertex_format.attr_len);
assert(attrib->comp_type == GWN_COMP_F32);
assert(attrib->comp_len == 1);
assert(imm.vertex_idx < imm.vertex_len);
assert(imm.prim_type != GWN_PRIM_NONE); /* make sure we're between a Begin/End pair */
#endif
setAttribValueBit(attrib_id);
float* data = (float*)(imm.vertex_data + attrib->offset);
/* printf("%s %td %p\n", __FUNCTION__, (GLubyte*)data - imm.buffer_data, data); */
data[0] = x;
}
void immAttrib2f(uint attrib_id, float x, float y)
{
Gwn_VertAttr* attrib = imm.vertex_format.attribs + attrib_id;
#if TRUST_NO_ONE
assert(attrib_id < imm.vertex_format.attr_len);
assert(attrib->comp_type == GWN_COMP_F32);
assert(attrib->comp_len == 2);
assert(imm.vertex_idx < imm.vertex_len);
assert(imm.prim_type != GWN_PRIM_NONE); /* make sure we're between a Begin/End pair */
#endif
setAttribValueBit(attrib_id);
float* data = (float*)(imm.vertex_data + attrib->offset);
/* printf("%s %td %p\n", __FUNCTION__, (GLubyte*)data - imm.buffer_data, data); */
data[0] = x;
data[1] = y;
}
void immAttrib3f(uint attrib_id, float x, float y, float z)
{
Gwn_VertAttr* attrib = imm.vertex_format.attribs + attrib_id;
#if TRUST_NO_ONE
assert(attrib_id < imm.vertex_format.attr_len);
assert(attrib->comp_type == GWN_COMP_F32);
assert(attrib->comp_len == 3);
assert(imm.vertex_idx < imm.vertex_len);
assert(imm.prim_type != GWN_PRIM_NONE); /* make sure we're between a Begin/End pair */
#endif
setAttribValueBit(attrib_id);
float* data = (float*)(imm.vertex_data + attrib->offset);
/* printf("%s %td %p\n", __FUNCTION__, (GLubyte*)data - imm.buffer_data, data); */
data[0] = x;
data[1] = y;
data[2] = z;
}
void immAttrib4f(uint attrib_id, float x, float y, float z, float w)
{
Gwn_VertAttr* attrib = imm.vertex_format.attribs + attrib_id;
#if TRUST_NO_ONE
assert(attrib_id < imm.vertex_format.attr_len);
assert(attrib->comp_type == GWN_COMP_F32);
assert(attrib->comp_len == 4);
assert(imm.vertex_idx < imm.vertex_len);
assert(imm.prim_type != GWN_PRIM_NONE); /* make sure we're between a Begin/End pair */
#endif
setAttribValueBit(attrib_id);
float* data = (float*)(imm.vertex_data + attrib->offset);
/* printf("%s %td %p\n", __FUNCTION__, (GLubyte*)data - imm.buffer_data, data); */
data[0] = x;
data[1] = y;
data[2] = z;
data[3] = w;
}
void immAttrib1u(uint attrib_id, uint x)
{
Gwn_VertAttr* attrib = imm.vertex_format.attribs + attrib_id;
#if TRUST_NO_ONE
assert(attrib_id < imm.vertex_format.attr_len);
assert(attrib->comp_type == GWN_COMP_U32);
assert(attrib->comp_len == 1);
assert(imm.vertex_idx < imm.vertex_len);
assert(imm.prim_type != GWN_PRIM_NONE); /* make sure we're between a Begin/End pair */
#endif
setAttribValueBit(attrib_id);
uint* data = (uint*)(imm.vertex_data + attrib->offset);
data[0] = x;
}
void immAttrib2i(uint attrib_id, int x, int y)
{
Gwn_VertAttr* attrib = imm.vertex_format.attribs + attrib_id;
#if TRUST_NO_ONE
assert(attrib_id < imm.vertex_format.attr_len);
assert(attrib->comp_type == GWN_COMP_I32);
assert(attrib->comp_len == 2);
assert(imm.vertex_idx < imm.vertex_len);
assert(imm.prim_type != GWN_PRIM_NONE); /* make sure we're between a Begin/End pair */
#endif
setAttribValueBit(attrib_id);
int* data = (int*)(imm.vertex_data + attrib->offset);
data[0] = x;
data[1] = y;
}
void immAttrib2s(uint attrib_id, short x, short y)
{
Gwn_VertAttr* attrib = imm.vertex_format.attribs + attrib_id;
#if TRUST_NO_ONE
assert(attrib_id < imm.vertex_format.attr_len);
assert(attrib->comp_type == GWN_COMP_I16);
assert(attrib->comp_len == 2);
assert(imm.vertex_idx < imm.vertex_len);
assert(imm.prim_type != GWN_PRIM_NONE); /* make sure we're between a Begin/End pair */
#endif
setAttribValueBit(attrib_id);
short* data = (short*)(imm.vertex_data + attrib->offset);
data[0] = x;
data[1] = y;
}
void immAttrib2fv(uint attrib_id, const float data[2])
{
immAttrib2f(attrib_id, data[0], data[1]);
}
void immAttrib3fv(uint attrib_id, const float data[3])
{
immAttrib3f(attrib_id, data[0], data[1], data[2]);
}
void immAttrib4fv(uint attrib_id, const float data[4])
{
immAttrib4f(attrib_id, data[0], data[1], data[2], data[3]);
}
void immAttrib3ub(uint attrib_id, unsigned char r, unsigned char g, unsigned char b)
{
Gwn_VertAttr* attrib = imm.vertex_format.attribs + attrib_id;
#if TRUST_NO_ONE
assert(attrib_id < imm.vertex_format.attr_len);
assert(attrib->comp_type == GWN_COMP_U8);
assert(attrib->comp_len == 3);
assert(imm.vertex_idx < imm.vertex_len);
assert(imm.prim_type != GWN_PRIM_NONE); /* make sure we're between a Begin/End pair */
#endif
setAttribValueBit(attrib_id);
GLubyte* data = imm.vertex_data + attrib->offset;
/* printf("%s %td %p\n", __FUNCTION__, data - imm.buffer_data, data); */
data[0] = r;
data[1] = g;
data[2] = b;
}
void immAttrib4ub(uint attrib_id, unsigned char r, unsigned char g, unsigned char b, unsigned char a)
{
Gwn_VertAttr* attrib = imm.vertex_format.attribs + attrib_id;
#if TRUST_NO_ONE
assert(attrib_id < imm.vertex_format.attr_len);
assert(attrib->comp_type == GWN_COMP_U8);
assert(attrib->comp_len == 4);
assert(imm.vertex_idx < imm.vertex_len);
assert(imm.prim_type != GWN_PRIM_NONE); /* make sure we're between a Begin/End pair */
#endif
setAttribValueBit(attrib_id);
GLubyte* data = imm.vertex_data + attrib->offset;
/* printf("%s %td %p\n", __FUNCTION__, data - imm.buffer_data, data); */
data[0] = r;
data[1] = g;
data[2] = b;
data[3] = a;
}
void immAttrib3ubv(uint attrib_id, const unsigned char data[3])
{
immAttrib3ub(attrib_id, data[0], data[1], data[2]);
}
void immAttrib4ubv(uint attrib_id, const unsigned char data[4])
{
immAttrib4ub(attrib_id, data[0], data[1], data[2], data[3]);
}
void immSkipAttrib(uint attrib_id)
{
#if TRUST_NO_ONE
assert(attrib_id < imm.vertex_format.attr_len);
assert(imm.vertex_idx < imm.vertex_len);
assert(imm.prim_type != GWN_PRIM_NONE); /* make sure we're between a Begin/End pair */
#endif
setAttribValueBit(attrib_id);
}
static void immEndVertex(void) /* and move on to the next vertex */
{
#if TRUST_NO_ONE
assert(imm.prim_type != GWN_PRIM_NONE); /* make sure we're between a Begin/End pair */
assert(imm.vertex_idx < imm.vertex_len);
#endif
/* have all attribs been assigned values?
* if not, copy value from previous vertex */
if (imm.unassigned_attrib_bits) {
#if TRUST_NO_ONE
assert(imm.vertex_idx > 0); /* first vertex must have all attribs specified */
#endif
for (uint a_idx = 0; a_idx < imm.vertex_format.attr_len; ++a_idx) {
if ((imm.unassigned_attrib_bits >> a_idx) & 1) {
const Gwn_VertAttr* a = imm.vertex_format.attribs + a_idx;
/* printf("copying %s from vertex %u to %u\n", a->name, imm.vertex_idx - 1, imm.vertex_idx); */
GLubyte* data = imm.vertex_data + a->offset;
memcpy(data, data - imm.vertex_format.stride, a->sz);
/* TODO: consolidate copy of adjacent attributes */
}
}
}
imm.vertex_idx++;
imm.vertex_data += imm.vertex_format.stride;
imm.unassigned_attrib_bits = imm.attrib_binding.enabled_bits;
}
void immVertex2f(uint attrib_id, float x, float y)
{
immAttrib2f(attrib_id, x, y);
immEndVertex();
}
void immVertex3f(uint attrib_id, float x, float y, float z)
{
immAttrib3f(attrib_id, x, y, z);
immEndVertex();
}
void immVertex4f(uint attrib_id, float x, float y, float z, float w)
{
immAttrib4f(attrib_id, x, y, z, w);
immEndVertex();
}
void immVertex2i(uint attrib_id, int x, int y)
{
immAttrib2i(attrib_id, x, y);
immEndVertex();
}
void immVertex2s(uint attrib_id, short x, short y)
{
immAttrib2s(attrib_id, x, y);
immEndVertex();
}
void immVertex2fv(uint attrib_id, const float data[2])
{
immAttrib2f(attrib_id, data[0], data[1]);
immEndVertex();
}
void immVertex3fv(uint attrib_id, const float data[3])
{
immAttrib3f(attrib_id, data[0], data[1], data[2]);
immEndVertex();
}
void immVertex2iv(uint attrib_id, const int data[2])
{
immAttrib2i(attrib_id, data[0], data[1]);
immEndVertex();
}
/* --- generic uniform functions --- */
#if 0
#if TRUST_NO_ONE
#define GET_UNIFORM const Gwn_ShaderInput* uniform = GWN_shaderinterface_uniform(imm.shader_interface, name); assert(uniform);
#else
#define GET_UNIFORM const Gwn_ShaderInput* uniform = GWN_shaderinterface_uniform(imm.shader_interface, name);
#endif
#else
/* NOTE: It is possible to have uniform fully optimized out from the shader.
* In this case we can't assert failure or allow NULL-pointer dereference.
* TODO(sergey): How can we detect existing-but-optimized-out uniform but still
* catch typos in uniform names passed to immUniform*() functions? */
#define GET_UNIFORM const Gwn_ShaderInput* uniform = GWN_shaderinterface_uniform(imm.shader_interface, name); if (uniform == NULL) return;
#endif
void immUniform1f(const char* name, float x)
{
GET_UNIFORM
glUniform1f(uniform->location, x);
}
void immUniform2f(const char* name, float x, float y)
{
GET_UNIFORM
glUniform2f(uniform->location, x, y);
}
void immUniform2fv(const char* name, const float data[2])
{
GET_UNIFORM
glUniform2fv(uniform->location, 1, data);
}
void immUniform3f(const char* name, float x, float y, float z)
{
GET_UNIFORM
glUniform3f(uniform->location, x, y, z);
}
void immUniform3fv(const char* name, const float data[3])
{
GET_UNIFORM
glUniform3fv(uniform->location, 1, data);
}
/* can increase this limit or move to another file */
#define MAX_UNIFORM_NAME_LEN 60
void immUniformArray3fv(const char* bare_name, const float *data, int count)
{
/* look up "name[0]" when given "name" */
const size_t len = strlen(bare_name);
#if TRUST_NO_ONE
assert(len <= MAX_UNIFORM_NAME_LEN);
#endif
char name[MAX_UNIFORM_NAME_LEN];
strcpy(name, bare_name);
name[len + 0] = '[';
name[len + 1] = '0';
name[len + 2] = ']';
name[len + 3] = '\0';
GET_UNIFORM
glUniform3fv(uniform->location, count, data);
}
void immUniform4f(const char* name, float x, float y, float z, float w)
{
GET_UNIFORM
glUniform4f(uniform->location, x, y, z, w);
}
void immUniform4fv(const char* name, const float data[4])
{
GET_UNIFORM
glUniform4fv(uniform->location, 1, data);
}
void immUniformArray4fv(const char* bare_name, const float *data, int count)
{
/* look up "name[0]" when given "name" */
const size_t len = strlen(bare_name);
#if TRUST_NO_ONE
assert(len <= MAX_UNIFORM_NAME_LEN);
#endif
char name[MAX_UNIFORM_NAME_LEN];
strcpy(name, bare_name);
name[len + 0] = '[';
name[len + 1] = '0';
name[len + 2] = ']';
name[len + 3] = '\0';
GET_UNIFORM
glUniform4fv(uniform->location, count, data);
}
void immUniformMatrix4fv(const char* name, const float data[4][4])
{
GET_UNIFORM
glUniformMatrix4fv(uniform->location, 1, GL_FALSE, (float *)data);
}
void immUniform1i(const char* name, int x)
{
GET_UNIFORM
glUniform1i(uniform->location, x);
}
void immUniform4iv(const char* name, const int data[4])
{
GET_UNIFORM
glUniform4iv(uniform->location, 1, data);
}
/* --- convenience functions for setting "uniform vec4 color" --- */
void immUniformColor4f(float r, float g, float b, float a)
{
const Gwn_ShaderInput* uniform = GWN_shaderinterface_uniform_builtin(imm.shader_interface, GWN_UNIFORM_COLOR);
#if TRUST_NO_ONE
assert(uniform != NULL);
#endif
glUniform4f(uniform->location, r, g, b, a);
}
void immUniformColor4fv(const float rgba[4])
{
immUniformColor4f(rgba[0], rgba[1], rgba[2], rgba[3]);
}
void immUniformColor3f(float r, float g, float b)
{
immUniformColor4f(r, g, b, 1.0f);
}
void immUniformColor3fv(const float rgb[3])
{
immUniformColor4f(rgb[0], rgb[1], rgb[2], 1.0f);
}
void immUniformColor3fvAlpha(const float rgb[3], float a)
{
immUniformColor4f(rgb[0], rgb[1], rgb[2], a);
}
/* TODO: v-- treat as sRGB? --v */
void immUniformColor3ub(unsigned char r, unsigned char g, unsigned char b)
{
const float scale = 1.0f / 255.0f;
immUniformColor4f(scale * r, scale * g, scale * b, 1.0f);
}
void immUniformColor4ub(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
{
const float scale = 1.0f / 255.0f;
immUniformColor4f(scale * r, scale * g, scale * b, scale * a);
}
void immUniformColor3ubv(const unsigned char rgb[3])
{
immUniformColor3ub(rgb[0], rgb[1], rgb[2]);
}
void immUniformColor3ubvAlpha(const unsigned char rgb[3], unsigned char alpha)
{
immUniformColor4ub(rgb[0], rgb[1], rgb[2], alpha);
}
void immUniformColor4ubv(const unsigned char rgba[4])
{
immUniformColor4ub(rgba[0], rgba[1], rgba[2], rgba[3]);
}

View File

@@ -27,8 +27,8 @@ set(INC
.
../glew-mx
../guardedalloc
../gawain
../../source/blender/blenlib
../../source/blender/gpu
)
set(INC_SYS

View File

@@ -49,7 +49,7 @@
#endif
extern "C" {
#include "gawain/gwn_immediate.h"
#include "GPU_immediate.h"
}
using namespace OCIO_NAMESPACE;

View File

@@ -31,7 +31,7 @@
#ifndef __BLF_INTERNAL_TYPES_H__
#define __BLF_INTERNAL_TYPES_H__
#include "../../../intern/gawain/gawain/gwn_vertex_buffer.h"
#include "GPU_vertex_buffer.h"
#include "GPU_texture.h"
#define BLF_BATCH_DRAW_LEN_MAX 2048 /* in glyph */

View File

@@ -45,7 +45,7 @@
#include "eevee_lightcache.h"
#include "eevee_private.h"
#include "../../../intern/gawain/gawain/gwn_context.h"
#include "GPU_context.h"
#include "WM_api.h"
#include "WM_types.h"

View File

@@ -82,7 +82,7 @@
#include "engines/workbench/workbench_engine.h"
#include "engines/external/external_engine.h"
#include "../../../intern/gawain/gawain/gwn_context.h"
#include "GPU_context.h"
#include "DEG_depsgraph.h"
#include "DEG_depsgraph_query.h"

View File

@@ -35,6 +35,7 @@
#include "BLI_threads.h"
#include "GPU_batch.h"
#include "GPU_context.h"
#include "GPU_framebuffer.h"
#include "GPU_shader.h"
#include "GPU_uniformbuffer.h"

View File

@@ -50,7 +50,6 @@ set(INC
../../../intern/glew-mx
../../../intern/guardedalloc
../../../intern/smoke/extern
../../../intern/gawain
)
set(INC_SYS
@@ -58,14 +57,17 @@ set(INC_SYS
)
set(SRC
intern/gpu_attr_binding.c
intern/gpu_basic_shader.c
intern/gpu_batch.c
intern/gpu_batch_presets.c
intern/gpu_batch_utils.c
intern/gpu_buffer_id.cpp
intern/gpu_buffers.c
intern/gpu_codegen.c
intern/gpu_debug.c
intern/gpu_draw.c
intern/gpu_element.c
intern/gpu_extensions.c
intern/gpu_framebuffer.c
intern/gpu_immediate.c
@@ -73,13 +75,18 @@ set(SRC
intern/gpu_init_exit.c
intern/gpu_material.c
intern/gpu_matrix.c
intern/gpu_primitive.c
intern/gpu_select.c
intern/gpu_select_pick.c
intern/gpu_select_sample_query.c
intern/gpu_shader.c
intern/gpu_shader_interface.c
intern/gpu_state.c
intern/gpu_texture.c
intern/gpu_uniformbuffer.c
intern/gpu_vertex_array_id.cpp
intern/gpu_vertex_buffer.c
intern/gpu_vertex_format.c
intern/gpu_viewport.c
shaders/gpu_shader_fx_lib.glsl
@@ -103,11 +110,15 @@ set(SRC
shaders/gpu_shader_smoke_frag.glsl
shaders/gpu_shader_smoke_vert.glsl
GPU_attr_binding.h
GPU_basic_shader.h
GPU_batch.h
GPU_buffer_id.h
GPU_buffers.h
GPU_common.h
GPU_debug.h
GPU_draw.h
GPU_element.h
GPU_extensions.h
GPU_framebuffer.h
GPU_glew.h
@@ -117,17 +128,26 @@ set(SRC
GPU_legacy_stubs.h
GPU_material.h
GPU_matrix.h
GPU_primitive.h
GPU_select.h
GPU_shader.h
GPU_shader_interface.h
GPU_state.h
GPU_texture.h
GPU_uniformbuffer.h
GPU_vertex_array_id.h
GPU_vertex_buffer.h
GPU_vertex_format.h
GPU_viewport.h
intern/gpu_attr_binding_private.h
intern/gpu_batch_private.h
intern/gpu_codegen.h
intern/gpu_primitive_private.h
intern/gpu_private.h
intern/gpu_select_private.h
intern/gpu_shader_private.h
intern/gpu_vertex_format_private.h
)
data_to_c_simple(shaders/gpu_shader_depth_only_frag.glsl SRC)

View File

@@ -32,7 +32,7 @@
#ifndef __GWN_ATTR_BINDING_H__
#define __GWN_ATTR_BINDING_H__
#include "gwn_common.h"
#include "GPU_common.h"
typedef struct Gwn_AttrBinding {
uint64_t loc_bits; /* store 4 bits for each of the 16 attribs */

View File

@@ -15,37 +15,185 @@
* 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 Blender Foundation.
* The Original Code is Copyright (C) 2016 by Mike Erwin.
* All rights reserved.
*
*
* Contributor(s): Mike Erwin
* Contributor(s): Blender Foundation
*
* ***** END GPL LICENSE BLOCK *****
*/
/* Batched geometry rendering is powered by the Gawain library.
* This file contains any additions or modifications specific to Blender.
/** \file blender/gpu/gwn_batch.h
* \ingroup gpu
*
* Gawain geometry batch
* Contains VAOs + VBOs + Shader representing a drawable entity.
*/
#ifndef __GPU_BATCH_H__
#define __GPU_BATCH_H__
#include "../../../intern/gawain/gawain/gwn_batch.h"
#include "../../../intern/gawain/gawain/gwn_batch_private.h"
// TODO: CMake magic to do this:
// #include "gawain/batch.h"
#include "BLI_compiler_attrs.h"
#include "BLI_sys_types.h"
#ifndef __GWN_BATCH_H__
#define __GWN_BATCH_H__
#include "GPU_vertex_buffer.h"
#include "GPU_element.h"
#include "GPU_shader_interface.h"
#include "GPU_shader.h"
/* gpu_batch.c */
void GWN_batch_program_set_builtin(Gwn_Batch *batch, GPUBuiltinShader shader_id) ATTR_NONNULL(1);
typedef enum {
GWN_BATCH_READY_TO_FORMAT,
GWN_BATCH_READY_TO_BUILD,
GWN_BATCH_BUILDING,
GWN_BATCH_READY_TO_DRAW
} Gwn_BatchPhase;
#define GWN_BATCH_VBO_MAX_LEN 3
#define GWN_BATCH_VAO_STATIC_LEN 3
#define GWN_BATCH_VAO_DYN_ALLOC_COUNT 16
typedef struct Gwn_Batch {
/* geometry */
Gwn_VertBuf* verts[GWN_BATCH_VBO_MAX_LEN]; /* verts[0] is required, others can be NULL */
Gwn_VertBuf* inst; /* instance attribs */
Gwn_IndexBuf* elem; /* NULL if element list not needed */
uint32_t gl_prim_type;
/* cached values (avoid dereferencing later) */
uint32_t vao_id;
uint32_t program;
const struct Gwn_ShaderInterface* interface;
/* book-keeping */
uint owns_flag;
struct Gwn_Context *context; /* used to free all vaos. this implies all vaos were created under the same context. */
Gwn_BatchPhase phase;
bool program_in_use;
/* Vao management: remembers all geometry state (vertex attrib bindings & element buffer)
* for each shader interface. Start with a static number of vaos and fallback to dynamic count
* if necessary. Once a batch goes dynamic it does not go back. */
bool is_dynamic_vao_count;
union {
/* Static handle count */
struct {
const struct Gwn_ShaderInterface* interfaces[GWN_BATCH_VAO_STATIC_LEN];
uint32_t vao_ids[GWN_BATCH_VAO_STATIC_LEN];
} static_vaos;
/* Dynamic handle count */
struct {
uint count;
const struct Gwn_ShaderInterface** interfaces;
uint32_t* vao_ids;
} dynamic_vaos;
};
/* XXX This is the only solution if we want to have some data structure using
* batches as key to identify nodes. We must destroy these nodes with this callback. */
void (*free_callback)(struct Gwn_Batch*, void*);
void* callback_data;
} Gwn_Batch;
enum {
GWN_BATCH_OWNS_VBO = (1 << 0),
/* each vbo index gets bit-shifted */
GWN_BATCH_OWNS_INSTANCES = (1 << 30),
GWN_BATCH_OWNS_INDEX = (1 << 31),
};
Gwn_Batch* GWN_batch_create_ex(Gwn_PrimType, Gwn_VertBuf*, Gwn_IndexBuf*, uint owns_flag);
void GWN_batch_init_ex(Gwn_Batch*, Gwn_PrimType, Gwn_VertBuf*, Gwn_IndexBuf*, uint owns_flag);
Gwn_Batch* GWN_batch_duplicate(Gwn_Batch* batch_src);
#define GWN_batch_create(prim, verts, elem) \
GWN_batch_create_ex(prim, verts, elem, 0)
#define GWN_batch_init(batch, prim, verts, elem) \
GWN_batch_init_ex(batch, prim, verts, elem, 0)
void GWN_batch_discard(Gwn_Batch*); /* verts & elem are not discarded */
void gwn_batch_vao_cache_clear(Gwn_Batch*);
void GWN_batch_callback_free_set(Gwn_Batch*, void (*callback)(Gwn_Batch*, void*), void*);
void GWN_batch_instbuf_set(Gwn_Batch*, Gwn_VertBuf*, bool own_vbo); /* Instancing */
int GWN_batch_vertbuf_add_ex(Gwn_Batch*, Gwn_VertBuf*, bool own_vbo);
#define GWN_batch_vertbuf_add(batch, verts) \
GWN_batch_vertbuf_add_ex(batch, verts, false)
void GWN_batch_program_set_no_use(Gwn_Batch*, uint32_t program, const Gwn_ShaderInterface*);
void GWN_batch_program_set(Gwn_Batch*, uint32_t program, const Gwn_ShaderInterface*);
void GWN_batch_program_set_builtin(Gwn_Batch *batch, GPUBuiltinShader shader_id);
/* Entire batch draws with one shader program, but can be redrawn later with another program. */
/* Vertex shader's inputs must be compatible with the batch's vertex format. */
void GWN_batch_program_use_begin(Gwn_Batch*); /* call before Batch_Uniform (temp hack?) */
void GWN_batch_program_use_end(Gwn_Batch*);
void GWN_batch_uniform_1ui(Gwn_Batch*, const char* name, int value);
void GWN_batch_uniform_1i(Gwn_Batch*, const char* name, int value);
void GWN_batch_uniform_1b(Gwn_Batch*, const char* name, bool value);
void GWN_batch_uniform_1f(Gwn_Batch*, const char* name, float value);
void GWN_batch_uniform_2f(Gwn_Batch*, const char* name, float x, float y);
void GWN_batch_uniform_3f(Gwn_Batch*, const char* name, float x, float y, float z);
void GWN_batch_uniform_4f(Gwn_Batch*, const char* name, float x, float y, float z, float w);
void GWN_batch_uniform_2fv(Gwn_Batch*, const char* name, const float data[2]);
void GWN_batch_uniform_3fv(Gwn_Batch*, const char* name, const float data[3]);
void GWN_batch_uniform_4fv(Gwn_Batch*, const char* name, const float data[4]);
void GWN_batch_uniform_2fv_array(Gwn_Batch*, const char* name, int len, const float *data);
void GWN_batch_uniform_4fv_array(Gwn_Batch*, const char* name, int len, const float *data);
void GWN_batch_uniform_mat4(Gwn_Batch*, const char* name, const float data[4][4]);
void GWN_batch_draw(Gwn_Batch*);
/* This does not bind/unbind shader and does not call GPU_matrix_bind() */
void GWN_batch_draw_range_ex(Gwn_Batch*, int v_first, int v_count, bool force_instance);
/* Does not even need batch */
void GWN_draw_primitive(Gwn_PrimType, int v_count);
#if 0 /* future plans */
/* Can multiple batches share a Gwn_VertBuf? Use ref count? */
/* We often need a batch with its own data, to be created and discarded together. */
/* WithOwn variants reduce number of system allocations. */
typedef struct BatchWithOwnVertexBuffer {
Gwn_Batch batch;
Gwn_VertBuf verts; /* link batch.verts to this */
} BatchWithOwnVertexBuffer;
typedef struct BatchWithOwnElementList {
Gwn_Batch batch;
Gwn_IndexBuf elem; /* link batch.elem to this */
} BatchWithOwnElementList;
typedef struct BatchWithOwnVertexBufferAndElementList {
Gwn_Batch batch;
Gwn_IndexBuf elem; /* link batch.elem to this */
Gwn_VertBuf verts; /* link batch.verts to this */
} BatchWithOwnVertexBufferAndElementList;
Gwn_Batch* create_BatchWithOwnVertexBuffer(Gwn_PrimType, Gwn_VertFormat*, uint v_len, Gwn_IndexBuf*);
Gwn_Batch* create_BatchWithOwnElementList(Gwn_PrimType, Gwn_VertBuf*, uint prim_len);
Gwn_Batch* create_BatchWithOwnVertexBufferAndElementList(Gwn_PrimType, Gwn_VertFormat*, uint v_len, uint prim_len);
/* verts: shared, own */
/* elem: none, shared, own */
Gwn_Batch* create_BatchInGeneral(Gwn_PrimType, VertexBufferStuff, ElementListStuff);
#endif /* future plans */
void gpu_batch_init(void);
void gpu_batch_exit(void);
#endif /* __GPU_BATCH_H__ */
/* Macros */
#define GWN_BATCH_DISCARD_SAFE(batch) do { \
if (batch != NULL) { \
GWN_batch_discard(batch); \
batch = NULL; \
} \
} while (0)
#endif /* __GWN_BATCH_H__ */

View File

@@ -41,7 +41,7 @@
extern "C" {
#endif
#include "gwn_common.h"
#include "GPU_common.h"
GLuint GWN_buf_id_alloc(void);
void GWN_buf_id_free(GLuint buffer_id);

View File

@@ -36,9 +36,9 @@
extern "C" {
#endif
#include "gwn_common.h"
#include "gwn_batch.h"
#include "gwn_shader_interface.h"
#include "GPU_common.h"
#include "GPU_batch.h"
#include "GPU_shader_interface.h"
typedef struct Gwn_Context Gwn_Context;

View File

@@ -32,7 +32,7 @@
#ifndef __GWN_ELEMENT_H__
#define __GWN_ELEMENT_H__
#include "gwn_primitive.h"
#include "GPU_primitive.h"
#define GWN_TRACK_INDEX_RANGE 1

View File

@@ -24,29 +24,113 @@
* ***** END GPL LICENSE BLOCK *****
*/
/* Immediate mode rendering is powered by the Gawain library.
* This file contains any additions or modifications specific to Blender.
/** \file blender/gpu/GPU_immediate.h
* \ingroup gpu
*
* Gawain immediate mode work-alike
*/
#ifndef __GPU_IMMEDIATE_H__
#define __GPU_IMMEDIATE_H__
#include "../../../intern/gawain/gawain/gwn_immediate.h"
#include "../../../intern/gawain/gawain/gwn_imm_util.h"
// TODO: CMake magic to do this:
// #include "gawain/gwn_immediate.h"
// #include "gawain/gwn_imm_util.h"
#include "GPU_vertex_format.h"
#include "GPU_primitive.h"
#include "GPU_shader_interface.h"
#include "GPU_batch.h"
#include "GPU_immediate_util.h"
#include "GPU_shader.h"
Gwn_VertFormat* immVertexFormat(void); /* returns a cleared vertex format, ready for add_attrib. */
void immBindProgram(uint32_t program, const Gwn_ShaderInterface*); /* every immBegin must have a program bound first. */
void immUnbindProgram(void); /* call after your last immEnd, or before binding another program. */
void immBegin(Gwn_PrimType, uint vertex_len); /* must supply exactly vertex_len vertices. */
void immBeginAtMost(Gwn_PrimType, uint max_vertex_len); /* can supply fewer vertices. */
void immEnd(void); /* finishes and draws. */
/* ImmBegin a batch, then use standard immFunctions as usual. */
/* ImmEnd will finalize the batch instead of drawing. */
/* Then you can draw it as many times as you like! Partially replaces the need for display lists. */
Gwn_Batch* immBeginBatch(Gwn_PrimType, uint vertex_len);
Gwn_Batch* immBeginBatchAtMost(Gwn_PrimType, uint vertex_len);
/* Provide attribute values that can change per vertex. */
/* First vertex after immBegin must have all its attributes specified. */
/* Skipped attributes will continue using the previous value for that attrib_id. */
void immAttrib1f(uint attrib_id, float x);
void immAttrib2f(uint attrib_id, float x, float y);
void immAttrib3f(uint attrib_id, float x, float y, float z);
void immAttrib4f(uint attrib_id, float x, float y, float z, float w);
void immAttrib2i(uint attrib_id, int x, int y);
void immAttrib1u(uint attrib_id, uint x);
void immAttrib2s(uint attrib_id, short x, short y);
void immAttrib2fv(uint attrib_id, const float data[2]);
void immAttrib3fv(uint attrib_id, const float data[3]);
void immAttrib4fv(uint attrib_id, const float data[4]);
void immAttrib3ub(uint attrib_id, unsigned char r, unsigned char g, unsigned char b);
void immAttrib4ub(uint attrib_id, unsigned char r, unsigned char g, unsigned char b, unsigned char a);
void immAttrib3ubv(uint attrib_id, const unsigned char data[4]);
void immAttrib4ubv(uint attrib_id, const unsigned char data[4]);
/* Explicitly skip an attribute. */
/* This advanced option kills automatic value copying for this attrib_id. */
void immSkipAttrib(uint attrib_id);
/* Provide one last attribute value & end the current vertex. */
/* This is most often used for 2D or 3D position (similar to glVertex). */
void immVertex2f(uint attrib_id, float x, float y);
void immVertex3f(uint attrib_id, float x, float y, float z);
void immVertex4f(uint attrib_id, float x, float y, float z, float w);
void immVertex2i(uint attrib_id, int x, int y);
void immVertex2s(uint attrib_id, short x, short y);
void immVertex2fv(uint attrib_id, const float data[2]);
void immVertex3fv(uint attrib_id, const float data[3]);
void immVertex2iv(uint attrib_id, const int data[2]);
/* Provide uniform values that don't change for the entire draw call. */
void immUniform1i(const char* name, int x);
void immUniform4iv(const char* name, const int data[4]);
void immUniform1f(const char* name, float x);
void immUniform2f(const char* name, float x, float y);
void immUniform2fv(const char* name, const float data[2]);
void immUniform3f(const char* name, float x, float y, float z);
void immUniform3fv(const char* name, const float data[3]);
void immUniformArray3fv(const char* name, const float *data, int count);
void immUniform4f(const char* name, float x, float y, float z, float w);
void immUniform4fv(const char* name, const float data[4]);
void immUniformArray4fv(const char* bare_name, const float *data, int count);
void immUniformMatrix4fv(const char* name, const float data[4][4]);
/* Convenience functions for setting "uniform vec4 color". */
/* The rgb functions have implicit alpha = 1.0. */
void immUniformColor4f(float r, float g, float b, float a);
void immUniformColor4fv(const float rgba[4]);
void immUniformColor3f(float r, float g, float b);
void immUniformColor3fv(const float rgb[3]);
void immUniformColor3fvAlpha(const float rgb[3], float a);
void immUniformColor3ub(unsigned char r, unsigned char g, unsigned char b);
void immUniformColor4ub(unsigned char r, unsigned char g, unsigned char b, unsigned char a);
void immUniformColor3ubv(const unsigned char rgb[3]);
void immUniformColor3ubvAlpha(const unsigned char rgb[3], unsigned char a);
void immUniformColor4ubv(const unsigned char rgba[4]);
/* Extend immBindProgram to use Blenders library of built-in shader programs.
* Use immUnbindProgram() when done. */
void immBindBuiltinProgram(GPUBuiltinShader shader_id);
/*
* Extend immUniformColor to take Blender's themes
*/
/* Extend immUniformColor to take Blender's themes */
void immUniformThemeColor(int color_id);
void immUniformThemeColor3(int color_id);
void immUniformThemeColorShade(int color_id, int offset);
@@ -55,4 +139,10 @@ void immUniformThemeColorBlendShade(int color_id1, int color_id2, float fac, int
void immUniformThemeColorBlend(int color_id1, int color_id2, float fac);
void immThemeColorShadeAlpha(int colorid, int coloffset, int alphaoffset);
/* These are called by the system -- not part of drawing API. */
void immInit(void);
void immActivate(void);
void immDeactivate(void);
void immDestroy(void);
#endif /* __GPU_IMMEDIATE_H__ */

View File

@@ -27,6 +27,15 @@
#ifndef __GPU_IMMEDIATE_UTIL_H__
#define __GPU_IMMEDIATE_UTIL_H__
/* Draw 2D rectangles (replaces glRect functions) */
/* caller is reponsible for vertex format & shader */
void immRectf(uint pos, float x1, float y1, float x2, float y2);
void immRecti(uint pos, int x1, int y1, int x2, int y2);
/* Same as immRectf/immRecti but does not call immBegin/immEnd. To use with GWN_PRIM_TRIS. */
void immRectf_fast_with_color(uint pos, uint col, float x1, float y1, float x2, float y2, const float color[4]);
void immRecti_fast_with_color(uint pos, uint col, int x1, int y1, int x2, int y2, const float color[4]);
void imm_cpack(uint x);
void imm_draw_circle_wire_2d(uint shdr_pos, float x, float y, float radius, int nsegments);

View File

@@ -32,7 +32,7 @@
#ifndef __GWN_PRIMITIVE_H__
#define __GWN_PRIMITIVE_H__
#include "gwn_common.h"
#include "GPU_common.h"
typedef enum {
GWN_PRIM_POINTS,

View File

@@ -32,7 +32,7 @@
#ifndef __GWN_SHADER_INTERFACE_H__
#define __GWN_SHADER_INTERFACE_H__
#include "gwn_common.h"
#include "GPU_common.h"
typedef enum {
GWN_UNIFORM_NONE = 0, /* uninitialized/unknown */

View File

@@ -41,8 +41,8 @@
extern "C" {
#endif
#include "gwn_common.h"
#include "gwn_context.h"
#include "GPU_common.h"
#include "GPU_context.h"
GLuint GWN_vao_default(void);
GLuint GWN_vao_alloc(void);

View File

@@ -32,7 +32,7 @@
#ifndef __GWN_VERTEX_BUFFER_H__
#define __GWN_VERTEX_BUFFER_H__
#include "gwn_vertex_format.h"
#include "GPU_vertex_format.h"
#define VRAM_USAGE 1
/* How to create a Gwn_VertBuf: */

View File

@@ -32,7 +32,7 @@
#ifndef __GWN_VERTEX_FORMAT_H__
#define __GWN_VERTEX_FORMAT_H__
#include "gwn_common.h"
#include "GPU_common.h"
#define GWN_VERT_ATTR_MAX_LEN 16
#define GWN_VERT_ATTR_MAX_NAMES 3

View File

@@ -29,8 +29,8 @@
* Gawain vertex attribute binding
*/
#include "gwn_attr_binding.h"
#include "gwn_attr_binding_private.h"
#include "GPU_attr_binding.h"
#include "gpu_attr_binding_private.h"
#include <stddef.h>
#include <stdlib.h>

View File

@@ -32,8 +32,8 @@
#ifndef __GWN_ATTR_BINDING_PRIVATE_H__
#define __GWN_ATTR_BINDING_PRIVATE_H__
#include "gwn_vertex_format.h"
#include "gwn_shader_interface.h"
#include "GPU_vertex_format.h"
#include "GPU_shader_interface.h"
void AttribBinding_clear(Gwn_AttrBinding*);

View File

@@ -15,32 +15,639 @@
* 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 Blender Foundation.
* The Original Code is Copyright (C) 2016 by Mike Erwin.
* All rights reserved.
*
* The Original Code is: all of this file.
*
* Contributor(s): Mike Erwin
* Contributor(s): Blender Foundation
*
* ***** END GPL LICENSE BLOCK *****
*/
/** \file blender/gpu/intern/gpu_batch.c
/** \file blender/gpu/intern/gwn_batch.c
* \ingroup gpu
*
* Gawain geometry batch
* Contains VAOs + VBOs + Shader representing a drawable entity.
*/
#include "MEM_guardedalloc.h"
#include "BLI_utildefines.h"
#include "BLI_rect.h"
#include "BLI_math.h"
#include "BLI_polyfill_2d.h"
#include "BLI_sort_utils.h"
#include "GPU_batch.h" /* own include */
#include "GPU_batch.h"
#include "GPU_batch_presets.h"
#include "GPU_buffer_id.h"
#include "GPU_matrix.h"
#include "GPU_shader.h"
#include "GPU_vertex_array_id.h"
#include "gpu_batch_private.h"
#include "gpu_primitive_private.h"
#include "gpu_shader_private.h"
#include <stdlib.h>
#include <string.h>
static void batch_update_program_bindings(Gwn_Batch* batch, uint v_first);
void gwn_batch_vao_cache_clear(Gwn_Batch* batch)
{
if (batch->context == NULL) {
return;
}
if (batch->is_dynamic_vao_count) {
for (int i = 0; i < batch->dynamic_vaos.count; ++i) {
if (batch->dynamic_vaos.vao_ids[i]) {
GWN_vao_free(batch->dynamic_vaos.vao_ids[i], batch->context);
}
if (batch->dynamic_vaos.interfaces[i]) {
GWN_shaderinterface_remove_batch_ref((Gwn_ShaderInterface *)batch->dynamic_vaos.interfaces[i], batch);
}
}
free(batch->dynamic_vaos.interfaces);
free(batch->dynamic_vaos.vao_ids);
}
else {
for (int i = 0; i < GWN_BATCH_VAO_STATIC_LEN; ++i) {
if (batch->static_vaos.vao_ids[i]) {
GWN_vao_free(batch->static_vaos.vao_ids[i], batch->context);
}
if (batch->static_vaos.interfaces[i]) {
GWN_shaderinterface_remove_batch_ref((Gwn_ShaderInterface *)batch->static_vaos.interfaces[i], batch);
}
}
}
batch->is_dynamic_vao_count = false;
for (int i = 0; i < GWN_BATCH_VAO_STATIC_LEN; ++i) {
batch->static_vaos.vao_ids[i] = 0;
batch->static_vaos.interfaces[i] = NULL;
}
gwn_context_remove_batch(batch->context, batch);
batch->context = NULL;
}
Gwn_Batch* GWN_batch_create_ex(
Gwn_PrimType prim_type, Gwn_VertBuf* verts, Gwn_IndexBuf* elem,
uint owns_flag)
{
Gwn_Batch* batch = calloc(1, sizeof(Gwn_Batch));
GWN_batch_init_ex(batch, prim_type, verts, elem, owns_flag);
return batch;
}
void GWN_batch_init_ex(
Gwn_Batch* batch, Gwn_PrimType prim_type, Gwn_VertBuf* verts, Gwn_IndexBuf* elem,
uint owns_flag)
{
#if TRUST_NO_ONE
assert(verts != NULL);
#endif
batch->verts[0] = verts;
for (int v = 1; v < GWN_BATCH_VBO_MAX_LEN; ++v) {
batch->verts[v] = NULL;
}
batch->inst = NULL;
batch->elem = elem;
batch->gl_prim_type = convert_prim_type_to_gl(prim_type);
batch->phase = GWN_BATCH_READY_TO_DRAW;
batch->is_dynamic_vao_count = false;
batch->owns_flag = owns_flag;
batch->free_callback = NULL;
}
/* This will share the VBOs with the new batch. */
Gwn_Batch* GWN_batch_duplicate(Gwn_Batch* batch_src)
{
Gwn_Batch* batch = GWN_batch_create_ex(GWN_PRIM_POINTS, batch_src->verts[0], batch_src->elem, 0);
batch->gl_prim_type = batch_src->gl_prim_type;
for (int v = 1; v < GWN_BATCH_VBO_MAX_LEN; ++v) {
batch->verts[v] = batch_src->verts[v];
}
return batch;
}
void GWN_batch_discard(Gwn_Batch* batch)
{
if (batch->owns_flag & GWN_BATCH_OWNS_INDEX) {
GWN_indexbuf_discard(batch->elem);
}
if (batch->owns_flag & GWN_BATCH_OWNS_INSTANCES) {
GWN_vertbuf_discard(batch->inst);
}
if ((batch->owns_flag & ~GWN_BATCH_OWNS_INDEX) != 0) {
for (int v = 0; v < GWN_BATCH_VBO_MAX_LEN; ++v) {
if (batch->verts[v] == NULL) {
break;
}
if (batch->owns_flag & (1 << v)) {
GWN_vertbuf_discard(batch->verts[v]);
}
}
}
gwn_batch_vao_cache_clear(batch);
if (batch->free_callback) {
batch->free_callback(batch, batch->callback_data);
}
free(batch);
}
void GWN_batch_callback_free_set(Gwn_Batch* batch, void (*callback)(Gwn_Batch*, void*), void* user_data)
{
batch->free_callback = callback;
batch->callback_data = user_data;
}
void GWN_batch_instbuf_set(Gwn_Batch* batch, Gwn_VertBuf* inst, bool own_vbo)
{
#if TRUST_NO_ONE
assert(inst != NULL);
#endif
/* redo the bindings */
gwn_batch_vao_cache_clear(batch);
if (batch->inst != NULL && (batch->owns_flag & GWN_BATCH_OWNS_INSTANCES)) {
GWN_vertbuf_discard(batch->inst);
}
batch->inst = inst;
if (own_vbo) {
batch->owns_flag |= GWN_BATCH_OWNS_INSTANCES;
}
else {
batch->owns_flag &= ~GWN_BATCH_OWNS_INSTANCES;
}
}
/* Returns the index of verts in the batch. */
int GWN_batch_vertbuf_add_ex(
Gwn_Batch* batch, Gwn_VertBuf* verts,
bool own_vbo)
{
/* redo the bindings */
gwn_batch_vao_cache_clear(batch);
for (uint v = 0; v < GWN_BATCH_VBO_MAX_LEN; ++v) {
if (batch->verts[v] == NULL) {
#if TRUST_NO_ONE
/* for now all VertexBuffers must have same vertex_len */
assert(verts->vertex_len == batch->verts[0]->vertex_len);
#endif
batch->verts[v] = verts;
/* TODO: mark dirty so we can keep attrib bindings up-to-date */
if (own_vbo)
batch->owns_flag |= (1 << v);
return v;
}
}
/* we only make it this far if there is no room for another Gwn_VertBuf */
#if TRUST_NO_ONE
assert(false);
#endif
return -1;
}
static GLuint batch_vao_get(Gwn_Batch *batch)
{
/* Search through cache */
if (batch->is_dynamic_vao_count) {
for (int i = 0; i < batch->dynamic_vaos.count; ++i)
if (batch->dynamic_vaos.interfaces[i] == batch->interface)
return batch->dynamic_vaos.vao_ids[i];
}
else {
for (int i = 0; i < GWN_BATCH_VAO_STATIC_LEN; ++i)
if (batch->static_vaos.interfaces[i] == batch->interface)
return batch->static_vaos.vao_ids[i];
}
/* Set context of this batch.
* It will be bound to it until gwn_batch_vao_cache_clear is called.
* Until then it can only be drawn with this context. */
if (batch->context == NULL) {
batch->context = GWN_context_active_get();
gwn_context_add_batch(batch->context, batch);
}
#if TRUST_NO_ONE
else {
/* Make sure you are not trying to draw this batch in another context. */
assert(batch->context == GWN_context_active_get());
}
#endif
/* Cache miss, time to add a new entry! */
GLuint new_vao = 0;
if (!batch->is_dynamic_vao_count) {
int i; /* find first unused slot */
for (i = 0; i < GWN_BATCH_VAO_STATIC_LEN; ++i)
if (batch->static_vaos.vao_ids[i] == 0)
break;
if (i < GWN_BATCH_VAO_STATIC_LEN) {
batch->static_vaos.interfaces[i] = batch->interface;
batch->static_vaos.vao_ids[i] = new_vao = GWN_vao_alloc();
}
else {
/* Not enough place switch to dynamic. */
batch->is_dynamic_vao_count = true;
/* Erase previous entries, they will be added back if drawn again. */
for (int j = 0; j < GWN_BATCH_VAO_STATIC_LEN; ++j) {
GWN_shaderinterface_remove_batch_ref((Gwn_ShaderInterface*)batch->static_vaos.interfaces[j], batch);
GWN_vao_free(batch->static_vaos.vao_ids[j], batch->context);
}
/* Init dynamic arrays and let the branch below set the values. */
batch->dynamic_vaos.count = GWN_BATCH_VAO_DYN_ALLOC_COUNT;
batch->dynamic_vaos.interfaces = calloc(batch->dynamic_vaos.count, sizeof(Gwn_ShaderInterface*));
batch->dynamic_vaos.vao_ids = calloc(batch->dynamic_vaos.count, sizeof(GLuint));
}
}
if (batch->is_dynamic_vao_count) {
int i; /* find first unused slot */
for (i = 0; i < batch->dynamic_vaos.count; ++i)
if (batch->dynamic_vaos.vao_ids[i] == 0)
break;
if (i == batch->dynamic_vaos.count) {
/* Not enough place, realloc the array. */
i = batch->dynamic_vaos.count;
batch->dynamic_vaos.count += GWN_BATCH_VAO_DYN_ALLOC_COUNT;
batch->dynamic_vaos.interfaces = realloc(batch->dynamic_vaos.interfaces, sizeof(Gwn_ShaderInterface*) * batch->dynamic_vaos.count);
batch->dynamic_vaos.vao_ids = realloc(batch->dynamic_vaos.vao_ids, sizeof(GLuint) * batch->dynamic_vaos.count);
memset(batch->dynamic_vaos.interfaces + i, 0, sizeof(Gwn_ShaderInterface*) * GWN_BATCH_VAO_DYN_ALLOC_COUNT);
memset(batch->dynamic_vaos.vao_ids + i, 0, sizeof(GLuint) * GWN_BATCH_VAO_DYN_ALLOC_COUNT);
}
batch->dynamic_vaos.interfaces[i] = batch->interface;
batch->dynamic_vaos.vao_ids[i] = new_vao = GWN_vao_alloc();
}
GWN_shaderinterface_add_batch_ref((Gwn_ShaderInterface*)batch->interface, batch);
#if TRUST_NO_ONE
assert(new_vao != 0);
#endif
/* We just got a fresh VAO we need to initialize it. */
glBindVertexArray(new_vao);
batch_update_program_bindings(batch, 0);
glBindVertexArray(0);
return new_vao;
}
void GWN_batch_program_set_no_use(Gwn_Batch* batch, uint32_t program, const Gwn_ShaderInterface* shaderface)
{
#if TRUST_NO_ONE
assert(glIsProgram(shaderface->program));
assert(batch->program_in_use == 0);
#endif
batch->interface = shaderface;
batch->program = program;
batch->vao_id = batch_vao_get(batch);
}
void GWN_batch_program_set(Gwn_Batch* batch, uint32_t program, const Gwn_ShaderInterface* shaderface)
{
GWN_batch_program_set_no_use(batch, program, shaderface);
GWN_batch_program_use_begin(batch); /* hack! to make Batch_Uniform* simpler */
}
void gwn_batch_remove_interface_ref(Gwn_Batch* batch, const Gwn_ShaderInterface* interface)
{
if (batch->is_dynamic_vao_count) {
for (int i = 0; i < batch->dynamic_vaos.count; ++i) {
if (batch->dynamic_vaos.interfaces[i] == interface) {
GWN_vao_free(batch->dynamic_vaos.vao_ids[i], batch->context);
batch->dynamic_vaos.vao_ids[i] = 0;
batch->dynamic_vaos.interfaces[i] = NULL;
break; /* cannot have duplicates */
}
}
}
else {
int i;
for (i = 0; i < GWN_BATCH_VAO_STATIC_LEN; ++i) {
if (batch->static_vaos.interfaces[i] == interface) {
GWN_vao_free(batch->static_vaos.vao_ids[i], batch->context);
batch->static_vaos.vao_ids[i] = 0;
batch->static_vaos.interfaces[i] = NULL;
break; /* cannot have duplicates */
}
}
}
}
static void create_bindings(
Gwn_VertBuf* verts, const Gwn_ShaderInterface* interface,
uint v_first, const bool use_instancing)
{
const Gwn_VertFormat* format = &verts->format;
const uint attr_len = format->attr_len;
const uint stride = format->stride;
GWN_vertbuf_use(verts);
for (uint a_idx = 0; a_idx < attr_len; ++a_idx) {
const Gwn_VertAttr* a = format->attribs + a_idx;
const GLvoid* pointer = (const GLubyte*)0 + a->offset + v_first * stride;
for (uint n_idx = 0; n_idx < a->name_len; ++n_idx) {
const Gwn_ShaderInput* input = GWN_shaderinterface_attr(interface, a->name[n_idx]);
if (input == NULL) continue;
if (a->comp_len == 16 || a->comp_len == 12 || a->comp_len == 8) {
#if TRUST_NO_ONE
assert(a->fetch_mode == GWN_FETCH_FLOAT);
assert(a->gl_comp_type == GL_FLOAT);
#endif
for (int i = 0; i < a->comp_len / 4; ++i) {
glEnableVertexAttribArray(input->location + i);
glVertexAttribDivisor(input->location + i, (use_instancing) ? 1 : 0);
glVertexAttribPointer(input->location + i, 4, a->gl_comp_type, GL_FALSE, stride,
(const GLubyte*)pointer + i * 16);
}
}
else
{
glEnableVertexAttribArray(input->location);
glVertexAttribDivisor(input->location, (use_instancing) ? 1 : 0);
switch (a->fetch_mode) {
case GWN_FETCH_FLOAT:
case GWN_FETCH_INT_TO_FLOAT:
glVertexAttribPointer(input->location, a->comp_len, a->gl_comp_type, GL_FALSE, stride, pointer);
break;
case GWN_FETCH_INT_TO_FLOAT_UNIT:
glVertexAttribPointer(input->location, a->comp_len, a->gl_comp_type, GL_TRUE, stride, pointer);
break;
case GWN_FETCH_INT:
glVertexAttribIPointer(input->location, a->comp_len, a->gl_comp_type, stride, pointer);
break;
}
}
}
}
}
static void batch_update_program_bindings(Gwn_Batch* batch, uint v_first)
{
for (int v = 0; v < GWN_BATCH_VBO_MAX_LEN && batch->verts[v] != NULL; ++v) {
create_bindings(batch->verts[v], batch->interface, (batch->inst) ? 0 : v_first, false);
}
if (batch->inst) {
create_bindings(batch->inst, batch->interface, v_first, true);
}
if (batch->elem) {
GWN_indexbuf_use(batch->elem);
}
}
void GWN_batch_program_use_begin(Gwn_Batch* batch)
{
/* NOTE: use_program & done_using_program are fragile, depend on staying in sync with
* the GL context's active program. use_program doesn't mark other programs as "not used". */
/* TODO: make not fragile (somehow) */
if (!batch->program_in_use) {
glUseProgram(batch->program);
batch->program_in_use = true;
}
}
void GWN_batch_program_use_end(Gwn_Batch* batch)
{
if (batch->program_in_use) {
#if PROGRAM_NO_OPTI
glUseProgram(0);
#endif
batch->program_in_use = false;
}
}
#if TRUST_NO_ONE
#define GET_UNIFORM const Gwn_ShaderInput* uniform = GWN_shaderinterface_uniform(batch->interface, name); assert(uniform);
#else
#define GET_UNIFORM const Gwn_ShaderInput* uniform = GWN_shaderinterface_uniform(batch->interface, name);
#endif
void GWN_batch_uniform_1ui(Gwn_Batch* batch, const char* name, int value)
{
GET_UNIFORM
glUniform1ui(uniform->location, value);
}
void GWN_batch_uniform_1i(Gwn_Batch* batch, const char* name, int value)
{
GET_UNIFORM
glUniform1i(uniform->location, value);
}
void GWN_batch_uniform_1b(Gwn_Batch* batch, const char* name, bool value)
{
GET_UNIFORM
glUniform1i(uniform->location, value ? GL_TRUE : GL_FALSE);
}
void GWN_batch_uniform_2f(Gwn_Batch* batch, const char* name, float x, float y)
{
GET_UNIFORM
glUniform2f(uniform->location, x, y);
}
void GWN_batch_uniform_3f(Gwn_Batch* batch, const char* name, float x, float y, float z)
{
GET_UNIFORM
glUniform3f(uniform->location, x, y, z);
}
void GWN_batch_uniform_4f(Gwn_Batch* batch, const char* name, float x, float y, float z, float w)
{
GET_UNIFORM
glUniform4f(uniform->location, x, y, z, w);
}
void GWN_batch_uniform_1f(Gwn_Batch* batch, const char* name, float x)
{
GET_UNIFORM
glUniform1f(uniform->location, x);
}
void GWN_batch_uniform_2fv(Gwn_Batch* batch, const char* name, const float data[2])
{
GET_UNIFORM
glUniform2fv(uniform->location, 1, data);
}
void GWN_batch_uniform_3fv(Gwn_Batch* batch, const char* name, const float data[3])
{
GET_UNIFORM
glUniform3fv(uniform->location, 1, data);
}
void GWN_batch_uniform_4fv(Gwn_Batch* batch, const char* name, const float data[4])
{
GET_UNIFORM
glUniform4fv(uniform->location, 1, data);
}
void GWN_batch_uniform_2fv_array(Gwn_Batch* batch, const char* name, const int len, const float *data)
{
GET_UNIFORM
glUniform2fv(uniform->location, len, data);
}
void GWN_batch_uniform_4fv_array(Gwn_Batch* batch, const char* name, const int len, const float *data)
{
GET_UNIFORM
glUniform4fv(uniform->location, len, data);
}
void GWN_batch_uniform_mat4(Gwn_Batch* batch, const char* name, const float data[4][4])
{
GET_UNIFORM
glUniformMatrix4fv(uniform->location, 1, GL_FALSE, (const float *)data);
}
static void primitive_restart_enable(const Gwn_IndexBuf *el)
{
// TODO(fclem) Replace by GL_PRIMITIVE_RESTART_FIXED_INDEX when we have ogl 4.3
glEnable(GL_PRIMITIVE_RESTART);
GLuint restart_index = (GLuint)0xFFFFFFFF;
#if GWN_TRACK_INDEX_RANGE
if (el->index_type == GWN_INDEX_U8)
restart_index = (GLuint)0xFF;
else if (el->index_type == GWN_INDEX_U16)
restart_index = (GLuint)0xFFFF;
#endif
glPrimitiveRestartIndex(restart_index);
}
static void primitive_restart_disable(void)
{
glDisable(GL_PRIMITIVE_RESTART);
}
void GWN_batch_draw(Gwn_Batch* batch)
{
#if TRUST_NO_ONE
assert(batch->phase == GWN_BATCH_READY_TO_DRAW);
assert(batch->verts[0]->vbo_id != 0);
#endif
GWN_batch_program_use_begin(batch);
GPU_matrix_bind(batch->interface); // external call.
GWN_batch_draw_range_ex(batch, 0, 0, false);
GWN_batch_program_use_end(batch);
}
void GWN_batch_draw_range_ex(Gwn_Batch* batch, int v_first, int v_count, bool force_instance)
{
#if TRUST_NO_ONE
assert(!(force_instance && (batch->inst == NULL)) || v_count > 0); // we cannot infer length if force_instance
#endif
const bool do_instance = (force_instance || batch->inst);
// If using offset drawing, use the default VAO and redo bindings.
if (v_first != 0 && (do_instance || batch->elem)) {
glBindVertexArray(GWN_vao_default());
batch_update_program_bindings(batch, v_first);
}
else {
glBindVertexArray(batch->vao_id);
}
if (do_instance) {
/* Infer length if vertex count is not given */
if (v_count == 0) {
v_count = batch->inst->vertex_len;
}
if (batch->elem) {
const Gwn_IndexBuf* el = batch->elem;
if (el->use_prim_restart) {
primitive_restart_enable(el);
}
#if GWN_TRACK_INDEX_RANGE
glDrawElementsInstancedBaseVertex(batch->gl_prim_type,
el->index_len,
el->gl_index_type,
0,
v_count,
el->base_index);
#else
glDrawElementsInstanced(batch->gl_prim_type, el->index_len, GL_UNSIGNED_INT, 0, v_count);
#endif
if (el->use_prim_restart) {
primitive_restart_disable();
}
}
else {
glDrawArraysInstanced(batch->gl_prim_type, 0, batch->verts[0]->vertex_len, v_count);
}
}
else {
/* Infer length if vertex count is not given */
if (v_count == 0) {
v_count = (batch->elem) ? batch->elem->index_len : batch->verts[0]->vertex_len;
}
if (batch->elem) {
const Gwn_IndexBuf* el = batch->elem;
if (el->use_prim_restart) {
primitive_restart_enable(el);
}
#if GWN_TRACK_INDEX_RANGE
if (el->base_index) {
glDrawRangeElementsBaseVertex(batch->gl_prim_type,
el->min_index,
el->max_index,
v_count,
el->gl_index_type,
0,
el->base_index);
}
else {
glDrawRangeElements(batch->gl_prim_type, el->min_index, el->max_index, v_count, el->gl_index_type, 0);
}
#else
glDrawElements(batch->gl_prim_type, v_count, GL_UNSIGNED_INT, 0);
#endif
if (el->use_prim_restart) {
primitive_restart_disable();
}
}
else {
glDrawArrays(batch->gl_prim_type, v_first, v_count);
}
}
/* Performance hog if you are drawing with the same vao multiple time.
* Only activate for debugging. */
// glBindVertexArray(0);
}
/* just draw some vertices and let shader place them where we want. */
void GWN_draw_primitive(Gwn_PrimType prim_type, int v_count)
{
/* we cannot draw without vao ... annoying ... */
glBindVertexArray(GWN_vao_default());
GLenum type = convert_prim_type_to_gl(prim_type);
glDrawArrays(type, 0, v_count);
/* Performance hog if you are drawing with the same vao multiple time.
* Only activate for debugging.*/
// glBindVertexArray(0);
}
/* -------------------------------------------------------------------- */
/** \name Utilities
* \{ */
@@ -67,4 +674,4 @@ void gpu_batch_exit(void)
gpu_batch_presets_exit();
}
/** \} */
/** \} */

View File

@@ -37,12 +37,11 @@
extern "C" {
#endif
#include "gwn_batch.h"
#include "gwn_context.h"
#include "gwn_shader_interface.h"
#include "GPU_batch.h"
#include "GPU_context.h"
#include "GPU_shader_interface.h"
void gwn_batch_remove_interface_ref(Gwn_Batch*, const Gwn_ShaderInterface*);
void gwn_batch_vao_cache_clear(Gwn_Batch*);
void gwn_context_add_batch(Gwn_Context*, Gwn_Batch*);
void gwn_context_remove_batch(Gwn_Context*, Gwn_Batch*);

View File

@@ -29,7 +29,8 @@
* Gawain buffer IDs
*/
#include "gwn_buffer_id.h"
#include "GPU_buffer_id.h"
#include <mutex>
#include <vector>

View File

@@ -29,8 +29,9 @@
* Gawain element list (AKA index buffer)
*/
#include "gwn_element.h"
#include "gwn_buffer_id.h"
#include "GPU_element.h"
#include "GPU_buffer_id.h"
#include <stdlib.h>
#define KEEP_SINGLE_COPY 1

View File

@@ -15,22 +15,145 @@
* 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 Blender Foundation.
* The Original Code is Copyright (C) 2016 by Mike Erwin.
* All rights reserved.
*
* The Original Code is: all of this file.
*
* Contributor(s): Mike Erwin
* Contributor(s): Blender Foundation
*
* ***** END GPL LICENSE BLOCK *****
*/
#include "GPU_immediate.h"
#include "GPU_matrix.h"
#include "UI_resources.h"
#include "BLI_utildefines.h"
/** \file blender/gpu/intern/gwn_immediate.c
* \ingroup gpu
*
* Gawain immediate mode work-alike
*/
#include "UI_resources.h"
#include "GPU_attr_binding.h"
#include "GPU_buffer_id.h"
#include "GPU_immediate.h"
#include "GPU_vertex_array_id.h"
#include "gpu_attr_binding_private.h"
#include "gpu_primitive_private.h"
#include "gpu_shader_private.h"
#include "gpu_vertex_format_private.h"
#include <string.h>
#include <stdlib.h>
/* necessary functions from matrix API */
extern void GPU_matrix_bind(const Gwn_ShaderInterface*);
extern bool GPU_matrix_dirty_get(void);
typedef struct {
/* TODO: organize this struct by frequency of change (run-time) */
Gwn_Batch* batch;
Gwn_Context* context;
/* current draw call */
GLubyte* buffer_data;
uint buffer_offset;
uint buffer_bytes_mapped;
uint vertex_len;
bool strict_vertex_len;
Gwn_PrimType prim_type;
Gwn_VertFormat vertex_format;
/* current vertex */
uint vertex_idx;
GLubyte* vertex_data;
uint16_t unassigned_attrib_bits; /* which attributes of current vertex have not been given values? */
GLuint vbo_id;
GLuint vao_id;
GLuint bound_program;
const Gwn_ShaderInterface* shader_interface;
Gwn_AttrBinding attrib_binding;
uint16_t prev_enabled_attrib_bits; /* <-- only affects this VAO, so we're ok */
} Immediate;
/* size of internal buffer -- make this adjustable? */
#define IMM_BUFFER_SIZE (4 * 1024 * 1024)
static bool initialized = false;
static Immediate imm;
void immInit(void)
{
#if TRUST_NO_ONE
assert(!initialized);
#endif
memset(&imm, 0, sizeof(Immediate));
imm.vbo_id = GWN_buf_id_alloc();
glBindBuffer(GL_ARRAY_BUFFER, imm.vbo_id);
glBufferData(GL_ARRAY_BUFFER, IMM_BUFFER_SIZE, NULL, GL_DYNAMIC_DRAW);
imm.prim_type = GWN_PRIM_NONE;
imm.strict_vertex_len = true;
glBindBuffer(GL_ARRAY_BUFFER, 0);
initialized = true;
}
void immActivate(void)
{
#if TRUST_NO_ONE
assert(initialized);
assert(imm.prim_type == GWN_PRIM_NONE); /* make sure we're not between a Begin/End pair */
assert(imm.vao_id == 0);
#endif
imm.vao_id = GWN_vao_alloc();
imm.context = GWN_context_active_get();
}
void immDeactivate(void)
{
#if TRUST_NO_ONE
assert(initialized);
assert(imm.prim_type == GWN_PRIM_NONE); /* make sure we're not between a Begin/End pair */
assert(imm.vao_id != 0);
#endif
GWN_vao_free(imm.vao_id, imm.context);
imm.vao_id = 0;
imm.prev_enabled_attrib_bits = 0;
}
void immDestroy(void)
{
GWN_buf_id_free(imm.vbo_id);
initialized = false;
}
Gwn_VertFormat* immVertexFormat(void)
{
GWN_vertformat_clear(&imm.vertex_format);
return &imm.vertex_format;
}
void immBindProgram(GLuint program, const Gwn_ShaderInterface* shaderface)
{
#if TRUST_NO_ONE
assert(imm.bound_program == 0);
assert(glIsProgram(program));
#endif
imm.bound_program = program;
imm.shader_interface = shaderface;
if (!imm.vertex_format.packed)
VertexFormat_pack(&imm.vertex_format);
glUseProgram(program);
get_attrib_locations(&imm.vertex_format, &imm.attrib_binding, shaderface);
GPU_matrix_bind(shaderface);
}
void immBindBuiltinProgram(GPUBuiltinShader shader_id)
{
@@ -38,6 +161,718 @@ void immBindBuiltinProgram(GPUBuiltinShader shader_id)
immBindProgram(shader->program, shader->interface);
}
void immUnbindProgram(void)
{
#if TRUST_NO_ONE
assert(imm.bound_program != 0);
#endif
#if PROGRAM_NO_OPTI
glUseProgram(0);
#endif
imm.bound_program = 0;
}
#if TRUST_NO_ONE
static bool vertex_count_makes_sense_for_primitive(uint vertex_len, Gwn_PrimType prim_type)
{
/* does vertex_len make sense for this primitive type? */
if (vertex_len == 0) {
return false;
}
switch (prim_type) {
case GWN_PRIM_POINTS:
return true;
case GWN_PRIM_LINES:
return vertex_len % 2 == 0;
case GWN_PRIM_LINE_STRIP:
case GWN_PRIM_LINE_LOOP:
return vertex_len >= 2;
case GWN_PRIM_LINE_STRIP_ADJ:
return vertex_len >= 4;
case GWN_PRIM_TRIS:
return vertex_len % 3 == 0;
case GWN_PRIM_TRI_STRIP:
case GWN_PRIM_TRI_FAN:
return vertex_len >= 3;
default:
return false;
}
}
#endif
void immBegin(Gwn_PrimType prim_type, uint vertex_len)
{
#if TRUST_NO_ONE
assert(initialized);
assert(imm.prim_type == GWN_PRIM_NONE); /* make sure we haven't already begun */
assert(vertex_count_makes_sense_for_primitive(vertex_len, prim_type));
#endif
imm.prim_type = prim_type;
imm.vertex_len = vertex_len;
imm.vertex_idx = 0;
imm.unassigned_attrib_bits = imm.attrib_binding.enabled_bits;
/* how many bytes do we need for this draw call? */
const uint bytes_needed = vertex_buffer_size(&imm.vertex_format, vertex_len);
#if TRUST_NO_ONE
assert(bytes_needed <= IMM_BUFFER_SIZE);
#endif
glBindBuffer(GL_ARRAY_BUFFER, imm.vbo_id);
/* does the current buffer have enough room? */
const uint available_bytes = IMM_BUFFER_SIZE - imm.buffer_offset;
/* ensure vertex data is aligned */
const uint pre_padding = padding(imm.buffer_offset, imm.vertex_format.stride); /* might waste a little space, but it's safe */
if ((bytes_needed + pre_padding) <= available_bytes) {
imm.buffer_offset += pre_padding;
}
else {
/* orphan this buffer & start with a fresh one */
/* this method works on all platforms, old & new */
glBufferData(GL_ARRAY_BUFFER, IMM_BUFFER_SIZE, NULL, GL_DYNAMIC_DRAW);
imm.buffer_offset = 0;
}
/* printf("mapping %u to %u\n", imm.buffer_offset, imm.buffer_offset + bytes_needed - 1); */
imm.buffer_data = glMapBufferRange(GL_ARRAY_BUFFER, imm.buffer_offset, bytes_needed,
GL_MAP_WRITE_BIT | GL_MAP_UNSYNCHRONIZED_BIT | (imm.strict_vertex_len ? 0 : GL_MAP_FLUSH_EXPLICIT_BIT));
#if TRUST_NO_ONE
assert(imm.buffer_data != NULL);
#endif
imm.buffer_bytes_mapped = bytes_needed;
imm.vertex_data = imm.buffer_data;
}
void immBeginAtMost(Gwn_PrimType prim_type, uint vertex_len)
{
#if TRUST_NO_ONE
assert(vertex_len > 0);
#endif
imm.strict_vertex_len = false;
immBegin(prim_type, vertex_len);
}
Gwn_Batch* immBeginBatch(Gwn_PrimType prim_type, uint vertex_len)
{
#if TRUST_NO_ONE
assert(initialized);
assert(imm.prim_type == GWN_PRIM_NONE); /* make sure we haven't already begun */
assert(vertex_count_makes_sense_for_primitive(vertex_len, prim_type));
#endif
imm.prim_type = prim_type;
imm.vertex_len = vertex_len;
imm.vertex_idx = 0;
imm.unassigned_attrib_bits = imm.attrib_binding.enabled_bits;
Gwn_VertBuf* verts = GWN_vertbuf_create_with_format(&imm.vertex_format);
GWN_vertbuf_data_alloc(verts, vertex_len);
imm.buffer_bytes_mapped = GWN_vertbuf_size_get(verts);
imm.vertex_data = verts->data;
imm.batch = GWN_batch_create_ex(prim_type, verts, NULL, GWN_BATCH_OWNS_VBO);
imm.batch->phase = GWN_BATCH_BUILDING;
return imm.batch;
}
Gwn_Batch* immBeginBatchAtMost(Gwn_PrimType prim_type, uint vertex_len)
{
imm.strict_vertex_len = false;
return immBeginBatch(prim_type, vertex_len);
}
static void immDrawSetup(void)
{
/* set up VAO -- can be done during Begin or End really */
glBindVertexArray(imm.vao_id);
/* enable/disable vertex attribs as needed */
if (imm.attrib_binding.enabled_bits != imm.prev_enabled_attrib_bits) {
for (uint loc = 0; loc < GWN_VERT_ATTR_MAX_LEN; ++loc) {
bool is_enabled = imm.attrib_binding.enabled_bits & (1 << loc);
bool was_enabled = imm.prev_enabled_attrib_bits & (1 << loc);
if (is_enabled && !was_enabled) {
glEnableVertexAttribArray(loc);
}
else if (was_enabled && !is_enabled) {
glDisableVertexAttribArray(loc);
}
}
imm.prev_enabled_attrib_bits = imm.attrib_binding.enabled_bits;
}
const uint stride = imm.vertex_format.stride;
for (uint a_idx = 0; a_idx < imm.vertex_format.attr_len; ++a_idx) {
const Gwn_VertAttr* a = imm.vertex_format.attribs + a_idx;
const uint offset = imm.buffer_offset + a->offset;
const GLvoid* pointer = (const GLubyte*)0 + offset;
const uint loc = read_attrib_location(&imm.attrib_binding, a_idx);
switch (a->fetch_mode) {
case GWN_FETCH_FLOAT:
case GWN_FETCH_INT_TO_FLOAT:
glVertexAttribPointer(loc, a->comp_len, a->gl_comp_type, GL_FALSE, stride, pointer);
break;
case GWN_FETCH_INT_TO_FLOAT_UNIT:
glVertexAttribPointer(loc, a->comp_len, a->gl_comp_type, GL_TRUE, stride, pointer);
break;
case GWN_FETCH_INT:
glVertexAttribIPointer(loc, a->comp_len, a->gl_comp_type, stride, pointer);
}
}
if (GPU_matrix_dirty_get()) {
GPU_matrix_bind(imm.shader_interface);
}
}
void immEnd(void)
{
#if TRUST_NO_ONE
assert(imm.prim_type != GWN_PRIM_NONE); /* make sure we're between a Begin/End pair */
#endif
uint buffer_bytes_used;
if (imm.strict_vertex_len) {
#if TRUST_NO_ONE
assert(imm.vertex_idx == imm.vertex_len); /* with all vertices defined */
#endif
buffer_bytes_used = imm.buffer_bytes_mapped;
}
else {
#if TRUST_NO_ONE
assert(imm.vertex_idx <= imm.vertex_len);
#endif
if (imm.vertex_idx == imm.vertex_len) {
buffer_bytes_used = imm.buffer_bytes_mapped;
}
else {
#if TRUST_NO_ONE
assert(imm.vertex_idx == 0 || vertex_count_makes_sense_for_primitive(imm.vertex_idx, imm.prim_type));
#endif
imm.vertex_len = imm.vertex_idx;
buffer_bytes_used = vertex_buffer_size(&imm.vertex_format, imm.vertex_len);
/* unused buffer bytes are available to the next immBegin */
}
/* tell OpenGL what range was modified so it doesn't copy the whole mapped range */
glFlushMappedBufferRange(GL_ARRAY_BUFFER, 0, buffer_bytes_used);
}
if (imm.batch) {
if (buffer_bytes_used != imm.buffer_bytes_mapped) {
GWN_vertbuf_data_resize(imm.batch->verts[0], imm.vertex_len);
/* TODO: resize only if vertex count is much smaller */
}
GWN_batch_program_set(imm.batch, imm.bound_program, imm.shader_interface);
imm.batch->phase = GWN_BATCH_READY_TO_DRAW;
imm.batch = NULL; /* don't free, batch belongs to caller */
}
else {
glUnmapBuffer(GL_ARRAY_BUFFER);
if (imm.vertex_len > 0) {
immDrawSetup();
glDrawArrays(convert_prim_type_to_gl(imm.prim_type), 0, imm.vertex_len);
}
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
/* prep for next immBegin */
imm.buffer_offset += buffer_bytes_used;
}
/* prep for next immBegin */
imm.prim_type = GWN_PRIM_NONE;
imm.strict_vertex_len = true;
}
static void setAttribValueBit(uint attrib_id)
{
uint16_t mask = 1 << attrib_id;
#if TRUST_NO_ONE
assert(imm.unassigned_attrib_bits & mask); /* not already set */
#endif
imm.unassigned_attrib_bits &= ~mask;
}
/* --- generic attribute functions --- */
void immAttrib1f(uint attrib_id, float x)
{
Gwn_VertAttr* attrib = imm.vertex_format.attribs + attrib_id;
#if TRUST_NO_ONE
assert(attrib_id < imm.vertex_format.attr_len);
assert(attrib->comp_type == GWN_COMP_F32);
assert(attrib->comp_len == 1);
assert(imm.vertex_idx < imm.vertex_len);
assert(imm.prim_type != GWN_PRIM_NONE); /* make sure we're between a Begin/End pair */
#endif
setAttribValueBit(attrib_id);
float* data = (float*)(imm.vertex_data + attrib->offset);
/* printf("%s %td %p\n", __FUNCTION__, (GLubyte*)data - imm.buffer_data, data); */
data[0] = x;
}
void immAttrib2f(uint attrib_id, float x, float y)
{
Gwn_VertAttr* attrib = imm.vertex_format.attribs + attrib_id;
#if TRUST_NO_ONE
assert(attrib_id < imm.vertex_format.attr_len);
assert(attrib->comp_type == GWN_COMP_F32);
assert(attrib->comp_len == 2);
assert(imm.vertex_idx < imm.vertex_len);
assert(imm.prim_type != GWN_PRIM_NONE); /* make sure we're between a Begin/End pair */
#endif
setAttribValueBit(attrib_id);
float* data = (float*)(imm.vertex_data + attrib->offset);
/* printf("%s %td %p\n", __FUNCTION__, (GLubyte*)data - imm.buffer_data, data); */
data[0] = x;
data[1] = y;
}
void immAttrib3f(uint attrib_id, float x, float y, float z)
{
Gwn_VertAttr* attrib = imm.vertex_format.attribs + attrib_id;
#if TRUST_NO_ONE
assert(attrib_id < imm.vertex_format.attr_len);
assert(attrib->comp_type == GWN_COMP_F32);
assert(attrib->comp_len == 3);
assert(imm.vertex_idx < imm.vertex_len);
assert(imm.prim_type != GWN_PRIM_NONE); /* make sure we're between a Begin/End pair */
#endif
setAttribValueBit(attrib_id);
float* data = (float*)(imm.vertex_data + attrib->offset);
/* printf("%s %td %p\n", __FUNCTION__, (GLubyte*)data - imm.buffer_data, data); */
data[0] = x;
data[1] = y;
data[2] = z;
}
void immAttrib4f(uint attrib_id, float x, float y, float z, float w)
{
Gwn_VertAttr* attrib = imm.vertex_format.attribs + attrib_id;
#if TRUST_NO_ONE
assert(attrib_id < imm.vertex_format.attr_len);
assert(attrib->comp_type == GWN_COMP_F32);
assert(attrib->comp_len == 4);
assert(imm.vertex_idx < imm.vertex_len);
assert(imm.prim_type != GWN_PRIM_NONE); /* make sure we're between a Begin/End pair */
#endif
setAttribValueBit(attrib_id);
float* data = (float*)(imm.vertex_data + attrib->offset);
/* printf("%s %td %p\n", __FUNCTION__, (GLubyte*)data - imm.buffer_data, data); */
data[0] = x;
data[1] = y;
data[2] = z;
data[3] = w;
}
void immAttrib1u(uint attrib_id, uint x)
{
Gwn_VertAttr* attrib = imm.vertex_format.attribs + attrib_id;
#if TRUST_NO_ONE
assert(attrib_id < imm.vertex_format.attr_len);
assert(attrib->comp_type == GWN_COMP_U32);
assert(attrib->comp_len == 1);
assert(imm.vertex_idx < imm.vertex_len);
assert(imm.prim_type != GWN_PRIM_NONE); /* make sure we're between a Begin/End pair */
#endif
setAttribValueBit(attrib_id);
uint* data = (uint*)(imm.vertex_data + attrib->offset);
data[0] = x;
}
void immAttrib2i(uint attrib_id, int x, int y)
{
Gwn_VertAttr* attrib = imm.vertex_format.attribs + attrib_id;
#if TRUST_NO_ONE
assert(attrib_id < imm.vertex_format.attr_len);
assert(attrib->comp_type == GWN_COMP_I32);
assert(attrib->comp_len == 2);
assert(imm.vertex_idx < imm.vertex_len);
assert(imm.prim_type != GWN_PRIM_NONE); /* make sure we're between a Begin/End pair */
#endif
setAttribValueBit(attrib_id);
int* data = (int*)(imm.vertex_data + attrib->offset);
data[0] = x;
data[1] = y;
}
void immAttrib2s(uint attrib_id, short x, short y)
{
Gwn_VertAttr* attrib = imm.vertex_format.attribs + attrib_id;
#if TRUST_NO_ONE
assert(attrib_id < imm.vertex_format.attr_len);
assert(attrib->comp_type == GWN_COMP_I16);
assert(attrib->comp_len == 2);
assert(imm.vertex_idx < imm.vertex_len);
assert(imm.prim_type != GWN_PRIM_NONE); /* make sure we're between a Begin/End pair */
#endif
setAttribValueBit(attrib_id);
short* data = (short*)(imm.vertex_data + attrib->offset);
data[0] = x;
data[1] = y;
}
void immAttrib2fv(uint attrib_id, const float data[2])
{
immAttrib2f(attrib_id, data[0], data[1]);
}
void immAttrib3fv(uint attrib_id, const float data[3])
{
immAttrib3f(attrib_id, data[0], data[1], data[2]);
}
void immAttrib4fv(uint attrib_id, const float data[4])
{
immAttrib4f(attrib_id, data[0], data[1], data[2], data[3]);
}
void immAttrib3ub(uint attrib_id, unsigned char r, unsigned char g, unsigned char b)
{
Gwn_VertAttr* attrib = imm.vertex_format.attribs + attrib_id;
#if TRUST_NO_ONE
assert(attrib_id < imm.vertex_format.attr_len);
assert(attrib->comp_type == GWN_COMP_U8);
assert(attrib->comp_len == 3);
assert(imm.vertex_idx < imm.vertex_len);
assert(imm.prim_type != GWN_PRIM_NONE); /* make sure we're between a Begin/End pair */
#endif
setAttribValueBit(attrib_id);
GLubyte* data = imm.vertex_data + attrib->offset;
/* printf("%s %td %p\n", __FUNCTION__, data - imm.buffer_data, data); */
data[0] = r;
data[1] = g;
data[2] = b;
}
void immAttrib4ub(uint attrib_id, unsigned char r, unsigned char g, unsigned char b, unsigned char a)
{
Gwn_VertAttr* attrib = imm.vertex_format.attribs + attrib_id;
#if TRUST_NO_ONE
assert(attrib_id < imm.vertex_format.attr_len);
assert(attrib->comp_type == GWN_COMP_U8);
assert(attrib->comp_len == 4);
assert(imm.vertex_idx < imm.vertex_len);
assert(imm.prim_type != GWN_PRIM_NONE); /* make sure we're between a Begin/End pair */
#endif
setAttribValueBit(attrib_id);
GLubyte* data = imm.vertex_data + attrib->offset;
/* printf("%s %td %p\n", __FUNCTION__, data - imm.buffer_data, data); */
data[0] = r;
data[1] = g;
data[2] = b;
data[3] = a;
}
void immAttrib3ubv(uint attrib_id, const unsigned char data[3])
{
immAttrib3ub(attrib_id, data[0], data[1], data[2]);
}
void immAttrib4ubv(uint attrib_id, const unsigned char data[4])
{
immAttrib4ub(attrib_id, data[0], data[1], data[2], data[3]);
}
void immSkipAttrib(uint attrib_id)
{
#if TRUST_NO_ONE
assert(attrib_id < imm.vertex_format.attr_len);
assert(imm.vertex_idx < imm.vertex_len);
assert(imm.prim_type != GWN_PRIM_NONE); /* make sure we're between a Begin/End pair */
#endif
setAttribValueBit(attrib_id);
}
static void immEndVertex(void) /* and move on to the next vertex */
{
#if TRUST_NO_ONE
assert(imm.prim_type != GWN_PRIM_NONE); /* make sure we're between a Begin/End pair */
assert(imm.vertex_idx < imm.vertex_len);
#endif
/* have all attribs been assigned values?
* if not, copy value from previous vertex */
if (imm.unassigned_attrib_bits) {
#if TRUST_NO_ONE
assert(imm.vertex_idx > 0); /* first vertex must have all attribs specified */
#endif
for (uint a_idx = 0; a_idx < imm.vertex_format.attr_len; ++a_idx) {
if ((imm.unassigned_attrib_bits >> a_idx) & 1) {
const Gwn_VertAttr* a = imm.vertex_format.attribs + a_idx;
/* printf("copying %s from vertex %u to %u\n", a->name, imm.vertex_idx - 1, imm.vertex_idx); */
GLubyte* data = imm.vertex_data + a->offset;
memcpy(data, data - imm.vertex_format.stride, a->sz);
/* TODO: consolidate copy of adjacent attributes */
}
}
}
imm.vertex_idx++;
imm.vertex_data += imm.vertex_format.stride;
imm.unassigned_attrib_bits = imm.attrib_binding.enabled_bits;
}
void immVertex2f(uint attrib_id, float x, float y)
{
immAttrib2f(attrib_id, x, y);
immEndVertex();
}
void immVertex3f(uint attrib_id, float x, float y, float z)
{
immAttrib3f(attrib_id, x, y, z);
immEndVertex();
}
void immVertex4f(uint attrib_id, float x, float y, float z, float w)
{
immAttrib4f(attrib_id, x, y, z, w);
immEndVertex();
}
void immVertex2i(uint attrib_id, int x, int y)
{
immAttrib2i(attrib_id, x, y);
immEndVertex();
}
void immVertex2s(uint attrib_id, short x, short y)
{
immAttrib2s(attrib_id, x, y);
immEndVertex();
}
void immVertex2fv(uint attrib_id, const float data[2])
{
immAttrib2f(attrib_id, data[0], data[1]);
immEndVertex();
}
void immVertex3fv(uint attrib_id, const float data[3])
{
immAttrib3f(attrib_id, data[0], data[1], data[2]);
immEndVertex();
}
void immVertex2iv(uint attrib_id, const int data[2])
{
immAttrib2i(attrib_id, data[0], data[1]);
immEndVertex();
}
/* --- generic uniform functions --- */
#if 0
#if TRUST_NO_ONE
#define GET_UNIFORM const Gwn_ShaderInput* uniform = GWN_shaderinterface_uniform(imm.shader_interface, name); assert(uniform);
#else
#define GET_UNIFORM const Gwn_ShaderInput* uniform = GWN_shaderinterface_uniform(imm.shader_interface, name);
#endif
#else
/* NOTE: It is possible to have uniform fully optimized out from the shader.
* In this case we can't assert failure or allow NULL-pointer dereference.
* TODO(sergey): How can we detect existing-but-optimized-out uniform but still
* catch typos in uniform names passed to immUniform*() functions? */
#define GET_UNIFORM const Gwn_ShaderInput* uniform = GWN_shaderinterface_uniform(imm.shader_interface, name); if (uniform == NULL) return;
#endif
void immUniform1f(const char* name, float x)
{
GET_UNIFORM
glUniform1f(uniform->location, x);
}
void immUniform2f(const char* name, float x, float y)
{
GET_UNIFORM
glUniform2f(uniform->location, x, y);
}
void immUniform2fv(const char* name, const float data[2])
{
GET_UNIFORM
glUniform2fv(uniform->location, 1, data);
}
void immUniform3f(const char* name, float x, float y, float z)
{
GET_UNIFORM
glUniform3f(uniform->location, x, y, z);
}
void immUniform3fv(const char* name, const float data[3])
{
GET_UNIFORM
glUniform3fv(uniform->location, 1, data);
}
/* can increase this limit or move to another file */
#define MAX_UNIFORM_NAME_LEN 60
void immUniformArray3fv(const char* bare_name, const float *data, int count)
{
/* look up "name[0]" when given "name" */
const size_t len = strlen(bare_name);
#if TRUST_NO_ONE
assert(len <= MAX_UNIFORM_NAME_LEN);
#endif
char name[MAX_UNIFORM_NAME_LEN];
strcpy(name, bare_name);
name[len + 0] = '[';
name[len + 1] = '0';
name[len + 2] = ']';
name[len + 3] = '\0';
GET_UNIFORM
glUniform3fv(uniform->location, count, data);
}
void immUniform4f(const char* name, float x, float y, float z, float w)
{
GET_UNIFORM
glUniform4f(uniform->location, x, y, z, w);
}
void immUniform4fv(const char* name, const float data[4])
{
GET_UNIFORM
glUniform4fv(uniform->location, 1, data);
}
void immUniformArray4fv(const char* bare_name, const float *data, int count)
{
/* look up "name[0]" when given "name" */
const size_t len = strlen(bare_name);
#if TRUST_NO_ONE
assert(len <= MAX_UNIFORM_NAME_LEN);
#endif
char name[MAX_UNIFORM_NAME_LEN];
strcpy(name, bare_name);
name[len + 0] = '[';
name[len + 1] = '0';
name[len + 2] = ']';
name[len + 3] = '\0';
GET_UNIFORM
glUniform4fv(uniform->location, count, data);
}
void immUniformMatrix4fv(const char* name, const float data[4][4])
{
GET_UNIFORM
glUniformMatrix4fv(uniform->location, 1, GL_FALSE, (float *)data);
}
void immUniform1i(const char* name, int x)
{
GET_UNIFORM
glUniform1i(uniform->location, x);
}
void immUniform4iv(const char* name, const int data[4])
{
GET_UNIFORM
glUniform4iv(uniform->location, 1, data);
}
/* --- convenience functions for setting "uniform vec4 color" --- */
void immUniformColor4f(float r, float g, float b, float a)
{
const Gwn_ShaderInput* uniform = GWN_shaderinterface_uniform_builtin(imm.shader_interface, GWN_UNIFORM_COLOR);
#if TRUST_NO_ONE
assert(uniform != NULL);
#endif
glUniform4f(uniform->location, r, g, b, a);
}
void immUniformColor4fv(const float rgba[4])
{
immUniformColor4f(rgba[0], rgba[1], rgba[2], rgba[3]);
}
void immUniformColor3f(float r, float g, float b)
{
immUniformColor4f(r, g, b, 1.0f);
}
void immUniformColor3fv(const float rgb[3])
{
immUniformColor4f(rgb[0], rgb[1], rgb[2], 1.0f);
}
void immUniformColor3fvAlpha(const float rgb[3], float a)
{
immUniformColor4f(rgb[0], rgb[1], rgb[2], a);
}
/* TODO: v-- treat as sRGB? --v */
void immUniformColor3ub(unsigned char r, unsigned char g, unsigned char b)
{
const float scale = 1.0f / 255.0f;
immUniformColor4f(scale * r, scale * g, scale * b, 1.0f);
}
void immUniformColor4ub(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
{
const float scale = 1.0f / 255.0f;
immUniformColor4f(scale * r, scale * g, scale * b, scale * a);
}
void immUniformColor3ubv(const unsigned char rgb[3])
{
immUniformColor3ub(rgb[0], rgb[1], rgb[2]);
}
void immUniformColor3ubvAlpha(const unsigned char rgb[3], unsigned char alpha)
{
immUniformColor4ub(rgb[0], rgb[1], rgb[2], alpha);
}
void immUniformColor4ubv(const unsigned char rgba[4])
{
immUniformColor4ub(rgba[0], rgba[1], rgba[2], rgba[3]);
}
void immUniformThemeColor(int color_id)
{
float color[4];

View File

@@ -18,8 +18,10 @@
* ***** END GPL LICENSE BLOCK *****
*/
/** \file source/blender/gpu/intern/gpu_immediate_util.c
/** \file blender/gpu/intern/gwn_imm_util.c
* \ingroup gpu
*
* Gawain immediate mode drawing utilities
*/
#include <stdio.h>
@@ -66,6 +68,72 @@ static const int cube_line_index[12][2] = {
{6, 7},
};
void immRectf(uint pos, float x1, float y1, float x2, float y2)
{
immBegin(GWN_PRIM_TRI_FAN, 4);
immVertex2f(pos, x1, y1);
immVertex2f(pos, x2, y1);
immVertex2f(pos, x2, y2);
immVertex2f(pos, x1, y2);
immEnd();
}
void immRecti(uint pos, int x1, int y1, int x2, int y2)
{
immBegin(GWN_PRIM_TRI_FAN, 4);
immVertex2i(pos, x1, y1);
immVertex2i(pos, x2, y1);
immVertex2i(pos, x2, y2);
immVertex2i(pos, x1, y2);
immEnd();
}
void immRectf_fast_with_color(uint pos, uint col, float x1, float y1, float x2, float y2, const float color[4])
{
immAttrib4fv(col, color);
immVertex2f(pos, x1, y1);
immAttrib4fv(col, color);
immVertex2f(pos, x2, y1);
immAttrib4fv(col, color);
immVertex2f(pos, x2, y2);
immAttrib4fv(col, color);
immVertex2f(pos, x1, y1);
immAttrib4fv(col, color);
immVertex2f(pos, x2, y2);
immAttrib4fv(col, color);
immVertex2f(pos, x1, y2);
}
void immRecti_fast_with_color(uint pos, uint col, int x1, int y1, int x2, int y2, const float color[4])
{
immAttrib4fv(col, color);
immVertex2i(pos, x1, y1);
immAttrib4fv(col, color);
immVertex2i(pos, x2, y1);
immAttrib4fv(col, color);
immVertex2i(pos, x2, y2);
immAttrib4fv(col, color);
immVertex2i(pos, x1, y1);
immAttrib4fv(col, color);
immVertex2i(pos, x2, y2);
immAttrib4fv(col, color);
immVertex2i(pos, x1, y2);
}
#if 0 /* more complete version in case we want that */
void immRecti_complete(int x1, int y1, int x2, int y2, const float color[4])
{
Gwn_VertFormat *format = immVertexFormat();
uint pos = add_attrib(format, "pos", GWN_COMP_I32, 2, GWN_FETCH_INT_TO_FLOAT);
immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
immUniformColor4fv(color);
immRecti(pos, x1, y1, x2, y2);
immUnbindProgram();
}
#endif
/**
* Pack color into 3 bytes
*

View File

@@ -29,7 +29,7 @@
* \ingroup gpu
*/
#include "../../../intern/gawain/gawain/gwn_shader_interface.h"
#include "GPU_shader_interface.h"
#define SUPPRESS_GENERIC_MATRIX_API
#define USE_GPU_PY_MATRIX_API /* only so values are declared */

View File

@@ -29,8 +29,8 @@
* Gawain geometric primitives
*/
#include "gwn_primitive.h"
#include "gwn_primitive_private.h"
#include "GPU_primitive.h"
#include "gpu_primitive_private.h"
Gwn_PrimClass GWN_primtype_class(Gwn_PrimType prim_type)
{

View File

@@ -29,9 +29,9 @@
* Gawain shader interface (C --> GLSL)
*/
#include "gwn_batch_private.h"
#include "gwn_shader_interface.h"
#include "gwn_vertex_array_id.h"
#include "gpu_batch_private.h"
#include "GPU_shader_interface.h"
#include "GPU_vertex_array_id.h"
#include <stdlib.h>
#include <stddef.h>
#include <string.h>

View File

@@ -26,7 +26,7 @@
#define __GPU_SHADER_PRIVATE_H__
#include "GPU_glew.h"
#include "gawain/gwn_shader_interface.h"
#include "GPU_shader_interface.h"
struct GPUShader {
GLuint program; /* handle for full program (links shader stages below) */

View File

@@ -34,9 +34,9 @@
* - free can be called from any thread
*/
#include "gwn_batch_private.h"
#include "gwn_vertex_array_id.h"
#include "gwn_context.h"
#include "gpu_batch_private.h"
#include "GPU_vertex_array_id.h"
#include "GPU_context.h"
#include <vector>
#include <string.h>
#include <pthread.h>

View File

@@ -29,9 +29,9 @@
* Gawain vertex buffer
*/
#include "gwn_vertex_buffer.h"
#include "gwn_buffer_id.h"
#include "gwn_vertex_format_private.h"
#include "GPU_vertex_buffer.h"
#include "GPU_buffer_id.h"
#include "gpu_vertex_format_private.h"
#include <stdlib.h>
#include <string.h>

View File

@@ -29,8 +29,8 @@
* Gawain vertex format
*/
#include "gwn_vertex_format.h"
#include "gwn_vertex_format_private.h"
#include "GPU_vertex_format.h"
#include "gpu_vertex_format_private.h"
#include <stddef.h>
#include <string.h>

View File

@@ -27,8 +27,8 @@
#include <Python.h>
#include "gawain/gwn_batch.h"
#include "gawain/gwn_vertex_format.h"
#include "GPU_batch.h"
#include "GPU_vertex_format.h"
#include "gwn_py_api.h"
#include "gwn_py_types.h"

View File

@@ -27,8 +27,8 @@
#include <Python.h>
#include "gawain/gwn_batch.h"
#include "gawain/gwn_vertex_format.h"
#include "GPU_batch.h"
#include "GPU_vertex_format.h"
#include "BLI_math.h"

View File

@@ -37,6 +37,7 @@ set(INC
../nodes
../physics
../draw
../gpu
../../../intern/atomic
../../../intern/guardedalloc
../../../intern/mikktspace

View File

@@ -97,7 +97,7 @@
#include "../../../windowmanager/WM_api.h" /* XXX */
#include "../../../windowmanager/wm_window.h" /* XXX */
#include "../../../intern/gawain/gawain/gwn_context.h"
#include "GPU_context.h"
#ifdef WITH_FREESTYLE
# include "FRS_freestyle.h"

View File

@@ -69,7 +69,7 @@
#include "GPU_matrix.h"
#include "GPU_immediate.h"
#include "GPU_immediate_util.h"
#include "GPU_batch.h"
#include "GPU_context.h"
#include "GPU_init_exit.h"
#include "DNA_scene_types.h"

View File

@@ -90,12 +90,11 @@
#include "GPU_immediate.h"
#include "GPU_material.h"
#include "GPU_texture.h"
#include "GPU_context.h"
#include "BLF_api.h"
#include "UI_resources.h"
#include "../../../intern/gawain/gawain/gwn_context.h"
/* for assert */
#ifndef NDEBUG
# include "BLI_threads.h"