2

I ve been messing around with DirectX and I ve been working on this weird idea of mine for some time now. It seems as if my pixel shader is not being invoked for some odd reason. I dont know if thats even possible or that its some stupid mistake of mine. The Code is as follows (I have copied most of it from the Tutorials.)

Shader (HLSL) Code as follows:

/*
********************************************************************************************************
*   Constant Buffers (Can only change per render call).
********************************************************************************************************
*/
cbuffer SConstantBuffer : register( b0 )
{
    float3 origin;
    float3 direction;
    float planeDistance;
    int screenWidth;
    int screenHeight;
};


/*
********************************************************************************************************
*   Input and Output Structures
********************************************************************************************************
*/
struct VS_INPUT
{
    float3 position     :   POSITION;
    float4 color        :   COLOR;
};

struct VS_OUTPUT
{
    float4 position     :   SV_POSITION;
    float4 color        :   COLOR;
};


/*
********************************************************************************************************
*   Vertex Shader main
********************************************************************************************************
*/
VS_OUTPUT VS( VS_INPUT input )
{
    VS_OUTPUT output = (VS_OUTPUT)0;

    float param_d;
    float3 pixelPoint;
    float3 zAxis = { 0.0, 0.0, 1.0 };
    float3 planeOrigin = origin + (direction * planeDistance);
    float3 pointDirection = (input.position - origin) / length( (input.position - origin) );

    param_d = dot( (planeOrigin - origin), direction ) / dot( pointDirection, direction );
    pixelPoint = ( pointDirection * param_d ) + origin;

    output.position.xyz = cross ( pixelPoint, direction );
    output.position.x = output.position.x * 5.0;
    output.position.y = output.position.y * 5.0;
    output.position.z = 20.0;
    output.position.w = output.position.z;

    output.color = input.color;

    return output;
}


/*
********************************************************************************************************
*   Pixel Shader main
********************************************************************************************************
*/
float4 PS( VS_OUTPUT input ) : SV_Target
{
    return float4( 1.0, 1.0, 1.0, 1.0); //input.color;
}

DirectX Init and Rendering code as Follows:

#include "Renderer.h"

CRenderer::CRenderer ( )
{
    this->m_dxDevice = NULL;
    this->m_dxDeviceContext = NULL;
    this->m_dxRenderTargetView = NULL;
    this->m_dxSwapChain = NULL;
    this->m_dxBackBuffer = NULL;
    this->m_dxVertexLayout = NULL;
    this->m_dxVertexBuffer = NULL;
    this->m_dxPixelShader = NULL;
    this->m_dxConstantBuffer = NULL;
}

HRESULT CompileShaderFromFile( WCHAR* szFileName, LPCSTR szEntryPoint, LPCSTR szShaderModel, ID3DBlob** ppBlobOut )
{
    HRESULT hr = S_OK;

    DWORD dwShaderFlags = D3DCOMPILE_ENABLE_STRICTNESS;
#if defined( DEBUG ) || defined( _DEBUG )
    // Set the D3DCOMPILE_DEBUG flag to embed debug information in the shaders.
    // Setting this flag improves the shader debugging experience, but still allows 
    // the shaders to be optimized and to run exactly the way they will run in 
    // the release configuration of this program.
    dwShaderFlags |= D3DCOMPILE_DEBUG;
#endif

    ID3DBlob* pErrorBlob;
    hr = D3DX11CompileFromFile( szFileName, NULL, NULL, szEntryPoint, szShaderModel, 
        dwShaderFlags, 0, NULL, ppBlobOut, &pErrorBlob, NULL );
    if( FAILED(hr) )
    {
        if( pErrorBlob != NULL )
            OutputDebugStringA( (char*)pErrorBlob->GetBufferPointer() );
        if( pErrorBlob ) pErrorBlob->Release();
        return hr;
    }
    if( pErrorBlob ) pErrorBlob->Release();

    return S_OK;
}

