[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [21768] branches/blender2.5/blender: 2.5: Texture Filtering

Brecht Van Lommel brecht at blender.org
Tue Jul 21 15:20:36 CEST 2009


Revision: 21768
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=21768
Author:   blendix
Date:     2009-07-21 15:20:35 +0200 (Tue, 21 Jul 2009)

Log Message:
-----------
2.5: Texture Filtering

Patch by Alfredo de Greef with high quality image texture filters.
This adds 3 new filters:

* SAT: Summed Area Tables. This is like mipmaps, but using somewhat
  more memory avoids some artifacts.
* EWA: Ellipitical Weighted Average, anisotropic filter.
* FELINE: Fast elliptical lines for anisotropic texture mapping.

The one change I made to this was to try to fix an alpha/premul
problem, hopefully I didn't break anything, it looks compatible
with the existing filter now for me.

Modified Paths:
--------------
    branches/blender2.5/blender/release/ui/buttons_texture.py
    branches/blender2.5/blender/source/blender/blenkernel/intern/texture.c
    branches/blender2.5/blender/source/blender/blenloader/intern/readfile.c
    branches/blender2.5/blender/source/blender/editors/interface/interface_templates.c
    branches/blender2.5/blender/source/blender/editors/space_view3d/view3d_draw.c
    branches/blender2.5/blender/source/blender/imbuf/IMB_imbuf.h
    branches/blender2.5/blender/source/blender/imbuf/intern/filter.c
    branches/blender2.5/blender/source/blender/makesdna/DNA_texture_types.h
    branches/blender2.5/blender/source/blender/makesrna/intern/rna_texture.c
    branches/blender2.5/blender/source/blender/render/intern/source/imagetexture.c
    branches/blender2.5/blender/source/blender/render/intern/source/texture.c

Modified: branches/blender2.5/blender/release/ui/buttons_texture.py
===================================================================
--- branches/blender2.5/blender/release/ui/buttons_texture.py	2009-07-21 13:15:29 UTC (rev 21767)
+++ branches/blender2.5/blender/release/ui/buttons_texture.py	2009-07-21 13:20:35 UTC (rev 21768)
@@ -426,19 +426,17 @@
 
 		sub = split.column() 
 		sub.itemL(text="Filter:")
-		#sub.itemR(tex, "filter", text="")
+		sub.itemR(tex, "filter", text="")
 		sub.itemR(tex, "mipmap")
 		row = sub.row()
 		row.itemR(tex, "mipmap_gauss", text="Gauss")
 		row.active = tex.mipmap
 		sub.itemR(tex, "interpolation")
-		"""
 		if tex.mipmap and tex.filter != 'DEFAULT':
 			if tex.filter == 'FELINE':
 				sub.itemR(tex, "filter_probes", text="Probes")
 			else:
 				sub.itemR(tex, "filter_eccentricity", text="Eccentricity")
-		"""
 
 class TEXTURE_PT_image_mapping(TextureButtonsPanel):
 	__idname__= "TEXTURE_PT_image_mapping"

Modified: branches/blender2.5/blender/source/blender/blenkernel/intern/texture.c
===================================================================
--- branches/blender2.5/blender/source/blender/blenkernel/intern/texture.c	2009-07-21 13:15:29 UTC (rev 21767)
+++ branches/blender2.5/blender/source/blender/blenkernel/intern/texture.c	2009-07-21 13:20:35 UTC (rev 21768)
@@ -435,12 +435,15 @@
 	VarStruct *varstr;
 	int a;
 
+	tex->type= TEX_CLOUDS;
 	tex->stype= 0;
 	tex->flag= TEX_CHECKER_ODD;
-	tex->imaflag= TEX_INTERPOL+TEX_MIPMAP+TEX_USEALPHA;
+	tex->imaflag= TEX_INTERPOL|TEX_MIPMAP|TEX_USEALPHA;
 	tex->extend= TEX_REPEAT;
 	tex->cropxmin= tex->cropymin= 0.0;
 	tex->cropxmax= tex->cropymax= 1.0;
+	tex->texfilter = TXF_DEFAULT;
+	tex->afmax = 8;
 	tex->xrepeat= tex->yrepeat= 1;
 	tex->fie_ima= 2;
 	tex->sfra= 1;

Modified: branches/blender2.5/blender/source/blender/blenloader/intern/readfile.c
===================================================================
--- branches/blender2.5/blender/source/blender/blenloader/intern/readfile.c	2009-07-21 13:15:29 UTC (rev 21767)
+++ branches/blender2.5/blender/source/blender/blenloader/intern/readfile.c	2009-07-21 13:20:35 UTC (rev 21768)
@@ -9273,6 +9273,7 @@
 		World *wo;
 		Object *ob;
 		Material *ma;
+		Tex *tex;
 		Scene *sce;
 		ToolSettings *ts;
 		int i;
@@ -9349,6 +9350,11 @@
 			}
 		}
 
