Add internal Format structure for texture formats

Texture formats have plenty of properties we'd like to query on them
like their texel size, whether they are compressend or if they are depth
stencil. Instead of making switch statements for each of these
properties, we store them in a new `Format` structure. Textures compute
their format only onces and then pass a const reference to it on
GetFormat().

This is in preparation of adding all WebGPU texture formats.

BUG=dawn:128

Change-Id: Iad2831cf16f14e1a1bfce2c10b22527fc982d1aa
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/8166
Commit-Queue: Corentin Wallez <cwallez@chromium.org>
Reviewed-by: Kai Ninomiya <kainino@chromium.org>
This commit is contained in:
Corentin Wallez
2019-06-21 10:16:15 +00:00
committed by Commit Bot service account
parent 4729b15365
commit a92f83b725
14 changed files with 297 additions and 323 deletions

View File

@@ -389,9 +389,8 @@ namespace dawn_native { namespace opengl {
gl.ActiveTexture(GL_TEXTURE0);
gl.BindTexture(target, texture->GetHandle());
gl.PixelStorei(
GL_UNPACK_ROW_LENGTH,
src.rowPitch / TextureFormatTexelBlockSizeInBytes(texture->GetFormat()));
gl.PixelStorei(GL_UNPACK_ROW_LENGTH,
src.rowPitch / texture->GetFormat().blockByteSize);
gl.PixelStorei(GL_UNPACK_IMAGE_HEIGHT, src.imageHeight);
switch (texture->GetDimension()) {
case dawn::TextureDimension::e2D:
@@ -452,9 +451,8 @@ namespace dawn_native { namespace opengl {
}
gl.BindBuffer(GL_PIXEL_PACK_BUFFER, buffer->GetHandle());
gl.PixelStorei(
GL_PACK_ROW_LENGTH,
dst.rowPitch / TextureFormatTexelBlockSizeInBytes(texture->GetFormat()));
gl.PixelStorei(GL_PACK_ROW_LENGTH,
dst.rowPitch / texture->GetFormat().blockByteSize);
gl.PixelStorei(GL_PACK_IMAGE_HEIGHT, dst.imageHeight);
ASSERT(copySize.depth == 1 && src.origin.z == 0);
void* offset = reinterpret_cast<void*>(static_cast<uintptr_t>(dst.offset));
@@ -582,7 +580,7 @@ namespace dawn_native { namespace opengl {
// TODO(kainino@chromium.org): the color clears (later in
// this function) may be undefined for non-normalized integer formats.
dawn::TextureFormat format = textureView->GetTexture()->GetFormat();
dawn::TextureFormat format = textureView->GetTexture()->GetFormat().format;
ASSERT(format == dawn::TextureFormat::RGBA8Unorm ||
format == dawn::TextureFormat::RG8Unorm ||
format == dawn::TextureFormat::R8Unorm ||
@@ -593,20 +591,25 @@ namespace dawn_native { namespace opengl {
if (renderPass->hasDepthStencilAttachment) {
TextureViewBase* textureView = renderPass->depthStencilAttachment.view.Get();
GLuint texture = ToBackend(textureView->GetTexture())->GetHandle();
dawn::TextureFormat format = textureView->GetTexture()->GetFormat();
const Format& format = textureView->GetTexture()->GetFormat();
// Attach depth/stencil buffer.
GLenum glAttachment = 0;
// TODO(kainino@chromium.org): it may be valid to just always use
// GL_DEPTH_STENCIL_ATTACHMENT here.
if (TextureFormatHasDepth(format)) {
if (TextureFormatHasStencil(format)) {
glAttachment = GL_DEPTH_STENCIL_ATTACHMENT;
} else {
switch (format.aspect) {
case Format::Aspect::Depth:
glAttachment = GL_DEPTH_ATTACHMENT;
}
} else {
glAttachment = GL_STENCIL_ATTACHMENT;
break;
case Format::Aspect::Stencil:
glAttachment = GL_STENCIL_ATTACHMENT;
break;
case Format::Aspect::DepthStencil:
glAttachment = GL_DEPTH_STENCIL_ATTACHMENT;
break;
default:
UNREACHABLE();
break;
}
GLenum target = ToBackend(textureView->GetTexture())->GetGLTarget();
@@ -614,7 +617,7 @@ namespace dawn_native { namespace opengl {
// TODO(kainino@chromium.org): the depth/stencil clears (later in
// this function) may be undefined for other texture formats.
ASSERT(format == dawn::TextureFormat::Depth24PlusStencil8);
ASSERT(format.format == dawn::TextureFormat::Depth24PlusStencil8);
}
}
@@ -639,20 +642,19 @@ namespace dawn_native { namespace opengl {
if (renderPass->hasDepthStencilAttachment) {
const auto& attachmentInfo = renderPass->depthStencilAttachment;
dawn::TextureFormat attachmentFormat =
attachmentInfo.view->GetTexture()->GetFormat();
const Format& attachmentFormat = attachmentInfo.view->GetTexture()->GetFormat();
// Load op - depth/stencil
bool doDepthClear = TextureFormatHasDepth(attachmentFormat) &&
bool doDepthClear = attachmentFormat.HasDepth() &&
(attachmentInfo.depthLoadOp == dawn::LoadOp::Clear);
bool doStencilClear = TextureFormatHasStencil(attachmentFormat) &&
bool doStencilClear = attachmentFormat.HasStencil() &&
(attachmentInfo.stencilLoadOp == dawn::LoadOp::Clear);
if (doDepthClear) {
gl.DepthMask(GL_TRUE);
}
if (doStencilClear) {
gl.StencilMask(GetStencilMaskFromStencilFormat(attachmentFormat));
gl.StencilMask(GetStencilMaskFromStencilFormat(attachmentFormat.format));
}
if (doDepthClear && doStencilClear) {

View File

@@ -102,7 +102,7 @@ namespace dawn_native { namespace opengl {
bool RequiresCreatingNewTextureView(const TextureBase* texture,
const TextureViewDescriptor* textureViewDescriptor) {
if (texture->GetFormat() != textureViewDescriptor->format) {
if (texture->GetFormat().format != textureViewDescriptor->format) {
return true;
}
@@ -139,7 +139,7 @@ namespace dawn_native { namespace opengl {
uint32_t arrayLayers = GetArrayLayers();
uint32_t sampleCount = GetSampleCount();
auto formatInfo = GetGLFormatInfo(GetFormat());
auto formatInfo = GetGLFormatInfo(GetFormat().format);
gl.BindTexture(mTarget, mHandle);
@@ -171,7 +171,7 @@ namespace dawn_native { namespace opengl {
if (GetDevice()->IsToggleEnabled(Toggle::NonzeroClearResourcesOnCreationForTesting)) {
static constexpr uint32_t MAX_TEXEL_SIZE = 16;
ASSERT(TextureFormatTexelBlockSizeInBytes(GetFormat()) <= MAX_TEXEL_SIZE);
ASSERT(GetFormat().blockByteSize <= MAX_TEXEL_SIZE);
GLubyte clearColor[MAX_TEXEL_SIZE];
std::fill(clearColor, clearColor + MAX_TEXEL_SIZE, 255);
@@ -208,7 +208,7 @@ namespace dawn_native { namespace opengl {
}
TextureFormatInfo Texture::GetGLFormat() const {
return GetGLFormatInfo(GetFormat());
return GetGLFormatInfo(GetFormat().format);
}
// TextureView