void ParticleManager::RenderParticles(DxGraphics* pDxGraphics, Camera& cam) { cam.CalculateViewMatrix(); XMMATRIX tWorld = XMMatrixTranspose(m_worldMat); XMMATRIX tView = XMMatrixTranspose(cam.GetViewMatrix()); XMMATRIX tProj = XMMatrixTranspose(cam.GetProjMatrix()); ID3D11DeviceContext* pCon = pDxGraphics->GetImmediateContext(); UINT stride = sizeof(Particle); UINT offset = 0; pCon->IASetVertexBuffers(0, 1, &m_vBuffer, &stride, &offset); pCon->IASetInputLayout(m_inputLayout); pCon->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_POINTLIST); pCon->VSSetShader(m_vShader, 0, 0); pCon->PSSetShader(m_pShader, 0, 0); pCon->UpdateSubresource(m_camWorldBuffer, 0, NULL, &tWorld, 0, 0); pCon->UpdateSubresource(m_camViewBuffer, 0, NULL, &tView, 0, 0); pCon->UpdateSubresource(m_camProjBuffer, 0, NULL, &tProj, 0, 0); pCon->VSSetConstantBuffers(0, 1, &m_camWorldBuffer); pCon->VSSetConstantBuffers(1, 1, &m_camViewBuffer); pCon->VSSetConstantBuffers(2, 1, &m_camProjBuffer); pCon->Draw(m_particleList.size(), 0); pCon->VSSetShader(NULL, 0, 0); pCon->GSSetShader(NULL, 0, 0); pCon->PSSetShader(NULL, 0, 0); }
void ParticleManager::UpdateParticles(float dt, DxGraphics* pDxGraphics, Camera& cam) { // Update the world mat XMMATRIX transMat = XMMatrixTranslation(0.0f, 0.0f, 0.0f); XMMATRIX rotMat = XMMatrixRotationRollPitchYaw(0.0f, 0.0f, 0.0f); XMMATRIX scaleMat = XMMatrixScaling(1.0f, 1.0f, 1.0f); m_worldMat = rotMat * scaleMat * transMat; cam.CalculateViewMatrix(); // stream out the particles which runs through the physics XMMATRIX tWorld = XMMatrixTranspose(m_worldMat); XMMATRIX tView = XMMatrixTranspose(cam.GetViewMatrix()); XMMATRIX tProj = XMMatrixTranspose(cam.GetProjMatrix()); m_massPoint.dt = dt; ID3D11DeviceContext* pCon = pDxGraphics->GetImmediateContext(); // Set the stream out buffer UINT offsetSO[1] = { 0 }; pCon->SOSetTargets(1, &m_vStream, offsetSO); UINT stride = sizeof(Particle); UINT offset = 0; pCon->IASetVertexBuffers(0, 1, &m_vBuffer, &stride, &offset); pCon->IASetInputLayout(m_inputLayout); pCon->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_POINTLIST); pCon->VSSetShader(m_vUpdate, 0, 0); pCon->GSSetShader(m_gUpdate, 0, 0); pCon->PSSetShader(NULL, 0, 0); //// Update the cBuffer pCon->UpdateSubresource(m_pointBuffer, 0, NULL, &m_massPoint, 0, 0); pCon->VSSetConstantBuffers(3, 1, &m_pointBuffer); pCon->Draw(m_particleList.size(), 0); pCon->VSSetShader(NULL, 0, 0); pCon->GSSetShader(NULL, 0, 0); pCon->PSSetShader(NULL, 0, 0); // Unbind the SO ID3D11Buffer* bufferArray[1] = { 0 }; pCon->SOSetTargets(1, bufferArray, offsetSO); std::swap(m_vStream, m_vBuffer); }
bool SkyBox::Render(std::shared_ptr<D3DRenderer> d3d, DirectX::CXMMATRIX world, DirectX::CXMMATRIX view, DirectX::CXMMATRIX projection, std::shared_ptr<Camera> camera, std::shared_ptr<Light> light) { ID3D11DeviceContext *context = d3d->GetDeviceContext(); DirectX::XMMATRIX sphereWorld = DirectX::XMMatrixIdentity(), scale = DirectX::XMMatrixScaling(0.3f, 0.3f, 0.3f), translation; translation = DirectX::XMMatrixTranslationFromVector(camera->GetPosition()); sphereWorld = scale * translation; if (!SetShaderParameters(context, sphereWorld, view, projection, texture->GetTexture())) return false; UINT strides = sizeof(DirectX::VertexPositionNormalTexture), offset = 0; context->IASetInputLayout(m_layout); context->IASetIndexBuffer(sphereIndexBuffer, DXGI_FORMAT_R32_UINT, 0); context->IASetVertexBuffers(0, 1, &sphereVertBuffer, &strides, &offset); context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); context->PSSetShader(pixelShader, NULL, 0); context->VSSetShader(vertexShader, NULL, 0); context->RSSetState(d3d->GetRasterState(1)); d3d->SetDepthLessEqual(); context->DrawIndexed(NumSphereFaces*3, 0, 0); d3d->End2D(); context->RSSetState(d3d->GetRasterState(0)); return true; }
void CFullscreenTriangleDrawer::DrawDX11( ID3D11ShaderResourceView* pTextureSRV ) { ID3D11Device* pDevice = static_cast<ID3D11Device*>( gD3DDevice ); ID3D11DeviceContext* pContext = NULL; pDevice->GetImmediateContext( &pContext ); CDX11StateGuard stateGuard; pContext->IASetInputLayout( NULL ); pContext->IASetIndexBuffer( NULL, DXGI_FORMAT_UNKNOWN, 0 ); pContext->IASetVertexBuffers( 0, 0, NULL, NULL, NULL ); pContext->IASetPrimitiveTopology( D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST ); pContext->VSSetShader( m_pVertexShader11, NULL, 0 ); pContext->PSSetShader( m_pPixelShader11, NULL, 0 ); ID3D11SamplerState* pNullSampler[] = { NULL }; pContext->PSSetSamplers( 0, 1, pNullSampler ); pContext->PSSetShaderResources( 0, 1, &pTextureSRV ); pContext->OMSetBlendState( m_pBlendState11, NULL, 0xFFFFFFFF ); // Draw pContext->Draw( 3, 0 ); }
//--------------------------------------------------------------------------- void FontRenderer::FontDrawingBegin( RenderContext* rc ) { assert( m_vertexBuffer != NULL ); assert( m_indexBuffer != NULL ); m_currentRC = rc; m_fontDrawOps.clear(); ID3D11DeviceContext* dc = m_currentRC->Context(); UINT viewportCount = 1; D3D11_VIEWPORT vp; dc->RSGetViewports(&viewportCount, &vp); UINT stride = sizeof(FontTextVertex); UINT offset = 0; dc->IASetInputLayout(m_vertexLayout); dc->IASetIndexBuffer(m_indexBuffer, DXGI_FORMAT_R32_UINT, 0); dc->IASetVertexBuffers(0, 1, &m_vertexBuffer, &stride, &offset); dc->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); dc->VSSetShader( m_vertexShader, 0, 0 ); dc->PSSetShader( m_pixelShader, 0, 0 ); dc->PSSetSamplers( 0, 1, &m_samplerState ); dc->RSSetState(m_rasterState); float blendFactor[4] = {1.0f}; dc->OMSetBlendState(m_blendState, blendFactor, 0xffffffff); m_activeFont = NULL; // needs to be set! }
void dx11ShaderOverride::terminateKey(MHWRender::MDrawContext& context, const MString& /*key*/) { if (fShaderNode) { MHWRender::MRenderer* theRenderer = MHWRender::MRenderer::theRenderer(); if (theRenderer) { dx11ShaderDX11Device* dxDevice = (dx11ShaderDX11Device*)theRenderer->GPUDeviceHandle(); if (dxDevice) { ID3D11DeviceContext* dxContext = NULL; dxDevice->GetImmediateContext(&dxContext); if (dxContext) { fShaderNode->restoreStates(dxContext, fStates); dxContext->VSSetShader(NULL, NULL, 0); dxContext->PSSetShader(NULL, NULL, 0); dxContext->GSSetShader(NULL, NULL, 0); dxContext->HSSetShader(NULL, NULL, 0); dxContext->DSSetShader(NULL, NULL, 0); dxContext->Release(); } } } } }
void TgcDX11Effect::begin() { ID3D11DeviceContext* deviceContext = ((TgcDX11Renderer*)GuiController::Instance->renderer)->deviceContext; //Set the vertex and pixel shaders that will be used to render deviceContext->VSSetShader(this->vertexShader, NULL, 0); deviceContext->PSSetShader(this->pixelShader, NULL, 0); }
void FBXObject::SetupDrawInputVertexShader() { ID3D11DeviceContext* pImmediateContext = DX11App::getInstance()->direct3d.pImmediateContext; pImmediateContext->IASetInputLayout( this->pInputLayout.second ); pImmediateContext->VSSetShader( this->pVertexShader.second, NULL, 0 ); }
static void DoRendering (const float* worldMatrix, const float* identityMatrix, float* projectionMatrix, const MyVertex* verts) { // Does actual rendering of a simple triangle #if SUPPORT_D3D11 // D3D11 case if (s_DeviceType == kUnityGfxRendererD3D11 && EnsureD3D11ResourcesAreCreated()) { ID3D11DeviceContext* ctx = NULL; g_D3D11Device->GetImmediateContext (&ctx); ID3D11RenderTargetView* pCurrentRenderTarget; ID3D11DepthStencilView* pCurrentDepthStencil; // Get the current render targets ctx->OMGetRenderTargets(1, &pCurrentRenderTarget, &pCurrentDepthStencil); ctx->OMSetRenderTargets(1, &g_pD3D11RenderTargetView, nullptr); /* // update native texture from code D3D11_TEXTURE2D_DESC desc; g_TexturePointer->GetDesc(&desc); unsigned char* data = new unsigned char[desc.Width*desc.Height * 4]; FillTextureFromCode(desc.Width, desc.Height, desc.Width * 4, data); ctx->UpdateSubresource(g_TexturePointer, 0, NULL, data, desc.Width * 4, 0); delete[] data; */ const float CLEAR_CLR[4] = { 1, 1, 0, 1 }; // Yellow ctx->ClearRenderTargetView(g_pD3D11RenderTargetView, CLEAR_CLR); // Restore the original render target ctx->OMSetRenderTargets(1, &pCurrentRenderTarget, pCurrentDepthStencil); // update constant buffer - just the world matrix in our case ctx->UpdateSubresource (g_D3D11CB, 0, NULL, worldMatrix, 64, 0); // set shaders ctx->VSSetConstantBuffers (0, 1, &g_D3D11CB); ctx->VSSetShader (g_D3D11VertexShader, NULL, 0); ctx->PSSetShader (g_D3D11PixelShader, NULL, 0); // update vertex buffer ctx->UpdateSubresource (g_D3D11VB, 0, NULL, verts, sizeof(verts[0])*3, 0); // set input assembler data and draw ctx->IASetInputLayout (g_D3D11InputLayout); ctx->IASetPrimitiveTopology (D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); UINT stride = sizeof(MyVertex); UINT offset = 0; ctx->IASetVertexBuffers (0, 1, &g_D3D11VB, &stride, &offset); ctx->Draw (3, 0); ctx->Release(); } #endif }
void DeferredPipeline::Lighting::set_layout_spot_light_shaders_samplers(ID3D11DeviceContext& device_context) { device_context.IASetInputLayout(_vertex_input_layout); device_context.VSSetShader(_vertex_shader, nullptr, 0); device_context.PSSetShader(_spot_light_pixel_shader, nullptr, 0); device_context.PSSetSamplers(0, 1, &_sampler_state); }
void CD3DPixelShader::UnbindShader() { if (!m_inited) return; ID3D11DeviceContext* pContext = g_Windowing.Get3D11Context(); pContext->IASetInputLayout(nullptr); pContext->VSSetShader(nullptr, nullptr, 0); }
void CD3DVertexShader::BindShader() { if (!m_inited) return; ID3D11DeviceContext* pContext = g_Windowing.Get3D11Context(); if (!pContext) return; pContext->IASetInputLayout(m_inputLayout); pContext->VSSetShader(m_VS, nullptr, 0); }
~CDX11StateGuard() { ID3D11Device* pDevice = static_cast<ID3D11Device*>( gD3DDevice ); ID3D11DeviceContext* pContext = NULL; pDevice->GetImmediateContext( &pContext ); // Apply saved state pContext->OMSetBlendState( m_pBlendState, m_BlendFactor, m_SampleMask ); pContext->RSSetState( m_pRasterizerState ); pContext->IASetPrimitiveTopology( m_PrimitiveTopology ); pContext->IASetIndexBuffer( m_pIndexBuffer, m_IndexBufferFormat, m_IndexBufferOffset ); pContext->IASetInputLayout( m_pInputLayout ); pContext->IASetVertexBuffers( 0, D3D11_IA_VERTEX_INPUT_RESOURCE_SLOT_COUNT, m_pVertexBuffers, m_pVertexBufferStrides, m_pVertexBufferOffsets ); pContext->VSSetShader( m_pVertexShader, m_ppVertexShaderClassInstances, m_VertexShaderClassInstancesCount ); pContext->PSSetShader( m_pPixelShader, m_ppPixelShaderClassInstances, m_PixelShaderClassInstancesCount ); pContext->PSSetSamplers( 0, D3D11_COMMONSHADER_SAMPLER_SLOT_COUNT, m_ppPixelShaderSamplers ); pContext->PSSetShaderResources( 0, D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT, m_ppPixelShaderResources ); // Release references SAFE_RELEASE( m_pBlendState ); SAFE_RELEASE( m_pRasterizerState ); SAFE_RELEASE( m_pIndexBuffer ); SAFE_RELEASE( m_pInputLayout ); for ( UINT i = 0; i < D3D11_IA_VERTEX_INPUT_RESOURCE_SLOT_COUNT; ++i ) { SAFE_RELEASE( m_pVertexBuffers[i] ); } SAFE_RELEASE( m_pVertexShader ); for ( UINT i = 0; i < m_VertexShaderClassInstancesCount; ++i ) { SAFE_RELEASE( m_ppVertexShaderClassInstances[i] ); } SAFE_RELEASE( m_pPixelShader ); for ( UINT i = 0; i < m_PixelShaderClassInstancesCount; ++i ) { SAFE_RELEASE( m_ppPixelShaderClassInstances[i] ); } for ( UINT i = 0; i < D3D11_COMMONSHADER_SAMPLER_SLOT_COUNT; ++i ) { SAFE_RELEASE( m_ppPixelShaderSamplers[i] ); } for ( UINT i = 0; i < D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT; ++i ) { SAFE_RELEASE( m_ppPixelShaderResources[i] ); } }
void MSAAFilter::RenderAA() { PIXEvent pixEvent(L"MSAA Resolve + Temporal AA"); ProfileBlock profileBlock(L"MSAA Resolve + Temporal AA"); ID3D11DeviceContext* context = deviceManager.ImmediateContext(); ID3D11RenderTargetView* rtvs[1] = { resolveTarget.RTView }; context->OMSetRenderTargets(1, rtvs, nullptr); if(AppSettings::UseStandardResolve) { if(AppSettings::MSAAMode == 0) context->CopyResource(resolveTarget.Texture, colorTarget.Texture); else context->ResolveSubresource(resolveTarget.Texture, 0, colorTarget.Texture, 0, colorTarget.Format); return; } const uint32 SampleRadius = static_cast<uint32>((AppSettings::ResolveFilterDiameter / 2.0f) + 0.499f); ID3D11PixelShader* pixelShader = resolvePS[AppSettings::MSAAMode]; context->PSSetShader(pixelShader, nullptr, 0); context->VSSetShader(resolveVS, nullptr, 0); resolveConstants.Data.TextureSize = Float2(static_cast<float>(colorTarget.Width), static_cast<float>(colorTarget.Height)); resolveConstants.Data.SampleRadius = SampleRadius;; resolveConstants.ApplyChanges(context); resolveConstants.SetPS(context, 0); ID3D11ShaderResourceView* srvs[] = { colorTarget.SRView, velocityTarget.SRView, depthBuffer.SRView, prevFrameTarget.SRView}; context->PSSetShaderResources(0, ArraySize_(srvs), srvs); ID3D11SamplerState* samplers[] = { samplerStates.LinearClamp(), samplerStates.Point() }; context->PSSetSamplers(0, ArraySize_(samplers), samplers); ID3D11Buffer* vbs[1] = { nullptr }; UINT strides[1] = { 0 }; UINT offsets[1] = { 0 }; context->IASetVertexBuffers(0, 1, vbs, strides, offsets); context->IASetInputLayout(nullptr); context->IASetIndexBuffer(nullptr, DXGI_FORMAT_R16_UINT, 0); context->Draw(3, 0); rtvs[0] = nullptr; context->OMSetRenderTargets(1, rtvs, nullptr); srvs[0] = srvs[1] = srvs[2] = nullptr; context->PSSetShaderResources(0, 3, srvs); context->CopyResource(prevFrameTarget.Texture, resolveTarget.Texture); }
void MultiTextureShader::RenderShader(Graphics* graphics) { ID3D11DeviceContext* deviceContext = graphics->GetImmediateContex(); deviceContext->IASetInputLayout(m_layout); // set the vertex and pixel shaders that will be used to render deviceContext->VSSetShader(m_vertexShader,NULL,0); deviceContext->PSSetShader(m_pixelShader,NULL,0); // set the sampler state deviceContext->PSSetSamplers(0,1,&m_sampleState); }
void D11State::draw() { clock_t t = clock(); float elapsed = static_cast<float>(t - timeT) / CLOCKS_PER_SEC; ++frameCount; if (elapsed > OVERLAY_FPS_INTERVAL) { OverlayMsg om; om.omh.uiMagic = OVERLAY_MAGIC_NUMBER; om.omh.uiType = OVERLAY_MSGTYPE_FPS; om.omh.iLength = sizeof(OverlayMsgFps); om.omf.fps = frameCount / elapsed; sendMessage(om); frameCount = 0; timeT = t; } checkMessage(static_cast<unsigned int>(vp.Width), static_cast<unsigned int>(vp.Height)); if (a_ucTexture && pSRView && (uiLeft != uiRight)) { if (!bDeferredContext) { pOrigStateBlock->Capture(); pMyStateBlock->Apply(); } // Set vertex buffer UINT stride = sizeof(SimpleVertex); UINT offset = 0; pDeviceContext->IASetVertexBuffers(0, 1, &pVertexBuffer, &stride, &offset); pDeviceContext->VSSetShader(pVertexShader, NULL, 0); pDeviceContext->GSSetShader(NULL, NULL, 0); pDeviceContext->PSSetShaderResources(0, 1, &pSRView); pDeviceContext->PSSetShader(pPixelShader, NULL, 0); pDeviceContext->DrawIndexed(6, 0, 0); if (bDeferredContext) { ID3D11CommandList *pCommandList; pDeviceContext->FinishCommandList(TRUE, &pCommandList); ID3D11DeviceContext *ctx = NULL; pDevice->GetImmediateContext(&ctx); ctx->ExecuteCommandList(pCommandList, TRUE); ctx->Release(); pCommandList->Release(); } else { pDeviceContext->Flush(); pMyStateBlock->Capture(); pOrigStateBlock->Apply(); } } }
void PipelineStateCache::setVertexShader(const VertexShader& vertexShader) { // FIXME: cache this ID3D11Buffer* const* constantBuffers; uint32 numConstantBuffers = vertexShader.queryBuffersArray(constantBuffers); m_deviceContext->VSSetConstantBuffers(0, numConstantBuffers, constantBuffers); ID3D11VertexShader* shaderResourcePtr = vertexShader.getResourcePtr(); if (shaderResourcePtr != m_currentVertexShader) { m_currentVertexShader = shaderResourcePtr; m_deviceContext->VSSetShader(shaderResourcePtr, NULL, 0); } }
// Callbacks to draw things in world space, left-hand space, and right-hand // space. void DrawWorld( void* userData //< Passed into AddRenderCallback , osvr::renderkit::GraphicsLibrary library //< Graphics library context to use , osvr::renderkit::RenderBuffer buffers //< Buffers to use , osvr::renderkit::OSVR_ViewportDescription viewport //< Viewport we're rendering into , OSVR_PoseState pose //< OSVR ModelView matrix set by RenderManager , osvr::renderkit::OSVR_ProjectionMatrix projection //< Projection matrix set by RenderManager , OSVR_TimeValue deadline //< When the frame should be sent to the screen ) { // Make sure our pointers are filled in correctly. if (library.D3D11 == nullptr) { std::cerr << "DrawWorld: No D3D11 GraphicsLibrary, this should not happen" << std::endl; return; } if (buffers.D3D11 == nullptr) { std::cerr << "DrawWorld: No D3D11 RenderBuffer, this should not happen" << std::endl; return; } ID3D11DeviceContext* context = library.D3D11->context; ID3D11RenderTargetView* renderTargetView = buffers.D3D11->colorBufferView; // Set vertex buffer UINT stride = sizeof(SimpleVertex); UINT offset = 0; context->IASetVertexBuffers(0, 1, &g_vertexBuffer, &stride, &offset); // Set primitive topology context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); // Set depth/stencil state context->OMSetDepthStencilState(g_depthStencilState, 1); // Draw a triangle using the simple shaders context->VSSetShader(vertexShader.Get(), nullptr, 0); context->PSSetShader(pixelShader.Get(), nullptr, 0); context->Draw(3, 0); }
void Shader::bind(D3DRenderer* renderer) { ID3D11DeviceContext* context = renderer->context(); context->IASetInputLayout(mpInputLayout); context->IASetPrimitiveTopology(mPrimitiveTopology); //Set any active shaders and disable ones not in use context->VSSetShader(mpVertexShader, NULL, 0); context->PSSetShader(mpPixelShader, NULL, 0); context->GSSetShader(mpGeometryShader, NULL, 0); context->CSSetShader(mpComputeShader, NULL, 0); context->HSSetShader(mpHullShader, NULL, 0); context->DSSetShader(mpDomainShader, NULL, 0); }
void ProjectiveTextureShader::RenderShader(int indexCount) { ID3D11DeviceContext* deviceContext = GraphicsDX::GetDeviceContext(); deviceContext->IASetInputLayout(m_layout); // Set the vertex and pixel shaders that will be used to render this triangle. deviceContext->VSSetShader(m_vertexShader, NULL, 0); deviceContext->PSSetShader(m_pixelShader, NULL, 0); // Set the sampler state in the pixel shader. deviceContext->PSSetSamplers(0, 1, &m_sampleState); // Render the triangle. deviceContext->DrawIndexed(indexCount, 0, 0); }
const void ShaderClass::RenderShader(UINT indexCount, UINT indexStart)const { ID3D11DeviceContext* c = SystemClass::GetInstance()->mGrapInst->GetContext(); // Set the vertex input layout. c->IASetInputLayout(mLayout); // Set the vertex and pixel shaders that will be used to render this triangle. c->VSSetShader(mVertexShader, nullptr, 0); c->HSSetShader(mHullShader, nullptr, 0); c->DSSetShader(mDomainShader, nullptr, 0); c->GSSetShader(mGeometryShader, nullptr, 0); c->PSSetShader(mPixelShader, nullptr, 0); // Render mesh stored in active buffers c->DrawIndexed(indexCount, indexStart, 0); }
void Draw() { ID3D11DeviceContext* context = g_d3d.context_.get(); // Clear float clearColor[] = { 0.3f, 0.3f, 0.3f, 1.0f }; context->ClearRenderTargetView( g_d3d.backBufferRTV_.get(), clearColor ); auto* rtv = g_d3d.backBufferRTV_.get(); context->OMSetRenderTargets( 1, &rtv, nullptr ); // Draw body auto cbModelWVP = DirectX::XMMatrixIdentity(); auto matWorld = DirectX::XMMatrixIdentity(); matWorld = DirectX::XMMatrixRotationRollPitchYaw( g_d3d.jointRot_[ 0 ], g_d3d.jointRot_[ 1 ], g_d3d.jointRot_[ 2 ] ); auto matView = DirectX::XMMatrixLookAtLH( DirectX::XMVectorSet( 0, 0, -3, 0 ), DirectX::XMVectorSet( 0, 0, 5, 0 ), DirectX::XMVectorSet( 0, 1, 0, 0 ) ); auto matProj = DirectX::XMMatrixPerspectiveFovLH( DirectX::XMConvertToRadians( 50 ), (float)g_windowWidth / (float)g_windowHeight, 0.01f, 1000.0f ); cbModelWVP = matWorld * matView * matProj; cbModelWVP = DirectX::XMMatrixTranspose( cbModelWVP ); g_d3d.context_->UpdateSubresource( g_d3d.modelCB_.get(), 0, nullptr, &cbModelWVP, 0, 0 ); auto* vb = g_d3d.modelVB_.get(); unsigned int stride = sizeof( D3D::MeshFormat ); unsigned int offset = 0; auto* cb = g_d3d.modelCB_.get(); D3D11_VIEWPORT viewport = { 0, 0, g_windowWidth, g_windowHeight, 0, 1 }; context->IASetPrimitiveTopology( D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST ); context->IASetInputLayout( g_d3d.modelIL_.get() ); context->IASetVertexBuffers( 0, 1, &vb, &stride, &offset ); context->VSSetShader( g_d3d.modelVS_.get(), nullptr, 0 ); context->VSSetConstantBuffers( 0, 1, &cb ); context->RSSetState( g_d3d.rasterState_.get() ); context->PSSetShader( g_d3d.modelPS_.get(), nullptr, 0 ); context->RSSetViewports( 1, &viewport ); context->Draw( 18, 0 ); g_d3d.swapChain_->Present( 1, 0 ); }
//---------------------------------------------------------------------------------------------------- bool EEPoints2D::Render() { if (!EEObject::Render()) return false; MapObjectBuffer(); ID3D11DeviceContext *deviceContext = EECore::s_EECore->GetDeviceContext(); deviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_POINTLIST); deviceContext->IASetInputLayout(s_pointsIL); UINT stride = sizeof(EEPoints2DVertex); UINT offset = 0; deviceContext->IASetVertexBuffers(0, 1, &m_pointsVB, &stride, &offset); deviceContext->IASetIndexBuffer(NULL, DXGI_FORMAT_R32_UINT, 0); deviceContext->VSSetShader(s_pointsVS, NULL, 0); deviceContext->PSSetShader(s_pointsPS, NULL, 0); deviceContext->Draw(m_points.size(), 0); return true; }
void FontShader::renderShader(int i_indexCount) { ID3D11DeviceContext* deviceContext = GraphicsDX::GetDeviceContext(); // Set the vertex input layout. deviceContext->IASetInputLayout(_layout); // Set the vertex and pixel shaders that will be used to render the triangles. deviceContext->VSSetShader(_vertexShader, NULL, 0); deviceContext->HSSetShader(nullptr, NULL, 0); deviceContext->DSSetShader(nullptr, NULL, 0); deviceContext->GSSetShader(nullptr, NULL, 0); deviceContext->PSSetShader(_pixelShader, NULL, 0); // Set the sampler state in the pixel shader. deviceContext->PSSetSamplers(0, 1, &_sampleState); // Render the triangles. deviceContext->DrawIndexed(i_indexCount, 0, 0); }
//--------------------------------------------------------------------------- void ShadowMapGen::Begin(RenderContext* rc, RenderSurface* pSurface, const AABB& bounds ) { m_rc = rc; m_pSurface = pSurface; ID3D11DeviceContext* dc = m_rc->Context(); // set depth-stencil state to default. dc->OMSetDepthStencilState(NULL,0); ShadowMaps::Inst()->UpdateLightCamera(dc, LightingState::Inst()->ProminentDirLight(), bounds); ShadowMaps::Inst()->SetAndClear(dc); dc->RSSetState( m_rasterStateShadow ); const Camera& lightCam = ShadowMaps::Inst()->GetCamera(); // update per frame cb ConstantBufferShadowMapGenPerFrame constBuffer; Matrix::Transpose(lightCam.View() ,constBuffer.view); Matrix::Transpose(lightCam.Proj(),constBuffer.proj); UpdateConstantBuffer(dc,m_pConstantBufferPerFrame,&constBuffer,sizeof(constBuffer)); // set imput layout. dc->IASetInputLayout( m_pVertexLayoutMesh ); // set shaders dc->VSSetShader( m_shaderShadowMapsVS, NULL, 0 ); dc->PSSetShader( NULL, NULL, 0 ); dc->GSSetShader( NULL, NULL, 0 ); ID3D11Buffer* constantBuffers[] = { m_pConstantBufferPerFrame, m_pConstantBufferPerDraw }; // set const buffers for vertex shader. dc->VSSetConstantBuffers( 0, ARRAY_SIZE(constantBuffers), constantBuffers ); }
//---------------------------------------------------------------------------------------------------- bool EECylinder::Render() { if (!EEObject::Render()) return false; MapObjectBuffer(); ID3D11DeviceContext *deviceConstext = EECore::s_EECore->GetDeviceContext(); deviceConstext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); deviceConstext->IASetInputLayout(s_objectIL); UINT stride = sizeof(EECylinderVertex); UINT offset = 0; deviceConstext->IASetVertexBuffers(0, 1, &m_objectVB, &stride, &offset); deviceConstext->IASetIndexBuffer(m_objectIB, DXGI_FORMAT_R32_UINT, 0); deviceConstext->VSSetShader(s_objectVS, NULL, 0); ID3D11ShaderResourceView *texture = m_tex.GetTexture(); deviceConstext->PSSetShaderResources(0, 1, &texture); deviceConstext->PSSetShader(s_objectPS, NULL, 0); deviceConstext->DrawIndexed(m_indexCount, 0, 0); return true; }
void Model::Paint(const XMMATRIX &world, Camera *camera, XMFLOAT3 lightPos) { unsigned int stride = mSizeOfVertexDesc; unsigned int offset = 0; XMMATRIX view = camera->GetViewMatrix(); XMMATRIX proj = camera->GetProjectionMatrix(); MatrixBuffer buffer; buffer.world = XMMatrixTranspose(world); buffer.view = XMMatrixTranspose(view); buffer.proj = XMMatrixTranspose(proj); buffer.wvp = XMMatrixTranspose(world * view * proj); buffer.cameraPos = camera->GetPosition(); ID3D11DeviceContext *dc = GameEngine::GetInstance()->GetGraphicsManager()->GetGraphicsDeviceContext(); dc->IASetVertexBuffers(0, 1, &mVertexBuffer, &stride, &offset); dc->IASetInputLayout(mInputLayout); dc->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); dc->VSSetShader(mVertexShader, 0, 0); dc->PSSetShader(mFragmentShader, 0, 0); dc->PSSetShaderResources(0, 1, &mTextureView); dc->PSSetSamplers(0, 1, &mTextureSamplerState); dc->UpdateSubresource(mMatrixBuffer, 0, 0, &buffer, 0, 0); dc->VSSetConstantBuffers(0, 1, &mMatrixBuffer); dc->UpdateSubresource(mLightBuffer, 0, 0, &lightPos, 0, 0); dc->VSSetConstantBuffers(1, 1, &mLightBuffer); if (mAnimation) { mAnimation->SetAnimationProperties(dc); } dc->Draw(mNumberOfVertices, 0); }
//---------------------------------------------------------------------------------------------------- bool EECurve2D::Render() { if (!EEObject::Render()) return false; MapObjectBuffer(); ID3D11DeviceContext *deviceContext = EECore::s_EECore->GetDeviceContext(); deviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP); deviceContext->IASetInputLayout(s_curveIL); UINT stride = sizeof(EECurve2DVertex); UINT offset = 0; deviceContext->IASetVertexBuffers(0, 1, &m_curveVB, &stride, &offset); deviceContext->IASetIndexBuffer(NULL, DXGI_FORMAT_R32_UINT, 0); deviceContext->VSSetShader(s_curveVS, NULL, 0); ID3D11ShaderResourceView *texture = m_tex.GetTexture(); deviceContext->PSSetShaderResources(0, 1, &texture); deviceContext->PSSetShader(s_curvePS, NULL, 0); deviceContext->Draw(m_curve.size() << 1, 0); return true; }
gl::Error PixelTransfer11::copyBufferToTexture(const gl::PixelUnpackState &unpack, unsigned int offset, RenderTargetD3D *destRenderTarget, GLenum destinationFormat, GLenum sourcePixelsType, const gl::Box &destArea) { ANGLE_TRY(loadResources()); gl::Extents destSize = destRenderTarget->getExtents(); ASSERT(destArea.x >= 0 && destArea.x + destArea.width <= destSize.width && destArea.y >= 0 && destArea.y + destArea.height <= destSize.height && destArea.z >= 0 && destArea.z + destArea.depth <= destSize.depth ); const gl::Buffer &sourceBuffer = *unpack.pixelBuffer.get(); ASSERT(mRenderer->supportsFastCopyBufferToTexture(destinationFormat)); ID3D11PixelShader *pixelShader = findBufferToTexturePS(destinationFormat); ASSERT(pixelShader); // The SRV must be in the proper read format, which may be different from the destination format // EG: for half float data, we can load full precision floats with implicit conversion GLenum unsizedFormat = gl::GetInternalFormatInfo(destinationFormat).format; GLenum sourceFormat = gl::GetSizedInternalFormat(unsizedFormat, sourcePixelsType); const d3d11::Format &sourceFormatInfo = d3d11::Format::Get(sourceFormat, mRenderer->getRenderer11DeviceCaps()); DXGI_FORMAT srvFormat = sourceFormatInfo.srvFormat; ASSERT(srvFormat != DXGI_FORMAT_UNKNOWN); Buffer11 *bufferStorage11 = GetAs<Buffer11>(sourceBuffer.getImplementation()); ID3D11ShaderResourceView *bufferSRV = nullptr; ANGLE_TRY_RESULT(bufferStorage11->getSRV(srvFormat), bufferSRV); ASSERT(bufferSRV != nullptr); ID3D11RenderTargetView *textureRTV = GetAs<RenderTarget11>(destRenderTarget)->getRenderTargetView(); ASSERT(textureRTV != nullptr); CopyShaderParams shaderParams; setBufferToTextureCopyParams(destArea, destSize, sourceFormat, unpack, offset, &shaderParams); ID3D11DeviceContext *deviceContext = mRenderer->getDeviceContext(); ID3D11Buffer *nullBuffer = nullptr; UINT zero = 0; // Are we doing a 2D or 3D copy? ID3D11GeometryShader *geometryShader = ((destSize.depth > 1) ? mBufferToTextureGS : NULL); auto stateManager = mRenderer->getStateManager(); deviceContext->VSSetShader(mBufferToTextureVS, NULL, 0); deviceContext->GSSetShader(geometryShader, NULL, 0); deviceContext->PSSetShader(pixelShader, NULL, 0); stateManager->setShaderResource(gl::SAMPLER_PIXEL, 0, bufferSRV); deviceContext->IASetInputLayout(NULL); deviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_POINTLIST); deviceContext->IASetVertexBuffers(0, 1, &nullBuffer, &zero, &zero); deviceContext->OMSetBlendState(NULL, NULL, 0xFFFFFFF); deviceContext->OMSetDepthStencilState(mCopyDepthStencilState, 0xFFFFFFFF); deviceContext->RSSetState(mCopyRasterizerState); stateManager->setOneTimeRenderTarget(textureRTV, nullptr); if (!StructEquals(mParamsData, shaderParams)) { d3d11::SetBufferData(deviceContext, mParamsConstantBuffer, shaderParams); mParamsData = shaderParams; } deviceContext->VSSetConstantBuffers(0, 1, &mParamsConstantBuffer); // Set the viewport D3D11_VIEWPORT viewport; viewport.TopLeftX = 0; viewport.TopLeftY = 0; viewport.Width = static_cast<FLOAT>(destSize.width); viewport.Height = static_cast<FLOAT>(destSize.height); viewport.MinDepth = 0.0f; viewport.MaxDepth = 1.0f; deviceContext->RSSetViewports(1, &viewport); UINT numPixels = (destArea.width * destArea.height * destArea.depth); deviceContext->Draw(numPixels, 0); // Unbind textures and render targets and vertex buffer stateManager->setShaderResource(gl::SAMPLER_PIXEL, 0, NULL); deviceContext->VSSetConstantBuffers(0, 1, &nullBuffer); mRenderer->markAllStateDirty(); return gl::NoError(); }
// WinMain int APIENTRY _tWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow ) { HRESULT hr; // ウィンドウクラスを登録 WNDCLASSEX wcex = { sizeof( WNDCLASSEX ), // cbSize CS_HREDRAW | CS_VREDRAW, // style WndProc, // lpfnWndProc 0, // cbClsExtra 0, // cbWndExtra hInstance, // hInstance NULL, // hIcon NULL, // hCursor ( HBRUSH )( COLOR_WINDOW + 1 ), // hbrBackGround NULL, // lpszMenuName g_className, // lpszClassName NULL // hIconSm }; if ( ! RegisterClassEx( &wcex ) ) { MessageBox( NULL, _T( "失敗: RegisterClassEx()" ), _T( "エラー" ), MB_OK | MB_ICONERROR ); return 0; } dtprintf( _T( "RegisterClassEx: ok\n" ) ); // ウィンドウサイズを計算 RECT r = { 0, 0, 800, 450 }; // 800x450 (16:9) if ( ! AdjustWindowRect( &r, WS_OVERLAPPEDWINDOW, FALSE ) ) { MessageBox( NULL, _T( "失敗: AdjustWindowRect()" ), _T( "エラー" ), MB_OK | MB_ICONERROR ); return 0; } dtprintf( _T( "AdjustWindowRect: ok (%d, %d)-(%d, %d)\n" ), r.left, r.top, r.right, r.bottom ); // ウィンドウ生成 HWND hWnd; hWnd = CreateWindow( g_className, g_windowName, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, r.right - r.left, r.bottom - r.top, NULL, NULL, hInstance, NULL ); if ( hWnd == NULL ) { MessageBox( NULL, _T( "失敗: CreateWindow()" ), _T( "エラー" ), MB_OK | MB_ICONERROR ); return 0; } dtprintf( _T( "CreateWindow: ok\n" ) ); // ウィンドウ表示 ShowWindow(hWnd, nCmdShow); dtprintf( _T( "ShowWindow: ok\n" ) ); // スワップチェイン設定 DXGI_SWAP_CHAIN_DESC scDesc = { { 1280, // BufferDesc.Width 720, // BufferDesc.Height { 60, // BufferDesc.RefreshRate.Numerator 1 // BufferDesc.RefreshRate.Denominator }, DXGI_FORMAT_R16G16B16A16_FLOAT, // BufferDesc.Format DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED, // BufferDesc.ScanlineOrdering DXGI_MODE_SCALING_CENTERED // BufferDesc.Scaling }, { 1, // SampleDesc.Count 0 // SampleDesc.Quality }, DXGI_USAGE_RENDER_TARGET_OUTPUT, // BufferUsage 1, // BufferCount hWnd, // OutputWindow TRUE, // Windowed DXGI_SWAP_EFFECT_DISCARD, // SwapEffect DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH // Flags }; // Direct3D11 デバイス・デバイスコンテキスト・スワップチェーンを生成 ID3D11Device * pDevice = NULL; ID3D11DeviceContext * pDeviceContext = NULL; IDXGISwapChain * pSwapChain = NULL; D3D_FEATURE_LEVEL feature; hr = D3D11CreateDeviceAndSwapChain( NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, 0, NULL, 0, D3D11_SDK_VERSION, &scDesc, &pSwapChain, &pDevice, &feature, &pDeviceContext ); if ( FAILED( hr ) ) { MessageBox( NULL, _T( "失敗: D3D11CreateDeviceAndSwapChain()" ), _T( "エラー" ), MB_OK | MB_ICONERROR ); return 0; } dtprintf( _T( "D3D11CreateDeviceAndSwapChain: ok (pDevice: 0x%p, pDeviceContext: 0x%p, pSwapChain: 0x%p, feature: 0x%4x)\n" ), pDevice, pDeviceContext, pSwapChain, ( int ) feature ); // バックバッファテクスチャを取得 ID3D11Texture2D * pBackBuffer = NULL; hr = pSwapChain->GetBuffer( 0, __uuidof( pBackBuffer ), reinterpret_cast< void ** >( &pBackBuffer ) ); if ( FAILED( hr ) ) { MessageBox( NULL, _T( "失敗: IDXGISwapChain::GetBuffer()" ), _T( "エラー" ), MB_OK | MB_ICONERROR ); return 0; } dtprintf( _T( "IDXGISwapChain::GetBuffer: ok (pBackBuffer: 0x%p)\n" ), pBackBuffer ); // レンダーターゲットビューを生成 ID3D11RenderTargetView * pRenderTargetView = NULL; hr = pDevice->CreateRenderTargetView( pBackBuffer, NULL, &pRenderTargetView ); if ( FAILED( hr ) ) { MessageBox( NULL, _T( "失敗: ID3D11Device::CreateRenderTargetView()" ), _T( "エラー" ), MB_OK | MB_ICONERROR ); return 0; } dtprintf( _T( "ID3D11Device::CreateRenderTargetView: ok (pRenderTargetView: 0x%p)\n" ), pRenderTargetView ); // デプス・ステンシルバッファとなるテクスチャを生成 D3D11_TEXTURE2D_DESC depthStencilBufferDesc = { 1280, // Width 720, // Height 1, // MipLevels 1, // ArraySize DXGI_FORMAT_D32_FLOAT, // Format { 1, // SampleDesc.Count 0 // SampleDesc.Quality }, D3D11_USAGE_DEFAULT, // Usage D3D11_BIND_DEPTH_STENCIL, // BindFlags 0, // CPUAccessFlags 0 // MiscFlags }; ID3D11Texture2D * pDepthStencilBuffer = NULL; hr = pDevice->CreateTexture2D( &depthStencilBufferDesc, NULL, &pDepthStencilBuffer ); if ( FAILED( hr ) ) { MessageBox( NULL, _T( "失敗: ID3D11Device::CreateTexture2D()" ), _T( "エラー" ), MB_OK | MB_ICONERROR ); return 0; } dtprintf( _T( "ID3D11Device::CreateTexture2D: ok (pDepthStencilBuffer: 0x%p)\n" ), pDepthStencilBuffer ); // デプス・ステンシルビューを生成 ID3D11DepthStencilView * pDepthStencilView = NULL; hr = pDevice->CreateDepthStencilView( pDepthStencilBuffer, NULL, &pDepthStencilView ); if ( FAILED( hr ) ) { MessageBox( NULL, _T( "失敗: ID3D11Device::CreateDepthStencilView()" ), _T( "エラー" ), MB_OK | MB_ICONERROR ); return 0; } dtprintf( _T( "ID3D11Device::CreateDepthStencilView: ok (pDepthStencilView: 0x%p)\n" ), pDepthStencilView ); // レンダーターゲットビューとデプス・ステンシルビューをバインド ID3D11RenderTargetView * pRenderTargetViews[] = { pRenderTargetView }; pDeviceContext->OMSetRenderTargets( 1, pRenderTargetViews, pDepthStencilView ); dtprintf( _T( "ID3D11DeviceContext::OMSetRenderTargets: ok\n" ) ); // バックバッファはもうここでは使わない COM_SAFE_RELEASE( pBackBuffer ); // ビューポートをバインド D3D11_VIEWPORT viewport = { 0.0f, // TopLeftX 0.0f, // TopLeftY 1280.0f, // Width 720.0f, // Height 0.0f, // MinDepth 1.0f // MaxDepth }; pDeviceContext->RSSetViewports( 1, &viewport ); dtprintf( _T( "ID3D11DeviceContext::RSSetViewports: ok\n" ) ); // 頂点データ float vertices[ 8 ][ 7 ] = { // Xaxis Yaxis Zaxis 赤 緑 青 Alpha { -0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f }, // 手前左上 { 0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f }, // 手前右上 { 0.5f, -0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f }, // 手前右下 { -0.5f, -0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 1.0f }, // 手前左下 { -0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 1.0f }, // 奥左上 { 0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 1.0f, 1.0f }, // 奥右上 { 0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 0.0f, 1.0f }, // 奥右下 { -0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f } // 奥左下 }; // 頂点バッファを生成 D3D11_BUFFER_DESC vertexBufferDesc = { sizeof( vertices ), // ByteWidth D3D11_USAGE_DEFAULT, // Usage D3D11_BIND_VERTEX_BUFFER, // BindFlags 0, // CPUAccessFlags 0, // MiscFlags 0 // StructureByteStride }; D3D11_SUBRESOURCE_DATA vertexResourceData = { vertices }; ID3D11Buffer * pVertexBuffer = NULL; hr = pDevice->CreateBuffer( &vertexBufferDesc, &vertexResourceData, &pVertexBuffer ); if ( FAILED( hr ) ) { MessageBox( NULL, _T( "失敗: ID3D11Device::CreateBuffer()" ), _T( "エラー" ), MB_OK | MB_ICONERROR ); return 0; } dtprintf( _T( "ID3D11Device::CreateBuffer: ok (pVertexBuffer: 0x%p)\n" ), pVertexBuffer ); // 頂点バッファをバインド UINT strides[] = { sizeof( float ) * 7 }; UINT offsets[] = { 0 }; pDeviceContext->IASetVertexBuffers( 0, 1, &pVertexBuffer, strides, offsets ); dtprintf( _T( "ID3D11DeviceContext::IASetVertexBuffers: ok\n" ) ); // インデックスデータ unsigned int indices[] = { 0, 1, 2, 0, 2, 3, // 手前 4, 0, 3, 4, 3, 7, // 左 1, 5, 6, 1, 6, 2, // 右 0, 4, 5, 0, 5, 1, // 上 2, 6, 7, 2, 7, 3, // 下 5, 4, 7, 5, 7, 6 }; // 裏 // インデックスバッファを生成 D3D11_BUFFER_DESC indexBufferDesc = { sizeof( indices ), // ByteWidth D3D11_USAGE_DEFAULT, // Usage D3D11_BIND_INDEX_BUFFER, // BindFlags 0, // CPUAccessFlags 0, // MiscFlags 0 // StructureByteStride }; D3D11_SUBRESOURCE_DATA indexResourceData = { indices }; ID3D11Buffer * pIndexBuffer = NULL; hr = pDevice->CreateBuffer( &indexBufferDesc, &indexResourceData, &pIndexBuffer ); if ( FAILED( hr ) ) { MessageBox( NULL, _T( "失敗: ID3D11Device::CreateBuffer()" ), _T( "エラー" ), MB_OK | MB_ICONERROR ); return 0; } dtprintf( _T( "ID3D11Device::CreateBuffer: ok (pIndexBuffer: 0x%p)\n" ), pIndexBuffer ); // インデックスバッファをバインド pDeviceContext->IASetIndexBuffer( pIndexBuffer, DXGI_FORMAT_R32_UINT, 0 ); dtprintf( _T( "ID3D11DeviceContext::IASetIndexBuffer: ok\n" ) ); // プリミティブタイプを設定 pDeviceContext->IASetPrimitiveTopology( D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST ); dtprintf( _T( "ID3D11DeviceContext::IASetPrimitiveTopology: ok\n" ) ); // 頂点シェーダ用の定数バッファを作成 D3D11_BUFFER_DESC VSConstantBufferDesc = { sizeof( D3DXMATRIX ) * 3, // ByteWidth D3D11_USAGE_DYNAMIC, // Usage D3D11_BIND_CONSTANT_BUFFER, // BindFlags D3D11_CPU_ACCESS_WRITE, // CPUAccessFlags 0, // MiscFlags 0 // StructureByteStride }; ID3D11Buffer * pVSConstantBuffer = NULL; hr = pDevice->CreateBuffer( &VSConstantBufferDesc, NULL, &pVSConstantBuffer ); if ( FAILED( hr ) ) { MessageBox( NULL, _T( "失敗: ID3D11Device::CreateBuffer()" ), _T( "エラー" ), MB_OK | MB_ICONERROR ); return 0; } dtprintf( _T( "ID3D11Device::CreateBuffer: ok (pVSConstantBuffer: 0x%p)\n" ), pVSConstantBuffer ); // 定数バッファをバインド pDeviceContext->VSSetConstantBuffers( 0, 1, &pVSConstantBuffer ); dtprintf( _T( "ID3D11DeviceContext::VSSetConstantBuffers: ok\n" ) ); // 頂点シェーダを作成 ID3D11VertexShader * pVertexShader = NULL; hr = pDevice->CreateVertexShader( g_vs_perspective, sizeof( g_vs_perspective ), NULL, &pVertexShader ); if ( FAILED( hr ) ) { MessageBox( NULL, _T( "失敗: ID3D11Device::CreateVertexShader()" ), _T( "エラー" ), MB_OK | MB_ICONERROR ); return 0; } dtprintf( _T( "ID3D11Device::CreateVertexShader: ok (pVertexShader: 0x%p)\n" ), pVertexShader ); // ピクセルシェーダを作成 ID3D11PixelShader * pPixelShader = NULL; hr = pDevice->CreatePixelShader( g_ps_constant, sizeof( g_ps_constant ), NULL, &pPixelShader ); if ( FAILED( hr ) ) { MessageBox( NULL, _T( "失敗: ID3D11Device::CreatePixelShader()" ), _T( "エラー" ), MB_OK | MB_ICONERROR ); return 0; } dtprintf( _T( "ID3D11Device::CreatePixelShader: ok (pPixelShader: 0x%p)\n" ), pPixelShader ); // シェーダをバインド pDeviceContext->VSSetShader( pVertexShader, NULL, 0 ); dtprintf( _T( "ID3D11DeviceContext::VSSetShader: ok\n" ) ); pDeviceContext->PSSetShader( pPixelShader, NULL, 0 ); dtprintf( _T( "ID3D11DeviceContext::PSSetShader: ok\n" ) ); pDeviceContext->GSSetShader( NULL, NULL, 0 ); pDeviceContext->HSSetShader( NULL, NULL, 0 ); pDeviceContext->DSSetShader( NULL, NULL, 0 ); // 入力エレメント記述子 D3D11_INPUT_ELEMENT_DESC verticesDesc[] = { { "IN_POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }, { "IN_COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, sizeof(float)*3, D3D11_INPUT_PER_VERTEX_DATA, 0 } }; // 入力レイアウトを生成 ID3D11InputLayout * pInputLayout = NULL; hr = pDevice->CreateInputLayout( verticesDesc, 2, g_vs_perspective, sizeof( g_vs_perspective ), &pInputLayout ); if ( FAILED( hr ) ) { MessageBox( NULL, _T( "失敗: ID3D11Device::CreateInputLayout()" ), _T( "エラー" ), MB_OK | MB_ICONERROR ); return 0; } dtprintf( _T( "ID3D11Device::CreateInputLayout: ok (pInputLayout: 0x%p)\n" ), pInputLayout ); // 入力レイアウトをバインド pDeviceContext->IASetInputLayout( pInputLayout ); dtprintf( _T( "ID3D11DeviceContext::IASetInputLayout: ok\n" ) ); // ラスタライザステートを生成 D3D11_RASTERIZER_DESC rasterizerStateDesc = { D3D11_FILL_SOLID, // FillMode // D3D11_FILL_WIREFRAME, // FillMode (ワイヤーフレーム表示) D3D11_CULL_BACK, // CullMode // D3D11_CULL_NONE, // CullMode (カリングなし) FALSE, // FrontCounterClockwise 0, // DepthBias 0.0f, // DepthBiasClamp 0.0f, // SlopeScaledDepthBias TRUE, // DepthClipEnable FALSE, // ScissorEnable FALSE, // MultisampleEnable FALSE // AntialiasedLineEnable }; ID3D11RasterizerState * pRasterizerState = NULL; hr = pDevice->CreateRasterizerState( &rasterizerStateDesc, &pRasterizerState ); if ( FAILED( hr ) ) { MessageBox( NULL, _T( "失敗: ID3D11Device::CreateRasterizerState()" ), _T( "エラー" ), MB_OK | MB_ICONERROR ); return 0; } dtprintf( _T( "ID3D11Device::CreateRasterizerState: ok (pRasterizerState: 0x%p)\n" ), pRasterizerState ); // ラスタライザステートをバインド pDeviceContext->RSSetState( pRasterizerState ); dtprintf( _T( "ID3D11DeviceContext::RSSetState: ok\n" ) ); // デプス・ステンシルステートを生成 D3D11_DEPTH_STENCIL_DESC depthStencilStateDesc = { TRUE, // DepthEnable D3D11_DEPTH_WRITE_MASK_ALL, // DepthWriteMask D3D11_COMPARISON_LESS, // DepthFunc FALSE, // StencilEnable D3D11_DEFAULT_STENCIL_READ_MASK, // StencilReadMask D3D11_DEFAULT_STENCIL_WRITE_MASK, // StencilWriteMask { D3D11_STENCIL_OP_KEEP, // FrontFace.StencilFailOp D3D11_STENCIL_OP_KEEP, // FrontFace.StencilDepthFailOp D3D11_STENCIL_OP_KEEP, // FrontFace.StencilPassOp D3D11_COMPARISON_ALWAYS // FrontFace.StencilFunc }, { D3D11_STENCIL_OP_KEEP, // BackFace.StencilFailOp D3D11_STENCIL_OP_KEEP, // BackFace.StencilDepthFailOp D3D11_STENCIL_OP_KEEP, // BackFace.StencilPassOp D3D11_COMPARISON_ALWAYS // BackFace.StencilFunc } }; ID3D11DepthStencilState * pDepthStencilState = NULL; hr = pDevice->CreateDepthStencilState( &depthStencilStateDesc, &pDepthStencilState ); if ( FAILED( hr ) ) { MessageBox( NULL, _T( "失敗: ID3D11Device::CreateDepthStencilState()" ), _T( "エラー" ), MB_OK | MB_ICONERROR ); return 0; } dtprintf( _T( "ID3D11Device::CreateDepthStencilState: ok (pDepthStencilState: 0x%p)\n" ), pDepthStencilState ); // デプス・ステンシルステートをバインド pDeviceContext->OMSetDepthStencilState( pDepthStencilState, 0 ); dtprintf( _T( "ID3D11DeviceContext::OMSetDepthStencilState: ok\n" ) ); MSG msg; while ( 1 ) { // メッセージを取得 if ( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) ) { if ( msg.message == WM_QUIT ) { dtprintf( _T( "PeekMessage: WM_QUIT\n" ) ); break; } // メッセージ処理 DispatchMessage( &msg ); } else { HRESULT hr; static unsigned int count = 0; float theta = ( count++ / 200.0f ) * ( 3.141593f / 2.0f ); // World-View-Projection 行列をそれぞれ生成 D3DXMATRIX world, view, projection; D3DXMatrixIdentity( &world ); const D3DXVECTOR3 eye( 1.8f * 1.414214f * -cosf( theta ), 1.8f, 1.8f * 1.414214f * sinf( theta ) ); const D3DXVECTOR3 at( 0.0f, 0.0f, 0.0f ); const D3DXVECTOR3 up( 0.0f, 1.0f, 0.0f ); D3DXMatrixLookAtRH( &view, &eye, &at, &up ); D3DXMatrixPerspectiveFovRH( &projection, 3.141593f / 4.0f, 1280.0f / 720.0f, 1.0f, 10000.0f ); // 頂点シェーダ用定数バッファへアクセス D3D11_MAPPED_SUBRESOURCE mapped; hr = pDeviceContext->Map( pVSConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped ); if ( SUCCEEDED( hr ) ) { D3DXMATRIX * mapped_m = static_cast< D3DXMATRIX * >( mapped.pData ); mapped_m[0] = world; mapped_m[1] = view; mapped_m[2] = projection; // 後始末 pDeviceContext->Unmap( pVSConstantBuffer, 0 ); } // レンダーターゲットビューをクリア const float clear[ 4 ] = { 0.0f, 0.0f, 0.3f, 1.0f }; // RGBA pDeviceContext->ClearRenderTargetView( pRenderTargetView, clear ); // デプス・ステンシルビューをクリア pDeviceContext->ClearDepthStencilView( pDepthStencilView, D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0f, 0 ); // 描画 pDeviceContext->DrawIndexed( 36, 0, 0 ); pSwapChain->Present( 1, 0 ); // ちょっとだけ待つ Sleep( 5 ); } } // シェーダをアンバインド pDeviceContext->VSSetShader( NULL, NULL, 0 ); pDeviceContext->PSSetShader( NULL, NULL, 0 ); // デバイス・リソース解放 COM_SAFE_RELEASE( pDepthStencilState ); COM_SAFE_RELEASE( pRasterizerState ); COM_SAFE_RELEASE( pInputLayout ); COM_SAFE_RELEASE( pPixelShader ); COM_SAFE_RELEASE( pVertexShader ); COM_SAFE_RELEASE( pVSConstantBuffer ); COM_SAFE_RELEASE( pIndexBuffer ); COM_SAFE_RELEASE( pVertexBuffer ); COM_SAFE_RELEASE( pDepthStencilView ); COM_SAFE_RELEASE( pDepthStencilBuffer ); COM_SAFE_RELEASE( pRenderTargetView ); COM_SAFE_RELEASE( pSwapChain ); COM_SAFE_RELEASE( pDeviceContext ); COM_SAFE_RELEASE( pDevice ); return msg.wParam; }