Per sample shading

This cl introduces per sample shading in the fragment shader. Rather than call the fragment shader multiple times per sample, this cl adds a potential loop in the fragment shader where each sample is processes in one of the loop's iteration. - Each multisample related loop now processes either all samples, like before, or the current sample, if per sample shading is enabled - A new per sample PixelProgram::maskAny() function was added - emitEpilog() now has an option not to clear phis in order to be able to only clear them on the last sample - The routine's fragCoord values are set per sample, with the proper sample offsets - Similarly, the xxxx and yyyy values used for interpolation are now offset with the proper sample offsets when per sample shading is enabled Bug: b/171415086 Change-Id: Ibd0c1bad23e2d81f7fa97240ebb50f88f1fee36e Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/51733Reviewed-by: 's avatarAntonio Maiorano <amaiorano@google.com> Commit-Queue: Alexis Hétu <sugoi@google.com> Tested-by: 's avatarAlexis Hétu <sugoi@google.com> Kokoro-Result: kokoro <noreply+kokoro@google.com>
parent 3e9b79ff
...@@ -57,6 +57,7 @@ void ComputeProgram::generate() ...@@ -57,6 +57,7 @@ void ComputeProgram::generate()
shader->emitProlog(&routine); shader->emitProlog(&routine);
emit(&routine); emit(&routine);
shader->emitEpilog(&routine); shader->emitEpilog(&routine);
shader->clearPhis(&routine);
} }
void ComputeProgram::setWorkgroupBuiltins(Pointer<Byte> data, SpirvRoutine *routine, Int workgroupID[3]) void ComputeProgram::setWorkgroupBuiltins(Pointer<Byte> data, SpirvRoutine *routine, Int workgroupID[3])
......
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
// limitations under the License. // limitations under the License.
#include "PixelProgram.hpp" #include "PixelProgram.hpp"
#include "Constants.hpp"
#include "SamplerCore.hpp" #include "SamplerCore.hpp"
#include "Device/Primitive.hpp" #include "Device/Primitive.hpp"
...@@ -56,14 +57,37 @@ Int4 PixelProgram::maskAny(Int cMask[4], Int sMask[4], Int zMask[4]) const ...@@ -56,14 +57,37 @@ Int4 PixelProgram::maskAny(Int cMask[4], Int sMask[4], Int zMask[4]) const
return mask; return mask;
} }
void PixelProgram::setBuiltins(Int &x, Int &y, Float4 (&z)[4], Float4 &w, Int cMask[4]) Int4 PixelProgram::maskAny(Int cMask, Int sMask, Int zMask) const
{
Int maskUnion = cMask & sMask & zMask;
// Convert to 4 booleans
Int4 laneBits = Int4(1, 2, 4, 8);
Int4 laneShiftsToMSB = Int4(31, 30, 29, 28);
Int4 mask(maskUnion);
mask = ((mask & laneBits) << laneShiftsToMSB) >> Int4(31);
return mask;
}
void PixelProgram::setBuiltins(Int &x, Int &y, Float4 (&z)[4], Float4 &w, Int cMask[4], int sampleId)
{ {
routine.setImmutableInputBuiltins(spirvShader); routine.setImmutableInputBuiltins(spirvShader);
// TODO(b/146486064): Consider only assigning these to the SpirvRoutine iff // TODO(b/146486064): Consider only assigning these to the SpirvRoutine iff
// they are ever going to be read. // they are ever going to be read.
routine.fragCoord[0] = SIMD::Float(Float(x)) + SIMD::Float(0.5f, 1.5f, 0.5f, 1.5f); float x0 = 0.5f;
routine.fragCoord[1] = SIMD::Float(Float(y)) + SIMD::Float(0.5f, 0.5f, 1.5f, 1.5f); float y0 = 0.5f;
float x1 = 1.5f;
float y1 = 1.5f;
if((state.multiSampleCount > 1) && (sampleId >= 0))
{
x0 = Constants::VkSampleLocations4[sampleId][0];
y0 = Constants::VkSampleLocations4[sampleId][1];
x1 = 1.0f + x0;
y1 = 1.0f + y0;
}
routine.fragCoord[0] = SIMD::Float(Float(x)) + SIMD::Float(x0, x1, x0, x1);
routine.fragCoord[1] = SIMD::Float(Float(y)) + SIMD::Float(y0, y0, y1, y1);
routine.fragCoord[2] = z[0]; // sample 0 routine.fragCoord[2] = z[0]; // sample 0
routine.fragCoord[3] = w; routine.fragCoord[3] = w;
...@@ -109,8 +133,11 @@ void PixelProgram::setBuiltins(Int &x, Int &y, Float4 (&z)[4], Float4 &w, Int cM ...@@ -109,8 +133,11 @@ void PixelProgram::setBuiltins(Int &x, Int &y, Float4 (&z)[4], Float4 &w, Int cM
}); });
} }
void PixelProgram::applyShader(Int cMask[4], Int sMask[4], Int zMask[4]) void PixelProgram::applyShader(Int cMask[4], Int sMask[4], Int zMask[4], int sampleId)
{ {
unsigned int sampleLoopInit = (sampleId >= 0) ? sampleId : 0;
unsigned int sampleLoopEnd = (sampleId >= 0) ? sampleId + 1 : state.multiSampleCount;
routine.descriptorSets = data + OFFSET(DrawData, descriptorSets); routine.descriptorSets = data + OFFSET(DrawData, descriptorSets);
routine.descriptorDynamicOffsets = data + OFFSET(DrawData, descriptorDynamicOffsets); routine.descriptorDynamicOffsets = data + OFFSET(DrawData, descriptorDynamicOffsets);
routine.pushConstants = data + OFFSET(DrawData, pushConstants); routine.pushConstants = data + OFFSET(DrawData, pushConstants);
...@@ -130,8 +157,8 @@ void PixelProgram::applyShader(Int cMask[4], Int sMask[4], Int zMask[4]) ...@@ -130,8 +157,8 @@ void PixelProgram::applyShader(Int cMask[4], Int sMask[4], Int zMask[4])
static_assert(SIMD::Width == 4, "Expects SIMD width to be 4"); static_assert(SIMD::Width == 4, "Expects SIMD width to be 4");
Int4 laneBits = Int4(1, 2, 4, 8); Int4 laneBits = Int4(1, 2, 4, 8);
Int4 inputSampleMask = Int4(1) & CmpNEQ(Int4(cMask[0]) & laneBits, Int4(0)); Int4 inputSampleMask = 0;
for(auto i = 1u; i < state.multiSampleCount; i++) for(auto i = sampleLoopInit; i < sampleLoopEnd; i++)
{ {
inputSampleMask |= Int4(1 << i) & CmpNEQ(Int4(cMask[i]) & laneBits, Int4(0)); inputSampleMask |= Int4(1 << i) & CmpNEQ(Int4(cMask[i]) & laneBits, Int4(0));
} }
...@@ -146,11 +173,15 @@ void PixelProgram::applyShader(Int cMask[4], Int sMask[4], Int zMask[4]) ...@@ -146,11 +173,15 @@ void PixelProgram::applyShader(Int cMask[4], Int sMask[4], Int zMask[4])
// Note: all lanes initially active to facilitate derivatives etc. Actual coverage is // Note: all lanes initially active to facilitate derivatives etc. Actual coverage is
// handled separately, through the cMask. // handled separately, through the cMask.
auto activeLaneMask = SIMD::Int(0xFFFFFFFF); auto activeLaneMask = SIMD::Int(0xFFFFFFFF);
auto storesAndAtomicsMask = maskAny(cMask, sMask, zMask); auto storesAndAtomicsMask = (sampleId >= 0) ? maskAny(cMask[sampleId], sMask[sampleId], zMask[sampleId]) : maskAny(cMask, sMask, zMask);
routine.killMask = 0; routine.killMask = 0;
spirvShader->emit(&routine, activeLaneMask, storesAndAtomicsMask, descriptorSets); spirvShader->emit(&routine, activeLaneMask, storesAndAtomicsMask, descriptorSets);
spirvShader->emitEpilog(&routine); spirvShader->emitEpilog(&routine);
if((sampleId < 0) || (sampleId == static_cast<int>(state.multiSampleCount - 1)))
{
spirvShader->clearPhis(&routine);
}
for(int i = 0; i < RENDERTARGETS; i++) for(int i = 0; i < RENDERTARGETS; i++)
{ {
...@@ -168,7 +199,7 @@ void PixelProgram::applyShader(Int cMask[4], Int sMask[4], Int zMask[4]) ...@@ -168,7 +199,7 @@ void PixelProgram::applyShader(Int cMask[4], Int sMask[4], Int zMask[4])
if(spirvShader->getModes().ContainsKill) if(spirvShader->getModes().ContainsKill)
{ {
for(auto i = 0u; i < state.multiSampleCount; i++) for(auto i = sampleLoopInit; i < sampleLoopEnd; i++)
{ {
cMask[i] &= ~routine.killMask; cMask[i] &= ~routine.killMask;
} }
...@@ -179,7 +210,7 @@ void PixelProgram::applyShader(Int cMask[4], Int sMask[4], Int zMask[4]) ...@@ -179,7 +210,7 @@ void PixelProgram::applyShader(Int cMask[4], Int sMask[4], Int zMask[4])
{ {
auto outputSampleMask = As<SIMD::Int>(routine.getVariable(it->second.Id)[it->second.FirstComponent]); auto outputSampleMask = As<SIMD::Int>(routine.getVariable(it->second.Id)[it->second.FirstComponent]);
for(auto i = 0u; i < state.multiSampleCount; i++) for(auto i = sampleLoopInit; i < sampleLoopEnd; i++)
{ {
cMask[i] &= SignMask(CmpNEQ(outputSampleMask & SIMD::Int(1 << i), SIMD::Int(0))); cMask[i] &= SignMask(CmpNEQ(outputSampleMask & SIMD::Int(1 << i), SIMD::Int(0)));
} }
...@@ -192,14 +223,19 @@ void PixelProgram::applyShader(Int cMask[4], Int sMask[4], Int zMask[4]) ...@@ -192,14 +223,19 @@ void PixelProgram::applyShader(Int cMask[4], Int sMask[4], Int zMask[4])
} }
} }
Bool PixelProgram::alphaTest(Int cMask[4]) Bool PixelProgram::alphaTest(Int cMask[4], int sampleId)
{ {
if(!state.alphaToCoverage) if(!state.alphaToCoverage)
{ {
return true; return true;
} }
alphaToCoverage(cMask, c[0].w); alphaToCoverage(cMask, c[0].w, sampleId);
if(sampleId >= 0)
{
return cMask[sampleId] != 0x0;
}
Int pass = cMask[0]; Int pass = cMask[0];
...@@ -211,8 +247,11 @@ Bool PixelProgram::alphaTest(Int cMask[4]) ...@@ -211,8 +247,11 @@ Bool PixelProgram::alphaTest(Int cMask[4])
return pass != 0x0; return pass != 0x0;
} }
void PixelProgram::rasterOperation(Pointer<Byte> cBuffer[4], Int &x, Int sMask[4], Int zMask[4], Int cMask[4]) void PixelProgram::rasterOperation(Pointer<Byte> cBuffer[4], Int &x, Int sMask[4], Int zMask[4], Int cMask[4], int sampleId)
{ {
unsigned int sampleLoopInit = (sampleId >= 0) ? sampleId : 0;
unsigned int sampleLoopEnd = (sampleId >= 0) ? sampleId + 1 : state.multiSampleCount;
for(int index = 0; index < RENDERTARGETS; index++) for(int index = 0; index < RENDERTARGETS; index++)
{ {
if(!state.colorWriteActive(index)) if(!state.colorWriteActive(index))
...@@ -237,7 +276,7 @@ void PixelProgram::rasterOperation(Pointer<Byte> cBuffer[4], Int &x, Int sMask[4 ...@@ -237,7 +276,7 @@ void PixelProgram::rasterOperation(Pointer<Byte> cBuffer[4], Int &x, Int sMask[4
case VK_FORMAT_A8B8G8R8_SRGB_PACK32: case VK_FORMAT_A8B8G8R8_SRGB_PACK32:
case VK_FORMAT_A2B10G10R10_UNORM_PACK32: case VK_FORMAT_A2B10G10R10_UNORM_PACK32:
case VK_FORMAT_A2R10G10B10_UNORM_PACK32: case VK_FORMAT_A2R10G10B10_UNORM_PACK32:
for(unsigned int q = 0; q < state.multiSampleCount; q++) for(unsigned int q = sampleLoopInit; q < sampleLoopEnd; q++)
{ {
if(state.multiSampleMask & (1 << q)) if(state.multiSampleMask & (1 << q))
{ {
...@@ -283,7 +322,7 @@ void PixelProgram::rasterOperation(Pointer<Byte> cBuffer[4], Int &x, Int sMask[4 ...@@ -283,7 +322,7 @@ void PixelProgram::rasterOperation(Pointer<Byte> cBuffer[4], Int &x, Int sMask[4
case VK_FORMAT_A8B8G8R8_SINT_PACK32: case VK_FORMAT_A8B8G8R8_SINT_PACK32:
case VK_FORMAT_A2B10G10R10_UINT_PACK32: case VK_FORMAT_A2B10G10R10_UINT_PACK32:
case VK_FORMAT_A2R10G10B10_UINT_PACK32: case VK_FORMAT_A2R10G10B10_UINT_PACK32:
for(unsigned int q = 0; q < state.multiSampleCount; q++) for(unsigned int q = sampleLoopInit; q < sampleLoopEnd; q++)
{ {
if(state.multiSampleMask & (1 << q)) if(state.multiSampleMask & (1 << q))
{ {
......
...@@ -34,10 +34,10 @@ public: ...@@ -34,10 +34,10 @@ public:
virtual ~PixelProgram() {} virtual ~PixelProgram() {}
protected: protected:
virtual void setBuiltins(Int &x, Int &y, Float4 (&z)[4], Float4 &w, Int cMask[4]); virtual void setBuiltins(Int &x, Int &y, Float4 (&z)[4], Float4 &w, Int cMask[4], int sampleId);
virtual void applyShader(Int cMask[4], Int sMask[4], Int zMask[4]); virtual void applyShader(Int cMask[4], Int sMask[4], Int zMask[4], int sampleId);
virtual Bool alphaTest(Int cMask[4]); virtual Bool alphaTest(Int cMask[4], int sampleId);
virtual void rasterOperation(Pointer<Byte> cBuffer[4], Int &x, Int sMask[4], Int zMask[4], Int cMask[4]); virtual void rasterOperation(Pointer<Byte> cBuffer[4], Int &x, Int sMask[4], Int zMask[4], Int cMask[4], int sampleId);
private: private:
// Color outputs // Color outputs
...@@ -48,6 +48,7 @@ private: ...@@ -48,6 +48,7 @@ private:
Int4 maskAny(Int cMask[4]) const; Int4 maskAny(Int cMask[4]) const;
Int4 maskAny(Int cMask[4], Int sMask[4], Int zMask[4]) const; Int4 maskAny(Int cMask[4], Int sMask[4], Int zMask[4]) const;
Int4 maskAny(Int cMask, Int sMask, Int zMask) const;
}; };
} // namespace sw } // namespace sw
......
...@@ -45,15 +45,15 @@ protected: ...@@ -45,15 +45,15 @@ protected:
// Depth output // Depth output
Float4 oDepth; Float4 oDepth;
virtual void setBuiltins(Int &x, Int &y, Float4 (&z)[4], Float4 &w, Int cMask[4]) = 0; virtual void setBuiltins(Int &x, Int &y, Float4 (&z)[4], Float4 &w, Int cMask[4], int sampleId) = 0;
virtual void applyShader(Int cMask[4], Int sMask[4], Int zMask[4]) = 0; virtual void applyShader(Int cMask[4], Int sMask[4], Int zMask[4], int sampleId) = 0;
virtual Bool alphaTest(Int cMask[4]) = 0; virtual Bool alphaTest(Int cMask[4], int sampleId) = 0;
virtual void rasterOperation(Pointer<Byte> cBuffer[4], Int &x, Int sMask[4], Int zMask[4], Int cMask[4]) = 0; virtual void rasterOperation(Pointer<Byte> cBuffer[4], Int &x, Int sMask[4], Int zMask[4], Int cMask[4], int sampleId) = 0;
void quad(Pointer<Byte> cBuffer[4], Pointer<Byte> &zBuffer, Pointer<Byte> &sBuffer, Int cMask[4], Int &x, Int &y) override; void quad(Pointer<Byte> cBuffer[4], Pointer<Byte> &zBuffer, Pointer<Byte> &sBuffer, Int cMask[4], Int &x, Int &y) override;
void alphaTest(Int &aMask, const Short4 &alpha); void alphaTest(Int &aMask, const Short4 &alpha);
void alphaToCoverage(Int cMask[4], const Float4 &alpha); void alphaToCoverage(Int cMask[4], const Float4 &alpha, int sampleId);
// Raster operations // Raster operations
void alphaBlend(int index, const Pointer<Byte> &cBuffer, Vector4s &current, const Int &x); void alphaBlend(int index, const Pointer<Byte> &cBuffer, Vector4s &current, const Int &x);
......
...@@ -2471,7 +2471,10 @@ void SpirvShader::emitEpilog(SpirvRoutine *routine) const ...@@ -2471,7 +2471,10 @@ void SpirvShader::emitEpilog(SpirvRoutine *routine) const
break; break;
} }
} }
}
void SpirvShader::clearPhis(SpirvRoutine *routine) const
{
// Clear phis that are no longer used. This serves two purposes: // Clear phis that are no longer used. This serves two purposes:
// (1) The phi rr::Variables are destructed, preventing pointless // (1) The phi rr::Variables are destructed, preventing pointless
// materialization. // materialization.
......
...@@ -784,6 +784,7 @@ public: ...@@ -784,6 +784,7 @@ public:
void emitProlog(SpirvRoutine *routine) const; void emitProlog(SpirvRoutine *routine) const;
void emit(SpirvRoutine *routine, RValue<SIMD::Int> const &activeLaneMask, RValue<SIMD::Int> const &storesAndAtomicsMask, const vk::DescriptorSet::Bindings &descriptorSets) const; void emit(SpirvRoutine *routine, RValue<SIMD::Int> const &activeLaneMask, RValue<SIMD::Int> const &storesAndAtomicsMask, const vk::DescriptorSet::Bindings &descriptorSets) const;
void emitEpilog(SpirvRoutine *routine) const; void emitEpilog(SpirvRoutine *routine) const;
void clearPhis(SpirvRoutine *routine) const;
bool containsImageWrite() const { return imageWriteEmitted; } bool containsImageWrite() const { return imageWriteEmitted; }
......
...@@ -83,6 +83,7 @@ void VertexProgram::program(Pointer<UInt> &batch, UInt &vertexCount) ...@@ -83,6 +83,7 @@ void VertexProgram::program(Pointer<UInt> &batch, UInt &vertexCount)
spirvShader->emit(&routine, activeLaneMask, storesAndAtomicsMask, descriptorSets); spirvShader->emit(&routine, activeLaneMask, storesAndAtomicsMask, descriptorSets);
spirvShader->emitEpilog(&routine); spirvShader->emitEpilog(&routine);
spirvShader->clearPhis(&routine);
} }
} // namespace sw } // namespace sw
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