3

I have mapped some values into my texture on my alpha channel. Actually I use my texture as 2Darray. What I need is a way to read the alpha value of the map at position e.g. [4][5] (representing x and y)

I need the returned value available in my pixelshader. Is there any way to do this?

I use DX9.

Thx in advance!

marius
  • 1,118
  • 1
  • 18
  • 38

2 Answers2

1

Do you want to use the texel at [4][5] (x,y) for your entire pixelshader? if that is your question you could just precalc that cordinate on the vertex shader and passit along to every vertex, and then sample with that uv cords. this way it wont get interpolated. (or it will, but it will only have one value to interpolate with)

other than that you probably have to specifiy abit more on what you are trying to achive. What are you using it for? when will it occure, what sort of mesh are you using it for?

Tordin
  • 340
  • 1
  • 10
  • I just want to have it for calculations. Like I run an algorithsm over each pixel calculation x and y coordinates and get the alpha values from this texture at the coordinates. That should be possible I think this is really easy to solve. – marius Oct 22 '12 at 07:41
  • Are you refering to just texturing with the correct uv cords of a polygon? you can just pass on the uv from the vertex if that is the case. – Tordin Oct 22 '12 at 07:59
  • No, I dont want to texture my polygon. This texture is not for texturing use! It was created from outside and has just some fake values that I need to read like an 2D-Array. Really I just want to use it like 'texture[x][y].alpha'. I got x and y calculated... that shouldn't be as difficult?! – marius Oct 22 '12 at 08:11
  • well, if it is in an regular array, then its something like for(int h = 0; h – Tordin Oct 22 '12 at 08:20
  • Still wrong, I dont want to get throu all pixels of the texture.. here is some code `texture lightMap; float4 PixelShaderMain(...) { ... float lightIntensity = getLightIntensity(angles); ... } float getLightIntensity(float2 angles) { int x = (int)((float)dimX*(angles.x/360)); int y = (int)((float)dimY*(angles.y/90)); return lightMap[y][x].alpha; } ` – marius Oct 22 '12 at 08:34
  • values for texture lightMap is set from outside (on initialize set) – marius Oct 22 '12 at 08:37
  • i dont know if that actualy is syntax correct. and besides, in pixelshader texcoords, it dont range from 0->W, but from 0->1. dont know if that helps. – Tordin Oct 22 '12 at 08:43
  • I think i got some other issue. I have to get into HLSL some more befor doing this. Thank you anyway :) – marius Oct 22 '12 at 09:02
  • 1
    i've found a solution on this here is the answer i got: You need to declare a texture and a texture sampler at the beginning of you shader (before functions). texture2D texture1; sampler2D myTexture1Sampler = sampler_state { Texture = ; MinFilter = LINEAR; MagFilter = LINEAR; MipFilter = LINEAR; };Then you could access pixel values of your texture through a dedicated function: float4 textureColor = tex2D(myTexture1Sampler, float2(u,v)); textureColor.r; textureColor.g; textureColor.b; textureColor.a; – marius Oct 22 '12 at 10:52
0

Texture2DArray is a shader model 4 thing. I don't believe you're using it on dx9.

If you are using shader model 4, then just use the function Load(4, 5).

Otherwise, for sm1,2,3, you can put the numbers you want, e.g. 4.0f and 5.0 into your vertex as normal texcoord data. Then have the pixel shader scale it by the size of the texture.

struct VertexInput {
    float4 pos : POSITION;
    float2 uv : TEXCOORD0; //0.0, 1.0, 2.0, 3.0, 4.0 etc
};

struct PixelInput {
    float4  position : POSITION;
    float2  uv : TEXCOORD0;
};

PixelInput vsTex(VertexInput vtx) 
{
    PixelInput output;
    float4 pos = vtx.pos;
    output.position = mul(pos, MatWorld);
    output.position = mul(output.position, MatView);
    output.position = mul(output.position, MatProj);

    output.uv = vtx.uv;

    return output;
}

float4 PixelShader(PixelInput input) : SV_Target
{
    float coords = pix.uv / float2(TEX_WIDTH, TEX_HEIGHT);
    return tex = tex2D(mySampler, coords);
}

Where TEX_WIDTH, TEX_HEIGHT are passed in via the 'defines' parameter of D3DXCompileShader. And

OR: just do 4.0f/tex_width and 5.0/tex_height in software and just pass that number (which will be between [0.0f,1.0f] through to the pixel shader)

Pod
  • 3,938
  • 2
  • 37
  • 45