XNA Game Development Forums
2012/05/18 07:21:45 *
Welcome, Guest. Please login or register.

Login with username, password and session length
 
   Home   Help Search Calendar Login Register  
Pages: [1]
  Print  
Author Topic: How can I speed up shaders??  (Read 2808 times)
EclipsE
Full Member
***
Offline Offline

Posts: 111


View Profile
« on: 2007/04/15 12:58:58 »

The title says it all Smiley
Logged
Tiago
Newbie
*
Offline Offline

Posts: 42


View Profile
« Reply #1 on: 2007/04/15 13:48:35 »

try NVidia's ShaderPerf something. ive never tried it myself but ive read thats what its meant to do, either optimize or help you optimize your shaders
Logged
Chr0n1x
Global Moderator
Sr. Member
*****
Offline Offline

Posts: 307


View Profile WWW
« Reply #2 on: 2007/04/16 23:41:47 »

ShaderPerf is a shader profiling tool to help you optimize the shader, it doesnt do it for you.

There isnt really a general way to speed up shaders, it all depends on what the shader does and the capabilities required for the technique.
One simple way is to use half precision instead of full float precision, this way you can have 2 variables in every register instead of only one, but it limits the range of values avaliable.
There are other ways, like reducing the number of texture calls needed, reducing the number of textures needed.

Also move unnecessary calculations, you dont need to calculate the viewprojection every vertex, so why do it, rather do that once at the beginnning of the scene render call and then pass it in, much faster and you only do the calculations once. As for worldviewproj, do it per model rather than per vertex, that way all you need to do per vertex is multiply the world space position by the worldviewproj which is passed in.

Another more advanced technique is to replace complex math functions with texture lookups. If you have a calculation that doesnt NEED to be calculated every pixel/frame, then do it in a pre-shader and then use that texture to get the values, that way you cut down the cost of the shader and only sacrifice some interactivity, which in some cases isnt needed.

Finally the most simple thing, optimize your algorithms. Take a look through and see what can be changed, what is not required. You might find you are doing something that could be replaced with something much simpler. Screw code readability, this is 3D programming, use comments to make your code readable. To use an analogy: you need to get to work quickly, do you take the long scenic route or the quick and direct route?

Hmm, thats all I can think of now, those are some basic steps you can do to make your shaders run a bit faster, in some cases it will work, in some cases not, all depends on the shader, and thus shader optimization is something you can only do once you know the details of the shader and its technique. If you post your shader we could help in this case, but most general things you can do are covered by the compiler as well, as long as shader optimization is turned on. (Default)

[EDIT]
Some small stats on the WorldViewProjection multiplication.
For a simple Matrix*Matrix, there are 256 multiplication operations, and 240 addition operations.
To get the WVP, we do 2 Matrix*Matrix multiplications, using the result of the first one, as a parameter for the second one. So in total we have 512 multiplication operations and 480 addition operations. Note that these are FLoating point OPerationS. (FLOPS) So they are not as simple as a normal integer multiplication or addition.

Now if we do this per model, thats fine, but if we do this per vertex, with a say, 4000 vertex model, thats 2,048,000 multiplication operations in total for that model, and 1,920,000 addition operations for that model. In total 3,968,000 FLOPS for the model, and this is a rather small model, and only one. Most complex scenes today have many more vertices, even with culling enabled there can still be more than that on screen at once.

(Note that I dont know exactly what the compiler does when optimizing, so it might calculate this once and then store it in a register rather than recalculating every vertex later. Maybe, maybe not.)
« Last Edit: 2007/04/17 00:02:20 by Chr0n1x » Logged

samprass
Newbie
*
Offline Offline

Posts: 10


View Profile
« Reply #3 on: 2007/04/17 09:55:31 »

One solution:

"This is where preshaders kick in: when the HLSL code is being compiled, the compiler checks for code that will be the same for every vertex. So if we put our matrix multiplications inside our vertex buffers, that part of HLSL code will get stripped away by the compiler, and put in a ?preshader?. This preshader is executed on the CPU before the vertex shader is actually called, and the resulting constants are passed to the HLSL code. This way, we can code the multiplications in our vertex shader (where they belong, as you?ll see), yet they will be processed only once on the CPU..." - from Riemer

See:  http://www.riemers.net/eng/Tutorials/XNA/Csharp/Series3/Preshaders.php

Logged
Chr0n1x
Global Moderator
Sr. Member
*****
Offline Offline

Posts: 307


View Profile WWW
« Reply #4 on: 2007/04/17 18:22:01 »

Ah cool, that confirms my note at the end there. Although remember that you cannot access this data, so if you wanna use it for anything in Update (eg. Get screen coords), you will have to recalculate it all again, so its up to you depending on what you want to do.
Logged

EclipsE
Full Member
***
Offline Offline

Posts: 111


View Profile
« Reply #5 on: 2007/04/20 01:51:41 »

Thanks for your help, I've done some optimization with the matrices and got improvement of 20 FPS!!!  Shocked
Logged
DaphydTheBard
Jr. Member
**
Offline Offline

Posts: 55


View Profile
« Reply #6 on: 2007/04/26 13:53:51 »

Also bare in mind that the 360 is a monster.

You can expect your game to run about three times faster (from what I'm told, and what I've seen) on the 360 than it will on a PC.
Logged
Chr0n1x
Global Moderator
Sr. Member
*****
Offline Offline

Posts: 307


View Profile WWW
« Reply #7 on: 2007/04/27 00:26:21 »

All depends on your current PC, but also remember the GC on the 360 is not as great, so make sure the rest of your code is good.
Logged

Pages: [1]
  Print  
 
Jump to:  

Related Topics
Subject Started by Replies Views Last post
Tutorial 3 :: More Rendering Complexity - Shaders and Cameras Tutorial Discussion « 1 2 3 4 » mikeschuld 52 17433 Last post 2009/12/12 17:04:40
by Mikeske
Shaders and rotating models.... General Discussion Nemo Krad 3 2827 Last post 2007/03/03 17:42:12
by Nemo Krad
OctaneX - Hi Speed Racer Show Off Board DaphydTheBard 14 5019 Last post 2007/06/20 00:09:30
by astennu
Multiple postscreen shaders Hazy Mind XNA Engine EclipsE 2 1670 Last post 2007/04/29 21:50:58
by Chr0n1x
Switching shaders. Motion Blur. Shadows. Hazy Mind XNA Engine amartinez1660 0 977 Last post 2007/05/07 00:01:25
by amartinez1660
Powered by MySQL Powered by PHP Powered by SMF 1.1.12 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Page created in 0.14 seconds with 18 queries.