Jump to content
  • Advertisement
Sign in to follow this  
PavelB

Create Vertices within the HLSL shader

This topic is 2353 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi,

I have the question regarding HLSL. When I create new model I have to give all vertices to the HLSL. The question is if it is possible to create all vertices within the shader itself. Here small example of what I am asking about. I'd like to send for instant the coordinates of the centre of the square and half diagonal length. And program I will create in shader will calculate all 6 coordinate by itself and "in the loop somehow" will execute it. In this case, my CPU won't work on this, but GPU itself.

Is it possible to do something like this?

Share this post


Link to post
Share on other sites
Advertisement
In D3D10 and up, yes.

Use an unsigned integer vertex shader parameter with semantic SV_VertexID, and don't use any other parameters that would be supplied by the input assembler (vertex attributes).

You can branch your VS logic according to the id parameter; just return a different output on each id value.

Additionally, you can create a (limited by memory) set of new vertices dynamically for each input primitive by using a geometry shader. Edited by Nik02

Share this post


Link to post
Share on other sites
I am new at shaders. I can find a lot of examples that would help me with the traditional ways of computing and very little with what I describe above. Could you give me some tutorials to read about this technique or even small working small sample of the example above and I will try using that for what I really want to try (see below)

In ideal variant for the square I'd like to do something like this:
I will send 3 parameters:
- Polar System Coordinate vector from "World Space" origin to the origin of the square's Object Polar Coordinate System;
- Quadrion of the orientation for the origin "unit" vector of the square's Object Polar Coordinate System;
- Polar System Coordinate vector to the point of one of the corners of the square within Object Polar Coordinate System.

It is it and then shader create the vertice buffer by itself.

P.S. As bonus, I'd like to keep the third vector in GPU as long as possible during the time my application is running. Only 2 first parameters will be changed between frames to do some animation. Edited by PavelB

Share this post


Link to post
Share on other sites
Well, the three "parameters" would best fit in a constant buffer.

If you would have bigger datasets, it would be best to split the constant data to multiple buffers based on the update frequency. However, the three vectors do not require very much memory, so a single constant buffer would suffice here (unless you actually want to store some variable separately from the others for some other reason).

The basic vertex shader that doesn't require vertex buffers would look something like this:
[source]
struct VS_OUT
{
float4 pos : SV_Position;
// insert other vs output fields here as needed
};
// vertex shader that draws a triangle without vbuffers
VS_OUT vshader(uint idx : SV_VertexId) {
VS_OUT ret = (VS_OUT)0;
switch (idx) {
case 0:
ret.pos = float4(0,0,0,1);
break;
case 1:
ret.pos = float4(1,0,0,1);
break;
case 2:
ret.pos = float4(0,1,0,1);
break;
}
return ret;
}
[/source]

The logic to calculate the vertex positions as per your requirements is just simple math.

If you need to study the HLSL basics, do read the DX SDK graphics documentation. You will find sample code there, too.

Share this post


Link to post
Share on other sites
Thanks.

It is what I found on SDK, too. Again It has permanent coordinates.

However, I cannot figure out, how I can do complex shader, which includes 2 triangles, as in Square or 400 triangles as for Circle, by sending in only center of those shapes. For instant, here is what I hope to achieve.

struct VS_OUT
{
float4 pos: SV_Position;
};
struct VS_IN
{
float4 origin;
float4 radius;
uint idx : SV_VertexId;
};
VS_OUT vsshader(VS_IN in)
{
VS_OUT ret = (VS_OUT)0;
float4 inRightTop;
... // Calculation of the Right Top corner
float4 inLeftTop;
....
float4 inRightBottom;
....
float4 inLeftBottom;
switch (idx) {
case 0:
ret.pos = inLeftBottom; break;
case 1:
ret.pos = inRightBottom; break;
case 2:
ret.pos = inRightTop; break;
case 3:
ret.pos = inLeftBottom; break;
case 4:
ret.pos = inRightTop; break;
case 5:
ret.pos = inLeftTop; break;
}
}

Share this post


Link to post
Share on other sites
You wouldn't put the "origin" and "radius" to the VS_IN structure. Instead, you would use a constant buffer, which essentially hosts shader-runtime constant memory that you can write from the host program:

[source]
cbuffer cbParameters {
float4 origin;
float4 radius;
// other consts here
}

// VS code here
[/source]

You would then access the "origin" and "radius" in the shader logic by their name, just like local variables.

On the host side, you have methods of creating, filling and setting the buffers.

Share this post


Link to post
Share on other sites
Also note that you are not limited to doing just branching logic with the vertex id. This fact can become handy when generating, say, a circle in the VS (you really don't want a switch with a case for every single vertex). Edited by Nik02

Share this post


Link to post
Share on other sites
This is what I try to understand, how do I give the indices to the shader. In regulat way it works this way:
Initialize Buffer:

......................... Code
// Load the vertex array with data.
vertices[0].position = ... // Bottom left.

vertices[1].position = ... // Bottom Right
vertices[2].position = ... // Top Right
vertices[3].position = ... // Top Left
// Load the index array with data.
indices[0] = 0; // Bottom left.
indices[1] = 1; // Bottom Right.
indices[2] = 2; // Top right.
indices[3] = 0; // Bottom left.
indices[4] = 2; // Top Right.
indices[5] = 3; // Top Left.

......... Code

Render Buffer:

[size="2"]deviceContext->IASetVertexBuffers(0, 1, &m_vertexBuffer, &stride, &offset);
[size="2"]deviceContext->IASetIndexBuffer(m_indexBuffer, DXGI_FORMAT_R32_UINT, 0);
[size="2"]deviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);


Now, what am I doing?
From the samples that I found, at least the simpliest one I should do something like this:

m_pD3D11Device->IASetInputLayout( NULL );
m_pD3D11Device->IASetPrimitiveTopology( D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST );

ID3DX11EffectTechnique * pTech = NULL;
pTech = m_pEffect->GetTechniqueByIndex(0);
pTech->GetPassByIndex(iPass)->Apply(0);
m_pD3D11Device->Draw( 3, 0 );

Where do I set the order of the indices for the triangles? How do I know in shader about it? Edited by PavelB

Share this post


Link to post
Share on other sites
But how does it know how many vertices in model - 3 or 300000.
is it Draw(300000, 0) tell about it? Edited by PavelB

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!