• 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
Tim Coolman

Blurred Texture Sampling Minification

4 posts in this topic

I am writing a program in C++ using DirectX 11. As part of my user interface, I want to display buttons whose background texture consists of a rectangle with rounded edges. However, I want to be able to make these buttons of various size, without having to maintain the same aspect ratio of the source texture. But I also don't want to corners to be squished or stretched when changing the size. Below is my original source image that I'm using for my texture/shader resource. Opaque black border and opaque white filling. The small area outside the border's corners is transparent.

[attachment=9819:Button Background Template.png]

To address this, instead of using a simple quad of four vertices and mapping the texture directly to the four corners, I created a surface consisting of a 4x4 grid of vertices. The idea being that as I change the size of my button, I can maintain the original dimensions of the four corners, and just stretch or shrink the edges and center areas.

I've tried to give a representation of what I'm talking about in the image below. The values on the left are texture 'Y' coordinates (the 'X' coordinates would be the same going from left to right). I'm also showing that I've given a color to the vertices as well (because in my pixel shader I am blending the vertex color with the sampled texture color).

[attachment=9822:Button Background Template Description.png]

As you can see from the images below, my plan works great when I expand the size of the button. However, if I shrink the size of the button relative to the original texture, the corners look correct, but the edge portions of the texture sampling gives a blurred result. (Ignore that the gradient isn't linear from top to bottom - I am aware of the reason for this. My focus is on the blurring)
(The original texture shown above is 100x100 pixels. The expanded button's dimensions are 400x200; the shrunk button's dimensions are 75x42).


I am confident that the issue is not with the blending that I'm doing - I removed that portion from the shader so I was doing a simple texture sample, but the blurred edges were still there.

I am fairly new to texture sampling, so I'm thinking maybe I have something wrong in my sample state that is causing the blur.

Here is my current sampler setup:
[source lang="cpp"] D3D11_SAMPLER_DESC samplerDescription;
ZeroMemory(& samplerDescription, sizeof(samplerDescription));
samplerDescription.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
samplerDescription.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP;
samplerDescription.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP;
samplerDescription.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP;
samplerDescription.ComparisonFunc = D3D11_COMPARISON_NEVER;
samplerDescription.MinLOD = 0;
samplerDescription.MaxLOD = D3D11_FLOAT32_MAX;[/source]
And if it helps, here is my Pixel Shader:
[source lang="cpp"]float4 PixelShaderTextureBlendHUD(PixelShaderInput pixelIn) : SV_Target
// Sample the texture for a color at the interpolated texture coordinate.
float4 outColor = DiffuseMap.Sample(SampleLinear, pixelIn.TextureCoordinate);

// Do multiplicate blending of the interpolated vertex color with the sampled color.
outColor *= pixelIn.Diffuse;

// If constant buffer 'IsGrayscale' flag is set, calculate the grayscale version of the color.
if(IsGrayscale > 0)
float luminance = outColor.x * 0.3f + outColor.y * 0.59f + outColor.z * 0.11f;
outColor = float4(luminance, luminance, luminance, outColor.w);

return outColor;

What might be causing the sampler to blur the image this way when the display area is smaller than the are of the texture being sampled? I tried using a Point sampler instead of Linear - that looked slightly different, but just as bad.

Any tips would be greatly appreciated!

Share this post

Link to post
Share on other sites
The problem is likely caused by the fact that the screen-space texture coordinate derivatives are different in the x and y direction. This causes the GPU to sample from lower mip level than would be needed with the larger derivative's dimension. In some cases, you can configure the behavior with the "texture quality" slider in your GPU driver's property pages - higher quality tends to favour the larger texture levels.

If your boxes have 1:1 relation to screen pixels, you could try to disable mip filtering altogether.

If you do need filtering, try to enable anisotropic mip filtering. Anisotropic filtering takes several samples along the vector defined by the texcoord derivatives and blends them so that you effectively get the actual maximum detail level at each pixel.

The maximum number of anisotropic samples is configurable by using the sampler state; if you use a lot of anisotropic samples, you get more accurate results at the expense of effective fillrate. The maximum number of samples is a hint to the GPU; it can still use less samples if it determines that the maximum amount is not needed (this is commonly called "anisotropic optimization"). Edited by Nik02

Share this post

Link to post
Share on other sites
To be honest I don't have any experience using MIP filtering. How would I go about turning it off? To use my texture in my pixel shader, I currently create an [i]ID3D11ShaderResourceView[/i] using the [i]D3DX11CreateShaderResourceViewFromFile[/i] function (my image is a PNG file). As I understand, the D3DX11 library is deprecated, but at this point I've yet to learn how else to create a texture from a file for use as an ID3D11ShaderResourceView.

So, using the [i]D3DX11CreateShaderResourceViewFromFile[/i] function, I see the [i]pLoadInfo[/i] parameter does contain a [i]MipLevels[/i] field, however I'm currently passing NULL in that parameter. By default, do multiple MIP levels get created from a single image file? I have not explicitly created various sizes of the same texture - so unless this is done automatically, I don't know why MIP filtering would even be happening with my current code.

Share this post

Link to post
Share on other sites
You want to set the MipLevels field to 1, that way no mip chain will be generated for you. Passing in NULL is the same as passing in 0, which generates a full mip chain according to the docs on that struct.


Share this post

Link to post
Share on other sites
You guys are my heroes. Rather than passing NULL for the [i]pLoadInfo[/i] parameter, I passed in a [i]D3DX11_IMAGE_LOAD_INFO[/i] structure where I only set the [i]MipLevels[/i] value to 1 (left all the rest at defaults). Worked like a charm!

I learn something new everyday. I didn't realize that DirectX would generate MIPs for you. I thought you had to supply your own variations of a texture to setup MIP filtering. Guess I should do some more reading up on MIPs and see what other kinds of trouble I can get myself into ;)

Thanks again.

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