Hello!I was wondering : is it possible to generate meshes just using shaders?Or deform already generated meshes with their wireframe?Can you calculate perlin noise using them?Thanks in advance!Any anwser is greatly apriciated!
3 Answers
3It is possible to generate a mesh just using Shaders, but not from nothing. Here's why (simplified version):
Shaders are programs that are executed on key positions in the graphics pipeline. The pipeline is a series of steps on the graphics card through which all geometry goes. Geometry starts out as vertices ordered into meshes (as you know) and the first steps involves "primitive assembly", that is, the assembly of vertices into the polygons they're a part of. After this point, the execution of a vertex shader takes place that optionally transforms each vertex further by changing its color, position, UV coordinates, or a wide range of other things.
When the vertex shader has finished executing on every vertex of a model, the model is rasterized into screen-space candicates for pixel updates to the frame buffer, the so-called "fragments" of the model. The reason they're not pixels yet but only candidates is that they might not get written to the frame buffer (they might fail a depth-test, for instance, because they're obscured by some other object). This is where the fragment shader kicks in - just like the vertex shader operates on every vertex of a model, the fragment shader operates on every fragment of the rasterized model, after it has "become pixels", to put it bluntly.
But the fragments passed to the fragment shader do not necessarily have to stem from just the geometry that originally entered the graphics card (i.e. a mesh generated on the CPU side). After DirectX10/OpenGL 3.2, a type of shader that can create new geometry at the GPU level was introduced to the pipeline. This is the so called "Geometry Shader", and its execution takes place after the vertex shader. Its input is the whole primitive, that is, the whole mesh currently being rendered, and it can generate new geometry based on the input, so it effectively changes what is rasterized into fragments at the GPU level. That's the key point in the answer to your question: Yes, you can generate mesh on the graphics card, but it requires that you write a Geometry Shader, and as far as I know, that you pass something to it to begin with, I don't think it executes unless something is already passing through the pipeline.
You can't generate new geometry with just vertex and fragment programs, because as explained, they operate on vertices that are sent through the pipeline from the CPU side, and the fragments that result from the rasterization of these vertices.
Your other two questions are easy. :)
Yes, you can deform already generated meshes by their wireframe, that is what the vertex shader does when you change the position of the vertices passed to it.
Yes, you can calculate perlin noise using shaders, see this link for an example. There are other examples if you google for it a bit.
here is an example of mesh generated using DX 11 … including some source code
Your blog needs authentication now. Perhaps it did not when you posted this, but anyhow I am very interested to see the source codes generating mesh with DX 11 ...
– sasantvActually with DX11 it is now possible to create all geometry on the GPU. We create a vertex buffer with a Compute Shader and push it into the render pipeline.
And if you use a ComputeShader, you can pull the resulting mesh back from the GPU to create or modify a mesh collider with it. Be forewarned however that pushing data to and from the GPU is expensive.
As an added note, just for the sake of correctness: I mentioned in my answer that the vertex shader executes after primitive assembly. It doesn't. Primitive assembly comes after vertex shading. :)
– CHPedersenThis answer is outdated. DX11 brought with it ways to create arbitrary geometry on the GPU using either computer shaders or hull shaders.
– Jeff-KesselmanAlthough compute shaders can be used for arbitrary calculations that includes (but is not limited to) geometry generation, We do this for our metaball system. We generate vertex buffers in memory on the card and then pass then through to the render pipeline for rendering.
– Jeff-Kesselman