+		/* texture filter */
+		for(tex = main->tex.first; tex; tex = tex->id.next)
+			if(tex->afmax == 0)
+				tex->afmax= 8;
+
 		for(ma = main->mat.first; ma; ma = ma->id.next) {
 			if(ma->mode & MA_HALO) {
 				ma->material_type= MA_TYPE_HALO;

Modified: branches/blender2.5/blender/source/blender/editors/interface/interface_templates.c
===================================================================
--- branches/blender2.5/blender/source/blender/editors/interface/interface_templates.c	2009-07-21 13:15:29 UTC (rev 21767)
+++ branches/blender2.5/blender/source/blender/editors/interface/interface_templates.c	2009-07-21 13:20:35 UTC (rev 21768)
@@ -1215,10 +1215,10 @@
 	uiBlockSetHandleFunc(block, do_preview_buttons, NULL);
 	
 	/* add buttons */
-	if(id) {
-		if(GS(id->name) == ID_MA || (parent && GS(parent->name) == ID_MA)) {
-			if(GS(id->name) == ID_MA) ma= (Material*)id;
-			else ma= (Material*)parent;
+	if(pid) {
+		if(GS(pid->name) == ID_MA || (pparent && GS(pparent->name) == ID_MA)) {
+			if(GS(pid->name) == ID_MA) ma= (Material*)pid;
+			else ma= (Material*)pparent;
 
 			uiLayoutColumn(row, 1);
 

Modified: branches/blender2.5/blender/source/blender/editors/space_view3d/view3d_draw.c
===================================================================
--- branches/blender2.5/blender/source/blender/editors/space_view3d/view3d_draw.c	2009-07-21 13:15:29 UTC (rev 21767)
+++ branches/blender2.5/blender/source/blender/editors/space_view3d/view3d_draw.c	2009-07-21 13:20:35 UTC (rev 21768)
@@ -1345,7 +1345,7 @@
 		int mip= 0;
 		
 		if(ibuf->mipmap[0]==NULL)
-			IMB_makemipmap(ibuf, 0);
+			IMB_makemipmap(ibuf, 0, 0);
 		
 		while(tzoom < 1.0f && mip<8 && ibuf->mipmap[mip]) {
 			tzoom*= 2.0f;

Modified: branches/blender2.5/blender/source/blender/imbuf/IMB_imbuf.h
===================================================================
--- branches/blender2.5/blender/source/blender/imbuf/IMB_imbuf.h	2009-07-21 13:15:29 UTC (rev 21767)
+++ branches/blender2.5/blender/source/blender/imbuf/IMB_imbuf.h	2009-07-21 13:20:35 UTC (rev 21768)
@@ -319,7 +319,7 @@
 void IMB_filter(struct ImBuf *ibuf);
 void IMB_filterN(struct ImBuf *out, struct ImBuf *in);
 void IMB_filter_extend(struct ImBuf *ibuf, char *mask);
-void IMB_makemipmap(struct ImBuf *ibuf, int use_filter);
+void IMB_makemipmap(struct ImBuf *ibuf, int use_filter, int SAT);
 
 /**
  *

Modified: branches/blender2.5/blender/source/blender/imbuf/intern/filter.c
===================================================================
--- branches/blender2.5/blender/source/blender/imbuf/intern/filter.c	2009-07-21 13:15:29 UTC (rev 21767)
+++ branches/blender2.5/blender/source/blender/imbuf/intern/filter.c	2009-07-21 13:20:35 UTC (rev 21768)
@@ -371,6 +371,7 @@
 	}
 }
 
+#if 0
 void IMB_makemipmap(ImBuf *ibuf, int use_filter)
 {
 	ImBuf *hbuf= ibuf;
@@ -394,5 +395,90 @@
 		minsize= hbuf->x<hbuf->y?hbuf->x:hbuf->y;
 	}
 }
+#endif
 
-
+void IMB_makemipmap(ImBuf *ibuf, int use_filter, int SAT)
+{
+	if (SAT) {
+		// to maximize precision subtract image average, use intermediate double SAT,
+		// only convert to float at the end
+		const double dv = 1.0/255.0;
+		double avg[4] = {0, 0, 0, 0};
+		const int x4 = ibuf->x << 2;
+		int x, y, i;
+		ImBuf* sbuf = IMB_allocImBuf(ibuf->x, ibuf->y, 32, IB_rectfloat, 0);
+		double *satp, *satbuf = MEM_callocN(sizeof(double)*ibuf->x*ibuf->y*4, "tmp SAT buf");
+		const double mf = ibuf->x*ibuf->y;
+		float* fp;
+		ibuf->mipmap[0] = sbuf;
+		if (ibuf->rect_float) {
+			fp = ibuf->rect_float;
+			for (y=0; y<ibuf->y; ++y)
+				for (x=0; x<ibuf->x; ++x) {
+					avg[0] += *fp++;
+					avg[1] += *fp++;
+					avg[2] += *fp++;
+					avg[3] += *fp++;
+				}
+		}
+		else {
+			char* cp = (char*)ibuf->rect;
+			for (y=0; y<ibuf->y; ++y)
+				for (x=0; x<ibuf->x; ++x) {
+					avg[0] += *cp++ * dv;
+					avg[1] += *cp++ * dv;
+					avg[2] += *cp++ * dv;
+					avg[3] += *cp++ * dv;
+				}
+		}
+		avg[0] /= mf;
+		avg[1] /= mf;
+		avg[2] /= mf;
+		avg[3] /= mf;
+		for (y=0; y<ibuf->y; ++y)
+			for (x=0; x<ibuf->x; ++x) {
+				const unsigned int p = (x + y*ibuf->x) << 2;
+				char* cp = (char*)ibuf->rect + p;
+				fp = ibuf->rect_float + p;
+				satp = satbuf + p;
+				for (i=0; i<4; ++i, ++cp, ++fp, ++satp) {
+					double sv = (ibuf->rect_float ? (double)*fp : (double)(*cp)*dv) - avg[i];
+					if (x > 0) sv += satp[-4];
+					if (y > 0) sv += satp[-x4];
+					if (x > 0 && y > 0) sv -= satp[-x4 - 4];
+					*satp = sv;
+				}
+			}
+		fp = sbuf->rect_float;
+		satp = satbuf;
+		for (y=0; y<ibuf->y; ++y)
+			for (x=0; x<ibuf->x; ++x) {
+				*fp++ = (float)*satp++;
+				*fp++ = (float)*satp++;
+				*fp++ = (float)*satp++;
+				*fp++ = (float)*satp++;
+			}
+		MEM_freeN(satbuf);
+		fp = &sbuf->rect_float[(sbuf->x - 1 + (sbuf->y - 1)*sbuf->x) << 2];
+		fp[0] = avg[0];
+		fp[1] = avg[1];
+		fp[2] = avg[2];
+		fp[3] = avg[3];
+	}
+	else {
+		ImBuf *hbuf = ibuf;
+		int curmap = 0;
+		while (curmap < IB_MIPMAP_LEVELS) {
+			if (use_filter) {
+				ImBuf *nbuf= IMB_allocImBuf(hbuf->x, hbuf->y, 32, IB_rect, 0);
+				IMB_filterN(nbuf, hbuf);
+				ibuf->mipmap[curmap] = IMB_onehalf(nbuf);
+				IMB_freeImBuf(nbuf);
+			}
+			else ibuf->mipmap[curmap] = IMB_onehalf(hbuf);
+			hbuf = ibuf->mipmap[curmap];
+			if (hbuf->x == 1 && hbuf->y == 1) break;
+			curmap++;
+		}
+	}
+}

Modified: branches/blender2.5/blender/source/blender/makesdna/DNA_texture_types.h
===================================================================
--- branches/blender2.5/blender/source/blender/makesdna/DNA_texture_types.h	2009-07-21 13:15:29 UTC (rev 21767)
+++ branches/blender2.5/blender/source/blender/makesdna/DNA_texture_types.h	2009-07-21 13:20:35 UTC (rev 21768)
@@ -150,11 +150,13 @@
 
 	/* newnoise: noisebasis type for clouds/marble/etc, noisebasis2 only used for distorted noise */
 	short noisebasis, noisebasis2;
-	
+
 	short imaflag, flag;
 	short type, stype;
 	
 	float cropxmin, cropymin, cropxmax, cropymax;
+	int texfilter;
+	int afmax;	// anisotropic filter maximum value, ewa -> max eccentricity, feline -> max probes
 	short xrepeat, yrepeat;
 	short extend;
 
@@ -253,6 +255,15 @@
 #define TEX_GAUSS_MIP	4096
 #define TEX_FILTER_MIN	8192
 
+/* texfilter */
+// TXF_DEFAULT -> blender's old texture filtering method
+#define TXF_DEFAULT		0
+#define TXF_EWA			1
+#define TXF_FELINE		2
+#define TXF_AREA		3
+// TXF_SAT only available when mipmaps disabled
+#define TXF_SAT			4
+
 /* imaflag unused, only for version check */
 #define TEX_FIELDS_		8
 #define TEX_ANIMCYCLIC_	64

Modified: branches/blender2.5/blender/source/blender/makesrna/intern/rna_texture.c
===================================================================
--- branches/blender2.5/blender/source/blender/makesrna/intern/rna_texture.c	2009-07-21 13:15:29 UTC (rev 21767)
+++ branches/blender2.5/blender/source/blender/makesrna/intern/rna_texture.c	2009-07-21 13:20:35 UTC (rev 21768)
@@ -39,6 +39,14 @@
 
 #include "WM_types.h"
 
+static EnumPropertyItem texture_filter_items[] = {
+	{TXF_DEFAULT, "DEFAULT", 0, "Default", ""},
+	{TXF_EWA, "EWA", 0, "EWA", ""},
+	{TXF_FELINE, "FELINE", 0, "FELINE", ""},
+	{TXF_AREA, "AREA", 0, "Area", ""},
+	{TXF_SAT, "SAT", 0, "SAT (4x mem)", ""},
+	{0, NULL, 0, NULL, NULL}};
+
 #ifdef RNA_RUNTIME
 
 #include "BKE_texture.h"
@@ -110,6 +118,35 @@
 		tex->coba= add_colorband(0);
 }
 
+static void rna_ImageTexture_mipmap_set(PointerRNA *ptr, int value)
+{
+	Tex *tex= (Tex*)ptr->data;
+
+	if(value) tex->imaflag |= TEX_MIPMAP;
+	else tex->imaflag &= ~TEX_MIPMAP;
+
+	if((tex->imaflag & TEX_MIPMAP) && tex->texfilter == TXF_SAT)
+		tex->texfilter = TXF_DEFAULT;
+}
+
+static EnumPropertyItem *rna_ImageTexture_filter_itemf(bContext *C, PointerRNA *ptr, int *free)
+{
+	Tex *tex= (Tex*)ptr->data;
+	EnumPropertyItem *item= NULL;
+	int totitem= 0;
+
+	RNA_enum_items_add_value(&item, &totitem, texture_filter_items, TXF_DEFAULT);
+	RNA_enum_items_add_value(&item, &totitem, texture_filter_items, TXF_EWA);
+	RNA_enum_items_add_value(&item, &totitem, texture_filter_items, TXF_FELINE);
+	RNA_enum_items_add_value(&item, &totitem, texture_filter_items, TXF_AREA);
+	if(tex->imaflag & TEX_MIPMAP)
+		RNA_enum_items_add_value(&item, &totitem, texture_filter_items, TXF_SAT);
+	
+	*free= 1;

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list