void APIENTRY RosUmdDeviceDdi::DdiStagingResourceMap(
    D3D10DDI_HDEVICE hDevice,
    D3D10DDI_HRESOURCE hResource,
    UINT subResource,
    D3D10_DDI_MAP mapType,
    UINT mapFlags,
    D3D10DDI_MAPPED_SUBRESOURCE* pMappedSubRes)
{
    RosUmdLogging::Entry(__FUNCTION__);

    RosUmdDevice* pRosUmdDevice = RosUmdDevice::CastFrom(hDevice);
    RosUmdResource * pResource = (RosUmdResource *)hResource.pDrvPrivate;

    try
    {
        pRosUmdDevice->StagingResourceMap(pResource, subResource, mapType, mapFlags, pMappedSubRes);
    }

    catch (std::exception & e)
    {
        pRosUmdDevice->SetException(e);
    }

    RosUmdLogging::Exit(__FUNCTION__);
}
void APIENTRY RosUmdDeviceDdi::DdiPsSetShader(D3D10DDI_HDEVICE hDevice, D3D10DDI_HSHADER hShader) { 
    RosUmdLogging::Call(__FUNCTION__); 

    RosUmdDevice * pDevice = RosUmdDevice::CastFrom(hDevice);
    RosUmdShader * pShader = RosUmdShader::CastFrom(hShader);
    pDevice->SetPixelShader(pShader);
}
void APIENTRY RosUmdDeviceDdi::DdiIaSetTopology(
    D3D10DDI_HDEVICE hDevice, 
    D3D10_DDI_PRIMITIVE_TOPOLOGY topology)
{
    RosUmdDevice * pDevice = RosUmdDevice::CastFrom(hDevice);
    pDevice->SetTopology(topology);
}
void APIENTRY RosUmdDeviceDdi::DdiDraw(
    D3D10DDI_HDEVICE hDevice, 
    UINT vertexCount,
    UINT startVertexLocation)
{
    RosUmdDevice * pDevice = RosUmdDevice::CastFrom(hDevice);
    pDevice->Draw(vertexCount, startVertexLocation);
}
void APIENTRY RosUmdDeviceDdi::DdiCheckCounterInfo(
    D3D10DDI_HDEVICE hDevice,
    D3D10DDI_COUNTER_INFO* pCounterInfo)
{
    RosUmdLogging::Call(__FUNCTION__);

    RosUmdDevice* pRosUmdDevice = RosUmdDevice::CastFrom(hDevice);
    pRosUmdDevice->CheckCounterInfo(pCounterInfo);
}
void APIENTRY RosUmdDeviceDdi::DdiSetViewports(
    D3D10DDI_HDEVICE hDevice,
    UINT numViewports, 
    UINT clearViewports, 
    const D3D10_DDI_VIEWPORT* pViewports)
{
    RosUmdDevice * pDevice = RosUmdDevice::CastFrom(hDevice);
    pDevice->SetViewports(numViewports, clearViewports, pViewports);
}
void APIENTRY RosUmdDeviceDdi::DdiClearRenderTargetView(
    D3D10DDI_HDEVICE hDevice,
    D3D10DDI_HRENDERTARGETVIEW hRenderTargetView,
    FLOAT clearColor[4])
{
    RosUmdDevice * pDevice = RosUmdDevice::CastFrom(hDevice);
    RosUmdRenderTargetView * pRenderTargetView = RosUmdRenderTargetView::CastFrom(hRenderTargetView);
    pDevice->ClearRenderTargetView(pRenderTargetView, clearColor);
}
void APIENTRY RosUmdDeviceDdi::DdiDrawIndexed(
    D3D10DDI_HDEVICE hDevice,
    UINT indexCount,
    UINT startIndexLocation,
    INT baseVertexLocation)
{
    RosUmdDevice * pDevice = RosUmdDevice::CastFrom(hDevice);
    pDevice->DrawIndexed(indexCount, startIndexLocation, baseVertexLocation);
}
void APIENTRY RosUmdDeviceDdi::DdiIaSetInputLayout(
    D3D10DDI_HDEVICE hDevice,
    D3D10DDI_HELEMENTLAYOUT hElementLayout)
{
    RosUmdLogging::Call(__FUNCTION__);

    RosUmdDevice * pDevice = RosUmdDevice::CastFrom(hDevice);
    RosUmdElementLayout * pElementLayout = RosUmdElementLayout::CastFrom(hElementLayout);
    pDevice->SetElementLayout(pElementLayout);
}
void APIENTRY RosUmdDeviceDdi::DdiDestroyShader(
    D3D10DDI_HDEVICE hDevice,
    D3D10DDI_HSHADER hShader) 
{ 
    RosUmdLogging::Call(__FUNCTION__);

    RosUmdDevice * pDevice = RosUmdDevice::CastFrom(hDevice);

    pDevice->DestroyShader(hShader);
}
void APIENTRY RosUmdDeviceDdi::DdiSetRasterizerState(
    D3D10DDI_HDEVICE hDevice, 
    D3D10DDI_HRASTERIZERSTATE hRasterizerState)
{
    RosUmdLogging::Call(__FUNCTION__); 

    RosUmdDevice * pDevice = RosUmdDevice::CastFrom(hDevice);
    RosUmdRasterizerState * pRasterizerState = RosUmdRasterizerState::CastFrom(hRasterizerState);
    pDevice->SetRasterizerState(pRasterizerState);
}
void APIENTRY RosUmdDeviceDdi::DdiSetDepthStencilState(
    D3D10DDI_HDEVICE hDevice,
    D3D10DDI_HDEPTHSTENCILSTATE hDepthStencilState,
    UINT StencilRef) 
{ 
    RosUmdLogging::Call(__FUNCTION__); 

    RosUmdDevice * pDevice = RosUmdDevice::CastFrom(hDevice);
    RosUmdDepthStencilState * pDepthStencilState = RosUmdDepthStencilState::CastFrom(hDepthStencilState);
    pDevice->SetDepthStencilState(pDepthStencilState, StencilRef);
}
void APIENTRY RosUmdDeviceDdi::DdiDSSetSamplers(
    D3D10DDI_HDEVICE hDevice,
    UINT Offset,
    UINT NumSamplers,
    const D3D10DDI_HSAMPLER* phSamplers)
{
    RosUmdLogging::Call(__FUNCTION__);

    RosUmdDevice * pDevice = RosUmdDevice::CastFrom(hDevice);
    pDevice->SetDomainSamplers(Offset, NumSamplers, phSamplers);
}
void APIENTRY RosUmdDeviceDdi::DdiIaSetIndexBuffer(
    D3D10DDI_HDEVICE hDevice,
    D3D10DDI_HRESOURCE hIndexBuffer,
    DXGI_FORMAT hIndexFormat,
    UINT offset)
{
    RosUmdLogging::Call(__FUNCTION__);

    RosUmdDevice * pDevice = RosUmdDevice::CastFrom(hDevice);
    pDevice->SetIndexBuffer(hIndexBuffer, hIndexFormat, offset);
}
void APIENTRY RosUmdDeviceDdi::DdiCheckFormatSupport(
    D3D10DDI_HDEVICE hDevice,
    DXGI_FORMAT Format,
    UINT* pFormatSupport)
{
    RosUmdLogging::Call(__FUNCTION__);

    RosUmdDevice* pRosUmdDevice = RosUmdDevice::CastFrom(hDevice);

    pRosUmdDevice->CheckFormatSupport(Format, pFormatSupport);
}
void APIENTRY RosUmdDeviceDdi::DdiCheckMultisampleQualityLevels(
    D3D10DDI_HDEVICE hDevice,
    DXGI_FORMAT Format,
    UINT SampleCount,
    UINT* pNumQualityLevels)
{
    RosUmdLogging::Call(__FUNCTION__);

    RosUmdDevice* pRosUmdDevice = RosUmdDevice::CastFrom(hDevice);
    pRosUmdDevice->CheckMultisampleQualityLevels(Format, SampleCount, pNumQualityLevels);
}
void APIENTRY RosUmdDeviceDdi::DdiSetBlendState(
    D3D10DDI_HDEVICE hDevice, 
    D3D10DDI_HBLENDSTATE hBlendState, 
    const FLOAT pBlendFactor[4], 
    UINT sampleMask)
{ 
    RosUmdLogging::Call(__FUNCTION__); 

    RosUmdDevice * pDevice = RosUmdDevice::CastFrom(hDevice);
    RosUmdBlendState * pBlendState = RosUmdBlendState::CastFrom(hBlendState);
    pDevice->SetBlendState(pBlendState, pBlendFactor, sampleMask);
}
void APIENTRY RosUmdDeviceDdi::DdiIaSetVertexBuffers(
    D3D10DDI_HDEVICE hDevice,
    UINT startBuffer,
    UINT numBuffers,
    const D3D10DDI_HRESOURCE* phBuffers,
    const UINT* pStrides, 
    const UINT* pOffsets)
{
    RosUmdLogging::Call(__FUNCTION__); 

    RosUmdDevice * pDevice = RosUmdDevice::CastFrom(hDevice);
    pDevice->SetVertexBuffers(startBuffer, numBuffers, phBuffers, pStrides, pOffsets);
}
void APIENTRY RosUmdDeviceDdi::DdiClearDepthStencilView(
    D3D10DDI_HDEVICE hDevice,
    D3D10DDI_HDEPTHSTENCILVIEW hDepthStencilView,
    UINT clearFlags,
    FLOAT depthValue,
    UINT8 stencilValue)
{
    RosUmdLogging::Call(__FUNCTION__);
    
    RosUmdDevice * pDevice = RosUmdDevice::CastFrom(hDevice);
    RosUmdDepthStencilView * pDepthStencilView = RosUmdDepthStencilView::CastFrom(hDepthStencilView);

    pDevice->ClearDepthStencilView(pDepthStencilView, clearFlags, depthValue, stencilValue);
}
void APIENTRY RosUmdDeviceDdi::DdiDestroyResource(
    D3D10DDI_HDEVICE hDevice,
    D3D10DDI_HRESOURCE hResource)
{
    RosUmdLogging::Entry(__FUNCTION__);

    RosUmdDevice* pRosUmdDevice = RosUmdDevice::CastFrom(hDevice);

    RosUmdResource * pResource = (RosUmdResource *)hResource.pDrvPrivate;

    pRosUmdDevice->DestroyResource(pResource);

    RosUmdLogging::Exit(__FUNCTION__);
}
void APIENTRY RosUmdDeviceDdi::DdiSetRenderTargets(
    D3D10DDI_HDEVICE hDevice,
    const D3D10DDI_HRENDERTARGETVIEW* phRenderTargetView,
    UINT NumRTVs,
    UINT RTVNumbertoUnbind,
    D3D10DDI_HDEPTHSTENCILVIEW hDepthStencilView,
    const D3D11DDI_HUNORDEREDACCESSVIEW* phUnorderedAccessView,
    const UINT* pUAVInitialCounts,
    UINT UAVIndex,
    UINT NumUAVs,
    UINT UAVFirsttoSet,
    UINT UAVNumberUpdated)
{
    RosUmdDevice * pDevice = RosUmdDevice::CastFrom(hDevice);

    pDevice->SetRenderTargets(phRenderTargetView, NumRTVs, RTVNumbertoUnbind, hDepthStencilView, phUnorderedAccessView, pUAVInitialCounts, UAVIndex,
        NumUAVs, UAVFirsttoSet, UAVNumberUpdated);
}
void APIENTRY RosUmdDeviceDdi::DdiDestroyDevice(
    D3D10DDI_HDEVICE hDevice)
{
    RosUmdLogging::Entry(__FUNCTION__);

    RosUmdDevice* pRosUmdDevice = RosUmdDevice::CastFrom(hDevice);

    try {
        pRosUmdDevice->Teardown();
        pRosUmdDevice->~RosUmdDevice();
    }

    catch (std::exception &)
    {
        // do nothing
    }

    RosUmdLogging::Exit(__FUNCTION__);
}
//----------------------------------------------------------------------------------------------------------------------------------
HRESULT APIENTRY RosUmdAdapter::CreateDevice(
    D3D10DDI_HADAPTER hAdapter,
    D3D10DDIARG_CREATEDEVICE* pArgs )
{
    RosUmdAdapter* pThis = RosUmdAdapter::CastFrom( hAdapter );

    RosUmdDevice * pRosUmdDevice = new (pArgs->hDrvDevice.pDrvPrivate) RosUmdDevice(pThis, pArgs);

    try
    {
        pRosUmdDevice->Standup();
    }

    catch( RosUmdException & e )
    {
        pRosUmdDevice->~RosUmdDevice();
        return e.m_hr;
    }

    return S_OK;
}
void APIENTRY RosUmdDeviceDdi::DdiCreateResource(
    D3D10DDI_HDEVICE hDevice,
    const D3D11DDIARG_CREATERESOURCE* pCreateResource,
    D3D10DDI_HRESOURCE hResource,
    D3D10DDI_HRTRESOURCE hRTResource)
{
    RosUmdLogging::Entry(__FUNCTION__);

    RosUmdDevice* pRosUmdDevice = RosUmdDevice::CastFrom(hDevice);

    try {
        pRosUmdDevice->CreateResource(pCreateResource, hResource, hRTResource);
    }

    catch (std::exception & e)
    {
        pRosUmdDevice->SetException(e);
    }

    RosUmdLogging::Exit(__FUNCTION__);
}
void APIENTRY RosUmdDeviceDdi::DdiCreateComputeShader(
    D3D10DDI_HDEVICE hDevice,
    const UINT* pCode,
    D3D10DDI_HSHADER hShader,
    D3D10DDI_HRTSHADER hRTShader)
{
    RosUmdLogging::Entry(__FUNCTION__);

    RosUmdDevice * pRosUmdDevice = RosUmdDevice::CastFrom(hDevice);

    try
    {
        pRosUmdDevice->CreateComputeShader(pCode, hShader, hRTShader);
    }

    catch (std::exception & e)
    {
        pRosUmdDevice->SetException(e);
    }

    RosUmdLogging::Exit(__FUNCTION__);
}
void APIENTRY RosUmdDeviceDdi::DdiStagingResourceUnmap(
    D3D10DDI_HDEVICE hDevice,
    D3D10DDI_HRESOURCE hResource,
    UINT subResource)
{
    RosUmdLogging::Entry(__FUNCTION__);

    RosUmdDevice* pRosUmdDevice = RosUmdDevice::CastFrom(hDevice);
    RosUmdResource * pResource = (RosUmdResource *)hResource.pDrvPrivate;

    try
    {
        pRosUmdDevice->StagingResourceUnmap(pResource, subResource);
    }

    catch (std::exception & e)
    {
        pRosUmdDevice->SetException(e);
    }

    RosUmdLogging::Exit(__FUNCTION__);
}
void APIENTRY RosUmdDeviceDdi::DdiResourceCopy(
    D3D10DDI_HDEVICE hDevice,
    D3D10DDI_HRESOURCE hDestinationResource,
    D3D10DDI_HRESOURCE hSourceResource)
{
    RosUmdLogging::Entry(__FUNCTION__);

    RosUmdDevice* pRosUmdDevice = RosUmdDevice::CastFrom(hDevice);
    RosUmdResource * pDestinationResource = (RosUmdResource *)hDestinationResource.pDrvPrivate;
    RosUmdResource * pSourceResource = (RosUmdResource *)hSourceResource.pDrvPrivate;

    try
    {
        pRosUmdDevice->ResourceCopy(pDestinationResource, pSourceResource);
    }

    catch (std::exception & e)
    {
        pRosUmdDevice->SetException(e);
    }

    RosUmdLogging::Exit(__FUNCTION__);
}
void APIENTRY RosUmdDeviceDdi::DdiCreateGeometryShader(
    D3D10DDI_HDEVICE hDevice,
    const UINT* pCode,
    D3D10DDI_HSHADER hShader,
    D3D10DDI_HRTSHADER hRTShader,
    const D3D11_1DDIARG_STAGE_IO_SIGNATURES* pSignatures)
{
    RosUmdLogging::Entry(__FUNCTION__);

    RosUmdDevice * pRosUmdDevice = RosUmdDevice::CastFrom(hDevice);

    try
    {
        pRosUmdDevice->CreateGeometryShader(pCode, hShader, hRTShader, pSignatures);
    }

    catch (std::exception & e)
    {
        pRosUmdDevice->SetException(e);
    }

    RosUmdLogging::Exit(__FUNCTION__);
}
void APIENTRY RosUmdDeviceDdi::DdiCreateDomainShader(
    D3D10DDI_HDEVICE hDevice,
    const UINT* pCode,
    D3D10DDI_HSHADER hShader,
    D3D10DDI_HRTSHADER hRTShader,
    const D3D11_1DDIARG_TESSELLATION_IO_SIGNATURES* pSignatures)
{ 
    RosUmdLogging::Entry(__FUNCTION__);

    RosUmdDevice * pRosUmdDevice = RosUmdDevice::CastFrom(hDevice);

    try
    {
        pRosUmdDevice->CreateTessellationShader(pCode, hShader, hRTShader, pSignatures, D3D11_SB_DOMAIN_SHADER);
    }

    catch (std::exception & e)
    {
        pRosUmdDevice->SetException(e);
    }

    RosUmdLogging::Exit(__FUNCTION__);
}
BOOL APIENTRY RosUmdDeviceDdi::DdiFlush(D3D10DDI_HDEVICE hDevice, UINT flushFlags)
{
    RosUmdLogging::Entry(__FUNCTION__);

    RosUmdDevice* pRosUmdDevice = RosUmdDevice::CastFrom(hDevice);

    BOOL bSuccess = TRUE;

    try
    {
        pRosUmdDevice->m_commandBuffer.Flush(flushFlags);
    }

    catch (std::exception & e)
    {
        pRosUmdDevice->SetException(e);
        bSuccess = FALSE;
    }

    RosUmdLogging::Exit(__FUNCTION__);

    return bSuccess;
}