Hallo zusammen.
Ich habe vor einer Woche meine DX9 Vergangenheit begraben und bin auf DX11 umgestiegen.
Allerdings sprechen alle Artikel und Tutorials von "konstanten DX11 Buffern", mit denen man die (Sicht- Projektions- Welt-) Matrix auf HLSL-Shader übertragen kann oder so.
Davon hab ich noch nie was gehört. Bei DX9 hab ich wenig mit Shadern gearbeitet und die gute alte "SetTramsform" Methode wurde scheinbar entfernt.
Ich bin mit meinem bisherigen Code soweit:
|
C-/C++-Quelltext
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
// create the vertex buffer
D3D11_BUFFER_DESC bd;
ZeroMemory(&bd, sizeof(bd));
bd.Usage = D3D11_USAGE_DYNAMIC; // write access access by CPU and GPU
bd.ByteWidth = sizeof(VERTEX) * m_vertexCount; // size is the VERTEX struct * 3
bd.BindFlags = D3D11_BIND_VERTEX_BUFFER; // use as a vertex buffer
bd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; // allow CPU to write in buffer
// Vertexbuffer erstellen
DieCode( m_lpDevice->CreateBuffer(&bd, NULL, &m_vertexBuffer) ); // create the buffer
// copy the vertices into the buffer
D3D11_MAPPED_SUBRESOURCE ms;
DieCode( m_DeviceContext->Map(m_vertexBuffer, NULL, D3D11_MAP_WRITE_DISCARD, NULL, &ms) ); // map the buffer
memcpy(ms.pData, OurVertices, sizeof(OurVertices)); // copy the data
m_DeviceContext->Unmap(m_vertexBuffer, NULL);
// Setup the description of the dynamic matrix constant buffer that is in the vertex shader.
matrixBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
matrixBufferDesc.ByteWidth = sizeof(MatrixBufferType);
matrixBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
matrixBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
matrixBufferDesc.MiscFlags = 0;
matrixBufferDesc.StructureByteStride = 0;
// Create the constant buffer pointer so we can access the vertex shader constant buffer from within this class.
HRESULT m_hx = m_lpDevice->CreateBuffer(&matrixBufferDesc, NULL, &m_matrixBuffer);
|
Dann beim Rendern:
|
C-/C++-Quelltext
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
HRESULT result;
D3D11_MAPPED_SUBRESOURCE mappedResource;
MatrixBufferType* dataPtr;
unsigned int bufferNumber;
// Berechnungen anstellen!
m_cam->Calculations();
// Transpose the matrices to prepare them for the shader.
// Kommentar: wie auch immer...
D3DXMatrixTranspose(&m_cam->m_worldMatrix, &m_cam->m_worldMatrix);
D3DXMatrixTranspose(&m_cam->m_viewMatrix, &m_cam->m_viewMatrix);
D3DXMatrixTranspose(&m_cam->m_projMatrix, &m_cam->m_projMatrix);
// Lock the constant buffer so it can be written to.
result = m_DeviceContext->Map(m_matrixBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
if(FAILED(result))
{
return S_FALSE;
}
// Get a pointer to the data in the constant buffer.
dataPtr = (MatrixBufferType*)mappedResource.pData;
// Copy the matrices into the constant buffer.
dataPtr->world = m_cam->m_worldMatrix;
dataPtr->view = m_cam->m_viewMatrix;
dataPtr->projection = m_cam->m_projMatrix;
// Unlock the constant buffer.
m_DeviceContext->Unmap(m_matrixBuffer, 0);
// Set the position of the constant buffer in the vertex shader.
bufferNumber = 0;
// Finanly set the constant buffer in the vertex shader with the updated values.
m_DeviceContext->VSSetConstantBuffers(bufferNumber, 1, &m_matrixBuffer);
|
Ich glaub dass mit dem HLSL Shader was nicht stimmt. Der Displaytreiber türzt völlig ab wenn ich diesen Code verwende:
|
HLSL-Quelltext
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
cbuffer MatrixBuffer
{
matrix worldMatrix;
matrix viewMatrix;
matrix projectionMatrix;
};
struct VOut
{
float4 position : SV_POSITION;
float4 color : COLOR;
};
VOut VShader(float4 position : POSITION, float4 color : COLOR)
{
VOut output;
// Hier krachts gewaltig
output.position = mul(input.position, worldMatrix);
output.position = mul(output.position, viewMatrix);
output.position = mul(output.position, projectionMatrix);
output.color = color;
return output;
}
float4 PShader(float4 position : SV_POSITION, float4 color : COLOR) : SV_TARGET
{
return color;
}
|
Könnte mir bitte jemand behiflich sein? Ich steh am Anfang des "Erstes-Dreieck" Programm und kann nichtmal die Kamera bewegen lassen weil die Shader streiken.
Danke für jegliche Hilfe
Johannes Schneider.