Sign in to follow this  
MARS_999

OpenGL OpenCL to make a game engines renderer?

Recommended Posts

I have been pondering lately, why couldn't one use OpenCL to do the graphics processing, (I am assuming software rendering) and then dump the final image to OpenGL as a textured fullscreen quad? Or is this not possible, or a good idea?

Has anyone tried this or even shown a proof of concept of this idea?

I am also assuming OpenCL can use SLI/Xfire solutions....

Thanks!

Share this post


Link to post
Share on other sites
[quote name='MARS_999' timestamp='1317394931' post='4867594']
I have been pondering lately, why couldn't one use OpenCL to do the graphics processing, (I am assuming software rendering) and then dump the final image to OpenGL as a textured fullscreen quad? Or is this not possible, or a good idea?

Has anyone tried this or even shown a proof of concept of this idea?

I am also assuming OpenCL can use SLI/Xfire solutions....

Thanks!
[/quote]

It is possible but not really suitable for realtime rendering, For things like raytracers it can give a big performance boost but you'd still measure the framerate in frames per minute or frames per hour rather than frames per second unless you keep the scenes extremely simple.

Share this post


Link to post
Share on other sites
I think that paper glaeken mentioned had results ranging from 2 to 8 times slower than hardware rasterization. So it's definitely not that far off, and probably even feasible for certain cases. However like any generalized system I'd imagine that you'd need to really take advantage of your additional flexibility to make it worth the loss in performance.

Share this post


Link to post
Share on other sites
my opencl rasterizer is bout 10% of the theoretical hardware peak performance (on my gtx460), and it wasn't all that hard to get it running, even with 1% the performance you'd have enough throughput to render decent scenes.

I've written it for the sake of fun, and because my catmull-clark tessellation resulted in quite a lot of data organized in not a compatible way for GPUs (e.g. positions were shared, as you need that for displacement to not have cracks, but UVs were by face). and converting all data would be quite some work and either I had created a lot of duplicated vertices or I'd spend quite some time to not have them... and then the hardware would need to render them anyway, rejecting a lot of micro triangles.




Share this post


Link to post
Share on other sites
I'm not sure if I'm missing the point, but why not use the GPU the way it was designed and use OpenGL or D3D to render, rather than build an entire system that pretty much is guaranteed to be slower?

It'd be an interesting pet project though.

Share this post


Link to post
Share on other sites
[quote name='Cornstalks' timestamp='1317428604' post='4867796']
It'd be an interesting pet project though.
[/quote]

That's pretty much the reason; unless you want to do raytracing then the only reason right now to do such a thing is the 'because I can' factor... which at times is a good reason as long as you know what you are getting yourself into :D

Share this post


Link to post
Share on other sites
Replacing OpenGL/Direct3D with a software one running on the GPU (while being fun) woudln't get you the same performance (although acceptable speeds should be doable). The GPU isn't really a general purpose computer, its designed to crunch OpenGL/Direct3D so you would loose any 3D specific optimization plus you would be bypassing the drivers which would also be optimizing the instructions for the specific hardware.

There's quite a few OpenCL/CUDA realtime raytracers out there that seem to get somewhat decent FPS.
[url="http://www.youtube.com/watch?v=_ypoSZk88XE&feature=related"]http://www.youtube.com/watch?v=_ypoSZk88XE&feature=related[/url]
[url="http://www.youtube.com/watch?v=v1JS4wyGGy0"]http://www.youtube.com/watch?v=v1JS4wyGGy0[/url]
[url="http://www.youtube.com/watch?v=TX56aqXoDW0"]http://www.youtube.com/watch?v=TX56aqXoDW0[/url]

With that said, it would be interesting to see if it is possible to combine traditional 3D graphics with the raytraced ones. OpenCL does have stuff that allows it to talk to OpenGL. You could do 1 normal render and overlay a lower quality but faster raytraced lighting ontop of it (just modify the normal lighting with the reflected/scattered raytraced light for example). Maybe generate realtime lightmaps that only have to be generated when the lightsource changes, so you get the quality of prebaked lighting with flexibility for things like dynamic shadows and allowing the levels to be generated at runtime without running though a 'baking lighting' phase with little in the way of slowdown (unless you move your lights a lot). Or use it to do special effects like multilevel reflections on glass.

