• Utilizing the GPU instead of CPU for rendering shaders
    9 replies, posted
So I am using microsoft XNA for my game, I know that XNA uses the GPU for drawing, but everything you write in VS is done on the CPU. So lets say I want to render glow for specific textures which will glow nicely over other things. Like this here. [img]https://dl.dropboxusercontent.com/u/45707598/glow.png[/img] This would require enormous amounts of loops and operations. Each pixel of the entire viewport must be checked and compared with the amassed glow masks which have been going through millions of comparisons to see which parts should glow, and which should not as they have been covered by the black box. This would be an insanely heavy CPU operation, especially since it will be rendered each and every single frame. How do I mage the GPU do this because as far as I know, this kind of stuff is just why the GPU exists, to do very very repetetive tasks with little to no variation. I have looked all over, but there are no good instructions on how to do stuff on the GPU, so I am asking here.
Look into bloom shaders. It's a post processing effect, where you use the scene as a texture and draw a quad over the whole screen with the bloom shader applied, using the scene texture as a source.
[QUOTE=thomasfn;42058376]Look into bloom shaders. It's a post processing effect, where you use the scene as a texture and draw a quad over the whole screen with the bloom shader applied, using the scene texture as a source.[/QUOTE] I will want to apply the glow on specific positions on textures, not apply bloom on the entire drawn result. Like this, I have a glow mask and a base texture, and the result will highlight only what the glow mask has instructed and not any other part of the texture [img]https://dl.dropboxusercontent.com/u/45707598/glow2.png[/img] And if an object moves over the glow, it will glow through any alpha pixels and "bleed" I have seen this done in 3D games real time, so I suppose it cant be that hard to achieve in 2D
[QUOTE=Fatfatfatty;42058343]So I am using microsoft XNA for my game, I know that XNA uses the GPU for drawing, but everything you write in VS is done on the CPU. So lets say I want to render glow for specific textures which will glow nicely over other things. Like this here. This would require enormous amounts of loops and operations. Each pixel of the entire viewport must be checked and compared with the amassed glow masks which have been going through millions of comparisons to see which parts should glow, and which should not as they have been covered by the black box. This would be an insanely heavy CPU operation, especially since it will be rendered each and every single frame. How do I mage the GPU do this because as far as I know, this kind of stuff is just why the GPU exists, to do very very repetetive tasks with little to no variation. I have looked all over, but there are no good instructions on how to do stuff on the GPU, so I am asking here.[/QUOTE] If you're feeling frisky, you can essentially do this directly in OpenCL. Each pixel can be calculated independently of every other pixel, which makes it an easily solvable parallel problem on the gpu I don't have any experience with shaders, and they would probably be a much more sensible route to go down
[QUOTE=Icedshot;42058479]If you're feeling frisky, you can essentially do this directly in OpenCL. Each pixel can be calculated independently of every other pixel, which makes it an easily solvable parallel problem on the gpu I don't have any experience with shaders, and they would probably be a much more sensible route to go down[/QUOTE] I've heard about shaders and i know what they are used for, but I do not know how to use them nor do i even know how they function, so many people seem to set them apart from classes and functions and i read xml files both here and there, it's confusing me a lot at this point about shaders and how they work exactly and how to interface with them and make them do what i want them to do.
Render only the parts of the screen you want to glow onto a rendertarget which is cleared black. Then draw your scene like normal to the screen. Finally, blur the rendertarget using some kind of gaussian blur shader onto the scene in additive mode. If additive doesn't look right, you'll need to use an alpha channel - clear the rendertarget with 0,0,0,0 and make sure the blur shader blurs alpha too. Then just alpha blend it on like normal. Alternatively you could try something with the stencil buffer but I'm not quite sure how that would work.
[QUOTE=Fatfatfatty;42058343]So I am using microsoft XNA for my game, I know that XNA uses the GPU for drawing, but everything you write in VS is done on the CPU. So lets say I want to render glow for specific textures which will glow nicely over other things. Like this here. [img]https://dl.dropboxusercontent.com/u/45707598/glow.png[/img] This would require enormous amounts of loops and operations. Each pixel of the entire viewport must be checked and compared with the amassed glow masks which have been going through millions of comparisons to see which parts should glow, and which should not as they have been covered by the black box. This would be an insanely heavy CPU operation, especially since it will be rendered each and every single frame. How do I mage the GPU do this because as far as I know, this kind of stuff is just why the GPU exists, to do very very repetetive tasks with little to no variation. I have looked all over, but there are no good instructions on how to do stuff on the GPU, so I am asking here.[/QUOTE] Yes, this would require an enormous number of operations if you brute-force it. There's no reason to do it this way. To use a metaphor, don't build a gigantic dump truck to move the mountain closer to your house. Don't move the mountain at all. Take a picture of it and hang the picture in your house. Okay, that was a terrible metaphor. There's a reason I'm a programmer and not a writer. Whatever, just do this: 1. Draw the box (or any other occluding objects) onto the glow mask. 2. Color the glow mask. 3. Blur the glow mask. Can be done on GPU. 4. Draw the glow mask on the original image. That's about 25 basic operations per pixel. All of them can be done on hardware, and it'll still be pretty fast even if you don't. Steps 1, 2, and 4 should be piss easy to do on hardware--you don't have to write any shaders. Just use the fixed pipeline that comes with XNA. Here are some efficient blur algorithms that will net you a performance of around 20 operations per pixel. (At least, the second one will.) They're meant for the GPU, but there's no reason they can't be adapted for the CPU. The first one is in HLSL, which is what XNA uses anyway. [url]http://www.gamerendering.com/2008/10/11/gaussian-blur-filter-shader/[/url] [url]http://xissburg.com/faster-gaussian-blur-in-glsl/[/url] For this it might be worth it to just bite the bullet and learn to write shaders, because this is the EXACT kind of thing shaders are meant for. Take a good, hard look at the first article. The second is not in HLSL, but it is more efficient.
So, Uh, I've tried creating a new shader to my content project, but it doesn't like the only shader that is .DGSL, I am feeling so stupid right now, but how to I actually start writing my shader, since every tutorial just assumes you know how to, but i've seen some source code examples and they are initialized by Content.Load, which means it's something of the content project. I am feeling so fucking dumb right now Nevermind, i figured it out.
I was mistaken. Neither tutorial I linked is HLSL, so neither is gonna work in XNA without modification. But the same principles should all apply.
In short, you will need to write sepparate programs in C, consisting of Vertex and Fragment program, load them into GPU and then run then in the GPU.
Sorry, you need to Log In to post a reply to this thread.