• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
IkarusDowned

Sampling Texture2D vs Texture1D?

6 posts in this topic

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?

0

Share this post


Link to post
Share on other sites

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
2

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

@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

0

Share this post


Link to post
Share on other sites

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!)

1

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  
Followers 0