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/Vulkan/ShaderManagerVulkan.h
Views: 1401
// Copyright (c) 2016- 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 <cstdio>20#include <cstdint>21#include <mutex>2223#include "Common/Thread/Promise.h"24#include "Common/Data/Collections/Hashmaps.h"25#include "Common/GPU/Vulkan/VulkanMemory.h"26#include "GPU/Common/ShaderCommon.h"27#include "GPU/Common/ShaderId.h"28#include "GPU/Common/VertexShaderGenerator.h"29#include "GPU/Common/FragmentShaderGenerator.h"30#include "GPU/Vulkan/VulkanUtil.h"31#include "Common/Math/lin/matrix4x4.h"32#include "GPU/Common/ShaderUniforms.h"3334class VulkanContext;35class DrawEngineVulkan;36class VulkanPushPool;3738class VulkanFragmentShader {39public:40VulkanFragmentShader(VulkanContext *vulkan, FShaderID id, FragmentShaderFlags flags, const char *code);41~VulkanFragmentShader();4243const std::string &source() const { return source_; }4445std::string GetShaderString(DebugShaderStringType type) const;46Promise<VkShaderModule> *GetModule() { return module_; }47const FShaderID &GetID() const { return id_; }4849FragmentShaderFlags Flags() const { return flags_; }5051protected:52Promise<VkShaderModule> *module_ = nullptr;5354VulkanContext *vulkan_;55std::string source_;56bool failed_ = false;57FShaderID id_;58FragmentShaderFlags flags_;59};6061class VulkanVertexShader {62public:63VulkanVertexShader(VulkanContext *vulkan, VShaderID id, VertexShaderFlags flags, const char *code, bool useHWTransform);64~VulkanVertexShader();6566const std::string &source() const { return source_; }6768bool UseHWTransform() const { return useHWTransform_; } // TODO: Roll into flags69VertexShaderFlags Flags() const { return flags_; }7071std::string GetShaderString(DebugShaderStringType type) const;72Promise<VkShaderModule> *GetModule() { return module_; }73const VShaderID &GetID() const { return id_; }7475protected:76Promise<VkShaderModule> *module_ = nullptr;7778VulkanContext *vulkan_;79std::string source_;80bool useHWTransform_;81VShaderID id_;82VertexShaderFlags flags_;83};8485class VulkanGeometryShader {86public:87VulkanGeometryShader(VulkanContext *vulkan, GShaderID id, const char *code);88~VulkanGeometryShader();8990const std::string &source() const { return source_; }9192std::string GetShaderString(DebugShaderStringType type) const;9394Promise<VkShaderModule> *GetModule() const { return module_; }95const GShaderID &GetID() { return id_; }9697protected:98Promise<VkShaderModule> *module_ = nullptr;99100VulkanContext *vulkan_;101std::string source_;102GShaderID id_;103};104105struct Uniforms {106// Uniform block scratchpad. These (the relevant ones) are copied to the current pushbuffer at draw time.107UB_VS_FS_Base ub_base{};108UB_VS_Lights ub_lights{};109UB_VS_Bones ub_bones{};110};111112class ShaderManagerVulkan : public ShaderManagerCommon {113public:114ShaderManagerVulkan(Draw::DrawContext *draw);115~ShaderManagerVulkan();116117void DeviceLost() override;118void DeviceRestore(Draw::DrawContext *draw) override;119120void GetShaders(int prim, VertexDecoder *decoder, VulkanVertexShader **vshader, VulkanFragmentShader **fshader, VulkanGeometryShader **gshader, const ComputedPipelineState &pipelineState, bool useHWTransform, bool useHWTessellation, bool weightsAsFloat, bool useSkinInDecode);121void ClearShaders() override;122void DirtyLastShader() override;123124int GetNumVertexShaders() const { return (int)vsCache_.size(); }125int GetNumFragmentShaders() const { return (int)fsCache_.size(); }126int GetNumGeometryShaders() const { return (int)gsCache_.size(); }127128// Used for saving/loading the cache. Don't need to be particularly fast.129VulkanVertexShader *GetVertexShaderFromID(VShaderID id) { return vsCache_.GetOrNull(id); }130VulkanFragmentShader *GetFragmentShaderFromID(FShaderID id) { return fsCache_.GetOrNull(id); }131VulkanGeometryShader *GetGeometryShaderFromID(GShaderID id) { return gsCache_.GetOrNull(id); }132133VulkanVertexShader *GetVertexShaderFromModule(VkShaderModule module);134VulkanFragmentShader *GetFragmentShaderFromModule(VkShaderModule module);135VulkanGeometryShader *GetGeometryShaderFromModule(VkShaderModule module);136137std::vector<std::string> DebugGetShaderIDs(DebugShaderType type) override;138std::string DebugGetShaderString(std::string id, DebugShaderType type, DebugShaderStringType stringType) override;139140uint64_t UpdateUniforms(bool useBufferedRendering);141142// TODO: Avoid copying these buffers if same as last draw, can still point to it assuming we're still in the same pushbuffer.143// Applies dirty changes and copies the buffer.144bool IsBaseDirty() { return true; }145bool IsLightDirty() { return true; }146bool IsBoneDirty() { return true; }147148uint32_t PushBaseBuffer(VulkanPushPool *dest, VkBuffer *buf) const {149return dest->Push(&uniforms_->ub_base, sizeof(uniforms_->ub_base), uboAlignment_, buf);150}151uint32_t PushLightBuffer(VulkanPushPool *dest, VkBuffer *buf) const {152return dest->Push(&uniforms_->ub_lights, sizeof(uniforms_->ub_lights), uboAlignment_, buf);153}154// TODO: Only push half the bone buffer if we only have four bones.155uint32_t PushBoneBuffer(VulkanPushPool *dest, VkBuffer *buf) const {156return dest->Push(&uniforms_->ub_bones, sizeof(uniforms_->ub_bones), uboAlignment_, buf);157}158159static bool LoadCacheFlags(FILE *f, DrawEngineVulkan *drawEngine);160bool LoadCache(FILE *f);161void SaveCache(FILE *f, DrawEngineVulkan *drawEngine);162163private:164void Clear();165166ShaderLanguageDesc compat_;167168typedef DenseHashMap<FShaderID, VulkanFragmentShader *> FSCache;169FSCache fsCache_;170171typedef DenseHashMap<VShaderID, VulkanVertexShader *> VSCache;172VSCache vsCache_;173174typedef DenseHashMap<GShaderID, VulkanGeometryShader *> GSCache;175GSCache gsCache_;176177char *codeBuffer_;178179uint64_t uboAlignment_;180181Uniforms *uniforms_;182183VulkanFragmentShader *lastFShader_ = nullptr;184VulkanVertexShader *lastVShader_ = nullptr;185VulkanGeometryShader *lastGShader_ = nullptr;186187FShaderID lastFSID_;188VShaderID lastVSID_;189GShaderID lastGSID_;190};191192193