It could be handy to look at backporting newer GPU features/extensions. Things would be slower but there's no reason you couldn't emulate something like a geometry shader on older hardware it it supports OpenCL.

It also might be possible to look at some kind of massively parallel rendering pipeline, so your game just uploads new position information to a buffer for the game objects but the GPU does most of that under the hood anyway and the bits it doesn't do would probably be the linear non parallel bits that would such on OpenCL.

A software renderer would be more portable, you could build a FPGA implementation of it example, and run it on a CPU (the future ones should have heaps of cores and probably some floating point processors like a GPU). But it's still going to be at fairly hefty performance costs. And chances are any system that runs OpenCL would be capable of running OpenGL anyway.

I wonder what the prospects are for a Direct3D OpenCL implementation, once again performance wise it would probably be better to just use an OpenGL wrapper or do a [url="http://www.phoronix.com/scan.php?page=article&item=mesa_gallium3d_d3d11&num=1"]native Direct3D[/url] implementation.

Share this post


Link to post
Share on other sites
[quote name='H3g3m0n' timestamp='1317434005' post='4867829']
I wonder what the prospects are for a Direct3D OpenCL implementation, once again performance wise it would probably be better to just use an OpenGL wrapper or do a [url="http://www.phoronix.com/scan.php?page=article&item=mesa_gallium3d_d3d11&num=1"]native Direct3D[/url] implementation.
[/quote]

Next to zero owing to the existence of [url="http://en.wikipedia.org/wiki/DirectCompute"]DirectCompute[/url].

Share this post


Link to post
Share on other sites
[quote name='MARS_999' timestamp='1317424294' post='4867770']
Interesting Kryt0n, would you be willing to show some screenshots?
[/quote]I did not take any of the OpenCL version, but it looks the same like the CPU version:

http://twitpic.com/3wozra


Share this post


Link to post
Share on other sites
[quote name='Krypt0n' timestamp='1317588940' post='4868384']
[quote name='MARS_999' timestamp='1317424294' post='4867770']
Interesting Kryt0n, would you be willing to show some screenshots?
[/quote]I did not take any of the OpenCL version, but it looks the same like the CPU version:

[url="http://twitpic.com/3wozra"]http://twitpic.com/3wozra[/url]



[/quote]

not bad! At least now I can think about it, for a fun side project...

Share this post


Link to post
Share on other sites
I do agree that OpenCL is not a smart choice, if what you want to achieve is the same thing you would get out from you OpenGL/DirectX anyway. But as already stated, CUDA/OpenGL raytracers are quite common nowadays (both real time and for off line rendering).
In fact, I think that once next Gen consoles are out, OpenCL/CUDA/DirectCompute will be added to improve engines (most probably through raytraced extensions) just as we slowly moved toward shaders 10 years ago.

Writing an OpenCL raytracer/path tracer or a mixed OpenCL/OpenGL engine would be a truly interesting experience (I'm currently trying to learn OpenCL to rewrite part of my raytracer myself, but I'm fighting against lack of spare time :-(

Share this post


Link to post
Share on other sites
I wrote a simple OpenCL ray-tracer as my master thesis last year. Even with almost no optimisations at all, I was getting interactive rates on small-mediocre scenes with GF8800/HD4670 cards. I just implemented both BVH and kDtree, both construction (in parallel!) and traversal, no hardcore shading though. This is the way, this will be the future, parallel ray-tracing algorithms :-) Simple, nice and fast in the future.

Nevertheless, the "because I can" factor is too strong, so be it ray-tracing or rasterisation, implementing it is always fun! Just because we can :D And come on, even shitty 5% of current "peak performances" is pretty enough to create nice games on top of such engines!

Have fun!

Share this post


Link to post
Share on other sites
[quote name='pcmaster' timestamp='1317727985' post='4868949']
I wrote a simple OpenCL ray-tracer as my master thesis last year. Even with almost no optimisations at all, I was getting interactive rates on small-mediocre scenes with GF8800/HD4670 cards. I just implemented both BVH and kDtree, both construction (in parallel!) and traversal, no hardcore shading though. This is the way, this will be the future, parallel ray-tracing algorithms :-) Simple, nice and fast in the future.

