void TgcDX11Effect::setTexture(string name, const TgcTexture* texture) { ID3D11DeviceContext* deviceContext = ((TgcDX11Renderer*)GuiController::Instance->renderer)->deviceContext; TgcEffectValues::Sampler sampler = this->samplers[name]; ID3D11SamplerState* dxSampler = this->dxSampleStates[name]; TgcDX11Texture* dxTexture = (TgcDX11Texture*)texture; //Set the sampler and texture for pixel shader if(sampler.shaderType == TgcEffectValues::PS) { deviceContext->PSSetSamplers(sampler.slot, 1, &dxSampler); deviceContext->PSSetShaderResources(sampler.slot, 1, &(dxTexture->dxTexture)); } //Set the sampler and texture for vertex shader else if(sampler.shaderType == TgcEffectValues::VS) { deviceContext->VSSetSamplers(sampler.slot, 1, &dxSampler); deviceContext->VSSetShaderResources(sampler.slot, 1, &(dxTexture->dxTexture)); } else if(sampler.shaderType == TgcEffectValues::VS_AND_PS) { deviceContext->VSSetSamplers(sampler.slot, 1, &dxSampler); deviceContext->PSSetSamplers(sampler.slot, 1, &dxSampler); deviceContext->VSSetShaderResources(sampler.slot, 1, &(dxTexture->dxTexture)); deviceContext->PSSetShaderResources(sampler.slot, 1, &(dxTexture->dxTexture)); } }
/** Sets a texture on the current shader instance. */ void Shader::SetTexture(u32 index, std::shared_ptr<Texture> texture) { ID3D11ShaderResourceView* resourceView = texture != nullptr ? texture->GetShaderResourceView() : nullptr; ID3D11DeviceContext* context = GetParent()->GetDeviceContext(); switch(GetType()) { case ShaderType::Compute: context->CSSetShaderResources(index, 1, &resourceView); break; case ShaderType::Domain: context->DSSetShaderResources(index, 1, &resourceView); break; case ShaderType::Geometry: context->GSSetShaderResources(index, 1, &resourceView); break; case ShaderType::Hull: context->HSSetShaderResources(index, 1, &resourceView); break; case ShaderType::Pixel: context->PSSetShaderResources(index, 1, &resourceView); break; case ShaderType::Vertex: context->VSSetShaderResources(index, 1, &resourceView); break; } }
//----------------------------------------------- void CPUTRenderTargetColor::SetRenderTarget( CPUTRenderParameters &renderParams, CPUTRenderTargetDepth *pDepthBuffer, DWORD renderTargetIndex, const float *pClearColor, bool clear, float zClearVal ) { // **************************** // Save the current render target "state" so we can restore it later. // **************************** mSavedWidth = CPUTRenderTargetColor::sCurrentWidth; mSavedHeight = CPUTRenderTargetColor::sCurrentHeight; // Save the render target view so we can restore it later. mpSavedColorRenderTargetView = CPUTRenderTargetColor::GetActiveRenderTargetView(); mpSavedDepthStencilView = CPUTRenderTargetDepth::GetActiveDepthStencilView(); CPUTRenderTargetColor::SetActiveWidthHeight( mWidth, mHeight ); CPUTRenderTargetDepth::SetActiveWidthHeight( mWidth, mHeight ); // TODO: support multiple render target views (i.e., MRT) ID3D11DeviceContext *pContext = CPUT_DX11::GetContext(); // Clear the shader resources to avoid a hazard warning ID3D11ShaderResourceView *pNullResources[16] = {0}; pContext->PSSetShaderResources(0, 16, pNullResources ); pContext->VSSetShaderResources(0, 16, pNullResources ); // **************************** // Set the new render target states // **************************** ID3D11DepthStencilView *pDepthStencilView = pDepthBuffer ? pDepthBuffer->GetDepthBufferView() : NULL; pContext->OMSetRenderTargets( 1, &mpColorRenderTargetView, pDepthStencilView ); CPUTRenderTargetColor::SetActiveRenderTargetView(mpColorRenderTargetView); CPUTRenderTargetDepth::SetActiveDepthStencilView(pDepthStencilView); if( clear ) { pContext->ClearRenderTargetView( mpColorRenderTargetView, pClearColor ); if( pDepthStencilView ) { pContext->ClearDepthStencilView( pDepthStencilView, D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, zClearVal, 0 ); } } D3D11_VIEWPORT viewport = { 0.0f, 0.0f, (float)mWidth, (float)mHeight, 0.0f, 1.0f }; pContext->RSSetViewports(1, &viewport); mRenderTargetSet = true; mSoftwareTextureDirty = true; } // CPUTRenderTargetColor::SetRenderTarget()
//----------------------------------------------- void CPUTRenderTargetDepth::SetRenderTarget( CPUTRenderParameters &renderParams, DWORD renderTargetIndex, float zClearVal, bool clear ) { // **************************** // Save the current render target "state" so we can restore it later. // **************************** mSavedWidth = CPUTRenderTargetDepth::GetActiveWidth(); mSavedHeight = CPUTRenderTargetDepth::GetActiveHeight(); CPUTRenderTargetColor::SetActiveWidthHeight( mWidth, mHeight ); CPUTRenderTargetDepth::SetActiveWidthHeight( mWidth, mHeight ); // TODO: support multiple render target views (i.e., MRT) ID3D11DeviceContext *pContext = CPUT_DX11::GetContext(); // Make sure this render target isn't currently bound as a texture. static ID3D11ShaderResourceView *pSRV[16] = {0}; pContext->PSSetShaderResources( 0, 16, pSRV ); // Save the color and depth views so we can restore them later. mpSavedColorRenderTargetView = CPUTRenderTargetColor::GetActiveRenderTargetView(); mpSavedDepthStencilView = CPUTRenderTargetDepth::GetActiveDepthStencilView(); // Clear the shader resources to avoid a hazard warning ID3D11ShaderResourceView *pNullResources[16] = {0}; pContext->PSSetShaderResources(0, 16, pNullResources ); pContext->VSSetShaderResources(0, 16, pNullResources ); // **************************** // Set the new render target states // **************************** ID3D11RenderTargetView *pView[1] = {NULL}; pContext->OMSetRenderTargets( 1, pView, mpDepthStencilView ); CPUTRenderTargetColor::SetActiveRenderTargetView( NULL ); CPUTRenderTargetDepth::SetActiveDepthStencilView( mpDepthStencilView ); if( clear ) { pContext->ClearDepthStencilView( mpDepthStencilView, D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, zClearVal, 0 ); } D3D11_VIEWPORT viewport = { 0.0f, 0.0f, (float)mWidth, (float)mHeight, 0.0f, 1.0f }; pContext->RSSetViewports( 1, &viewport ); mRenderTargetSet = true; } // CPUTRenderTargetDepth::SetRenderTarget()
/** 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; }