• 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
BenS1

How to use Texture Samplers

12 posts in this topic

Somehow I seem to have missed a couple of the simpler subjects despite reading every DirectX 11 book I can find. Todays subject is Texture Samplers.

I've successfully created Samplers in code in the past using code like this:

[CODE]
// Create the sample state
D3D11_SAMPLER_DESC sampDesc;
ZeroMemory( &sampDesc, sizeof(sampDesc) );
sampDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
sampDesc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP;
sampDesc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP;
sampDesc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP;
sampDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;
sampDesc.MinLOD = 0;
sampDesc.MaxLOD = D3D11_FLOAT32_MAX;
DXCall(g_pd3dDevice->CreateSamplerState( &sampDesc, &m_pSamplerLinear));
[/CODE]

However, its my understanding that you can define samplers in your HLSL code instead of your C++ code. So for example I could have the following in my HLSL:

[CODE]
SamplerState permSampler2d
{
Filter = MIN_MAG_MIP_POINT;
AddressU = WRAP;
AddressV = WRAP;
};
[/CODE]

The problem is, when I define the sampler in my HLSL code I always get the following Debug messages in Visual Studio:
[CODE]
449 : ID3D11DeviceContext::DrawIndexed: The Pixel Shader unit expects a Sampler to be set at Slot 0, but none is bound. This is perfectly valid, as a NULL Sampler maps to default Sampler state. However, the developer may not want to rely on the defaults.
449 : ID3D11DeviceContext::DrawIndexed: The resource return type for component 0 declared in the shader code (FLOAT) is not compatible with the Shader Resource View format bound to slot 0 of the Pixel Shader unit (UINT). This mismatch is invalid if the shader actually uses the view (e.g. it is not skipped due to shader code branching).
[/CODE]

And any attempt to use the sampler in my Pixel Shader always returns black:

[CODE]
float4 PS(VertexOut pin) : SV_Target
{
float4 colour = permTexture2d.SampleLevel(permSampler2d, pin.TextureUV, 0);
return colour;
}
[/CODE]

I've confirmed from the Visual Studio 2012 Graphics Debugger that the texture is correct at this point, and the TextureUV co-ords are correct so I can only guess I'm doing something wrong with the sampler.

I feel that I'm missing something obvious here, but I don't know what.

Any help would be appreciated.

Thanks
Ben
P.S. Note that I'm not using the Effects framework. I presume defining Samplers in HLSL isn't something that only possible through Effects? Edited by BenS1
0

Share this post


Link to post
Share on other sites
[quote name='Zaoshi Kaba' timestamp='1352063954' post='4997295']
I think Samplers are part of Effects Framework. I'm not sure why that code even compiles.
[/quote]

Ok thanks. So I preusme I have to create my samplers through C++ code as per:
[CODE]
// Create the sample state
D3D11_SAMPLER_DESC sampDesc;
ZeroMemory( &sampDesc, sizeof(sampDesc) );
sampDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
sampDesc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP;
sampDesc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP;
sampDesc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP;
sampDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;
sampDesc.MinLOD = 0;
sampDesc.MaxLOD = D3D11_FLOAT32_MAX;
DXCall(g_pd3dDevice->CreateSamplerState( &sampDesc, &m_pSamplerLinear));
[/CODE]

All the DirectX 11 books I've got are great, but I wish there was a DirectX 11 book out there that covers real DirectX and not DirectX hidden by the Effects Framework\D3DX\DXUT etc.

I guess that now that stuff has gone in the latest Windows SDK version of DirectX any future books will cover pure DirectX. Hopefully they won't all assume that we only want to write Metro style games for Win 8.

Thanks for your help.
Ben
0

Share this post


Link to post
Share on other sites
[quote name='Zaoshi Kaba' timestamp='1352063954' post='4997295']
I think Samplers are part of Effects Framework. I'm not sure why that code even compiles.
[/quote]

Yes, declaring sampler states this way requires using Effects. It compiles because the shader compiler also handles compiling effects, so some of the effect syntax is part of HLSL (which is pretty unfortunate, IMO).
2

Share this post


