Du bist nicht angemeldet.

Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!

Werbeanzeige

  • »Johannes Schneider« ist der Autor dieses Themas

Beiträge: 103

Beruf: Chemiestudent

  • Private Nachricht senden

1

25.02.2013, 20:04

Dx11: Matritzen durch Konstante Buffer zu HLSL-Shadern übertragen

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.
"Das Glück des Forschers besteht nicht darin, die Wahrheit zu besitzen, sondern eine Wahrheit zu erringen. Und in diesem fortschreitendem, erfolgreichen Suchen nach der Wahrheit - darin liegt die
eigentliche Befriedigung." Max Planck

  • »Johannes Schneider« ist der Autor dieses Themas

Beiträge: 103

Beruf: Chemiestudent

  • Private Nachricht senden

2

25.02.2013, 20:28

Ich habs!
Ein Blick in Tutorial 4 der DX Dokumentation hilft schon weiter ^^

dort steht geschrieben:


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
// Create the constant buffer
    bd.Usage = D3D11_USAGE_DEFAULT;
    bd.ByteWidth = sizeof(ConstantBuffer);
    bd.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
    bd.CPUAccessFlags = 0;
    hr = g_pd3dDevice->CreateBuffer( &bd, NULL, &g_pConstantBuffer );
    if( FAILED( hr ) )
        return hr;

    // Initialize the world matrix
    g_World = XMMatrixIdentity();

    // Initialize the view matrix
    XMVECTOR Eye = XMVectorSet( 0.0f, 1.0f, -5.0f, 0.0f );
    XMVECTOR At = XMVectorSet( 0.0f, 1.0f, 0.0f, 0.0f );
    XMVECTOR Up = XMVectorSet( 0.0f, 1.0f, 0.0f, 0.0f );
    g_View = XMMatrixLookAtLH( Eye, At, Up );

    // Initialize the projection matrix
    g_Projection = XMMatrixPerspectiveFovLH( XM_PIDIV2, width / (FLOAT)height, 0.01f, 100.0f );

   // Später beim rendern

    //
    // Update variables
    //
    ConstantBuffer cb;
    cb.mWorld = XMMatrixTranspose( g_World );
    cb.mView = XMMatrixTranspose( g_View );
    cb.mProjection = XMMatrixTranspose( g_Projection );
    g_pImmediateContext->UpdateSubresource( g_pConstantBuffer, 0, NULL, &cb, 0, 0 );


usw. Wie gesagt Tutorial 4 der DX Doku.

Johannes Schneider
"Das Glück des Forschers besteht nicht darin, die Wahrheit zu besitzen, sondern eine Wahrheit zu erringen. Und in diesem fortschreitendem, erfolgreichen Suchen nach der Wahrheit - darin liegt die
eigentliche Befriedigung." Max Planck