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.cpp
Views: 1401
#include "ppsspp_config.h"12#include <cstdint>3#include <cfloat>4#include <vector>5#include <string>6#include <d3d11.h>7#include <D3Dcompiler.h>89#if PPSSPP_PLATFORM(UWP)10#define ptr_D3DCompile D3DCompile11#else12#include "Common/GPU/D3D11/D3D11Loader.h"13#endif1415#include "Common/CommonFuncs.h"16#include "Common/Log.h"17#include "Common/StringUtils.h"1819#include "D3D11Util.h"2021std::vector<uint8_t> CompileShaderToBytecodeD3D11(const char *code, size_t codeSize, const char *target, UINT flags) {22ID3DBlob *compiledCode = nullptr;23ID3DBlob *errorMsgs = nullptr;24HRESULT result = ptr_D3DCompile(code, codeSize, nullptr, nullptr, nullptr, "main", target, flags, 0, &compiledCode, &errorMsgs);25std::string errors;26if (errorMsgs) {27errors = std::string((const char *)errorMsgs->GetBufferPointer(), errorMsgs->GetBufferSize());28std::string numberedCode = LineNumberString(code);29if (SUCCEEDED(result)) {30std::vector<std::string_view> lines;31SplitString(errors, '\n', lines);32for (auto &line : lines) {33auto trimmed = StripSpaces(line);34// Ignore the useless warning about taking the power of negative numbers.35if (trimmed.find("pow(f, e) will not work for negative f") != std::string::npos) {36continue;37}38if (trimmed.size() > 1) { // ignore single nulls, not sure how they appear.39WARN_LOG(Log::G3D, "%.*s", (int)trimmed.length(), trimmed.data());40}41}42} else {43ERROR_LOG(Log::G3D, "%s: %s\n\n%s", "errors", errors.c_str(), numberedCode.c_str());44}45OutputDebugStringA(errors.c_str());46OutputDebugStringA(numberedCode.c_str());47errorMsgs->Release();48}49if (compiledCode) {50// Success!51const uint8_t *buf = (const uint8_t *)compiledCode->GetBufferPointer();52std::vector<uint8_t> compiled = std::vector<uint8_t>(buf, buf + compiledCode->GetBufferSize());53_assert_(compiled.size() != 0);54compiledCode->Release();55return compiled;56}57return std::vector<uint8_t>();58}5960ID3D11VertexShader *CreateVertexShaderD3D11(ID3D11Device *device, const char *code, size_t codeSize, std::vector<uint8_t> *byteCodeOut, D3D_FEATURE_LEVEL featureLevel, UINT flags) {61const char *profile = featureLevel <= D3D_FEATURE_LEVEL_9_3 ? "vs_4_0_level_9_1" : "vs_4_0";62std::vector<uint8_t> byteCode = CompileShaderToBytecodeD3D11(code, codeSize, profile, flags);63if (byteCode.empty())64return nullptr;6566ID3D11VertexShader *vs;67device->CreateVertexShader(byteCode.data(), byteCode.size(), nullptr, &vs);68if (byteCodeOut)69*byteCodeOut = byteCode;70return vs;71}7273ID3D11PixelShader *CreatePixelShaderD3D11(ID3D11Device *device, const char *code, size_t codeSize, D3D_FEATURE_LEVEL featureLevel, UINT flags) {74const char *profile = featureLevel <= D3D_FEATURE_LEVEL_9_3 ? "ps_4_0_level_9_1" : "ps_4_0";75std::vector<uint8_t> byteCode = CompileShaderToBytecodeD3D11(code, codeSize, profile, flags);76if (byteCode.empty())77return nullptr;7879ID3D11PixelShader *ps;80device->CreatePixelShader(byteCode.data(), byteCode.size(), nullptr, &ps);81return ps;82}8384ID3D11ComputeShader *CreateComputeShaderD3D11(ID3D11Device *device, const char *code, size_t codeSize, D3D_FEATURE_LEVEL featureLevel, UINT flags) {85if (featureLevel <= D3D_FEATURE_LEVEL_9_3)86return nullptr;87std::vector<uint8_t> byteCode = CompileShaderToBytecodeD3D11(code, codeSize, "cs_4_0", flags);88if (byteCode.empty())89return nullptr;9091ID3D11ComputeShader *cs;92device->CreateComputeShader(byteCode.data(), byteCode.size(), nullptr, &cs);93return cs;94}9596ID3D11GeometryShader *CreateGeometryShaderD3D11(ID3D11Device *device, const char *code, size_t codeSize, D3D_FEATURE_LEVEL featureLevel, UINT flags) {97if (featureLevel <= D3D_FEATURE_LEVEL_9_3)98return nullptr;99std::vector<uint8_t> byteCode = CompileShaderToBytecodeD3D11(code, codeSize, "gs_5_0", flags);100if (byteCode.empty())101return nullptr;102103ID3D11GeometryShader *gs;104device->CreateGeometryShader(byteCode.data(), byteCode.size(), nullptr, &gs);105return gs;106}107108void StockObjectsD3D11::Create(ID3D11Device *device) {109D3D11_BLEND_DESC blend_desc{};110blend_desc.RenderTarget[0].BlendEnable = false;111blend_desc.IndependentBlendEnable = false;112for (int i = 0; i < 16; i++) {113blend_desc.RenderTarget[0].RenderTargetWriteMask = i;114ASSERT_SUCCESS(device->CreateBlendState(&blend_desc, &blendStateDisabledWithColorMask[i]));115}116117D3D11_DEPTH_STENCIL_DESC depth_desc{};118depth_desc.DepthEnable = FALSE;119ASSERT_SUCCESS(device->CreateDepthStencilState(&depth_desc, &depthStencilDisabled));120depth_desc.StencilEnable = TRUE;121depth_desc.StencilReadMask = 0xFF;122depth_desc.StencilWriteMask = 0xFF;123depth_desc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_REPLACE;124depth_desc.FrontFace.StencilFailOp = D3D11_STENCIL_OP_REPLACE;125depth_desc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_REPLACE;126depth_desc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS;127depth_desc.BackFace = depth_desc.FrontFace;128ASSERT_SUCCESS(device->CreateDepthStencilState(&depth_desc, &depthDisabledStencilWrite));129130D3D11_RASTERIZER_DESC raster_desc{};131raster_desc.FillMode = D3D11_FILL_SOLID;132raster_desc.CullMode = D3D11_CULL_NONE;133raster_desc.ScissorEnable = FALSE;134raster_desc.DepthClipEnable = TRUE; // the default! FALSE is unsupported on D3D11 level 9135ASSERT_SUCCESS(device->CreateRasterizerState(&raster_desc, &rasterStateNoCull));136137D3D11_SAMPLER_DESC sampler_desc{};138sampler_desc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;139sampler_desc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;140sampler_desc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;141sampler_desc.Filter = D3D11_FILTER_MIN_MAG_MIP_POINT;142sampler_desc.ComparisonFunc = D3D11_COMPARISON_NEVER;143for (int i = 0; i < 4; i++)144sampler_desc.BorderColor[i] = 1.0f;145sampler_desc.MinLOD = -FLT_MAX;146sampler_desc.MaxLOD = FLT_MAX;147sampler_desc.MipLODBias = 0.0f;148sampler_desc.MaxAnisotropy = 1;149ASSERT_SUCCESS(device->CreateSamplerState(&sampler_desc, &samplerPoint2DWrap));150sampler_desc.Filter = D3D11_FILTER_MIN_MAG_LINEAR_MIP_POINT;151ASSERT_SUCCESS(device->CreateSamplerState(&sampler_desc, &samplerLinear2DWrap));152sampler_desc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP;153sampler_desc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP;154sampler_desc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP;155sampler_desc.Filter = D3D11_FILTER_MIN_MAG_MIP_POINT;156ASSERT_SUCCESS(device->CreateSamplerState(&sampler_desc, &samplerPoint2DClamp));157sampler_desc.Filter = D3D11_FILTER_MIN_MAG_LINEAR_MIP_POINT;158ASSERT_SUCCESS(device->CreateSamplerState(&sampler_desc, &samplerLinear2DClamp));159}160161void StockObjectsD3D11::Destroy() {162for (int i = 0; i < 16; i++) {163blendStateDisabledWithColorMask[i]->Release();164}165depthStencilDisabled->Release();166depthDisabledStencilWrite->Release();167rasterStateNoCull->Release();168samplerPoint2DWrap->Release();169samplerLinear2DWrap->Release();170samplerPoint2DClamp->Release();171samplerLinear2DClamp->Release();172}173174StockObjectsD3D11 stockD3D11;175176177