Merge pull request #6445 from unknownbrackets/gpu-stencil

Optimize stencil uploading
This commit is contained in:
Henrik Rydgård committed 2014-06-28 11:03:01 +02:00
commit 1d90c06099
3 files changed
+119 -27

No files matched your search

+1 -1
View File
@@ -1005,7 +1005,7 @@ void FramebufferManager::DoSetRenderFrameBuffer() {
ClearBuffer();
if (useBufferedRendering_ && !updateVRAM_) {
gpu->PerformMemoryUpload(fb_address_mem, byteSize);
gpu->PerformStencilUpload(fb_address_mem, byteSize);
NotifyStencilUpload(fb_address_mem, byteSize, true);
// TODO: Is it worth trying to upload the depth buffer?
}
+1 -1
View File
@@ -226,7 +226,7 @@ public:
inline bool ShouldDownloadFramebuffer(const VirtualFramebuffer *vfb) const;
bool NotifyFramebufferCopy(u32 src, u32 dest, int size, bool isMemset = false);
bool NotifyStencilUpload(u32 addr, int size);
bool NotifyStencilUpload(u32 addr, int size, bool skipZero = false);
void DestroyFramebuf(VirtualFramebuffer *vfb);
void ResizeFramebufFBO(VirtualFramebuffer *vfb, u16 w, u16 h, bool force = false);
+117 -25
View File
@@ -50,7 +50,41 @@ static const char *stencil_vs =
" gl_Position = a_position;\n"
"}\n";
bool FramebufferManager::NotifyStencilUpload(u32 addr, int size) {
static u8 StencilBits5551(const u8 *ptr8, u32 numPixels) {
const u32 *ptr = (const u32 *)ptr8;
for (u32 i = 0; i < numPixels / 2; ++i) {
if (ptr[i] & 0x80008000) {
return 1;
}
}
return 0;
}
static u8 StencilBits4444(const u8 *ptr8, u32 numPixels) {
const u32 *ptr = (const u32 *)ptr8;
u32 bits = 0;
for (u32 i = 0; i < numPixels / 2; ++i) {
bits |= ptr[i];
}
return ((bits >> 12) & 0xF) | (bits >> 28);
}
static u8 StencilBits8888(const u8 *ptr8, u32 numPixels) {
const u32 *ptr = (const u32 *)ptr8;
u32 bits = 0;
for (u32 i = 0; i < numPixels; ++i) {
bits |= ptr[i];
}
return bits >> 24;
}
bool FramebufferManager::NotifyStencilUpload(u32 addr, int size, bool skipZero) {
if (!MayIntersectFramebuffer(addr)) {
return false;
}
@@ -66,6 +100,45 @@ bool FramebufferManager::NotifyStencilUpload(u32 addr, int size) {
return false;
}
int values = 0;
u8 usedBits = 0;
switch (dstBuffer->format) {
case GE_FORMAT_565:
// Well, this doesn't make much sense.
return false;
case GE_FORMAT_5551:
usedBits = StencilBits5551(Memory::GetPointer(addr), dstBuffer->fb_stride * dstBuffer->bufferHeight);
values = 2;
break;
case GE_FORMAT_4444:
usedBits = StencilBits4444(Memory::GetPointer(addr), dstBuffer->fb_stride * dstBuffer->bufferHeight);
values = 16;
break;
case GE_FORMAT_8888:
usedBits = StencilBits8888(Memory::GetPointer(addr), dstBuffer->fb_stride * dstBuffer->bufferHeight);
values = 256;
break;
case GE_FORMAT_INVALID:
// Impossible.
break;
}
if (usedBits == 0) {
if (skipZero) {
// Common when creating buffers, it's already 0. We're done.
return false;
}
// Let's not bother with the shader if it's just zero.
glstate.scissorTest.disable();
glstate.colorMask.set(GL_FALSE, GL_FALSE, GL_FALSE, GL_TRUE);
glClearColor(0, 0, 0, 0);
glClearStencil(0);
glClear(GL_STENCIL_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
return true;
}
GLSLProgram *program = 0;
if (!stencilUploadProgram_) {
std::string errorString;
@@ -91,31 +164,37 @@ bool FramebufferManager::NotifyStencilUpload(u32 addr, int size) {
glstate.stencilTest.enable();
glstate.stencilOp.set(GL_REPLACE, GL_REPLACE, GL_REPLACE);
// TODO: Doing it the slow way for now.
int values = 0;
bool useBlit = false;
bool useNV = false;
switch (dstBuffer->format) {
case GE_FORMAT_565:
// Well, this doesn't make much sense.
return false;
case GE_FORMAT_5551:
values = 2;
break;
case GE_FORMAT_4444:
values = 16;
break;
case GE_FORMAT_8888:
values = 256;
break;
case GE_FORMAT_INVALID:
// Impossible.
break;
#ifndef USING_GLES2
if (gl_extensions.FBO_ARB) {
useNV = false;
useBlit = true;
}
#else
if (gl_extensions.GLES3 || gl_extensions.NV_framebuffer_blit) {
useNV = !gl_extensions.GLES3;
useBlit = true;
}
#endif
if (dstBuffer->fbo) {
// Our fragment shader (and discard) is slow. Since the source is 1x, we can stencil to 1x.
// Then after we're done, we'll just blit it across and stretch it there.
if (dstBuffer->bufferWidth == dstBuffer->renderWidth || !dstBuffer->fbo) {
useBlit = false;
}
u16 w = useBlit ? dstBuffer->bufferWidth : dstBuffer->renderWidth;
u16 h = useBlit ? dstBuffer->bufferHeight : dstBuffer->renderHeight;
FBO *blitFBO = NULL;
if (useBlit) {
blitFBO = GetTempFBO(w, h, FBO_8888);
fbo_bind_as_render_target(blitFBO);
} else if (dstBuffer->fbo) {
fbo_bind_as_render_target(dstBuffer->fbo);
}
glViewport(0, 0, dstBuffer->renderWidth, dstBuffer->renderHeight);
glViewport(0, 0, w, h);
glClearStencil(0);
glClear(GL_STENCIL_BUFFER_BIT);
@@ -124,6 +203,10 @@ bool FramebufferManager::NotifyStencilUpload(u32 addr, int size) {
GLint u_stencilValue = glsl_uniform_loc(stencilUploadProgram_, "u_stencilValue");
for (int i = 1; i < values; i += i) {
if (!(usedBits & i)) {
// It's already zero, let's skip it.
continue;
}
// DrawActiveTexture unbinds it, so rebind here before setting uniforms.
glsl_bind(stencilUploadProgram_);
if (dstBuffer->format == GE_FORMAT_4444) {
@@ -140,11 +223,20 @@ bool FramebufferManager::NotifyStencilUpload(u32 addr, int size) {
}
glStencilMask(0xFF);
if (currentRenderVfb_) {
RebindFramebuffer();
} else {
fbo_unbind();
if (useBlit) {
fbo_bind_as_render_target(dstBuffer->fbo);
fbo_bind_for_read(blitFBO);
if (!useNV) {
glBlitFramebuffer(0, 0, w, h, 0, 0, dstBuffer->renderWidth, dstBuffer->renderHeight, GL_STENCIL_BUFFER_BIT, GL_NEAREST);
} else {
#if defined(USING_GLES2) && defined(ANDROID) // We only support this extension on Android, it's not even available on PC.
glBlitFramebufferNV(0, 0, w, h, 0, 0, dstBuffer->renderWidth, dstBuffer->renderHeight, GL_STENCIL_BUFFER_BIT, GL_NEAREST);
#endif // defined(USING_GLES2) && defined(ANDROID)
}
}
fbo_unbind();
RebindFramebuffer();
glstate.viewport.restore();
return true;
}