[Hilfe] Dx11 Simples Problem

Für Fragen zu Grafik APIs wie DirectX und OpenGL sowie Shaderprogrammierung.
Antworten
AlClemento
Beiträge: 12
Registriert: 16.04.2010, 20:01

[Hilfe] Dx11 Simples Problem

Beitrag von AlClemento »

Hallo ZFX'ler!

Also ich programmiere gerade an einer kleinen Demo mit Dx11(June 2010) und brauche eure Hilfe bei einem Problem, welches mich seit gestern beschäftigt.

Das Problem ist folgendes: ich sehe kein Dreieck bei meinem PostProcess Effekt (onder sonst irgendwie). Das klingt jetzt nach einem recht einfachen Problem, jedoch hab ich mit PIX schon mehrmals die Schritte mit dem SDK-Tutorial07 verglichen, finde den Fehler allerdings nicht. Ich denke es wird ein recht einfacher kleiner (und somit peinlicher) Fehler werden, nur komme ich einfach nicht dahinter.

Wie bereits erwähnt will ich einfach nur einen PostProcessing-Effekt ausgeben. zZ soll das nur eine Farbe sein (0,1,1,1). Im Pix sieht man schön, dass die 2 Dreiecke korrekt auf dem Screen verteilt werden, der Pixelshader wird allerdings nicht aufgerufen. Deswegen hab ich euch die Pixrun-Datei des ersten Frames angehängt (interessant wirds erst ab EID 102 - vorher sind meist nur Allocations, die ich für später brauchen werde);

Vielen Dank schon im Vorraus!

MfG, Alc

Natürlich darf ein wenig Code auch nicht fehlen:

Kurze Erklärung: zuerst wird die initdev() aufgerufen, in der Drawingloop kommt dann die render() zum Einsatz, welche auf die PostTone/Mesh zugreift

Code: Alles auswählen

D3D_DRIVER_TYPE driverType = D3D_DRIVER_TYPE_NULL;
D3D_FEATURE_LEVEL featureLevel = D3D_FEATURE_LEVEL_11_0;

IDXGISwapChain* finalPassSwapChain = NULL;
ID3D11RenderTargetView* finalPassRTV = NULL;
D3D11_VIEWPORT finalPassVP = D3D11_VIEWPORT();

ID3D11RasterizerState* rasterizer = NULL;

