-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCubismRenderer_CC.h
241 lines (204 loc) · 8.09 KB
/
CubismRenderer_CC.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#pragma once
#include "CubismRenderer.hpp"
#include "CubismFramework.hpp"
#include "Type/csmVector.hpp"
#include "Type/csmRectF.hpp"
#include "Math/CubismVector2.hpp"
#include "CubismOffscreenSurface_CC.h"
#include "cocos2d.h"
#define CSM_RENDERER_EXT ((CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) && !defined(CC_USE_GFX))
namespace Live2D { namespace Cubism {namespace Framework { namespace Rendering {
class CubismRenderer_CC;
class CubismClippingContext_CC;
class CubismDrawCommand_CC
{
cocos2d::CustomCommand _command;
csmUint8* _drawBuffer = nullptr;
csmSizeInt _vbSize = 0;
public:
CubismDrawCommand_CC();
~CubismDrawCommand_CC();
cocos2d::CustomCommand* GetCommand() { return &_command; }
void CreateVertexBuffer(csmSizeInt stride, csmSizeInt count);
void CreateIndexBuffer(csmSizeInt count);
void UpdateVertexBuffer(void* data, void* uvData, csmSizeInt count);
void UpdateIndexBuffer(void* data, csmSizeInt count);
};
class CubismClippingManager_CC
{
friend class CubismShader_CC;
friend class CubismRenderer_CC;
CubismRenderer::CubismTextureColor* GetChannelFlagAsColor(csmInt32 channelNo);
void CalcClippedDrawTotalBounds(
CubismModel& model, CubismClippingContext_CC* clippingContext);
CubismClippingManager_CC();
virtual ~CubismClippingManager_CC();
void Initialize(
CubismModel& model, csmInt32 drawableCount,
const csmInt32** drawableMasks, const csmInt32* drawableMaskCounts);
void SetupClippingContext(
CubismModel& model, CubismRenderer_CC* renderer,
const cocos2d::Viewport& lastViewport);
CubismClippingContext_CC* FindSameClip(
const csmInt32* drawableMasks, csmInt32 drawableMaskCounts) const;
void SetupLayoutBounds(csmInt32 usingClipCount) const;
csmVector<CubismClippingContext_CC*>* GetClippingContextListForDraw();
void SetClippingMaskBufferSize(csmFloat32 width, csmFloat32 height);
CubismVector2 GetClippingMaskBufferSize() const;
csmInt32 _currentFrameNo;
csmVector<CubismRenderer::CubismTextureColor*> _channelColors;
csmVector<CubismClippingContext_CC*> _clippingContextListForMask;
csmVector<CubismClippingContext_CC*> _clippingContextListForDraw;
CubismVector2 _clippingMaskBufferSize;
CubismMatrix44 _tmpMatrix;
CubismMatrix44 _tmpMatrixForMask;
CubismMatrix44 _tmpMatrixForDraw;
csmRectF _tmpBoundsOnModel;
};
class CubismClippingContext_CC
{
friend class CubismClippingManager_CC;
friend class CubismShader_CC;
friend class CubismRenderer_CC;
CubismClippingContext_CC(CubismClippingManager_CC* manager, CubismModel& model,
const csmInt32* clippingDrawableIndices, csmInt32 clipCount);
virtual ~CubismClippingContext_CC();
void AddClippedDrawable(csmInt32 drawableIndex);
CubismClippingManager_CC* GetClippingManager();
csmBool _isUsing;
const csmInt32* _clippingIdList;
csmInt32 _clippingIdCount;
csmInt32 _layoutChannelNo;
csmRectF* _layoutBounds;
csmRectF* _allClippedDrawRect;
CubismMatrix44 _matrixForMask;
CubismMatrix44 _matrixForDraw;
csmVector<csmInt32>* _clippedDrawableIndexList;
csmVector<CubismDrawCommand_CC*>* _clippingCommandBufferList;
CubismClippingManager_CC* _owner;
};
class CubismShader_CC
{
friend class CubismRenderer_CC;
static CubismShader_CC* GetInstance();
static void DeleteInstance();
struct CubismShaderSet
{
cocos2d::backend::Program* ShaderProgram;
int AttributePositionLocation;
int AttributeTexCoordLocation;
cocos2d::backend::UniformLocation UniformMatrixLocation;
cocos2d::backend::UniformLocation UniformClipMatrixLocation;
cocos2d::backend::UniformLocation SamplerTexture0Location;
cocos2d::backend::UniformLocation SamplerTexture1Location;
cocos2d::backend::UniformLocation UniformBaseColorLocation;
cocos2d::backend::UniformLocation UniformMultiplyColorLocation;
cocos2d::backend::UniformLocation UniformScreenColorLocation;
cocos2d::backend::UniformLocation UnifromChannelFlagLocation;
};
CubismShader_CC();
virtual ~CubismShader_CC();
void SetupShaderProgram(CubismRenderer_CC* renderer
, cocos2d::PipelineDescriptor* desc
, cocos2d::Texture2D* texture
, csmInt32 vertexCount, csmFloat32* vertexArray
, csmFloat32* uvArray, csmFloat32 opacity
, CubismRenderer::CubismBlendMode colorBlendMode
, const CubismRenderer::CubismTextureColor& baseColor
, CubismRenderer::CubismTextureColor multiplyColor
, CubismRenderer::CubismTextureColor screenColor
, csmBool isPremultipliedAlpha, CubismMatrix44& matrix4x4
, csmBool invertedMask);
void ReleaseShaderProgram();
void GenerateShaders();
static cocos2d::backend::Program* LoadShaderProgram(
const std::string& vertShaderSrc, const std::string& fragShaderSrc);
#if CSM_RENDERER_EXT
public:
static void SetExtShaderMode(csmBool extMode, csmBool extPAMode);
private:
static csmBool s_extMode;
static csmBool s_extPAMode;
#endif
csmVector<CubismShaderSet*> _shaderSets;
};
class CubismRendererProfile_CC
{
friend class CubismRenderer_CC;
CubismRendererProfile_CC();
virtual ~CubismRendererProfile_CC() {}
void Save();
void Restore();
cocos2d::Renderer* ccr = nullptr;
bool _lastScissorTest;
bool _lastStencilTest;
bool _lastDepthTest;
cocos2d::backend::CullMode _lastCullFace;
cocos2d::backend::Winding _lastWinding;
cocos2d::Viewport _lastViewport;
};
class CubismRenderer_CC : public CubismRenderer
{
friend class CubismRenderer;
friend class CubismClippingManager_CC;
friend class CubismShader_CC;
friend class CubismOffscreenFrame_CC;
public:
CubismRenderer_CC(const CubismRenderer_CC&) = delete;
CubismRenderer_CC& operator=(const CubismRenderer_CC&) = delete;
void Initialize(CubismModel* model) override;
void BindTexture(csmUint32 modelTextureNo, cocos2d::Texture2D* texture);
void SetClippingMaskBufferSize(csmFloat32 width, csmFloat32 height);
CubismVector2 GetClippingMaskBufferSize() const;
CubismOffscreenFrame_CC* GetOffscreenFrame() { return &_offscreenFrameBuffer; }
protected:
CubismRenderer_CC();
virtual ~CubismRenderer_CC();
void DoDrawModel() override;
void DrawMesh(
csmInt32 textureNo, csmInt32 indexCount, csmInt32 vertexCount
, csmUint16* indexArray, csmFloat32* vertexArray, csmFloat32* uvArray
, csmFloat32 opacity, CubismBlendMode colorBlendMode
, csmBool invertedMask) override;
void DrawMeshCC(CubismDrawCommand_CC* command,
csmInt32 textureNo, csmInt32 indexCount, csmInt32 vertexCount
, csmUint16* indexArray, csmFloat32* vertexArray, csmFloat32* uvArray
, const CubismTextureColor& multiplyColor, const CubismTextureColor& screenColor
, csmFloat32 opacity, CubismBlendMode colorBlendMode
, csmBool invertedMask);
#if CSM_RENDERER_EXT
public:
static void SetExtShaderMode(csmBool extMdoe, csmBool extPAMode = false);
static void ReloadShader();
#endif
private:
static void DoStaticRelease();
void PreDraw();
void PostDraw() {}
void SaveProfile() override;
void RestoreProfile() override;
void SetClippingContextBufferForMask(CubismClippingContext_CC* clip);
CubismClippingContext_CC* GetClippingContextBufferForMask() const;
void SetClippingContextBufferForDraw(CubismClippingContext_CC* clip);
CubismClippingContext_CC* GetClippingContextBufferForDraw() const;
protected:
void SetViewPort(const cocos2d::Viewport& value);
void SetViewPort(int x, int y, unsigned int w, unsigned int h);
void SetCullMode(cocos2d::backend::CullMode value);
void AddCallBack(const std::function<void()>& callback);
void ClearCommands();
private:
cocos2d::Renderer* ccr = nullptr;
std::vector<std::shared_ptr<cocos2d::RenderCommand>> cmds;
cocos2d::Map<csmInt32, cocos2d::Texture2D*> _textures;
csmVector<csmInt32> _sortedDrawableIndexList;
CubismRendererProfile_CC _rendererProfile;
CubismClippingManager_CC* _clippingManager;
CubismClippingContext_CC* _clippingContextBufferForMask;
CubismClippingContext_CC* _clippingContextBufferForDraw;
CubismMatrix44 _mvpMatrix;
CubismOffscreenFrame_CC _offscreenFrameBuffer;
cocos2d::backend::CullMode _lastCullMode;
csmVector<CubismDrawCommand_CC*> _drawableDrawCommandBuffer;
};
}}}}