CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/GPU/D3D11/D3D11Util.h
Views: 1401
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
25
class PushBufferD3D11 {
26
public:
27
PushBufferD3D11(ID3D11Device *device, size_t size, D3D11_BIND_FLAG bindFlags) : size_(size) {
28
D3D11_BUFFER_DESC desc{};
29
desc.BindFlags = bindFlags;
30
desc.ByteWidth = (UINT)size;
31
desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
32
desc.Usage = D3D11_USAGE_DYNAMIC;
33
device->CreateBuffer(&desc, nullptr, &buffer_);
34
}
35
PushBufferD3D11(PushBufferD3D11 &) = delete;
36
~PushBufferD3D11() {
37
buffer_->Release();
38
}
39
ID3D11Buffer *Buf() const {
40
return buffer_;
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_, 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_, 0);
67
}
68
69
private:
70
ID3D11Buffer *buffer_ = nullptr;
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
ID3D11VertexShader *CreateVertexShaderD3D11(ID3D11Device *device, const char *code, size_t codeSize, std::vector<uint8_t> *byteCodeOut, D3D_FEATURE_LEVEL featureLevel, UINT flags = 0);
79
ID3D11PixelShader *CreatePixelShaderD3D11(ID3D11Device *device, const char *code, size_t codeSize, D3D_FEATURE_LEVEL featureLevel, UINT flags = 0);
80
ID3D11ComputeShader *CreateComputeShaderD3D11(ID3D11Device *device, const char *code, size_t codeSize, D3D_FEATURE_LEVEL featureLevel, UINT flags = 0);
81
ID3D11GeometryShader *CreateGeometryShaderD3D11(ID3D11Device *device, const char *code, size_t codeSize, D3D_FEATURE_LEVEL featureLevel, UINT flags = 0);
82
83
class StockObjectsD3D11 {
84
public:
85
void Create(ID3D11Device *device);
86
void Destroy();
87
88
ID3D11DepthStencilState *depthStencilDisabled;
89
ID3D11DepthStencilState *depthDisabledStencilWrite;
90
ID3D11BlendState *blendStateDisabledWithColorMask[16];
91
ID3D11RasterizerState *rasterStateNoCull;
92
ID3D11SamplerState *samplerPoint2DWrap;
93
ID3D11SamplerState *samplerLinear2DWrap;
94
ID3D11SamplerState *samplerPoint2DClamp;
95
ID3D11SamplerState *samplerLinear2DClamp;
96
};
97
98
#define ASSERT_SUCCESS(x) \
99
if (!SUCCEEDED((x))) \
100
Crash();
101
102
extern StockObjectsD3D11 stockD3D11;
103
104