HRESULT CRenderer::InitDevice ( HWND arg_hWnd )
{
    HRESULT status = S_OK;
    this->m_hWnd = arg_hWnd;

    RECT rctWindowSize;
    GetClientRect( arg_hWnd, &rctWindowSize );
    UINT dWidth = rctWindowSize.right - rctWindowSize.left;
    UINT dHeight = rctWindowSize.bottom - rctWindowSize.top;

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

    D3D_DRIVER_TYPE pDriverType [] = 
    {
        D3D_DRIVER_TYPE_HARDWARE,
        D3D_DRIVER_TYPE_WARP,
        D3D_DRIVER_TYPE_REFERENCE
    };
    UINT numDriverType = ARRAYSIZE(pDriverType);

    D3D_FEATURE_LEVEL pFeatureLevel [] =
    {
        D3D_FEATURE_LEVEL_11_0,
        D3D_FEATURE_LEVEL_10_1,
        D3D_FEATURE_LEVEL_10_0
    };
    UINT numFeatureLevel = ARRAYSIZE(pFeatureLevel);


    //Fillout Device Descriptor
    DXGI_SWAP_CHAIN_DESC objSwapChainDesc;
    ZeroMemory(&objSwapChainDesc, sizeof(objSwapChainDesc));
    objSwapChainDesc.BufferCount = 1;
    objSwapChainDesc.BufferDesc.Width = dWidth;
    objSwapChainDesc.BufferDesc.Height = dHeight;
    objSwapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
    objSwapChainDesc.BufferDesc.RefreshRate.Numerator = 60;
    objSwapChainDesc.BufferDesc.RefreshRate.Denominator = 1;
    objSwapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
    objSwapChainDesc.OutputWindow = this->m_hWnd;
    objSwapChainDesc.SampleDesc.Count = 1;
    objSwapChainDesc.SampleDesc.Quality = 0;
    objSwapChainDesc.Windowed = true;


    //Parse and create the appropriate type of device
    for ( UINT driverTypeIndex = 0; driverTypeIndex < numDriverType; driverTypeIndex++ )
    {
        this->m_objDriverType = pDriverType[driverTypeIndex];
        status = D3D11CreateDeviceAndSwapChain ( NULL, 
                                                 this->m_objDriverType, 
                                                 NULL, 
                                                 dCreateDeviceFlag, 
                                                 pFeatureLevel, 
                                                 numFeatureLevel, 
                                                 D3D11_SDK_VERSION,
                                                 &objSwapChainDesc, 
                                                 &(this->m_dxSwapChain), 
                                                 &(this->m_dxDevice), 
                                                 &(this->m_objFeatureLevel), 
                                                 &(this->m_dxDeviceContext) );
        if ( SUCCEEDED(status) )
            break;
    }
    if ( FAILED(status) )
        return status;


    //Create a render traget
    this->m_dxBackBuffer = NULL;
    status = this->m_dxSwapChain->GetBuffer ( 0, __uuidof(ID3D11Texture2D), (LPVOID*)(&this->m_dxBackBuffer) );
    if ( FAILED(status) )
        return status;

    status = this->m_dxDevice->CreateRenderTargetView ( this->m_dxBackBuffer, NULL, &(this->m_dxRenderTargetView) );
    if ( FAILED(status) )
        return status;


    //CreatDepth Stencil
    D3D11_TEXTURE2D_DESC objDepthStencilDesc;
    ZeroMemory(&objDepthStencilDesc, sizeof(objDepthStencilDesc));

    D3D11_DEPTH_STENCIL_VIEW_DESC objDepthStencilViewDesc;
    ZeroMemory(&objDepthStencilViewDesc, sizeof(objDepthStencilViewDesc));

    objDepthStencilDesc.Width = dWidth;
    objDepthStencilDesc.Height = dHeight;
    objDepthStencilDesc.MipLevels = 1;
    objDepthStencilDesc.ArraySize = 1;
    objDepthStencilDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
    objDepthStencilDesc.SampleDesc.Count = 1;
    objDepthStencilDesc.SampleDesc.Quality = 0;
    objDepthStencilDesc.Usage = D3D11_USAGE_DEFAULT;
    objDepthStencilDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
    objDepthStencilDesc.MiscFlags = 0;
    objDepthStencilDesc.CPUAccessFlags = 0;

    status = this->m_dxDevice->CreateTexture2D ( 
                                                &objDepthStencilDesc, 
                                                NULL,
                                                &(this->m_dxDepthStencilBuffer)
                                                );
    if ( FAILED(status) )
        return status;

    objDepthStencilViewDesc.Format = objDepthStencilDesc.Format;
    objDepthStencilViewDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
    objDepthStencilViewDesc.Texture2D.MipSlice = 0;

    status = this->m_dxDevice->CreateDepthStencilView ( 
                                                       this->m_dxDepthStencilBuffer,
                                                       &objDepthStencilViewDesc,
                                                       &(this->m_dxDepthStencilView)
                                                      );
    if ( FAILED(status) )
        return status;

    this->m_dxDeviceContext->OMSetRenderTargets ( 1, &(this->m_dxRenderTargetView), this->m_dxDepthStencilView );


    //Set View Port (Can be commented out;
    this->m_objViewport.Width       = (FLOAT)dWidth;
    this->m_objViewport.Height      = (FLOAT)dHeight;
    this->m_objViewport.MinDepth    = 0.0;
    this->m_objViewport.MaxDepth    = 1.0;
    this->m_objViewport.TopLeftX    = 0;
    this->m_objViewport.TopLeftY    = 0;

    this->m_dxDeviceContext->RSSetViewports ( 1, &(this->m_objViewport) );


    //Setting up Shaders
    //Compiling Vertex Shader file
    ID3DBlob * dxVSBlob = NULL;
    status = CompileShaderFromFile ( L"RTShaders.fx", "VS", "vs_4_0", &dxVSBlob );
    if (FAILED (status))
    {
        dxVSBlob->Release ( );
        return status;
    }

    //Creating Compiled Shader
    status = this->m_dxDevice->CreateVertexShader ( dxVSBlob->GetBufferPointer ( ), 
                                                    dxVSBlob->GetBufferSize ( ),
                                                    NULL,
                                                    &(this->m_dxVertexShader) );
    if( FAILED(status) )
    {
        dxVSBlob->Release ( );
        return status;
    }

    //Describe Layout
    D3D11_INPUT_ELEMENT_DESC layout [] =
    {
        {
            "POSITION",
            0,
            DXGI_FORMAT_R32G32B32_FLOAT,
            0,
            0,
            D3D11_INPUT_PER_VERTEX_DATA,
            0
        },
/*      {
            "NORMAL",
            0,
            DXGI_FORMAT_R32G32B32A32_FLOAT,
            0,
            16,
            D3D11_INPUT_PER_VERTEX_DATA,
            0
        },
*/      {
            "COLOR",
            0,
            DXGI_FORMAT_R32G32B32_FLOAT,
            0,
            12,                             //NEED TO CHANGE THIS TO 32 WHEN UNCOMMENTING THE ABOVE BLOCK
            D3D11_INPUT_PER_VERTEX_DATA,
            0
        },
    };
    UINT numElements = ARRAYSIZE(layout);

    //Create Layout
    status = this->m_dxDevice->CreateInputLayout ( layout, 
                                                   numElements, 
                                                   dxVSBlob->GetBufferPointer ( ), 
                                                   dxVSBlob->GetBufferSize ( ), 
                                                   &(this->m_dxVertexLayout) );
    dxVSBlob->Release ( );
    dxVSBlob = NULL;
    if ( FAILED(status) )
    {
        return status;
    }

    //Set Vertex Layout to the pipeline
    this->m_dxDeviceContext->IASetInputLayout ( this->m_dxVertexLayout );

    //Compiling Pixel Shader File
    ID3DBlob * dxPSBlob = NULL;
    status = CompileShaderFromFile ( L"RTShaders.fx", "PS", "ps_4_0", &dxPSBlob );
    if ( FAILED(status) )
    {
        if ( dxPSBlob )
            dxPSBlob->Release ( );
        return status;
    }

    //Creating Compiled Pixel Shader
    status = this->m_dxDevice->CreatePixelShader ( 
                                                 dxPSBlob->GetBufferPointer ( ),
                                                 dxPSBlob->GetBufferSize ( ),
                                                 NULL,
                                                 &(this->m_dxPixelShader)
                                                 );
    dxPSBlob->Release ( );
    dxPSBlob = NULL;
    if ( FAILED(status) )
    {
        return status;
    }

    return S_OK;
}
HRESULT CRenderer::CreateVertexBuffer ( )
{
    HRESULT status = S_OK;

    D3D11_BUFFER_DESC objBufferDesc;
    D3D11_SUBRESOURCE_DATA objSubresData;
    UINT dStride = sizeof (SVertex);
    UINT dOffset = 0;

    ZeroMemory(&objBufferDesc, sizeof(objBufferDesc));
    ZeroMemory(&objSubresData, sizeof(objSubresData));

    objBufferDesc.ByteWidth = sizeof(SVertex) * 1681;
    objBufferDesc.Usage = D3D11_USAGE_DEFAULT;
    objBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
    objBufferDesc.CPUAccessFlags = 0;

    objSubresData.pSysMem = pVertexData;

    status = this->m_dxDevice->CreateBuffer (
                                            &objBufferDesc,
                                            &objSubresData,
                                            &(this->m_dxVertexBuffer)
                                            );
    if ( FAILED(status) )
        return status;

    this->m_dxDeviceContext->IASetVertexBuffers ( 0, 1, &(this->m_dxVertexBuffer), &dStride, &dOffset );

    this->m_dxDeviceContext->IASetPrimitiveTopology ( D3D11_PRIMITIVE_TOPOLOGY_POINTLIST );

    return S_OK;
}

