Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCampbell Barton <ideasman42@gmail.com>2018-01-15 12:38:06 +0300
committerCampbell Barton <ideasman42@gmail.com>2018-01-15 12:38:06 +0300
commitbfada6cc4a22ba2c8c7ad504bc5133b9bf9ae890 (patch)
tree7bab8edfce8f928de689ba97c256fcaf299d66be /source/blender
parentae6cc4a21db52cfaca5898a4f98a6eb8def2e65a (diff)
GPU_batch: Add utility to create 2D shapes
Shapes are represented by compact byte array, encoded by 'make_shape_2d_from_blend.py' in the tools repo.
Diffstat (limited to 'source/blender')
-rw-r--r--source/blender/gpu/GPU_batch.h3
-rw-r--r--source/blender/gpu/intern/gpu_batch.c101
2 files changed, 103 insertions, 1 deletions
diff --git a/source/blender/gpu/GPU_batch.h b/source/blender/gpu/GPU_batch.h
index a6aa529fd2d..8b2dc2af355 100644
--- a/source/blender/gpu/GPU_batch.h
+++ b/source/blender/gpu/GPU_batch.h
@@ -43,6 +43,9 @@
/* gpu_batch.c */
void GWN_batch_program_set_builtin(Gwn_Batch *, GPUBuiltinShader);
+Gwn_Batch *GPU_batch_from_poly_2d_encoded(
+ const uchar *polys_flat, uint polys_flat_len, float min, float max);
+
void gpu_batch_init(void);
void gpu_batch_exit(void);
diff --git a/source/blender/gpu/intern/gpu_batch.c b/source/blender/gpu/intern/gpu_batch.c
index fcc7110e0aa..c74f367c447 100644
--- a/source/blender/gpu/intern/gpu_batch.c
+++ b/source/blender/gpu/intern/gpu_batch.c
@@ -29,7 +29,12 @@
* \ingroup gpu
*/
+#include "MEM_guardedalloc.h"
+
#include "BLI_utildefines.h"
+#include "BLI_math.h"
+#include "BLI_polyfill2d.h"
+
#include "GPU_batch.h" /* own include */
#include "gpu_shader_private.h"
@@ -44,6 +49,101 @@ void GWN_batch_program_set_builtin(Gwn_Batch *batch, GPUBuiltinShader shader_id)
GWN_batch_program_set(batch, shader->program, shader->interface);
}
+/** \} */
+
+
+
+/* -------------------------------------------------------------------- */
+/** \name Batch Creation
+ * \{ */
+
+Gwn_Batch *GPU_batch_from_poly_2d_encoded(
+ const uchar *polys_flat, uint polys_flat_len, float min, float max)
+{
+ uchar (*polys)[2] = (void *)polys_flat;
+ uint polys_len = polys_flat_len / 2;
+
+ /* Over alloc in both cases */
+ float (*verts)[2] = MEM_mallocN(sizeof(*verts) * polys_len, __func__);
+ float (*verts_step)[2] = verts;
+ uint (*tris)[3] = MEM_mallocN(sizeof(*tris) * polys_len, __func__);
+ uint (*tris_step)[3] = tris;
+ const float range_uchar = (max - min) / 255.0f;
+
+ uint i_poly = 0;
+ uint i_vert = 0;
+ while (i_poly != polys_len) {
+ for (uint j = 0; j < 2; j++) {
+ verts[i_vert][j] = min + ((float)polys[i_poly][j] * range_uchar);
+ }
+ i_vert++;
+ i_poly++;
+ if (polys[i_poly - 1][0] == polys[i_poly][0] &&
+ polys[i_poly - 1][1] == polys[i_poly][1])
+ {
+ const uint verts_len = (&verts[i_vert]) - verts_step;
+ BLI_assert(verts_len >= 3);
+ const uint tris_len = (verts_len - 2);
+ BLI_polyfill_calc(verts_step, verts_len, 0, tris_step);
+ /* offset indices */
+ if (verts_step != verts) {
+ uint *t = tris_step[0];
+ const uint offset = (verts_step - verts);
+ uint tot = tris_len * 3;
+ while (tot--) {
+ *t += offset;
+ t++;
+ }
+ BLI_assert(t == tris_step[tris_len]);
+ }
+ verts_step += verts_len;
+ tris_step += (verts_len - 2);
+ i_poly++;
+ /* ignore the duplicate point */
+ }
+ }
+
+ /* We have vertices and tris, make a batch from this. */
+ static Gwn_VertFormat format = {0};
+ static struct { uint pos; } attr_id;
+ if (format.attrib_ct == 0) {
+ attr_id.pos = GWN_vertformat_attr_add(&format, "pos", GWN_COMP_F32, 2, GWN_FETCH_FLOAT);
+ }
+
+ const uint verts_len = (verts_step - verts);
+ const uint tris_len = (tris_step - tris);
+ Gwn_VertBuf *vbo = GWN_vertbuf_create_with_format(&format);
+ GWN_vertbuf_data_alloc(vbo, verts_len);
+
+ Gwn_VertBufRaw pos_step;
+ GWN_vertbuf_attr_get_raw_data(vbo, attr_id.pos, &pos_step);
+
+ for (uint i = 0; i < verts_len; i++) {
+ copy_v2_v2(GWN_vertbuf_raw_step(&pos_step), verts[i]);
+ }
+
+ Gwn_IndexBufBuilder elb;
+ GWN_indexbuf_init(&elb, GWN_PRIM_TRIS, tris_len, verts_len);
+ for (uint i = 0; i < tris_len; i++) {
+ GWN_indexbuf_add_tri_verts(&elb, UNPACK3(tris[i]));
+ }
+ Gwn_IndexBuf *indexbuf = GWN_indexbuf_build(&elb);
+
+ MEM_freeN(tris);
+ MEM_freeN(verts);
+
+ return GWN_batch_create_ex(
+ GWN_PRIM_TRIS, vbo,
+ indexbuf,
+ GWN_BATCH_OWNS_VBO | GWN_BATCH_OWNS_INDEX);
+}
+
+/** \} */
+
+/* -------------------------------------------------------------------- */
+/** \name Init/Exit
+ * \{ */
+
void gpu_batch_init(void)
{
gpu_batch_presets_init();
@@ -55,4 +155,3 @@ void gpu_batch_exit(void)
}
/** \} */
-