void FBXObject::SetupDrawConstantBuffer() { static std::vector<XMFLOAT4X4> mBoneTransforms(128); for(std::size_t i = 0; i < this->AnimController.Mesh.second->mNumBones; ++i) { XMMATRIX invBind = XMLoadFloat4x4(&this->AnimController.Mesh.second->mSkeleton[i].invBindPose); XMMATRIX currPose = XMLoadFloat4x4(&this->AnimController.CurrentGlobalPose[i]); XMMATRIX total = invBind * currPose; XMMATRIX invTotal = XMMatrixTranspose( total); XMFLOAT4X4 inv; XMStoreFloat4x4(&inv, invTotal); mBoneTransforms[i] = inv; } cBuffer::cbChangeEveryFrame cb; XMFLOAT4X4 world = this->object.CalculateMatrix(); cb.mWorld = XMLoadFloat4x4(&world); cb.colour.diffuse = this->object.Colour.Diffuse; cb.colour.ambient = this->object.Colour.Ambient; cb.colour.spec = this->object.Colour.Spec; ID3D11DeviceContext* pImmediateContext = DX11App::getInstance()->direct3d.pImmediateContext; pImmediateContext->UpdateSubresource( this->pCBChangesEveryFrame.second, 0, NULL, &cb, 0, 0 ); pImmediateContext->VSSetConstantBuffers( 2, 1, &(this->pCBChangesEveryFrame.second) ); pImmediateContext->PSSetConstantBuffers( 2, 1, &(this->pCBChangesEveryFrame.second) ); pImmediateContext->UpdateSubresource( this->pAnimBonesBuffer.second, 0, NULL, &(mBoneTransforms.front()), 0, 0 ); pImmediateContext->VSSetConstantBuffers( 3, 1, &(this->pAnimBonesBuffer.second) ); }
void TgcDX11Effect::setConstantBuffer(string name, const void* bufferData) { ID3D11DeviceContext* deviceContext = ((TgcDX11Renderer*)GuiController::Instance->renderer)->deviceContext; TgcEffectValues::ConstantBuffer cBuffer = this->constantBuffers[name]; ID3D11Buffer* dxCbuffer = this->dxConstantBuffers[name]; //Map D3D11_MAPPED_SUBRESOURCE mappedResource; HRESULT result = deviceContext->Map(dxCbuffer, cBuffer.slot, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource); //Copy values memcpy(mappedResource.pData, bufferData, cBuffer.size); //Unmap deviceContext->Unmap(dxCbuffer, cBuffer.slot); //Set buffer in VS o PS if(cBuffer.shaderType == TgcEffectValues::VS) { deviceContext->VSSetConstantBuffers(cBuffer.slot, 1, &dxCbuffer); } else if(cBuffer.shaderType == TgcEffectValues::PS) { deviceContext->PSSetConstantBuffers(cBuffer.slot, 1, &dxCbuffer); } else if(cBuffer.shaderType == TgcEffectValues::VS_AND_PS) { deviceContext->VSSetConstantBuffers(cBuffer.slot, 1, &dxCbuffer); deviceContext->PSSetConstantBuffers(cBuffer.slot, 1, &dxCbuffer); } }
HRESULT DeferredPipeline::Lighting::set_ps_spot_light_buffer(ID3D11DeviceContext & device_context, Lighting_PS_Spot_Light_Buffer & ps_light_buffer) { HRESULT hr; D3D11_MAPPED_SUBRESOURCE resource; Lighting_PS_Spot_Light_Buffer* mappedBuffer; hr = device_context.Map(_ps_spot_light_buffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &resource); if (FAILED(hr)) { return hr; } mappedBuffer = (Lighting_PS_Spot_Light_Buffer*)resource.pData; mappedBuffer->ambient = ps_light_buffer.ambient; mappedBuffer->diffuse = ps_light_buffer.diffuse; mappedBuffer->specular = ps_light_buffer.specular; mappedBuffer->specular_power = ps_light_buffer.specular_power; mappedBuffer->light_direction = ps_light_buffer.light_direction; mappedBuffer->attenuation = ps_light_buffer.attenuation; mappedBuffer->cutoff = ps_light_buffer.cutoff; mappedBuffer->position = ps_light_buffer.position; device_context.Unmap(_ps_spot_light_buffer, 0); device_context.PSSetConstantBuffers(1, 1, &_ps_spot_light_buffer); return S_OK; }
/** Sets a buffer of values on the current shader instance. */ void Shader::SetConstantBuffer(u32 index, std::shared_ptr<ConstantBufferBase> buffer) { ID3D11Buffer* bufferD3D = buffer != nullptr ? buffer->GetBuffer() : nullptr; ID3D11DeviceContext* context = GetParent()->GetDeviceContext(); switch(GetType()) { case ShaderType::Compute: context->CSSetConstantBuffers(index, 1, &bufferD3D); break; case ShaderType::Domain: context->DSSetConstantBuffers(index, 1, &bufferD3D); break; case ShaderType::Geometry: context->GSSetConstantBuffers(index, 1, &bufferD3D); break; case ShaderType::Hull: context->HSSetConstantBuffers(index, 1, &bufferD3D); break; case ShaderType::Pixel: context->PSSetConstantBuffers(index, 1, &bufferD3D); break; case ShaderType::Vertex: context->VSSetConstantBuffers(index, 1, &bufferD3D); break; } }
void CGUIShaderDX::ApplyStateBlock(void) { if (!m_bCreated) return; ID3D11DeviceContext* pContext = g_Windowing.Get3D11Context(); m_vertexShader.BindShader(); pContext->VSSetConstantBuffers(0, 1, &m_pWVPBuffer); m_pixelShader[m_currentShader].BindShader(); pContext->PSSetConstantBuffers(0, 1, &m_pWVPBuffer); pContext->PSSetConstantBuffers(1, 1, &m_pVPBuffer); ID3D11SamplerState* samplers[] = { m_pSampLinear, m_pSampPoint }; pContext->PSSetSamplers(0, ARRAYSIZE(samplers), samplers); RestoreBuffers(); }
//------------------------------------------------------------------------------------ void Water::_RenderWaterDepth() { ID3D11DeviceContext* pContext = m_pRenderSystem->GetDeviceContext(); pContext->UpdateSubresource( m_pCB_Depth, 0, NULL, &m_constantBufDepth, 0, 0 ); pContext->VSSetConstantBuffers( 1, 1, &m_pCB_Depth ); pContext->PSSetConstantBuffers( 1, 1, &m_pCB_Depth ); m_pRT_Depth->Update(m_pWaterDepthMaterial); }
//------------------------------------------------------------------------------------ void Water::_FinalCompose() { ID3D11DeviceContext* pContext = m_pRenderSystem->GetDeviceContext(); pContext->UpdateSubresource( m_pCB_VS, 0, NULL, &m_constantBufVS, 0, 0 ); pContext->VSSetConstantBuffers( 1, 1, &m_pCB_VS ); pContext->UpdateSubresource( m_pCB_PS, 0, NULL, &m_constantBufPS, 0, 0 ); pContext->PSSetConstantBuffers( 2, 1, &m_pCB_PS ); m_waterMesh->GetSubMesh(0)->SetMaterial(m_pFinalComposeMaterial); m_pEntity->Render(); }
void PipelineStateCache::setPixelShader(const PixelShader& pixelShader) { // FIXME: cache this ID3D11Buffer* const* constantBuffers; uint32 numConstantBuffers = pixelShader.queryBuffersArray(constantBuffers); m_deviceContext->PSSetConstantBuffers(0, numConstantBuffers, constantBuffers); ID3D11PixelShader* shaderResourcePtr = pixelShader.getResourcePtr(); if (shaderResourcePtr != m_currentPixelShader) { m_currentPixelShader = shaderResourcePtr; m_deviceContext->PSSetShader(shaderResourcePtr, NULL, 0); } }
//-------------------------------------------------------------------------------------- // Name: CTiledResourceDevice::UpdatePSConstants // Desc: Copies the pixel shader shadow constants into the constant buffers and sets them // into the D3D device context. //-------------------------------------------------------------------------------------- VOID CTiledResourceDevice::UpdatePSConstants() { // Update the tiled resource constants: HRESULT hr = UpdateDynamicConstantBuffer( m_pd3dDeviceContext, m_pPSTiledResourceCB, m_PSTiledResourceConstants, sizeof(m_PSTiledResourceConstants) ); ASSERT( SUCCEEDED(hr) ); // Update the page pool constants: hr = UpdateDynamicConstantBuffer( m_pd3dDeviceContext, m_pPSPagePoolCB, m_PSPagePoolConstants, sizeof(m_PSPagePoolConstants) ); ASSERT( SUCCEEDED(hr) ); // Set the constant buffers to slots 12 and 13 on the D3D device context: ID3D11Buffer* pBuffers[] = { m_pPSTiledResourceCB, m_pPSPagePoolCB }; m_pd3dDeviceContext->PSSetConstantBuffers( 12, 2, pBuffers ); }
void BezierSceneObject::renderTheScene(GraphicsContext & p_in) { auto * invoker = m_quadInvoker; auto * Model = m_model; updateIncrement += p_in.deltaTime * 0.01f * m_model->TweakVariables["timeMod"]; ID3D11DeviceContext * tempDevice = p_in.GetD3DContext()->GetDeviceContext(); XMMATRIX tempMat; Model->ModelMatrix(tempMat); Model->UpdateBuffers(p_in.c_d3d->GetDeviceContext(), updateBezierPoints(updateIncrement, m_model->TweakVariables["AmpIncrement"])); XMMATRIX orbitMatrix = XMMatrixTranslation(10.0f , 0.0f, 0.0f) * XMMatrixRotationY(updateIncrement * 0.1f); // Release the vertex array as it is no longer needed. delete [] vertices; vertices = 0; QuadInvoker::MatrixBufferType *tempMatBuff = invoker->MapCBuffer<QuadInvoker::MatrixBufferType>(tempDevice , "matrixBuffer"); tempMatBuff->worldMatrix = XMMatrixTranspose(tempMat* orbitMatrix); tempMatBuff->viewMatrix = XMMatrixTranspose( *p_in.c_view); tempMatBuff->projectionMatrix = XMMatrixTranspose(*p_in.c_projection); invoker->UnMapCBuffer(tempDevice , "matrixBuffer"); tempDevice->VSSetConstantBuffers(0, 1, &invoker->GetBufferMap()["matrixBuffer"]); tempDevice->DSSetConstantBuffers(0, 1, &invoker->GetBufferMap()["matrixBuffer"]); QuadInvoker::TessellationBufferType *tempTessBuff = invoker->MapCBuffer<QuadInvoker::TessellationBufferType>(tempDevice , "tessBuffer"); tempTessBuff->cameraPosition = p_in.c_cam->GetPos(); tempTessBuff->misc = XMFLOAT3(); tempTessBuff->tessellationAmount = m_model->TweakVariables["TessFactor"]; tempTessBuff->LODModifier = 0; invoker->UnMapCBuffer(tempDevice , "tessBuffer"); tempDevice->HSSetConstantBuffers(0, 1, &invoker->GetBufferMap()["tessBuffer"]); QuadInvoker::LightBufferType *tempLightBuff = invoker->MapCBuffer<QuadInvoker::LightBufferType>(tempDevice, "LightBuffer"); tempLightBuff->ambientColor = p_in.c_lightInfo->GetAmbientColor(); tempLightBuff->diffuseColor = p_in.c_lightInfo->GetDiffuseColor(); tempLightBuff->lightDirection = p_in.c_lightInfo->Getdirection(); tempLightBuff->specularPower = p_in.c_lightInfo->GetSpecularPower(); invoker->UnMapCBuffer(tempDevice, "LightBuffer"); tempDevice->PSSetConstantBuffers(0, 1, &invoker->GetBufferMap()["LightBuffer"]); Model->RenderAllBuffers<ModelLightingClass::LightingVertexType> (tempDevice, D3D11_PRIMITIVE_TOPOLOGY_16_CONTROL_POINT_PATCHLIST); invoker->RenderShader(tempDevice , Model->GetIndexCount()); }
void DirectionalLightShader::Setup(DirectionalLight * light) { ID3D11DeviceContext* context = gCore.GetContext(); D3D11_MAPPED_SUBRESOURCE mr; if (SUCCEEDED(context->Map(mCB, 0, D3D11_MAP_WRITE_DISCARD, 0, &mr))) { CBLight* dst = (CBLight*)mr.pData; Color2Vector3(light->GetColor(), dst->color); dst->dir = light->GetTransform().position; dst->att = light->GetIntensity(); light->GetVP(dst->vp); context->Unmap(mCB, 0); } context->PSSetConstantBuffers(1, 1, &mCB); context->PSSetShader(mPS, nullptr, 0); }
//================================================================================================================================== void HDR::UpdateBloomConstants(float middleGrey, float bloomThreshold, float bloomMultiplier) { ID3D11DeviceContext* context = mD3DSystem->GetDeviceContext(); cbBloomConstants cBC; cBC.g_MiddleGrey = middleGrey; cBC.g_BloomThreshold = bloomThreshold; cBC.g_BloomMultiplier = bloomMultiplier; cBC.padding = 0; D3D11_MAPPED_SUBRESOURCE mapped_res; context->Map(m_pBloomConstants, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped_res); { assert(mapped_res.pData); *(cbBloomConstants*)mapped_res.pData = cBC; } context->Unmap(m_pBloomConstants, 0); context->CSSetConstantBuffers(2, 1, &m_pBloomConstants); context->PSSetConstantBuffers(2, 1, &m_pBloomConstants); }
void SkeletonModelFragmentShader::setParameters( ID3D11DeviceContext& deviceContext, const Texture2DSpecBind< TexBind::ShaderResource, unsigned char >& alphaTexture, const Texture2DSpecBind< TexBind::ShaderResource, uchar4 >& emissiveTexture, const Texture2DSpecBind< TexBind::ShaderResource, uchar4 >& albedoTexture, const Texture2DSpecBind< TexBind::ShaderResource, uchar4 >& normalTexture, const Texture2DSpecBind< TexBind::ShaderResource, unsigned char >& metalnessTexture, const Texture2DSpecBind< TexBind::ShaderResource, unsigned char >& roughnessTexture, const Texture2DSpecBind< TexBind::ShaderResource, unsigned char >& indexOfRefractionTexture, const float4& extraEmissive ) { const int resourceCount = 7; ID3D11ShaderResourceView* textureResource[ resourceCount ] = { alphaTexture.getShaderResourceView(), emissiveTexture.getShaderResourceView(), albedoTexture.getShaderResourceView(), normalTexture.getShaderResourceView(), metalnessTexture.getShaderResourceView(), roughnessTexture.getShaderResourceView(), indexOfRefractionTexture.getShaderResourceView() }; deviceContext.PSSetShaderResources( 0, resourceCount, textureResource ); deviceContext.PSSetSamplers( 0, 1, m_samplerState.GetAddressOf() ); D3D11_MAPPED_SUBRESOURCE mappedResource; ConstantBuffer* dataPtr; HRESULT result = deviceContext.Map( m_constantInputBuffer.Get(), 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource ); if ( result < 0 ) throw std::exception( "BlockModelVertexShader::setParameters - mapping constant buffer to CPU memory failed" ); dataPtr = (ConstantBuffer*)mappedResource.pData; dataPtr->extraEmissive = extraEmissive; deviceContext.Unmap( m_constantInputBuffer.Get(), 0 ); deviceContext.PSSetConstantBuffers( 0, 1, m_constantInputBuffer.GetAddressOf() ); }
HRESULT DeferredPipeline::Lighting::set_ps_constant_buffer(ID3D11DeviceContext & device_context, Lighting_PS_Constant_Buffer & ps_constant_buffer) { HRESULT hr; D3D11_MAPPED_SUBRESOURCE resource; Lighting_PS_Constant_Buffer* mappedBuffer; hr = device_context.Map(_ps_constant_buffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &resource); if (FAILED(hr)) { return hr; } mappedBuffer = (Lighting_PS_Constant_Buffer*)resource.pData; mappedBuffer->view = ps_constant_buffer.view; mappedBuffer->inverse_projection = ps_constant_buffer.inverse_projection; mappedBuffer->camera_position = ps_constant_buffer.camera_position; device_context.Unmap(_ps_constant_buffer, 0); device_context.PSSetConstantBuffers(0, 1, &_ps_constant_buffer); return S_OK; }
void BindableProgram::Bind (ID3D11DeviceContext& context, ShaderLibrary& library, const SourceConstantBufferPtr src_buffers [DEVICE_CONSTANT_BUFFER_SLOTS_COUNT], const InputLayout& input_layout, BindableProgramContext& program_context) { try { //поиск входного лэйаута if (!program_context.input_layout) { program_context.input_layout = program.GetInputLayout (library, input_layout); context.IASetInputLayout (program_context.input_layout.get ()); } //биндинг программы if (!program_context.program_binded) { program.Bind (context); program_context.program_binded = true; } //поиск и биндинг буферов //TODO: bindable buffers cache for dirty switch optimization // if (program_context.has_dirty_buffers) { ID3D11Buffer* buffers [ShaderType_Num][DEVICE_CONSTANT_BUFFER_SLOTS_COUNT]; memset (buffers, 0, sizeof (buffers)); for (BufferPrototypeArray::iterator iter=buffer_prototypes.begin (), end=buffer_prototypes.end (); iter!=end; ++iter) { TargetConstantBufferPrototype& prototype = **iter; const unsigned char* index = prototype.GetSourceBuffersIndices (); bool dirty = false; for (size_t i=0, count=prototype.GetSourceBuffersCount (); i<count; i++, index++) if (program_context.dirty_buffers [*index]) { dirty = true; break; } // if (!dirty) // continue; TargetConstantBuffer& buffer = prototype.GetBuffer (src_buffers, library); buffer.Bind (context, buffers); } //установка контекста context.CSSetConstantBuffers (0, DEVICE_CONSTANT_BUFFER_SLOTS_COUNT, buffers [ShaderType_Compute]); context.DSSetConstantBuffers (0, DEVICE_CONSTANT_BUFFER_SLOTS_COUNT, buffers [ShaderType_Domain]); context.GSSetConstantBuffers (0, DEVICE_CONSTANT_BUFFER_SLOTS_COUNT, buffers [ShaderType_Geometry]); context.HSSetConstantBuffers (0, DEVICE_CONSTANT_BUFFER_SLOTS_COUNT, buffers [ShaderType_Hull]); context.PSSetConstantBuffers (0, DEVICE_CONSTANT_BUFFER_SLOTS_COUNT, buffers [ShaderType_Pixel]); context.VSSetConstantBuffers (0, DEVICE_CONSTANT_BUFFER_SLOTS_COUNT, buffers [ShaderType_Vertex]); program_context.has_dirty_buffers = false; memset (program_context.dirty_buffers, 0, sizeof (program_context.dirty_buffers)); } } catch (xtl::exception& e) { e.touch ("render::low_level::dx11::BindableProgram::Bind"); throw; } }
void BaseShader::PreRender(void) { ID3D11DeviceContext* deviceContext = Application::GetInstance().GetGraphicsDevice()->GetDXSystem()->GetDeviceContext(); HRESULT result; D3D11_MAPPED_SUBRESOURCE mappedResource; D3DXMATRIX world; D3DXMATRIX proj; D3DXMATRIX view; unsigned int pixelBufferNum = 0; unsigned int vertexBufferNum = 0; Application::GetInstance().GetGraphicsDevice()->GetDXSystem()->GetWorldMatrix(world); Application::GetInstance().GetGraphicsDevice()->GetDXSystem()->GetProjectionMatrix(proj); Application::GetInstance().GetGraphicsDevice()->m_debugCamera->GetViewMatrix(view); D3DXMatrixTranspose(&world, &world); D3DXMatrixTranspose(&view, &view); D3DXMatrixTranspose(&proj, &proj); for (auto it = m_buffers.begin(); it != m_buffers.end(); it++) { eBufferType buffType = (*it).first; if (buffType > eBufferType_Vertex && buffType < eBufferType_Pixel) { result = deviceContext->Map((*it).second, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource); if (FAILED(result)) { LogManager::GetInstance().Error("Shader::Render-> could not lock a buffer"); return; } //Specific switch (buffType) { case eBufferType_Vertex_Matrix: { MatrixBufferType* dataPtr = (MatrixBufferType*)mappedResource.pData; dataPtr->world = world; dataPtr->projection = proj; dataPtr->view = view; } break; case eBufferType_Vertex_Camera: { CameraBufferType* dataPtr = (CameraBufferType*)mappedResource.pData; dataPtr->cameraPosition = Application::GetInstance().GetGraphicsDevice()->m_debugCamera->GetPosition(); dataPtr->padding = 0.0f; } break; case eBufferType_Vertex_Light: { LightBufferType* dataPtr = (LightBufferType*)mappedResource.pData; dataPtr->ambientColour = D3DXVECTOR4(0.15f, 0.15f, 0.15f, 1.0f); dataPtr->diffuseColour = D3DXVECTOR4(1.0f, 1.0f, 1.0f, 1.0f); dataPtr->specularColour = D3DXVECTOR4(1.0f, 1.0f, 1.0f, 1.0f); dataPtr->direction = D3DXVECTOR3(0.0f, -1.0f, 0.0f); dataPtr->specularPower = 1024.0f; } break; default: assert(false && "It's pretty impossible to get here, re-evaluate your life"); break; } deviceContext->Unmap((*it).second, 0); deviceContext->VSSetConstantBuffers(vertexBufferNum, 1, &(*it).second); vertexBufferNum++; } else if (buffType > eBufferType_Pixel) { result = deviceContext->Map((*it).second, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource); if (FAILED(result)) { LogManager::GetInstance().Error("Shader::Render-> could not lock a buffer"); return; } //Specific switch (buffType) { case eBufferType_Pixel_Matrix: { MatrixBufferType* dataPtr = (MatrixBufferType*)mappedResource.pData; dataPtr->world = world; dataPtr->projection = proj; dataPtr->view = view; } break; case eBufferType_Pixel_Camera: { CameraBufferType* dataPtr = (CameraBufferType*)mappedResource.pData; dataPtr->cameraPosition = Application::GetInstance().GetGraphicsDevice()->m_debugCamera->GetPosition(); dataPtr->padding = 0.0f; } break; case eBufferType_Pixel_Light: { LightBufferType* dataPtr = (LightBufferType*)mappedResource.pData; dataPtr->ambientColour = D3DXVECTOR4(0.15f, 0.15f, 0.15f, 1.0f); dataPtr->diffuseColour = D3DXVECTOR4(1.0f, 1.0f, 1.0f, 1.0f); dataPtr->specularColour = D3DXVECTOR4(1.0f, 1.0f, 1.0f, 1.0f); dataPtr->direction = D3DXVECTOR3(0.0f, -1.0f, 0.0f); dataPtr->specularPower = 1024.0f; } break; default: assert(false && "It's pretty impossible to get here, re-evaluate your life"); break; } deviceContext->Unmap((*it).second, 0); deviceContext->PSSetConstantBuffers(pixelBufferNum, 1, &(*it).second); pixelBufferNum++; } else { assert(false && "You dun goofed"); } } std::vector<ID3D11ShaderResourceView> texturesToSend; for (auto it = m_textures.begin(); it != m_textures.end(); it++) { if (m_supportedTextures.count((*it).first) > 0) { ID3D11ShaderResourceView* temp = (*it).second->GetTexture(); deviceContext->PSSetShaderResources((*it).first, 1, &temp); } } deviceContext->IASetInputLayout(m_layout); deviceContext->VSSetShader(m_vertexShader, nullptr, 0); deviceContext->PSSetShader(m_pixelShader, nullptr, 0); deviceContext->PSSetSamplers(0, 1, &m_sampleState); }
void BillboardTrees::Render() { if (!m_loadingComplete) return; auto renderStateMgr = RenderStateMgr::Instance(); ID3D11DeviceContext* context = m_deviceResources->GetD3DDeviceContext(); // Set IA stage UINT stride = sizeof(PointSize); UINT offset = 0; if (ShaderChangement::PrimitiveType != D3D11_PRIMITIVE_TOPOLOGY_POINTLIST) { context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_POINTLIST); ShaderChangement::PrimitiveType = D3D11_PRIMITIVE_TOPOLOGY_POINTLIST; } if (ShaderChangement::InputLayout != m_treeInputLayout.Get()) { context->IASetInputLayout(m_treeInputLayout.Get()); ShaderChangement::InputLayout = m_treeInputLayout.Get(); } context->IASetVertexBuffers(0, 1, m_treeSpriteVB.GetAddressOf(), &stride, &offset); ID3D11Buffer* cbuffers[2] = { m_perFrameCB->GetBuffer(), m_treeSettingsCB.GetBuffer() }; ID3D11SamplerState* samplers[1] = { renderStateMgr->LinearSam() }; // Bind shaders, constant buffers, srvs and samplers context->VSSetShader(m_treeVS.Get(), 0, 0); ShaderChangement::VS = m_treeVS.Get(); context->GSSetShader(m_treeGS.Get(), 0, 0); context->GSSetConstantBuffers(0, 1, cbuffers); switch (m_renderOptions) { case BillTreeRenderOption::Light3: // Light context->PSSetShader(m_treeLight3PS.Get(), 0, 0); ShaderChangement::PS = m_treeLight3PS.Get(); break; case BillTreeRenderOption::Light3TexClip: // LightTexClip context->PSSetShader(m_treeLight3TexClipPS.Get(), 0, 0); ShaderChangement::PS = m_treeLight3TexClipPS.Get(); break; case BillTreeRenderOption::Light3TexClipFog: // LightTexClipFog context->PSSetShader(m_treeLight3TexClipFogPS.Get(), 0, 0); ShaderChangement::PS = m_treeLight3TexClipFogPS.Get(); break; default: throw ref new Platform::InvalidArgumentException("No such render option"); } context->PSSetConstantBuffers(0, 2, cbuffers); context->PSSetSamplers(0, 1, samplers); context->PSSetShaderResources(0, 1, m_treeTextureMapArraySRV.GetAddressOf()); float blendFactor[4] = { 0.0f, 0.0f, 0.0f, 0.0f }; if (m_alphaToCoverage) context->OMSetBlendState(renderStateMgr->AlphaToCoverBS(), blendFactor, 0xffffffff); context->Draw(m_treeCount, 0); // Recover render state if (m_alphaToCoverage) context->OMSetBlendState(nullptr, blendFactor, 0xffffffff); // Remove gs context->GSSetShader(nullptr, 0, 0); ShaderChangement::GS = nullptr; }
/** Binds a pipeline-state */ void D3D11GraphicsEngineQueued::BindPipelineState(const PipelineState* state) { D3D11PipelineState* s = (D3D11PipelineState*)state; D3D11PipelineState* b = (D3D11PipelineState*)BoundPipelineStateByThread[GetCurrentThreadId()]; ID3D11DeviceContext* context = GetDeferredContextByThread(); if(!b) b = (D3D11PipelineState*)&DefaultPipelineState; // Bind state if(b->BlendState != s->BlendState) SC_DBG(context->OMSetBlendState(s->BlendState, (float *)&D3DXVECTOR4(0, 0, 0, 0), 0xFFFFFFFF), GothicRendererInfo::SC_BS); if(b->SamplerState != s->SamplerState) SC_DBG(context->PSSetSamplers(0, 1, &s->SamplerState), GothicRendererInfo::SC_SMPL); if(b->DepthStencilState != s->DepthStencilState) SC_DBG(context->OMSetDepthStencilState(s->DepthStencilState, 0), GothicRendererInfo::SC_DSS); if(b->RasterizerState != s->RasterizerState) SC_DBG(context->RSSetState(s->RasterizerState), GothicRendererInfo::SC_RS); // Bind constantbuffers (They are likely to change for every object) if(!s->ConstantBuffersVS.empty() && s->ConstantBuffersVS != b->ConstantBuffersVS)SC_DBG(context->VSSetConstantBuffers(0, s->ConstantBuffersVS.size(), &s->ConstantBuffersVS[0]),GothicRendererInfo::SC_CB); if(!s->ConstantBuffersPS.empty() && s->ConstantBuffersPS != b->ConstantBuffersPS)SC_DBG(context->PSSetConstantBuffers(0, s->ConstantBuffersPS.size(), &s->ConstantBuffersPS[0]),GothicRendererInfo::SC_CB); if(!s->ConstantBuffersHDS.empty() && s->ConstantBuffersHDS != b->ConstantBuffersHDS)SC_DBG(context->HSSetConstantBuffers(0, s->ConstantBuffersHDS.size(), &s->ConstantBuffersHDS[0]),GothicRendererInfo::SC_CB); if(!s->ConstantBuffersHDS.empty() && s->ConstantBuffersHDS != b->ConstantBuffersHDS)SC_DBG(context->DSSetConstantBuffers(0, s->ConstantBuffersHDS.size(), &s->ConstantBuffersHDS[0]),GothicRendererInfo::SC_CB); if(!s->ConstantBuffersGS.empty() && s->ConstantBuffersGS != b->ConstantBuffersGS)SC_DBG(context->GSSetConstantBuffers(0, s->ConstantBuffersGS.size(), &s->ConstantBuffersGS[0]),GothicRendererInfo::SC_CB); // Vertexbuffers UINT off[] = {0,0}; if(memcmp(s->BaseState.VertexBuffers, b->BaseState.VertexBuffers, sizeof(b->BaseState.VertexBuffers)) != 0) SC_DBG(context->IASetVertexBuffers(0, s->VertexBuffers.size(), &s->VertexBuffers[0], s->BaseState.VertexStride, off), GothicRendererInfo::SC_VB); if(!s->StructuredBuffersVS.empty() && memcmp(s->BaseState.StructuredBuffersVS, b->BaseState.StructuredBuffersVS, sizeof(b->BaseState.StructuredBuffersVS)) != 0) SC_DBG(context->VSSetShaderResources(0, 1, &s->StructuredBuffersVS[0]), GothicRendererInfo::SC_VB); if(s->IndexBuffer != b->IndexBuffer) SC_DBG(context->IASetIndexBuffer(s->IndexBuffer, s->BaseState.IndexStride == 4 ? DXGI_FORMAT_R32_UINT : DXGI_FORMAT_R16_UINT, 0), GothicRendererInfo::SC_IB); // Shaders if(s->VertexShader != b->VertexShader) SC_DBG(context->VSSetShader(s->VertexShader, NULL, NULL), GothicRendererInfo::SC_VS); if(s->InputLayout != b->InputLayout) SC_DBG(context->IASetInputLayout(s->InputLayout), GothicRendererInfo::SC_IL); if(s->PixelShader != b->PixelShader) SC_DBG(context->PSSetShader(s->PixelShader, NULL, NULL), GothicRendererInfo::SC_PS); if(s->HullShader != b->HullShader) SC_DBG(context->HSSetShader(s->HullShader, NULL, NULL), GothicRendererInfo::SC_HS); if(s->DomainShader != b->DomainShader) SC_DBG(context->DSSetShader(s->DomainShader, NULL, NULL), GothicRendererInfo::SC_DS); if(s->GeometryShader != b->GeometryShader) SC_DBG(context->GSSetShader(s->GeometryShader, NULL, NULL), GothicRendererInfo::SC_GS); // Rendertargets if(memcmp(s->RenderTargetViews, b->RenderTargetViews, sizeof(void*) * s->NumRenderTargetViews) != 0 || s->DepthStencilView != b->DepthStencilView) SC_DBG(context->OMSetRenderTargets(s->NumRenderTargetViews, s->RenderTargetViews, s->DepthStencilView), GothicRendererInfo::SC_RTVDSV); // Textures if(memcmp(s->Textures, b->Textures, sizeof(void*) * s->BaseState.NumTextures) != 0) SC_DBG(context->PSSetShaderResources(0, s->BaseState.NumTextures, s->Textures), GothicRendererInfo::SC_TX); // Primitive topology //Context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); // Replace old state // FIXME: Might not threadsave BoundPipelineStateByThread[GetCurrentThreadId()] = s; }
bool ShadowMappingShader::SetShaderParameters(D3DXMATRIX worldMatrix, D3DXMATRIX viewMatrix, D3DXMATRIX projectionMatrix, D3DXMATRIX lightViewMatrix, D3DXMATRIX lightProjectionMatrix, ID3D11ShaderResourceView* texture, ID3D11ShaderResourceView* depthMapTexture, D3DXVECTOR3 lightPosition, D3DXVECTOR4 ambientColor, D3DXVECTOR4 diffuseColor) { HRESULT result; D3D11_MAPPED_SUBRESOURCE mappedResource; unsigned int bufferNumber; MatrixBufferType* dataPtr; LightBufferType* dataPtr2; LightBufferType2* dataPtr3; // Transpose the matrices to prepare them for the shader. D3DXMatrixTranspose(&worldMatrix, &worldMatrix); D3DXMatrixTranspose(&viewMatrix, &viewMatrix); D3DXMatrixTranspose(&projectionMatrix, &projectionMatrix); D3DXMatrixTranspose(&lightViewMatrix, &lightViewMatrix); D3DXMatrixTranspose(&lightProjectionMatrix, &lightProjectionMatrix); ID3D11DeviceContext* deviceContext = GraphicsDX::GetDeviceContext(); // Lock the constant buffer so it can be written to. result = deviceContext->Map(m_matrixBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource); if (FAILED(result)) { return false; } // Get a pointer to the data in the constant buffer. dataPtr = (MatrixBufferType*)mappedResource.pData; // Copy the matrices into the constant buffer. dataPtr->world = worldMatrix; dataPtr->view = viewMatrix; dataPtr->projection = projectionMatrix; dataPtr->lightView = lightViewMatrix; dataPtr->lightProjection = lightProjectionMatrix; // Unlock the constant buffer. deviceContext->Unmap(m_matrixBuffer, 0); // Set the position of the constant buffer in the vertex shader. bufferNumber = 0; // Now set the constant buffer in the vertex shader with the updated values. deviceContext->VSSetConstantBuffers(bufferNumber, 1, &m_matrixBuffer); // Set shader texture resource in the pixel shader. deviceContext->PSSetShaderResources(0, 1, &texture); deviceContext->PSSetShaderResources(1, 1, &depthMapTexture); // Lock the light constant buffer so it can be written to. result = deviceContext->Map(m_lightBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource); if (FAILED(result)) { return false; } // Get a pointer to the data in the constant buffer. dataPtr2 = (LightBufferType*)mappedResource.pData; // Copy the lighting variables into the constant buffer. dataPtr2->ambientColor = ambientColor; dataPtr2->diffuseColor = diffuseColor; // Unlock the constant buffer. deviceContext->Unmap(m_lightBuffer, 0); // Set the position of the light constant buffer in the pixel shader. bufferNumber = 0; // Finally set the light constant buffer in the pixel shader with the updated values. deviceContext->PSSetConstantBuffers(bufferNumber, 1, &m_lightBuffer); // Lock the second light constant buffer so it can be written to. result = deviceContext->Map(m_lightBuffer2, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource); if (FAILED(result)) { return false; } // Get a pointer to the data in the constant buffer. dataPtr3 = (LightBufferType2*)mappedResource.pData; // Copy the lighting variables into the constant buffer. dataPtr3->lightPosition = lightPosition; dataPtr3->padding = 0.0f; // Unlock the constant buffer. deviceContext->Unmap(m_lightBuffer2, 0); // Set the position of the light constant buffer in the vertex shader. bufferNumber = 1; // Finally set the light constant buffer in the pixel shader with the updated values. deviceContext->VSSetConstantBuffers(bufferNumber, 1, &m_lightBuffer2); return true; }
HRESULT DeferredPipeline::OutputMerger::set_materials_buffer(ID3D11DeviceContext & device_context) { device_context.PSSetConstantBuffers(0, 1, &_ps_materials_buffer); return S_OK; }
bool FontShader::setShaderParameters(ID3D11ShaderResourceView* i_fontTexture, D3DXVECTOR4 i_fontColor) { HRESULT result; D3D11_MAPPED_SUBRESOURCE mappedResource; ConstantBufferType* dataPtr; unsigned int bufferNumber; PixelBufferType* dataPtr2; ID3D11DeviceContext* deviceContext = GraphicsDX::GetDeviceContext(); // Lock the constant buffer so it can be written to. result = deviceContext->Map(_constantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource); if (FAILED(result)) { return false; } // Get a pointer to the data in the constant buffer. dataPtr = (ConstantBufferType*)mappedResource.pData; D3DXMATRIX orthoViewMatrix(Graphics::GetCamera()->getOrthoViewMatrix()); D3DXMATRIX orthoProjMatrix(Graphics::GetCamera()->getOrthoProjMatrix()); // Transpose the matrices to prepare them for the shader. D3DXMatrixTranspose(&orthoViewMatrix, &orthoViewMatrix); D3DXMatrixTranspose(&orthoProjMatrix, &orthoProjMatrix); // Copy the matrices into the constant buffer. dataPtr->view = orthoViewMatrix; dataPtr->projection = orthoProjMatrix; // Unlock the constant buffer. deviceContext->Unmap(_constantBuffer, 0); // Set the position of the constant buffer in the vertex shader. bufferNumber = 0; // Now set the constant buffer in the vertex shader with the updated values. deviceContext->VSSetConstantBuffers(bufferNumber, 1, &_constantBuffer); // Set shader texture resource in the pixel shader. deviceContext->PSSetShaderResources(0, 1, &i_fontTexture); // Lock the pixel constant buffer so it can be written to. result = deviceContext->Map(_pixelBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource); if (FAILED(result)) { return false; } // Get a pointer to the data in the pixel constant buffer. dataPtr2 = (PixelBufferType*)mappedResource.pData; // Copy the pixel color into the pixel constant buffer. dataPtr2->pixelColor = i_fontColor; // Unlock the pixel constant buffer. deviceContext->Unmap(_pixelBuffer, 0); // Set the position of the pixel constant buffer in the pixel shader. bufferNumber = 0; // Now set the pixel constant buffer in the pixel shader with the updated value. deviceContext->PSSetConstantBuffers(bufferNumber, 1, &_pixelBuffer); return true; }