~ppsspp/ppsspp/ppsspp_1.3.0

« back to all changes in this revision

Viewing changes to GPU/GLES/ShaderManager.h

  • Committer: Sérgio Benjamim
  • Date: 2017-01-02 00:12:05 UTC
  • Revision ID: sergio_br2@yahoo.com.br-20170102001205-cxbta9za203nmjwm
1.3.0 source (from ppsspp_1.3.0-r160.p5.l1762.a165.t83~56~ubuntu16.04.1.tar.xz).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 "base/basictypes.h"
 
21
#include "Globals.h"
 
22
#include <map>
 
23
 
 
24
#include "GPU/Common/ShaderCommon.h"
 
25
#include "GPU/Common/ShaderId.h"
 
26
#include "GPU/GLES/VertexShaderGenerator.h"
 
27
#include "GPU/GLES/FragmentShaderGenerator.h"
 
28
 
 
29
class Shader;
 
30
 
 
31
// Pre-fetched attrs and uniforms
 
32
enum {
 
33
        ATTR_POSITION = 0,
 
34
        ATTR_TEXCOORD = 1,
 
35
        ATTR_NORMAL = 2,
 
36
        ATTR_W1 = 3,
 
37
        ATTR_W2 = 4,
 
38
        ATTR_COLOR0 = 5,
 
39
        ATTR_COLOR1 = 6,
 
40
 
 
41
        ATTR_COUNT,
 
42
};
 
43
 
 
44
class LinkedShader {
 
45
public:
 
46
        LinkedShader(ShaderID VSID, Shader *vs, ShaderID FSID, Shader *fs, bool useHWTransform);
 
47
        ~LinkedShader();
 
48
 
 
49
        void use(const ShaderID &VSID, LinkedShader *previous);
 
50
        void stop();
 
51
        void UpdateUniforms(u32 vertType, const ShaderID &VSID);
 
52
 
 
53
        Shader *vs_;
 
54
        // Set to false if the VS failed, happens on Mali-400 a lot for complex shaders.
 
55
        bool useHWTransform_;
 
56
 
 
57
        uint32_t program;
 
58
        u32 availableUniforms;
 
59
        u32 dirtyUniforms;
 
60
 
 
61
        // Present attributes in the shader.
 
62
        int attrMask;  // 1 << ATTR_ ... or-ed together.
 
63
 
 
64
        int u_stencilReplaceValue;
 
65
        int u_tex;
 
66
        int u_proj;
 
67
        int u_proj_through;
 
68
        int u_texenv;
 
69
        int u_view;
 
70
        int u_texmtx;
 
71
        int u_world;
 
72
        int u_depthRange;   // x,y = viewport xscale/xcenter. z,w=clipping minz/maxz (?)
 
73
 
 
74
#ifdef USE_BONE_ARRAY
 
75
        int u_bone;  // array, size is numBones
 
76
#else
 
77
        int u_bone[8];
 
78
#endif
 
79
        int numBones;
 
80
 
 
81
        // Shader blending.
 
82
        int u_fbotex;
 
83
        int u_blendFixA;
 
84
        int u_blendFixB;
 
85
        int u_fbotexSize;
 
86
 
 
87
        // Fragment processing inputs
 
88
        int u_alphacolorref;
 
89
        int u_alphacolormask;
 
90
        int u_testtex;
 
91
        int u_fogcolor;
 
92
        int u_fogcoef;
 
93
 
 
94
        // Texturing
 
95
        int u_uvscaleoffset;
 
96
        int u_texclamp;
 
97
        int u_texclampoff;
 
98
 
 
99
        // Lighting
 
100
        int u_ambient;
 
101
        int u_matambientalpha;
 
102
        int u_matdiffuse;
 
103
        int u_matspecular;
 
104
        int u_matemissive;
 
105
        int u_lightpos[4];
 
106
        int u_lightdir[4];
 
107
        int u_lightatt[4];  // attenuation
 
108
        int u_lightangle[4]; // spotlight cone angle (cosine)
 
109
        int u_lightspotCoef[4]; // spotlight dropoff
 
110
        int u_lightdiffuse[4];  // each light consist of vec4[3]
 
111
        int u_lightspecular[4];  // attenuation
 
112
        int u_lightambient[4];  // attenuation
 
113
};
 
