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

Intrinsics to improve performance of interpolation / mix functions

17 posts in this topic

Hello all

 

In pixel shaders (SM3 to 5) I often do some mixing / interpolation between two or more vectors (e.g. colors) or scalars (e.g. luminances).

 

A simple example of a common code as used for example in gaussion-blur-like implementation, looks like this

 

float4 mixColor = (tex2D(colorSampler, uv-blur)+tex2D(colorSampler, uv+blur)) / 2.f;

 

I would like to improve that, performance- and instruction-limits-wise, making use of hardware-supported HLSL intrinsic functions, but I'm not sure what would best here. lerp(), for example? I think I'd basically need the opposite of the mad() command. Are there better ways?

0

Share this post


Link to post
Share on other sites

It is pretty difficult to make a simple average of two values faster. Your compiler probably changes the division by 2 to multiply by 0.5 and that's like the end of your optimization possibilities - at least with so short code. 

 

Gaussian blur can be implemented better with a compute shader - with a different algorithm.

 

Cheers!

2

Share this post


Link to post
Share on other sites

Thanks to you both.

 

I'm doings loads of such averages, not just one single. That was just an example. Typically I'm processing e.g 128 to 1024 iterations in a for-loop (depending on the effect I implement). That's why performance considerations come into play. Compute shaders are out of scope in my case, I have to do everything in plain old HLSL pixel shaders.

 

As for what the compiler already optimizes, this is primarily what interests me. My question might have been not clear enough. I'm less interested in what theoretically could improve performance but what command / functions or best practices lead to the best optimized compiled code.

 

I am well aware that different GPU hardware (or firmware) might JIT compile intermediate code differently, but I'm interesting to know what *most* common GPU / systems will do.

 

E.g. it wouldn't help to use a mad() in my shaders when most systems would still compile that command into two instructions (mul,add) instead of a single madd. Similar goes for lerp() and the like.The noise() function for example is not supported by most GPUs although present in the HLSL standard.

 

Of course I can read what Microsoft writes on their HLSL intrinsic functions pages. But since reality differs from standards definitions such as HLSL, I'm interesting to learn how to write my HLSL shaders to benefit from hardware supported features as much as possible.

Edited by Meltac
1

Share this post


Link to post
Share on other sites

On the topic: Is there a simple and fast way to get and compare the average of the components of a vector, e.g. luminance of a color? I'm often doing things like this:


float lum1 = (col1.r+col1.g+col1.b)/3.f;
float lum2 = (col2.r+col2.g+col2.b)/3.f;

if (lum2-lum1 > threshold) { ... }


Which of cause generates loads of instrucstions for a simple average computation and comparison. Is there a simpler / leaner  / more elegant way?

Edited by Meltac
0

Share this post


Link to post
Share on other sites

Well, you can easily sum the components (and do the division) in a single instruction:

float lum1 = dot(col1.rgb, float3(1.0f / 3.0f, 1.0f / 3.0f, 1.0f / 3.0f));
...
1

Share this post


Link to post
Share on other sites

Yeah - I mean dot(x, 1) is a horizontal add already. On older vector GPUs, that's pretty much the best you're going to do. On GCN (and other modern scalar GPUs), it basically doesn't matter - you're doing the same amount of math either way (as long as you factor out the common operations). Also, you can re-write the original code slightly to be faster (even on scalar GPUs):

float lum1 = (col1.r + col1.g + col1.b);
float lum2 = (col2.r + col2.g + col2.b);
if (lum2 - lum1 > (3 * threshold))  // Pre-compute 3 * threshold, if possible, too!
{ ... }
2

Share this post


Link to post
Share on other sites

Thank you guys! I'll try those tweaks.

 

Btw, when talking about "thinking low level while coding high level", how can I determine how many atomic GPU instructinos a certain GPU's driver / JIT compiler produces by a given bytecode-compiled shader?

 

I mean, examining fxc's assembly output for performance reasons doesn't seem to make much sense if the GPU instructions produced by the JIT compiler differ much from that (e.g. if some assembly code containing, say, 10 "bytecode" instructions but the driver makes 15 GPU instructons out of those while a different bytecode with 15 instructions produces the same amount of GPU instructions).

Edited by Meltac
0

Share this post


Link to post
Share on other sites

Thank you guys! I'll try those tweaks.

 

Btw, when talking about "thinking low level while coding high level", how can I determine how many atomic GPU instructinos a certain GPU's driver / JIT compiler produces by a given bytecode-compiled shader?

 

I mean, examining fxc's assembly output for performance reasons doesn't seem to make much sense if the GPU instructions produced by the JIT compiler differ much from that (e.g. if some assembly code containing, say, 10 "bytecode" instructions but the driver makes 15 GPU instructons out of those while a different bytecode with 15 instructions produces the same amount of GPU instructions).

 

It's impossible to do without tools from the GPU vendor. AMD provides such a tool, Nvidia does not.

0

Share this post


Link to post
Share on other sites


It's impossible to do without tools from the GPU vendor. AMD provides such a tool, Nvidia does not.

 

Thanks. But there seem to be at least some heuristics, or experiences, or basic information on the particular instruction set of a GPU, right?

 

I mean, people seem to know whether a GPU has a horizontal add instruction or not, so I guess there are other instructions that I could specifically address using particular HLSL codes, so I'm interested in learning those.

0

Share this post


Link to post
Share on other sites

Well, the difficulty in knowing that, and keeping track of which GPU can do which operations, is part of the reason that newer architectures aren't vectorized (in the same way). Rather than doing instruction-level parallelism (with things like dot product and multiply-add on a float4), all operations are standard scalar floating point. Instead, the GPU runs the same scalar math on more threads in parallel (clusters of pixels or vertices). The end result is that - in general - micro-optimizing the order of operations is less important, because you can really just think about the naturally correct math to get what you need.

 

Example: It used to make sense to accumulate lighting information from four lights in parallel, in a transposed fashion. Doing work per light meant that you were operating on three of the four channels (rgb/xyz) while doing your math. That was basically wasting 1/4 of your potential ALU. So you'd accumulate all of the red contribution in one variable, green in another, etc... And you'd do it for four lights (one in each channel of those variables). That got you up to a 33% speedup.

 

On the scalar GPUs, that kind of optimization does nothing - you're still doing the same number of basic operations to accumulate the final result, and because everything is broken down to scalar pieces (a 3 component dot product takes a multiply and two multiply-adds), the total number of operations doesn't change.

 

I think the presentations by Humus give a good level of insight into what the compiler can/can't do, and what's typically fast. The #1 thing to remember is that GPUs have a multiply-then-add instruction, but not add-then-multiply. Always move your constant biases through your scale factors to take advantage of that. And things like combining lots of linear operations into a single matrix operation is going to get you lots more performance than worrying about whether to use lerp or not - if lerp correctly describes the math you want to do, you need to trust that it's going to be implemented using the best available instructions on each GPU.

Edited by osmanb
1

Share this post


Link to post
Share on other sites

Hey thanks osmanb for those explanations and hints. I'll consider them.

 

What about branches (in the sense of if-then-else code sections) and loops? Both are known as being potentially heavily affecting performance, so are there considerations about how to speed up those code flow operations, i.e. what sort of optimizations on the HLSL side help generating well performing GPU operations?

Edited by Meltac
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