Sending info to GPU via 2D rectangle texture

Hi,

I’m writing some info in the channels of a Texture2D, and I want to retrieve this in a shader in per pixel basis, but for this is necessary the use of texRECT() in CG.

My problem is that I don’t know how can I pass the Texture2D to the shader in a RECT Property.

¡Thanks in advance!

I don’t think rectangle textures are supported in Unity. Can you pass the inverse of the resolution of the texture and use that to scale your pixel coordinates and use them as UV coordinates in tex2d()?

in shader properties declare it as 2D

Properties {
	_Texture ("Base (RGB)", 2D) = "gray" {}
}

sample it as a RECT

uniform samplerRECT _Texture;

and in a shader itself,use

texRECT(_Texture, uv)

You can use non-power of 2 textures in Unity too,maybe they’ll be recognized as RECT automatically?I’m not sure on that

Thanks for the quick response!

multivac: I already try your solution and at first sight seems fine, but I think that doesn’t work, if I use:

Shader "Learning/Partial Texture RECT min" {
	Properties {
		_Texture( "Base(RGB)", 2D ) = "gray" {}
	}
	SubShader {
		Pass {

CGPROGRAM
#pragma vertex vert
#pragma fragment frag

#include "UnityCG.cginc"

uniform samplerRECT _Texture;
//uniform sampler2D _Texture;

struct sVert {
	float4 vertex : POSITION;
	float4 texCoord : TEXCOORD0;
};

sVert vert( sVert sIN ) { 
	sVert sOUT;		
	sOUT.vertex = mul( glstate.matrix.mvp, sIN.vertex );
	sOUT.texCoord = sIN.texCoord;
	return sOUT;
}

struct sColor {
	float4 oColor : COLOR;
};

sColor frag( sVert sIN ) {
	sColor sOUT;
		sOUT.oColor = texRECT( _Texture, float2(0,0) );
//		sOUT.oColor = tex2D( _Texture, float2(0,0) );
	return sOUT;
}
ENDCG
		}
	} 
}

with the following script

function Start () {
	var mapTexture = new Texture2D(128, 128);

	// Fill the texture with Sierpinski's fractal pattern!
	for (y=0; y < mapTexture.height; ++y) {
		for (x=0; x < mapTexture.width; ++x) {
			var color = (x&y) ? Color.red : Color.blue;
			mapTexture.SetPixel (x, y, color);
		}
	}
	// Apply all SetPixel calls
	mapTexture.Apply();	 
	renderer.material.SetTexture("_Texture", mapTexture );
}

… the color obtained is black instead of blue (the shader works fine if I uncomment the lines with “sampler2D” and “tex2D”, and comment the lines with “samplerRECT” and “texRECT”, because the color returned is almost blue).

What am I doing wrong?

I did a quick test and it returns blue color for me with both cases.
Might be quirk of videocards,I’m running windows vista with ati hd3850.
Try shifting the coordinates by one pixel(1/128),border-cases might handle oddly.

I’m not sure, but from a bit of Googling it looks like support for ARB_texture_non_power_of_two can be spotty. I don’t know where to find a list of video cards that do support it, though.

Thanks again,

I’ve got an iMac, with 3.06 GHz Intel Corel 2 Duo and a NVIDIA GeForce GT 130.

On that computer the same code is not working in MacOS but it does work in Windows version of Unity :?.

I think at the end, I’ll have to work approximating with sampler2D for avoiding problems.

Cheers!