Commit ea71c6b6 by Shahbaz Youssefi Committed by Commit Bot

Vulkan: Emulate R32F images with R32UI

GL requires that imageAtomicExchange be supported for r32f formats. However VK_FORMAT_FEATURE_STORAGE_*_ATOMIC_BIT is nearly unsupported everywhere without some Vulkan extension that brings in unnecessary support. This GL feature is emulated by transforming the shader to use r32ui for all images that originally specified r32f. floatToUintBits and uintBitsToFloat is used to maintain correct usage of the image* builtin functions. Bug: angleproject:5535 Change-Id: Ie607089935d3283b3ffa054f4b4385b81fb8f53d Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/2635453 Commit-Queue: Shahbaz Youssefi <syoussefi@chromium.org> Reviewed-by: 's avatarJamie Madill <jmadill@chromium.org> Reviewed-by: 's avatarTim Van Patten <timvp@google.com>
parent a4d05638
......@@ -459,6 +459,14 @@ struct FeaturesVk : FeatureSetBase
"exposeNonConformantExtensionsAndVersions", FeatureCategory::VulkanWorkarounds,
"Expose GLES versions and extensions that are not conformant.", &members,
"http://anglebug.com/5375"};
// imageAtomicExchange is expected to work for r32f formats, but support for atomic operations
// for VK_FORMAT_R32_SFLOAT is rare. This support is emulated by using an r32ui format for such
// images instead.
Feature emulateR32fImageAtomicExchange = {
"emulateR32fImageAtomicExchange", FeatureCategory::VulkanWorkarounds,
"Emulate r32f images with r32ui to support imageAtomicExchange.", &members,
"http://anglebug.com/5535"};
};
inline FeaturesVk::FeaturesVk() = default;
......
......@@ -333,6 +333,8 @@ angle_translator_lib_vulkan_sources = [
"src/compiler/translator/tree_ops/vulkan/RewriteDfdy.h",
"src/compiler/translator/tree_ops/vulkan/RewriteInterpolateAtOffset.cpp",
"src/compiler/translator/tree_ops/vulkan/RewriteInterpolateAtOffset.h",
"src/compiler/translator/tree_ops/vulkan/RewriteR32fImages.cpp",
"src/compiler/translator/tree_ops/vulkan/RewriteR32fImages.h",
"src/compiler/translator/tree_ops/vulkan/RewriteStructSamplers.cpp",
"src/compiler/translator/tree_ops/vulkan/RewriteStructSamplers.h",
]
......
......@@ -25,6 +25,7 @@ constexpr const ImmutableString kMainName("main");
constexpr const ImmutableString kImageLoadName("imageLoad");
constexpr const ImmutableString kImageStoreName("imageStore");
constexpr const ImmutableString kImageSizeName("imageSize");
constexpr const ImmutableString kImageAtomicExchangeName("imageAtomicExchange");
constexpr const ImmutableString kAtomicCounterName("atomicCounter");
static const char kFunctionMangledNameSeparator = '(';
......@@ -214,7 +215,8 @@ bool TFunction::isMain() const
bool TFunction::isImageFunction() const
{
return symbolType() == SymbolType::BuiltIn &&
(name() == kImageSizeName || name() == kImageLoadName || name() == kImageStoreName);
(name() == kImageSizeName || name() == kImageLoadName || name() == kImageStoreName ||
name() == kImageAtomicExchangeName);
}
bool TFunction::isAtomicCounterFunction() const
......
......@@ -29,6 +29,7 @@
#include "compiler/translator/tree_ops/vulkan/RewriteCubeMapSamplersAs2DArray.h"
#include "compiler/translator/tree_ops/vulkan/RewriteDfdy.h"
#include "compiler/translator/tree_ops/vulkan/RewriteInterpolateAtOffset.h"
#include "compiler/translator/tree_ops/vulkan/RewriteR32fImages.h"
#include "compiler/translator/tree_ops/vulkan/RewriteStructSamplers.h"
#include "compiler/translator/tree_util/BuiltIn.h"
#include "compiler/translator/tree_util/DriverUniform.h"
......@@ -731,6 +732,7 @@ bool TranslatorVulkan::translateImpl(TIntermBlock *root,
// Write out default uniforms into a uniform block assigned to a specific set/binding.
int defaultUniformCount = 0;
int aggregateTypesUsedForUniforms = 0;
int r32fImageCount = 0;
int atomicCounterCount = 0;
for (const auto &uniform : getUniforms())
{
......@@ -744,6 +746,11 @@ bool TranslatorVulkan::translateImpl(TIntermBlock *root,
++aggregateTypesUsedForUniforms;
}
if (uniform.active && gl::IsImageType(uniform.type) && uniform.imageUnitFormat == GL_R32F)
{
++r32fImageCount;
}
if (uniform.active && gl::IsAtomicCounterType(uniform.type))
{
++atomicCounterCount;
......@@ -853,6 +860,14 @@ bool TranslatorVulkan::translateImpl(TIntermBlock *root,
driverUniforms->addGraphicsDriverUniformsToShader(root, &getSymbolTable());
}
if (r32fImageCount > 0)
{
if (!RewriteR32fImages(this, root, &getSymbolTable()))
{
return false;
}
}
if (atomicCounterCount > 0)
{
// ANGLEUniforms.acbBufferOffsets
......
......@@ -317,6 +317,7 @@ class MonomorphizeTraverser final : public TIntermTraverser
// subscripted (i.e. the function itself expects an array), or
// - The opaque uniform is an atomic counter
// - The opaque uniform is a samplerCube and ES2's cube sampling emulation is requested.
// - The opaque uniform is an image* with r32f format.
//
const TType &type = uniform->getType();
const bool isArrayOfArrayOfSamplerOrImage =
......@@ -326,11 +327,13 @@ class MonomorphizeTraverser final : public TIntermTraverser
const bool isSamplerCubeEmulation =
type.isSamplerCube() &&
(mCompileOptions & SH_EMULATE_SEAMFUL_CUBE_MAP_SAMPLING) != 0;
const bool isR32fImage =
type.isImage() && type.getLayoutQualifier().imageInternalFormat == EiifR32F;
if (!(isStructContainingSamplers ||
(isSamplerInStruct && isParameterArrayOfOpaqueType) ||
(isArrayOfArrayOfSamplerOrImage && isParameterArrayOfOpaqueType) ||
isAtomicCounter || isSamplerCubeEmulation))
isAtomicCounter || isSamplerCubeEmulation || isR32fImage))
{
continue;
}
......
......@@ -12,6 +12,7 @@
// - Partially subscripted array of array of images
// - Atomic counters
// - samplerCube variables when emulating ES2's cube map sampling
// - image* variables with r32f formats (to emulate imageAtomicExchange)
//
// This transformation basically duplicates such functions, removes the
// sampler/image/atomic_counter parameters and uses the opaque uniforms used by the caller.
......
......@@ -63,12 +63,6 @@ class RewriteExpressionTraverser final : public TIntermTraverser
bool visitBinary(Visit visit, TIntermBinary *node) override
{
// Only interested in opaque uniforms.
if (!IsOpaqueType(node->getType().getBasicType()))
{
return true;
}
TIntermTyped *rewritten =
RewriteArrayOfArraySubscriptExpression(mCompiler, node, mUniformMap);
if (rewritten == nullptr)
......@@ -130,7 +124,11 @@ TIntermTyped *RewriteArrayOfArraySubscriptExpression(TCompiler *compiler,
TIntermBinary *node,
const UniformMap &uniformMap)
{
ASSERT(IsOpaqueType(node->getType().getBasicType()));
// Only interested in opaque uniforms.
if (!IsOpaqueType(node->getType().getBasicType()))
{
return nullptr;
}
TIntermSymbol *opaqueUniform = nullptr;
......@@ -315,12 +313,6 @@ class RewriteArrayOfArrayOfOpaqueUniformsTraverser : public TIntermTraverser
// Same implementation as in RewriteExpressionTraverser. That traverser cannot replace root.
bool visitBinary(Visit visit, TIntermBinary *node) override
{
// Only interested in opaque uniforms.
if (!IsOpaqueType(node->getType().getBasicType()))
{
return true;
}
TIntermTyped *rewritten =
RewriteArrayOfArraySubscriptExpression(mCompiler, node, mUniformMap);
if (rewritten == nullptr)
......
......@@ -225,14 +225,14 @@ class RewriteAtomicCountersTraverser : public TIntermTraverser
void visitSymbol(TIntermSymbol *symbol) override
{
// Connot encounter the atomic counter symbol directly. It can only be used with functions,
// Cannot encounter the atomic counter symbol directly. It can only be used with functions,
// and therefore it's handled by visitAggregate.
ASSERT(!symbol->getType().isAtomicCounter());
}
bool visitBinary(Visit visit, TIntermBinary *node) override
{
// Connot encounter an atomic counter expression directly. It can only be used with
// Cannot encounter an atomic counter expression directly. It can only be used with
// functions, and therefore it's handled by visitAggregate.
ASSERT(!node->getType().isAtomicCounter());
return true;
......
//
// Copyright 2021 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// RewriteR32fImages: Change images qualified with r32f to use r32ui instead.
//
#include "compiler/translator/tree_ops/vulkan/RewriteR32fImages.h"
#include "compiler/translator/Compiler.h"
#include "compiler/translator/ImmutableStringBuilder.h"
#include "compiler/translator/StaticType.h"
#include "compiler/translator/SymbolTable.h"
#include "compiler/translator/tree_util/IntermNode_util.h"
#include "compiler/translator/tree_util/IntermTraverse.h"
#include "compiler/translator/tree_util/ReplaceVariable.h"
namespace sh
{
namespace
{
bool IsR32fImage(const TType &type)
{
return type.getQualifier() == EvqUniform && type.isImage() &&
type.getLayoutQualifier().imageInternalFormat == EiifR32F;
}
using ImageMap = angle::HashMap<const TVariable *, const TVariable *>;
TIntermTyped *RewriteBuiltinFunctionCall(TCompiler *compiler,
TSymbolTable *symbolTable,
TIntermAggregate *node,
const ImageMap &imageMap);
// Given an expression, this traverser calculates a new expression where builtin function calls to
// r32f images are replaced with ones to the mapped r32ui image. In particular, this is run on the
// right node of EOpIndexIndirect binary nodes, so that the expression in the index gets a chance to
// go through this transformation.
class RewriteExpressionTraverser final : public TIntermTraverser
{
public:
explicit RewriteExpressionTraverser(TCompiler *compiler,
TSymbolTable *symbolTable,
const ImageMap &imageMap)
: TIntermTraverser(true, false, false, symbolTable),
mCompiler(compiler),
mImageMap(imageMap)
{}
bool visitAggregate(Visit visit, TIntermAggregate *node) override
{
TIntermTyped *rewritten =
RewriteBuiltinFunctionCall(mCompiler, mSymbolTable, node, mImageMap);
if (rewritten == nullptr)
{
return true;
}
queueReplacement(rewritten, OriginalNode::IS_DROPPED);
// Don't iterate as the expression is rewritten.
return false;
}
private:
TCompiler *mCompiler;
const ImageMap &mImageMap;
};
// Rewrite the index of an EOpIndexIndirect expression as well as any arguments to the builtin
// function call.
TIntermTyped *RewriteExpression(TCompiler *compiler,
TSymbolTable *symbolTable,
TIntermTyped *expression,
const ImageMap &imageMap)
{
// Create a fake block to insert the node in. The root itself may need changing.
TIntermBlock block;
block.appendStatement(expression);
RewriteExpressionTraverser traverser(compiler, symbolTable, imageMap);
block.traverse(&traverser);
bool valid = traverser.updateTree(compiler, &block);
ASSERT(valid);
TIntermTyped *rewritten = block.getChildNode(0)->getAsTyped();
return rewritten;
}
// Given a builtin function call such as the following:
//
// imageLoad(expression, ...);
//
// expression is in the form of:
//
// - image uniform
// - image uniform array indexed with EOpIndexDirect or EOpIndexIndirect. Note that
// RewriteArrayOfArrayOfOpaqueUniforms has already ensured that the image array is
// single-dimension.
//
// The latter case (with EOpIndexIndirect) is not valid GLSL (up to GL_EXT_gpu_shader5), but if it
// were, the index itself could have contained an image builtin function call, so is recursively
// processed (in case supported in future). Additionally, the other builtin function arguments may
// need processing too.
//
// This function creates a similar expression where the image uniforms (of type r32f) are replaced
// with those of r32ui type.
//
TIntermTyped *RewriteBuiltinFunctionCall(TCompiler *compiler,
TSymbolTable *symbolTable,
TIntermAggregate *node,
const ImageMap &imageMap)
{
if (node->getOp() != EOpCallBuiltInFunction)
{
// AST functions don't require modification as r32f image function parameters are removed by
// MonomorphizeUnsupportedFunctionsInVulkanGLSL.
return nullptr;
}
// If it's an |image*| function, replace the function with an equivalent that uses an r32ui
// image.
if (!node->getFunction()->isImageFunction())
{
return nullptr;
}
TIntermSequence *arguments = node->getSequence();
TIntermTyped *imageExpression = (*arguments)[0]->getAsTyped();
ASSERT(imageExpression);
// Find the image uniform that's being indexed, if indexed.
TIntermBinary *asBinary = imageExpression->getAsBinaryNode();
TIntermSymbol *imageUniform = imageExpression->getAsSymbolNode();
if (asBinary)
{
ASSERT(asBinary->getOp() == EOpIndexDirect || asBinary->getOp() == EOpIndexIndirect);
imageUniform = asBinary->getLeft()->getAsSymbolNode();
}
ASSERT(imageUniform);
if (!IsR32fImage(imageUniform->getType()))
{
return nullptr;
}
ASSERT(imageMap.find(&imageUniform->variable()) != imageMap.end());
const TVariable *replacementImage = imageMap.at(&imageUniform->variable());
// Build the expression again, with the image uniform replaced. If index is dynamic,
// recursively process it.
TIntermTyped *replacementExpression = new TIntermSymbol(replacementImage);
// Index it, if indexed.
if (asBinary != nullptr)
{
TIntermTyped *index = asBinary->getRight();
switch (asBinary->getOp())
{
case EOpIndexDirect:
break;
case EOpIndexIndirect:
{
// Run RewriteExpressionTraverser on the index node. This case is currently
// impossible with known extensions.
UNREACHABLE();
index = RewriteExpression(compiler, symbolTable, index, imageMap);
break;
}
default:
UNREACHABLE();
break;
}
replacementExpression = new TIntermBinary(asBinary->getOp(), replacementExpression, index);
}
TIntermSequence substituteArguments;
substituteArguments.push_back(replacementExpression);
for (size_t argIndex = 1; argIndex < arguments->size(); ++argIndex)
{
TIntermTyped *arg = (*arguments)[argIndex]->getAsTyped();
// Run RewriteExpressionTraverser on the argument. It may itself be an expression with an
// r32f image that needs to be rewritten.
arg = RewriteExpression(compiler, symbolTable, arg, imageMap);
substituteArguments.push_back(arg);
}
const ImmutableString &functionName = node->getFunction()->name();
bool isImageAtomicExchange = functionName == "imageAtomicExchange";
bool isImageLoad = false;
if (functionName == "imageStore" || isImageAtomicExchange)
{
// The last parameter is float data, which should be changed to floatBitsToUint(data).
TIntermTyped *data = substituteArguments.back()->getAsTyped();
substituteArguments.back() = new TIntermUnary(EOpFloatBitsToUint, data, nullptr);
}
else if (functionName == "imageLoad")
{
isImageLoad = true;
}
else
{
// imageSize does not have any other arguments.
ASSERT(functionName == "imageSize");
ASSERT(arguments->size() == 1);
}
TIntermTyped *replacementCall =
CreateBuiltInFunctionCallNode(functionName.data(), &substituteArguments, *symbolTable, 310);
// If imageLoad or imageAtomicExchange, the result is now uint, which should be converted with
// uintBitsToFloat. With imageLoad, the alpha channel should always read 1.0 regardless.
if (isImageLoad || isImageAtomicExchange)
{
if (isImageLoad)
{
// imageLoad().rgb
replacementCall = new TIntermSwizzle(replacementCall, {0, 1, 2});
}
// uintBitsToFloat(imageLoad().rgb), or uintBitsToFloat(imageAtomicExchange())
replacementCall = new TIntermUnary(EOpUintBitsToFloat, replacementCall, nullptr);
if (isImageLoad)
{
// vec4(uintBitsToFloat(imageLoad().rgb), 1.0)
const TType &vec4Type = *StaticType::GetBasic<EbtFloat, 4>();
TIntermSequence constructorArgs = {replacementCall, CreateFloatNode(1.0f)};
replacementCall = TIntermAggregate::CreateConstructor(vec4Type, &constructorArgs);
}
}
return replacementCall;
}
// Traverser that:
//
// 1. Converts the layout(r32f, ...) ... image* name; declarations to use the r32ui format
// 2. Converts |imageLoad| and |imageStore| functions to use |uintBitsToFloat| and |floatBitsToUint|
// respectively.
// 3. Converts |imageAtomicExchange| to use |floatBitsToUint| and |uintBitsToFloat|.
class RewriteR32fImagesTraverser : public TIntermTraverser
{
public:
RewriteR32fImagesTraverser(TCompiler *compiler, TSymbolTable *symbolTable)
: TIntermTraverser(true, false, false, symbolTable), mCompiler(compiler)
{}
bool visitDeclaration(Visit visit, TIntermDeclaration *node) override
{
if (visit != PreVisit)
{
return true;
}
const TIntermSequence &sequence = *(node->getSequence());
TIntermTyped *declVariable = sequence.front()->getAsTyped();
const TType &type = declVariable->getType();
if (!IsR32fImage(type))
{
return true;
}
TIntermSymbol *oldSymbol = declVariable->getAsSymbolNode();
ASSERT(oldSymbol != nullptr);
const TVariable &oldVariable = oldSymbol->variable();
TType *newType = new TType(type);
TLayoutQualifier layoutQualifier = type.getLayoutQualifier();
layoutQualifier.imageInternalFormat = EiifR32UI;
newType->setLayoutQualifier(layoutQualifier);
switch (type.getBasicType())
{
case EbtImage2D:
newType->setBasicType(EbtUImage2D);
break;
case EbtImage3D:
newType->setBasicType(EbtUImage3D);
break;
case EbtImage2DArray:
newType->setBasicType(EbtUImage2DArray);
break;
case EbtImageCube:
newType->setBasicType(EbtUImageCube);
break;
case EbtImage1D:
newType->setBasicType(EbtUImage1D);
break;
case EbtImage1DArray:
newType->setBasicType(EbtUImage1DArray);
break;
case EbtImage2DMS:
newType->setBasicType(EbtUImage2DMS);
break;
case EbtImage2DMSArray:
newType->setBasicType(EbtUImage2DMSArray);
break;
case EbtImage2DRect:
newType->setBasicType(EbtUImage2DRect);
break;
case EbtImageCubeArray:
newType->setBasicType(EbtUImageCubeArray);
break;
case EbtImageRect:
newType->setBasicType(EbtUImageRect);
break;
case EbtImageBuffer:
newType->setBasicType(EbtUImageBuffer);
break;
default:
UNREACHABLE();
}
TVariable *newVariable =
new TVariable(oldVariable.uniqueId(), oldVariable.name(), oldVariable.symbolType(),
oldVariable.extension(), newType);
mImageMap[&oldVariable] = newVariable;
TIntermDeclaration *newDecl = new TIntermDeclaration();
newDecl->appendDeclarator(new TIntermSymbol(newVariable));
queueReplacement(newDecl, OriginalNode::IS_DROPPED);
return false;
}
// Same implementation as in RewriteExpressionTraverser. That traverser cannot replace root.
bool visitAggregate(Visit visit, TIntermAggregate *node) override
{
TIntermTyped *rewritten =
RewriteBuiltinFunctionCall(mCompiler, mSymbolTable, node, mImageMap);
if (rewritten == nullptr)
{
return true;
}
queueReplacement(rewritten, OriginalNode::IS_DROPPED);
return false;
}
void visitSymbol(TIntermSymbol *symbol) override
{
// Cannot encounter the image symbol directly. It can only be used with built-in functions,
// and therefore it's handled by visitAggregate.
ASSERT(!IsR32fImage(symbol->getType()));
}
private:
TCompiler *mCompiler;
// Map from r32f image to r32ui image
ImageMap mImageMap;
};
} // anonymous namespace
bool RewriteR32fImages(TCompiler *compiler, TIntermBlock *root, TSymbolTable *symbolTable)
{
RewriteR32fImagesTraverser traverser(compiler, symbolTable);
root->traverse(&traverser);
return traverser.updateTree(compiler, root);
}
} // namespace sh
//
// Copyright 2021 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// RewriteR32fImages: Change images qualified with r32f to use r32ui instead. The only supported
// operation on these images is imageAtomicExchange(), which works identically with r32ui. This
// avoids requiring atomic operations support for the R32_FLOAT format in Vulkan.
#ifndef COMPILER_TRANSLATOR_TREEOPS_VULKAN_REWRITER32FIMAGES_H_
#define COMPILER_TRANSLATOR_TREEOPS_VULKAN_REWRITER32FIMAGES_H_
#include "common/angleutils.h"
namespace sh
{
class TCompiler;
class TIntermBlock;
class TSymbolTable;
ANGLE_NO_DISCARD bool RewriteR32fImages(TCompiler *compiler,
TIntermBlock *root,
TSymbolTable *symbolTable);
} // namespace sh
#endif // COMPILER_TRANSLATOR_TREEOPS_VULKAN_REWRITER32FIMAGES_H_
......@@ -1276,7 +1276,7 @@ angle::Result ProgramExecutableVk::updateImagesDescriptorSet(
TextureVk *textureVk = activeImages[imageUnit];
const vk::BufferView *view = nullptr;
ANGLE_TRY(textureVk->getBufferViewAndRecordUse(contextVk, format, &view));
ANGLE_TRY(textureVk->getBufferViewAndRecordUse(contextVk, format, true, &view));
const ShaderInterfaceVariableInfo &info =
mVariableInfoMap.get(shaderType, mappedImageName);
......@@ -1510,7 +1510,8 @@ angle::Result ProgramExecutableVk::updateTexturesDescriptorSet(ContextVk *contex
GLuint textureUnit = samplerBinding.boundTextureUnits[arrayElement];
TextureVk *textureVk = activeTextures[textureUnit].texture;
const vk::BufferView *view = nullptr;
ANGLE_TRY(textureVk->getBufferViewAndRecordUse(contextVk, nullptr, &view));
ANGLE_TRY(
textureVk->getBufferViewAndRecordUse(contextVk, nullptr, false, &view));
const std::string samplerName =
GlslangGetMappedSamplerName(samplerUniform.name);
......
......@@ -2052,6 +2052,10 @@ void RendererVk::initFeatures(DisplayVk *displayVk,
&mFeatures, preferDrawClearOverVkCmdClearAttachments,
IsPixel2(mPhysicalDeviceProperties.vendorID, mPhysicalDeviceProperties.deviceID));
// r32f image emulation is done unconditionally so VK_FORMAT_FEATURE_STORAGE_*_ATOMIC_BIT is not
// required.
ANGLE_FEATURE_CONDITION(&mFeatures, emulateR32fImageAtomicExchange, true);
angle::PlatformMethods *platform = ANGLEPlatformCurrent();
platform->overrideFeaturesVk(platform, &mFeatures);
......
......@@ -301,6 +301,19 @@ angle::Result CopyAndStageImageSubresource(ContextVk *contextVk,
return angle::Result::Continue;
}
const vk::Format *AdjustStorageViewFormatPerWorkarounds(ContextVk *contextVk,
const vk::Format *intended)
{
// r32f images are emulated with r32ui.
if (contextVk->getFeatures().emulateR32fImageAtomicExchange.enabled &&
intended->actualImageFormatID == angle::FormatID::R32_FLOAT)
{
return &contextVk->getRenderer()->getFormat(angle::FormatID::R32_UINT);
}
return intended;
}
} // anonymous namespace
// TextureVk implementation.
......@@ -2586,7 +2599,9 @@ angle::Result TextureVk::getStorageImageView(ContextVk *contextVk,
const vk::ImageView **imageViewOut)
{
angle::FormatID formatID = angle::Format::InternalFormatToID(binding.format);
const vk::Format &format = contextVk->getRenderer()->getFormat(formatID);
const vk::Format *format = &contextVk->getRenderer()->getFormat(formatID);
format = AdjustStorageViewFormatPerWorkarounds(contextVk, format);
gl::LevelIndex nativeLevelGL =
getNativeImageLevel(gl::LevelIndex(static_cast<uint32_t>(binding.level)));
......@@ -2598,7 +2613,7 @@ angle::Result TextureVk::getStorageImageView(ContextVk *contextVk,
return getImageViews().getLevelLayerStorageImageView(
contextVk, *mImage, nativeLevelVk, nativeLayer,
VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT, format.actualImageFormatID,
VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT, format->actualImageFormatID,
imageViewOut);
}
......@@ -2606,12 +2621,13 @@ angle::Result TextureVk::getStorageImageView(ContextVk *contextVk,
return getImageViews().getLevelStorageImageView(
contextVk, mState.getType(), *mImage, nativeLevelVk, nativeLayer,
VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT, format.actualImageFormatID,
VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT, format->actualImageFormatID,
imageViewOut);
}
angle::Result TextureVk::getBufferViewAndRecordUse(ContextVk *contextVk,
const vk::Format *imageUniformFormat,
bool isImage,
const vk::BufferView **viewOut)
{
RendererVk *renderer = contextVk->getRenderer();
......@@ -2625,6 +2641,11 @@ angle::Result TextureVk::getBufferViewAndRecordUse(ContextVk *contextVk,
imageUniformFormat = &renderer->getFormat(baseLevelDesc.format.info->sizedInternalFormat);
}
if (isImage)
{
imageUniformFormat = AdjustStorageViewFormatPerWorkarounds(contextVk, imageUniformFormat);
}
// Create a view for the required format.
const vk::BufferHelper &buffer = vk::GetImpl(mState.getBuffer().get())->getBuffer();
......
......@@ -233,6 +233,7 @@ class TextureVk : public TextureImpl, public angle::ObserverInterface
angle::Result getBufferViewAndRecordUse(ContextVk *contextVk,
const vk::Format *imageUniformFormat,
bool isImage,
const vk::BufferView **viewOut);
// Normally, initialize the image with enabled mipmap level counts.
......
......@@ -32,25 +32,6 @@ namespace vk
{
namespace
{
bool HasShaderImageAtomicsSupport(const RendererVk *rendererVk,
const gl::Extensions &supportedExtensions)
{
// Only VK_FORMAT_R32_SFLOAT doesn't have mandatory support for the STORAGE_IMAGE_ATOMIC and
// STORAGE_TEXEL_BUFFER_ATOMIC features.
const Format &formatVk = rendererVk->getFormat(GL_R32F);
const bool hasImageAtomicSupport = rendererVk->hasImageFormatFeatureBits(
formatVk.actualImageFormatID, VK_FORMAT_FEATURE_STORAGE_IMAGE_ATOMIC_BIT);
bool hasBufferAtomicSupport = true;
if (supportedExtensions.textureBufferAny())
{
hasBufferAtomicSupport = rendererVk->hasBufferFormatFeatureBits(
formatVk.actualBufferFormatID, VK_FORMAT_FEATURE_STORAGE_TEXEL_BUFFER_ATOMIC_BIT);
}
return hasImageAtomicSupport && hasBufferAtomicSupport;
}
// Checks to see if each format can be reinterpreted to an equivalent format in a different
// colorspace. If all supported formats can be reinterpreted, it returns true. Formats which are not
// supported at all are ignored and not counted as failures.
......@@ -909,11 +890,9 @@ void RendererVk::ensureCapsInitialized() const
// GL_OES_shader_image_atomic requires that image atomic functions have support for r32i and
// r32ui formats. These formats have mandatory support for STORAGE_IMAGE_ATOMIC and
// STORAGE_TEXEL_BUFFER_ATOMIC features in Vulkan. Additionally, it requires that
// imageAtomicExchange supports r32f. Exposing this extension is thus restricted to this format
// having support for the aforementioned features.
mNativeExtensions.shaderImageAtomicOES =
vk::HasShaderImageAtomicsSupport(this, mNativeExtensions) ||
getFeatures().exposeNonConformantExtensionsAndVersions.enabled;
// imageAtomicExchange supports r32f, which is emulated in ANGLE transforming the shader to
// expect r32ui instead.
mNativeExtensions.shaderImageAtomicOES = true;
// Geometry shaders are required for ES 3.2.
// We don't support GS when we are emulating line raster due to the tricky position varying.
......
......@@ -277,9 +277,6 @@
5276 NVIDIA VULKAN : dEQP-GLES31.functional.copy_image.compressed.viewclass_etc* = FAIL
5276 NVIDIA VULKAN : dEQP-GLES31.functional.copy_image.mixed.*eac* = FAIL
// imageAtomicExchange failure with r32f format
5353 NVIDIA VULKAN : dEQP-GLES31.functional.image_load_store.*.atomic.exchange_r32f* = FAIL
// Vulkan Android failures with these formats
5277 VULKAN ANDROID : dEQP-GLES31.functional.copy_image.non_compressed.viewclass_32_bits.rgba8_snorm_rgb10_a2* = FAIL
5277 VULKAN ANDROID : dEQP-GLES31.functional.copy_image.non_compressed.viewclass_32_bits.rgba8_snorm_rgb9_e5* = FAIL
......
......@@ -3661,7 +3661,6 @@ TEST_P(GLSLTest_ES31, ArraysOfArraysImage)
// Test that multiple arrays of arrays of images work as expected.
TEST_P(GLSLTest_ES31, ConsecutiveArraysOfArraysImage)
{
swapBuffers();
// http://anglebug.com/5072
ANGLE_SKIP_TEST_IF(IsIntel() && IsLinux() && IsOpenGL());
......@@ -3791,6 +3790,174 @@ TEST_P(GLSLTest_ES31, ConsecutiveArraysOfArraysImage)
glUnmapBuffer(GL_SHADER_STORAGE_BUFFER);
}
// Test that arrays of arrays of images of r32f format work when passed to functions.
TEST_P(GLSLTest_ES31, ArraysOfArraysOfR32fImages)
{
// Skip if GL_OES_shader_image_atomic is not enabled.
ANGLE_SKIP_TEST_IF(!IsGLExtensionEnabled("GL_OES_shader_image_atomic"));
// http://anglebug.com/5072
ANGLE_SKIP_TEST_IF(IsIntel() && IsLinux() && IsOpenGL());
// Fails on D3D due to mistranslation.
ANGLE_SKIP_TEST_IF(IsD3D());
// Fails on Android on GLES.
ANGLE_SKIP_TEST_IF(IsAndroid() && IsOpenGLES());
// http://anglebug.com/5353
ANGLE_SKIP_TEST_IF(IsNVIDIA() && IsOpenGL());
GLint maxComputeImageUniforms;
glGetIntegerv(GL_MAX_COMPUTE_IMAGE_UNIFORMS, &maxComputeImageUniforms);
ANGLE_SKIP_TEST_IF(maxComputeImageUniforms < 7);
constexpr char kComputeShader[] = R"(#version 310 es
#extension GL_OES_shader_image_atomic : require
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
layout(binding = 0, r32f) uniform highp image2D image1[2][3];
layout(binding = 6, r32f) uniform highp image2D image2;
void testFunction(image2D imageOut[2][3])
{
// image1 is an array of 1x1 images.
// image2 is a 1x4 image with the following data:
//
// (0, 0): 234.5
// (0, 1): 4.0
// (0, 2): 456.0
// (0, 3): 987.0
// Write to [0][0]
imageStore(imageOut[0][0], ivec2(0, 0), vec4(1234.5));
// Write to [0][1]
imageStore(imageOut[0][1], ivec2(0, 0), imageLoad(image2, ivec2(0, 0)));
// Write to [0][2]
imageStore(imageOut[0][2], ivec2(0, 0), vec4(imageSize(image2).y));
// Write to [1][0]
imageStore(imageOut[1][0], ivec2(0,
imageSize(image2).y - int(imageLoad(image2, ivec2(0, 1)).x)
), vec4(678.0));
// Write to [1][1]
imageStore(imageOut[1][1], ivec2(0, 0),
vec4(imageAtomicExchange(image2, ivec2(0, 2), 135.0)));
// Write to [1][2]
imageStore(imageOut[1][2], ivec2(0, 0),
imageLoad(image2, ivec2(imageSize(image2).x - 1, 3)));
}
void main(void)
{
testFunction(image1);
})";
ANGLE_GL_COMPUTE_PROGRAM(program, kComputeShader);
EXPECT_GL_NO_ERROR();
glUseProgram(program);
constexpr GLsizei kImageRows = 2;
constexpr GLsizei kImageCols = 3;
constexpr GLfloat kImageData = 0;
GLTexture images[kImageRows][kImageCols];
for (size_t row = 0; row < kImageRows; row++)
{
for (size_t col = 0; col < kImageCols; col++)
{
glBindTexture(GL_TEXTURE_2D, images[row][col]);
glTexStorage2D(GL_TEXTURE_2D, 1, GL_R32F, 1, 1);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 1, 1, GL_RED, GL_FLOAT, &kImageData);
glBindImageTexture(row * kImageCols + col, images[row][col], 0, GL_FALSE, 0,
GL_READ_WRITE, GL_R32F);
EXPECT_GL_NO_ERROR();
}
}
constexpr GLsizei kImage2Size = 4;
constexpr std::array<GLfloat, kImage2Size> kImage2Data = {
234.5f,
4.0f,
456.0f,
987.0f,
};
GLTexture image2;
glBindTexture(GL_TEXTURE_2D, image2);
glTexStorage2D(GL_TEXTURE_2D, 1, GL_R32F, 1, kImage2Size);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 1, kImage2Size, GL_RED, GL_FLOAT, kImage2Data.data());
glBindImageTexture(6, image2, 0, GL_FALSE, 0, GL_READ_WRITE, GL_R32F);
EXPECT_GL_NO_ERROR();
glDispatchCompute(1, 1, 1);
EXPECT_GL_NO_ERROR();
glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT);
// Verify the previous dispatch with another dispatch
constexpr char kVerifyShader[] = R"(#version 310 es
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
layout(binding = 0, r32f) uniform highp readonly image2D image1[2][3];
layout(binding = 6, r32f) uniform highp readonly image2D image2;
layout(binding = 0, std430) buffer Output {
float image2Data[4];
float image1Data[6];
} outbuf;
void main(void)
{
for (int i = 0; i < 4; ++i)
{
outbuf.image2Data[i] = imageLoad(image2, ivec2(0, i)).x;
}
outbuf.image1Data[0] = imageLoad(image1[0][0], ivec2(0, 0)).x;
outbuf.image1Data[1] = imageLoad(image1[0][1], ivec2(0, 0)).x;
outbuf.image1Data[2] = imageLoad(image1[0][2], ivec2(0, 0)).x;
outbuf.image1Data[3] = imageLoad(image1[1][0], ivec2(0, 0)).x;
outbuf.image1Data[4] = imageLoad(image1[1][1], ivec2(0, 0)).x;
outbuf.image1Data[5] = imageLoad(image1[1][2], ivec2(0, 0)).x;
})";
ANGLE_GL_COMPUTE_PROGRAM(verifyProgram, kVerifyShader);
EXPECT_GL_NO_ERROR();
glUseProgram(verifyProgram);
constexpr std::array<GLfloat, kImage2Size + kImageRows *kImageCols> kOutputInitData = {};
GLBuffer outputBuffer;
glBindBuffer(GL_SHADER_STORAGE_BUFFER, outputBuffer);
glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(kOutputInitData), kOutputInitData.data(),
GL_STATIC_DRAW);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, outputBuffer);
EXPECT_GL_NO_ERROR();
glDispatchCompute(1, 1, 1);
EXPECT_GL_NO_ERROR();
glMemoryBarrier(GL_BUFFER_UPDATE_BARRIER_BIT);
// Verify
const GLfloat *ptr = reinterpret_cast<const GLfloat *>(
glMapBufferRange(GL_SHADER_STORAGE_BUFFER, 0, sizeof(kOutputInitData), GL_MAP_READ_BIT));
EXPECT_EQ(ptr[0], kImage2Data[0]);
EXPECT_EQ(ptr[1], kImage2Data[1]);
EXPECT_NEAR(ptr[2], 135.0f, 0.0001f);
EXPECT_EQ(ptr[3], kImage2Data[3]);
EXPECT_NEAR(ptr[4], 1234.5f, 0.0001f);
EXPECT_NEAR(ptr[5], kImage2Data[0], 0.0001f);
EXPECT_NEAR(ptr[6], kImage2Size, 0.0001f);
EXPECT_NEAR(ptr[7], 678.0f, 0.0001f);
EXPECT_NEAR(ptr[8], kImage2Data[2], 0.0001f);
EXPECT_NEAR(ptr[9], kImage2Data[3], 0.0001f);
glUnmapBuffer(GL_SHADER_STORAGE_BUFFER);
}
// Test that structs containing arrays of samplers work as expected.
TEST_P(GLSLTest_ES31, StructArraySampler)
{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment