CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/GPU/D3D11/D3D11Util.h
Views: 1401
// Copyright (c) 2017- PPSSPP Project.12// This program is free software: you can redistribute it and/or modify3// it under the terms of the GNU General Public License as published by4// the Free Software Foundation, version 2.0 or later versions.56// This program is distributed in the hope that it will be useful,7// but WITHOUT ANY WARRANTY; without even the implied warranty of8// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the9// GNU General Public License 2.0 for more details.1011// A copy of the GPL 2.0 should have been included with the program.12// If not, see http://www.gnu.org/licenses/1314// Official git repository and contact information can be found at15// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.1617#pragma once1819#include <cstdint>20#include <vector>2122#include <d3d11.h>2324class PushBufferD3D11 {25public:26PushBufferD3D11(ID3D11Device *device, size_t size, D3D11_BIND_FLAG bindFlags) : size_(size) {27D3D11_BUFFER_DESC desc{};28desc.BindFlags = bindFlags;29desc.ByteWidth = (UINT)size;30desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;31desc.Usage = D3D11_USAGE_DYNAMIC;32device->CreateBuffer(&desc, nullptr, &buffer_);33}34PushBufferD3D11(PushBufferD3D11 &) = delete;35~PushBufferD3D11() {36buffer_->Release();37}38ID3D11Buffer *Buf() const {39return buffer_;40}4142// Should be done each frame43void Reset() {44pos_ = 0;45nextMapDiscard_ = true;46}4748uint8_t *BeginPush(ID3D11DeviceContext *context, UINT *offset, size_t size, int align = 16) {49D3D11_MAPPED_SUBRESOURCE map;50pos_ = (pos_ + align - 1) & ~(align - 1);51if (pos_ + size > size_) {52// Wrap! Note that with this method, since we return the same buffer as before, you have to do the draw immediately after,53// can't defer like in Vulkan. We instead let the driver handle the invalidation etc.54pos_ = 0;55nextMapDiscard_ = true;56}57context->Map(buffer_, 0, nextMapDiscard_ ? D3D11_MAP_WRITE_DISCARD : D3D11_MAP_WRITE_NO_OVERWRITE, 0, &map);58nextMapDiscard_ = false;59*offset = (UINT)pos_;60uint8_t *retval = (uint8_t *)map.pData + pos_;61pos_ += size;62return retval;63}64void EndPush(ID3D11DeviceContext *context) {65context->Unmap(buffer_, 0);66}6768private:69ID3D11Buffer *buffer_ = nullptr;70size_t pos_ = 0;71size_t size_;72bool nextMapDiscard_ = false;73};7475std::vector<uint8_t> CompileShaderToBytecodeD3D11(const char *code, size_t codeSize, const char *target, UINT flags);7677ID3D11VertexShader *CreateVertexShaderD3D11(ID3D11Device *device, const char *code, size_t codeSize, std::vector<uint8_t> *byteCodeOut, D3D_FEATURE_LEVEL featureLevel, UINT flags = 0);78ID3D11PixelShader *CreatePixelShaderD3D11(ID3D11Device *device, const char *code, size_t codeSize, D3D_FEATURE_LEVEL featureLevel, UINT flags = 0);79ID3D11ComputeShader *CreateComputeShaderD3D11(ID3D11Device *device, const char *code, size_t codeSize, D3D_FEATURE_LEVEL featureLevel, UINT flags = 0);80ID3D11GeometryShader *CreateGeometryShaderD3D11(ID3D11Device *device, const char *code, size_t codeSize, D3D_FEATURE_LEVEL featureLevel, UINT flags = 0);8182class StockObjectsD3D11 {83public:84void Create(ID3D11Device *device);85void Destroy();8687ID3D11DepthStencilState *depthStencilDisabled;88ID3D11DepthStencilState *depthDisabledStencilWrite;89ID3D11BlendState *blendStateDisabledWithColorMask[16];90ID3D11RasterizerState *rasterStateNoCull;91ID3D11SamplerState *samplerPoint2DWrap;92ID3D11SamplerState *samplerLinear2DWrap;93ID3D11SamplerState *samplerPoint2DClamp;94ID3D11SamplerState *samplerLinear2DClamp;95};9697#define ASSERT_SUCCESS(x) \98if (!SUCCEEDED((x))) \99Crash();100101extern StockObjectsD3D11 stockD3D11;102103104