Nevertheless, the "because I can" factor is too strong, so be it ray-tracing or rasterisation, implementing it is always fun! Just because we can :D And come on, even shitty 5% of current "peak performances" is pretty enough to create nice games on top of such engines!

Have fun!
[/quote]

That is kind of my thoughts... Simple games and what not, maybe worth taking a look at this, vs. relying on a GPU to make the Casual game market... e.g. Facebook crap games or Angry Birds types... Or even simple vector style 3d games?

Share this post


Link to post
Share on other sites
[quote name='MARS_999' timestamp='1317770412' post='4869138']
That is kind of my thoughts... Simple games and what not, maybe worth taking a look at this, vs. relying on a GPU to make the Casual game market... e.g. Facebook crap games or Angry Birds types... Or even simple vector style 3d games?
[/quote]
...because that market usually owns a GPU capable of doing heavy processing like the GTX480. We're still a long, very long time until we reach a playable level on the low-end, consumer-grade integrated GPUs for that kind of rendering.

Share this post


Link to post
Share on other sites
[quote name='Matias Goldberg' timestamp='1317772303' post='4869146']
[quote name='MARS_999' timestamp='1317770412' post='4869138']
That is kind of my thoughts... Simple games and what not, maybe worth taking a look at this, vs. relying on a GPU to make the Casual game market... e.g. Facebook crap games or Angry Birds types... Or even simple vector style 3d games?
[/quote]
...because that market usually owns a GPU capable of doing heavy processing like the GTX480. We're still a long, very long time until we reach a playable level on the low-end, consumer-grade integrated GPUs for that kind of rendering.
[/quote]
but he said it was running fine on a 8800GT, it scores about 11k in 3dmark06, the on-die gpu on AMD Llano scores something between 10k and 11k in 3dmark06 (and is probably more memory limited than a 8800GT). I think it's not that far of until you can start playing with opencl-only games.

I think OpenCL/Cuda rasterization can have quite some advantages, e.g.

-if you rely on gpu rasterization for occlusion culling, you'd have to switch a lot of times between D3D/OGL and Opencl/Cuda and in this case it might be faster to rasterize in a GPGPU language.

-Maybe you're making some quad rasterization, you can either evaluate pixel with openCL or you'd have to tesselate the quad to not suffer from typical triangle borders across a quad.


-you can do proper parabolid rasterization

so, doing it a little bit smarter due to the freedom opencl offers, you can actually be even faster or achive things, than you cannot with the usual rasterizer.




Share this post


Link to post
Share on other sites
[quote name='Tachikoma' timestamp='1317809040' post='4869333']
I'm bit of a n00b to CL myself, but don't you have to roll your own boilerplate code, such as texture samplers with filtering? That kind of thing is practically "free" in OpenGL.
[/quote]
you have texture units in cuda and opencl with all the possibilities.

the only thing that is missing so far is writing volume textures from compute kernels (although there is a hack from cyril, the gigavoxel guy), but you can't do that from opengl either, just from cpu.


Share this post


Link to post
Share on other sites
Off the top of my head, these are the things you'd be missing out on when rolling your own GPU rasterizer:

1. Automatic unpacking of vertex attributes, along with any special HW caching that might be involved
2. Post-transform vertex cache
3. The fixed-function tessellation unit
4. Triangle setup
5. HW rasterization and all associated states, such as scissor or clip planes
6. Early z + stencil unit
7. Automatic mip level determination based on fragment UV derivatives (or any access derivatives, for that matter)
8. Fixed function blending
9. Fixed function z + stencil test
10. Any sort of MSAA support

Share this post


Link to post
Share on other sites
[quote name='MJP' timestamp='1317839634' post='4869486']
1. Automatic unpacking of vertex attributes, along with any special HW caching that might be involved[/quote]nowadays they are patching the shader, it's less automatic then it was.
[quote]2. Post-transform vertex cache[/quote]you can use the shared memory, or it might make even more sense to transform everything just once. beside that, you might apply your transformations just to the needed attributes, untouched ones like UVs would not be touched, that can make the transform faster than using vertexshaders.


