Path: blob/master/samples/wp8/OcvImageManipulation/PhoneXamlDirect3DApp1/PhoneXamlDirect3DApp1Comp/QuadRenderer.cpp
16350 views
#include "pch.h"1#include "QuadRenderer.h"23using namespace DirectX;4using namespace Microsoft::WRL;5using namespace Windows::Foundation;6using namespace Windows::UI::Core;78QuadRenderer::QuadRenderer() :9m_loadingComplete(false),10m_indexCount(0)11{12}1314void QuadRenderer::CreateTextureFromByte(byte* buffer,int width,int height)15{16int pixelSize = 4;1718if (m_Texture.Get() == nullptr)19{20CD3D11_TEXTURE2D_DESC textureDesc(21DXGI_FORMAT_B8G8R8A8_UNORM, // format22static_cast<UINT>(width), // width23static_cast<UINT>(height), // height241, // arraySize251, // mipLevels26D3D11_BIND_SHADER_RESOURCE, // bindFlags27D3D11_USAGE_DYNAMIC, // usage28D3D11_CPU_ACCESS_WRITE, // cpuaccessFlags291, // sampleCount300, // sampleQuality310 // miscFlags32);3334D3D11_SUBRESOURCE_DATA data;35data.pSysMem = buffer;36data.SysMemPitch = pixelSize*width;37data.SysMemSlicePitch = pixelSize*width*height;3839DX::ThrowIfFailed(40m_d3dDevice->CreateTexture2D(41&textureDesc,42&data,43m_Texture.ReleaseAndGetAddressOf()44)45);4647m_d3dDevice->CreateShaderResourceView(m_Texture.Get(), NULL, m_SRV.ReleaseAndGetAddressOf());48D3D11_SAMPLER_DESC sampDesc;49ZeroMemory(&sampDesc, sizeof(sampDesc));50sampDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;51sampDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;52sampDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;53sampDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;54sampDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;55sampDesc.MinLOD = 0;56sampDesc.MaxLOD = D3D11_FLOAT32_MAX;57m_d3dDevice->CreateSamplerState(&sampDesc, m_QuadsTexSamplerState.ReleaseAndGetAddressOf());58}59else60{61int nRowSpan = width * pixelSize;62D3D11_MAPPED_SUBRESOURCE mappedResource;63HRESULT hr = m_d3dContext->Map(m_Texture.Get(), 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);64BYTE* mappedData = static_cast<BYTE*>(mappedResource.pData);6566for (int i = 0; i < height; ++i)67{68memcpy(mappedData + (i*mappedResource.RowPitch), buffer + (i*nRowSpan), nRowSpan);69}7071m_d3dContext->Unmap(m_Texture.Get(), 0);72}73}7475void QuadRenderer::CreateDeviceResources()76{77Direct3DBase::CreateDeviceResources();78D3D11_BLEND_DESC blendDesc;79ZeroMemory( &blendDesc, sizeof(blendDesc) );8081D3D11_RENDER_TARGET_BLEND_DESC rtbd;82ZeroMemory( &rtbd, sizeof(rtbd) );8384rtbd.BlendEnable = TRUE;85rtbd.SrcBlend = D3D11_BLEND_SRC_ALPHA;86rtbd.DestBlend = D3D11_BLEND_INV_SRC_ALPHA;87rtbd.BlendOp = D3D11_BLEND_OP_ADD;88rtbd.SrcBlendAlpha = D3D11_BLEND_ONE;89rtbd.DestBlendAlpha = D3D11_BLEND_ZERO;90rtbd.BlendOpAlpha = D3D11_BLEND_OP_ADD;91rtbd.RenderTargetWriteMask = 0x0f;9293blendDesc.AlphaToCoverageEnable = false;94blendDesc.RenderTarget[0] = rtbd;9596m_d3dDevice->CreateBlendState(&blendDesc, &m_Transparency);9798D3D11_RASTERIZER_DESC cmdesc;99ZeroMemory(&cmdesc, sizeof(D3D11_RASTERIZER_DESC));100101cmdesc.FillMode = D3D11_FILL_SOLID;102cmdesc.CullMode = D3D11_CULL_BACK;103cmdesc.DepthClipEnable = TRUE;104105cmdesc.FrontCounterClockwise = true;106m_d3dDevice->CreateRasterizerState(&cmdesc, &CCWcullMode);107108cmdesc.FrontCounterClockwise = false;109m_d3dDevice->CreateRasterizerState(&cmdesc, &CWcullMode);110111auto loadVSTask = DX::ReadDataAsync("SimpleVertexShader.cso");112auto loadPSTask = DX::ReadDataAsync("SimplePixelShader.cso");113auto createVSTask = loadVSTask.then([this](Platform::Array<byte>^ fileData)114{115DX::ThrowIfFailed(116m_d3dDevice->CreateVertexShader(117fileData->Data,118fileData->Length,119nullptr,120&m_vertexShader121)122);123124const D3D11_INPUT_ELEMENT_DESC vertexDesc[] =125{126{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },127{ "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },128};129130DX::ThrowIfFailed(131m_d3dDevice->CreateInputLayout(132vertexDesc,133ARRAYSIZE(vertexDesc),134fileData->Data,135fileData->Length,136&m_inputLayout137)138);139});140141auto createPSTask = loadPSTask.then([this](Platform::Array<byte>^ fileData)142{143DX::ThrowIfFailed(144m_d3dDevice->CreatePixelShader(145fileData->Data,146fileData->Length,147nullptr,148&m_pixelShader149)150);151152CD3D11_BUFFER_DESC constantBufferDesc(sizeof(ModelViewProjectionConstantBuffer), D3D11_BIND_CONSTANT_BUFFER);153DX::ThrowIfFailed(154m_d3dDevice->CreateBuffer(155&constantBufferDesc,156nullptr,157&m_constantBuffer158)159);160});161162auto createCubeTask = (createPSTask && createVSTask).then([this] ()163{164Vertex v[] =165{166Vertex(-1.0f, -1.0f, 1.0f, 1.0f, 1.0f),167Vertex(1.0f, -1.0f, 1.0f, 0.0f, 1.0f),168Vertex(1.0f, 1.0f, 1.0f, 0.0f, 0.0f),169Vertex(-1.0f, 1.0f, 1.0f, 1.0f, 0.0f)170};171172D3D11_SUBRESOURCE_DATA vertexBufferData = {0};173vertexBufferData.pSysMem = v;174vertexBufferData.SysMemPitch = 0;175vertexBufferData.SysMemSlicePitch = 0;176CD3D11_BUFFER_DESC vertexBufferDesc(sizeof(v), D3D11_BIND_VERTEX_BUFFER);177DX::ThrowIfFailed(178m_d3dDevice->CreateBuffer(179&vertexBufferDesc,180&vertexBufferData,181&m_vertexBuffer182)183);184185DWORD indices[] =186{187// Front Face1880, 2, 1,1890, 3, 2,190191};192193m_indexCount = ARRAYSIZE(indices);194195D3D11_SUBRESOURCE_DATA indexBufferData = {0};196indexBufferData.pSysMem = indices;197indexBufferData.SysMemPitch = 0;198indexBufferData.SysMemSlicePitch = 0;199CD3D11_BUFFER_DESC indexBufferDesc(sizeof(indices), D3D11_BIND_INDEX_BUFFER);200DX::ThrowIfFailed(201m_d3dDevice->CreateBuffer(202&indexBufferDesc,203&indexBufferData,204&m_indexBuffer205)206);207});208209createCubeTask.then([this] ()210{211m_loadingComplete = true;212});213}214215void QuadRenderer::CreateWindowSizeDependentResources()216{217Direct3DBase::CreateWindowSizeDependentResources();218219float aspectRatio = m_windowBounds.Width / m_windowBounds.Height;220float fovAngleY = 60.0f * (XM_PI / 180.0f);221222if (aspectRatio < 1.0f)223{224fovAngleY /= aspectRatio;225}226227XMStoreFloat4x4(228&m_constantBufferData.projection,229XMMatrixTranspose(230XMMatrixPerspectiveFovRH(231fovAngleY,232aspectRatio,2330.01f,234100.0f235)236)237);238}239240void QuadRenderer::Update(float timeTotal, float timeDelta)241{242(void) timeDelta; // Unused parameter.243244XMVECTOR X = XMVectorSet(0.0f, 0.0f, .3f, 0.0f);245XMVECTOR Y = XMVectorSet(0.0f, 0.0f, 0.0f, 0.0f);246XMVECTOR Z = XMVectorSet(1.0f, 0.0f, 0.0f, 0.0f);247248XMStoreFloat4x4(&m_constantBufferData.view, XMMatrixTranspose(XMMatrixLookAtLH(X, Y, Z)));249XMStoreFloat4x4(&m_constantBufferData.model, XMMatrixTranspose(XMMatrixRotationY(timeTotal * XM_PIDIV4)));250}251252void QuadRenderer::Render()253{254Render(m_renderTargetView, m_depthStencilView);255}256257void QuadRenderer::Render(Microsoft::WRL::ComPtr<ID3D11RenderTargetView> renderTargetView, Microsoft::WRL::ComPtr<ID3D11DepthStencilView> depthStencilView)258{259const float black[] = {0, 0, 0, 1.0 };260261m_d3dContext->ClearRenderTargetView(262renderTargetView.Get(),263black264);265266m_d3dContext->ClearDepthStencilView(267depthStencilView.Get(),268D3D11_CLEAR_DEPTH,2691.0f,2700271);272273if (m_SRV && m_loadingComplete) // Only draw the cube once it is loaded (loading is asynchronous).274{275m_d3dContext->OMSetRenderTargets(2761,277renderTargetView.GetAddressOf(),278depthStencilView.Get()279);280281m_d3dContext->UpdateSubresource(282m_constantBuffer.Get(),2830,284NULL,285&m_constantBufferData,2860,2870288);289290UINT stride = sizeof(Vertex);291UINT offset = 0;292293m_d3dContext->IASetVertexBuffers(2940,2951,296m_vertexBuffer.GetAddressOf(),297&stride,298&offset299);300301m_d3dContext->IASetIndexBuffer(302m_indexBuffer.Get(),303DXGI_FORMAT_R32_UINT,3040305);306307m_d3dContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);308m_d3dContext->IASetInputLayout(m_inputLayout.Get());309310m_d3dContext->VSSetShader(311m_vertexShader.Get(),312nullptr,3130314);315316m_d3dContext->VSSetConstantBuffers(3170,3181,319m_constantBuffer.GetAddressOf()320);321322m_d3dContext->PSSetShader(323m_pixelShader.Get(),324nullptr,3250326);327328m_d3dContext->PSSetShaderResources(0, 1, m_SRV.GetAddressOf());329m_d3dContext->PSSetSamplers(0, 1, m_QuadsTexSamplerState.GetAddressOf());330m_d3dContext->OMSetBlendState(m_Transparency.Get(), nullptr, 0xffffffff);331m_d3dContext->RSSetState(CCWcullMode.Get());332333m_d3dContext->DrawIndexed(334m_indexCount,3350,3360337);338}339}340341