File: d3d11_allocator.h

package info (click to toggle)
libvpl-tools 1.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,652 kB
  • sloc: cpp: 107,469; python: 4,303; ansic: 3,202; sh: 159; lisp: 52; makefile: 13
file content (218 lines) | stat: -rw-r--r-- 7,343 bytes parent folder | download | duplicates (3)
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
/*############################################################################
  # Copyright (C) 2005 Intel Corporation
  #
  # SPDX-License-Identifier: MIT
  ############################################################################*/

#ifndef __D3D11_ALLOCATOR_H__
#define __D3D11_ALLOCATOR_H__

#include <limits>
#include "base_allocator.h"

#ifdef __gnu_linux__
    #include <stdint.h> // for uintptr_t on Linux
#endif

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

#if (defined(_WIN32) || defined(_WIN64))

    #include <d3d11.h>
    #include <map>
    #include <vector>

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);
    virtual mfxStatus ReallocImpl(mfxMemId midIn,
                                  const mfxFrameInfo* info,
                                  mfxU16 memType,
                                  mfxMemId* midOut);

    D3D11AllocatorParams m_initParams;
    ID3D11DeviceContext* m_pDeviceContext;

    struct TextureResource {
        std::vector<mfxMemId> outerMids;
        std::vector<ID3D11Texture2D*> textures;
        std::vector<ID3D11Texture2D*> stagingTexture;
        bool bAlloc;

        TextureResource() : outerMids(), textures(), stagingTexture(), 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(),
                  m_pStaging(NULL) {
            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;
};

#endif // #if defined(_WIN32) || defined(_WIN64)
#endif // __D3D11_ALLOCATOR_H__