Sign in to follow this  
IkarusDowned

Sampling Texture2D vs Texture1D?

Recommended Posts

IkarusDowned    291

Hello guys, 

I've run into another kinda interesting bug which I'm having trouble figuring out.

I'm trying to do some (albeit strange) lighting calculations where the point light diffuse, radius and position are baked (in the case of radius and position, encoded) into 1D textures. As such, I've created 1D textures like so:

bool CreateTexture1DClass::Initialize(ID3D11Device *device, std::vector<UINT8> &perChannelData, int width)
{
	m_width = width;
	D3D11_TEXTURE1D_DESC desc;
	ZeroMemory(&desc, sizeof(desc));

	desc.Width = m_width;
	desc.MipLevels = desc.ArraySize = 1;
	desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
	desc.MiscFlags = 0;
	desc.Usage = D3D11_USAGE_DYNAMIC ;
	desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
	desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;


	HRESULT result;

	D3D11_SUBRESOURCE_DATA subresource;
	subresource.SysMemSlicePitch = 0;
	subresource.SysMemPitch = perChannelData.size() * sizeof(UINT8);
	subresource.pSysMem = &perChannelData[0];

	result = device->CreateTexture1D(&desc, &subresource, &m_pTexture);
	if(FAILED(result))
	{
		return false;
	}

	D3D11_SHADER_RESOURCE_VIEW_DESC shaderResourceViewDesc;
	shaderResourceViewDesc.Format = desc.Format;
	shaderResourceViewDesc.Texture1D.MostDetailedMip = 0;
	shaderResourceViewDesc.Texture1D.MipLevels = 1;
	shaderResourceViewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE1D;
	result = device->CreateShaderResourceView(m_pTexture, &shaderResourceViewDesc, &m_shaderResourceView);
	if(FAILED(result))
	{
		return false;
	}
	return true;
}

Now, for testing purposes I pre-load the color and radius into textures, then outputted them as 1D DDS files to see if my logic was right. The good news here is, it worked!

 

Now, onto using them in my shaders. As I said I'm trying to do something (possibly) weird, which is to render the point-light diffuse values to a texture as part of the deferred step, but sampling the values in the domain shader (as I have new vertices being made and such). Here's how I've declared my textures and the samplers:

Texture2D tex; 
Texture2D normalMap; 
Texture2D specularMap; 

Texture2D displacementMap; 
Texture1D pointLightDiffuseMap;
Texture1D pointLightPositionMap;
Texture1D pointLightRadiusMap;

SamplerState PixelSampler;
SamplerState DomainSampler;

in my domain shader for now, I'm just doing the following:

output.pointDiffuseAdd = pointLightDiffuseMap.SampleLevel(DomainSampler, 0, 0);

this essentially gets piped straight thru to the PixelOutput, so essentially i get a deferred render target which is filled with a flat color. 

Now, The problem is this: The pointLightDiffuseMap at the moment is just a 1D texture which has two values: Red, Blue. If my understanding is correct, with the SampleLevel's second parameter being 0, I should get back a full-red image. However, what I get back is a deep shade of purple! (it /almost/ looks like its blending the red and blue). Any ideas as to what could cause this? Is my sampler states setup wrong?

 

Here's a sampler state setup:

D3D11_SAMPLER_DESC samplerDesc;
	samplerDesc.Filter = D3D11_FILTER_MIN_POINT_MAG_MIP_LINEAR;
	samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
	samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
	samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
	samplerDesc.MipLODBias = 0.0f;
	samplerDesc.MaxAnisotropy = 1;
	samplerDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
	samplerDesc.BorderColor[0] = 0;
	samplerDesc.BorderColor[1] = 0;
	samplerDesc.BorderColor[2] = 0;
	samplerDesc.BorderColor[3] = 0;
	samplerDesc.MinLOD = 0;
	samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;

device->CreateSamplerState(&samplerDesc, &m_displacementSampler);

