Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/GPU/D3D11/D3D11Util.h
3187 views
1
// Copyright (c) 2017- PPSSPP Project.
2
3
// This program is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, version 2.0 or later versions.
6
7
// This program is distributed in the hope that it will be useful,
8
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
// GNU General Public License 2.0 for more details.
11
12
// A copy of the GPL 2.0 should have been included with the program.
13
// If not, see http://www.gnu.org/licenses/
14
15
// Official git repository and contact information can be found at
16
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
17
18
#pragma once
19
20
#include <cstdint>
21
#include <vector>
22
23
#include <d3d11.h>
24
#include <wrl/client.h>
25
26
class PushBufferD3D11 {
27
public:
28
PushBufferD3D11(ID3D11Device *device, size_t size, D3D11_BIND_FLAG bindFlags) : size_(size) {
29
D3D11_BUFFER_DESC desc{};
30
desc.BindFlags = bindFlags;
31
desc.ByteWidth = (UINT)size;
32
desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
33
desc.Usage = D3D11_USAGE_DYNAMIC;
34
device->CreateBuffer(&desc, nullptr, &buffer_);
35
}
36
PushBufferD3D11(PushBufferD3D11 &) = delete;
37
~PushBufferD3D11() {
38
}
39
ID3D11Buffer *Buf() const {
40
return buffer_.Get();
41
}
42
43
// Should be done each frame
44
void Reset() {
45
pos_ = 0;
46
nextMapDiscard_ = true;
47
}
48
49
uint8_t *BeginPush(ID3D11DeviceContext *context, UINT *offset, size_t size, int align = 16) {
50
D3D11_MAPPED_SUBRESOURCE map;
51
pos_ = (pos_ + align - 1) & ~(align - 1);
52
if (pos_ + size > size_) {
53
// Wrap! Note that with this method, since we return the same buffer as before, you have to do the draw immediately after,
54
// can't defer like in Vulkan. We instead let the driver handle the invalidation etc.
55
pos_ = 0;
56
nextMapDiscard_ = true;
57
}
58
context->Map(buffer_.Get(), 0, nextMapDiscard_ ? D3D11_MAP_WRITE_DISCARD : D3D11_MAP_WRITE_NO_OVERWRITE, 0, &map);
59
nextMapDiscard_ = false;
60
*offset = (UINT)pos_;
61
uint8_t *retval = (uint8_t *)map.pData + pos_;
62
pos_ += size;
63
return retval;
64
}
65
void EndPush(ID3D11DeviceContext *context) {
66
context->Unmap(buffer_.Get(), 0);
67
}
68
69
private:
70
Microsoft::WRL::ComPtr<ID3D11Buffer> buffer_;
71
size_t pos_ = 0;
72
size_t size_;
73
bool nextMapDiscard_ = false;
74
};
75
76
std::vector<uint8_t> CompileShaderToBytecodeD3D11(const char *code, size_t codeSize, const char *target, UINT flags);
77
78
HRESULT CreateVertexShaderD3D11(ID3D11Device *device, const char *code, size_t codeSize, std::vector<uint8_t> *byteCodeOut, D3D_FEATURE_LEVEL featureLevel, UINT flags, ID3D11VertexShader **);
79
HRESULT CreatePixelShaderD3D11(ID3D11Device *device, const char *code, size_t codeSize, D3D_FEATURE_LEVEL featureLevel, UINT flags, ID3D11PixelShader **);
80
HRESULT CreateComputeShaderD3D11(ID3D11Device *device, const char *code, size_t codeSize, D3D_FEATURE_LEVEL featureLevel, UINT flags, ID3D11ComputeShader **);
81
HRESULT CreateGeometryShaderD3D11(ID3D11Device *device, const char *code, size_t codeSize, D3D_FEATURE_LEVEL featureLevel, UINT flags, ID3D11GeometryShader **);
82
83
#define ASSERT_SUCCESS(x) \
84
if (!SUCCEEDED((x))) \
85
Crash();
86
87