It is currently Sat Aug 19, 2017 4:24 am

All times are UTC - 5 hours




 Page 1 of 1 [ 6 posts ] 
Author Message
 Post subject: Shader VMK 36 - Challenge 2 Solution
PostPosted: Thu Jan 30, 2014 8:21 pm 
Site Admin

Joined: Sun Feb 11, 2007 8:59 am
Posts: 1105
Location: Ontario Canada
In this VMK I show the solution to the second shader challenge that I presented in the last VMK. Congrats to skilz80 and davidemo89 for solving the challenge.


Offline
 Profile  
 
 Post subject: Re: Shader VMK 36 - Challenge 2 Solution
PostPosted: Fri Jan 31, 2014 12:02 am 

Joined: Sat Aug 16, 2008 7:58 am
Posts: 448
I have a question that focuses on the difference between the way I implemented the shader versus how you did it in your solution video. Would it be more optimized or more efficient to do the color calculation in the vertex shader instead of the fragment shader? If a vertex shader is only called one to a few times per frame where the fragment shader is called multiple times per frame you would only have 3 division or multiplication calls versus hundreds of calls. It is good to know where code can be optimized and still produce the same result.


Offline
 Profile  
 
 Post subject: Re: Shader VMK 36 - Challenge 2 Solution
PostPosted: Fri Jan 31, 2014 9:08 am 
Site Admin

Joined: Sun Feb 11, 2007 8:59 am
Posts: 1105
Location: Ontario Canada
The vertex shader runs once per vertex on the surface, while the fragment shader runs once per pixel on the surface. Therefore you are correct, the fragment shader will run many more times then the vertex shader.

When building complex shaders with lots of calculations, always try to do the work in the vertex shader as much as possible rather than doing it in the pixel shader. Also, if you can offload calculations from the vertex shader and do them in the C++ code and then pass the result into the vertex shader via a uniform variable, then this is also desired (we do that with the Model View Projection matrix when we know that all vertices have the same matrix multiplied).

There are trade offs between doing calculations in the vertex shader vs doing them in the pixel shader though. When you calculate things like light effects or bump maps, you want to do those in the pixel shader or else your results are not going to look good.

Compare the following images to see the differences:
This is a basic light model calculated in the vertex shader:
Image

This is the same light model calculated in the pixel shader:
Image

For this challenge, we are only rendering a single surface so you will not see a performance difference when doing the calculations in the pixel shader vs the vertex shader.


Offline
 Profile  
 
 Post subject: Re: Shader VMK 36 - Challenge 2 Solution
PostPosted: Fri Jan 31, 2014 9:39 am 

Joined: Sat Aug 16, 2008 7:58 am
Posts: 448
Thank you for the clarity on the two. It follows HLSL closely except that Direct X has more then just a vertex and pixel shader. It also has a hull and domain shader, plus you can render a full scene, partial scene or single object from a back buffer onto a render to texture object, do your calculations then do a normal render call. The syntax is a bit different, and the c++ source code between the two are very different. I have a direct x 10 book and I have also followed rastertek.com website for directx 10 and 11. I am familiar with the concepts of the shaders it is just learning how to go from one api to another.


Offline
 Profile  
 
 Post subject: Re: Shader VMK 36 - Challenge 2 Solution
PostPosted: Fri Jan 31, 2014 9:54 am 
Site Admin

Joined: Sun Feb 11, 2007 8:59 am
Posts: 1105
Location: Ontario Canada
skilz80 wrote:
...except that Direct X has more then just a vertex and pixel shader.


Up until now I've only discussed the vertex and pixel shaders, but OpenGL has many more you can write. There are tessellation control shaders, tessellation evaluation shaders, geometry shaders, and compute shaders. Have a look at the newest OpenGL spec for details on each of these:

OpenGL 4.4 spec wrote:
Vertex shaders describe the operations that occur on vertex attributes. Tesselation control and evaluation shaders are used to control the operation of the tessellator, and are described in section 11.2. Geometry shaders affect the processing of primitives assembled from vertices (See section 11.3). Fragment shaders affect the processing of fragments during rasterization (see section 15). A single program object can contain all of these shaders, or any subset thereof.

Compute shaders perform general-purpose computation for dispatched arrays of shader invocations (see section 19), but do not operate on primitives processed by the other shader types.


http://www.opengl.org/registry/doc/glspec44.core.pdf


Offline
 Profile  
 
 Post subject: Re: Shader VMK 36 - Challenge 2 Solution
PostPosted: Fri Jan 31, 2014 12:49 pm 

Joined: Sat Aug 16, 2008 7:58 am
Posts: 448
I thought OpenGL would have something similar but I wasn't 100% sure.


Offline
 Profile  
 
Display posts from previous:  Sort by  
 Page 1 of 1 [ 6 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Jump to:  

cron