HRESULT CRenderer::CreateConstantBuffer ( )
{
    HRESULT status = S_OK;

    D3D11_BUFFER_DESC objBufferDesc;

    ZeroMemory(&objBufferDesc, sizeof(objBufferDesc));

    objBufferDesc.ByteWidth = sizeof(SConstantBuffer) + (16 - sizeof(SConstantBuffer)%16);
    objBufferDesc.Usage = D3D11_USAGE_DEFAULT;
    objBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
    objBufferDesc.CPUAccessFlags = 0;

    status = this->m_dxDevice->CreateBuffer (
                                            &objBufferDesc,
                                            NULL,
                                            &(this->m_dxConstantBuffer)
                                            );

    return status;
}

HRESULT CRenderer::Render ( )
{
    HRESULT status = S_OK;
    RECT screenRect;
    SConstantBuffer objConstBuffer;

    float pClearColor [4] = { 0.0f, 0.125f, 0.5f, 1.0f };

    this->m_dxDeviceContext->ClearRenderTargetView ( this->m_dxRenderTargetView, pClearColor );
    this->m_dxDeviceContext->ClearDepthStencilView ( this->m_dxDepthStencilView, D3D11_CLEAR_DEPTH, 1.0f, 0 );

    GetClientRect ( this->m_hWnd, &screenRect );
    objConstBuffer.f4Origin = XMFLOAT3 ( 0.0f, 0.0f, 0.0f );
    objConstBuffer.f4Direction = XMFLOAT3 ( 0.0f, 0.0f, 1.0f );
    objConstBuffer.fDistanceFromPlane = 2.0f;
    objConstBuffer.dScreenWidth = screenRect.right - screenRect.left;
    objConstBuffer.dScreenHeight = screenRect.bottom - screenRect.top;
    this->m_dxDeviceContext->UpdateSubresource ( this->m_dxConstantBuffer, 0, NULL, &objConstBuffer, 0, 0 );

    this->m_dxDeviceContext->VSSetShader ( this->m_dxVertexShader, NULL, 0 );
    this->m_dxDeviceContext->VSSetConstantBuffers ( 0, 1, &(this->m_dxConstantBuffer) );
    this->m_dxDeviceContext->PSSetShader ( this->m_dxPixelShader, NULL, 0 );
    this->m_dxDeviceContext->Draw ( 1680, 0 );

    this->m_dxSwapChain->Present ( 0, 0 );

    return status;
}

