Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Create Vertices within the HLSL shader


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
13 replies to this topic

#1 PavelB   Members   -  Reputation: 106

Like
0Likes
Like

Posted 12 May 2012 - 11:27 PM

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?

Sponsor:

#2 Nik02   Crossbones+   -  Reputation: 2914

Like
1Likes
Like

Posted 12 May 2012 - 11:47 PM

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, 13 May 2012 - 12:01 AM.

Niko Suni


#3 PavelB   Members   -  Reputation: 106

Like
0Likes
Like

Posted 13 May 2012 - 06:35 AM

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, 13 May 2012 - 06:52 AM.


#4 Nik02   Crossbones+   -  Reputation: 2914

Like
0Likes
Like

Posted 13 May 2012 - 07:12 AM

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:

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;

}



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.

Niko Suni


#5 PavelB   Members   -  Reputation: 106

Like
0Likes
Like

Posted 13 May 2012 - 07:45 AM

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;
   }
}


#6 Nik02   Crossbones+   -  Reputation: 2914

Like
0Likes
Like

Posted 13 May 2012 - 07:53 AM

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:


cbuffer cbParameters {

float4 origin;

float4 radius;

// other consts here

}



// VS code here



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.

Niko Suni


#7 Nik02   Crossbones+   -  Reputation: 2914

Like
0Likes
Like

Posted 13 May 2012 - 07:59 AM

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, 13 May 2012 - 08:04 AM.

Niko Suni


#8 PavelB   Members   -  Reputation: 106

Like
0Likes
Like

Posted 13 May 2012 - 08:27 AM

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]
[size="2"]deviceContext->IASetIndexBuffer(m_indexBuffer, DXGI_FORMAT_R32_UINT, 0); [/size]
[size="2"]deviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); [/size]

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, 13 May 2012 - 08:28 AM.


#9 Nik02   Crossbones+   -  Reputation: 2914

Like
0Likes
Like

Posted 13 May 2012 - 08:30 AM

The vertex id is the index of the current vertex.

Niko Suni


#10 PavelB   Members   -  Reputation: 106

Like
0Likes
Like

Posted 13 May 2012 - 08:35 AM

But how does it know how many vertices in model - 3 or 300000.
is it Draw(300000, 0) tell about it?

Edited by PavelB, 13 May 2012 - 08:36 AM.


#11 Nik02   Crossbones+   -  Reputation: 2914

Like
0Likes
Like

Posted 13 May 2012 - 09:57 AM

If you call Draw(300000,0), your vertex shader will be invoked 300000 times, and the SV_VertexId parameter is incremented at each vertex. You decide what you want to do with it :)

Niko Suni


#12 Nik02   Crossbones+   -  Reputation: 2914

Like
0Likes
Like

Posted 13 May 2012 - 10:05 AM

It is best to think it this way: since you don't get anything from vertex buffers, you are not limited to any previously allocated memory limits.

There is a practical limitation on the number of vertices per draw call (due to driver timeouts), but the figure is well in millions.

Edited by Nik02, 13 May 2012 - 10:08 AM.

Niko Suni


#13 PavelB   Members   -  Reputation: 106

Like
0Likes
Like

Posted 13 May 2012 - 02:37 PM

I don't know. Something wrong with my understanding of what you are saying. Below I show the logic I am using and it works for the triangle, but it doesn't work for square.
As you see below I commented a lot of code and it still consistant. It works, but show only one of the triangles of the square. depends on which one of them are 0,1,2. When I replace the order it shows one triangle or another.