HRESULT initdev(){
	HRESULT hr = S_OK;

    UINT createDeviceFlags = 0;
#ifdef _DEBUG
    createDeviceFlags |= D3D11_CREATE_DEVICE_DEBUG;
#endif

    D3D_DRIVER_TYPE driverTypes[] =
    {
        D3D_DRIVER_TYPE_HARDWARE,
        D3D_DRIVER_TYPE_WARP,
        D3D_DRIVER_TYPE_REFERENCE,
    };
    UINT numDriverTypes = ARRAYSIZE( driverTypes );

    D3D_FEATURE_LEVEL featureLevels[] =
    {
        D3D_FEATURE_LEVEL_11_0,
        D3D_FEATURE_LEVEL_10_1,
        D3D_FEATURE_LEVEL_10_0,
    };
    UINT numFeatureLevels = ARRAYSIZE( featureLevels );

    DXGI_SWAP_CHAIN_DESC sd;
    ZeroMemory( &sd, sizeof( sd ) );
    sd.BufferCount = 1;
    sd.BufferDesc.Width = window_width;
    sd.BufferDesc.Height = window_height;
    sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM_SRGB;
    sd.BufferDesc.RefreshRate.Numerator = 60;
    sd.BufferDesc.RefreshRate.Denominator = 1;
	sd.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;
    sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
    sd.OutputWindow = g_hWnd;
    sd.SampleDesc.Count = 1;
    sd.SampleDesc.Quality = 0;
    sd.Windowed = TRUE;

    for( UINT driverTypeIndex = 0; driverTypeIndex < numDriverTypes; driverTypeIndex++ )
    {
        driverType = driverTypes[driverTypeIndex];
        hr = D3D11CreateDeviceAndSwapChain( NULL, driverType, NULL, createDeviceFlags, featureLevels, numFeatureLevels,
                                            D3D11_SDK_VERSION, &sd, &finalPassSwapChain, &dxDevice, &featureLevel, &dxContext );
        if( SUCCEEDED( hr ) )
            break;
    }
    if( FAILED( hr ) )
        return hr;

    // Create a render target view
    ID3D11Texture2D* pBackBuffer = NULL;
    hr = finalPassSwapChain->GetBuffer( 0, __uuidof( ID3D11Texture2D ), ( LPVOID* )&pBackBuffer );
    if( FAILED( hr ) )
        return hr;

    hr = dxDevice->CreateRenderTargetView( pBackBuffer, NULL, &finalPassRTV );
    pBackBuffer->Release();
    if( FAILED( hr ) )
        return hr;

    finalPassVP.Width = (FLOAT)window_width;
    finalPassVP.Height = (FLOAT)window_height;
    finalPassVP.MinDepth = 0.0f;
    finalPassVP.MaxDepth = 1.0f;
    finalPassVP.TopLeftX = 0;
    finalPassVP.TopLeftY = 0;

	D3D11_RASTERIZER_DESC rd;
	rd.FillMode = D3D11_FILL_SOLID;
	rd.CullMode = D3D11_CULL_NONE;
	rd.FrontCounterClockwise = FALSE;
	rd.DepthBias = FALSE;
	rd.DepthBiasClamp = 0.0f;
	rd.SlopeScaledDepthBias = 0.0f;
	rd.DepthClipEnable = FALSE;
	rd.ScissorEnable = FALSE;
	rd.MultisampleEnable = FALSE;
	rd.AntialiasedLineEnable = FALSE; //TODO: or true???
	hr = dxDevice->CreateRasterizerState(&rd,&rasterizer); HR_RETURN(hr);

    return S_OK;
}

Code: Alles auswählen

void render(float timeDelta, float absoluteTime)
{
	if(curScene && curScene->eye) {
		dxContext->RSSetViewports(1,&finalPassVP);
		dxContext->RSSetState(rasterizer);

		float ClearColor[4] = { 0.0f, 0.125f, 0.3f, 1.0f }; // red, green, blue, alpha
		dxContext->ClearRenderTargetView( finalPassRTV, ClearColor );
		dxContext->OMSetRenderTargets(1,&finalPassRTV,NULL);
		dxContext->OMSetBlendState(NULL,NULL, 0);
		allShaders->postTone->onFrame(1.2f);
		allShaders->postTone->Setup();
		allShaders->postTone->setHDRTex(&(curScene->eye->composed->shaderres));
		allShaders->postTone->doPostProc();
    }

	finalPassSwapChain->Present( 0, 0 );

}
PostTone.cpp:

Code: Alles auswählen

