Lookup tables using Textures

Started by
19 comments, last by nara 14 years, 12 months ago
One more thing to say:

First, a question: Is your 1280x1024 lookup texture just storing sampling coords for the other 1920x1080 texture?

If "yes", listen:

I think you don't need to use/create a lookup texture. You can perform your conversion and use the results as sampling coords in pixel shader.

But one important thing: Texcoords of a pixel(x, y) must be (x-0.5, y-0.5). For example, I have a 640x480 texture. If I want to get the value in the pixel (120, 58), I must use that line: tex2D (smpSampler, 119.5, 57.5); This line gives me the value of the pixel (120, 58).

Also you can look at "Directly mapping texels to pixels" in the SDK Documentation.

Regards,
Rohat.
There's no "hard", and "the impossible" takes just a little time.
Advertisement
Hi Rahat,

A big thanks to you for the help. I changed my code accordingly and saw some output appear. For some strange reason after the second time i'm getting only a blank red screen. I know this might sound stupid but that is what is getting outputted.

Yes the 1280x1024 is going to store the coords for 1920x1080 texture. Since i need to compute the various chanel in similar ways i thought it might be better of to store them as look up tables then computing at run time. But i will defntiely look into the sdk area mentioned by you. Can you also look at the code below and let me know if anything looks fishy for the red screen.

VS Shader - None

PS Shader
PS_OUTPUT Main_PS(float2 ScreenPosition : TEXCOORD0,
sampler View1TexSampler,
sampler View2TexSampler,
sampler View3TexSampler) : COLOR0
{
float2 R_Cord =0;

R_Cord.x = tex2D(View2TexSampler,ScreenPosition);//.xy;
R_Cord.y = tex2D(View3TexSampler,ScreenPosition);//.xy;
FinalColor.r = tex2D(View1TexSampler,R_Cord).r;
output.diffuse = FinalColor;
return output;
}

CPP Code
[source lang ="cpp"]hr = D3DXCreateTexture(g_pd3dDevice,1280,1024,1,D3DUSAGE_DYNAMIC,D3DFMT_G16R16F,D3DPOOL_DEFAULT ,&R_MapXTex);  		D3DLOCKED_RECT pLockedRect;	hr= R_MapXTex->LockRect(0,&pLockedRect,0,0);	BYTE* pBits = (BYTE*)pLockedRect.pBits;	for (UINT y = 0; y < 1024; y++)	{		D3DCOLOR* CurPoint = (D3DCOLOR*)(pBits + pLockedRect.Pitch * y);		for (UINT x = 0; x < 1280; x++)		{ 			CurPoint[x] = ((x*1.5) / 960)-1;		}	}R_MapXTex->UnlockRect (0);hr = D3DXCreateTexture(g_pd3dDevice,1280,1024,1,D3DUSAGE_DYNAMIC,D3DFMT_G16R16F,D3DPOOL_DEFAULT ,&R_MapXTex1);  	D3DLOCKED_RECT pLockedRect1;			hr= R_MapXTex1->LockRect(0,&pLockedRect1,0,0);	BYTE* pBits1 = (BYTE*)pLockedRect1.pBits;	for (UINT y = 0; y < 1024; y++)	{	D3DCOLOR* CurPoint = (D3DCOLOR*)(pBits1 + pLockedRect1.Pitch * y);	for (UINT x = 0; x < 1280; x++)	{ 	  CurPoint[x] = ((y) / 540)+1;	 }	}R_MapXTex1->UnlockRect (0);


We can't use r & g directly for curPoint as suggested by you earlier. Hence i have passed Y values in the second texture lookup.

Thanks once again.

