/** 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 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 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; } }
/** 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; }