[Bf-blender-cvs] [1cdfd0c] cycles-ptex-24: Add some Ptex code to BKE

Nicholas Bishop noreply at git.blender.org
Fri Jan 30 18:00:11 CET 2015


Commit: 1cdfd0c9bc92109d1506b938bb87ca144ef38e1b
Author: Nicholas Bishop
Date:   Sun Jan 25 22:31:50 2015 +0100
Branches: cycles-ptex-24
https://developer.blender.org/rB1cdfd0c9bc92109d1506b938bb87ca144ef38e1b

Add some Ptex code to BKE

===================================================================

A	source/blender/blenkernel/BKE_ptex.h
M	source/blender/blenkernel/CMakeLists.txt
A	source/blender/blenkernel/intern/bke_ptex.c

===================================================================

diff --git a/source/blender/blenkernel/BKE_ptex.h b/source/blender/blenkernel/BKE_ptex.h
new file mode 100644
index 0000000..1193337
--- /dev/null
+++ b/source/blender/blenkernel/BKE_ptex.h
@@ -0,0 +1,76 @@
+/*
+ * ***** 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.
+ *
+ */
+
+#ifndef __BKE_PTEX_H__
+#define __BKE_PTEX_H__
+
+#include "DNA_customdata_types.h"
+#include "DNA_meshdata_types.h"
+
+struct DerivedMesh;
+struct Image;
+struct Object;
+
+void BKE_loop_ptex_init(MLoopPtex *loop_ptex,
+						const MPtexTexelInfo texel_info,
+						const MPtexLogRes log_res);
+
+struct Image *
+BKE_ptex_mesh_image_get(struct Object *ob,
+						const char layer_name[MAX_CUSTOMDATA_LAYER_NAME]);
+
+/* Free contents of loop_ptex, but not loop_ptex itself */
+void BKE_loop_ptex_free(MLoopPtex *loop_ptex);
+
+size_t BKE_loop_ptex_rect_num_bytes(const MLoopPtex *loop_ptex);
+
+size_t BKE_ptex_rect_num_bytes(const MPtexTexelInfo texel_info,
+							   const MPtexLogRes res);
+
+/* SDNA uses built-in type, these just convert to enum member */
+PtexDataType BKE_ptex_texel_data_type(const MPtexTexelInfo texel_info);
+PtexDataType BKE_loop_ptex_texel_data_type(const MLoopPtex *loop_ptex);
+
+void BKE_loop_ptex_resize(MLoopPtex *loop_ptex, const MPtexLogRes logres);
+
+void BKE_ptex_update_from_image(MLoopPtex *loop_ptex, const int totloop);
+
+/* Add CD_LOOP_PTEX layer to DerivedMesh. The 'id' field will be just
+ * the sequential loop indices, TODO */
+void BKE_ptex_derived_mesh_inject(struct DerivedMesh *dm);
+
+struct DerivedMesh *BKE_ptex_derived_mesh_subdivide(struct DerivedMesh *dm);
+
+/* TODO */
+/* CustomData layer, not saved in file */
+typedef struct {
+    /* TODO: bad to do such mini allocations */
+
+    float *weights;
+
+	/* TODO: consider quad uv? */
+
+	/* TODO: already available? */
+	int *orig_loop_indices;
+	int num_loops;
+} MLoopInterp;
+
+void BKE_ptex_derived_mesh_interp_coords(struct DerivedMesh *dm);
+
+#endif
diff --git a/source/blender/blenkernel/CMakeLists.txt b/source/blender/blenkernel/CMakeLists.txt
index b3469ce..7d4e876 100644
--- a/source/blender/blenkernel/CMakeLists.txt
+++ b/source/blender/blenkernel/CMakeLists.txt
@@ -48,6 +48,7 @@ set(INC
 	../../../intern/smoke/extern
 	../../../intern/atomic
 	../../../extern/libmv
+	../../../extern/ptex
 
 	# XXX - BAD LEVEL CALL WM_api.h
 	../windowmanager
@@ -68,6 +69,7 @@ set(SRC
 	intern/appdir.c
 	intern/armature.c
 	intern/autoexec.c
+	intern/bke_ptex.c
 	intern/blender.c
 	intern/bmfont.c
 	intern/boids.c
@@ -250,6 +252,7 @@ set(SRC
 	BKE_pbvh.h
 	BKE_pointcache.h
 	BKE_property.h
+	BKE_ptex.h
 	BKE_report.h
 	BKE_rigidbody.h
 	BKE_sca.h
diff --git a/source/blender/blenkernel/intern/bke_ptex.c b/source/blender/blenkernel/intern/bke_ptex.c
new file mode 100644
index 0000000..a1c24d3
--- /dev/null
+++ b/source/blender/blenkernel/intern/bke_ptex.c
@@ -0,0 +1,532 @@
+/*
+ * ***** 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.
+ *
+ */
+
+#include <string.h>
+
+#include "BLI_utildefines.h"
+
+#include "BKE_customdata.h"
+#include "BKE_DerivedMesh.h"
+#include "BKE_image.h"
+#include "BKE_mesh.h"
+#include "BKE_ptex.h"
+#include "BKE_subsurf.h"
+#include "BLI_math_interp.h"
+#include "DNA_image_types.h"
+#include "DNA_mesh_types.h"
+#include "DNA_modifier_types.h"
+#include "GPU_extensions.h"
+#include "IMB_imbuf.h"
+#include "IMB_imbuf_types.h"
+#include "MEM_guardedalloc.h"
+#include "bl_ptex.h"
+
+static int ptex_data_type_num_bytes(const PtexDataType data_type)
+{
+	switch (data_type) {
+		case MPTEX_DATA_TYPE_UINT8:
+			return 1;
+		case MPTEX_DATA_TYPE_UINT16:
+			return 2;
+		case MPTEX_DATA_TYPE_FLOAT32:
+			return 4;
+	}
+
+	BLI_assert(!"Invalid PtexDataType");
+	return 0;
+}
+
+const int ptex_rlog2_limit = 30;
+static bool ptex_rlog2_valid(const int rlog2)
+{
+	/* Limit sides to about a billion texels */
+	return rlog2 >= 0 && rlog2 <= ptex_rlog2_limit;
+}
+
+static int ptex_res_from_rlog2(const int rlog2)
+{
+	BLI_assert(ptex_rlog2_valid(rlog2));
+	return (1 << rlog2);
+}
+
+static size_t ptex_area_from_logres(const MPtexLogRes logres)
+{
+	return ptex_res_from_rlog2(logres.u) * ptex_res_from_rlog2(logres.v);
+}
+
+static void ptex_data_from_float(void *dst_v, const float *src,
+								 const PtexDataType data_type,
+								 const size_t count)
+{
+	int i;
+	switch (data_type) {
+		case MPTEX_DATA_TYPE_UINT8:
+			{
+				uint8_t *dst = dst_v;
+				for (i = 0; i < count; i++) {
+					dst[i] = FTOCHAR(src[i]);
+				}
+				return;
+			}
+
+		case MPTEX_DATA_TYPE_UINT16:
+			{
+				uint16_t *dst = dst_v;
+				for (i = 0; i < count; i++) {
+					dst[i] = FTOUSHORT(src[i]);
+				}
+				return;
+			}
+
+		case MPTEX_DATA_TYPE_FLOAT32:
+			{
+				memcpy(dst_v, src, sizeof(*src) * count);
+				return;
+			}
+	}
+
+	BLI_assert(!"Invalid PtexDataType");
+}
+
+static size_t bke_ptex_bytes_per_texel(const MPtexTexelInfo texel_info)
+{
+	return (ptex_data_type_num_bytes(texel_info.data_type) *
+			texel_info.num_channels);
+}
+
+size_t BKE_ptex_rect_num_bytes(const MPtexTexelInfo texel_info,
+							   const MPtexLogRes logres)
+{
+	return (bke_ptex_bytes_per_texel(texel_info) *
+			ptex_area_from_logres(logres));
+}
+
+size_t BKE_loop_ptex_rect_num_bytes(const MLoopPtex *loop_ptex)
+{
+	return BKE_ptex_rect_num_bytes(loop_ptex->texel_info,
+								   loop_ptex->logres);
+}
+
+static void *bke_ptex_texels_calloc(const MPtexTexelInfo texel_info,
+									const MPtexLogRes logres)
+{
+	return MEM_callocN(BKE_ptex_rect_num_bytes(texel_info, logres),
+					   "bke_ptex_texels_calloc");
+}
+
+static void *bke_ptex_texels_malloc(const MPtexTexelInfo texel_info,
+									const MPtexLogRes logres)
+{
+	return MEM_mallocN(BKE_ptex_rect_num_bytes(texel_info, logres),
+					   "bke_ptex_texels_malloc");
+}
+
+void BKE_loop_ptex_init(MLoopPtex *loop_ptex,
+						const MPtexTexelInfo texel_info,
+						const MPtexLogRes logres)
+{
+	BLI_assert(ptex_rlog2_valid(logres.u));
+	BLI_assert(ptex_rlog2_valid(logres.v));
+	BLI_assert(texel_info.num_channels >= 1 &&
+			   texel_info.num_channels < 255);
+
+	loop_ptex->texel_info = texel_info;
+	loop_ptex->logres = logres;
+
+	loop_ptex->rect = bke_ptex_texels_calloc(texel_info, logres);
+
+	/* TODO: for testing, fill with some data */
+	{
+		const int u_res = ptex_res_from_rlog2(logres.u);
+		const int v_res = ptex_res_from_rlog2(logres.v);
+		const int bytes_per_component =
+			ptex_data_type_num_bytes(texel_info.data_type);
+		char *dst = loop_ptex->rect;
+		int x, y, c;
+		for (y = 0; y < v_res; y++) {
+			for (x = 0; x < u_res; x++) {
+				for (c = 0; c < texel_info.num_channels; c++) {
+					if (c == 0) {
+						const float u = (float)x / (float)(u_res - 1);
+						ptex_data_from_float(dst, &u,
+											 texel_info.data_type, 1);
+					}
+					else if (c == 1) {
+						const float v = (float)y / (float)(v_res - 1);
+						ptex_data_from_float(dst, &v,
+											 texel_info.data_type, 1);
+					}
+					else {
+						const float z = 1;
+						ptex_data_from_float(dst, &z,
+											 texel_info.data_type, 1);
+					}
+					dst += bytes_per_component;
+				}
+			}
+		}
+	}
+}
+
+static void ptex_get_loop_data(void *src, int index, void **texels,
+							   int *u_rlog2, int *v_rlog2)
+{
+	MLoopPtex *loop_ptex = src;
+	MLoopPtex *lp = &loop_ptex[index];
+	(*texels) = lp->rect;
+	(*u_rlog2) = lp->logres.u;
+	(*v_rlog2) = lp->logres.v;
+}
+
+BLI_STATIC_ASSERT(sizeof(MPtexTexelInfo) == 4, "MPtexTexelInfo size != 4");
+BLI_STATIC_ASSERT(sizeof(MPtexLogRes) == 4, "MPtexLogRes size != 4");
+
+static void ptex_pack_loops(Image **image,
+							MLoopPtex *loop_ptex,
+							const int num_loops)
+{
+	struct PtexPackedLayout *layout = NULL;
+	struct PtexPackedTexture *ppt = NULL;
+	struct ImBuf *ibuf;
+	int i;
+
+	// TODO
+	/* if (pack->ibuf) { */
+	/* 	IMB_freeImBuf(pack->ibuf); */
+	/* 	pack->ibuf = NULL; */
+	/* } */
+	//mesh_ptex_pack_textures_free(pack);
+
+	layout = ptex_packed_layout_new(num_loops);
+	for (i = 0; i < num_loops; i++) {
+		ptex_packed_layout_add(layout,
+							   ptex_res_from_rlog2(loop_ptex[i].logres.u),
+							   ptex_res_from_rlog2(loop_ptex[i].logres.v),
+							   i);
+	}
+	ptex_packed_layout_finalize(layout);
+
+	ppt = ptex_packed_texture_new();
+	ptex_packed_texture_from_layout(ppt, layout,
+									ptex_get_loop_data,
+									loop_ptex,
+									num_loops,
+									loop_ptex->texel_info.data_type,
+									loop_ptex->texel_info.num_channels,
+									-1);
+
+	// TODO
+	ibuf = IMB_allocImBuf(ptex_packed_texture_width(ppt),
+								ptex_packed_texture_height(ppt),
+								ptex_packed_texture_bytes_per_texel(ppt),
+								IB_rect);
+
+	// TODO: copy-pasted from imb_ptex
+	{
+		unsigned char *dst;
+		const unsigned char *src;
+		int x, y, c;
+		// TODO: unnecessary copying, also channels and stuff
+		// TODO: 

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list