Thursday, August 16, 2018

xna 4.0 - Does XNA support 1 dimensional textures


Does XNA support 1 dimensional textures. And by 1 dimension texture I mean something like Texture1d not a Texture2d where one dimension is 1 (because of the 4096 limit)



Answer



As per my comment, SetValue(Vector4[]) does not work. However, you should be able to use a Texture2D to achieve what you are after.


You can map a one-dimensional index to a two-dimensional index using the following function:



x = index % width;
y = (index - x) / width;

Therefore you can simply perform a SetData(Vector4[]) on a Texture2D and map the indicies within the shader (make sure you use point sampling). If you use all the pixels in a 4096x4096 texture you will effectively have an array that can contain 16777216 values.


int ValuesStride = 4096; // a.k.a Width.
texture2D Values;
sampler2D ValuesSampler =
sampler_state
{
Texture = ;

Filter = POINT;
AddressU = CLAMP;
AddressV = CLAMP;
};

float4 PixelShaderFunction(float4 Position : POSITION0, float2 UV : TEXCOORD0) : COLOR0
{
var value = ArrayLookup(ValuesSampler, ValuesStride, 100);
// ...
}


float4 ArrayLookup(sampler2D sampler, int width, int index)
{
float2 coord = (float2)0;
coord.x = index % width;
// We don't need to subtract coord.x because of how integral math works.
coord.y = asint(index / width);
return tex2D(sampler, coord);
}

No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...