CRenderer::~CRenderer ( )
{
    if ( this->m_dxDeviceContext ) 
        this->m_dxDeviceContext->ClearState ( );

    if ( this->m_dxPixelShader )
        this->m_dxPixelShader->Release ( );
    this->m_dxPixelShader = NULL;

    if ( this->m_dxVertexLayout )
        this->m_dxVertexLayout->Release ( );
    this->m_dxVertexLayout = NULL;

    if ( this->m_dxVertexBuffer )
        this->m_dxVertexBuffer->Release ( );
    this->m_dxVertexBuffer = NULL;

    if ( this->m_dxDepthStencilBuffer )
        this->m_dxDepthStencilBuffer->Release ( );
    this->m_dxDepthStencilBuffer = NULL;

    if ( this->m_dxDepthStencilView )
        this->m_dxDepthStencilView->Release ( );
    this->m_dxDepthStencilView = NULL;

    if ( this->m_dxRenderTargetView )
        this->m_dxRenderTargetView->Release ( );
    this->m_dxRenderTargetView = NULL;

    if ( this->m_dxBackBuffer->Release ( ) )
        this->m_dxBackBuffer->Release ( );
    this->m_dxBackBuffer = NULL;

    if ( this->m_dxSwapChain )
        this->m_dxSwapChain->Release ( );
    this->m_dxSwapChain = NULL;

    if ( this->m_dxDeviceContext )
        this->m_dxDeviceContext->Release ( );
    this->m_dxDeviceContext = NULL;

    if ( this->m_dxDevice )
        this->m_dxDevice->Release ( );
    this->m_dxDevice = NULL;
}

Nothing Gets rendered on the screen, I should see while dots or a big white patch but all I see is the Render Target only Cleared. I ve been running Pix and the PreVS and PostVS transformations seem fine. Please Help!

Joe
  • 41,484
  • 20
  • 104
  • 125
Safi
  • 21
  • 7
  • Your code seems to be fine at a first sight. Could you post a screenshot of PIX' mesh window? You can also right click a pixel in the Render window and choose Debug Pixel to step through the shader stages. You'll also see, if a pixel has been removed for some reason. – Nico Schertler Sep 16 '12 at 10:01
  • One initial guess would be a problem with your constant buffer, can you please post the code for m_dxConstantBuffer (and header file)? Data in there is 16 bytes aligned, so you're quite likely to have padding issues. – mrvux Jul 20 '13 at 21:27
  • 1
    Maybe I'm dense but I can't see where you put anything in your vertex buffer? – Slagh Aug 08 '13 at 00:44
  • Are you calling all the necessary functions... i.e. CreateConstantBuffer(), CreateVertexBuffer(), etc.. – Miguel Dec 30 '13 at 21:32

0 Answers0