3D Fundamentals [Pixel Shader] Tutorial 9

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
thessandre
Posts: 2
Joined: September 18th, 2019, 10:06 am

3D Fundamentals [Pixel Shader] Tutorial 9

Post by thessandre » September 18th, 2019, 11:06 am

Hi Chili and all,

After watching Tutorial 9, I wanted to exercise with an custom-made effect :geek: :

For each pixel on the cube
Calculate the distance with the centre of the cube
Depending on the distance
Color between white and blue, given
white would be the smallest possible distance (hence sqrt(0.25) = 0.50)
blue would be for the largest possible distance (hence sqrt(0.75) = 0.86)
Randomly color about 1% of the points in white

This would basically look like a cube of blue space with a white sun inside. I figured this would look cool 8-)

I was able to create a new scene and a new texture effect for this, and organise things in game.cpp.
I was able to find a quick and dirty solution procedurally generating my own texture within the effect, creating a new generated texture class derived from the Surface class and saving it to a file before loading it. It does look pretty *fucking* sweet. :mrgreen:

However I found it impossible to modify the code using the same pipeline (or even adapting the pipeline) so that I could inject vertex data representing the centre of the cube, have it transformed, calculate the distance and deduct the intensity of blue vs. white. I can understand that things are a little tricky because our triangle drawing gfx.PutPixel call is based on interpolation between vertex colors or loading a texture.

But if we could do it for a texture why would it not be possible to hardcode such a coloring rule in the pixel shader, but without generating an actual texture ? I was not able to do it, mainly because I failed at propagating the vertex data representing the centre of the cube. :x

Can pixel shaders not depend on external objects data rather than on data from the actual object being rendered ? - I can't buy that.
Chili, guys, can you help me figuring out how to modify the pixel shader in order to make it depend from a vertex at the centre of the cube (wherever the cube is on screen) ?

Thessandre

Eagerly looking forward to get drenched from your science :mrgreen:

Cheers

PS. The code attached are my changes when branching out from the code of tutorial 11 (after creation of the ZBuffer)

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: 3D Fundamentals [Pixel Shader] Tutorial 9

Post by albinopapa » September 19th, 2019, 3:26 pm

No code attached.

In Direct3D specifically which is what the 3D fundamentals framework was modeled on, runs these shaders for specific domains. A vertex shader only runs per vertex and only has the structure layout of a vertex and a result, which is passed on through the pipeline. A pixel shader runs per pixel on screen, so it's limited to attributes that have been interpolated from the vertex result structure.

However, you do have access to constant buffers that for the entirety of rendering an object, remains the same. So for instance, you could supply a constant buffer to the Effect::PixelShader that you update per cube with the center position of the cube. The interpolated vertex position - the constant buffer cube position will get you your delta.

In the framework, chili doesn't really use a "constant buffer", but instead just has free members of the individual shaders and set functions. In D3D, you'll need to use these constant buffers.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: 3D Fundamentals [Pixel Shader] Tutorial 9

Post by albinopapa » September 19th, 2019, 3:27 pm

Share link of your github repository and we'll be able to better guide you.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

thessandre
Posts: 2
Joined: September 18th, 2019, 10:06 am

Re: 3D Fundamentals [Pixel Shader] Tutorial 9

Post by thessandre » September 22nd, 2019, 2:11 pm

Hello albinopapa,

Thank you for your answer which totally makes sense.

When reaching Chili's D3D Tutorial I will definitely dig into the Constant Buffer capabilities to store external data which then can be used to calculate Pixel Shader effects depending on these data.

Below I stored to modified and added files in the following github repository.
They branch from the T11-End tag based on the Chili's original master and mainly consist in generating the texture and mapping it onto the cube

https://github.com/thessandre/3DFundame ... xel_Shader

However if like Chili I was to use members (and set fonctions ? which ones do you mean) within the pixel shader to store data such as the centre of the cube, how would you recommend me to proceed ?

Looking forward to your input
Cheers
Thessandre

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: 3D Fundamentals [Pixel Shader] Tutorial 9

Post by albinopapa » September 23rd, 2019, 9:21 pm

error 404

Even going to ( https://github.com/thessandre ) says you don't have any repositories in your github account. Make sure to sync what you have in Visual Studio in order for your changes to be saved and shareable.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: 3D Fundamentals [Pixel Shader] Tutorial 9

Post by chili » September 26th, 2019, 4:41 pm

It was probably a slight missed opportunity there to not better mimic the constant buffer arrangement of hardware APIs.
Chili

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: 3D Fundamentals [Pixel Shader] Tutorial 9

Post by albinopapa » September 26th, 2019, 9:18 pm

I did think about it at first, but I think because I was already aware of the fact I just figured, "meh, tutorial code".

I'd hate to be a tutorial writer, mad props to you chili. Not just for doing what you're doing, but this is the longest and hardest I've seen you go, pun definitely intended, cheers.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: 3D Fundamentals [Pixel Shader] Tutorial 9

Post by chili » September 27th, 2019, 10:58 am

Can't stop, won't stop.
Chili

Post Reply