Ver Fonte

libobs: Add option to duplicate vertex/index buffer data

Prevents from having to pass ownership of buffer data from caller when
using gs_vertexbuffer_create() or gs_indexbuffer_create() (which is a
design flaw).
jp9000 há 8 anos atrás
pai
commit
a46f0b4808
2 ficheiros alterados com 49 adições e 0 exclusões
  1. 47 0
      libobs/graphics/graphics.c
  2. 2 0
      libobs/graphics/graphics.h

+ 47 - 0
libobs/graphics/graphics.c

@@ -1468,6 +1468,46 @@ gs_vertbuffer_t *gs_vertexbuffer_create(struct gs_vb_data *data,
 	if (!gs_valid("gs_vertexbuffer_create"))
 		return NULL;
 
+	if (data && data->num && (flags & GS_DUP_BUFFER) != 0) {
+		struct gs_vb_data *new_data = gs_vbdata_create();
+
+		new_data->num = data->num;
+
+#define DUP_VAL(val) \
+		do { \
+			if (data->val) \
+				new_data->val = bmemdup(data->val, \
+						sizeof(*data->val) * \
+						data->num); \
+		} while (false)
+
+		DUP_VAL(points);
+		DUP_VAL(normals);
+		DUP_VAL(tangents);
+		DUP_VAL(colors);
+#undef DUP_VAL
+
+		if (data->tvarray && data->num_tex) {
+			new_data->num_tex = data->num_tex;
+			new_data->tvarray = bzalloc(
+					sizeof(struct gs_tvertarray) *
+					data->num_tex);
+
+			for (size_t i = 0; i < data->num_tex; i++) {
+				struct gs_tvertarray *tv = &data->tvarray[i];
+				struct gs_tvertarray *new_tv =
+					&new_data->tvarray[i];
+				size_t size = tv->width * sizeof(float);
+
+				new_tv->width = tv->width;
+				new_tv->array = bmemdup(tv->array,
+						size * data->num);
+			}
+		}
+
+		data = new_data;
+	}
+
 	return graphics->exports.device_vertexbuffer_create(graphics->device,
 			data, flags);
 }
@@ -1480,6 +1520,13 @@ gs_indexbuffer_t *gs_indexbuffer_create(enum gs_index_type type,
 	if (!gs_valid("gs_indexbuffer_create"))
 		return NULL;
 
+	if (indices && num && (flags & GS_DUP_BUFFER) != 0) {
+		size_t size = type == GS_UNSIGNED_SHORT
+			? sizeof(unsigned short)
+			: sizeof(unsigned long);
+		indices = bmemdup(indices, size * num);
+	}
+
 	return graphics->exports.device_indexbuffer_create(graphics->device,
 			type, indices, num, flags);
 }

+ 2 - 0
libobs/graphics/graphics.h

@@ -419,6 +419,8 @@ EXPORT gs_texture_t *gs_texrender_get_texture(const gs_texrender_t *texrender);
 #define GS_DYNAMIC       (1<<1)
 #define GS_RENDER_TARGET (1<<2)
 #define GS_GL_DUMMYTEX   (1<<3) /**<< texture with no allocated texture data */
+#define GS_DUP_BUFFER    (1<<4) /**<< do not pass buffer ownership when
+				 *    creating a vertex/index buffer */
 
 /* ---------------- */
 /* global functions */