Commit 68d9ad83 by Nicolas Capens Committed by Nicolas Capens

Remove the sw::Color<> class

It was only still being used to store framebuffer blend constants. Use sw::float4 instead. Bug: b/146224130 Change-Id: Ie5660e237f4b675564d5a492f9d0a38505d07953 Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/43388Tested-by: 's avatarNicolas Capens <nicolascapens@google.com> Reviewed-by: 's avatarAlexis Hétu <sugoi@google.com> Reviewed-by: 's avatarBen Clayton <bclayton@google.com> Kokoro-Presubmit: kokoro <noreply+kokoro@google.com>
parent 9aff7ae7
...@@ -42,7 +42,6 @@ swiftshader_source_set("Device") { ...@@ -42,7 +42,6 @@ swiftshader_source_set("Device") {
"BC_Decoder.cpp", "BC_Decoder.cpp",
"Blitter.cpp", "Blitter.cpp",
"Clipper.cpp", "Clipper.cpp",
"Color.cpp",
"Config.cpp", "Config.cpp",
"Context.cpp", "Context.cpp",
"ETC_Decoder.cpp", "ETC_Decoder.cpp",
......
// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "Color.hpp"
namespace sw {
} // namespace sw
// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef sw_Color_hpp
#define sw_Color_hpp
#include "System/Math.hpp"
#include "System/Types.hpp"
namespace sw {
template<class T>
struct Color
{
Color();
Color(const Color<byte> &c);
Color(const Color<short> &c);
Color(const Color<float> &c);
Color(int c);
Color(unsigned short c);
Color(unsigned long c);
Color(unsigned int c);
Color(T r, T g, T b, T a = 1);
operator unsigned int() const;
T &operator[](int i);
const T &operator[](int i) const;
Color<T> operator+() const;
Color<T> operator-() const;
Color<T> &operator=(const Color<T> &c);
Color<T> &operator+=(const Color<T> &c);
Color<T> &operator*=(float l);
static Color<T> gradient(const Color<T> &c1, const Color<T> &c2, float d);
static Color<T> shade(const Color<T> &c1, const Color<T> &c2, float d);
template<class S>
friend Color<S> operator+(const Color<S> &c1, const Color<S> &c2);
template<class S>
friend Color<S> operator-(const Color<S> &c1, const Color<S> &c2);
template<class S>
friend Color<S> operator*(float l, const Color<S> &c);
template<class S>
friend Color<S> operator*(const Color<S> &c1, const Color<S> &c2);
template<class S>
friend Color<S> operator/(const Color<S> &c, float l);
T r;
T g;
T b;
T a;
};
} // namespace sw
#include "System/Math.hpp"
namespace sw {
template<class T>
inline Color<T>::Color()
{
}
template<>
inline Color<byte>::Color(const Color<byte> &c)
{
r = c.r;
g = c.g;
b = c.b;
a = c.a;
}
template<>
inline Color<byte>::Color(const Color<short> &c)
{
r = static_cast<byte>(clamp(c.r >> 4, 0, 255));
g = static_cast<byte>(clamp(c.g >> 4, 0, 255));
b = static_cast<byte>(clamp(c.b >> 4, 0, 255));
a = static_cast<byte>(clamp(c.a >> 4, 0, 255));
}
template<>
inline Color<byte>::Color(const Color<float> &c)
{
r = static_cast<byte>(ifloor(clamp(c.r * 256.0f, 0.0f, 255.0f)));
g = static_cast<byte>(ifloor(clamp(c.g * 256.0f, 0.0f, 255.0f)));
b = static_cast<byte>(ifloor(clamp(c.b * 256.0f, 0.0f, 255.0f)));
a = static_cast<byte>(ifloor(clamp(c.a * 256.0f, 0.0f, 255.0f)));
}
template<>
inline Color<short>::Color(const Color<short> &c)
{
r = c.r;
g = c.g;
b = c.b;
a = c.a;
}
template<>
inline Color<short>::Color(const Color<byte> &c)
{
r = c.r << 4;
g = c.g << 4;
b = c.b << 4;
a = c.a << 4;
}
template<>
inline Color<float>::Color(const Color<float> &c)
{
r = c.r;
g = c.g;
b = c.b;
a = c.a;
}
template<>
inline Color<short>::Color(const Color<float> &c)
{
r = static_cast<short>(iround(clamp(c.r * 4095.0f, -4096.0f, 4095.0f)));
g = static_cast<short>(iround(clamp(c.g * 4095.0f, -4096.0f, 4095.0f)));
b = static_cast<short>(iround(clamp(c.b * 4095.0f, -4096.0f, 4095.0f)));
a = static_cast<short>(iround(clamp(c.a * 4095.0f, -4096.0f, 4095.0f)));
}
template<>
inline Color<float>::Color(const Color<byte> &c)
{
r = c.r / 255.0f;
g = c.g / 255.0f;
b = c.b / 255.0f;
a = c.a / 255.0f;
}
template<>
inline Color<float>::Color(const Color<short> &c)
{
r = c.r / 4095.0f;
g = c.g / 4095.0f;
b = c.b / 4095.0f;
a = c.a / 4095.0f;
}
template<>
inline Color<float>::Color(unsigned short c)
{
r = (float)(c & 0xF800) / (float)0xF800;
g = (float)(c & 0x07E0) / (float)0x07E0;
b = (float)(c & 0x001F) / (float)0x001F;
a = 1;
}
template<>
inline Color<short>::Color(unsigned short c)
{
// 4.12 fixed-point format
r = ((c & 0xF800) >> 4) + ((c & 0xF800) >> 9) + ((c & 0xF800) >> 14);
g = ((c & 0x07E0) << 1) + ((c & 0x07E0) >> 5);
b = ((c & 0x001F) << 7) + ((c & 0x001F) << 2) + ((c & 0x001F) >> 3);
a = 0x1000;
}
template<>
inline Color<byte>::Color(unsigned short c)
{
r = (byte)(((c & 0xF800) >> 8) + ((c & 0xE000) >> 13));
g = (byte)(((c & 0x07E0) >> 3) + ((c & 0x0600) >> 9));
b = (byte)(((c & 0x001F) << 3) + ((c & 0x001C) >> 2));
a = 0xFF;
}
template<>
inline Color<float>::Color(int c)
{
const float d = 1.0f / 255.0f;
r = (float)((c & 0x00FF0000) >> 16) * d;
g = (float)((c & 0x0000FF00) >> 8) * d;
b = (float)((c & 0x000000FF) >> 0) * d;
a = (float)((c & 0xFF000000) >> 24) * d;
}
template<>
inline Color<short>::Color(int c)
{
// 4.12 fixed-point format
r = (short)((c & 0x00FF0000) >> 12);
g = (short)((c & 0x0000FF00) >> 4);
b = (short)((c & 0x000000FF) << 4);
a = (short)((c & 0xFF000000) >> 20);
}
template<>
inline Color<byte>::Color(int c)
{
r = (byte)((c & 0x00FF0000) >> 16);
g = (byte)((c & 0x0000FF00) >> 8);
b = (byte)((c & 0x000000FF) >> 0);
a = (byte)((c & 0xFF000000) >> 24);
}
template<>
inline Color<float>::Color(unsigned int c)
{
const float d = 1.0f / 255.0f;
r = (float)((c & 0x00FF0000) >> 16) * d;
g = (float)((c & 0x0000FF00) >> 8) * d;
b = (float)((c & 0x000000FF) >> 0) * d;
a = (float)((c & 0xFF000000) >> 24) * d;
}
template<>
inline Color<short>::Color(unsigned int c)
{
// 4.12 fixed-point format
r = (short)((c & 0x00FF0000) >> 12);
g = (short)((c & 0x0000FF00) >> 4);
b = (short)((c & 0x000000FF) << 4);
a = (short)((c & 0xFF000000) >> 20);
}
template<>
inline Color<byte>::Color(unsigned int c)
{
r = (byte)((c & 0x00FF0000) >> 16);
g = (byte)((c & 0x0000FF00) >> 8);
b = (byte)((c & 0x000000FF) >> 0);
a = (byte)((c & 0xFF000000) >> 24);
}
template<>
inline Color<float>::Color(unsigned long c)
{
const float d = 1.0f / 255.0f;
r = (float)((c & 0x00FF0000) >> 16) * d;
g = (float)((c & 0x0000FF00) >> 8) * d;
b = (float)((c & 0x000000FF) >> 0) * d;
a = (float)((c & 0xFF000000) >> 24) * d;
}
template<>
inline Color<short>::Color(unsigned long c)
{
// 4.12 fixed-point format
r = (short)((c & 0x00FF0000) >> 12);
g = (short)((c & 0x0000FF00) >> 4);
b = (short)((c & 0x000000FF) << 4);
a = (short)((c & 0xFF000000) >> 20);
}
template<>
inline Color<byte>::Color(unsigned long c)
{
r = (byte)((c & 0x00FF0000) >> 16);
g = (byte)((c & 0x0000FF00) >> 8);
b = (byte)((c & 0x000000FF) >> 0);
a = (byte)((c & 0xFF000000) >> 24);
}
template<class T>
inline Color<T>::Color(T r_, T g_, T b_, T a_)
{
r = r_;
g = g_;
b = b_;
a = a_;
}
template<>
inline Color<float>::operator unsigned int() const
{
return ((unsigned int)min(b * 255.0f, 255.0f) << 0) |
((unsigned int)min(g * 255.0f, 255.0f) << 8) |
((unsigned int)min(r * 255.0f, 255.0f) << 16) |
((unsigned int)min(a * 255.0f, 255.0f) << 24);
}
template<>
inline Color<short>::operator unsigned int() const
{
return ((unsigned int)min(b >> 4, 255) << 0) |
((unsigned int)min(g >> 4, 255) << 8) |
((unsigned int)min(r >> 4, 255) << 16) |
((unsigned int)min(a >> 4, 255) << 24);
}
template<>
inline Color<byte>::operator unsigned int() const
{
return (b << 0) +
(g << 8) +
(r << 16) +
(a << 24);
}
template<class T>
inline T &Color<T>::operator[](int i)
{
return (&r)[i];
}
template<class T>
inline const T &Color<T>::operator[](int i) const
{
return (&r)[i];
}
template<class T>
inline Color<T> Color<T>::operator+() const
{
return *this;
}
template<class T>
inline Color<T> Color<T>::operator-() const
{
return Color(-r, -g, -b, -a);
}
template<class T>
inline Color<T> &Color<T>::operator=(const Color &c)
{
r = c.r;
g = c.g;
b = c.b;
a = c.a;
return *this;
}
template<class T>
inline Color<T> &Color<T>::operator+=(const Color &c)
{
r += c.r;
g += c.g;
b += c.b;
a += c.a;
return *this;
}
template<class T>
inline Color<T> &Color<T>::operator*=(float l)
{
*this = l * *this;
return *this;
}
template<class T>
inline Color<T> operator+(const Color<T> &c1, const Color<T> &c2)
{
return Color<T>(c1.r + c2.r,
c1.g + c2.g,
c1.b + c2.b,
c1.a + c2.a);
}
template<class T>
inline Color<T> operator-(const Color<T> &c1, const Color<T> &c2)
{
return Color<T>(c1.r - c2.r,
c1.g - c2.g,
c1.b - c2.b,
c1.a - c2.a);
}
template<class T>
inline Color<T> operator*(float l, const Color<T> &c)
{
T r = (T)(l * c.r);
T g = (T)(l * c.g);
T b = (T)(l * c.b);
T a = (T)(l * c.a);
return Color<T>(r, g, b, a);
}
template<class T>
inline Color<T> operator*(const Color<T> &c1, const Color<T> &c2)
{
T r = c1.r * c2.r;
T g = c1.g * c2.g;
T b = c1.b * c2.b;
T a = c1.a * c2.a;
return Color<T>(r, g, b, a);
}
template<>
inline Color<short> operator*(const Color<short> &c1, const Color<short> &c2)
{
short r = c1.r * c2.r >> 12;
short g = c1.g * c2.g >> 12;
short b = c1.b * c2.b >> 12;
short a = c1.a * c2.a >> 12;
return Color<short>(r, g, b, a);
}
template<>
inline Color<byte> operator*(const Color<byte> &c1, const Color<byte> &c2)
{
byte r = c1.r * c2.r >> 8;
byte g = c1.g * c2.g >> 8;
byte b = c1.b * c2.b >> 8;
byte a = c1.a * c2.a >> 8;
return Color<byte>(r, g, b, a);
}
template<class T>
inline Color<T> operator/(const Color<T> &c, float l)
{
l = 1.0f / l;
T r = (T)(l * c.r);
T g = (T)(l * c.g);
T b = (T)(l * c.b);
T a = (T)(l * c.a);
return Color<T>(r, g, b, a);
}
template<class T>
inline Color<T> Color<T>::gradient(const Color<T> &c1, const Color<T> &c2, float d)
{
d = 1.0f / d;
T r = (c2.r - c1.r) * d;
T g = (c2.g - c1.g) * d;
T b = (c2.b - c1.b) * d;
T a = (c2.a - c1.a) * d;
return Color<T>(r, g, b, a);
}
template<class T>
inline Color<T> Color<T>::shade(const Color<T> &c1, const Color<T> &c2, float d)
{
T r = c1.r + (T)(d * (c2.r - c1.r));
T g = c1.g + (T)(d * (c2.g - c1.g));
T b = c1.b + (T)(d * (c2.b - c1.b));
T a = c1.a + (T)(d * (c2.a - c1.a));
return Color<T>(r, g, b, a);
}
} // namespace sw
#endif // sw_Color_hpp
...@@ -59,28 +59,28 @@ PixelProcessor::~PixelProcessor() ...@@ -59,28 +59,28 @@ PixelProcessor::~PixelProcessor()
routineCache = nullptr; routineCache = nullptr;
} }
void PixelProcessor::setBlendConstant(const Color<float> &blendConstant) void PixelProcessor::setBlendConstant(const float4 &blendConstant)
{ {
// TODO(b/140935644): Check if clamp is required // TODO(b/140935644): Check if clamp is required
factor.blendConstant4W[0] = word4(static_cast<uint16_t>(iround(0xFFFFu * blendConstant.r))); factor.blendConstant4W[0] = word4(static_cast<uint16_t>(iround(0xFFFFu * blendConstant.x)));
factor.blendConstant4W[1] = word4(static_cast<uint16_t>(iround(0xFFFFu * blendConstant.g))); factor.blendConstant4W[1] = word4(static_cast<uint16_t>(iround(0xFFFFu * blendConstant.y)));
factor.blendConstant4W[2] = word4(static_cast<uint16_t>(iround(0xFFFFu * blendConstant.b))); factor.blendConstant4W[2] = word4(static_cast<uint16_t>(iround(0xFFFFu * blendConstant.z)));
factor.blendConstant4W[3] = word4(static_cast<uint16_t>(iround(0xFFFFu * blendConstant.a))); factor.blendConstant4W[3] = word4(static_cast<uint16_t>(iround(0xFFFFu * blendConstant.w)));
factor.invBlendConstant4W[0] = word4(0xFFFFu - factor.blendConstant4W[0][0]); factor.invBlendConstant4W[0] = word4(0xFFFFu - factor.blendConstant4W[0][0]);
factor.invBlendConstant4W[1] = word4(0xFFFFu - factor.blendConstant4W[1][0]); factor.invBlendConstant4W[1] = word4(0xFFFFu - factor.blendConstant4W[1][0]);
factor.invBlendConstant4W[2] = word4(0xFFFFu - factor.blendConstant4W[2][0]); factor.invBlendConstant4W[2] = word4(0xFFFFu - factor.blendConstant4W[2][0]);
factor.invBlendConstant4W[3] = word4(0xFFFFu - factor.blendConstant4W[3][0]); factor.invBlendConstant4W[3] = word4(0xFFFFu - factor.blendConstant4W[3][0]);
factor.blendConstant4F[0] = float4(blendConstant.r); factor.blendConstant4F[0] = float4(blendConstant.x);
factor.blendConstant4F[1] = float4(blendConstant.g); factor.blendConstant4F[1] = float4(blendConstant.y);
factor.blendConstant4F[2] = float4(blendConstant.b); factor.blendConstant4F[2] = float4(blendConstant.z);
factor.blendConstant4F[3] = float4(blendConstant.a); factor.blendConstant4F[3] = float4(blendConstant.w);
factor.invBlendConstant4F[0] = float4(1 - blendConstant.r); factor.invBlendConstant4F[0] = float4(1 - blendConstant.x);
factor.invBlendConstant4F[1] = float4(1 - blendConstant.g); factor.invBlendConstant4F[1] = float4(1 - blendConstant.y);
factor.invBlendConstant4F[2] = float4(1 - blendConstant.b); factor.invBlendConstant4F[2] = float4(1 - blendConstant.z);
factor.invBlendConstant4F[3] = float4(1 - blendConstant.a); factor.invBlendConstant4F[3] = float4(1 - blendConstant.w);
} }
void PixelProcessor::setRoutineCacheSize(int cacheSize) void PixelProcessor::setRoutineCacheSize(int cacheSize)
......
...@@ -15,7 +15,6 @@ ...@@ -15,7 +15,6 @@
#ifndef sw_PixelProcessor_hpp #ifndef sw_PixelProcessor_hpp
#define sw_PixelProcessor_hpp #define sw_PixelProcessor_hpp
#include "Color.hpp"
#include "Context.hpp" #include "Context.hpp"
#include "Memset.hpp" #include "Memset.hpp"
#include "RoutineCache.hpp" #include "RoutineCache.hpp"
...@@ -149,7 +148,7 @@ public: ...@@ -149,7 +148,7 @@ public:
virtual ~PixelProcessor(); virtual ~PixelProcessor();
void setBlendConstant(const Color<float> &blendConstant); void setBlendConstant(const float4 &blendConstant);
protected: protected:
const State update(const Context *context) const; const State update(const Context *context) const;
......
...@@ -15,7 +15,6 @@ ...@@ -15,7 +15,6 @@
#ifndef sw_Sampler_hpp #ifndef sw_Sampler_hpp
#define sw_Sampler_hpp #define sw_Sampler_hpp
#include "Device/Color.hpp"
#include "Device/Config.hpp" #include "Device/Config.hpp"
#include "System/Types.hpp" #include "System/Types.hpp"
#include "Vulkan/VkFormat.h" #include "Vulkan/VkFormat.h"
......
...@@ -15,7 +15,6 @@ ...@@ -15,7 +15,6 @@
#ifndef Vertex_hpp #ifndef Vertex_hpp
#define Vertex_hpp #define Vertex_hpp
#include "Color.hpp"
#include "Device/Config.hpp" #include "Device/Config.hpp"
#include "System/Types.hpp" #include "System/Types.hpp"
......
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
#include "Device/Config.hpp" #include "Device/Config.hpp"
#include "Device/Sampler.hpp" #include "Device/Sampler.hpp"
#include "System/Debug.hpp" #include "System/Debug.hpp"
#include "System/Math.hpp"
#include "System/Types.hpp" #include "System/Types.hpp"
#include "Vulkan/VkConfig.h" #include "Vulkan/VkConfig.h"
#include "Vulkan/VkDescriptorSet.hpp" #include "Vulkan/VkDescriptorSet.hpp"
......
...@@ -17,7 +17,6 @@ ...@@ -17,7 +17,6 @@
#include "ShaderCore.hpp" #include "ShaderCore.hpp"
#include "SpirvShader.hpp" #include "SpirvShader.hpp"
#include "Device/Color.hpp"
#include "Device/VertexProcessor.hpp" #include "Device/VertexProcessor.hpp"
namespace vk { namespace vk {
......
...@@ -18,7 +18,6 @@ ...@@ -18,7 +18,6 @@
#include "VkConfig.h" #include "VkConfig.h"
#include "VkDescriptorSet.hpp" #include "VkDescriptorSet.hpp"
#include "VkObject.hpp" #include "VkObject.hpp"
#include "Device/Color.hpp"
#include "Device/Context.hpp" #include "Device/Context.hpp"
#include <memory> #include <memory>
...@@ -159,7 +158,7 @@ public: ...@@ -159,7 +158,7 @@ public:
{ {
VkViewport viewport; VkViewport viewport;
VkRect2D scissor; VkRect2D scissor;
sw::Color<float> blendConstants; sw::float4 blendConstants;
float depthBiasConstantFactor = 0.0f; float depthBiasConstantFactor = 0.0f;
float depthBiasClamp = 0.0f; float depthBiasClamp = 0.0f;
float depthBiasSlopeFactor = 0.0f; float depthBiasSlopeFactor = 0.0f;
......
...@@ -398,10 +398,10 @@ GraphicsPipeline::GraphicsPipeline(const VkGraphicsPipelineCreateInfo *pCreateIn ...@@ -398,10 +398,10 @@ GraphicsPipeline::GraphicsPipeline(const VkGraphicsPipelineCreateInfo *pCreateIn
if(!hasDynamicState(VK_DYNAMIC_STATE_BLEND_CONSTANTS)) if(!hasDynamicState(VK_DYNAMIC_STATE_BLEND_CONSTANTS))
{ {
blendConstants.r = colorBlendState->blendConstants[0]; blendConstants.x = colorBlendState->blendConstants[0];
blendConstants.g = colorBlendState->blendConstants[1]; blendConstants.y = colorBlendState->blendConstants[1];
blendConstants.b = colorBlendState->blendConstants[2]; blendConstants.z = colorBlendState->blendConstants[2];
blendConstants.a = colorBlendState->blendConstants[3]; blendConstants.w = colorBlendState->blendConstants[3];
} }
for(auto i = 0u; i < colorBlendState->attachmentCount; i++) for(auto i = 0u; i < colorBlendState->attachmentCount; i++)
...@@ -545,7 +545,7 @@ const VkViewport &GraphicsPipeline::getViewport() const ...@@ -545,7 +545,7 @@ const VkViewport &GraphicsPipeline::getViewport() const
return viewport; return viewport;
} }
const sw::Color<float> &GraphicsPipeline::getBlendConstants() const const sw::float4 &GraphicsPipeline::getBlendConstants() const
{ {
return blendConstants; return blendConstants;
} }
......
...@@ -102,7 +102,7 @@ public: ...@@ -102,7 +102,7 @@ public:
const sw::Context &getContext() const; const sw::Context &getContext() const;
const VkRect2D &getScissor() const; const VkRect2D &getScissor() const;
const VkViewport &getViewport() const; const VkViewport &getViewport() const;
const sw::Color<float> &getBlendConstants() const; const sw::float4 &getBlendConstants() const;
bool hasDynamicState(VkDynamicState dynamicState) const; bool hasDynamicState(VkDynamicState dynamicState) const;
bool hasPrimitiveRestartEnable() const { return primitiveRestartEnable; } bool hasPrimitiveRestartEnable() const { return primitiveRestartEnable; }
...@@ -117,7 +117,7 @@ private: ...@@ -117,7 +117,7 @@ private:
sw::Context context; sw::Context context;
VkRect2D scissor; VkRect2D scissor;
VkViewport viewport; VkViewport viewport;
sw::Color<float> blendConstants; sw::float4 blendConstants;
}; };
class ComputePipeline : public Pipeline, public ObjectBase<ComputePipeline, VkPipeline> class ComputePipeline : public Pipeline, public ObjectBase<ComputePipeline, VkPipeline>
......
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