fix: Bind default textures per framebuffer, not per shader program

This commit is contained in:
Vsevolod Kremianskii 2021-03-09 00:25:05 +07:00
parent 58fb95d942
commit d4ff311cc1
8 changed files with 61 additions and 44 deletions

View file

@ -27,7 +27,6 @@
#include "glm/ext.hpp"
#include "stateutil.h"
#include "texture.h"
#include "textures.h"
@ -1100,43 +1099,15 @@ void Shaders::initUniformBlocks() {
}
void Shaders::initTextureUniforms() {
shared_ptr<Texture> defaultTexture(Textures::instance().getDefault());
setUniform("uDiffuse", TextureUnits::diffuse);
setActiveTextureUnit(TextureUnits::diffuse);
defaultTexture->bind();
setUniform("uLightmap", TextureUnits::lightmap);
setActiveTextureUnit(TextureUnits::lightmap);
defaultTexture->bind();
setUniform("uEnvmap", TextureUnits::envmap);
setActiveTextureUnit(TextureUnits::envmap);
defaultTexture->bind();
setUniform("uBumpmap", TextureUnits::bumpmap);
setActiveTextureUnit(TextureUnits::bumpmap);
defaultTexture->bind();
setUniform("uBloom", TextureUnits::bloom);
setActiveTextureUnit(TextureUnits::bloom);
defaultTexture->bind();
setUniform("uIrradianceMap", TextureUnits::irradianceMap);
setActiveTextureUnit(TextureUnits::irradianceMap);
defaultTexture->bind();
setUniform("uPrefilterMap", TextureUnits::prefilterMap);
setActiveTextureUnit(TextureUnits::prefilterMap);
defaultTexture->bind();
setUniform("uBRDFLookup", TextureUnits::brdfLookup);
setActiveTextureUnit(TextureUnits::brdfLookup);
defaultTexture->bind();
setUniform("uShadowMap", TextureUnits::shadowMap);
setActiveTextureUnit(TextureUnits::shadowMap);
defaultTexture->bind();
}
Shaders::~Shaders() {

View file

@ -51,18 +51,6 @@ enum class ShaderProgram {
BillboardBillboard
};
struct TextureUnits {
static constexpr int diffuse { 0 };
static constexpr int lightmap { 1 };
static constexpr int envmap { 2 };
static constexpr int bumpmap { 3 };
static constexpr int bloom { 5 };
static constexpr int irradianceMap { 6 };
static constexpr int prefilterMap { 7 };
static constexpr int brdfLookup { 8 };
static constexpr int shadowMap { 9 };
};
struct UniformFeatureFlags {
static constexpr int diffuse = 1;
static constexpr int lightmap = 2;

View file

@ -27,7 +27,9 @@
#include "image/tgafile.h"
#include "image/tpcfile.h"
#include "image/txifile.h"
#include "stateutil.h"
#include "textureutil.h"
#include "types.h"
using namespace std;
@ -50,12 +52,47 @@ void Textures::init(GameID gameId) {
_default->init();
_default->bind();
_default->clearPixels(1, 1, PixelFormat::RGB);
// Initialize default cubemap texture
_defaultCubemap = make_shared<Texture>("default_cubemap", getTextureProperties(TextureUsage::CubeMapDefault));
_defaultCubemap->init();
_defaultCubemap->bind();
_defaultCubemap->clearPixels(1, 1, PixelFormat::RGB);
}
void Textures::invalidateCache() {
_cache.clear();
}
void Textures::bindDefaults() {
setActiveTextureUnit(TextureUnits::diffuse);
_default->bind();
setActiveTextureUnit(TextureUnits::lightmap);
_default->bind();
setActiveTextureUnit(TextureUnits::envmap);
_defaultCubemap->bind();
setActiveTextureUnit(TextureUnits::bumpmap);
_default->bind();
setActiveTextureUnit(TextureUnits::bloom);
_default->bind();
setActiveTextureUnit(TextureUnits::irradianceMap);
_defaultCubemap->bind();
setActiveTextureUnit(TextureUnits::prefilterMap);
_defaultCubemap->bind();
setActiveTextureUnit(TextureUnits::brdfLookup);
_default->bind();
setActiveTextureUnit(TextureUnits::shadowMap);
_defaultCubemap->bind();
}
shared_ptr<Texture> Textures::get(const string &resRef, TextureUsage usage) {
auto maybeTexture = _cache.find(resRef);
if (maybeTexture != _cache.end()) {

View file

@ -40,13 +40,17 @@ public:
void init(resource::GameID gameId);
void invalidateCache();
std::shared_ptr<Texture> get(const std::string &resRef, TextureUsage usage = TextureUsage::Default);
/**
* Binds default textures to all texture units. Call once per framebuffer.
*/
void bindDefaults();
std::shared_ptr<Texture> getDefault() const { return _default; }
std::shared_ptr<Texture> get(const std::string &resRef, TextureUsage usage = TextureUsage::Default);
private:
resource::GameID _gameId { resource::GameID::KotOR };
std::shared_ptr<render::Texture> _default;
std::shared_ptr<render::Texture> _defaultCubemap;
std::unordered_map<std::string, std::shared_ptr<Texture>> _cache;
std::shared_ptr<Texture> doGet(const std::string &resRef, TextureUsage usage);

View file

@ -53,7 +53,8 @@ Texture::Properties getTextureProperties(TextureUsage usage, bool headless) {
properties.borderColor = glm::vec4(1.0f);
}
if (usage == TextureUsage::EnvironmentMap ||
if (usage == TextureUsage::CubeMapDefault ||
usage == TextureUsage::EnvironmentMap ||
usage == TextureUsage::IrradianceMap ||
usage == TextureUsage::PrefilterMap ||
usage == TextureUsage::CubeMapDepthBuffer) {

View file

@ -47,6 +47,7 @@ enum class PixelFormat {
*/
enum class TextureUsage {
Default,
CubeMapDefault,
GUI,
Diffuse,
Lightmap,
@ -101,6 +102,18 @@ struct GraphicsOptions {
int shadowResolution { 0 };
};
struct TextureUnits {
static constexpr int diffuse { 0 };
static constexpr int lightmap { 1 };
static constexpr int envmap { 2 };
static constexpr int bumpmap { 3 };
static constexpr int bloom { 5 };
static constexpr int irradianceMap { 6 };
static constexpr int prefilterMap { 7 };
static constexpr int brdfLookup { 8 };
static constexpr int shadowMap { 9 };
};
class IEventHandler {
public:
virtual bool handle(const SDL_Event &event) = 0;

View file

@ -24,6 +24,7 @@
#include "../../render/meshes.h"
#include "../../render/shaders.h"
#include "../../render/stateutil.h"
#include "../../render/textures.h"
#include "../../render/textureutil.h"
#include "../node/cameranode.h"
@ -56,6 +57,7 @@ void ControlRenderPipeline::init() {
_geometry.init();
_geometry.bind();
Textures::instance().bindDefaults();
_geometry.attachColor(*_geometryColor);
_geometry.attachDepth(*_geometryDepth);
_geometry.checkCompleteness();

View file

@ -76,6 +76,7 @@ void WorldRenderPipeline::init() {
_geometry.init();
_geometry.bind();
Textures::instance().bindDefaults();
_geometry.attachColor(*_geometryColor1, 0);
_geometry.attachColor(*_geometryColor2, 1);
_geometry.attachDepth(*_depthRenderbuffer);