[quote]3. The fixed-function tessellation unit[/quote]vs your own flexible tesselation (e.g. my catmull clark)


[quote]4. Triangle setup[/quote]you can setup one triangle per "thread" into shared memory, that can be fast. beyond that, you can have your quad-setup, pentagon-setup...
[quote]5. HW rasterization and all associated states, such as scissor or clip planes[/quote]on the other side you can have one state per triangle with no state-change penalties, as it's up to you what data you pass, this way you can e.g. create a deferred shading pipeline that allows you to use scissor-rects and still draw all lights in just one 'call'. 1000lights in the classic pipe means 1000 drawcalls.
[quote]6. Early z + stencil unit[/quote]are you sure you don't mean HiZ or ZCull? early-z is nothing more than a branch in the shader units and operates on fragment granularity.
[quote]7. Automatic mip level determination based on fragment UV derivatives (or any access derivatives, for that matter)[/quote] that's sadly very true to my knowledge, people are begging for this since like 4 years.
[quote]8. Fixed function blending
9. Fixed function z + stencil test[/quote]that one is really a bit challenging to not run into race conditions but at the same time to not slow down using atomics
[quote]10. Any sort of MSAA support[/quote]
on the other side, you can use some adaptive scheme to hide aliasing, not just on geometry edges, e.g. when using some parallax shader.







Share this post


Link to post
Share on other sites
[quote name='Krypt0n' timestamp='1317850016' post='4869558']
[quote]3. The fixed-function tessellation unit[/quote]vs your own flexible tesselation (e.g. my catmull clark)
[/quote]


covered in the tradional pipeline by Tessellation shaders, no?

[quote]
[quote]7. Automatic mip level determination based on fragment UV derivatives (or any access derivatives, for that matter)[/quote] that's sadly very true to my knowledge, people are begging for this since like 4 years.
[/quote]

The problem is without knowledge of how the values are changing per-pixel quad it is probably impossible to do. The hardware can only provide it because it does triangle setup and can work it out across the quads, but as it has no knowledge of what your data structure is or what your shader is doing there isn't really a sensible of trying to automagically do it, thus you are left to reproduce this bit of hardware yourself.

On point 5, lights in a deferred renderer; that is why hybrid solutions which use SPUs or Compute shaders exist. Tradional pipeline does what it does best and then the compute shaders kick in for grouped work.

Finally can you explain your point 1 comment? How do they patch the shader to deal with per-vertex attributes? (I know it was/is common to do it with 'constant' per-draw call data, although less so in recent hardware with real registers if memory serves, but per-vertex?)

Oh, and on point 2; unless you are doing 'in place' and have the vertex data packed by types I don't see how it can be faster? Properly setup vertex data is going to be cache/memory friendly anyway and just streams in, sreams out... in fact vertex transform is rarely a bottleneck these days, you are more likely to crash into bandwidth for pixels or later amplication than hit one with a vertex shader transform.

Share this post


Link to post
Share on other sites
I was merely enumerating things that you would have to implement yourself in order to have a featureset comparable to a modern DX/GL rasterization pipeline. Obviously some of those things are easier to emulate while some are not, and will also have varying levels of impact on performance. And yes, I was talking about the ZCull units in hardware that cull fragments before they are executed.

Share this post


Link to post
Share on other sites
[quote name='phantom' timestamp='1317854260' post='4869585']
[quote name='Krypt0n' timestamp='1317850016' post='4869558']
[quote]3. The fixed-function tessellation unit[/quote]vs your own flexible tesselation (e.g. my catmull clark)
[/quote]


covered in the tradional pipeline by Tessellation shaders, no?[/quote]not really, most used algorithms are recursive, you cannot do it in one pass. There are for sure approximations, like adjusting Bézier patch anchors to adapt to the surface you'd get with catmull clark, but if you had an animated char, you'd have to recompute the whole setup, which again is not a simple one-pass-one-unit work.
[quote]


[quote]
[quote]7. Automatic mip level determination based on fragment UV derivatives (or any access derivatives, for that matter)[/quote] that's sadly very true to my knowledge, people are begging for this since like 4 years.
[/quote]