Link to post
Share on other sites
The effects framework is now open-source. I haven't looked at the code yet, but surely you could copy the relevant part that parses effect files?
EDIT: Oh, but the compiler is not open-source :( In that case, the best I can think of is that you write your own parser that reads the file and generates sampler states. Painful. Edited by hupsilardee
1

Share this post


Link to post
Share on other sites
Thanks MJP and hupsilardee,

Ok, its nto a big problem, I was just trying to save a little code on the C++ side, and having seen the Samplers defined in other HLSL code I just assumed it was part of HLSL.... now that I know its actually part of the Effects framework I'll just go back to creating the samplers from C++. Not a big problem.

Thanks again
Ben
0

Share this post


Link to post
Share on other sites
Ok, I now create and set the samplers from my C++ code... but now I'm getting these errors:

D3D11: ERROR: ID3D11DeviceContext::DrawIndexed: The Shader Resource View in slot 1 of the Pixel Shader unit is using the Format (R8G8B8A8_UINT). This format does not support 'Sample', 'SampleLevel', 'SampleBias' or 'SampleGrad', at least one of which may being used on the Resource by the shader. This mismatch is invalid if the shader actually uses the view (e.g. it is not skipped due to shader code branching). [ EXECUTION ERROR #371: DEVICE_DRAW_RESOURCE_FORMAT_SAMPLE_UNSUPPORTED ]

D3D11: ERROR: ID3D11DeviceContext::DrawIndexed: The resource return type for component 0 declared in the shader code (FLOAT) is not compatible with the Shader Resource View format bound to slot 1 of the Pixel Shader unit (UINT). This mismatch is invalid if the shader actually uses the view (e.g. it is not skipped due to shader code branching). [ EXECUTION ERROR #361: DEVICE_DRAW_RESOURCE_RETURN_TYPE_MISMATCH ]

So if I can't use SampleLevel to read from my textures then what do I use?

Note that I'm trying to convert the Perlin Noise code from GPU Gems 2 to DirectX 11. The original code was DirectX 9 and used "tex2Dlod" to sample the textures, but apparently this is deprecated in DirectX 11.

Thanks again for any help.
Ben
0

Share this post


Link to post
Share on other sites
You Sample from floating point textures and you Load from integer textures.

Sample : [url="http://msdn.microsoft.com/en-us/library/windows/desktop/bb509695%28v=vs.85%29.aspx"]http://msdn.microsoft.com/en-us/library/windows/desktop/bb509695%28v=vs.85%29.aspx[/url]

Load : [url="http://msdn.microsoft.com/en-us/library/windows/desktop/bb509694%28v=vs.85%29.aspx"]http://msdn.microsoft.com/en-us/library/windows/desktop/bb509694%28v=vs.85%29.aspx[/url]
2

Share this post


Link to post
Share on other sites
You could try to use R8G8B8A8_FLOAT format (or higher accuracy), or possibly use [url="http://msdn.microsoft.com/en-us/library/windows/desktop/bb509694(v=vs.85).aspx"]Load[/url] function to load specific bytes your UINT texture.
1

Share this post


Link to post
Share on other sites
Thanks Gavin and Zaoshi,

Looking at the documentation for Load it looks like the texture co-ords are Ints, which is a little odd as texture co-ords are normally between 0 and 1.

Does the Load function support wrapping etc (The documentation doesn't say)?

Actually, just changing my texture formats from DXGI_FORMAT_R8G8B8A8_UINT to DXGI_FORMAT_R8G8B8A8_UNORM and from DXGI_FORMAT_R8_UINT to DXGI_FORMAT_R8_UNORM seems to have fixed the problem.

Out of interest, what's the difference between DXGI_FORMAT_R8_UNORM and DXGI_FORMAT_A8_UNORM?

Thanks
Ben

.
0

Share this post


Link to post
Share on other sites
The coords for the <uint> texture are indeed integer based. I'm not sure if they've done it that way for clarity. If you think about it, sampling probably just uses integer based accesses beneath the hood and lerps the values. But then .. couldn't integers be interpolated as well ! I guess it's the case that the common scenarios have influenced the design here.

[quote]Does the Load function support wrapping etc (The documentation doesn't say)?[/quote]
From the documentation :
Note When one or more of the coordinates in Location exceed the u, v, or w mipmap level dimensions of the texture, Load returns zero in all components. Direct3D guarantees to return zero for any resource that is accessed out of bounds.

So I guess it's like border addressing, but with only zero as the border value. I'm assuming that u & v are the standard texture coords (in this case de-normalized) and w is the mip-map level. So if my assumption is correct, then there is no wrapping.

[quote]Out of interest, what's the difference between DXGI_FORMAT_R8_UNORM and DXGI_FORMAT_A8_UNORM?[/quote]

I don't know, I see them as the same thing in practice but then I'm not familiar with a whole range of techniques and so there could be some part of the pipeline that requires it that way. My best guess is that it's simply for clarity
2

Share this post


Link to post
Share on other sites
For Load the coordinates are integer based because you don't get any filtering. You're explicitly specifying which texel you want to sample, so for instance 10.1 and 10.9 would both return the same the same texel (since texels are centered at 0.5 increments). Load isn't something you use for typical UV mapping scenarios, where you want filtering applied to the result.

The difference between R8_UNORM and A8_UNORM is that R8_UNORM will return the texel value in the red component of the sample, while A8_UNORM will return the value in the alpha channel. There's no difference in how the data is stored in the texture, it only affects how the texture is sampled.
2

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