Ok, if you cannot use D3DCOLOR when casting, just change it to D3DXCOLOR. This type has r, g, b and a channels (I suspected that D3DCOLOR has rgba channels but I've no time to try this). Now, you can write seperate values to seperate channels ;)

Also, you're writing only .r channel of the output in pixel shader:
FinalColor.r = tex2D(View1TexSampler,R_Cord).r;output.diffuse = FinalColor;


What about the other channels?
There's no "hard", and "the impossible" takes just a little time.
Hi Rohat,

I changed that and tried. As far as the other channels are concerened i'm currently passing the same coordinates right now. So my output in the PS looks like this
FinalColor.r = tex2D(View1TexSampler,R_Cord).r;
FinalColor.g = tex2D(View1TexSampler,R_Cord).g;
FinalColor.b = tex2D(View1TexSampler,R_Cord).b;
Output.diffuse = (float4)(FinalColor,1.0f);

But inspite of this i'm unable to get any output. I'm not sure where i'm going wrong. My suspect is the x & Y conversations which i'm doing.
If you have any thoughts please let me know.

Thanks,
Nara
Hi Still no luck. One more question are we suppose to set sampler states for textures created for lookup tables as well.

Hi Nara,

Sorry I couldn't answer for a long time, I've got a lot of work :(

I think that the values that you convert/get maybe too big for a channel of D3DXCOLOR while writing to look-up texture. So, that channel could not store this, I think.

Try like following:
hr= R_MapXTex->LockRect(0,&pLockedRect,0,0);BYTE* pBits = (BYTE*)pLockedRect.pBits;for (UINT y = 0; y < 1024; y++){	D3DCOLOR *CurPoint =(D3DCOLOR*)(pBits + pLockedRect.Pitch * y);	for (UINT x = 0; x < 1280; x++)	{ 	CurPoint[x].r = (((x*1.5) / 960)-1) / 2048; // converting 1920 coords 	CurPoint[x].g = (((y) / 540)+1) / 2048; // converting 1080 coords        //Store in r and g channels	}}R_MapXTex->UnlockRect (0);


Look at lines that I wrote to r and g channels; I just divided to 2048 to get a value between [0, 1]. Then in your shader, after sampling this, you sould multiply that value with 2048.

I think now you can get the correct results.

Hope this WORKS :)

Regards,
Rohat.
There's no "hard", and "the impossible" takes just a little time.
Hi Rohat,

Thanks for the information, but i get only a red screen. Accordingly i have made the following changes.

[source lang ="cpp"]hr = D3DXCreateTexture(g_pd3dDevice,1280,1024,1,					D3DUSAGE_DYNAMIC,D3DFMT_G16R16F,D3DPOOL_DEFAULT ,&R_MapXTex);	D3DLOCKED_RECT pLockedRect;	hr= R_MapXTex->LockRect(0,&pLockedRect,0,0);	BYTE* pBits = (BYTE*)pLockedRect.pBits;	for (UINT y = 0; y < 1024; y++)	{		D3DXCOLOR* CurPoint = (D3DXCOLOR*)(pBits + pLockedRect.Pitch * y);//		float *CurPoint = (float*)(pBits + pLockedRect.Pitch * y);		float scale =1.5;		for (UINT x = 0; x < 1280; x++)		{ 			CurPoint[x].r = (((x*scale)/ 960)-1)/2048;		}	}R_MapXTex->UnlockRect (0);hr = D3DXCreateTexture(g_pd3dDevice,1280,1024,1,					D3DUSAGE_DYNAMIC,D3DFMT_G16R16F,D3DPOOL_DEFAULT ,&R_MapXTex1);  	D3DLOCKED_RECT pLockedRect1;			hr= R_MapXTex1->LockRect(0,&pLockedRect1,0,0);	BYTE* pBits1 = (BYTE*)pLockedRect1.pBits;	for (UINT y = 0; y < 1024; y++)	{		D3DXCOLOR* CurPoint = (D3DXCOLOR*)(pBits1 + pLockedRect1.Pitch * y);//		float* CurPoint = (float*)(pBits1 + pLockedRect1.Pitch * y);		for (UINT x = 0; x < 1280; x++)		{ 			CurPoint[x].r = (-((y)/ 540)+1)/2048;		}	}R_MapXTex1->UnlockRect (0);


Also i have changed the PS as follows

PS_OUTPUT Main_PS(float2 ScreenPosition : TEXCOORD0,
sampler View1TexSampler,
sampler View2TexSampler,
sampler View3TexSampler,
PS_INPUT input) : COLOR0
{
// zero out members of output
PS_OUTPUT output = (PS_OUTPUT)0;

float3 FinalColor =0;
float2 R_Cord =0;

R_Cord.x = ((float)tex2D(View2TexSampler , ScreenPosition ).r)*2048;
R_Cord.y = ((float)tex2D(View3TexSampler , ScreenPosition ).r)*2048;

FinalColor.r = tex2D(View1TexSampler , R_Cord ).r;

}

All I'm now seeing a red screen instead of a black screen. Do you think i need to try with a different scale number.

Thanks for all the help.

Rohat,

One more thing. I have been trying to debug the code through PIX and for some reason it fails at Compileshaderfile. I have changed from Retail code generation to Debug code generation and enabled shader debugging in the DirectX Control panel.

This is the output i get
Frame 000001 ........PRE: D3DXCompileShaderFromFileA(0x0048FDF8, NULL, NULL, 0x0048FE0C, 0x0048FE18, 0x00000001, 0x0012FD5C, 0x0012FD50, 0x004A1D1C)
Frame 000001 ........POST: <E_FAIL> D3DXCompileShaderFromFileA(0x0048FDF8, NULL, NULL, 0x0048FE0C, 0x0048FE18, 0x00000001, 0x0012FD5C, 0x0012FD50, 0x004A1D1C)
An unhandled exception occurred.

The same code executes when run through VS2005 under the same conditions of DirectX Control panel.

Do you have any thoughts.

Nara
Nara,

About your last message...
I've no idea about debugging /w PIX, because I don't use it. Sorry.

About your another message...
I know you got mad at me :)
I see that you're doing it all correct. But;
a-) Your conversion algorithm maybe wrong
b-) Maybe we should use a different scaling factor.

I can't say anything about (a), you know the best. But about (b), we want the value in r. and/or .g channels to be in [0, 1]. You can try higher values (e.g. 4096).
There's no "hard", and "the impossible" takes just a little time.
Oops!

I Thought I Saw A Pussy Cat :)

I've just looked your shader; you're using a sampler as input parameter. Is your sampler containing any texture data? I think, no!

Look, did you do like following?:
sampler View1TexSampler = sampler_state{  texture = <YourView1Tex>;  MinFilter = POINT;  MagFilter = POINT;  MipFilter = NONE;  AddressU = CLAMP;  AddressV = CLAMP;};sampler View2TexSampler = sampler_state{  texture = <YourView2Tex>;  MinFilter = POINT;  MagFilter = POINT;  MipFilter = NONE;  AddressU = CLAMP;  AddressV = CLAMP;};etc.


If your answer is "no", first you should declare'em correctly.
I hope this is the only mistake.

Rohat.
There's no "hard", and "the impossible" takes just a little time.

This topic is closed to new replies.

Advertisement