Add depth-stencil textures and attachments (#77)

Related: #29
This commit is contained in:
Kai Ninomiya
2017-07-17 12:03:16 -04:00
committed by GitHub
parent 28684d93ed
commit fec8c58a97
24 changed files with 227 additions and 194 deletions

View File

@@ -103,7 +103,6 @@ namespace opengl {
auto* device = ToBackend(GetDevice());
const auto& info = currentRenderPass->GetSubpassInfo(currentSubpass);
bool usingBackbuffer = false; // HACK(kainino@chromium.org): workaround for not having depth attachments
for (uint32_t index = 0; index < info.colorAttachments.size(); ++index) {
uint32_t attachment = info.colorAttachments[index];
@@ -115,16 +114,35 @@ namespace opengl {
texture = ToBackend(textureView->GetTexture())->GetHandle();
} else {
texture = device->GetCurrentTexture();
usingBackbuffer = true;
}
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + index,
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER,
GL_COLOR_ATTACHMENT0 + index,
GL_TEXTURE_2D, texture, 0);
}
// TODO(kainino@chromium.org): load depth attachment from subpass
if (usingBackbuffer) {
GLuint texture = device->GetCurrentDepthTexture();
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
GL_TEXTURE_2D, texture, 0);
if (info.depthStencilAttachmentSet) {
uint32_t attachment = info.depthStencilAttachment;
auto textureView = currentFramebuffer->GetTextureView(attachment);
GLuint texture = ToBackend(textureView->GetTexture())->GetHandle();
nxt::TextureFormat format = textureView->GetTexture()->GetFormat();
GLenum glAttachment = 0;
if (TextureFormatHasDepth(format)) {
if (TextureFormatHasStencil(format)) {
glAttachment = GL_DEPTH_STENCIL_ATTACHMENT;
} else {
glAttachment = GL_DEPTH_ATTACHMENT;
}
} else {
glAttachment = GL_STENCIL_ATTACHMENT;
}
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER,
glAttachment, GL_TEXTURE_2D, texture, 0);
// Load action
glClearStencil(0);
glClearDepth(1.0);
glClear(GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
}
}
break;

View File

@@ -111,9 +111,7 @@ namespace opengl {
void Device::HACKCLEAR() {
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, backFBO);
glClearColor(0, 0, 0, 1);
glStencilMask(0xff);
glClearStencil(0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
glClear(GL_COLOR_BUFFER_BIT);
}
void Device::InitBackbuffer() {
@@ -121,16 +119,10 @@ namespace opengl {
glBindTexture(GL_TEXTURE_2D, backTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 640, 480, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
glGenTextures(1, &backDepthTexture);
glBindTexture(GL_TEXTURE_2D, backDepthTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH24_STENCIL8, 640, 480, 0, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, nullptr);
glGenFramebuffers(1, &backFBO);
glBindFramebuffer(GL_READ_FRAMEBUFFER, backFBO);
glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
GL_TEXTURE_2D, backTexture, 0);
glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
GL_TEXTURE_2D, backDepthTexture, 0);
HACKCLEAR();
}
@@ -146,10 +138,6 @@ namespace opengl {
return backTexture;
}
GLuint Device::GetCurrentDepthTexture() {
return backDepthTexture;
}
// Bind Group
BindGroup::BindGroup(BindGroupBuilder* builder)

View File

@@ -103,12 +103,10 @@ namespace opengl {
void InitBackbuffer();
void CommitBackbuffer();
GLuint GetCurrentTexture();
GLuint GetCurrentDepthTexture();
private:
GLuint backFBO = 0;
GLuint backTexture = 0;
GLuint backDepthTexture = 0;
};
class BindGroup : public BindGroupBase {

View File

@@ -37,6 +37,8 @@ namespace opengl {
switch (format) {
case nxt::TextureFormat::R8G8B8A8Unorm:
return {GL_RGBA8, GL_RGBA, GL_UNSIGNED_BYTE};
case nxt::TextureFormat::D32FloatS8Uint:
return {GL_DEPTH32F_STENCIL8, GL_DEPTH_STENCIL, GL_FLOAT_32_UNSIGNED_INT_24_8_REV};
default:
UNREACHABLE();
}