Пример #1
0
void SkyRenderer::Draw(RenderContext& renderContext,
                       const SceneConstants& sceneConstants)
{
    ID3D11DeviceContext& context = m_graphicsDevice.GetD3DContext();

    //
    //  Update the constant buffer.
    //
    m_constants.viewMatrix = sceneConstants.viewMatrix;
    m_constants.projectionMatrix = sceneConstants.projectionMatrix;

    D3D11_MAPPED_SUBRESOURCE map;
    D3DCHECK(m_graphicsDevice.GetD3DContext().Map(m_constantBuffer.get(),   //  pResource
                                                 0,                         //  Subresource
                                                 D3D11_MAP_WRITE_DISCARD,   //  MapType
                                                 0,                         //  MapFlags
                                                 &map));                    //  pMappedResource
    memcpy(map.pData, &m_constants, sizeof(m_constants));
    m_graphicsDevice.GetD3DContext().Unmap(m_constantBuffer.get(), 0);

    //
    //  Execute the render op.
    //
    ID3D11Buffer* vertexBufferPtr = m_shared->vertexBuffer.get();
    size_t offset = 0,
           stride = sizeof(Vertex);
    ID3D11ShaderResourceView* shaderResourceViewPtrs[] =
    {
        m_shared->skyTextureView.get()
    };
    ID3D11SamplerState* samplerPtr = m_shared->samplerState.get();

    context.IASetVertexBuffers(0, 1, &vertexBufferPtr, &stride, &offset);
    context.IASetIndexBuffer(m_shared->indexBuffer.get(), DXGI_FORMAT_R16_UINT, 0);
    context.IASetInputLayout(m_shared->inputLayout.get());
    context.IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);

    ID3D11Buffer* constantBufferPtr = m_constantBuffer.get();
    context.VSSetConstantBuffers(0, 1, &constantBufferPtr);
    context.VSSetShader(m_shared->vertexShader.get(), NULL, 0);

    context.GSSetShader(NULL, NULL, 0);

    context.PSSetShader(m_shared->pixelShader.get(), NULL, 0);
    context.PSSetShaderResources(0, 1, shaderResourceViewPtrs);
    context.PSSetSamplers(0, 1, &samplerPtr);

    renderContext.PushRasterizerState(m_shared->rasterizerState);
    renderContext.PushDepthStencilState(m_shared->depthStencilState);

    context.DrawIndexed(36, 0, 0);

    renderContext.PopRasterizerState();
    renderContext.PopDepthStencilState();
}
Пример #2
0
void VoxelRenderer::Flush(RenderContext& renderContext,
                          const SceneConstants& sceneConstants)
{
    //  Set up global shader constants
    m_constants.projectionViewMatrix = sceneConstants.viewMatrix *
                                       sceneConstants.projectionMatrix;
    m_constants.clipPlane = sceneConstants.clipPlane;

    //  Calculate camera distances for all queued render ops
    float3 cameraPos = sceneConstants.cameraPos;
    for (size_t i = 0; i < m_renderOps.size(); ++i)
    {
        float3 diff = m_renderOps[i].position - cameraPos;
        m_renderOps[i].distance2 = Dot(diff, diff);
    }
    for (size_t i = 0; i < m_gapFillerRenderOps.size(); ++i)
    {
        float3 diff = m_gapFillerRenderOps[i].position - cameraPos;
        m_gapFillerRenderOps[i].distance2 = Dot(diff, diff);
    }
    for (size_t i = 0; i < m_transparentRenderOps.size(); ++i)
    {
        float3 diff = m_transparentRenderOps[i].position - cameraPos;
        m_transparentRenderOps[i].distance2 = Dot(diff, diff);
    }
    
    //  Sort by camera distance (front to back for opaque render ops,
    //  back to front for transparent)
    auto transparentSorter = [](const RenderOp& lhs, const RenderOp& rhs)
    {
        return lhs.distance2 > rhs.distance2;
    };
    auto opaqueSorter = [](const RenderOp& lhs, const RenderOp& rhs)
    {
        return lhs.distance2 < rhs.distance2;
    };
    std::sort(m_renderOps.begin(), m_renderOps.end(), opaqueSorter);
    std::sort(m_gapFillerRenderOps.begin(), m_gapFillerRenderOps.end(), opaqueSorter);
    std::sort(m_transparentRenderOps.begin(), m_transparentRenderOps.end(), transparentSorter);

    //  Finally time to draw them
    renderContext.PushDepthStencilState(m_shared->depthStencilState);
    for (size_t i = 0; i < m_renderOps.size(); i++)
    {
        Draw(m_renderOps[i]);
    }
    m_renderOps.clear();
    renderContext.PopDepthStencilState();

    renderContext.PushDepthStencilState(m_shared->fillGapsDepthStencilState);
    for (size_t i = 0; i < m_gapFillerRenderOps.size(); ++i)
    {
        Draw(m_gapFillerRenderOps[i]);
    }
    m_gapFillerRenderOps.clear();
    renderContext.PopDepthStencilState();

    renderContext.PushDepthStencilState(m_shared->transparentDepthStencilState);
    renderContext.PushBlendState(m_shared->blendState);
    for (size_t i = 0; i < m_transparentRenderOps.size(); i++)
    {
        Draw(m_transparentRenderOps[i]);
    }
    m_transparentRenderOps.clear();
    renderContext.PopBlendState();
    renderContext.PopDepthStencilState();
}