Commit e899321b by Nicolas Capens Committed by Nicolas Capens

Refactor remaining cases of memset(this, ...)

Use Memset<T> as the first base class of cache key types to ensure they get initialized before any other base classes or members get constructed. Bug: b/134932616 Change-Id: I8f28252696d6e017db11da068180d2425cdf1d57 Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/33249Tested-by: 's avatarNicolas Capens <nicolascapens@google.com> Presubmit-Ready: Nicolas Capens <nicolascapens@google.com> Kokoro-Presubmit: kokoro <noreply+kokoro@google.com> Reviewed-by: 's avatarBen Clayton <bclayton@google.com>
parent 302a9723
...@@ -20,16 +20,16 @@ ...@@ -20,16 +20,16 @@
#include "Vulkan/VkDebug.hpp" #include "Vulkan/VkDebug.hpp"
#include "Vulkan/VkImageView.hpp" #include "Vulkan/VkImageView.hpp"
#include <string.h> #include <cstring>
namespace sw namespace sw
{ {
unsigned int PixelProcessor::States::computeHash() uint32_t PixelProcessor::States::computeHash()
{ {
unsigned int *state = (unsigned int*)this; uint32_t *state = reinterpret_cast<uint32_t*>(this);
unsigned int hash = 0; uint32_t hash = 0;
for(unsigned int i = 0; i < sizeof(States) / 4; i++) for(unsigned int i = 0; i < sizeof(States) / sizeof(uint32_t); i++)
{ {
hash ^= state[i]; hash ^= state[i];
} }
...@@ -37,11 +37,6 @@ namespace sw ...@@ -37,11 +37,6 @@ namespace sw
return hash; return hash;
} }
PixelProcessor::State::State()
{
memset(this, 0, sizeof(State));
}
bool PixelProcessor::State::operator==(const State &state) const bool PixelProcessor::State::operator==(const State &state) const
{ {
if(hash != state.hash) if(hash != state.hash)
...@@ -49,6 +44,7 @@ namespace sw ...@@ -49,6 +44,7 @@ namespace sw
return false; return false;
} }
static_assert(is_memcmparable<State>::value, "Cannot memcmp State");
return memcmp(static_cast<const States*>(this), static_cast<const States*>(&state), sizeof(States)) == 0; return memcmp(static_cast<const States*>(this), static_cast<const States*>(&state), sizeof(States)) == 0;
} }
......
...@@ -30,9 +30,11 @@ namespace sw ...@@ -30,9 +30,11 @@ namespace sw
class PixelProcessor class PixelProcessor
{ {
public: public:
struct States struct States : Memset<States>
{ {
unsigned int computeHash(); States() : Memset(this, 0) {}
uint32_t computeHash();
uint64_t shaderID; uint64_t shaderID;
...@@ -70,8 +72,6 @@ namespace sw ...@@ -70,8 +72,6 @@ namespace sw
struct State : States struct State : States
{ {
State();
bool operator==(const State &state) const; bool operator==(const State &state) const;
int colorWriteActive(int index) const int colorWriteActive(int index) const
...@@ -79,7 +79,7 @@ namespace sw ...@@ -79,7 +79,7 @@ namespace sw
return (colorWriteMask >> (index * 4)) & 0xF; return (colorWriteMask >> (index * 4)) & 0xF;
} }
unsigned int hash; uint32_t hash;
}; };
struct Stencil struct Stencil
......
...@@ -23,14 +23,16 @@ ...@@ -23,14 +23,16 @@
#include "Vulkan/VkDebug.hpp" #include "Vulkan/VkDebug.hpp"
#include "Pipeline/SpirvShader.hpp" #include "Pipeline/SpirvShader.hpp"
#include <cstring>
namespace sw namespace sw
{ {
unsigned int SetupProcessor::States::computeHash() uint32_t SetupProcessor::States::computeHash()
{ {
unsigned int *state = (unsigned int*)this; uint32_t *state = reinterpret_cast<uint32_t*>(this);
unsigned int hash = 0; uint32_t hash = 0;
for(unsigned int i = 0; i < sizeof(States) / 4; i++) for(unsigned int i = 0; i < sizeof(States) / sizeof(uint32_t); i++)
{ {
hash ^= state[i]; hash ^= state[i];
} }
...@@ -38,11 +40,6 @@ namespace sw ...@@ -38,11 +40,6 @@ namespace sw
return hash; return hash;
} }
SetupProcessor::State::State(int i)
{
memset(this, 0, sizeof(State));
}
bool SetupProcessor::State::operator==(const State &state) const bool SetupProcessor::State::operator==(const State &state) const
{ {
if(hash != state.hash) if(hash != state.hash)
...@@ -50,6 +47,7 @@ namespace sw ...@@ -50,6 +47,7 @@ namespace sw
return false; return false;
} }
static_assert(is_memcmparable<State>::value, "Cannot memcmp States");
return memcmp(static_cast<const States*>(this), static_cast<const States*>(&state), sizeof(States)) == 0; return memcmp(static_cast<const States*>(this), static_cast<const States*>(&state), sizeof(States)) == 0;
} }
......
...@@ -32,9 +32,11 @@ namespace sw ...@@ -32,9 +32,11 @@ namespace sw
class SetupProcessor class SetupProcessor
{ {
public: public:
struct States struct States : Memset<States>
{ {
unsigned int computeHash(); States() : Memset(this, 0) {}
uint32_t computeHash();
bool isDrawPoint : 1; bool isDrawPoint : 1;
bool isDrawLine : 1; bool isDrawLine : 1;
...@@ -53,11 +55,9 @@ namespace sw ...@@ -53,11 +55,9 @@ namespace sw
struct State : States struct State : States
{ {
State(int i = 0);
bool operator==(const State &states) const; bool operator==(const State &states) const;
unsigned int hash; uint32_t hash;
}; };
typedef bool (*RoutinePointer)(Primitive *primitive, const Triangle *triangle, const Polygon *polygon, const DrawData *draw); typedef bool (*RoutinePointer)(Primitive *primitive, const Triangle *triangle, const Polygon *polygon, const DrawData *draw);
......
...@@ -19,7 +19,7 @@ ...@@ -19,7 +19,7 @@
#include "System/Math.hpp" #include "System/Math.hpp"
#include "Vulkan/VkDebug.hpp" #include "Vulkan/VkDebug.hpp"
#include <string.h> #include <cstring>
namespace sw namespace sw
{ {
...@@ -31,12 +31,12 @@ namespace sw ...@@ -31,12 +31,12 @@ namespace sw
} }
} }
unsigned int VertexProcessor::States::computeHash() uint32_t VertexProcessor::States::computeHash()
{ {
unsigned int *state = (unsigned int*)this; uint32_t *state = reinterpret_cast<uint32_t*>(this);
unsigned int hash = 0; uint32_t hash = 0;
for(unsigned int i = 0; i < sizeof(States) / 4; i++) for(unsigned int i = 0; i < sizeof(States) / sizeof(uint32_t); i++)
{ {
hash ^= state[i]; hash ^= state[i];
} }
...@@ -44,11 +44,6 @@ namespace sw ...@@ -44,11 +44,6 @@ namespace sw
return hash; return hash;
} }
VertexProcessor::State::State()
{
memset(this, 0, sizeof(State));
}
bool VertexProcessor::State::operator==(const State &state) const bool VertexProcessor::State::operator==(const State &state) const
{ {
if(hash != state.hash) if(hash != state.hash)
...@@ -56,6 +51,7 @@ namespace sw ...@@ -56,6 +51,7 @@ namespace sw
return false; return false;
} }
static_assert(is_memcmparable<State>::value, "Cannot memcmp States");
return memcmp(static_cast<const States*>(this), static_cast<const States*>(&state), sizeof(States)) == 0; return memcmp(static_cast<const States*>(this), static_cast<const States*>(&state), sizeof(States)) == 0;
} }
......
...@@ -49,9 +49,11 @@ namespace sw ...@@ -49,9 +49,11 @@ namespace sw
class VertexProcessor class VertexProcessor
{ {
public: public:
struct States struct States : Memset<States>
{ {
unsigned int computeHash(); States() : Memset(this, 0) {}
uint32_t computeHash();
uint64_t shaderID; uint64_t shaderID;
...@@ -73,11 +75,9 @@ namespace sw ...@@ -73,11 +75,9 @@ namespace sw
struct State : States struct State : States
{ {
State();
bool operator==(const State &state) const; bool operator==(const State &state) const;
unsigned int hash; uint32_t hash;
}; };
typedef void (*RoutinePointer)(Vertex *output, unsigned int *batch, VertexTask *vertexTask, DrawData *draw); typedef void (*RoutinePointer)(Vertex *output, unsigned int *batch, VertexTask *vertexTask, DrawData *draw);
......
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