// 2D描画開始 IZ_BOOL CGraphicsDevice::Begin2D() { IZ_ASSERT(m_2DRenderer != IZ_NULL); // 描画開始 IZ_BOOL ret = m_2DRenderer->BeginDraw(); if (ret) { // 2D描画用レンダーステート設定 SaveRenderState(); SetRenderState(E_GRAPH_RS_ZWRITEENABLE, IZ_FALSE); SetRenderState(E_GRAPH_RS_ZENABLE, IZ_FALSE); SetRenderState(E_GRAPH_RS_ALPHATESTENABLE, IZ_FALSE); SetRenderState(E_GRAPH_RS_ALPHABLENDENABLE, IZ_TRUE); SetRenderState(E_GRAPH_RS_BLENDMETHOD, E_GRAPH_ALPHA_BLEND_NORMAL); SetRenderState(E_GRAPH_RS_FILLMODE, E_GRAPH_FILL_MODE_SOLID); SetRenderState(E_GRAPH_RS_CULLMODE, E_GRAPH_CULL_DEFAULT); SetShaderProgram(IZ_NULL); m_Flags.is_render_2d = IZ_TRUE; } return ret; }
//Computes deferrred rendering, call befor "DrawTransparent" void DirectXGraphicEngine::ComputeDeferred() { if (!m_IsDrawing) { Logger::Log("Rendering call ComputeDeferred was called before begin was called", "DirectXRenderSystem", LoggerType::MSG_ERROR); return; } SetShaderProgram(m_DeferredComputeShader); //remove render gbuffers from render target to bind it to resource ID3D11RenderTargetView* t_DeleteRenderTargets[3] = {nullptr, nullptr, nullptr}; m_DeviceContext->OMSetRenderTargets(3, t_DeleteRenderTargets, m_DepthStencilView); //set backbuffer as unordered access m_DeviceContext->CSSetUnorderedAccessViews(0, 1, &m_UnorderedAccessView, nullptr); //set gbuffers as shader resource m_DeviceContext->CSSetShaderResources(1, 3, m_GBufferSRV); //check how many threadgroups we need UINT x = UINT(ceil((FLOAT)m_Width / (FLOAT)m_DeferredThreadGSize)); UINT y = UINT(ceil((FLOAT)m_Height / (FLOAT)m_DeferredThreadGSize)); //dispatch threads m_DeviceContext->Dispatch(x, y, 1); //set the gbuffers back as shader resources ID3D11ShaderResourceView* t_DeleteShaderResource[3] = { nullptr, nullptr, nullptr }; m_DeviceContext->CSSetShaderResources(1, 3, t_DeleteShaderResource); m_DeviceContext->OMSetRenderTargets(3, m_GBufferRTV, m_DepthStencilView); }
void RendererCommon::ApplyMaterial(const Material& Material, Mesh* const pMesh, const View& View) { SetShaderProgram(Material.GetShaderProgram()); ApplyRenderState(Material.GetRenderState()); const uint NumSamplers = Material.GetNumSamplers(); for (uint SamplerIndex = 0; SamplerIndex < NumSamplers; ++SamplerIndex) { ApplySamplerState(SamplerIndex, Material.GetSamplerState(SamplerIndex)); } DEBUGASSERT(Material.GetSDP()); Material.GetSDP()->SetShaderParameters(this, pMesh, View); }
//Call "BeginDraw()" befor, draws nontransparent, call "ComputeDeferred()" after TODO:: test to wrap the update of matrix buffer around the creation, so we dont copy memory 2 times void DirectXGraphicEngine::DrawOpaque(std::vector<RenderObject*> *p_RenderObects) { if (!m_IsDrawing) { Logger::Log("Rendering call DrawOpaque was called before begin was called", "DirectXRenderSystem", LoggerType::MSG_ERROR); return; } //set shaders SetShaderProgram(m_OpaqueShaders); //get our transform DirectXMatrixLibrary* t_MatrixLib = (DirectXMatrixLibrary*)t_MatrixLib->GetInstance(); //check if any objects UINT t_Size = p_RenderObects->size(); if (t_Size == 0) { Logger::Log("No opaque objects exist in list to draw", "DirectXRenderSystem", LoggerType::MSG_INFO); return; } //keep track how many new matrices we have UINT t_NumOfMatrices = 0; //set first mesh, current renderobject UINT t_CurMeshID = p_RenderObects->at(0)->meshHandle; UINT t_CurMatID = p_RenderObects->at(0)->materialHandle; UINT t_CurStart = p_RenderObects->at(0)->startIndex; UINT t_CurIndexAmount = p_RenderObects->at(0)->IndexAmount; //the new renderobject UINT t_NewMeshID; UINT t_NewMatID; UINT t_NewStart; UINT t_NewIndexAmount; SetMesh(&t_CurMeshID); SetMaterial(&t_CurMatID); for (UINT i = 0; i < t_Size; i++) { //check if same mesh t_NewMeshID = p_RenderObects->at(i)->meshHandle; t_NewMatID = p_RenderObects->at(i)->materialHandle; t_NewStart = p_RenderObects->at(i)->startIndex; t_NewIndexAmount = p_RenderObects->at(i)->IndexAmount; if (t_CurMeshID == t_NewMeshID) { if (t_CurMatID == t_NewMatID) { if (t_CurStart == t_NewStart && t_CurIndexAmount == t_NewIndexAmount) { //everything is same, except the world matrix if (t_NumOfMatrices < m_MaxNumOfInstances) { //copy memory from matrix in the library to our list memcpy(&m_MatriceList[t_NumOfMatrices], t_MatrixLib->GetMatrix(p_RenderObects->at(i)->matrixHandle), sizeof(XMFLOAT4X4)); //t_Matrices[t_NumOfMatrices] = p_RenderObects[i]. t_NumOfMatrices++; continue; } else { //TODO:: increase the size of the buffer oh no... Logger::Log("OBS! instance list to small! Not sure what consequences this causes OBS!", "DirectXRenderSystem", LoggerType::MSG_ERROR); } } } } //draw here D3D11_MAPPED_SUBRESOURCE t_MappedData; ZeroMemory(&t_MappedData, sizeof(D3D11_MAPPED_SUBRESOURCE)); //get pointer to graphic memory HRESULT hr = m_DeviceContext->Map(m_InstanceBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &t_MappedData); XMMATRIX* dataView = reinterpret_cast<XMMATRIX*>(t_MappedData.pData); //copy memory to the pointer memcpy(&dataView[0], &m_MatriceList[0], t_NumOfMatrices* sizeof(XMFLOAT4X4)); //save it m_DeviceContext->Unmap(m_InstanceBuffer, 0); //draw instances m_DeviceContext->DrawIndexedInstanced(t_CurIndexAmount, t_NumOfMatrices, t_CurStart, 0, 0); //set stuff if (t_CurMeshID == t_NewMeshID) { //change if new mesh SetMesh(&t_CurMeshID); } if (t_CurMatID == t_NewMatID) { //change if new material SetMaterial(&t_CurMatID); } t_CurMeshID = t_NewMeshID; t_CurMatID = t_NewMatID; t_CurStart = t_NewStart; t_CurIndexAmount = t_NewIndexAmount; memcpy( &m_MatriceList[t_NumOfMatrices], t_MatrixLib->GetMatrix( p_RenderObects->at( i )->matrixHandle ), sizeof( XMFLOAT4X4 ) ); t_NumOfMatrices = 1; } //draw last D3D11_MAPPED_SUBRESOURCE t_MappedData; ZeroMemory(&t_MappedData, sizeof(D3D11_MAPPED_SUBRESOURCE)); //get pointer to graphic memory HRESULT hr = m_DeviceContext->Map(m_InstanceBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &t_MappedData); XMMATRIX* dataView = reinterpret_cast<XMMATRIX*>(t_MappedData.pData); //copy memory to the pointer memcpy(&dataView[0], &m_MatriceList[0], t_NumOfMatrices* sizeof(XMFLOAT4X4)); //save it m_DeviceContext->Unmap(m_InstanceBuffer, 0); //draw instances m_DeviceContext->DrawIndexedInstanced( t_NewIndexAmount, t_NumOfMatrices, 0, 0, 0 ); }