2016-03-17 10:56:43 +00:00
|
|
|
// Copyright (c) 2016- PPSSPP Project.
|
2015-10-10 14:41:19 +00:00
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, version 2.0 or later versions.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License 2.0 for more details.
|
|
|
|
|
|
|
|
// A copy of the GPL 2.0 should have been included with the program.
|
|
|
|
// If not, see http://www.gnu.org/licenses/
|
|
|
|
|
|
|
|
// Official git repository and contact information can be found at
|
|
|
|
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <map>
|
|
|
|
|
|
|
|
#include "base/basictypes.h"
|
|
|
|
#include "Globals.h"
|
|
|
|
#include "GPU/Common/ShaderCommon.h"
|
|
|
|
#include "GPU/Common/ShaderId.h"
|
|
|
|
#include "GPU/Vulkan/VertexShaderGeneratorVulkan.h"
|
|
|
|
#include "GPU/Vulkan/FragmentShaderGeneratorVulkan.h"
|
|
|
|
#include "GPU/Vulkan/VulkanUtil.h"
|
|
|
|
#include "math/lin/matrix4x4.h"
|
|
|
|
|
|
|
|
void ConvertProjMatrixToVulkan(Matrix4x4 & in);
|
|
|
|
|
|
|
|
// Pretty much full. Will need more bits for more fine grained dirty tracking for lights.
|
2017-01-23 16:32:18 +00:00
|
|
|
enum : uint64_t {
|
2016-01-09 00:23:32 +00:00
|
|
|
DIRTY_BASE_UNIFORMS =
|
2016-03-20 13:54:21 +00:00
|
|
|
DIRTY_WORLDMATRIX | DIRTY_PROJTHROUGHMATRIX | DIRTY_VIEWMATRIX | DIRTY_TEXMATRIX | DIRTY_ALPHACOLORREF |
|
2016-01-09 00:23:32 +00:00
|
|
|
DIRTY_PROJMATRIX | DIRTY_FOGCOLOR | DIRTY_FOGCOEF | DIRTY_TEXENV | DIRTY_STENCILREPLACEVALUE |
|
|
|
|
DIRTY_ALPHACOLORMASK | DIRTY_SHADERBLEND | DIRTY_UVSCALEOFFSET | DIRTY_TEXCLAMP | DIRTY_DEPTHRANGE | DIRTY_MATAMBIENTALPHA,
|
|
|
|
DIRTY_LIGHT_UNIFORMS =
|
|
|
|
DIRTY_LIGHT0 | DIRTY_LIGHT1 | DIRTY_LIGHT2 | DIRTY_LIGHT3 |
|
|
|
|
DIRTY_MATDIFFUSE | DIRTY_MATSPECULAR | DIRTY_MATEMISSIVE | DIRTY_AMBIENT,
|
2015-10-10 14:41:19 +00:00
|
|
|
};
|
|
|
|
|
2016-01-24 16:30:26 +00:00
|
|
|
// TODO: Split into two structs, one for software transform and one for hardware transform, to save space.
|
2016-03-20 19:53:46 +00:00
|
|
|
// 512 bytes. Probably can't get to 256 (nVidia's UBO alignment).
|
2016-01-09 10:07:14 +00:00
|
|
|
struct UB_VS_FS_Base {
|
2015-10-10 14:41:19 +00:00
|
|
|
float proj[16];
|
2016-01-24 16:30:26 +00:00
|
|
|
float proj_through[16];
|
2015-10-10 14:41:19 +00:00
|
|
|
float view[16];
|
|
|
|
float world[16];
|
|
|
|
float tex[16]; // not that common, may want to break out
|
|
|
|
float uvScaleOffset[4];
|
|
|
|
float depthRange[4];
|
2016-03-20 19:53:46 +00:00
|
|
|
float fogCoef_stencil[4];
|
2015-10-10 14:41:19 +00:00
|
|
|
float matAmbient[4];
|
2016-01-05 23:38:45 +00:00
|
|
|
// Fragment data
|
|
|
|
float fogColor[4];
|
|
|
|
float texEnvColor[4];
|
2016-03-17 05:51:18 +00:00
|
|
|
int alphaColorRef[4];
|
2016-01-10 11:27:45 +00:00
|
|
|
int colorTestMask[4];
|
2016-01-05 23:38:45 +00:00
|
|
|
float blendFixA[4];
|
|
|
|
float blendFixB[4];
|
|
|
|
float texClamp[4];
|
|
|
|
float texClampOffset[4];
|
2015-10-10 14:41:19 +00:00
|
|
|
};
|
|
|
|
|
2016-01-03 17:31:03 +00:00
|
|
|
static const char *ub_baseStr =
|
2016-01-24 16:30:26 +00:00
|
|
|
R"( mat4 proj_mtx;
|
|
|
|
mat4 proj_through_mtx;
|
2016-01-05 23:38:45 +00:00
|
|
|
mat4 view_mtx;
|
|
|
|
mat4 world_mtx;
|
|
|
|
mat4 tex_mtx;
|
2016-01-09 10:07:14 +00:00
|
|
|
vec4 uvscaleoffset;
|
2015-10-10 14:41:19 +00:00
|
|
|
vec4 depthRange;
|
2016-03-20 19:53:46 +00:00
|
|
|
vec3 fogcoef_stencilreplace;
|
2016-01-09 00:23:32 +00:00
|
|
|
vec4 matambientalpha;
|
2016-01-10 11:27:45 +00:00
|
|
|
vec3 fogcolor;
|
|
|
|
vec3 texenv;
|
2016-03-17 05:51:18 +00:00
|
|
|
ivec4 alphacolorref;
|
2016-01-10 11:27:45 +00:00
|
|
|
ivec4 alphacolormask;
|
2016-01-05 23:38:45 +00:00
|
|
|
vec3 blendFixA;
|
2016-01-05 22:37:18 +00:00
|
|
|
vec3 blendFixB;
|
2016-01-05 23:38:45 +00:00
|
|
|
vec4 texclamp;
|
2016-01-05 22:37:18 +00:00
|
|
|
vec2 texclampoff;
|
2015-10-10 14:41:19 +00:00
|
|
|
)";
|
|
|
|
|
2016-03-20 19:53:46 +00:00
|
|
|
// 576 bytes. Can we get down to 512?
|
2015-10-10 14:41:19 +00:00
|
|
|
struct UB_VS_Lights {
|
2016-01-02 01:08:05 +00:00
|
|
|
float ambientColor[4];
|
|
|
|
float materialDiffuse[4];
|
|
|
|
float materialSpecular[4];
|
|
|
|
float materialEmissive[4];
|
2015-10-10 14:41:19 +00:00
|
|
|
float lpos[4][4];
|
|
|
|
float ldir[4][4];
|
|
|
|
float latt[4][4];
|
|
|
|
float lightAngle[4][4]; // TODO: Merge with lightSpotCoef, use .xy
|
|
|
|
float lightSpotCoef[4][4];
|
|
|
|
float lightAmbient[4][4];
|
|
|
|
float lightDiffuse[4][4];
|
|
|
|
float lightSpecular[4][4];
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char *ub_vs_lightsStr =
|
2016-01-10 10:41:46 +00:00
|
|
|
R"( vec4 globalAmbient;
|
2016-01-09 00:23:32 +00:00
|
|
|
vec3 matdiffuse;
|
2016-01-10 10:41:46 +00:00
|
|
|
vec4 matspecular;
|
|
|
|
vec3 matemissive;
|
|
|
|
vec3 pos[4];
|
|
|
|
vec3 dir[4];
|
|
|
|
vec3 att[4];
|
2016-01-09 00:23:32 +00:00
|
|
|
float angle[4];
|
2016-01-10 10:41:46 +00:00
|
|
|
float spotCoef[4];
|
2016-01-09 00:23:32 +00:00
|
|
|
vec3 ambient[4];
|
|
|
|
vec3 diffuse[4];
|
|
|
|
vec3 specular[4];
|
2015-10-10 14:41:19 +00:00
|
|
|
)";
|
|
|
|
|
2016-03-20 19:56:33 +00:00
|
|
|
// With some cleverness, we could get away with uploading just half this when only the four first
|
|
|
|
// bones are being used. This is 512b, 256b would be great.
|
|
|
|
// Could also move to 4x3 matrices - would let us fit 5 bones into 256b.
|
2015-10-10 14:41:19 +00:00
|
|
|
struct UB_VS_Bones {
|
|
|
|
float bones[8][16];
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char *ub_vs_bonesStr =
|
2016-01-10 10:41:46 +00:00
|
|
|
R"( mat4 m[8];
|
2015-10-10 14:41:19 +00:00
|
|
|
)";
|
|
|
|
|
2016-01-09 20:19:18 +00:00
|
|
|
class VulkanContext;
|
|
|
|
class VulkanPushBuffer;
|
2015-10-10 14:41:19 +00:00
|
|
|
|
|
|
|
class VulkanFragmentShader {
|
|
|
|
public:
|
2016-01-05 20:18:43 +00:00
|
|
|
VulkanFragmentShader(VulkanContext *vulkan, ShaderID id, const char *code, bool useHWTransform);
|
2015-10-10 14:41:19 +00:00
|
|
|
~VulkanFragmentShader();
|
|
|
|
|
|
|
|
const std::string &source() const { return source_; }
|
|
|
|
|
|
|
|
bool Failed() const { return failed_; }
|
|
|
|
bool UseHWTransform() const { return useHWTransform_; }
|
|
|
|
|
|
|
|
std::string GetShaderString(DebugShaderStringType type) const;
|
2016-01-09 00:23:32 +00:00
|
|
|
VkShaderModule GetModule() const { return module_; }
|
2015-10-10 14:41:19 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
VkShaderModule module_;
|
|
|
|
|
2016-01-05 20:18:43 +00:00
|
|
|
VulkanContext *vulkan_;
|
2015-10-10 14:41:19 +00:00
|
|
|
std::string source_;
|
|
|
|
bool failed_;
|
|
|
|
bool useHWTransform_;
|
|
|
|
ShaderID id_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class VulkanVertexShader {
|
|
|
|
public:
|
2016-03-20 15:33:34 +00:00
|
|
|
VulkanVertexShader(VulkanContext *vulkan, ShaderID id, const char *code, int vertType, bool useHWTransform, bool usesLighting);
|
2015-10-10 14:41:19 +00:00
|
|
|
~VulkanVertexShader();
|
|
|
|
|
|
|
|
const std::string &source() const { return source_; }
|
|
|
|
|
|
|
|
bool Failed() const { return failed_; }
|
|
|
|
bool UseHWTransform() const { return useHWTransform_; }
|
2016-03-20 15:33:34 +00:00
|
|
|
bool HasBones() const {
|
|
|
|
return id_.Bit(VS_BIT_ENABLE_BONES);
|
|
|
|
}
|
|
|
|
bool HasLights() const {
|
|
|
|
return usesLighting_;
|
|
|
|
}
|
2015-10-10 14:41:19 +00:00
|
|
|
|
|
|
|
std::string GetShaderString(DebugShaderStringType type) const;
|
2016-01-09 00:23:32 +00:00
|
|
|
VkShaderModule GetModule() const { return module_; }
|
2015-10-10 14:41:19 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
VkShaderModule module_;
|
|
|
|
|
2016-01-05 20:18:43 +00:00
|
|
|
VulkanContext *vulkan_;
|
2015-10-10 14:41:19 +00:00
|
|
|
std::string source_;
|
|
|
|
bool failed_;
|
|
|
|
bool useHWTransform_;
|
2016-03-20 15:33:34 +00:00
|
|
|
bool usesLighting_;
|
2015-10-10 14:41:19 +00:00
|
|
|
ShaderID id_;
|
|
|
|
};
|
|
|
|
|
2016-01-09 20:19:18 +00:00
|
|
|
class VulkanPushBuffer;
|
|
|
|
|
2017-01-23 19:48:23 +00:00
|
|
|
class ShaderManagerVulkan : public ShaderManagerCommon {
|
2015-10-10 14:41:19 +00:00
|
|
|
public:
|
2016-01-05 20:18:43 +00:00
|
|
|
ShaderManagerVulkan(VulkanContext *vulkan);
|
2015-10-10 14:41:19 +00:00
|
|
|
~ShaderManagerVulkan();
|
|
|
|
|
2016-10-09 18:17:03 +00:00
|
|
|
void DeviceRestore(VulkanContext *vulkan);
|
|
|
|
|
2016-01-09 10:07:14 +00:00
|
|
|
void GetShaders(int prim, u32 vertType, VulkanVertexShader **vshader, VulkanFragmentShader **fshader, bool useHWTransform);
|
2016-01-10 13:24:10 +00:00
|
|
|
void ClearShaders();
|
2015-10-10 14:41:19 +00:00
|
|
|
void DirtyShader();
|
|
|
|
void DirtyLastShader();
|
|
|
|
|
2016-01-05 20:18:43 +00:00
|
|
|
int GetNumVertexShaders() const { return (int)vsCache_.size(); }
|
|
|
|
int GetNumFragmentShaders() const { return (int)fsCache_.size(); }
|
2015-10-10 14:41:19 +00:00
|
|
|
|
|
|
|
std::vector<std::string> DebugGetShaderIDs(DebugShaderType type);
|
|
|
|
std::string DebugGetShaderString(std::string id, DebugShaderType type, DebugShaderStringType stringType);
|
|
|
|
|
2017-01-24 19:00:03 +00:00
|
|
|
uint64_t UpdateUniforms();
|
2016-01-09 11:17:02 +00:00
|
|
|
|
2016-01-09 00:23:32 +00:00
|
|
|
// TODO: Avoid copying these buffers if same as last draw, can still point to it assuming we're still in the same pushbuffer.
|
|
|
|
// Applies dirty changes and copies the buffer.
|
|
|
|
bool IsBaseDirty() { return true; }
|
|
|
|
bool IsLightDirty() { return true; }
|
|
|
|
bool IsBoneDirty() { return true; }
|
|
|
|
|
2016-03-20 08:52:13 +00:00
|
|
|
uint32_t PushBaseBuffer(VulkanPushBuffer *dest, VkBuffer *buf);
|
|
|
|
uint32_t PushLightBuffer(VulkanPushBuffer *dest, VkBuffer *buf);
|
|
|
|
uint32_t PushBoneBuffer(VulkanPushBuffer *dest, VkBuffer *buf);
|
2016-01-09 00:23:32 +00:00
|
|
|
|
2015-10-10 14:41:19 +00:00
|
|
|
private:
|
2017-01-24 19:00:03 +00:00
|
|
|
void BaseUpdateUniforms(uint64_t dirtyUniforms);
|
|
|
|
void LightUpdateUniforms(uint64_t dirtyUniforms);
|
|
|
|
void BoneUpdateUniforms(uint64_t dirtyUniforms);
|
2015-10-10 14:41:19 +00:00
|
|
|
|
|
|
|
void Clear();
|
|
|
|
|
2016-01-05 20:18:43 +00:00
|
|
|
VulkanContext *vulkan_;
|
2015-10-10 14:41:19 +00:00
|
|
|
|
2016-01-09 11:17:02 +00:00
|
|
|
typedef std::map<ShaderID, VulkanFragmentShader *> FSCache;
|
|
|
|
FSCache fsCache_;
|
|
|
|
|
|
|
|
typedef std::map<ShaderID, VulkanVertexShader *> VSCache;
|
|
|
|
VSCache vsCache_;
|
|
|
|
|
2015-10-10 14:41:19 +00:00
|
|
|
char *codeBuffer_;
|
|
|
|
|
2017-01-23 16:32:18 +00:00
|
|
|
uint64_t uboAlignment_;
|
2015-10-10 14:41:19 +00:00
|
|
|
// Uniform block scratchpad. These (the relevant ones) are copied to the current pushbuffer at draw time.
|
2016-01-09 10:07:14 +00:00
|
|
|
UB_VS_FS_Base ub_base;
|
2015-10-10 14:41:19 +00:00
|
|
|
UB_VS_Lights ub_lights;
|
|
|
|
UB_VS_Bones ub_bones;
|
|
|
|
|
|
|
|
VulkanFragmentShader *lastFShader_;
|
|
|
|
VulkanVertexShader *lastVShader_;
|
|
|
|
|
|
|
|
ShaderID lastFSID_;
|
|
|
|
ShaderID lastVSID_;
|
|
|
|
};
|