1 // Copyright (c) 2012- 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 <map>
21 #include <cstdint>
22 
23 #include "Common/Common.h"
24 #include "GPU/Common/VertexShaderGenerator.h"
25 #include "GPU/Common/FragmentShaderGenerator.h"
26 #include "GPU/Common/ShaderCommon.h"
27 #include "GPU/Common/ShaderId.h"
28 #include "Common/Math/lin/matrix4x4.h"
29 
30 namespace DX9 {
31 
32 class PSShader;
33 class VSShader;
34 
35 // Real public interface
36 
37 class PSShader {
38 public:
39 	PSShader(LPDIRECT3DDEVICE9 device, FShaderID id, const char *code);
40 	~PSShader();
41 
source()42 	const std::string &source() const { return source_; }
43 
Failed()44 	bool Failed() const { return failed_; }
45 
46 	std::string GetShaderString(DebugShaderStringType type) const;
47 
48 	LPDIRECT3DPIXELSHADER9 shader = nullptr;
49 
50 protected:
51 	std::string source_;
52 	bool failed_ = false;
53 	FShaderID id_;
54 };
55 
56 class VSShader {
57 public:
58 	VSShader(LPDIRECT3DDEVICE9 device, VShaderID id, const char *code, bool useHWTransform);
59 	~VSShader();
60 
source()61 	const std::string &source() const { return source_; }
62 
Failed()63 	bool Failed() const { return failed_; }
UseHWTransform()64 	bool UseHWTransform() const { return useHWTransform_; }
65 
66 	std::string GetShaderString(DebugShaderStringType type) const;
67 
68 	LPDIRECT3DVERTEXSHADER9 shader = nullptr;
69 
70 protected:
71 	std::string source_;
72 	bool failed_ = false;
73 	bool useHWTransform_;
74 	VShaderID id_;
75 };
76 
77 class ShaderManagerDX9 : public ShaderManagerCommon {
78 public:
79 	ShaderManagerDX9(Draw::DrawContext *draw, LPDIRECT3DDEVICE9 device);
80 	~ShaderManagerDX9();
81 
82 	void ClearCache(bool deleteThem);  // TODO: deleteThem currently not respected
83 	VSShader *ApplyShader(bool useHWTransform, bool useHWTessellation, u32 vertType, bool weightsAsFloat);
84 	void DirtyShader();
85 	void DirtyLastShader() override;
86 
GetNumVertexShaders()87 	int GetNumVertexShaders() const { return (int)vsCache_.size(); }
GetNumFragmentShaders()88 	int GetNumFragmentShaders() const { return (int)fsCache_.size(); }
89 
90 	std::vector<std::string> DebugGetShaderIDs(DebugShaderType type);
91 	std::string DebugGetShaderString(std::string id, DebugShaderType type, DebugShaderStringType stringType);
92 
93 private:
94 	void PSUpdateUniforms(u64 dirtyUniforms);
95 	void VSUpdateUniforms(u64 dirtyUniforms);
96 	inline void PSSetColorUniform3Alpha255(int creg, u32 color, u8 alpha);
97 	inline void PSSetColorUniform3(int creg, u32 color);
98 	inline void PSSetFloat(int creg, float value);
99 	inline void PSSetFloatArray(int creg, const float *value, int count);
100 
101 	void VSSetMatrix4x3_3(int creg, const float *m4x3);
102 	inline void VSSetColorUniform3(int creg, u32 color);
103 	inline void VSSetColorUniform3ExtraFloat(int creg, u32 color, float extra);
104 	inline void VSSetColorUniform3Alpha(int creg, u32 color, u8 alpha);
105 	void VSSetMatrix(int creg, const float* pMatrix);
106 	void VSSetFloat(int creg, float value);
107 	void VSSetFloatArray(int creg, const float *value, int count);
108 	void VSSetFloat24Uniform3(int creg, const u32 data[3]);
109 	void VSSetFloatUniform4(int creg, float data[4]);
110 
111 	void Clear();
112 
113 	LPDIRECT3DDEVICE9 device_;
114 
115 	FShaderID lastFSID_;
116 	VShaderID lastVSID_;
117 
118 	char *codeBuffer_;
119 
120 	VSShader *lastVShader_ = nullptr;
121 	PSShader *lastPShader_ = nullptr;
122 
123 	typedef std::map<FShaderID, PSShader *> FSCache;
124 	FSCache fsCache_;
125 
126 	typedef std::map<VShaderID, VSShader *> VSCache;
127 	VSCache vsCache_;
128 };
129 
130 };
131