The problem is without knowledge of how the values are changing per-pixel quad it is probably impossible to do. The hardware can only provide it because it does triangle setup and can work it out across the quads, but as it has no knowledge of what your data structure is or what your shader is doing there isn't really a sensible of trying to automagically do it, thus you are left to reproduce this bit of hardware yourself.
[/quote]I might be wrong for now, but last time I tried there wasn't any texture fetch instruction to select an lod or supply the derivates. So, the problem is beyond just the derivate calculation for quads.


[quote]On point 5, lights in a deferred renderer; that is why hybrid solutions which use SPUs or Compute shaders exist. Tradional pipeline does what it does best and then the compute shaders kick in for grouped work. [/quote]that's what I was trying to say.

[quote]Finally can you explain your point 1 comment? How do they patch the shader to deal with per-vertex attributes? (I know it was/is common to do it with 'constant' per-draw call data, although less so in recent hardware with real registers if memory serves, but per-vertex?)[/quote]having dedicate hardware, like the input assembly stage, that would idle 90% of the time (as you usually read maybe 4 attributes, while your shader computes 100-200 vertex instructions) would be kind of a waste, if your shader unit is capable to do the read as well (not slower, not faster). that's why vertexshaders get patched to make the appropriate read. (just like you'd do in GPGPU languages).
[quote]Oh, and on point 2; unless you are doing 'in place' and have the vertex data packed by types I don't see how it can be faster? Properly setup vertex data is going to be cache/memory friendly anyway and just streams in, sreams out... in fact vertex transform is rarely a bottleneck these days, you are more likely to crash into bandwidth for pixels or later amplication than hit one with a vertex shader transform.


[/quote]you are right, the indices are quite optimal to have a high hit rate, but they are based on quite redundant vertex data, as every attribute of your vertex can cause a split. simplest example is a cube; if you just have positions, you transform 8 vertices, if you add normals, you deal with 24 (6side x 4 vertices) and if you let some artist play with the UVs you might end up with 36 (6side x 2triangle x 3vertices). transformatoins.

bandwidth is a good point, but the amount of data you move around for vertices is usually not that big in relation to the instructions you have to deal with them. In my case, the 1Mio vertices were using 8MB of position memory, linearly used. I think, if I had added it to a typical pipeline with shadows etc. I had had transformed every vertex just once per frame, not in every pass.


