4+1 questions about shaders (low level)

Started by
6 comments, last by BlueChip 16 years, 10 months ago
__________1st QUESTION__________ if I use (for example) 5 texture, 4 datail textures and 1 alpha texture, and I have a texture coordinate for detail textures and a texture coordinate for alpha texture. In my mind, I've needed of 5 float4 (for the textures rgba values) and 2 float2 for textures coordinates..... But many examples tell me, that I need of this struct..

struct PS_INPUT
{
    float4 Position   	  : POSITION;
    float2 AlphaCoord  	  : TEXCOORD0;
    float2 TexCoord0	  : TEXCOORD1;
    float2 TexCoord1   	  : TEXCOORD2;
    float2 TexCoord2	  : TEXCOORD3;
    float2 TexCoord3   	  : TEXCOORD4;
};
Can you explain it? __________2nd QUESTION__________ look this:

sampler2D Texture2D;

PS_OUTPUT ps_main( in PS_INPUT In )
{
        PS_OUTPUT Out;                         
	float4 texture = tex2D(Texture2D, In.TexCoord);
	omissis...
}
with this code I fill 'float4 texture' with the RGBA values of the my texture at pixel with coordinates = TexCoord..... but Texture2D is an empty variable, and the input 'In.TexCoord' is only a coordinates.. so the question is: how do this code to read these rgba values? is not better a function like this? float4 texture = tex2D(MYTEXTURE, In.TexCoord); in this way, if I've more textures with same dimensions, I can use only a TEXCOORD. __________3rd QUESTION__________ DX Documentation, about D3DTSS_TEXCOORDINDEX say: ..omissis.. Index of the texture coordinate set to use with this texture stage. You can specify up to eight sets of texture coordinates per vertex. If a vertex does not include a set of texture coordinates at the specified index, the system defaults to the u and v coordinates (0,0). This flag is used only for fixed function vertex processing. For example, it should not be used with vertex shaders. When rendering using vertex shaders, each stage's texture coordinate index must be set to its default value. The default index for each stage is equal to the stage index. Set this state to the zero-based index of the coordinate set for each vertex that this texture stage uses. ..omissis.. So, this means that , with PS adn VS, I must specify alls the coordinates in the vertex structure?? in this way:

struct TERRAINVERTEX 
{
    float x, y, z;
    float tu0, tv0, tu1, tv1, tu2, tv2, tu3, tv3, tu4, tv4;   // The texture coordinates.
};
#define D3DFVF_TERRAINVERTEX (D3DFVF_XYZ  | D3DFVF_TEX5)
and in the end to fill up the buffer

TERRAIN Vertices[4] = 
{
    x, y, z, tu0, tv0, tu1, tv1, tu2, tv2, tu3, tv3, tu4, tv4, 
    x, y, z, tu0, tv0, tu1, tv1, tu2, tv2, tu3, tv3, tu4, tv4, 
    x, y, z, tu0, tv0, tu1, tv1, tu2, tv2, tu3, tv3, tu4, tv4, 
    x, y, z, tu0, tv0, tu1, tv1, tu2, tv2, tu3, tv3, tu4, tv4
};
but if tu0 = tu1 = tu2 etc etc. does is it a waste of time and memory? __________4th QUESTION__________ which relation there is between PS declaration and VS declaration?? I.e, if I want this PS struct:

struct PS_INPUT
{
    float4 Position   	  : POSITION;
    float2 AlphaCoord  	  : TEXCOORD0;
    float2 TexCoord0	  : TEXCOORD1;
    float2 TexCoord1   	  : TEXCOORD2;
    float2 TexCoord2	  : TEXCOORD3;
    float2 TexCoord3   	  : TEXCOORD4;
};
my VS struct must be this:

struct VS_INPUT
{
    float4 Position   : POSITION;
    float2 Alpha      : TEXCOORD0;
    float2 Texture0   : TEXCOORD1;
    float2 Texture1   : TEXCOORD2;
    float2 Texture2   : TEXCOORD3;
    float2 Texture3   : TEXCOORD4;
};
i.e. VertexShader struct MUST be like my vertex struct, and my pixelshader struc MUST be like my VertexShader struct?? is enough for now.... sorry for these silly questions, but when I search in internet, some clues, I can discover only sites where someone tell me like to do something, or sites where someone tell me what shader registry done.... none tell me how works the shader..... __________LAST QUESTION__________ I've find this:

ps_1_4
////////////////////////////////
// r0: alphamaps
// r1 - r4: textures
////////////////////////////////
// Sample textures
texld r0, t0
texld r1, t1
texld r2, t1
texld r3, t1
texld r4, t1
// Combine textures together based off of their alphamaps
blablablablalbalbalba
blablablablalbalbalba
blablablablalbalbalba
I don't undestand it, but I think that its declaration is more clear because seem that this shader say: Hello world! I've 5 textures, the first with coordinates t0, the others with coordinates t1
Advertisement
1. The PS_INPUT is just the data passed from the vertex shader to the pixel shader. The pixel data hasn't been read yet, so there isn't 5xfloat4 of pixel data to pass, just 5xfloat2 coords. The pixel shader will use these coords to read the pixel data.

2. This style is often used when the textures and sampler states are set in code. The texture is set via pDevice->SetTexture(0, pTex). It's implied that the texture is there. When using the FX system you usually just set a texture on the FX object, and your sampler block looks like this:

