Skip to content

Commit

Permalink
Convert Renderer::GetName to StringHash
Browse files Browse the repository at this point in the history
  • Loading branch information
sturnclaw committed Oct 11, 2021
1 parent d1ff88c commit 7754ea8
Show file tree
Hide file tree
Showing 24 changed files with 56 additions and 87 deletions.
5 changes: 2 additions & 3 deletions src/Background.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ namespace Background {
{
}

static size_t s_texture0Name = Graphics::Renderer::GetName("texture0");
void UniverseBox::Init()
{
// Load default cubemap
Expand Down Expand Up @@ -192,7 +191,7 @@ namespace Background {
m_cubemap.Reset(s_defaultCubeMap.Get());
}

m_material->SetTexture(s_texture0Name, m_cubemap.Get());
m_material->SetTexture("texture0"_hash, m_cubemap.Get());
}

Starfield::Starfield(Graphics::Renderer *renderer, Random &rand, const SystemPath *const systemPath, RefCountedPtr<Galaxy> galaxy)
Expand All @@ -217,7 +216,7 @@ namespace Background {

m_material.Reset(m_renderer->CreateMaterial("starfield", desc, stateDesc));
Graphics::Texture *texture = Graphics::TextureBuilder::Billboard("textures/star_point.png").GetOrCreateTexture(m_renderer, "billboard");
m_material->SetTexture(Graphics::Renderer::GetName("texture0"), texture);
m_material->SetTexture("texture0"_hash, texture);
m_material->emissive = Color::WHITE;

// Create material to be used with hyperjump 'star streaks'
Expand Down
4 changes: 2 additions & 2 deletions src/BaseSphere.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ void BaseSphere::DrawAtmosphereSurface(Graphics::Renderer *renderer,
renderer->GetStats().AddToStatCount(Graphics::Stats::STAT_ATMOSPHERES, 1);
}

static size_t s_baseSphereData = Graphics::Renderer::GetName("BaseSphereData");
static size_t s_numShadows = Graphics::Renderer::GetName("NumShadows");
static size_t s_baseSphereData = "BaseSphereData"_hash;
static size_t s_numShadows = "NumShadows"_hash;
void BaseSphere::SetMaterialParameters(const matrix4x4d &trans, const float radius, const std::vector<Camera::Shadow> &shadows, const AtmosphereParameters &ap)
{
BaseSphereDataBlock matData{};
Expand Down
4 changes: 2 additions & 2 deletions src/Beam.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ void Beam::BuildModel()
rsd.cullMode = Graphics::CULL_NONE;

s_sideMat.reset(Pi::renderer->CreateMaterial("unlit", desc, rsd));
s_sideMat->SetTexture(Graphics::Renderer::GetName("texture0"),
s_sideMat->SetTexture("texture0"_hash,
Graphics::TextureBuilder::Billboard("textures/beam_l.dds").GetOrCreateTexture(Pi::renderer, "billboard"));

s_glowMat.reset(Pi::renderer->CreateMaterial("unlit", desc, rsd));
s_glowMat->SetTexture(Graphics::Renderer::GetName("texture0"),
s_glowMat->SetTexture("texture0"_hash,
Graphics::TextureBuilder::Billboard("textures/projectile_w.dds").GetOrCreateTexture(Pi::renderer, "billboard"));

//zero at projectile position
Expand Down
2 changes: 1 addition & 1 deletion src/Camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ Camera::Camera(RefCountedPtr<CameraContext> context, Graphics::Renderer *rendere
rsd.primitiveType = Graphics::POINTS;

m_billboardMaterial.reset(m_renderer->CreateMaterial("billboards", desc, rsd));
m_billboardMaterial->SetTexture(Graphics::Renderer::GetName("texture0"),
m_billboardMaterial->SetTexture("texture0"_hash,
Graphics::TextureBuilder::Billboard("textures/planet_billboard.dds").GetOrCreateTexture(m_renderer, "billboard"));
}

Expand Down
6 changes: 3 additions & 3 deletions src/GasGiant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ bool GasGiant::AddTextureFaceResult(GasGiantJobs::STextureFaceResult *res)

// change the planet texture for the new higher resolution texture
if (m_surfaceMaterial.Get()) {
m_surfaceMaterial->SetTexture(Graphics::Renderer::GetName("texture0"),
m_surfaceMaterial->SetTexture("texture0"_hash,
m_surfaceTexture.Get());
m_surfaceTextureSmall.Reset();
}
Expand Down Expand Up @@ -507,7 +507,7 @@ bool GasGiant::AddGPUGenResult(GasGiantJobs::SGPUGenResult *res)

// change the planet texture for the new higher resolution texture
if (m_surfaceMaterial.Get()) {
m_surfaceMaterial->SetTexture(Graphics::Renderer::GetName("texture0"),
m_surfaceMaterial->SetTexture("texture0"_hash,
m_surfaceTexture.Get());
m_surfaceTextureSmall.Reset();
}
Expand Down Expand Up @@ -739,7 +739,7 @@ void GasGiant::SetUpMaterials()
// surface material is solid
Graphics::RenderStateDesc rsd;
m_surfaceMaterial.Reset(Pi::renderer->CreateMaterial("gassphere_base", surfDesc, rsd));
m_surfaceMaterial->SetTexture(Graphics::Renderer::GetName("texture0"),
m_surfaceMaterial->SetTexture("texture0"_hash,
m_surfaceTexture.Valid() ? m_surfaceTexture.Get() : m_surfaceTextureSmall.Get());

{
Expand Down
8 changes: 4 additions & 4 deletions src/GasGiantJobs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,9 @@ namespace GasGiantJobs {
m_material.reset(r->CreateMaterial("gen_gas_giant_colour", desc, rsd));

// setup noise textures
m_material->SetTexture(Graphics::Renderer::GetName("permTexture"),
m_material->SetTexture("permTexture"_hash,
Graphics::TextureBuilder::Raw("textures/permTexture.png").GetOrCreateTexture(Pi::renderer, "noise"));
m_material->SetTexture(Graphics::Renderer::GetName("gradTexture"),
m_material->SetTexture("gradTexture"_hash,
Graphics::TextureBuilder::Raw("textures/gradTexture.png").GetOrCreateTexture(Pi::renderer, "noise"));

Graphics::Texture *rampTexture = nullptr;
Expand All @@ -165,7 +165,7 @@ namespace GasGiantJobs {
break;
}

m_material->SetTexture(Graphics::Renderer::GetName("rampTexture"), rampTexture);
m_material->SetTexture("rampTexture"_hash, rampTexture);

// these might need to be reversed
const vector2f &texSize(size);
Expand Down Expand Up @@ -217,7 +217,7 @@ namespace GasGiantJobs {
dataBlock.hueAdjust = hueAdjust;
dataBlock.time = 0;

pQuad->GetMaterial()->SetBufferDynamic(Graphics::Renderer::GetName("GenColorData"), &dataBlock);
pQuad->GetMaterial()->SetBufferDynamic("GenColorData"_hash, &dataBlock);
}

// ********************************************************************************
Expand Down
3 changes: 1 addition & 2 deletions src/GeoPatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,6 @@ void GeoPatch::UpdateVBOs(Graphics::Renderer *renderer)

// the default sphere we do the horizon culling against
static const SSphere s_sph;
static const size_t s_patchDetailName = Graphics::Renderer::GetName("PatchDetailFrequency");
void GeoPatch::Render(Graphics::Renderer *renderer, const vector3d &campos, const matrix4x4d &modelView, const Graphics::Frustum &frustum)
{
PROFILE_SCOPED()
Expand Down Expand Up @@ -293,7 +292,7 @@ void GeoPatch::Render(Graphics::Renderer *renderer, const vector3d &campos, cons
// per-patch detail texture scaling value
const float fDetailFrequency = pow(2.0f, float(m_geosphere->GetMaxDepth()) - float(m_depth));

m_geosphere->GetSurfaceMaterial()->SetPushConstant(s_patchDetailName, fDetailFrequency);
m_geosphere->GetSurfaceMaterial()->SetPushConstant("PatchDetailFrequency"_hash, fDetailFrequency);
renderer->DrawMesh(m_patchMesh.get(), m_geosphere->GetSurfaceMaterial().Get());
#ifdef DEBUG_BOUNDING_SPHERES
if (m_boundsphere.get()) {
Expand Down
4 changes: 2 additions & 2 deletions src/GeoSphere.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -484,8 +484,8 @@ void GeoSphere::SetUpMaterials()

m_texHi.Reset(Graphics::TextureBuilder::Model("textures/high.dds").GetOrCreateTexture(Pi::renderer, "model"));
m_texLo.Reset(Graphics::TextureBuilder::Model("textures/low.dds").GetOrCreateTexture(Pi::renderer, "model"));
m_surfaceMaterial->SetTexture(Graphics::Renderer::GetName("texture0"), m_texHi.Get());
m_surfaceMaterial->SetTexture(Graphics::Renderer::GetName("texture1"), m_texLo.Get());
m_surfaceMaterial->SetTexture("texture0"_hash, m_texHi.Get());
m_surfaceMaterial->SetTexture("texture1"_hash, m_texLo.Get());
}

{
Expand Down
4 changes: 2 additions & 2 deletions src/NavLights.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ void NavLights::Init(Graphics::Renderer *renderer)
rsd.primitiveType = Graphics::POINTS;

matHalos4x4.Reset(renderer->CreateMaterial("billboards", desc, rsd));
matHalos4x4->SetTexture(Graphics::Renderer::GetName("texture0"), texHalos4x4.Get());
matHalos4x4->SetPushConstant(Graphics::Renderer::GetName("coordDownScale"), 0.5f);
matHalos4x4->SetTexture("texture0"_hash, texHalos4x4.Get());
matHalos4x4->SetPushConstant("coordDownScale"_hash, 0.5f);

g_initted = true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Planet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ void Planet::GenerateRings(Graphics::Renderer *renderer)
rsd.primitiveType = Graphics::TRIANGLE_STRIP;

m_ringMaterial.reset(renderer->CreateMaterial("planetrings", desc, rsd));
m_ringMaterial->SetTexture(Graphics::Renderer::GetName("texture0"), m_ringTexture.Get());
m_ringMaterial->SetTexture("texture0"_hash, m_ringTexture.Get());
}

void Planet::DrawGasGiantRings(Renderer *renderer, const matrix4x4d &modelView)
Expand Down
4 changes: 2 additions & 2 deletions src/Projectile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ void Projectile::BuildModel()
rsd.cullMode = Graphics::CULL_NONE;

s_sideMat.reset(Pi::renderer->CreateMaterial("unlit", desc, rsd));
s_sideMat->SetTexture(Graphics::Renderer::GetName("texture0"),
s_sideMat->SetTexture("texture0"_hash,
Graphics::TextureBuilder::Billboard("textures/projectile_l.dds").GetOrCreateTexture(Pi::renderer, "billboard"));
s_glowMat.reset(Pi::renderer->CreateMaterial("unlit", desc, rsd));
s_glowMat->SetTexture(Graphics::Renderer::GetName("texture0"),
s_glowMat->SetTexture("texture0"_hash,
Graphics::TextureBuilder::Billboard("textures/projectile_w.dds").GetOrCreateTexture(Pi::renderer, "billboard"));

//zero at projectile position
Expand Down
14 changes: 7 additions & 7 deletions src/Sfx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ void SfxManager::Init(Graphics::Renderer *r)
// ECM effect is different, not managed by Sfx at all, should it be factored out?
desc.effect = Graphics::EFFECT_BILLBOARD;
ecmParticle.reset(r->CreateMaterial("billboards", desc, additiveAlphaState));
ecmParticle->SetTexture(Graphics::Renderer::GetName("texture0"),
ecmParticle->SetTexture("texture0"_hash,
Graphics::TextureBuilder::Billboard("textures/ecm.png").GetOrCreateTexture(r, "billboard"));

// load material definition data
Expand All @@ -407,26 +407,26 @@ void SfxManager::Init(Graphics::Renderer *r)

desc.effect = m_materialData[TYPE_DAMAGE].effect;
damageParticle.reset(r->CreateMaterial("billboards", desc, additiveAlphaState));
damageParticle->SetTexture(Graphics::Renderer::GetName("texture0"),
damageParticle->SetTexture("texture0"_hash,
Graphics::TextureBuilder::Billboard(cfg.String("damageFile")).GetOrCreateTexture(r, "billboard"));
if (desc.effect == Graphics::EFFECT_BILLBOARD_ATLAS)
damageParticle->SetPushConstant(Graphics::Renderer::GetName("coordDownScale"),
damageParticle->SetPushConstant("coordDownScale"_hash,
m_materialData[TYPE_DAMAGE].coord_downscale);

desc.effect = m_materialData[TYPE_SMOKE].effect;
smokeParticle.reset(r->CreateMaterial("billboards", desc, alphaState));
smokeParticle->SetTexture(Graphics::Renderer::GetName("texture0"),
smokeParticle->SetTexture("texture0"_hash,
Graphics::TextureBuilder::Billboard(cfg.String("smokeFile")).GetOrCreateTexture(r, "billboard"));
if (desc.effect == Graphics::EFFECT_BILLBOARD_ATLAS)
smokeParticle->SetPushConstant(Graphics::Renderer::GetName("coordDownScale"),
smokeParticle->SetPushConstant("coordDownScale"_hash,
m_materialData[TYPE_SMOKE].coord_downscale);

desc.effect = m_materialData[TYPE_EXPLOSION].effect;
explosionParticle.reset(r->CreateMaterial("billboards", desc, alphaState));
explosionParticle->SetTexture(Graphics::Renderer::GetName("texture0"),
explosionParticle->SetTexture("texture0"_hash,
Graphics::TextureBuilder::Billboard(cfg.String("explosionFile")).GetOrCreateTexture(r, "billboard"));
if (desc.effect == Graphics::EFFECT_BILLBOARD_ATLAS)
explosionParticle->SetPushConstant(Graphics::Renderer::GetName("coordDownScale"),
explosionParticle->SetPushConstant("coordDownScale"_hash,
m_materialData[TYPE_EXPLOSION].coord_downscale);
}

Expand Down
4 changes: 2 additions & 2 deletions src/Shields.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ namespace {
static RefCountedPtr<Graphics::UniformBuffer> s_matUniformBuffer;
static const std::string s_shieldGroupName("Shields");
static const std::string s_matrixTransformName("_accMtx4");
static const size_t s_shieldDataName = Graphics::Renderer::GetName("ShieldData");
static const size_t s_numHitsName = Graphics::Renderer::GetName("NumHits");
static const size_t s_shieldDataName = "ShieldData"_hash;
static const size_t s_numHitsName = "NumHits"_hash;

static RefCountedPtr<Graphics::Material> GetGlobalShieldMaterial()
{
Expand Down
7 changes: 2 additions & 5 deletions src/Ship.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ const float Ship::DEFAULT_SHIELD_COOLDOWN_TIME = 1.0f;
const double Ship::DEFAULT_LIFT_TO_DRAG_RATIO = 0.001;

namespace {
size_t s_heatingNormalParam;
size_t s_heatGradientTexParam;
static constexpr size_t s_heatingNormalParam = "heatingNormal"_hash;
static constexpr size_t s_heatGradientTexParam = "heatGradient"_hash;
} // namespace

Ship::Ship(const ShipType::Id &shipId) :
Expand Down Expand Up @@ -332,9 +332,6 @@ void Ship::InitEquipSet()

void Ship::InitMaterials()
{
s_heatingNormalParam = Graphics::Renderer::GetName("heatingNormal");
s_heatGradientTexParam = Graphics::Renderer::GetName("heatGradient");

SceneGraph::Model *pModel = GetModel();
assert(pModel);

Expand Down
14 changes: 0 additions & 14 deletions src/graphics/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,4 @@ namespace Graphics {
m_textureCache.clear();
}

size_t Renderer::GetName(const std::string &s)
{
Uint32 a = 0xDEAD, b = 0xBEEF;
lookup3_hashlittle2(s.c_str(), s.size(), &a, &b);
return a | (Uint64(b) << 32);
}

size_t Renderer::GetName(const char *s)
{
Uint32 a = 0xDEAD, b = 0xBEEF;
lookup3_hashlittle2(s, strlen(s), &a, &b);
return a | (Uint64(b) << 32);
}

} // namespace Graphics
4 changes: 2 additions & 2 deletions src/graphics/Renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "Light.h"
#include "Stats.h"
#include "Types.h"
#include "core/StringHash.h"
#include "graphics/BufferCommon.h"
#include "libs.h"
#include "matrix4x4.h"
Expand Down Expand Up @@ -233,8 +234,7 @@ namespace Graphics {
Stats &GetStats() { return m_stats; }

// Returns a hashed name for referring to material constant slots and other constant-size string names
static size_t GetName(const std::string &s);
static size_t GetName(const char *s);
static constexpr size_t GetName(std::string_view s) { return hash_64_fnv1a(s.data(), s.size()); }

protected:
int m_width;
Expand Down
6 changes: 3 additions & 3 deletions src/graphics/opengl/MaterialGL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ namespace Graphics {
};
static_assert(sizeof(DrawDataBlock) == 256, "");

static size_t s_lightDataName = Renderer::GetName("LightData");
static size_t s_drawDataName = Renderer::GetName("DrawData");
static size_t s_lightIntensityName = Renderer::GetName("lightIntensity");
static size_t s_lightDataName = "LightData"_hash;
static size_t s_drawDataName = "DrawData"_hash;
static size_t s_lightIntensityName = "lightIntensity"_hash;

// XXX potential target for allocation optimization
// (e.g. use renderer-owned freelist block allocator and align and group all three buffers into one allocation?)
Expand Down
2 changes: 1 addition & 1 deletion src/graphics/opengl/Shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ Shader::Shader(const std::string &name) :
AddConstantBinding(info.name, info.type, info.binding);
}

if (GetBufferBindingInfo(Renderer::GetName("DrawData")).binding == InvalidBinding) {
if (GetBufferBindingInfo("DrawData"_hash).binding == InvalidBinding) {
Log::Warning("Shaderdef file {} is missing DrawData buffer assignment! Defaulting to {}.\n",
fileInfo.GetName(), info.bufferBindings.back().binding + 1);
AddBufferBinding("DrawData", info.bufferBindings.back().binding + 1);
Expand Down
4 changes: 2 additions & 2 deletions src/pigui/PiGuiRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

using namespace PiGui;

size_t InstanceRenderer::s_textureName = Graphics::Renderer::GetName("texture0");
size_t InstanceRenderer::s_vertexDepthName = Graphics::Renderer::GetName("vertexDepth");
static constexpr size_t s_textureName = "texture0"_hash;
static constexpr size_t s_vertexDepthName = "vertexDepth"_hash;

InstanceRenderer::InstanceRenderer(Graphics::Renderer *r) :
m_renderer(r)
Expand Down
2 changes: 0 additions & 2 deletions src/pigui/PiGuiRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ namespace PiGui {

private:
Graphics::Renderer *m_renderer;
static size_t s_textureName;
static size_t s_vertexDepthName;

std::unique_ptr<Graphics::Material> m_material;
std::unique_ptr<Graphics::VertexBuffer> m_vtxBuffer;
Expand Down
18 changes: 6 additions & 12 deletions src/scenegraph/BaseLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,6 @@ void BaseLoader::ConvertMaterialDefinition(const MaterialDefinition &mdef)
mat->emissive = mdef.emissive;
mat->shininess = mdef.shininess;

const static size_t s_tex0name = Graphics::Renderer::GetName("texture0");
const static size_t s_tex1name = Graphics::Renderer::GetName("texture1");
const static size_t s_tex2name = Graphics::Renderer::GetName("texture2");
const static size_t s_tex3name = Graphics::Renderer::GetName("texture3");
const static size_t s_tex6name = Graphics::Renderer::GetName("texture6");

//semitransparent material
//the node must be marked transparent when using this material
//and should not be mixed with opaque materials
Expand All @@ -90,11 +84,11 @@ void BaseLoader::ConvertMaterialDefinition(const MaterialDefinition &mdef)
if (!normTex.empty())
texture6 = Graphics::TextureBuilder::Normal(normTex).GetOrCreateTexture(m_renderer, "model");

mat->SetTexture(s_tex0name, texture0);
mat->SetTexture(s_tex1name, texture1);
mat->SetTexture(s_tex2name, texture2);
mat->SetTexture(s_tex3name, texture3);
mat->SetTexture(s_tex6name, texture6);
mat->SetTexture("texture0"_hash, texture0);
mat->SetTexture("texture1"_hash, texture1);
mat->SetTexture("texture2"_hash, texture2);
mat->SetTexture("texture3"_hash, texture3);
mat->SetTexture("texture6"_hash, texture6);

m_model->m_materials.push_back(std::make_pair(mdef.name, mat));
}
Expand All @@ -114,7 +108,7 @@ RefCountedPtr<Graphics::Material> BaseLoader::GetDecalMaterial(unsigned int inde

// XXX add depth bias to render state parameter
decMat.Reset(m_renderer->CreateMaterial("multi", matDesc, rsd));
decMat->SetTexture(Graphics::Renderer::GetName("texture0"),
decMat->SetTexture("texture0"_hash,
Graphics::TextureBuilder::GetTransparentTexture(m_renderer));
decMat->specular = Color::BLACK;
decMat->diffuse = Color::WHITE;
Expand Down
2 changes: 1 addition & 1 deletion src/scenegraph/Label3D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace SceneGraph {

m_geometry.reset(font->CreateVertexArray());
m_material.Reset(r->CreateMaterial("multi", matdesc, rsd));
m_material->SetTexture(Graphics::Renderer::GetName("texture0"), font->GetTexture());
m_material->SetTexture("texture0"_hash, font->GetTexture());
m_material->diffuse = Color::WHITE;
m_material->emissive = Color(38, 38, 38);
m_material->specular = Color::WHITE;
Expand Down
Loading

0 comments on commit 7754ea8

Please sign in to comment.