Path: blob/master/samples/wp8/OcvRotatingCube/PhoneXamlDirect3DApp1/PhoneXamlDirect3DApp1Comp/CubeRenderer.cpp
16354 views
#include "pch.h"1#include "CubeRenderer.h"234using namespace DirectX;5using namespace Microsoft::WRL;6using namespace Windows::Foundation;7using namespace Windows::UI::Core;89CubeRenderer::CubeRenderer() :10m_loadingComplete(false),11m_indexCount(0)12{13}1415void CubeRenderer::CreateTextureFromByte(byte* buffer, int width, int height)16{17int pixelSize = 4;1819if (m_texture.Get() == nullptr)20{21CD3D11_TEXTURE2D_DESC textureDesc(22DXGI_FORMAT_B8G8R8A8_UNORM, // format23static_cast<UINT>(width), // width24static_cast<UINT>(height), // height251, // arraySize261, // mipLevels27D3D11_BIND_SHADER_RESOURCE, // bindFlags28D3D11_USAGE_DYNAMIC, // usage29D3D11_CPU_ACCESS_WRITE, // cpuaccessFlags301, // sampleCount310, // sampleQuality320 // miscFlags33);3435D3D11_SUBRESOURCE_DATA data;36data.pSysMem = buffer;37data.SysMemPitch = pixelSize*width;38data.SysMemSlicePitch = pixelSize*width*height;3940DX::ThrowIfFailed(41m_d3dDevice->CreateTexture2D(42&textureDesc,43&data,44m_texture.ReleaseAndGetAddressOf()45)46);4748m_d3dDevice->CreateShaderResourceView(m_texture.Get(), NULL, m_SRV.ReleaseAndGetAddressOf());49D3D11_SAMPLER_DESC sampDesc;50ZeroMemory(&sampDesc, sizeof(sampDesc));51sampDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;52sampDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;53sampDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;54sampDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;55sampDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;56sampDesc.MinLOD = 0;57sampDesc.MaxLOD = D3D11_FLOAT32_MAX;58m_d3dDevice->CreateSamplerState(&sampDesc, m_cubesTexSamplerState.ReleaseAndGetAddressOf());59}60else61{62int nRowSpan = width * pixelSize;63D3D11_MAPPED_SUBRESOURCE mappedResource;64HRESULT hr = m_d3dContext->Map(m_texture.Get(), 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);65BYTE* mappedData = static_cast<BYTE*>(mappedResource.pData);6667for (int i = 0; i < height; ++i)68{69memcpy(mappedData + (i*mappedResource.RowPitch), buffer + (i*nRowSpan), nRowSpan);70}7172m_d3dContext->Unmap(m_texture.Get(), 0);73}74}7576void CubeRenderer::CreateDeviceResources()77{78Direct3DBase::CreateDeviceResources();79D3D11_BLEND_DESC blendDesc;80ZeroMemory( &blendDesc, sizeof(blendDesc) );8182D3D11_RENDER_TARGET_BLEND_DESC rtbd;83ZeroMemory( &rtbd, sizeof(rtbd) );848586rtbd.BlendEnable = TRUE;87rtbd.SrcBlend = D3D11_BLEND_SRC_ALPHA;88rtbd.DestBlend = D3D11_BLEND_INV_SRC_ALPHA;89rtbd.BlendOp = D3D11_BLEND_OP_ADD;90rtbd.SrcBlendAlpha = D3D11_BLEND_ONE;91rtbd.DestBlendAlpha = D3D11_BLEND_ZERO;92rtbd.BlendOpAlpha = D3D11_BLEND_OP_ADD;93rtbd.RenderTargetWriteMask = 0x0f;94959697blendDesc.AlphaToCoverageEnable = false;98blendDesc.RenderTarget[0] = rtbd;99100m_d3dDevice->CreateBlendState(&blendDesc, &m_transparency);101102103D3D11_RASTERIZER_DESC cmdesc;104ZeroMemory(&cmdesc, sizeof(D3D11_RASTERIZER_DESC));105106cmdesc.FillMode = D3D11_FILL_SOLID;107cmdesc.CullMode = D3D11_CULL_BACK;108cmdesc.DepthClipEnable = TRUE;109110111cmdesc.FrontCounterClockwise = true;112m_d3dDevice->CreateRasterizerState(&cmdesc, &m_CCWcullMode);113114cmdesc.FrontCounterClockwise = false;115m_d3dDevice->CreateRasterizerState(&cmdesc, &m_CWcullMode);116117118auto loadVSTask = DX::ReadDataAsync("SimpleVertexShader.cso");119auto loadPSTask = DX::ReadDataAsync("SimplePixelShader.cso");120121auto createVSTask = loadVSTask.then([this](Platform::Array<byte>^ fileData) {122DX::ThrowIfFailed(123m_d3dDevice->CreateVertexShader(124fileData->Data,125fileData->Length,126nullptr,127&m_vertexShader128)129);130131const D3D11_INPUT_ELEMENT_DESC vertexDesc[] =132{133{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },134{ "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },135};136137138139140DX::ThrowIfFailed(141m_d3dDevice->CreateInputLayout(142vertexDesc,143ARRAYSIZE(vertexDesc),144fileData->Data,145fileData->Length,146&m_inputLayout147)148);149});150151auto createPSTask = loadPSTask.then([this](Platform::Array<byte>^ fileData) {152DX::ThrowIfFailed(153m_d3dDevice->CreatePixelShader(154fileData->Data,155fileData->Length,156nullptr,157&m_pixelShader158)159);160161CD3D11_BUFFER_DESC constantBufferDesc(sizeof(ModelViewProjectionConstantBuffer), D3D11_BIND_CONSTANT_BUFFER);162DX::ThrowIfFailed(163m_d3dDevice->CreateBuffer(164&constantBufferDesc,165nullptr,166&m_constantBuffer167)168);169});170171auto createCubeTask = (createPSTask && createVSTask).then([this] () {172Vertex v[] =173{174// Front Face175Vertex(-1.0f, -1.0f, -1.0f, 0.0f, 1.0f),176Vertex(-1.0f, 1.0f, -1.0f, 0.0f, 0.0f),177Vertex( 1.0f, 1.0f, -1.0f, 1.0f, 0.0f),178Vertex( 1.0f, -1.0f, -1.0f, 1.0f, 1.0f),179180// Back Face181Vertex(-1.0f, -1.0f, 1.0f, 1.0f, 1.0f),182Vertex( 1.0f, -1.0f, 1.0f, 0.0f, 1.0f),183Vertex( 1.0f, 1.0f, 1.0f, 0.0f, 0.0f),184Vertex(-1.0f, 1.0f, 1.0f, 1.0f, 0.0f),185186// Top Face187Vertex(-1.0f, 1.0f, -1.0f, 0.0f, 1.0f),188Vertex(-1.0f, 1.0f, 1.0f, 0.0f, 0.0f),189Vertex( 1.0f, 1.0f, 1.0f, 1.0f, 0.0f),190Vertex( 1.0f, 1.0f, -1.0f, 1.0f, 1.0f),191192// Bottom Face193Vertex(-1.0f, -1.0f, -1.0f, 1.0f, 1.0f),194Vertex( 1.0f, -1.0f, -1.0f, 0.0f, 1.0f),195Vertex( 1.0f, -1.0f, 1.0f, 0.0f, 0.0f),196Vertex(-1.0f, -1.0f, 1.0f, 1.0f, 0.0f),197198// Left Face199Vertex(-1.0f, -1.0f, 1.0f, 0.0f, 1.0f),200Vertex(-1.0f, 1.0f, 1.0f, 0.0f, 0.0f),201Vertex(-1.0f, 1.0f, -1.0f, 1.0f, 0.0f),202Vertex(-1.0f, -1.0f, -1.0f, 1.0f, 1.0f),203204// Right Face205Vertex( 1.0f, -1.0f, -1.0f, 0.0f, 1.0f),206Vertex( 1.0f, 1.0f, -1.0f, 0.0f, 0.0f),207Vertex( 1.0f, 1.0f, 1.0f, 1.0f, 0.0f),208Vertex( 1.0f, -1.0f, 1.0f, 1.0f, 1.0f),209};210211212213D3D11_SUBRESOURCE_DATA vertexBufferData = {0};214vertexBufferData.pSysMem = v;215vertexBufferData.SysMemPitch = 0;216vertexBufferData.SysMemSlicePitch = 0;217CD3D11_BUFFER_DESC vertexBufferDesc(sizeof(v), D3D11_BIND_VERTEX_BUFFER);218DX::ThrowIfFailed(219m_d3dDevice->CreateBuffer(220&vertexBufferDesc,221&vertexBufferData,222&m_vertexBuffer223)224);225226DWORD indices[] = {227// Front Face2280, 2, 1,2290, 3, 2,230231// Back Face2324, 6, 5,2334, 7, 6,234235// Top Face2368, 10, 9,2378, 11, 10,238239// Bottom Face24012, 14, 13,24112, 15, 14,242243// Left Face24416, 18, 17,24516, 19, 18,246247// Right Face24820, 22, 21,24920, 23, 22250};251252m_indexCount = ARRAYSIZE(indices);253254D3D11_SUBRESOURCE_DATA indexBufferData = {0};255indexBufferData.pSysMem = indices;256indexBufferData.SysMemPitch = 0;257indexBufferData.SysMemSlicePitch = 0;258CD3D11_BUFFER_DESC indexBufferDesc(sizeof(indices), D3D11_BIND_INDEX_BUFFER);259DX::ThrowIfFailed(260m_d3dDevice->CreateBuffer(261&indexBufferDesc,262&indexBufferData,263&m_indexBuffer264)265);266});267268createCubeTask.then([this] () {269m_loadingComplete = true;270});271}272273void CubeRenderer::CreateWindowSizeDependentResources()274{275Direct3DBase::CreateWindowSizeDependentResources();276277float aspectRatio = m_windowBounds.Width / m_windowBounds.Height;278float fovAngleY = 70.0f * XM_PI / 180.0f;279if (aspectRatio < 1.0f)280{281fovAngleY /= aspectRatio;282}283284XMStoreFloat4x4(285&m_constantBufferData.projection,286XMMatrixTranspose(287XMMatrixPerspectiveFovRH(288fovAngleY,289aspectRatio,2900.01f,291100.0f292)293)294);295}296297void CubeRenderer::Update(float timeTotal, float timeDelta)298{299(void) timeDelta; // Unused parameter.300301XMVECTOR eye = XMVectorSet(0.0f, 0.0f, 3.f, 0.0f);302XMVECTOR at = XMVectorSet(0.0f, 0.0f, 0.0f, 0.0f);303XMVECTOR up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);304305XMStoreFloat4x4(&m_constantBufferData.view, XMMatrixTranspose(XMMatrixLookAtRH(eye, at, up)));306XMStoreFloat4x4(&m_constantBufferData.model, XMMatrixTranspose(XMMatrixRotationY(timeTotal * XM_PIDIV4)));307308309}310311void CubeRenderer::Render()312{313314std::lock_guard<std::mutex> lock(m_mutex);315Render(m_renderTargetView, m_depthStencilView);316}317318void CubeRenderer::Render(Microsoft::WRL::ComPtr<ID3D11RenderTargetView> renderTargetView, Microsoft::WRL::ComPtr<ID3D11DepthStencilView> depthStencilView)319{320321const float black[] = {0, 0, 0, 1.0 };322m_d3dContext->ClearRenderTargetView(323renderTargetView.Get(),324black325);326327m_d3dContext->ClearDepthStencilView(328depthStencilView.Get(),329D3D11_CLEAR_DEPTH,3301.0f,3310332);333334335336// Only draw the cube once it is loaded (loading is asynchronous).337if (!m_SRV || !m_loadingComplete)338{339return;340}341342m_d3dContext->OMSetRenderTargets(3431,344renderTargetView.GetAddressOf(),345depthStencilView.Get()346);347348m_d3dContext->UpdateSubresource(349m_constantBuffer.Get(),3500,351NULL,352&m_constantBufferData,3530,3540355);356357UINT stride = sizeof(Vertex);358UINT offset = 0;359m_d3dContext->IASetVertexBuffers(3600,3611,362m_vertexBuffer.GetAddressOf(),363&stride,364&offset365);366367m_d3dContext->IASetIndexBuffer(368m_indexBuffer.Get(),369DXGI_FORMAT_R32_UINT,3700371);372373374m_d3dContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);375376m_d3dContext->IASetInputLayout(m_inputLayout.Get());377378m_d3dContext->VSSetShader(379m_vertexShader.Get(),380nullptr,3810382);383384m_d3dContext->VSSetConstantBuffers(3850,3861,387m_constantBuffer.GetAddressOf()388);389390m_d3dContext->PSSetShader(391m_pixelShader.Get(),392nullptr,3930394);395396m_d3dContext->PSSetShaderResources( 0, 1, m_SRV.GetAddressOf());397m_d3dContext->PSSetSamplers( 0, 1, m_cubesTexSamplerState.GetAddressOf());398399//float blendFactor[] = {0.75f, 0.75f, 0.75f, 1.0f};400m_d3dContext->OMSetBlendState(m_transparency.Get(), nullptr, 0xffffffff);401402m_d3dContext->RSSetState(m_CCWcullMode.Get());403m_d3dContext->DrawIndexed(404m_indexCount,4050,4060407);408409m_d3dContext->RSSetState(m_CWcullMode.Get());410m_d3dContext->DrawIndexed(411m_indexCount,4120,4130414);415}416417