29 #include "DeviceContext.h" 30 #include "DeviceObjectBase.h" 32 #include "ResourceMapping.h" 34 #include "ObjectBase.h" 35 #include "DebugUtilities.h" 36 #include "SwapChain.h" 37 #include "ValidatedCast.h" 38 #include "GraphicsAccessories.h" 67 template<
typename BaseInterface>
80 m_bIsDeferred(bIsDeferred)
88 IMPLEMENT_QUERY_INTERFACE_IN_PLACE( IID_DeviceContext, TObjectBase )
91 inline virtual void SetVertexBuffers( Uint32 StartSlot, Uint32 NumBuffersSet, IBuffer **ppBuffers, Uint32 *pStrides, Uint32 *pOffsets, Uint32 Flags )
override = 0;
94 inline virtual void InvalidateState()
override = 0;
97 inline virtual void SetPipelineState(IPipelineState *pPipelineState)
override = 0;
100 inline bool CommitShaderResources(IShaderResourceBinding *pShaderResourceBinding, Uint32 Flags,
int);
103 inline virtual void SetIndexBuffer( IBuffer *pIndexBuffer, Uint32 ByteOffset )
override = 0;
106 inline void SetViewports( Uint32 NumViewports,
const Viewport *pViewports, Uint32 &RTWidth, Uint32 &RTHeight );
109 inline void SetScissorRects( Uint32 NumRects,
const Rect *pRects, Uint32 &RTWidth, Uint32 &RTHeight );
113 inline bool SetRenderTargets( Uint32 NumRenderTargets, ITextureView *ppRenderTargets[], ITextureView *pDepthStencil, Uint32 Dummy = 0 );
137 inline bool SetBlendFactors(
const float *BlendFactors,
int Dummy);
139 inline bool SetStencilRef(Uint32 StencilRef,
int Dummy);
196 const bool m_bIsDeferred =
false;
200 template<
typename BaseInterface>
205 LOG_ERROR_MESSAGE(
"Start vertex buffer slot ", StartSlot,
" is out of allowed range [0, ",
MaxBufferSlots-1,
"]." );
211 LOG_ERROR_MESSAGE(
"The range of vertex buffer slots being set [", StartSlot,
", ", StartSlot + NumBuffersSet - 1,
"] is out of allowed range [0, ",
MaxBufferSlots - 1,
"]." );
217 for(Uint32 s=0; s < m_NumVertexStreams; ++s)
219 m_NumVertexStreams = 0;
221 m_NumVertexStreams = std::max(m_NumVertexStreams, StartSlot + NumBuffersSet );
223 for( Uint32 Buff = 0; Buff < NumBuffersSet; ++Buff )
225 auto &CurrStream = m_VertexStreams[StartSlot + Buff];
227 CurrStream.Stride = pStrides ? pStrides[Buff] : 0;
228 CurrStream.Offset = pOffsets ? pOffsets[Buff] : 0;
230 if( CurrStream.pBuffer )
232 const auto &BuffDesc = CurrStream.pBuffer->GetDesc();
235 LOG_ERROR_MESSAGE(
"Buffer \"", BuffDesc.Name ? BuffDesc.Name :
"",
"\" being bound as vertex buffer to slot ", Buff,
" was not created with BIND_VERTEX_BUFFER flag" );
241 while(m_NumVertexStreams > 0 && !m_VertexStreams[m_NumVertexStreams-1].pBuffer)
245 template<
typename BaseInterface>
248 m_pPipelineState = pPipelineState;
251 template<
typename BaseInterface>
255 if (!m_pPipelineState)
257 LOG_ERROR_MESSAGE(
"No pipeline state is bound to the pipeline");
261 if (pShaderResourceBinding)
264 if (pPSO != m_pPipelineState)
266 LOG_ERROR_MESSAGE(
"Shader resource binding object does not match currently bound pipeline state");
274 template<
typename BaseInterface>
278 m_IsDefaultFramebufferBound =
false;
281 template<
typename BaseInterface>
284 m_pIndexBuffer = pIndexBuffer;
285 m_IndexDataStartOffset = ByteOffset;
287 const auto &BuffDesc = m_pIndexBuffer->
GetDesc();
290 LOG_ERROR_MESSAGE(
"Buffer \"", BuffDesc.Name ? BuffDesc.Name :
"",
"\" being bound as index buffer was not created with BIND_INDEX_BUFFER flag" );
296 template<
typename BaseInterface>
299 VERIFY( ppPSO !=
nullptr,
"Null pointer provided null" );
300 VERIFY( *ppPSO ==
nullptr,
"Memory address contains a pointer to a non-null blend state" );
303 m_pPipelineState->QueryInterface( IID_PipelineState, reinterpret_cast<IObject**>( ppPSO ) );
310 for( Uint32 f = 0; f < 4; ++f )
311 BlendFactors[f] = m_BlendFactors[f];
312 StencilRef = m_StencilRef;
315 template<
typename BaseInterface>
318 bool FactorsDiffer =
false;
319 for( Uint32 f = 0; f < 4; ++f )
321 if( m_BlendFactors[f] != BlendFactors[f] )
322 FactorsDiffer =
true;
323 m_BlendFactors[f] = BlendFactors[f];
325 return FactorsDiffer;
328 template<
typename BaseInterface>
329 inline bool DeviceContextBase<BaseInterface> :: SetStencilRef(Uint32 StencilRef,
int)
331 if (m_StencilRef != StencilRef)
333 m_StencilRef = StencilRef;
339 template<
typename BaseInterface>
345 for( Uint32 rt = 0; rt < m_NumBoundRenderTargets; ++rt )
347 if(
auto *pRTView = m_pBoundRenderTargets[rt].RawPtr() )
350 auto *pTex = pRTView->GetTexture();
351 auto MipLevel = pRTView->GetDesc().MostDetailedMip;
352 const auto &TexDesc = pTex->GetDesc();
353 RTWidth = TexDesc.Width >> MipLevel;
354 RTHeight = TexDesc.Height >> MipLevel;
355 VERIFY( RTWidth > 0 && RTHeight > 0,
"RT dimension is zero" );
361 if( RTWidth == 0 || RTHeight == 0 )
363 auto *pDSView = m_pBoundDepthStencil.RawPtr();
364 if( pDSView !=
nullptr )
367 auto *pTex = pDSView->GetTexture();
368 const auto &TexDesc = pTex->GetDesc();
369 RTWidth = TexDesc.Width;
370 RTHeight = TexDesc.Height;
376 if( RTWidth == 0 || RTHeight == 0 )
386 LOG_ERROR(
"Failed to determine default render target size: swap chain is not initialized in the device context");
391 template<
typename BaseInterface>
394 if( RTWidth == 0 || RTHeight == 0 )
396 GetRenderTargetSize( RTWidth, RTHeight );
402 Viewport DefaultVP( 0, 0, static_cast<float>(RTWidth), static_cast<float>(RTHeight) );
404 if( m_NumViewports == 1 && pViewports ==
nullptr )
406 pViewports = &DefaultVP;
409 for( Uint32 vp = 0; vp < m_NumViewports; ++vp )
411 m_Viewports[vp] = pViewports[vp];
412 VERIFY( m_Viewports[vp].Width >= 0,
"Incorrect viewport width (", m_Viewports[vp].Width,
")" );
413 VERIFY( m_Viewports[vp].Height >= 0 ,
"Incorrect viewport height (", m_Viewports[vp].Height,
")" );
414 VERIFY( m_Viewports[vp].MaxDepth >= m_Viewports[vp].MinDepth,
"Incorrect viewport depth range [", m_Viewports[vp].MinDepth,
", ", m_Viewports[vp].MaxDepth,
"]" );
418 template<
typename BaseInterface>
421 NumViewports = m_NumViewports;
424 for( Uint32 vp = 0; vp < m_NumViewports; ++vp )
425 pViewports[vp] = m_Viewports[vp];
429 template<
typename BaseInterface>
432 if( RTWidth == 0 || RTHeight == 0 )
434 GetRenderTargetSize( RTWidth, RTHeight );
440 for( Uint32 sr = 0; sr < m_NumScissorRects; ++sr )
442 m_ScissorRects[sr] = pRects[sr];
443 VERIFY( m_ScissorRects[sr].left <= m_ScissorRects[sr].right,
"Incorrect horizontal bounds for a scissor rect [", m_ScissorRects[sr].left,
", ", m_ScissorRects[sr].right,
")" );
444 VERIFY( m_ScissorRects[sr].top <= m_ScissorRects[sr].bottom,
"Incorrect vertical bounds for a scissor rect [", m_ScissorRects[sr].top,
", ", m_ScissorRects[sr].bottom,
")" );
448 template<
typename BaseInterface>
451 bool bBindRenderTargets =
false;
452 if( NumRenderTargets != m_NumBoundRenderTargets )
454 bBindRenderTargets =
true;
455 for(Uint32 rt = NumRenderTargets; rt < m_NumBoundRenderTargets; ++rt )
456 m_pBoundRenderTargets[rt].Release();
458 m_NumBoundRenderTargets = NumRenderTargets;
461 for( Uint32 rt = 0; rt < NumRenderTargets; ++rt )
463 auto *pRTView = ppRenderTargets[rt];
467 const auto &ViewDesc = pRTView->
GetDesc();
468 VERIFY( ViewDesc.ViewType ==
TEXTURE_VIEW_RENDER_TARGET,
"Texture view object named \"", ViewDesc.Name ? ViewDesc.Name :
"",
"\" has incorrect view type (", GetTexViewTypeLiteralName(ViewDesc.ViewType),
"). Render target view is expected" );
474 if( m_pBoundRenderTargets[rt] != pRTView )
476 m_pBoundRenderTargets[rt] = pRTView;
477 bBindRenderTargets =
true;
484 const auto &ViewDesc = pDepthStencil->
GetDesc();
485 VERIFY( ViewDesc.ViewType ==
TEXTURE_VIEW_DEPTH_STENCIL,
"Texture view object named \"", ViewDesc.Name ? ViewDesc.Name :
"",
"\" has incorrect view type (", GetTexViewTypeLiteralName(ViewDesc.ViewType),
"). Depth stencil view is expected" );
489 if( m_pBoundDepthStencil != pDepthStencil)
491 m_pBoundDepthStencil = pDepthStencil;
492 bBindRenderTargets =
true;
495 if (NumRenderTargets == 0 && pDepthStencil ==
nullptr)
497 if (!m_IsDefaultFramebufferBound)
499 m_IsDefaultFramebufferBound =
true;
500 bBindRenderTargets =
true;
504 m_IsDefaultFramebufferBound =
false;
506 return bBindRenderTargets;
509 template<
typename BaseInterface>
512 NumRenderTargets = m_NumBoundRenderTargets;
516 for( Uint32 rt = 0; rt < NumRenderTargets; ++rt )
518 VERIFY( ppRTVs[rt] ==
nullptr,
"Non-null pointer found in RTV array element #", rt );
519 auto pBoundRTV = m_pBoundRenderTargets[rt];
521 pBoundRTV->QueryInterface( IID_TextureView, reinterpret_cast<IObject**>(ppRTVs + rt) );
523 ppRTVs[rt] =
nullptr;
527 VERIFY( ppRTVs[rt] ==
nullptr,
"Non-null pointer found in RTV array element #", rt );
528 ppRTVs[rt] =
nullptr;
534 VERIFY( *ppDSV ==
nullptr,
"Non-null DSV pointer found" );
535 if( m_pBoundDepthStencil )
536 m_pBoundDepthStencil->QueryInterface( IID_TextureView, reinterpret_cast<IObject**>(ppDSV) );
542 template<
typename BaseInterface>
545 for(Uint32 stream=0; stream < m_NumVertexStreams; ++stream)
548 for(Uint32 stream=m_NumVertexStreams; stream < _countof(m_VertexStreams); ++stream)
550 VERIFY(m_VertexStreams[stream].pBuffer ==
nullptr,
"Unexpected non-null buffer");
551 VERIFY(m_VertexStreams[stream].Offset == 0,
"Unexpected non-zero offset");
552 VERIFY(m_VertexStreams[stream].Stride == 0,
"Unexpected non-zero stride");
555 m_NumVertexStreams = 0;
557 m_pPipelineState.Release();
559 m_pIndexBuffer.Release();
560 m_IndexDataStartOffset = 0;
564 for(
int i = 0; i < 4; ++i )
565 m_BlendFactors[i] = -1;
567 for(Uint32 vp=0; vp < m_NumViewports; ++vp)
571 for(Uint32 sr=0; sr < m_NumScissorRects; ++sr)
572 m_ScissorRects[sr] =
Rect();
573 m_NumScissorRects = 0;
576 for(Uint32 rt=0; rt < m_NumBoundRenderTargets; ++rt )
577 m_pBoundRenderTargets[rt].Release();
579 for (Uint32 rt = m_NumBoundRenderTargets; rt < _countof(m_pBoundRenderTargets); ++rt)
581 VERIFY(m_pBoundRenderTargets[rt] ==
nullptr,
"Non-null render target found");
584 m_NumBoundRenderTargets = 0;
586 m_pBoundDepthStencil.Release();
bool SetRenderTargets(Uint32 NumRenderTargets, ITextureView *ppRenderTargets[], ITextureView *pDepthStencil, Uint32 Dummy=0)
Caches the render target and depth stencil views. Returns true if any view is different from the cach...
Definition: DeviceContextBase.h:449
Uint32 m_NumViewports
Number of current viewports.
Definition: DeviceContextBase.h:178
static constexpr Uint32 MaxBufferSlots
Maximum number of input buffer slots. D3D11_IA_VERTEX_INPUT_RESOURCE_SLOT_COUNT == 32...
Definition: Constants.h:33
Swap chain description.
Definition: GraphicsTypes.h:912
void GetRenderTargetSize(Uint32 &RTWidth, Uint32 &RTHeight)
Returns the size of the currently bound render target.
Definition: DeviceContextBase.h:340
Render device interface.
Definition: RenderDevice.h:55
Uint32 m_NumVertexStreams
Number of bound vertex streams.
Definition: DeviceContextBase.h:158
Template class implementing base functionality for an object.
Definition: ObjectBase.h:62
bool CommitShaderResources(IShaderResourceBinding *pShaderResourceBinding, Uint32 Flags, int)
Base implementation of IDeviceContext::CommitShaderResources(); validates parameters.
Definition: DeviceContextBase.h:252
DeviceContextBase(IReferenceCounters *pRefCounters, IRenderDevice *pRenderDevice, bool bIsDeferred)
Definition: DeviceContextBase.h:77
void GetRenderTargets(Uint32 &NumRenderTargets, ITextureView **ppRTVs, ITextureView **ppDSV)
Returns currently bound render targets.
Definition: DeviceContextBase.h:510
Namespace for the OpenGL implementation of the graphics engine.
Definition: BufferD3D11Impl.h:34
Uint32 Offset
Offset in bytes.
Definition: DeviceContextBase.h:53
Base implementation of the device context.
Definition: DeviceContextBase.h:68
Uint32 m_NumScissorRects
Number of current scissor rects.
Definition: DeviceContextBase.h:183
void SetViewports(Uint32 NumViewports, const Viewport *pViewports, Uint32 &RTWidth, Uint32 &RTHeight)
Caches the viewports.
Definition: DeviceContextBase.h:392
bool IsDefaultFBBound()
Returns true if currently bound frame buffer is the default frame buffer.
Definition: DeviceContextBase.h:122
static constexpr Uint32 MaxRenderTargets
Maximum number of simultaneous render targets.
Definition: Constants.h:36
A buffer can be bound as an index buffer.
Definition: GraphicsTypes.h:65
Describes the viewport.
Definition: DeviceContext.h:376
virtual void SetSwapChain(ISwapChain *pSwapChain) override final
Sets the strong pointer to the swap chain.
Definition: DeviceContextBase.h:116
Shader resource binding interface.
Definition: ShaderResourceBinding.h:40
IRenderDevice * GetDevice()
Returns the render device.
Definition: DeviceContextBase.h:134
Describes input vertex stream.
Definition: DeviceContextBase.h:48
A buffer can be bound as a vertex buffer.
Definition: GraphicsTypes.h:64
Buffer interface.
Definition: Buffer.h:200
Uint32 m_StencilRef
Current stencil reference value.
Definition: DeviceContextBase.h:170
RefCntAutoPtr< ITextureView > m_pBoundDepthStencil
Strong references to the bound depth stencil view.
Definition: DeviceContextBase.h:194
Swap chain interface.
Definition: SwapChain.h:41
Rect m_ScissorRects[MaxRenderTargets]
Current scissor rects.
Definition: DeviceContextBase.h:181
A texture view will define a depth stencil view that will be used as the target for rendering operati...
Definition: GraphicsTypes.h:205
VertexStreamInfo m_VertexStreams[MaxBufferSlots]
Vertex streams. Every stream holds strong reference to the buffer.
Definition: DeviceContextBase.h:155
Texture view interface.
Definition: TextureView.h:163
RefCntAutoPtr< IPipelineState > m_pPipelineState
Strong reference to the bound pipeline state object.
Definition: DeviceContextBase.h:161
Uint32 m_IndexDataStartOffset
Offset from the beginning of the index buffer to the start of the index data, in bytes.
Definition: DeviceContextBase.h:167
ISwapChain * GetSwapChain()
Returns the swap chain.
Definition: DeviceContextBase.h:119
void ClearStateCache()
Clears all cached resources.
Definition: DeviceContextBase.h:543
virtual const TextureViewDesc & GetDesc() const =0
Returns the texture view description used to create the object.
void SetScissorRects(Uint32 NumRects, const Rect *pRects, Uint32 &RTWidth, Uint32 &RTHeight)
Caches the scissor rects.
Definition: DeviceContextBase.h:430
Definition: PipelineState.h:209
RefCntAutoPtr< ITextureView > m_pBoundRenderTargets[MaxRenderTargets]
Vector of strong references to the bound render targets.
Definition: DeviceContextBase.h:186
void GetPipelineState(IPipelineState **ppPSO, float *BlendFactors, Uint32 &StencilRef)
Returns currently bound pipeline state and blend factors.
Definition: DeviceContextBase.h:297
bool m_IsDefaultFramebufferBound
Flag indicating if default render target & depth-stencil buffer are currently bound.
Definition: DeviceContextBase.h:191
void GetViewports(Uint32 &NumViewports, Viewport *pViewports)
Returns currently set viewports.
Definition: DeviceContextBase.h:419
Uint32 Stride
Stride in bytes.
Definition: DeviceContextBase.h:52
Viewport m_Viewports[MaxRenderTargets]
Current viewports.
Definition: DeviceContextBase.h:176
Describes the rectangle.
Definition: DeviceContext.h:420
Template class that implements reference counting.
Definition: RefCntAutoPtr.h:71
RefCntAutoPtr< IBuffer > pBuffer
Strong reference to the buffer object.
Definition: DeviceContextBase.h:51
virtual void SetPipelineState(IPipelineState *pPipelineState) override=0
Base implementation of IDeviceContext::SetPipelineState(); caches references to the pipeline state ob...
Definition: DeviceContextBase.h:246
RefCntAutoPtr< IRenderDevice > m_pDevice
Strong reference to the device.
Definition: DeviceContextBase.h:148
Reset the vertex buffers to only the buffers specified in this call. All buffers previously bound to ...
Definition: DeviceContext.h:359
RefCntAutoPtr< IBuffer > m_pIndexBuffer
Strong reference to the bound index buffer.
Definition: DeviceContextBase.h:164
Uint32 m_NumBoundRenderTargets
Number of bound render targets.
Definition: DeviceContextBase.h:188
Float32 m_BlendFactors[4]
Curent blend factors.
Definition: DeviceContextBase.h:173
virtual void SetVertexBuffers(Uint32 StartSlot, Uint32 NumBuffersSet, IBuffer **ppBuffers, Uint32 *pStrides, Uint32 *pOffsets, Uint32 Flags) override=0
Base implementation of IDeviceContext::SetVertexBuffers(); validates parameters and caches references...
Definition: DeviceContextBase.h:201
RefCntAutoPtr< ISwapChain > m_pSwapChain
Strong reference to the swap chain. Swap chain holds weak reference to the immediate context...
Definition: DeviceContextBase.h:152
Uint32 Height
The swap chain height. Default value is 0.
Definition: GraphicsTypes.h:918
Uint32 Width
The swap chain width. Default value is 0.
Definition: GraphicsTypes.h:915
virtual void SetIndexBuffer(IBuffer *pIndexBuffer, Uint32 ByteOffset) override=0
Base implementation of IDeviceContext::SetIndexBuffer(); caches the strong reference to the index buf...
Definition: DeviceContextBase.h:282
virtual class IPipelineState * GetPipelineState()=0
Returns pointer to the referenced buffer object.
A texture view will define a render target view that will be used as the target for rendering operati...
Definition: GraphicsTypes.h:201
virtual const BufferDesc & GetDesc() const =0
Returns the buffer description used to create the object.