#1 Members - Reputation: 103
Posted 12 May 2012 - 11:27 PM
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?
#2 Members - Reputation: 1991
Posted 12 May 2012 - 11:47 PM
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.
Software developer
#3 Members - Reputation: 103
Posted 13 May 2012 - 06:35 AM
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 Members - Reputation: 1991
Posted 13 May 2012 - 07:12 AM
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.
Software developer
#5 Members - Reputation: 103
Posted 13 May 2012 - 07:45 AM
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 Members - Reputation: 1991
Posted 13 May 2012 - 07:53 AM
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.
Software developer
#7 Members - Reputation: 1991
Posted 13 May 2012 - 07:59 AM
Edited by Nik02, 13 May 2012 - 08:04 AM.
Software developer
#8 Members - Reputation: 103
Posted 13 May 2012 - 08:27 AM
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. ......... CodeRender 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.
#12 Members - Reputation: 1991
Posted 13 May 2012 - 10:05 AM
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.
Software developer
#13 Members - Reputation: 103
Posted 13 May 2012 - 02:37 PM
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 Members - Reputation: 1991
Posted 13 May 2012 - 10:55 PM
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.
Software developer