@MJP just wanted to give my 2cents to it ;)




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  

  • Announcements

  • Forum Statistics

    • Total Topics
      628354
    • Total Posts
      2982236
  • Similar Content

    • By test opty
      Hi all,
       
      I'm starting OpenGL using a tut on the Web. But at this point I would like to know the primitives needed for creating a window using OpenGL. So on Windows and using MS VS 2017, what is the simplest code required to render a window with the title of "First Rectangle", please?
       
       
    • By DejayHextrix
      Hi, New here. 
      I need some help. My fiance and I like to play this mobile game online that goes by real time. Her and I are always working but when we have free time we like to play this game. We don't always got time throughout the day to Queue Buildings, troops, Upgrades....etc.... 
      I was told to look into DLL Injection and OpenGL/DirectX Hooking. Is this true? Is this what I need to learn? 
      How do I read the Android files, or modify the files, or get the in-game tags/variables for the game I want? 
      Any assistance on this would be most appreciated. I been everywhere and seems no one knows or is to lazy to help me out. It would be nice to have assistance for once. I don't know what I need to learn. 
      So links of topics I need to learn within the comment section would be SOOOOO.....Helpful. Anything to just get me started. 
      Thanks, 
      Dejay Hextrix 
    • By mellinoe
      Hi all,
      First time poster here, although I've been reading posts here for quite a while. This place has been invaluable for learning graphics programming -- thanks for a great resource!
      Right now, I'm working on a graphics abstraction layer for .NET which supports D3D11, Vulkan, and OpenGL at the moment. I have implemented most of my planned features already, and things are working well. Some remaining features that I am planning are Compute Shaders, and some flavor of read-write shader resources. At the moment, my shaders can just get simple read-only access to a uniform (or constant) buffer, a texture, or a sampler. Unfortunately, I'm having a tough time grasping the distinctions between all of the different kinds of read-write resources that are available. In D3D alone, there seem to be 5 or 6 different kinds of resources with similar but different characteristics. On top of that, I get the impression that some of them are more or less "obsoleted" by the newer kinds, and don't have much of a place in modern code. There seem to be a few pivots:
      The data source/destination (buffer or texture) Read-write or read-only Structured or unstructured (?) Ordered vs unordered (?) These are just my observations based on a lot of MSDN and OpenGL doc reading. For my library, I'm not interested in exposing every possibility to the user -- just trying to find a good "middle-ground" that can be represented cleanly across API's which is good enough for common scenarios.
      Can anyone give a sort of "overview" of the different options, and perhaps compare/contrast the concepts between Direct3D, OpenGL, and Vulkan? I'd also be very interested in hearing how other folks have abstracted these concepts in their libraries.
    • By aejt
      I recently started getting into graphics programming (2nd try, first try was many years ago) and I'm working on a 3d rendering engine which I hope to be able to make a 3D game with sooner or later. I have plenty of C++ experience, but not a lot when it comes to graphics, and while it's definitely going much better this time, I'm having trouble figuring out how assets are usually handled by engines.
      I'm not having trouble with handling the GPU resources, but more so with how the resources should be defined and used in the system (materials, models, etc).
      This is my plan now, I've implemented most of it except for the XML parts and factories and those are the ones I'm not sure of at all:
      I have these classes:
      For GPU resources:
      Geometry: holds and manages everything needed to render a geometry: VAO, VBO, EBO. Texture: holds and manages a texture which is loaded into the GPU. Shader: holds and manages a shader which is loaded into the GPU. For assets relying on GPU resources:
      Material: holds a shader resource, multiple texture resources, as well as uniform settings. Mesh: holds a geometry and a material. Model: holds multiple meshes, possibly in a tree structure to more easily support skinning later on? For handling GPU resources:
      ResourceCache<T>: T can be any resource loaded into the GPU. It owns these resources and only hands out handles to them on request (currently string identifiers are used when requesting handles, but all resources are stored in a vector and each handle only contains resource's index in that vector) Resource<T>: The handles given out from ResourceCache. The handles are reference counted and to get the underlying resource you simply deference like with pointers (*handle).  
      And my plan is to define everything into these XML documents to abstract away files:
      Resources.xml for ref-counted GPU resources (geometry, shaders, textures) Resources are assigned names/ids and resource files, and possibly some attributes (what vertex attributes does this geometry have? what vertex attributes does this shader expect? what uniforms does this shader use? and so on) Are reference counted using ResourceCache<T> Assets.xml for assets using the GPU resources (materials, meshes, models) Assets are not reference counted, but they hold handles to ref-counted resources. References the resources defined in Resources.xml by names/ids. The XMLs are loaded into some structure in memory which is then used for loading the resources/assets using factory classes:
      Factory classes for resources:
      For example, a texture factory could contain the texture definitions from the XML containing data about textures in the game, as well as a cache containing all loaded textures. This means it has mappings from each name/id to a file and when asked to load a texture with a name/id, it can look up its path and use a "BinaryLoader" to either load the file and create the resource directly, or asynchronously load the file's data into a queue which then can be read from later to create the resources synchronously in the GL context. These factories only return handles.
      Factory classes for assets:
      Much like for resources, these classes contain the definitions for the assets they can load. For example, with the definition the MaterialFactory will know which shader, textures and possibly uniform a certain material has, and with the help of TextureFactory and ShaderFactory, it can retrieve handles to the resources it needs (Shader + Textures), setup itself from XML data (uniform values), and return a created instance of requested material. These factories return actual instances, not handles (but the instances contain handles).
       
       
      Is this a good or commonly used approach? Is this going to bite me in the ass later on? Are there other more preferable approaches? Is this outside of the scope of a 3d renderer and should be on the engine side? I'd love to receive and kind of advice or suggestions!
      Thanks!
    • By nedondev
      I 'm learning how to create game by using opengl with c/c++ coding, so here is my fist game. In video description also have game contain in Dropbox. May be I will make it better in future.
      Thanks.
  • Popular Now