[Bf-blender-cvs] [8b5e7d7] cycles-ptex-06: Code cleanup

Nicholas Bishop noreply at git.blender.org
Thu Jan 15 20:13:34 CET 2015


Commit: 8b5e7d7b07fdadc6931ae86cf4a4c283dcd0fc55
Author: Nicholas Bishop
Date:   Thu Jan 15 18:54:15 2015 +0100
Branches: cycles-ptex-06
https://developer.blender.org/rB8b5e7d7b07fdadc6931ae86cf4a4c283dcd0fc55

Code cleanup

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

M	intern/cycles/render/image.cpp

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

diff --git a/intern/cycles/render/image.cpp b/intern/cycles/render/image.cpp
index 913059c..d50d594 100644
--- a/intern/cycles/render/image.cpp
+++ b/intern/cycles/render/image.cpp
@@ -41,6 +41,50 @@ struct PtexTableElement {
 struct PtexPackedTexture {
 	PtexPackedTexture() : width(0), height(0) {}
 
+	class Cursor {
+	public:
+		Cursor(PtexPackedTexture &output, const int start_x,
+			   const int start_y)
+		: output(output),
+		  offset(output.calc_offset(start_x, start_y))
+		{
+		}
+
+		void set_increment(const int new_incr)
+		{
+			incr = new_incr;
+		}
+
+		void step()
+		{
+			offset += incr;
+		}
+
+		uint8_t *data() {
+			return output.data(offset);
+		}
+
+	private:
+		PtexPackedTexture &output;
+		int incr;
+		int offset;
+	};
+
+	int calc_offset(const int x, const int y) const
+	{
+		return (y * width + x) * bytes_per_texel;
+	}
+
+	uint8_t *data(const int pixel_index)
+	{
+		assert(pixel_index >= 0);
+		assert(pixel_index < texels.size() - num_channels);
+
+		return texels.data() + pixel_index;
+	}
+
+	// TODO: more privacy here
+
 	vector<uint8_t> texels;
 
 	vector<PtexTableElement> table;
@@ -51,10 +95,7 @@ struct PtexPackedTexture {
 	int num_channels;
 
 	int bytes_per_texel;
-
-	bool valid_pixel_index(int index) const {
-		return (index >= 0) && (index < texels.size() - num_channels);
-	}
+	int dst_stride;
 
 	void test_write() {
 		// Quick test to visually examine the packed output
@@ -167,16 +208,12 @@ struct PtexEdgeIter {
 		pos += incr * n;
 	}
 
-	void read_num_texels(PtexPackedTexture &output, const int num,
-						 int *dst_offset, const int dst_incr)
+	void copy_num_texels(PtexPackedTexture::Cursor &cursor,
+						 const int num)
 	{
 		for (int i = 0; i < num; i++) {
-			const int offset = *dst_offset;
-
-			assert(output.valid_pixel_index(offset));
-			read_texel(output.texels.data() + offset);
-
-			(*dst_offset) += dst_incr;
+			copy_texel(cursor.data());
+			cursor.step();
 			step();
 		}
 	}
@@ -184,7 +221,7 @@ struct PtexEdgeIter {
 	/* TODO(nicholasbishop): not doing any special filtering for
 	 * adjacent faces of differing resolutions, not sure how much that
 	 * will matter in practice */
-	void read_texel(void *texel)
+	void copy_texel(void *texel)
 	{
 		const int x = (int)(pos.x);
 		const int y = (int)(pos.y);
@@ -198,93 +235,31 @@ struct PtexEdgeIter {
 	float2 incr;
 };
 
-#if 0
-struct PtexBorderIter {
-	PtexBorderIter(PtexPackedTexture &output,
-				   PtexPtr<PtexTexture> &r,
-				   const uint face_id,
-				   const int dst_stride)
-		: elem(output.table[face_id]),
-		  info(r->getFaceInfo(face_id)),
-		  dst_stride(dst_stride),
-		  cur_side(Ptex::e_bottom)
-	{
-		const int bpt = output.bytes_per_texel;
-
-		inc[0][0] = bpt;
-		inc[0][1] = 0;
-
-		inc[1][0] = 0;
-		inc[1][1] = dst_stride;
-
-		inc[2][0] = -bpt;
-		inc[2][1] = 0;
-
-		inc[3][0] = 0;
-		inc[3][1] = -dst_stride;
-
-		dst_offset = ((elem.co[1] - 1) * output.width +
-					  (elem.co[0] - 1)) * bpt;
-	}
-
-	bool next() {
-		const int side_res = (cur_side == Ptex::e_bottom || cur_side == Ptex::e_top) {
-			
-		}
-		const int side_res = elem.res[(i == 0 || i == 2) ? 0 : 1];
-	}
-
-	const PtexTableElement &elem;
-	const Ptex::FaceInfo &info;
-	const int dst_stride;
-	int dst_offset;
-	Ptex::EdgeId cur_side;
-	int inc[4][2];
-};
-#endif
-
-static void ptex_copy_face_pixels(PtexPackedTexture &output,
+static void ptex_copy_face_border(PtexPackedTexture::Cursor &cursor,
 								  PtexPtr<PtexTexture> &r,
-								  const int dst_stride,
-								  const uint face_id)
+								  const int face_id,
+								  const Ptex::FaceInfo &info,
+								  const Ptex::EdgeId edge_id,
+								  const int side_res)
 {
-	PtexTableElement &elem = output.table[face_id];
-
-	const int bpt = output.bytes_per_texel;
-	int dst_offset = ((elem.co[1] * output.width + elem.co[0]) * bpt);
-
-	if (face_id != 1)
-		;//return;
-
-	/* Copy the whole face at highest resolution */
-	r->getData(face_id, output.texels.data() + dst_offset, dst_stride);
-
-	/* Add borders by copying data from adjacent faces */
-	const Ptex::FaceInfo &info = r->getFaceInfo(face_id);
-	const bool is_subface = info.isSubface();
-	const int dst_incr_array[4] = {bpt, dst_stride, -bpt, -dst_stride};
-	dst_offset = ((elem.co[1] - 1) * output.width +
-				  (elem.co[0] - 1)) * bpt;
-
-	for (int i = 0; i < 4; i++) {
-		/* In general there is only one adjacent face, but when a quad
-		 * is adjacent to a non-quad there are two adjacent subfaces */
-		int adj_face_id[2] = {info.adjface(i), -1};
-		Ptex::EdgeId adj_edge[2] = {info.adjedge(i)};
-		bool no_adj = false;
-
-		// TODO
-		const int side_res = elem.res[(i == 0 || i == 2) ? 0 : 1];
-
-		if (adj_face_id[0] == -1) {
-			/* If there's no adjacent face, pretend the face is
-			 * adjacent to itself (i.e. copy its own borders) */
-			adj_face_id[0] = face_id;
-			adj_edge[0] = (Ptex::EdgeId)i;
-			no_adj = true;
-		}
-
+	// TODO: handle corners correctly, for now just skipping over
+	// them:
+	cursor.step();
+
+	/* In general there is only one adjacent face, but when a quad is
+	 * adjacent to a non-quad there are two adjacent subfaces */
+	int adj_face_id[2] = {info.adjface(edge_id), -1};
+	Ptex::EdgeId adj_edge[2] = {info.adjedge(edge_id)};
+
+	if (adj_face_id[0] == -1) {
+		/* If there's no adjacent face, pretend the face is adjacent
+		 * to itself (i.e. copy its own borders) */
+		PtexEdgeIter iter(*r->getData(face_id), edge_id, side_res, false);
+		iter.copy_num_texels(cursor, side_res);
+	}
+	else {
 		const Ptex::FaceInfo &adj_info = r->getFaceInfo(adj_face_id[0]);
+		const bool is_subface = info.isSubface();
 		if (!is_subface && adj_info.isSubface()) {
 			/* Face is adjacent to two subfaces */
 			int next_edge_id = (adj_edge[0] + 3) % 4;
@@ -293,44 +268,75 @@ static void ptex_copy_face_pixels(PtexPackedTexture &output,
 			adj_edge[1] = (Ptex::EdgeId)((adj_edge[1] + 3) % 4);
 		}
 
-		// TODO: handle corners correctly
-		const int dst_incr = dst_incr_array[i];
-		dst_offset += dst_incr;
-
 		PtexFaceData *face_data[2] = {
 			r->getData(adj_face_id[0]),
 			adj_face_id[1] == -1 ? NULL : r->getData(adj_face_id[1])
 		};
 
-		if (no_adj) {
-			PtexEdgeIter iter(*face_data[0], adj_edge[0], side_res, false);
-			iter.read_num_texels(output, side_res, &dst_offset, dst_incr);
-		}
-		else if (is_subface && !adj_info.isSubface()) {
+		if (is_subface && !adj_info.isSubface()) {
 			PtexEdgeIter iter(*face_data[0], adj_edge[0], side_res * 2, true);
 			const bool is_primary = adj_info.adjface(adj_edge[0]) == face_id;
 			if (is_primary) {
 				iter.step(side_res);
 			}
 
-			iter.read_num_texels(output, side_res, &dst_offset, dst_incr);
+			iter.copy_num_texels(cursor, side_res);
 		}
 		else if (!is_subface && adj_info.isSubface()) {
 			const int half_side_res = side_res / 2;
 			for (int sf = 0; sf <= 1; sf++) {
 				PtexEdgeIter iter(*face_data[sf], adj_edge[sf],
 								  half_side_res, true);
-				iter.read_num_texels(output, half_side_res, &dst_offset,
-									 dst_incr);
+				iter.copy_num_texels(cursor, half_side_res);
 			}
 		}
 		else {
 			PtexEdgeIter iter(*face_data[0], adj_edge[0], side_res, true);
-			iter.read_num_texels(output, side_res, &dst_offset, dst_incr);
+			iter.copy_num_texels(cursor, side_res);
 		}
 	}
 }
 
+static void ptex_copy_face_texels(PtexPackedTexture &output,
+								  PtexPtr<PtexTexture> &r,
+								  const uint face_id)
+{
+	PtexTableElement &elem = output.table[face_id];
+
+	if (face_id != 0)
+		;//return;
+
+	/* Copy the whole face at highest resolution */
+	r->getData(face_id,
+			   output.data(output.calc_offset(elem.co[0], elem.co[1])),
+			   output.dst_stride);
+
+	/* Add borders by copying data from adjacent faces */
+	const Ptex::FaceInfo &info = r->getFaceInfo(face_id);
+
+	const int dst_incr[4] = {
+		output.bytes_per_texel,
+		output.dst_stride,
+		-output.bytes_per_texel,
+		-output.dst_stride
+	};
+
+	PtexPackedTexture::Cursor cursor(output,
+										  elem.co[0] - 1,
+										  elem.co[1] - 1);
+	for (int i = 0; i < 4; i++) {
+		Ptex::EdgeId edge_id = (Ptex::EdgeId)i;
+
+		// TODO
+		const int side_res = elem.res[(i == 0 || i == 2) ? 0 : 1];
+
+		cursor.set_increment(dst_incr[i]);
+
+		ptex_copy_face_border(cursor, r, face_id, info,
+							  edge_id, side_res);
+	}
+}
+
 // TODO
 static bool ptex_pack(PtexPackedTexture *output, const char *path,
 					  const uint width, PtexCache *ptex_cache) {
@@ -403,6 +409,7 @@ static bool ptex_pack(PtexPackedTexture *output, const char *path,
 	const int type_size_in_bytes = ptex_data_type_size_in_bytes(r->dataType());
 
 	output->bytes_per_texel = r->numChannels() * type_size_in_bytes;
+	output->dst_stride = output->width * output->bytes_per_texel;
 
 	output->num_channels = r->numChannels();
 	
@@ -410,12 +417,11 @@ static bool ptex_pack(PtexPackedTexture *output, const char *path,
 						  output->bytes_per_texel);
 
 	// Copy face textures
-	const int dst_stride = output->width * output->bytes_per_texel;
 	for (SortedFaceIds::const_iterator iter = sorted_face_ids.begin();
 		 iter != sorted_face_ids.end(); ++iter) {
 		const uint face_id = iter->second;
 		
-		ptex_copy_face_pixels(*output, r, dst_stride, face_id);
+		ptex_copy_face_texels(*output, r, face_id);
 	}
 
 	return true;




More information about the Bf-blender-cvs mailing list