ID3DBlob* pVSBlob = NULL;
hr = CompileShaderFromFile( L"Tutorial02.fx", "vshader", "vs_4_0", &pVSBlob );
if( FAILED( hr ) )
{
  MessageBox( NULL,
	 L"The FX file cannot be compiled.  Please run this executable from the directory that contains the FX file.", L"Error", MB_OK );
  return hr;
}
// Create the vertex shader
hr = g_pd3dDevice->CreateVertexShader( pVSBlob->GetBufferPointer(), pVSBlob->GetBufferSize(), NULL, &g_pVertexShader );
if( FAILED( hr ) )
{
  pVSBlob->Release();
  return hr;
}
//// Define the input layout
//D3D11_INPUT_ELEMENT_DESC layout[] =
//{
// { "SV_VertexId", 0, DXGI_FORMAT_R32G32B32_UINT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
//};
//UINT numElements = ARRAYSIZE( layout );
//// Create the input layout
//hr = g_pd3dDevice->CreateInputLayout( layout, numElements, pVSBlob->GetBufferPointer(),
//		   pVSBlob->GetBufferSize(), &g_pVertexLayout );
//pVSBlob->Release();
//if( FAILED( hr ) )
// return hr;
// Set the input layout
//g_pImmediateContext->IASetInputLayout( NULL );
// Compile the pixel shader
ID3DBlob* pPSBlob = NULL;
hr = CompileShaderFromFile( L"Tutorial02.fx", "PS", "ps_4_0", &pPSBlob );
if( FAILED( hr ) )
{
  MessageBox( NULL,
	 L"The FX file cannot be compiled.  Please run this executable from the directory that contains the FX file.", L"Error", MB_OK );
  return hr;
}
// Create the pixel shader
hr = g_pd3dDevice->CreatePixelShader( pPSBlob->GetBufferPointer(), pPSBlob->GetBufferSize(), NULL, &g_pPixelShader );
pPSBlob->Release();
if( FAILED( hr ) )
  return hr;
//// Create vertex buffer
//SimpleVertex vertices[] =
//{
// XMFLOAT3( 0.0f, 0.5f, 0.5f ),
// XMFLOAT3( 0.5f, -0.5f, 0.5f ),
// XMFLOAT3( -0.5f, -0.5f, 0.5f ),
//};
//D3D11_BUFFER_DESC bd;
//ZeroMemory( &bd, sizeof(bd) );
//bd.Usage = D3D11_USAGE_DEFAULT;
//bd.ByteWidth = sizeof( SimpleVertex ) * 3;
//bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
//bd.CPUAccessFlags = 0;
//D3D11_SUBRESOURCE_DATA InitData;
//ZeroMemory( &InitData, sizeof(InitData) );
//InitData.pSysMem = vertices;
//hr = g_pd3dDevice->CreateBuffer( &bd, &InitData, &g_pVertexBuffer );
//if( FAILED( hr ) )
// return hr;
//// Set vertex buffer
//UINT stride = sizeof( SimpleVertex );
//UINT offset = 0;
//g_pImmediateContext->IASetVertexBuffers( 0, 1, &g_pVertexBuffer, &stride, &offset );
// Set primitive topology
g_pImmediateContext->IASetPrimitiveTopology( D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST );

//--------------------------------------------------------------------------------------
// File: Tutorial02.fx
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//--------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------
// Vertex Shader
//--------------------------------------------------------------------------------------
float4 VS( float4 Pos : POSITION ) : SV_POSITION
{
return Pos;
}

//--------------------------------------------------------------------------------------
// Pixel Shader
//--------------------------------------------------------------------------------------
float4 PS( float4 Pos : SV_POSITION ) : SV_Target
{
return float4( 1.0f, 1.0f, 0.0f, 1.0f );    // Yellow, with Alpha = 1
}
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 3:
    ret.pos = float4(-0.1,-0.1,1,1);
    break;
  case 4:
   ret.pos = float4(0.1,0.1,1,1);	
    break;
  case 5:	
    ret.pos = float4(0.1,-0.1,1,1);	
   break;
  case 0:
    ret.pos = float4(-0.1,-0.1,1,1);
    break;
  case 1:
   ret.pos = float4(-0.1,0.1,1,1);	
    break;
  case 2:	
    ret.pos = float4(0.1,0.1,1,1);	
   break;
}
return ret;
}


#14 Nik02   Crossbones+   -  Reputation: 2914

Like
0Likes
Like

Posted 13 May 2012 - 10:55 PM

The backface culling happens after the primitives have been generated.

The geometric normal of the triangle determines whether it is front facing or back facing. The geometric normal of the triangle is cross product of the normalized triangle edges p1-p0 and p2-p0. If the z component of the cross product is negative, the triangle is considered facing away from the "camera" and therefore invisible (in ordinary rendering). If you exchange places of any two points of the triangle, the cross product's direction (and of course the z component) is negated, and the triangle is effectively flipped to its back.

You can switch of the backface culling by using an appropriate rasterizer state; however, to optimize performance, keep it on and ensure that your triangles are wound consistently.

Edited by Nik02, 13 May 2012 - 11:11 PM.

Niko Suni





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS