GPU_batch_from_poly_2d_encoded: optional rctf arg

Also use compiler attributes
This commit is contained in:
2018-01-16 00:06:39 +11:00
parent 5cb43b0f20
commit 7ba07b7e64
3 changed files with 27 additions and 8 deletions

View File

@@ -114,7 +114,7 @@ static void button2d_draw_intern(
/* We shouldn't need the +1, but a NULL char is set. */
char *polys = MEM_mallocN(polys_len + 1, __func__);
RNA_property_string_get(mpr->ptr, prop, polys);
button->shape_batch = GPU_batch_from_poly_2d_encoded((uchar *)polys, polys_len, -1.0f, 1.0f);
button->shape_batch = GPU_batch_from_poly_2d_encoded((uchar *)polys, polys_len, NULL);
MEM_freeN(polys);
}
}

View File

@@ -33,26 +33,31 @@
#include "../../../intern/gawain/gawain/gwn_batch.h"
struct rctf;
// TODO: CMake magic to do this:
// #include "gawain/batch.h"
#include "BLI_compiler_attrs.h"
#include "GPU_shader.h"
/* Extend GWN_batch_program_set to use Blenders library of built-in shader programs. */
/* gpu_batch.c */
void GWN_batch_program_set_builtin(Gwn_Batch *, GPUBuiltinShader);
void GWN_batch_program_set_builtin(Gwn_Batch *batch, GPUBuiltinShader shader_id) ATTR_NONNULL(1);
Gwn_Batch *GPU_batch_from_poly_2d_encoded(
const uchar *polys_flat, uint polys_flat_len, float min, float max);
const uchar *polys_flat, uint polys_flat_len, const struct rctf *rect
) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1);
void gpu_batch_init(void);
void gpu_batch_exit(void);
/* gpu_batch_presets.c */
/* Replacement for gluSphere */
Gwn_Batch *GPU_batch_preset_sphere(int lod);
Gwn_Batch *GPU_batch_preset_sphere_wire(int lod);
Gwn_Batch *GPU_batch_preset_sphere(int lod) ATTR_WARN_UNUSED_RESULT;
Gwn_Batch *GPU_batch_preset_sphere_wire(int lod) ATTR_WARN_UNUSED_RESULT;
void gpu_batch_presets_init(void);
void gpu_batch_presets_exit(void);

View File

@@ -32,6 +32,7 @@
#include "MEM_guardedalloc.h"
#include "BLI_utildefines.h"
#include "BLI_rect.h"
#include "BLI_math.h"
#include "BLI_polyfill2d.h"
@@ -61,25 +62,38 @@ void GWN_batch_program_set_builtin(Gwn_Batch *batch, GPUBuiltinShader shader_id)
* Creates triangles from a byte-array of polygons.
*
* See 'make_shape_2d_from_blend.py' utility to create data to pass to this function.
*
* \param polys_flat: Pairs of X, Y coordinates (repeating to signify closing the polygon).
* \param polys_flat_len: Length of the array (must be an even number).
* \param rect: Optional region to map the byte 0..255 coords to. When not set use -1..1.
*/
Gwn_Batch *GPU_batch_from_poly_2d_encoded(
const uchar *polys_flat, uint polys_flat_len, float min, float max)
const uchar *polys_flat, uint polys_flat_len, const rctf *rect)
{
uchar (*polys)[2] = (void *)polys_flat;
uint polys_len = polys_flat_len / 2;
BLI_assert(polys_flat_len == polys_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;
const float range_uchar[2] = {
(rect ? (rect->xmax - rect->xmin) : 2.0f) / 255.0f,
(rect ? (rect->ymax - rect->ymin) : 2.0f) / 255.0f,
};
const float min_uchar[2] = {
(rect ? rect->xmin : -1.0f),
(rect ? rect->ymin : -1.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);
verts[i_vert][j] = min_uchar[j] + ((float)polys[i_poly][j] * range_uchar[j]);
}
i_vert++;
i_poly++;