P.S. i do check the result to make sure I don't error, I just don't put it here to reduce code-confusion

 

also, here's how I'm setting my samplers / textures into their respective locations:

deviceContext->PSSetShaderResources(0, 1, &diffuseTexture);
deviceContext->PSSetShaderResources(1, 1, &normalMap);
deviceContext->PSSetShaderResources(2, 1, &specularMap);

deviceContext->DSSetShaderResources(0, 1, &displacementMap);
deviceContext->DSSetShaderResources(1, 1, &pointLightDiffuseMap);
deviceContext->DSSetShaderResources(2, 1, &pointLightPositionMap);
deviceContext->DSSetShaderResources(3, 1, &pointLightRadiusMap);

Oddly enough, none of the other code seems effected by the weirdness happening from the 1D texture sampling issue. Any ideas as to what I could be doing wrong?

Share this post


Link to post
Share on other sites
n3Xus    951

If I understand correctly... :

 

Your 1D texture has 4 components (4d vector). ( desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; -> R,G,B,A)

If you sample at location 0 it will read the 4D vector. That 4d seems to be (1,0,1,0) (R and G are 1).

RGB components are not separated, they are combined.

 

So when you mixed them you get purple.

 

You need sampleLevel(sampler,0,....).x to get red.

 

SampleLevel should be used if you need a specific mip-map, it has nothing to do with RGB channels.

 

EDIT: you should use the default sample function, not sampleLevel if you don't need mip-maps.

Edited by n3Xus

Share this post


Link to post
Share on other sites
MJP    19753

EDIT: you should use the default sample function, not sampleLevel if you don't need mip-maps.

 

SampleLevel with lod == 0 is totally fine if you don't want mipmap sampling. In fact it looks like he's sampling in a domain shader, in which case using Sample isn't even an option at all since it relies on pixel quad derivatives.

Share this post


Link to post
Share on other sites
IkarusDowned    291

@MJP

Its a tiny 1-d texture so I don't do mipmap sampling. Yes, I'm doing it in the Domain Shader because of something I'm trying out which needs access to the vertex location properties.

 

@n3xus:

As MJP points out, the sampling is happening in the domain shader so I can't use the default Sample function. 

 

To clarify, I don't want the component value of the RGBA texture value at location 0, i want the ENTIRE value.

The 4-d rgba value is (1,0,0,1) where if I do SampleLevel(DomainSampler, 0, 0) and just send that value straight to the Pixel Shader, it should produce red.

I'll double check the texture, but the output I get when i cut it to file seems right. 

 

The input to the texture is the following:

(1,0,0,1) and (0,0,1,1)

So, I have two pixels where the first one is RGBA of Red, the second of Blue, and i want the color value in its whole (not as components, aka JUST the x or y value).

I don't understand how, but is it possible that SampleLevel(DomainSampler, 0, 0) is  somehow sampling with the wrong channel bit size? I've set the shader resource view to use the same channel bit information

Share this post


Link to post
Share on other sites
IkarusDowned    291

Once again, chatting with you all helped me solve the issue.

A couple things seemed to be needed for me to solve this:

 

1) I changed the Filter type to MIN_MAG_MIP_POINT. linear interpolated across the colors (which...is what it does...)

2) For my usage, I needed to switch the Addressing to CLAMP. 

 

I don't know if this mattered, but I also:

3) moved the three Texture1D declarations in the shader to an array, and did the DSSetShaderResource as an array...this probably didn't change much but I /think/ is a better approach

 

Also, I double-checked all the channel values and hand-calculated a bunch to make sure that what I was seeing in the SaveDDSToFile call was in fact what I was inputting -- as it turns out the DDS viewer I am using is a bit weird and unreliable...best to just do the math by hand.

 

Now, the calculations all seem correct and i'm getting some interesting stuff being done using the 1D textures.

Thanks again (and double thanks to MJP who helped me out twice in 2 weeks!)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this