texture2D diffuseTexture : Diffuse;
sampler2D TextureSampler = sampler_state
{
texture = <diffuseTexture>;
AddressU = CLAMP;
AddressV = CLAMP;
AddressW = CLAMP;
MIPFILTER = LINEAR;
MINFILTER = LINEAR;
MAGFILTER = LINEAR;
};

Using this style allows the FX system to bind the texture and settings to the correct sampler. When using plain shaders, not FX, you must find which sampler is being used and set the texture and sampler states on the correct index by yourself. It's probably in the shader's constant table, but I'm not sure.

3. All this says is that when using a shader don't change D3DTSS_TEXCOORDINDEX. If you do, make sure you set it back such that stage 0 uses index 0, stage 1 uses index 1, stage 2 uses index 2, etc.

You don't need to pad your vertex input data. Pixel input data for SM1 is a bit more complicated, but in SM2 or higher you can just pass 1 texcoord to the pixel shader and keep reusing it if you want. In SM1 you can have 1 texcoord input to your vertex shader, and output it multiple times to the pixel shader. The SM1 pixel shader can only read each texcoord once.

4. One struct is what's coming into your vertex shader, and one struct is what's being output by the vertex shader and input to the pixel shader. The structs used for the vertex shader output and pixel shader input should match. Often the structs are similar, but not always.

5. That's the assembly code way to sampling 5 textures. They're reading the texture using 2 texcoord inputs, and storing it in one of five registers. It's like 5 tex2D(sampler, coord) statements in a row. The first using a different set of texcoord than the remaining 4.
1)Ok,I've understood.. thank.. it's very useful

2)well.. I don't think that is in costanttable... there is not a field for texture identifyer
typedef struct _D3DXSHADER_CONSTANTTABLE {    DWORD Size;    DWORD Creator;    DWORD Version;    DWORD Constants;    DWORD ConstantInfo;} D3DXSHADER_CONSTANTTABLE;


maybe follows the order??
sampler2D Texture2DA; ---> is linke by SetTexture(0, pTex).
sampler2D Texture2DB; ---> is linke by SetTexture(1, pTex).
sampler2D Texture2DC; ---> is linke by SetTexture(2, pTex).
sampler2D Texture2DD; ---> is linke by SetTexture(3, pTex).

I'll do some tests...

3)ok.. then if I use (with M2 or higher) more times the same texcoord,
I must be sure that there is a link between 'sampler2D texureN' and the
'SetTexture(N, ptex)...
in this awy 'float4 texture1 = tex2D(Texture2DA, In.TexCoord1);'
give me a different value than 'float4 texture1 = tex2D(Texture2DB, In.TexCoord1);'

before, I thought that TexCoor_N saw only value in tenture_N

4)perfect
5)like above

thanks Namethatnobodyelsetook, now all is more clearly.
At this point I must learn pnly how my textures_stages are linked at pixel shader variables....

Quote:Original post by BlueChip
2)well.. I don't think that is in costanttable... there is not a field for texture identifyer
typedef struct _D3DXSHADER_CONSTANTTABLE {    DWORD Size;    DWORD Creator;    DWORD Version;    DWORD Constants;    DWORD ConstantInfo;} D3DXSHADER_CONSTANTTABLE;
ID3DXConstantTable::GetSamplerIndex() is what you want. This will translate your name "Texture2D" in the FX file to the index (e.g. '0') you need to use in a SetTexture() call in your code.

Quote:Original post by BlueChip
maybe follows the order??
sampler2D Texture2DA; ---> is linke by SetTexture(0, pTex).
sampler2D Texture2DB; ---> is linke by SetTexture(1, pTex).
sampler2D Texture2DC; ---> is linke by SetTexture(2, pTex).
sampler2D Texture2DD; ---> is linke by SetTexture(3, pTex).
No, this is incorrect.

Assuming anything about these indices is dangerous as the compiler has free reign to assign them to whichever sampler slot it wants. You might, due to dead-code elimination, not even have the sampler being used.


hth
Jack

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

thanks jollyjeffers, finally, now all is clear...

now I go to add

SetTexture((GetSamplerIndex(mytexturen),n)

and I'll see if I've realy underst00d...
dark... I don't see the light again...

I've tried, but I've got a surprise....

ID3DXConstantTable::GetSamplerIndex doesn't exist :(

there was, but now ID3DXConstantTable has:
GetBufferPointer GetBufferSize GetConstant GetConstantByName GetConstantDesc GetDesc SetBoolArray SetFloatArray SetInt SetIntArray SetMatrix SetMatrixArray SetMatrixPointerArray . SetMatrixTranspose SetMatrixTransposeArray SetMatrixTransposePointerArraySetValue  SetVectorSetVectorArray 


probably I've not understood jollyjeffers's explaining....
someone can give me more clues...

thanks again

Odd. It's in my SDK (Aug 06) help. It's in my SDK .h files. It's in the 360 SDK too. I don't have the latest PC SDK around to check if they dropped it at some point, but I don't think so. It's here, which is the June07 docs.

What SDK are you using? Do the SDK .h/.lib files get used before the ones shipped with your compiler (move them to the top of the list)?
I see.... very strange...

I use june 2007 SDK... and object 'ID3DXConstantTable' exists...
but if I try to call 'GetSamplerIndex' I get this

error C2039: 'GetSamplerIndex' : is not a member of 'ID3DXConstantTable'

mmmm... I'll install SDK again...

This topic is closed to new replies.

Advertisement