114
 
 
115
enum {
 
116
        DIRTY_PROJMATRIX = (1 << 0),
 
117
        DIRTY_PROJTHROUGHMATRIX = (1 << 1),
 
118
        DIRTY_FOGCOLOR = (1 << 2),
 
119
        DIRTY_FOGCOEF = (1 << 3),
 
120
        DIRTY_TEXENV = (1 << 4),
 
121
        DIRTY_ALPHACOLORREF = (1 << 5),
 
122
 
 
123
        // 1 << 6 is free! Wait, not anymore...
 
124
        DIRTY_STENCILREPLACEVALUE = (1 << 6),
 
125
 
 
126
        DIRTY_ALPHACOLORMASK = (1 << 7),
 
127
        DIRTY_LIGHT0 = (1 << 8),
 
128
        DIRTY_LIGHT1 = (1 << 9),
 
129
        DIRTY_LIGHT2 = (1 << 10),
 
130
        DIRTY_LIGHT3 = (1 << 11),
 
131
 
 
132
        DIRTY_MATDIFFUSE = (1 << 12),
 
133
        DIRTY_MATSPECULAR = (1 << 13),
 
134
        DIRTY_MATEMISSIVE = (1 << 14),
 
135
        DIRTY_AMBIENT = (1 << 15),
 
136
        DIRTY_MATAMBIENTALPHA = (1 << 16),
 
137
 
 
138
        DIRTY_SHADERBLEND = (1 << 17),  // Used only for in-shader blending.
 
139
 
 
140
        DIRTY_UVSCALEOFFSET = (1 << 18),  // this will be dirtied ALL THE TIME... maybe we'll need to do "last value with this shader compares"
 
141
 
 
142
        // Texclamp is fairly rare so let's share it's bit with DIRTY_DEPTHRANGE.
 
143
        DIRTY_TEXCLAMP = (1 << 19),
 
144
        DIRTY_DEPTHRANGE = (1 << 19),
 
145
 
 
146
        DIRTY_WORLDMATRIX = (1 << 21),
 
147
        DIRTY_VIEWMATRIX = (1 << 22),  // Maybe we'll fold this into projmatrix eventually
 
148
        DIRTY_TEXMATRIX = (1 << 23),
 
149
        DIRTY_BONEMATRIX0 = (1 << 24),
 
150
        DIRTY_BONEMATRIX1 = (1 << 25),
 
151
        DIRTY_BONEMATRIX2 = (1 << 26),
 
152
        DIRTY_BONEMATRIX3 = (1 << 27),
 
153
        DIRTY_BONEMATRIX4 = (1 << 28),
 
154
        DIRTY_BONEMATRIX5 = (1 << 29),
 
155
        DIRTY_BONEMATRIX6 = (1 << 30),
 
156
        DIRTY_BONEMATRIX7 = (1 << 31),
 
157
 
 
158
        DIRTY_ALL = 0xFFFFFFFF
 
159
};
 
160
 
 
161
// Real public interface
 
162
 
 
163
class Shader {
 
164
public:
 
165
        Shader(const char *code, uint32_t glShaderType, bool useHWTransform);
 
166
        ~Shader();
 
167
        uint32_t shader;
 
168
 
 
169
        bool Failed() const { return failed_; }
 
170
        bool UseHWTransform() const { return useHWTransform_; } // only relevant for vtx shaders
 
171
 
 
172
        std::string GetShaderString(DebugShaderStringType type, ShaderID id) const;
 
173
 
 
174
private:
 
175
        std::string source_;
 
176
        bool failed_;
 
177
        bool useHWTransform_;
 
178
        bool isFragment_;
 
179
};
 
180
 
 
181
class ShaderManager {
 
182
public:
 
183
        ShaderManager();
 
184
        ~ShaderManager();
 
185
 
 
186
        void ClearCache(bool deleteThem);  // TODO: deleteThem currently not respected
 
187
 
 
188
        // This is the old ApplyShader split into two parts, because of annoying information dependencies.
 
189
        // If you call ApplyVertexShader, you MUST call ApplyFragmentShader soon afterwards.
 
190
        Shader *ApplyVertexShader(int prim, u32 vertType, ShaderID *VSID);
 
191
        LinkedShader *ApplyFragmentShader(ShaderID VSID, Shader *vs, u32 vertType, int prim);
 
192
 
 
193
        void DirtyShader();
 
194
        void DirtyUniform(u32 what) {
 
195
                globalDirty_ |= what;
 
196
        }
 
197
        void DirtyLastShader();  // disables vertex arrays
 
198
 
 
199
        int NumVertexShaders() const { return (int)vsCache_.size(); }
 
200
        int NumFragmentShaders() const { return (int)fsCache_.size(); }
 
201
        int NumPrograms() const { return (int)linkedShaderCache_.size(); }
 
202
 
 
203
        std::vector<std::string> DebugGetShaderIDs(DebugShaderType type);
 
204
        std::string DebugGetShaderString(std::string id, DebugShaderType type, DebugShaderStringType stringType);
 
205
 
 
206
        void LoadAndPrecompile(const std::string &filename);
 
207
        void Save(const std::string &filename);
 
208
 
 
209
private:
 
210
        void Clear();
 
211
        Shader *CompileFragmentShader(ShaderID id);
 
212
        Shader *CompileVertexShader(ShaderID id);
 
213
 
 
214
        struct LinkedShaderCacheEntry {
 
215
                LinkedShaderCacheEntry(Shader *vs_, Shader *fs_, LinkedShader *ls_)
 
216
                        : vs(vs_), fs(fs_), ls(ls_) { }
 
217
 
 
218
                Shader *vs;
 
219
                Shader *fs;
 
220
                LinkedShader *ls;
 
221
        };
 
222
        typedef std::vector<LinkedShaderCacheEntry> LinkedShaderCache;
 
223
 
 
224
        LinkedShaderCache linkedShaderCache_;
 
225
 
 
226
        bool lastVShaderSame_;
 
227
 
 
228
        ShaderID lastFSID_;
 
229
        ShaderID lastVSID_;
 
230
 
 
231
        LinkedShader *lastShader_;
 
232
        u32 globalDirty_;
 
233
        u32 shaderSwitchDirty_;
 
234
        char *codeBuffer_;
 
235
 
 
236
        typedef std::map<ShaderID, Shader *> FSCache;
 
237
        FSCache fsCache_;
 
238
 
 
239
        typedef std::map<ShaderID, Shader *> VSCache;
 
240
        VSCache vsCache_;
 
241
 
 
242
        bool diskCacheDirty_;
 
243
};