コード例 #1
0
GDK_IMETHODIMP DirectionalLightShader::Initialize(_In_ Renderer* pRenderer)
{
    HRESULT hr = S_OK;

    DevicePtr spDevice = pRenderer->GetDevice();
    stde::com_ptr<IStream> spStream;

    uint64 length = 0;

    // TODO: Fill in real material table. For now let's just
    // assume material 0xffffffff is the directional light, and that we support it well
    AddSupportedMaterial(0xffffffff, 100);

    CHECKHR(FileStream::Create(L"DirectionalLight.hlsl", true, &spStream));
    length = FileStream::GetLength(spStream);
    ISTRUE(length > 0, E_FAIL);

    {
        std::vector<byte> buffer(length);
        ULONG cbRead = 0;
        stde::com_ptr<ID3DBlob> spCode, spErrors;

        D3D11_BUFFER_DESC bufferDesc = {0};
        D3D11_INPUT_ELEMENT_DESC inputElem = {0};
        D3D11_BLEND_DESC blendDesc = {0};

        std::vector<D3D11_INPUT_ELEMENT_DESC> inputElems;

        DWORD dwShaderFlags = D3DCOMPILE_ENABLE_STRICTNESS;

#ifdef _DEBUG
        dwShaderFlags |= D3DCOMPILE_DEBUG;
#endif

        CHECKHR(spStream->Read(buffer.data(), buffer.size(), &cbRead));
        hr = D3DCompile(buffer.data(), buffer.size(), nullptr, nullptr, nullptr, "vsFullscreenQuad", "vs_4_0", dwShaderFlags, 0, &spCode, &spErrors);
        if (FAILED(hr))
        {
            DebugOut("Error compiling shader:\n\n%s\n", static_cast<PSTR>(spErrors->GetBufferPointer()));
            CHECKHR(hr);
        }

        CHECKHR(spDevice->CreateVertexShader(spCode->GetBufferPointer(), spCode->GetBufferSize(), nullptr, &_spQuadVertexShader));
        // Store off the shader code for input layout construction later
        SetShaderCode(spCode);

        spCode = nullptr;
        spErrors = nullptr;
        hr = D3DCompile(buffer.data(), buffer.size(), nullptr, nullptr, nullptr, "psDirectionalLight", "ps_4_0", dwShaderFlags, 0, &spCode, &spErrors);
        if (FAILED(hr))
        {
            DebugOut("Error compiling shader:\n\n%s\n", static_cast<PSTR>(spErrors->GetBufferPointer()));
            CHECKHR(hr);
        }

        CHECKHR(spDevice->CreatePixelShader(spCode->GetBufferPointer(), spCode->GetBufferSize(), nullptr, &_spPixelShader));

        // build constant buffers

        bufferDesc.Usage = D3D11_USAGE_DEFAULT;
        bufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
        bufferDesc.ByteWidth = sizeof(_frameConstants);
        bufferDesc.StructureByteStride = sizeof(_frameConstants);
        CHECKHR(spDevice->CreateBuffer(&bufferDesc, nullptr, &_spFrameConstantBuffer));
        {
            std::string name("DirectionalLightShader::FrameConstantBuffer");
            _spFrameConstantBuffer->SetPrivateData(WKPDID_D3DDebugObjectName, name.size(), name.c_str());
        }

        // Fill in the screen size (only needed once for now, unless we support dynamic resize)
        _frameConstants.ScreenSize.Set(
            pRenderer->GetSettings().DefaultView.ScreenWidth, 
            pRenderer->GetSettings().DefaultView.ScreenHeight);

        bufferDesc.Usage = D3D11_USAGE_DEFAULT;
        bufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
        bufferDesc.ByteWidth = sizeof(_lightConstants);
        bufferDesc.StructureByteStride = sizeof(_lightConstants);
        CHECKHR(spDevice->CreateBuffer(&bufferDesc, nullptr, &_spLightConstantBuffer));
        {
            std::string name("DirectionalLightShader::LightConstantBuffer");
            _spFrameConstantBuffer->SetPrivateData(WKPDID_D3DDebugObjectName, name.size(), name.c_str());
        }

        blendDesc.RenderTarget[0].BlendEnable = TRUE;
        blendDesc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD;
        blendDesc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD;
        blendDesc.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA;
        blendDesc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_SRC_ALPHA;
        blendDesc.RenderTarget[0].DestBlend = D3D11_BLEND_DEST_ALPHA;
        blendDesc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_DEST_ALPHA;
        blendDesc.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;
        CHECKHR(spDevice->CreateBlendState(&blendDesc, &_spBlendState));
    }

EXIT
    return hr;
}
コード例 #2
0
GDK_METHODIMP SimpleShader::Initialize(_In_ Renderer* pRenderer)
{
    HRESULT hr = S_OK;

    DevicePtr spDevice = pRenderer->GetDevice();
    stde::com_ptr<IStream> spStream;

    uint64 length = 0;

    // TODO: Fill in real material table. For now let's just
    // assume material 0 is the only one, and that we support it well
    AddSupportedMaterial(0, 100);

    CHECKHR(FileStream::Create(L"SimpleShader.hlsl", true, &spStream));
    length = FileStream::GetLength(spStream);
    ISTRUE(length > 0, E_FAIL);

    {
        std::vector<byte> buffer(length);
        ULONG cbRead = 0;
        stde::com_ptr<ID3DBlob> spCode, spErrors;

        D3D11_BUFFER_DESC bufferDesc = {0};
        D3D11_INPUT_ELEMENT_DESC inputElem = {0};
        D3D11_SAMPLER_DESC samplerDesc;

        ZeroMemory(&samplerDesc, sizeof(samplerDesc));

        std::vector<D3D11_INPUT_ELEMENT_DESC> inputElems;

        DWORD dwShaderFlags = D3DCOMPILE_ENABLE_STRICTNESS;
#ifdef _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

        CHECKHR(spStream->Read(buffer.data(), buffer.size(), &cbRead));
        hr = D3DCompile(buffer.data(), buffer.size(), nullptr, nullptr, nullptr, "vsSimpleShader", "vs_4_0", dwShaderFlags, 0, &spCode, &spErrors);
        if (FAILED(hr))
        {
            DebugOut("Error compiling shader:\n\n%s\n", static_cast<PSTR>(spErrors->GetBufferPointer()));
            CHECKHR(hr);
        }

        CHECKHR(spDevice->CreateVertexShader(spCode->GetBufferPointer(), spCode->GetBufferSize(), nullptr, &_spVertexShader));
        // Store off the shader code for input layout construction later
        SetShaderCode(spCode);

        spCode = nullptr;
        spErrors = nullptr;
        hr = D3DCompile(buffer.data(), buffer.size(), nullptr, nullptr, nullptr, "psSimpleShader", "ps_4_0", dwShaderFlags, 0, &spCode, &spErrors);
        if (FAILED(hr))
        {
            DebugOut("Error compiling shader:\n\n%s\n", static_cast<PSTR>(spErrors->GetBufferPointer()));
            CHECKHR(hr);
        }

        CHECKHR(spDevice->CreatePixelShader(spCode->GetBufferPointer(), spCode->GetBufferSize(), nullptr, &_spPixelShader));

        // build constant buffers

        bufferDesc.Usage = D3D11_USAGE_DEFAULT;
        bufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
        bufferDesc.ByteWidth = sizeof(_frameConstants);
        bufferDesc.StructureByteStride = sizeof(_frameConstants);
        CHECKHR(spDevice->CreateBuffer(&bufferDesc, nullptr, &_spFrameConstantBuffer));
        {
            std::string name("SimpleShader::FrameConstantBuffer");
            _spFrameConstantBuffer->SetPrivateData(WKPDID_D3DDebugObjectName, name.size(), name.c_str());
        }

        bufferDesc.ByteWidth = sizeof(_objectConstants);
        bufferDesc.StructureByteStride = sizeof(_objectConstants);
        CHECKHR(spDevice->CreateBuffer(&bufferDesc, nullptr, &_spObjectConstantBuffer));
        {
            std::string name("SimpleShader::ObjectConstantBuffer");
            _spObjectConstantBuffer->SetPrivateData(WKPDID_D3DDebugObjectName, name.size(), name.c_str());
        }

        samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
        samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
        samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
        samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
        CHECKHR(spDevice->CreateSamplerState(&samplerDesc, &_spLinearSampler));

        samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_POINT;
        CHECKHR(spDevice->CreateSamplerState(&samplerDesc, &_spPointSampler));
    }

EXIT
    return hr;
}