PostTone::PostTone(HRESULT* r) : vertexShader(NULL), pixelShader(NULL), cbNeverChanges(NULL), cbChangesEveryFrame(NULL), vertexLayout(NULL), scbNeverChanges(), scbChangesEveryFrame()
{
	HRESULT hr = S_OK;

	//SHADERS
	
	ID3DBlob* pVSBlob = NULL;
    hr = CompileShaderFromFile( L"PostTone.fx", "VS", "vs_5_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 );
		if(r) *r = hr; return;
    }

	hr = dxDevice->CreateVertexShader( pVSBlob->GetBufferPointer(), pVSBlob->GetBufferSize(), NULL, &vertexShader );
    if( FAILED( hr ) )
    {    
        pVSBlob->Release();
		if(r) *r = hr; return;
    }
	D3D11_INPUT_ELEMENT_DESC layout[] =
    {
        { "POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
        { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 8, D3D11_INPUT_PER_VERTEX_DATA, 0 },
    };
    UINT numElements = ARRAYSIZE( layout );

    // Create the input layout
	hr = dxDevice->CreateInputLayout( layout, numElements, pVSBlob->GetBufferPointer(),
                                          pVSBlob->GetBufferSize(), &vertexLayout );
    pVSBlob->Release();
    HR_STOP_CONST(hr,r);

	ID3DBlob* pPSBlob = NULL;
    hr = CompileShaderFromFile( L"PostTone.fx", "PS", "ps_5_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 );
        if(r) *r = hr; return;
    }

    // Create the pixel shader
    hr = dxDevice->CreatePixelShader( pPSBlob->GetBufferPointer(), pPSBlob->GetBufferSize(), NULL, &pixelShader );
    pPSBlob->Release();
    HR_STOP_CONST(hr,r);

	//BUFFERS
	D3D11_BUFFER_DESC bd;
    ZeroMemory( &bd, sizeof(bd) );

	bd.Usage = D3D11_USAGE_DEFAULT;
    bd.ByteWidth = sizeof(scbNeverChanges);
    bd.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
    bd.CPUAccessFlags = 0;
    hr = dxDevice->CreateBuffer( &bd, NULL, &cbNeverChanges );
	HR_STOP_CONST(hr,r);

	bd.Usage = D3D11_USAGE_DEFAULT;
    bd.ByteWidth = sizeof(scbChangesEveryFrame);
    bd.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
    bd.CPUAccessFlags = 0;
    hr = dxDevice->CreateBuffer( &bd, NULL, &cbChangesEveryFrame );
	HR_STOP_CONST(hr,r);

	scbNeverChanges.lumV.x = 0.2126f;
	scbNeverChanges.lumV.y = 0.7152f;
	scbNeverChanges.lumV.z = 0.0722f;
	scbNeverChanges.lumV.w = 0.0f;
    dxContext->UpdateSubresource( cbNeverChanges, 0, NULL, &scbNeverChanges, 0, 0 );
}

PostTone::~PostTone() {
	SAFE_RELEASE(vertexShader);
	SAFE_RELEASE(pixelShader);
	SAFE_RELEASE(cbNeverChanges);
	SAFE_RELEASE(cbChangesEveryFrame);
	SAFE_RELEASE(vertexLayout);
}

void PostTone::Setup() {
	dxContext->IASetPrimitiveTopology( D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST );
	dxContext->IASetInputLayout(vertexLayout);
	dxContext->GSSetShader(NULL,NULL,0);
	dxContext->HSSetShader(NULL,NULL,0);
	dxContext->DSSetShader(NULL,NULL,0);
	dxContext->VSSetShader( vertexShader, NULL, 0 );
    dxContext->VSSetConstantBuffers( 0, 1, &cbNeverChanges );
    dxContext->VSSetConstantBuffers( 1, 1, &cbChangesEveryFrame );
	dxContext->PSSetShader( pixelShader, NULL, 0 );
	dxContext->PSSetSamplers( 0, 1, &srSamplerLinear );
	dxContext->PSSetConstantBuffers( 0, 1, &cbNeverChanges );
    dxContext->PSSetConstantBuffers( 1, 1, &cbChangesEveryFrame );
}

void PostTone::onFrame(FLOAT avgLuminance) {
	scbChangesEveryFrame.avgLum.x = avgLuminance;
	scbChangesEveryFrame.avgLum.y = avgLuminance;
	scbChangesEveryFrame.avgLum.z = avgLuminance;
	scbChangesEveryFrame.avgLum.w = avgLuminance;
	dxContext->UpdateSubresource( cbChangesEveryFrame, 0, NULL, &scbChangesEveryFrame, 0, 0 );
}

void PostTone::setHDRTex(ID3D11ShaderResourceView** hdrTex) {
	dxContext->PSSetShaderResources( 0, 1, hdrTex );
};

void PostTone::doPostProc() {
	srPostProcMesh->setToContext();
	srPostProcMesh->draw();
}

#endif
PostTone.fx

Code: Alles auswählen

Texture2D hdrTex : register( t0 );
SamplerState samLinear : register( s0 );

cbuffer cbNeverChanges : register( b0 )
{
    float4 lumV;
};

cbuffer cbChangesEveryFrame : register( b1 )
{
	float4 avgLum;
};

//--------------------------------------------------------------------------------------
struct VS_INPUT
{
    float2 Pos : POSITION;
    float2 Tex : TEXCOORD0;
};

struct PS_INPUT
{
    float4 Pos : SV_POSITION;
    float2 Tex : TEXCOORD0;
};

PS_INPUT VS( VS_INPUT input )
{
    PS_INPUT output = (PS_INPUT)0;
    output.Pos.xy = input.Pos;
	output.Pos.z = 0.0f;
	output.Pos.w = 1.0f;
    output.Tex = input.Tex;
    
    return output;
}

float4 PS( PS_INPUT input) : SV_TARGET
{
	return float4(0.0f,1.0f,1.0f,1.0f);
}
Mesh.ccp

Code: Alles auswählen

Mesh::Mesh(void* vertices, UINT numV, UINT _stride, UINT _offset, WORD* indices, UINT numI, HRESULT* r) : numIndeces(0), numVertices(0), stride(0), offset(0), vertexBuffer(NULL), indexBuffer(NULL)  {
	HRESULT hr = S_OK;

	numVertices = numV;
	stride = _stride;
	offset = _offset;
	numIndeces = numI;

	D3D11_BUFFER_DESC bd; ZeroMemory( &bd, sizeof(bd) );
    bd.Usage = D3D11_USAGE_DEFAULT;
	bd.ByteWidth = stride * numVertices;
    bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
    bd.CPUAccessFlags = 0;
    D3D11_SUBRESOURCE_DATA InitData; ZeroMemory( &InitData, sizeof(InitData) );
    InitData.pSysMem = vertices;
    hr = dxDevice->CreateBuffer( &bd, &InitData, &vertexBuffer );
	HR_STOP_CONST(hr,r);

	bd.Usage = D3D11_USAGE_DEFAULT;
    bd.ByteWidth = sizeof( WORD ) * numIndeces;
    bd.BindFlags = D3D11_BIND_INDEX_BUFFER;
    bd.CPUAccessFlags = 0;
	InitData.pSysMem = indices;
    hr = dxDevice->CreateBuffer( &bd, &InitData, &indexBuffer );
}

Mesh::~Mesh() {
	SAFE_RELEASE(indexBuffer);
	SAFE_RELEASE(vertexBuffer);
}

void Mesh::setToContext() {
	dxContext->IASetVertexBuffers(0,1,&vertexBuffer,&stride,&offset);
	dxContext->IASetIndexBuffer(indexBuffer,DXGI_FORMAT_R16_UINT,0);
}

void Mesh::draw() {
	dxContext->DrawIndexed(numIndeces,0,0);
}
Initshaders.cpp (Geometrie-Teil):

Code: Alles auswählen

PostProcVertex vertices[] =
    {
        { XMFLOAT2( -1.0f,  1.0f ), XMFLOAT2( 0.0f, 0.0f ) },
        { XMFLOAT2(  1.0f,  1.0f ), XMFLOAT2( 1.0f, 0.0f ) },
        { XMFLOAT2( -1.0f, -1.0f ), XMFLOAT2( 0.0f, 1.0f ) },
        { XMFLOAT2(  1.0f, -1.0f ), XMFLOAT2( 1.0f, 1.0f ) },
	 };

	WORD indices[] =
    {
        0,1,2,
		1,3,2
    };

	srPostProcMesh = new Mesh((void*)vertices,4,sizeof(PostProcVertex),0,indices,6,&hr);
	HR_RETURN(hr);
Dateianhänge
CuberichTestRunFrame1.zip
Pixrun des ersten Frames
(5.66 KiB) 162-mal heruntergeladen
AlClemento
Beiträge: 12
Registriert: 16.04.2010, 20:01

Re: [Hilfe] Dx11 Simples Problem

Beitrag von AlClemento »

Danke, hat sich erübrigt: den BlendState darf man natürlich nicht auf NULL setzen.
Antworten