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

Smeared edges pattern using HLSL for color space conversion

10 posts in this topic

I've asked the same question on Stackoverflow but I doubt I'll get a good answer there.

I'm trying to write a YUV to RGB shader in HLSL. Specifically, it converts the Yuv420p format which consists of an N*M plane of Y values, followed by an (N/2)*(M/2) plane of U values and then an (N/2)*(M/2) plane of V values. For example this 1280x720 picture:

lnnKWnt.png

looks like this in YUV format interpreted as an 8-bit, 1280x1080 texture:

[url=http://imgur.com/ndYpPuK]ndYpPuK.png[/URL]

In Direct3D11, I'm loading this as a Texture2D with format R8_UNorm and dimensions 1280x1080. The tricky part is reconstituting the U and V planes, because as you can see, half the lines are on the left side of the texture, and the other half is on the right side (this is simply due to how Direct3D views it as a texture; the lines are simply one after the other in memory). In the shader, I do this like so:

    struct PS_IN
    {
        float4 pos : SV_POSITION;
        float2 tex : TEXCOORD;
    };

    Texture2D picture;
    SamplerState pictureSampler;
    
    float4 PS(PS_IN input) : SV_Target
    {
        int pixelCoord = input.tex.y * 720;
        bool evenRow = (pixelCoord / 2) % 2 == 0;
    
        //(...) illustrating U values:
        float ux = input.tex.x / 2.0;
        float uy = input.tex.y / 6.0 + (4.0 / 6.0);
        if (!evenRow)
        {
            ux += 0.5;
        }
    
        float u = picture.Sample(pictureSampler, float2(ux, uy)).r;
        u *= 255.0;
    
        // for debug purposes, display just the U values
        float4 rgb;
        rgb.r = u;//y + (1.402 * (v - 128.0));
        rgb.g = u;//y - (0.344 * (u - 128.0)) - (0.714 * (v - 128.0));
        rgb.b = u;//y + (1.772 * (u - 128.0));
        rgb.a = 255.0;
    
        return rgb / 255.0;
    }

However, for some strange reason, this seems to produce a weird horizontal pattern of smeared edges:

[url=http://imgur.com/YeJ1C56]YeJ1C56.png[/URL]

Note that if put either true or false as the condition (so that ux is either always or never incremented by 0.5f), the pattern doesn't appear - although of course we get half the resolution. Also note that I did a basically copy-paste C# translation of the HLSL code and it doesn't produce this effect:

[url=http://imgur.com/rYkP1gE]rYkP1gE.png[/URL]

FWIW, I'm using WPF to create the window and initialize Direct3D using its HWND via WindowInteropHelper. The size of the window is set to exactly 1280x720.

Edited by Asik
0

Share this post


Link to post
Share on other sites

What kind of filtering do you have set up on the YUV texture you pass to your pixel shader? Linear, nearest, max anisotropic? And also, does your YUV texture have mipmaps? My first guess is that if you have mipmaps or anisotropic filtering then you might get some weird results because your texture coordinate derivative will be really large between rows, which will cause a super small mip to be selected.

0

Share this post


Link to post
Share on other sites


The size of the window is set to exactly 1280x720.

 

First - I'm completely ignorant of WPF, etc., so this may be an inane comment. However, is that the size of the window, or the size of the client area? I.e., does the size of your backbuffer match the size of presentation area?

0

Share this post


Link to post
Share on other sites

 


The size of the window is set to exactly 1280x720.

 

First - I'm completely ignorant of WPF, etc., so this may be an inane comment. However, is that the size of the window, or the size of the client area? I.e., does the size of your backbuffer match the size of presentation area?

Yes it matches exactly, I've verified this by having the shader output a pattern of alternating lines which end up perfectly aligned with physical pixels.

 

What kind of filtering do you have set up on the YUV texture you pass to your pixel shader? Linear, nearest, max anisotropic? And also, does your YUV texture have mipmaps? My first guess is that if you have mipmaps or anisotropic filtering then you might get some weird results because your texture coordinate derivative will be really large between rows, which will cause a super small mip to be selected.

This is the texture description (in C# using SharpDX):

            var textureDescription = new Texture2DDescription {
                ArraySize = 1,
                BindFlags = BindFlags.ShaderResource,
                CpuAccessFlags = CpuAccessFlags.Write,
                Format = Format.R8_UNorm,
                Width = 1280,
                Height = 720 + 720 / 2,
                MipLevels = 1,
                OptionFlags = ResourceOptionFlags.None,
                SampleDescription = new SampleDescription(1, 0),
                Usage = ResourceUsage.Dynamic
            };

And the sampler state:

            var sampler = new SamplerState(device, new SamplerStateDescription {
                Filter = Filter.ComparisonAnisotropic,
                AddressU = TextureAddressMode.Wrap,
                AddressV = TextureAddressMode.Wrap,
                AddressW = TextureAddressMode.Wrap,
                BorderColor = Color.Black,
                ComparisonFunction = Comparison.Never,
                MaximumAnisotropy = 16,
                MipLodBias = 0,
                MinimumLod = 0,
                MaximumLod = 16,
            });

What do you suggest I change?

0

Share this post


Link to post
Share on other sites

Ahh, I would suggest changing MaximumAnisotropy in your sampler state from 16 to 1.

 

EDIT: And possibly the filter to Filter.MinMagMipPoint (not sure if that's what it's called in C#, it's D3D11_FILTER_MIN_MAG_MIP_POINT in C++), if all you want to do is convert an image then you don't really need (or want) texture filtering.

 

EDIT2: Some context on why I think anisotropic filtering is your problem: anisotropic filtering is used to filter out aliasing artifacts that occur when a surface is viewed at an oblique angle. The GPU can tell when a surface is at an oblique angle if one of the texture coordinate derivatives is large and the other is small, so in your case when you change the .x coordinate by .5 every other row, it's tricking the gpu into thinking your surface is at a really oblique angle, so it's doing a lot of anisotropic filtering (essentially blurring) to remove high frequency data that it thinks is going to show up as aliasing artifacts. But really in your case you aren't viewing the surface at an oblique angle, and the filtering is unnecessary and counter productive.

Edited by Samith
1

Share this post


Link to post
Share on other sites

By far the simplest option to get this right is to split the data into three R8 textures - one for each channel.

 

This lets you use hardware texture filtering, which should look better than point filtering. It also means you can easily apply the result to arbitrary geometry.

0

Share this post


Link to post
Share on other sites

Ahh, I would suggest changing MaximumAnisotropy in your sampler state from 16 to 1.

 

EDIT: And possibly the filter to Filter.MinMagMipPoint (not sure if that's what it's called in C#, it's D3D11_FILTER_MIN_MAG_MIP_POINT in C++), if all you want to do is convert an image then you don't really need (or want) texture filtering.

 

EDIT2: Some context on why I think anisotropic filtering is your problem: anisotropic filtering is used to filter out aliasing artifacts that occur when a surface is viewed at an oblique angle. The GPU can tell when a surface is at an oblique angle if one of the texture coordinate derivatives is large and the other is small, so in your case when you change the .x coordinate by .5 every other row, it's tricking the gpu into thinking your surface is at a really oblique angle, so it's doing a lot of anisotropic filtering (essentially blurring) to remove high frequency data that it thinks is going to show up as aliasing artifacts. But really in your case you aren't viewing the surface at an oblique angle, and the filtering is unnecessary and counter productive.

You're my hero! That was it and it's a logical explanation of the phenomenon. smile.pngsmile.pngsmile.png

0

Share this post


Link to post
Share on other sites

By far the simplest option to get this right is to split the data into three R8 textures - one for each channel.

 

This lets you use hardware texture filtering, which should look better than point filtering. It also means you can easily apply the result to arbitrary geometry.

Someone at Stackoverflow said I should use Texture.Load instead which doesn't perform any filtering. That indeed sounds more like what I really need for this kind of processing, what do you think?

0

Share this post


Link to post
Share on other sites

Texture.Load is essentially the same as Texture.Sample when you're using a point filter.

 

If you split the data into three separate textures then you can use Texture.Sample as normal, and whatever filtering you want. It also makes the pixel shader much simpler and faster as you don't need to mess about calculating weird texture coordinates.

 

It should also look better if you use bilinear filtering on the chroma channels, instead of making them blocky by using point sampling.

1

Share this post


Link to post
Share on other sites


It should also look better if you use bilinear filtering on the chroma channels, instead of making them blocky by using point sampling.

 

This is a definite advantage over the point sampling I suggested and the Texture.Load the person on stackoverflow suggested. 

0

Share this post


Link to post
Share on other sites

Texture.Load is essentially the same as Texture.Sample when you're using a point filter.

 

If you split the data into three separate textures then you can use Texture.Sample as normal, and whatever filtering you want. It also makes the pixel shader much simpler and faster as you don't need to mess about calculating weird texture coordinates.

 

It should also look better if you use bilinear filtering on the chroma channels, instead of making them blocky by using point sampling.

I'm wondering if the cost of splitting the data into 3 textures won't offset the cost benefit of simplifying the pixel shader. Also, I will also need shaders that convert packed YUV formats (i.e. Y0 U0 Y1 V0 etc.) and it'll be impossible to cost-effectively split the data before conversion. I might as well do the conversion in software in that case.

 

As a more general solution, I'm thinking of rendering the texture into a render target of exactly the same size so as to avoid any artifacts related to point filtering, and then draw that render target unto the backbuffer which will apply filtering on the final result.

Edited by Asik
0

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