1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
// Copyright 2005-2016 The Mumble Developers. All rights reserved.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file at the root of the
// Mumble source tree or at <https://www.mumble.info/LICENSE>.
/* Copyright (C) 2011-2013, Nye Liu <nyet@nyet.org>
Copyright (C) 2011-2013, Kissaki <kissaki@gmx.de>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
- Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
- Neither the name of the Mumble Developers nor the names of its
contributors may be used to endorse or promote products derived from this
software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "D11StateBlock.h"
void D11CreateStateBlock(ID3D11DeviceContext *pDeviceContext, D11StateBlock **ppStateBlock)
{
*ppStateBlock = new D11StateBlock(pDeviceContext);
}
D11StateBlock::D11StateBlock(ID3D11DeviceContext *pDC)
: pRasterizerState(0), uiSampleMask(0)
, Format(DXGI_FORMAT_UNKNOWN), Topology(D3D11_PRIMITIVE_TOPOLOGY_UNDEFINED)
, pBlendState(0), pIndexBuffer(0), pInputLayout(0), uiOffset(0)
, pDepthStencilView(0) {
uiNumViewports = D3D11_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE;
ZeroMemory(pViewports, sizeof(pViewports));
ZeroMemory(pRenderTargetViews, sizeof (pRenderTargetViews));
ZeroMemory(fBlendFactor, sizeof (fBlendFactor));
ZeroMemory(pVertexBuffers, sizeof (pVertexBuffers));
ZeroMemory(uiStrides, sizeof (uiStrides));
ZeroMemory(uiOffsets, sizeof (uiOffsets));
pDeviceContext = pDC;
pDeviceContext->AddRef();
}
D11StateBlock::~D11StateBlock() {
ReleaseObjects();
pDeviceContext->Release();
}
void D11StateBlock::Capture()
{
// Release potential references to make sure we have no leaks.
ReleaseObjects();
pDeviceContext->RSGetState(&pRasterizerState);
uiNumViewports = D3D11_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE;
pDeviceContext->RSGetViewports(&uiNumViewports, pViewports);
pDeviceContext->OMGetRenderTargets(D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT, pRenderTargetViews, &pDepthStencilView);
pDeviceContext->OMGetBlendState(&pBlendState, fBlendFactor, &uiSampleMask);
pDeviceContext->IAGetInputLayout(&pInputLayout);
pDeviceContext->IAGetIndexBuffer(&pIndexBuffer, &Format, &uiOffset);
pDeviceContext->IAGetPrimitiveTopology(&Topology);
pDeviceContext->IAGetVertexBuffers(0, D3D11_IA_VERTEX_INPUT_RESOURCE_SLOT_COUNT, pVertexBuffers, uiStrides, uiOffsets);
}
void D11StateBlock::Apply()
{
pDeviceContext->RSSetState(pRasterizerState);
pDeviceContext->RSSetViewports(uiNumViewports, pViewports);
pDeviceContext->OMSetRenderTargets(D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT, pRenderTargetViews, pDepthStencilView);
pDeviceContext->OMSetBlendState(pBlendState, fBlendFactor, uiSampleMask);
pDeviceContext->IASetInputLayout(pInputLayout);
pDeviceContext->IASetIndexBuffer(pIndexBuffer, Format, uiOffset);
pDeviceContext->IASetPrimitiveTopology(Topology);
pDeviceContext->IASetVertexBuffers(0, D3D11_IA_VERTEX_INPUT_RESOURCE_SLOT_COUNT, pVertexBuffers, uiStrides, uiOffsets);
ReleaseObjects();
}
/// Release state information
void D11StateBlock::ReleaseObjects()
{
if (pRasterizerState) {
pRasterizerState->Release();
pRasterizerState = NULL;
}
for (int i=0; i<D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT; i++)
if (pRenderTargetViews[i]) {
pRenderTargetViews[i]->Release();
pRenderTargetViews[i] = NULL;
}
if (pDepthStencilView) {
pDepthStencilView->Release();
pDepthStencilView = NULL;
}
if (pBlendState) {
pBlendState->Release();
pBlendState = NULL;
}
if (pInputLayout) {
pInputLayout->Release();
pInputLayout = NULL;
}
if (pIndexBuffer) {
pIndexBuffer->Release();
pIndexBuffer = NULL;
}
for (int i=0; i<D3D11_IA_VERTEX_INPUT_RESOURCE_SLOT_COUNT; i++)
if (pVertexBuffers[i]) {
pVertexBuffers[i]->Release();
pVertexBuffers[i] = NULL;
}
}
|