Commit dfd9bdfd by Shahbaz Youssefi Committed by Commit Bot

Reland "Vulkan: Generate gl_Position pre-rotation in SPIR-V"

This reverts commit 3d39b7c5. Reason for revert: Fixed interaction with the `forceDriverUniformOverSpecConst` workaround. Original change's description: > Revert "Vulkan: Generate gl_Position pre-rotation in SPIR-V" > > This reverts commit 0f86b196. > > Reason for revert: > Breaks pre-rotation for all apps, so they are displayed in portrait instead of landscape. > > Original change's description: > > Vulkan: Generate gl_Position pre-rotation in SPIR-V > > > > Instead of having the translator output pre-rotation code in the vertex > > stage based on a specialization constant, this change makes the SPIR-V > > transformer perform pre-rotation of gl_Position on the last geometry > > stage. > > > > An alternative solution would be to generate pre-rotation code in the > > translator in every geometry stage, each controlled by a separate > > specialization constant. This change avoids unnecessary modifications > > to earlier stages. The generated shaders are also smaller, as they > > don't contain a mat2[8] pre-rotation constant matrix. The SPIR-V > > transformer knows the pre-rotation at transformation time, so it can > > simply use swizzles to achieve the same results. > > > > This also ties in with upcoming changes which move gl_Position.z > > correction to the last geometry shader stage, which is trivially done > > piggy-backing on the infrastructure in this change. > > > > Bug: angleproject:5478 > > Change-Id: I9d5d9d19f3ccda665f5504368ce5ddfa5f383faf > > Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/2598584 > > Commit-Queue: Shahbaz Youssefi <syoussefi@chromium.org> > > Reviewed-by: Charlie Lao <cclao@google.com> > > Reviewed-by: Jamie Madill <jmadill@chromium.org> > > TBR=syoussefi@chromium.org,jmadill@chromium.org,cclao@google.com > > Change-Id: I81f237fa6b10c7d59831363bee8999e7ad2f09be > No-Presubmit: true > No-Tree-Checks: true > No-Try: true > Bug: angleproject:5478 > Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/2633694 > Reviewed-by: Tim Van Patten <timvp@google.com> > Commit-Queue: Tim Van Patten <timvp@google.com> TBR=timvp@google.com,syoussefi@chromium.org,jmadill@chromium.org,cclao@google.com Bug: angleproject:5478 Change-Id: I7c5eaeef03d9520abd36a1c4a766b6abbf4fdb45 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/2633709Reviewed-by: 's avatarShahbaz Youssefi <syoussefi@chromium.org> Commit-Queue: Shahbaz Youssefi <syoussefi@chromium.org>
parent 60015ff6
......@@ -306,39 +306,6 @@ ANGLE_NO_DISCARD bool AppendVertexShaderDepthCorrectionToMain(TCompiler *compile
return RunAtTheEndOfShader(compiler, root, assignment, symbolTable);
}
// This operation performs Android pre-rotation and y-flip. For Android (and potentially other
// platforms), the device may rotate, such that the orientation of the application is rotated
// relative to the native orientation of the device. This is corrected in part by multiplying
// gl_Position by a mat2.
// The equations reduce to an expression:
//
// gl_Position.xy = gl_Position.xy * preRotation
ANGLE_NO_DISCARD bool AppendPreRotation(TCompiler *compiler,
TIntermBlock *root,
TSymbolTable *symbolTable,
SpecConst *specConst,
const DriverUniform *driverUniforms)
{
TIntermTyped *preRotationRef = specConst->getPreRotationMatrix();
if (!preRotationRef)
{
preRotationRef = driverUniforms->getPreRotationMatrixRef();
}
TIntermSymbol *glPos = new TIntermSymbol(BuiltInVariable::gl_Position());
TVector<int> swizzleOffsetXY = {0, 1};
TIntermSwizzle *glPosXY = new TIntermSwizzle(glPos, swizzleOffsetXY);
// Create the expression "(gl_Position.xy * preRotation)"
TIntermBinary *zRotated = new TIntermBinary(EOpMatrixTimesVector, preRotationRef, glPosXY);
// Create the assignment "gl_Position.xy = (gl_Position.xy * preRotation)"
TIntermBinary *assignment =
new TIntermBinary(TOperator::EOpAssign, glPosXY->deepCopy(), zRotated);
// Append the assignment as a statement at the end of the shader.
return RunAtTheEndOfShader(compiler, root, assignment, symbolTable);
}
ANGLE_NO_DISCARD bool AppendTransformFeedbackOutputToMain(TCompiler *compiler,
TIntermBlock *root,
TSymbolTable *symbolTable)
......@@ -1154,11 +1121,6 @@ bool TranslatorVulkan::translateImpl(TIntermBlock *root,
{
return false;
}
if ((compileOptions & SH_ADD_PRE_ROTATION) != 0 &&
!AppendPreRotation(this, root, &getSymbolTable(), specConst, driverUniforms))
{
return false;
}
break;
}
......
......@@ -36,7 +36,6 @@ constexpr const char kNumSamples[] = "numSamples";
constexpr const char kHalfRenderArea[] = "halfRenderArea";
constexpr const char kFlipXY[] = "flipXY";
constexpr const char kNegFlipXY[] = "negFlipXY";
constexpr const char kPreRotation[] = "preRotation";
constexpr const char kFragRotation[] = "fragRotation";
} // anonymous namespace
......@@ -218,10 +217,9 @@ TFieldList *DriverUniformExtended::createUniformFields(TSymbolTable *symbolTable
{
TFieldList *driverFieldList = DriverUniform::createUniformFields(symbolTable);
constexpr size_t kNumGraphicsDriverUniformsExt = 5;
constexpr size_t kNumGraphicsDriverUniformsExt = 4;
constexpr std::array<const char *, kNumGraphicsDriverUniformsExt>
kGraphicsDriverUniformNamesExt = {
{kHalfRenderArea, kFlipXY, kNegFlipXY, kPreRotation, kFragRotation}};
kGraphicsDriverUniformNamesExt = {{kHalfRenderArea, kFlipXY, kNegFlipXY, kFragRotation}};
const std::array<TType *, kNumGraphicsDriverUniformsExt> kDriverUniformTypesExt = {{
new TType(EbtFloat, 2),
......@@ -229,7 +227,6 @@ TFieldList *DriverUniformExtended::createUniformFields(TSymbolTable *symbolTable
new TType(EbtFloat, 2),
// NOTE: There's a vec2 gap here that can be used in the future
new TType(EbtFloat, 2, 2),
new TType(EbtFloat, 2, 2),
}};
for (size_t uniformIndex = 0; uniformIndex < kNumGraphicsDriverUniformsExt; ++uniformIndex)
......@@ -268,11 +265,6 @@ TIntermBinary *DriverUniformExtended::getFragRotationMatrixRef() const
return createDriverUniformRef(kFragRotation);
}
TIntermBinary *DriverUniformExtended::getPreRotationMatrixRef() const
{
return createDriverUniformRef(kPreRotation);
}
TIntermBinary *DriverUniformExtended::getHalfRenderAreaRef() const
{
return createDriverUniformRef(kHalfRenderArea);
......
......@@ -44,7 +44,6 @@ class DriverUniform
virtual TIntermBinary *getFlipXYRef() const { return nullptr; }
virtual TIntermBinary *getNegFlipXYRef() const { return nullptr; }
virtual TIntermBinary *getFragRotationMatrixRef() const { return nullptr; }
virtual TIntermBinary *getPreRotationMatrixRef() const { return nullptr; }
virtual TIntermBinary *getHalfRenderAreaRef() const { return nullptr; }
virtual TIntermSwizzle *getNegFlipYRef() const { return nullptr; }
......@@ -65,7 +64,6 @@ class DriverUniformExtended : public DriverUniform
TIntermBinary *getFlipXYRef() const override;
TIntermBinary *getNegFlipXYRef() const override;
TIntermBinary *getFragRotationMatrixRef() const override;
TIntermBinary *getPreRotationMatrixRef() const override;
TIntermBinary *getHalfRenderAreaRef() const override;
TIntermSwizzle *getNegFlipYRef() const override;
......
......@@ -35,16 +35,6 @@ using Mat2x2 = std::array<float, 4>;
using Mat2x2EnumMap =
angle::PackedEnumMap<vk::SurfaceRotation, Mat2x2, angle::EnumSize<vk::SurfaceRotation>()>;
constexpr Mat2x2EnumMap kPreRotationMatrices = {
{{vk::SurfaceRotation::Identity, {{1.0f, 0.0f, 0.0f, 1.0f}}},
{vk::SurfaceRotation::Rotated90Degrees, {{0.0f, -1.0f, 1.0f, 0.0f}}},
{vk::SurfaceRotation::Rotated180Degrees, {{-1.0f, 0.0f, 0.0f, -1.0f}}},
{vk::SurfaceRotation::Rotated270Degrees, {{0.0f, 1.0f, -1.0f, 0.0f}}},
{vk::SurfaceRotation::FlippedIdentity, {{1.0f, 0.0f, 0.0f, 1.0f}}},
{vk::SurfaceRotation::FlippedRotated90Degrees, {{0.0f, -1.0f, 1.0f, 0.0f}}},
{vk::SurfaceRotation::FlippedRotated180Degrees, {{-1.0f, 0.0f, 0.0f, -1.0f}}},
{vk::SurfaceRotation::FlippedRotated270Degrees, {{0.0f, 1.0f, -1.0f, 0.0f}}}}};
constexpr Mat2x2EnumMap kFragRotationMatrices = {
{{vk::SurfaceRotation::Identity, {{1.0f, 0.0f, 0.0f, 1.0f}}},
{vk::SurfaceRotation::Rotated90Degrees, {{0.0f, 1.0f, 1.0f, 0.0f}}},
......@@ -238,7 +228,14 @@ TIntermTyped *CreateFloatArrayWithRotationIndex(const Vec2EnumMap &valuesEnumMap
SpecConst::SpecConst(TSymbolTable *symbolTable, ShCompileOptions compileOptions)
: mSymbolTable(symbolTable), mCompileOptions(compileOptions)
{}
{
// Mark SpecConstUsage::Rotation unconditionally. gl_Position is always rotated.
if ((mCompileOptions & SH_USE_SPECIALIZATION_CONSTANT) != 0 &&
(mCompileOptions & SH_ADD_PRE_ROTATION) != 0)
{
mUsageBits.set(vk::SpecConstUsage::Rotation);
}
}
SpecConst::~SpecConst() {}
......@@ -337,16 +334,6 @@ TIntermTyped *SpecConst::getMultiplierYForDFdy()
return CreateFloatArrayWithRotationIndex(kRotatedFlipXYForDFdy, 1, 1, getFlipRotation());
}
TIntermTyped *SpecConst::getPreRotationMatrix()
{
if (!(mCompileOptions & SH_USE_SPECIALIZATION_CONSTANT))
{
return nullptr;
}
mUsageBits.set(vk::SpecConstUsage::Rotation);
return GenerateMat2x2ArrayWithIndex(kPreRotationMatrices, getFlipRotation());
}
TIntermTyped *SpecConst::getFragRotationMatrix()
{
if (!(mCompileOptions & SH_USE_SPECIALIZATION_CONSTANT))
......
......@@ -34,7 +34,6 @@ class SpecConst
TIntermTyped *getMultiplierYForDFdx();
TIntermTyped *getMultiplierXForDFdy();
TIntermTyped *getMultiplierYForDFdy();
TIntermTyped *getPreRotationMatrix();
TIntermTyped *getFragRotationMatrix();
TIntermTyped *getFlipXY();
TIntermTyped *getNegFlipXY();
......
......@@ -12,6 +12,7 @@
#include <functional>
#include "libANGLE/renderer/ProgramImpl.h"
#include "libANGLE/renderer/renderer_utils.h"
namespace rx
{
......@@ -55,6 +56,7 @@ struct GlslangSourceOptions
struct GlslangSpirvOptions
{
gl::ShaderType shaderType = gl::ShaderType::InvalidEnum;
SurfaceRotation preRotation = SurfaceRotation::Identity;
bool removeEarlyFragmentTestsOptimization = false;
bool removeDebugInfo = false;
bool isTransformFeedbackStage = false;
......
......@@ -95,9 +95,6 @@ struct GraphicsDriverUniformsExtended
std::array<float, 2> negFlipXY;
std::array<int32_t, 2> padding;
// Used to pre-rotate gl_Position for swapchain images on Android (a mat2, which is padded to
// the size of two vec4's).
std::array<float, 8> preRotation;
// Used to pre-rotate gl_FragCoord for swapchain images on Android (a mat2, which is padded to
// the size of two vec4's).
std::array<float, 8> fragRotation;
......@@ -191,34 +188,15 @@ bool IsRenderPassStartedAndUsesImage(const vk::CommandBufferHelper &renderPassCo
}
// When an Android surface is rotated differently than the device's native orientation, ANGLE must
// rotate gl_Position in the vertex shader and gl_FragCoord in the fragment shader. The following
// are the rotation matrices used.
// rotate gl_Position in the last pre-rasterization shader and gl_FragCoord in the fragment shader.
// Rotation of gl_Position is done in SPIR-V. The following are the rotation matrices for the
// fragment shader.
//
// Note: these are mat2's that are appropriately padded (4 floats per row).
using PreRotationMatrixValues = std::array<float, 8>;
constexpr angle::PackedEnumMap<rx::SurfaceRotation,
PreRotationMatrixValues,
angle::EnumSize<rx::SurfaceRotation>()>
kPreRotationMatrices = {
{{rx::SurfaceRotation::Identity, {{1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f}}},
{rx::SurfaceRotation::Rotated90Degrees,
{{0.0f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f}}},
{rx::SurfaceRotation::Rotated180Degrees,
{{-1.0f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f}}},
{rx::SurfaceRotation::Rotated270Degrees,
{{0.0f, 1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f}}},
{rx::SurfaceRotation::FlippedIdentity,
{{1.0f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f}}},
{rx::SurfaceRotation::FlippedRotated90Degrees,
{{0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f}}},
{rx::SurfaceRotation::FlippedRotated180Degrees,
{{-1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f}}},
{rx::SurfaceRotation::FlippedRotated270Degrees,
{{0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f}}}}};
constexpr angle::PackedEnumMap<rx::SurfaceRotation,
PreRotationMatrixValues,
angle::EnumSize<rx::SurfaceRotation>()>
kFragRotationMatrices = {
{{rx::SurfaceRotation::Identity, {{1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f}}},
{rx::SurfaceRotation::Rotated90Degrees,
......@@ -3215,9 +3193,15 @@ void ContextVk::updateGraphicsPipelineDescWithSpecConstUsageBits(SpecConstUsageB
bool yFlipped =
isViewportFlipEnabledForDrawFBO() && usageBits.test(sh::vk::SpecConstUsage::YFlip);
// usageBits are only set when specialization constants are used. With gl_Position pre-rotation
// handled by the SPIR-V transformer, we need to have this information even when the driver
// uniform path is taken to pre-rotate everything else.
const bool programUsesRotation = usageBits.test(sh::vk::SpecConstUsage::Rotation) ||
getFeatures().forceDriverUniformOverSpecConst.enabled;
// If program is not using rotation at all, we force it to use the Identity or FlippedIdentity
// slot to improve the program cache hit rate
if (!usageBits.test(sh::vk::SpecConstUsage::Rotation))
if (!programUsesRotation)
{
rotationAndFlip = yFlipped ? SurfaceRotation::FlippedIdentity : SurfaceRotation::Identity;
}
......@@ -3842,9 +3826,6 @@ angle::Result ContextVk::handleDirtyGraphicsDriverUniforms(const gl::Context *co
driverUniformsExt->halfRenderArea = {halfRenderAreaWidth, halfRenderAreaHeight};
driverUniformsExt->flipXY = {flipX, flipY};
driverUniformsExt->negFlipXY = {flipX, -flipY};
memcpy(&driverUniformsExt->preRotation,
&kPreRotationMatrices[mCurrentRotationDrawFramebuffer],
sizeof(PreRotationMatrixValues));
memcpy(&driverUniformsExt->fragRotation,
&kFragRotationMatrices[mCurrentRotationDrawFramebuffer],
sizeof(PreRotationMatrixValues));
......
......@@ -28,12 +28,15 @@ bool ValidateTransformedSpirV(ContextVk *contextVk,
const ShaderInterfaceVariableInfoMap &variableInfoMap,
const gl::ShaderMap<SpirvBlob> &spirvBlobs)
{
const gl::ShaderType lastPreFragmentStage = gl::GetLastPreFragmentStage(linkedShaderStages);
for (gl::ShaderType shaderType : linkedShaderStages)
{
GlslangSpirvOptions options;
options.shaderType = shaderType;
options.preRotation = SurfaceRotation::FlippedRotated90Degrees;
options.removeDebugInfo = true;
options.isTransformFeedbackStage = shaderType == gl::ShaderType::Vertex;
options.isTransformFeedbackStage = shaderType == lastPreFragmentStage;
SpirvBlob transformed;
if (GlslangWrapperVk::TransformSpirV(contextVk, options, variableInfoMap,
......@@ -117,6 +120,7 @@ ProgramInfo::~ProgramInfo() = default;
angle::Result ProgramInfo::initProgram(ContextVk *contextVk,
const gl::ShaderType shaderType,
bool isLastPreFragmentStage,
const ShaderInfo &shaderInfo,
ProgramTransformOptions optionBits,
const ShaderInterfaceVariableInfoMap &variableInfoMap)
......@@ -131,7 +135,12 @@ angle::Result ProgramInfo::initProgram(ContextVk *contextVk,
options.removeEarlyFragmentTestsOptimization =
shaderType == gl::ShaderType::Fragment && optionBits.removeEarlyFragmentTestsOptimization;
options.removeDebugInfo = !contextVk->getRenderer()->getEnableValidationLayers();
options.isTransformFeedbackStage = shaderType == gl::ShaderType::Vertex;
options.isTransformFeedbackStage = isLastPreFragmentStage;
if (isLastPreFragmentStage)
{
options.preRotation = static_cast<SurfaceRotation>(optionBits.surfaceRotation);
}
ANGLE_TRY(GlslangWrapperVk::TransformSpirV(contextVk, options, variableInfoMap,
originalSpirvBlob, &transformedSpirvBlob));
......@@ -677,14 +686,18 @@ angle::Result ProgramExecutableVk::getGraphicsPipeline(
mTransformOptions.surfaceRotation = ToUnderlying(desc.getSurfaceRotation());
// This must be called after mTransformOptions have been set.
ProgramInfo &programInfo = getGraphicsProgramInfo(mTransformOptions);
for (const gl::ShaderType shaderType : glExecutable->getLinkedShaderStages())
ProgramInfo &programInfo = getGraphicsProgramInfo(mTransformOptions);
const gl::ShaderBitSet linkedShaderStages = glExecutable->getLinkedShaderStages();
const gl::ShaderType lastPreFragmentStage = gl::GetLastPreFragmentStage(linkedShaderStages);
for (const gl::ShaderType shaderType : linkedShaderStages)
{
ProgramVk *programVk = getShaderProgram(glState, shaderType);
if (programVk)
{
ANGLE_TRY(programVk->initGraphicsShaderProgram(contextVk, shaderType, mTransformOptions,
&programInfo, mVariableInfoMap));
ANGLE_TRY(programVk->initGraphicsShaderProgram(
contextVk, shaderType, shaderType == lastPreFragmentStage, mTransformOptions,
&programInfo, mVariableInfoMap));
}
}
......
......@@ -67,6 +67,7 @@ class ProgramInfo final : angle::NonCopyable
angle::Result initProgram(ContextVk *contextVk,
const gl::ShaderType shaderType,
bool isLastPreFragmentStage,
const ShaderInfo &shaderInfo,
ProgramTransformOptions optionBits,
const ShaderInterfaceVariableInfoMap &variableInfoMap);
......
......@@ -148,11 +148,13 @@ class ProgramVk : public ProgramImpl
ANGLE_INLINE angle::Result initGraphicsShaderProgram(
ContextVk *contextVk,
const gl::ShaderType shaderType,
bool isLastPreFragmentStage,
ProgramTransformOptions optionBits,
ProgramInfo *programInfo,
const ShaderInterfaceVariableInfoMap &variableInfoMap)
{
return initProgram(contextVk, shaderType, optionBits, programInfo, variableInfoMap);
return initProgram(contextVk, shaderType, isLastPreFragmentStage, optionBits, programInfo,
variableInfoMap);
}
ANGLE_INLINE angle::Result initComputeProgram(
......@@ -161,7 +163,7 @@ class ProgramVk : public ProgramImpl
const ShaderInterfaceVariableInfoMap &variableInfoMap)
{
ProgramTransformOptions optionBits = {};
return initProgram(contextVk, gl::ShaderType::Compute, optionBits, programInfo,
return initProgram(contextVk, gl::ShaderType::Compute, false, optionBits, programInfo,
variableInfoMap);
}
......@@ -194,6 +196,7 @@ class ProgramVk : public ProgramImpl
ANGLE_INLINE angle::Result initProgram(ContextVk *contextVk,
const gl::ShaderType shaderType,
bool isLastPreFragmentStage,
ProgramTransformOptions optionBits,
ProgramInfo *programInfo,
const ShaderInterfaceVariableInfoMap &variableInfoMap)
......@@ -204,8 +207,8 @@ class ProgramVk : public ProgramImpl
// specialization constants.
if (!programInfo->valid(shaderType))
{
ANGLE_TRY(programInfo->initProgram(contextVk, shaderType, mOriginalShaderInfo,
optionBits, variableInfoMap));
ANGLE_TRY(programInfo->initProgram(contextVk, shaderType, isLastPreFragmentStage,
mOriginalShaderInfo, optionBits, variableInfoMap));
}
ASSERT(programInfo->valid(shaderType));
......
......@@ -109,11 +109,11 @@ class EGLPreRotationSurfaceTest : public ANGLETestWithParam<EGLPreRotationSurfac
std::vector<const char *> disabledFeatures;
if (::testing::get<1>(GetParam()))
{
enabledFeatures.push_back("enable_pre_rotation_surfaces");
enabledFeatures.push_back("enablePreRotateSurfaces");
}
else
{
disabledFeatures.push_back("enable_pre_rotation_surfaces");
disabledFeatures.push_back("enablePreRotateSurfaces");
}
enabledFeatures.push_back(nullptr);
disabledFeatures.push_back(nullptr);
......
......@@ -1348,9 +1348,6 @@ TEST_P(GeometryShaderTest, Prerotation)
{
ANGLE_SKIP_TEST_IF(!IsGLExtensionEnabled("GL_EXT_geometry_shader"));
// http://anglebug.com/5478
ANGLE_SKIP_TEST_IF(IsVulkan());
constexpr char kVS[] = R"(#version 310 es
void main()
{
......
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