Skip to content

Commit 96765e4

Browse files
committed
Latte: Refactor clear code
1 parent 111637a commit 96765e4

File tree

1 file changed

+83
-92
lines changed

1 file changed

+83
-92
lines changed

src/Cafe/OS/libs/gx2/GX2_Blit.cpp

Lines changed: 83 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -82,35 +82,89 @@ namespace GX2
8282
}
8383
}
8484

85-
void GX2ClearColor(GX2ColorBuffer* colorBuffer, float r, float g, float b, float a)
85+
void SubmitHLEClear(GX2ColorBuffer* colorBuffer, float colorRGBA[4], GX2DepthBuffer* depthBuffer, float depthClearValue, uint8 stencilClearValue, bool clearColor, bool clearDepth, bool clearStencil)
8686
{
8787
GX2ReserveCmdSpace(50);
88+
uint32 hleClearFlags = 0;
89+
if (clearColor)
90+
hleClearFlags |= 1;
91+
if (clearDepth)
92+
hleClearFlags |= 2;
93+
if (clearStencil)
94+
hleClearFlags |= 4;
95+
// color buffer
96+
MPTR colorPhysAddr = MPTR_NULL;
97+
uint32 colorFormat = 0;
98+
uint32 colorTileMode = 0;
99+
uint32 colorWidth = 0;
100+
uint32 colorHeight = 0;
101+
uint32 colorPitch = 0;
102+
uint32 colorFirstSlice = 0;
103+
uint32 colorNumSlices = 0;
104+
if (colorBuffer != nullptr)
105+
{
106+
colorPhysAddr = memory_virtualToPhysical(colorBuffer->surface.imagePtr);
107+
colorFormat = (uint32)colorBuffer->surface.format.value();
108+
colorTileMode = (uint32)colorBuffer->surface.tileMode.value();
109+
colorWidth = colorBuffer->surface.width;
110+
colorHeight = colorBuffer->surface.height;
111+
colorPitch = colorBuffer->surface.pitch;
112+
colorFirstSlice = _swapEndianU32(colorBuffer->viewFirstSlice);
113+
colorNumSlices = _swapEndianU32(colorBuffer->viewNumSlices);
114+
}
115+
// depth buffer
116+
MPTR depthPhysAddr = MPTR_NULL;
117+
uint32 depthFormat = 0;
118+
uint32 depthTileMode = 0;
119+
uint32 depthWidth = 0;
120+
uint32 depthHeight = 0;
121+
uint32 depthPitch = 0;
122+
uint32 depthFirstSlice = 0;
123+
uint32 depthNumSlices = 0;
124+
if (depthBuffer != nullptr)
125+
{
126+
depthPhysAddr = memory_virtualToPhysical(depthBuffer->surface.imagePtr);
127+
depthFormat = (uint32)depthBuffer->surface.format.value();
128+
depthTileMode = (uint32)depthBuffer->surface.tileMode.value();
129+
depthWidth = depthBuffer->surface.width;
130+
depthHeight = depthBuffer->surface.height;
131+
depthPitch = depthBuffer->surface.pitch;
132+
depthFirstSlice = _swapEndianU32(depthBuffer->viewFirstSlice);
133+
depthNumSlices = _swapEndianU32(depthBuffer->viewNumSlices);
134+
}
135+
136+
gx2WriteGather_submit(pm4HeaderType3(IT_HLE_CLEAR_COLOR_DEPTH_STENCIL, 23),
137+
hleClearFlags,
138+
colorPhysAddr,
139+
colorFormat,
140+
colorTileMode,
141+
colorWidth,
142+
colorHeight,
143+
colorPitch,
144+
colorFirstSlice,
145+
colorNumSlices,
146+
depthPhysAddr,
147+
depthFormat,
148+
depthTileMode,
149+
depthWidth,
150+
depthHeight,
151+
depthPitch,
152+
depthFirstSlice,
153+
depthNumSlices,
154+
(uint32)(colorRGBA[0] * 255.0f),
155+
(uint32)(colorRGBA[1] * 255.0f),
156+
(uint32)(colorRGBA[2] * 255.0f),
157+
(uint32)(colorRGBA[3] * 255.0f),
158+
*(uint32*)&depthClearValue,
159+
stencilClearValue&0xFF);
160+
}
161+
162+
void GX2ClearColor(GX2ColorBuffer* colorBuffer, float r, float g, float b, float a)
163+
{
88164
if ((colorBuffer->surface.resFlag & GX2_RESFLAG_USAGE_COLOR_BUFFER) != 0)
89165
{
90-
gx2WriteGather_submitU32AsBE(pm4HeaderType3(IT_HLE_CLEAR_COLOR_DEPTH_STENCIL, 23));
91-
gx2WriteGather_submitU32AsBE(1); // color (1)
92-
gx2WriteGather_submitU32AsBE(memory_virtualToPhysical(colorBuffer->surface.imagePtr));
93-
gx2WriteGather_submitU32AsBE((uint32)colorBuffer->surface.format.value());
94-
gx2WriteGather_submitU32AsBE((uint32)colorBuffer->surface.tileMode.value());
95-
gx2WriteGather_submitU32AsBE(colorBuffer->surface.width);
96-
gx2WriteGather_submitU32AsBE(colorBuffer->surface.height);
97-
gx2WriteGather_submitU32AsBE(colorBuffer->surface.pitch);
98-
gx2WriteGather_submitU32AsBE(_swapEndianU32(colorBuffer->viewFirstSlice));
99-
gx2WriteGather_submitU32AsBE(_swapEndianU32(colorBuffer->viewNumSlices));
100-
gx2WriteGather_submitU32AsBE(MPTR_NULL);
101-
gx2WriteGather_submitU32AsBE(0); // depth buffer format
102-
gx2WriteGather_submitU32AsBE(0); // tilemode for depth buffer
103-
gx2WriteGather_submitU32AsBE(0);
104-
gx2WriteGather_submitU32AsBE(0);
105-
gx2WriteGather_submitU32AsBE(0);
106-
gx2WriteGather_submitU32AsBE(0);
107-
gx2WriteGather_submitU32AsBE(0);
108-
gx2WriteGather_submitU32AsBE((uint32)(r * 255.0f));
109-
gx2WriteGather_submitU32AsBE((uint32)(g * 255.0f));
110-
gx2WriteGather_submitU32AsBE((uint32)(b * 255.0f));
111-
gx2WriteGather_submitU32AsBE((uint32)(a * 255.0f));
112-
gx2WriteGather_submitU32AsBE(0); // clear depth
113-
gx2WriteGather_submitU32AsBE(0); // clear stencil
166+
float colorRGBA[4] = { r, g, b, a };
167+
SubmitHLEClear(colorBuffer, colorRGBA, nullptr, 0.0f, 0, true, false, false);
114168
}
115169
else
116170
{
@@ -120,7 +174,6 @@ namespace GX2
120174

121175
void GX2ClearBuffersEx(GX2ColorBuffer* colorBuffer, GX2DepthBuffer* depthBuffer, float r, float g, float b, float a, float depthClearValue, uint8 stencilClearValue, GX2ClearFlags clearFlags)
122176
{
123-
GX2ReserveCmdSpace(50);
124177
_updateDepthStencilClearRegs(depthClearValue, stencilClearValue, clearFlags);
125178

126179
uint32 hleClearFlags = 0;
@@ -130,42 +183,13 @@ namespace GX2
130183
hleClearFlags |= 4;
131184
hleClearFlags |= 1;
132185

133-
// send command to clear color, depth and stencil
134-
if (_swapEndianU32(colorBuffer->viewFirstSlice) != 0)
135-
debugBreakpoint();
136-
gx2WriteGather_submitU32AsBE(pm4HeaderType3(IT_HLE_CLEAR_COLOR_DEPTH_STENCIL, 23));
137-
gx2WriteGather_submitU32AsBE(hleClearFlags); // color (1), depth (2), stencil (4)
138-
gx2WriteGather_submitU32AsBE(memory_virtualToPhysical(colorBuffer->surface.imagePtr));
139-
gx2WriteGather_submitU32AsBE((uint32)colorBuffer->surface.format.value());
140-
gx2WriteGather_submitU32AsBE((uint32)colorBuffer->surface.tileMode.value());
141-
gx2WriteGather_submitU32AsBE((uint32)colorBuffer->surface.width);
142-
gx2WriteGather_submitU32AsBE((uint32)colorBuffer->surface.height);
143-
gx2WriteGather_submitU32AsBE((uint32)colorBuffer->surface.pitch);
144-
gx2WriteGather_submitU32AsBE(_swapEndianU32(colorBuffer->viewFirstSlice));
145-
gx2WriteGather_submitU32AsBE(_swapEndianU32(colorBuffer->viewNumSlices));
146-
gx2WriteGather_submitU32AsBE(memory_virtualToPhysical(depthBuffer->surface.imagePtr));
147-
gx2WriteGather_submitU32AsBE((uint32)depthBuffer->surface.format.value());
148-
gx2WriteGather_submitU32AsBE((uint32)depthBuffer->surface.tileMode.value());
149-
gx2WriteGather_submitU32AsBE((uint32)depthBuffer->surface.width);
150-
gx2WriteGather_submitU32AsBE((uint32)depthBuffer->surface.height);
151-
gx2WriteGather_submitU32AsBE((uint32)depthBuffer->surface.pitch);
152-
gx2WriteGather_submitU32AsBE(_swapEndianU32(depthBuffer->viewFirstSlice));
153-
gx2WriteGather_submitU32AsBE(_swapEndianU32(depthBuffer->viewNumSlices));
154-
155-
gx2WriteGather_submitU32AsBE((uint32)(r * 255.0f));
156-
gx2WriteGather_submitU32AsBE((uint32)(g * 255.0f));
157-
gx2WriteGather_submitU32AsBE((uint32)(b * 255.0f));
158-
gx2WriteGather_submitU32AsBE((uint32)(a * 255.0f));
159-
160-
gx2WriteGather_submitU32AsBE(*(uint32*)&depthClearValue); // clear depth
161-
gx2WriteGather_submitU32AsBE(stencilClearValue&0xFF); // clear stencil
186+
float colorRGBA[4] = { r, g, b, a };
187+
SubmitHLEClear(colorBuffer, colorRGBA, depthBuffer, depthClearValue, stencilClearValue, true, (clearFlags & GX2ClearFlags::CLEAR_DEPTH) != 0, (clearFlags & GX2ClearFlags::CLEAR_STENCIL) != 0);
162188
}
163189

164190
// always uses passed depthClearValue/stencilClearValue for clearing, even if clear flags dont specify value updates
165191
void GX2ClearDepthStencilEx(GX2DepthBuffer* depthBuffer, float depthClearValue, uint8 stencilClearValue, GX2ClearFlags clearFlags)
166192
{
167-
GX2ReserveCmdSpace(50);
168-
169193
if (!depthBuffer && (depthBuffer->surface.width == 0 || depthBuffer->surface.height == 0))
170194
{
171195
// Super Smash Bros tries to clear an uninitialized depth surface?
@@ -175,41 +199,8 @@ namespace GX2
175199

176200
_updateDepthStencilClearRegs(depthClearValue, stencilClearValue, clearFlags);
177201

178-
uint32 hleClearFlags = 0;
179-
if ((clearFlags & GX2ClearFlags::CLEAR_DEPTH) != 0)
180-
hleClearFlags |= 2;
181-
if ((clearFlags & GX2ClearFlags::CLEAR_STENCIL) != 0)
182-
hleClearFlags |= 4;
183-
184-
// send command to clear color, depth and stencil
185-
if (hleClearFlags != 0)
186-
{
187-
gx2WriteGather_submitU32AsBE(pm4HeaderType3(IT_HLE_CLEAR_COLOR_DEPTH_STENCIL, 23));
188-
gx2WriteGather_submitU32AsBE(hleClearFlags); // color (1), depth (2), stencil (4)
189-
gx2WriteGather_submitU32AsBE(MPTR_NULL);
190-
gx2WriteGather_submitU32AsBE(0); // format for color buffer
191-
gx2WriteGather_submitU32AsBE(0); // tilemode for color buffer
192-
gx2WriteGather_submitU32AsBE(0);
193-
gx2WriteGather_submitU32AsBE(0);
194-
gx2WriteGather_submitU32AsBE(0);
195-
gx2WriteGather_submitU32AsBE(0);
196-
gx2WriteGather_submitU32AsBE(0);
197-
gx2WriteGather_submitU32AsBE(memory_virtualToPhysical(depthBuffer->surface.imagePtr));
198-
gx2WriteGather_submitU32AsBE((uint32)depthBuffer->surface.format.value());
199-
gx2WriteGather_submitU32AsBE((uint32)depthBuffer->surface.tileMode.value());
200-
gx2WriteGather_submitU32AsBE((uint32)depthBuffer->surface.width);
201-
gx2WriteGather_submitU32AsBE((uint32)depthBuffer->surface.height);
202-
gx2WriteGather_submitU32AsBE((uint32)depthBuffer->surface.pitch);
203-
gx2WriteGather_submitU32AsBE(_swapEndianU32(depthBuffer->viewFirstSlice));
204-
gx2WriteGather_submitU32AsBE(_swapEndianU32(depthBuffer->viewNumSlices));
205-
gx2WriteGather_submitU32AsBE(0);
206-
gx2WriteGather_submitU32AsBE(0);
207-
gx2WriteGather_submitU32AsBE(0);
208-
gx2WriteGather_submitU32AsBE(0);
209-
210-
gx2WriteGather_submitU32AsBE(*(uint32*)&depthClearValue); // clear depth
211-
gx2WriteGather_submitU32AsBE(stencilClearValue & 0xFF); // clear stencil
212-
}
202+
float colorRGBA[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
203+
SubmitHLEClear(nullptr, colorRGBA, depthBuffer, depthClearValue, stencilClearValue, false, (clearFlags & GX2ClearFlags::CLEAR_DEPTH) != 0, (clearFlags & GX2ClearFlags::CLEAR_STENCIL) != 0);
213204
}
214205

215206
void GX2BlitInit()

0 commit comments

Comments
 (0)