-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathd3d11_allocator.h
More file actions
255 lines (231 loc) · 8.36 KB
/
Copy pathd3d11_allocator.h
File metadata and controls
255 lines (231 loc) · 8.36 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*
* Copyright (c) 2013, INTEL CORPORATION
* 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 INTEL CORPORATION 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 COPYRIGHT HOLDER 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.
*/
#pragma once
#include "base_allocator.h"
//application can provide either generic mid from surface or this wrapper
//wrapper distinguishes from generic mid by highest 1 bit
//if it set then remained pointer points to extended structure of memid
//64 bits system layout
/*----+-----------------------------------------------------------+
|b63=1|63 bits remained for pointer to extended structure of memid|
|b63=0|63 bits from original mfxMemId |
+-----+----------------------------------------------------------*/
//32 bits system layout
/*--+---+--------------------------------------------+
|b31=1|31 bits remained for pointer to extended memid|
|b31=0|31 bits remained for surface pointer |
+---+---+-------------------------------------------*/
//#pragma warning (disable:4293)
class MFXReadWriteMid
{
static const uintptr_t bits_offset = std::numeric_limits<uintptr_t>::digits - 1;
static const uintptr_t clear_mask = ~((uintptr_t)1 << bits_offset);
public:
enum
{
//if flag not set it means that read and write
not_set = 0,
reuse = 1,
read = 2,
write = 4,
};
//here mfxmemid might be as MFXReadWriteMid or mfxMemId memid
MFXReadWriteMid(mfxMemId mid, mfxU8 flag = not_set)
{
//setup mid
m_mid_to_report = (mfxMemId)((uintptr_t)&m_mid | ((uintptr_t)1 << bits_offset));
if (0 != ((uintptr_t)mid >> bits_offset))
{
//it points to extended structure
mfxMedIdEx * pMemIdExt = reinterpret_cast<mfxMedIdEx *>((uintptr_t)mid & clear_mask);
m_mid.pId = pMemIdExt->pId;
if (reuse == flag)
{
m_mid.read_write = pMemIdExt->read_write;
}
else
{
m_mid.read_write = flag;
}
}
else
{
m_mid.pId = mid;
if (reuse == flag)
m_mid.read_write = not_set;
else
m_mid.read_write = flag;
}
}
bool isRead() const
{
return 0 != (m_mid.read_write & read) || !m_mid.read_write;
}
bool isWrite() const
{
return 0 != (m_mid.read_write & write) || !m_mid.read_write;
}
/// returns original memid without read write flags
mfxMemId raw() const
{
return m_mid.pId;
}
operator mfxMemId() const
{
return m_mid_to_report;
}
private:
struct mfxMedIdEx
{
mfxMemId pId;
mfxU8 read_write;
};
mfxMedIdEx m_mid;
mfxMemId m_mid_to_report;
};
struct ID3D11VideoDevice;
struct ID3D11VideoContext;
struct D3D11AllocatorParams : mfxAllocatorParams
{
ID3D11Device *pDevice;
bool bUseSingleTexture;
DWORD uncompressedResourceMiscFlags;
D3D11AllocatorParams()
: pDevice()
, bUseSingleTexture()
, uncompressedResourceMiscFlags()
{
}
};
class D3D11FrameAllocator: public BaseFrameAllocator
{
public:
D3D11FrameAllocator();
virtual ~D3D11FrameAllocator();
virtual mfxStatus Init(mfxAllocatorParams *pParams);
virtual mfxStatus Close();
virtual ID3D11Device * GetD3D11Device()
{
return m_initParams.pDevice;
};
virtual mfxStatus LockFrame(mfxMemId mid, mfxFrameData *ptr);
virtual mfxStatus UnlockFrame(mfxMemId mid, mfxFrameData *ptr);
virtual mfxStatus GetFrameHDL(mfxMemId mid, mfxHDL *handle);
protected:
static DXGI_FORMAT ConverColortFormat(mfxU32 fourcc);
virtual mfxStatus CheckRequestType(mfxFrameAllocRequest *request);
virtual mfxStatus ReleaseResponse(mfxFrameAllocResponse *response);
virtual mfxStatus AllocImpl(mfxFrameAllocRequest *request, mfxFrameAllocResponse *response);
D3D11AllocatorParams m_initParams;
ID3D11DeviceContext *m_pDeviceContext;
struct TextureResource
{
std::vector<mfxMemId> outerMids;
std::vector<ID3D11Texture2D*> textures;
std::vector<ID3D11Texture2D*> stagingTexture;
bool bAlloc;
TextureResource()
: bAlloc(true)
{
}
static bool isAllocated (TextureResource & that)
{
return that.bAlloc;
}
ID3D11Texture2D* GetTexture(mfxMemId id)
{
if (outerMids.empty())
return NULL;
return textures[((uintptr_t)id - (uintptr_t)outerMids.front()) % textures.size()];
}
UINT GetSubResource(mfxMemId id)
{
if (outerMids.empty())
return NULL;
return (UINT)(((uintptr_t)id - (uintptr_t)outerMids.front()) / textures.size());
}
void Release()
{
size_t i = 0;
for(i = 0; i < textures.size(); i++)
{
textures[i]->Release();
}
textures.clear();
for(i = 0; i < stagingTexture.size(); i++)
{
stagingTexture[i]->Release();
}
stagingTexture.clear();
//marking texture as deallocated
bAlloc = false;
}
};
class TextureSubResource
{
TextureResource * m_pTarget;
ID3D11Texture2D * m_pTexture;
ID3D11Texture2D * m_pStaging;
UINT m_subResource;
public:
TextureSubResource(TextureResource * pTarget = NULL, mfxMemId id = 0)
: m_pTarget(pTarget)
, m_pTexture()
, m_subResource()
{
if (NULL != m_pTarget && !m_pTarget->outerMids.empty())
{
ptrdiff_t idx = (uintptr_t)MFXReadWriteMid(id).raw() - (uintptr_t)m_pTarget->outerMids.front();
m_pTexture = m_pTarget->textures[idx % m_pTarget->textures.size()];
m_subResource = (UINT)(idx / m_pTarget->textures.size());
m_pStaging = m_pTarget->stagingTexture.empty() ? NULL : m_pTarget->stagingTexture[idx];
}
}
ID3D11Texture2D* GetStaging()const
{
return m_pStaging;
}
ID3D11Texture2D* GetTexture()const
{
return m_pTexture;
}
UINT GetSubResource()const
{
return m_subResource;
}
void Release()
{
if (NULL != m_pTarget)
m_pTarget->Release();
}
};
TextureSubResource GetResourceFromMid(mfxMemId);
std::list <TextureResource> m_resourcesByRequest;//each alloc request generates new item in list
typedef std::list <TextureResource>::iterator referenceType;
std::vector<referenceType> m_memIdMap;
};