[Bf-blender-cvs] [e128072] cycles-ptex-12: Move some Ptex code to BKE

Nicholas Bishop noreply at git.blender.org
Mon Jan 26 02:04:39 CET 2015


Commit: e1280720c927514520c6db4f17305c85cc1072e9
Author: Nicholas Bishop
Date:   Sun Jan 25 22:31:50 2015 +0100
Branches: cycles-ptex-12
https://developer.blender.org/rBe1280720c927514520c6db4f17305c85cc1072e9

Move 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..93d537a
--- /dev/null
+++ b/source/blender/blenkernel/BKE_ptex.h
@@ -0,0 +1,41 @@
+/*
+ * ***** 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_meshdata_types.h"
+
+struct Image;
+struct Object;
+
+void BKE_loop_ptex_init(MLoopPtex *loop_ptex,
+						const PtexDataType data_type,
+						const int num_channels,
+						const int u_rlog2,
+						const int v_rlog2);
+
+struct Image *BKE_ptex_mesh_image_get(struct Object *ob);
+
+/* Free contents of loop_ptex, but not loop_ptex itself */
+void BKE_loop_ptex_free(MLoopPtex *loop_ptex);
+
+size_t BKE_loop_ptex_data_num_bytes(MLoopPtex *loop_ptex);
+
+#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..f256694
--- /dev/null
+++ b/source/blender/blenkernel/intern/bke_ptex.c
@@ -0,0 +1,283 @@
+/*
+ * ***** 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_image.h"
+#include "BKE_mesh.h"
+#include "BKE_ptex.h"
+#include "DNA_image_types.h"
+#include "DNA_mesh_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_rlog2(const int u_rlog2, const int v_rlog2)
+{
+	return ptex_res_from_rlog2(u_rlog2) * ptex_res_from_rlog2(v_rlog2);
+}
+
+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");
+}
+
+size_t BKE_loop_ptex_data_num_bytes(MLoopPtex *loop_ptex)
+{
+	return (ptex_data_type_num_bytes(loop_ptex->data_type) *
+			ptex_area_from_rlog2(loop_ptex->u_rlog2, loop_ptex->v_rlog2) *
+			loop_ptex->num_channels);
+}
+
+void BKE_loop_ptex_init(MLoopPtex *loop_ptex,
+						const PtexDataType data_type,
+						const int num_channels,
+						const int u_rlog2,
+						const int v_rlog2)
+{
+	BLI_assert(ptex_rlog2_valid(u_rlog2));
+	BLI_assert(ptex_rlog2_valid(v_rlog2));
+	BLI_assert(num_channels >= 1 && num_channels < 255);
+
+	loop_ptex->data_type = data_type;
+	loop_ptex->num_channels = num_channels;
+	loop_ptex->u_rlog2 = u_rlog2;
+	loop_ptex->v_rlog2 = v_rlog2;
+
+	loop_ptex->data = MEM_callocN(BKE_loop_ptex_data_num_bytes(loop_ptex),
+								  "MLoopPtex.data");
+
+	/* TODO: for testing, fill with some data */
+	{
+		const int u_res = ptex_res_from_rlog2(u_rlog2);
+		const int v_res = ptex_res_from_rlog2(v_rlog2);
+		const int bytes_per_component = ptex_data_type_num_bytes(data_type);
+		char *dst = loop_ptex->data;
+		int x, y, c;
+		for (y = 0; y < v_res; y++) {
+			for (x = 0; x < u_res; x++) {
+				for (c = 0; c < num_channels; c++) {
+					if (c == 0) {
+						const float u = (float)x / (float)(u_res - 1);
+						ptex_data_from_float(dst, &u, data_type, 1);
+					}
+					else if (c == 1) {
+						const float v = (float)y / (float)(v_res - 1);
+						ptex_data_from_float(dst, &v, data_type, 1);
+					}
+					else {
+						const float z = 1;
+						ptex_data_from_float(dst, &z, 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->data;
+	(*u_rlog2) = lp->u_rlog2;
+	(*v_rlog2) = lp->v_rlog2;
+}
+
+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].u_rlog2),
+							   ptex_res_from_rlog2(loop_ptex[i].v_rlog2),
+							   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->data_type,
+									loop_ptex->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: only 8 bit ints for now
+
+		dst = (unsigned char*)ibuf->rect;
+		src = ptex_packed_texture_texels(ppt);
+		for (y = 0; y < ibuf->y; y++) {
+			for (x = 0; x < ibuf->x; x++) {
+				for (c = 0; c < 4; c++) {
+					*dst = *src;
+					src++;
+					dst++;
+				}
+			}
+		}
+
+		ibuf->ptex_coords =
+			MEM_mallocN(sizeof(*ibuf->ptex_coords) *
+						ptex_packed_texture_table_len(ppt),
+						"ptex_coords");
+		for (c = 0; c < ptex_packed_texture_table_len(ppt); c++) {
+			const struct PtexTableElement *elem =
+				ptex_packed_texture_table_elem(ppt, c);
+			ibuf->ptex_coords[c][0] = elem->co[0];
+			ibuf->ptex_coords[c][1] = elem->co[1];
+			ibuf->ptex_coords[c][2] = elem->res[0];
+			ibuf->ptex_coords[c][3] = elem->res[1];
+		}
+		ibuf->num_ptex_coords = ptex_packed_texture_table_len(ppt);
+	}
+
+	IMB_float_from_rect(ibuf);
+
+	(*image) = BKE_image_add_from_imbuf(ibuf);
+
+	ptex_packed_layout_delete(layout);
+	ptex_packed_texture_delete(ppt);
+}
+
+Image *BKE_ptex_mesh_image_get(struct Object *ob)
+{
+	Mesh *me = BKE_mesh_from_object(ob);
+	if (me) {
+		MLoopPtex *loop_ptex = CustomData_get_layer(&me->ldata,
+													CD_LOOP_PTEX);
+		if (loop_ptex) {
+			// TODO
+			if (!loop_ptex->image) {
+				ptex_pack_loops(&loop_ptex->image, loop_ptex, me->totloop);
+			}
+			else if (loop_ptex->image->tpageflag & IMA_TPAGE_REFRESH) {
+				//mesh_ptex_pack_textures_free(loop_ptex->pack);
+				loop_ptex->image->tpageflag &= ~IMA_TPAGE_REFRESH;
+			}
+			// TODO
+
+			/* if (!loop_ptex->pack->mapping_texture) { */
+			/* 	mesh_ptex_pack_update_textures(loop_ptex->pack); */
+			/* } */
+
+			return loop_ptex->image;
+		}
+	}
+	return NULL;
+}
+
+void BKE_loop_ptex_free(MLoopPtex *loop_ptex)
+{
+	if (loop_ptex->data) {
+		MEM_freeN(loop_ptex->data);
+	}
+	//mesh_ptex_pack_free(loop_ptex->pack);
+}




More information about the Bf-blender-cvs mailing list