Skip to content

Commit

Permalink
Replace GenericShader with Magnum's Flat shader.
Browse files Browse the repository at this point in the history
The goal of this commit is to preserve current visual output while
porting to Magnum's builtin functionality, in order to ensure no
regression happened before we jump on more complex shaders.

There's one feature of the GenericShader that still has to wait for
changes on Magnum side and that's the primitive ID texturing used by PLY
files. The relevant shader code is separated into a new
PrimitiveIDTextured{Shader,Drawable} where it will temporarily live
until Magnum can handle this as well.
  • Loading branch information
mosra committed Aug 16, 2019
1 parent af3d137 commit 009146b
Show file tree
Hide file tree
Showing 14 changed files with 199 additions and 252 deletions.
29 changes: 19 additions & 10 deletions src/esp/assets/ResourceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <Magnum/EigenIntegration/GeometryIntegration.h>
#include <Magnum/ImageView.h>
#include <Magnum/PixelFormat.h>
#include <Magnum/Shaders/Flat.h>
#include <Magnum/Trade/AbstractImporter.h>
#include <Magnum/Trade/ImageData.h>
#include <Magnum/Trade/MeshObjectData3D.h>
Expand All @@ -18,7 +19,8 @@

#include "esp/geo/geo.h"
#include "esp/gfx/GenericDrawable.h"
#include "esp/gfx/GenericShader.h"
#include "esp/gfx/PrimitiveIDTexturedDrawable.h"
#include "esp/gfx/PrimitiveIDTexturedShader.h"
#include "esp/io/io.h"
#include "esp/io/json.h"
#include "esp/scene/SceneConfiguration.h"
Expand Down Expand Up @@ -69,9 +71,7 @@ Magnum::GL::AbstractShaderProgram* ResourceManager::getShaderProgram(
switch (type) {
case INSTANCE_MESH_SHADER: {
shaderPrograms_[INSTANCE_MESH_SHADER] =
std::make_shared<gfx::GenericShader>(
gfx::GenericShader::Flag::VertexColored |
gfx::GenericShader::Flag::PrimitiveIDTextured);
std::make_shared<gfx::PrimitiveIDTexturedShader>();
} break;

#ifdef ESP_BUILD_PTEX_SUPPORT
Expand All @@ -83,18 +83,22 @@ Magnum::GL::AbstractShaderProgram* ResourceManager::getShaderProgram(

case COLORED_SHADER: {
shaderPrograms_[COLORED_SHADER] =
std::make_shared<gfx::GenericShader>();
std::make_shared<Magnum::Shaders::Flat3D>(
Magnum::Shaders::Flat3D::Flag::ObjectId);
} break;

case VERTEX_COLORED_SHADER: {
shaderPrograms_[VERTEX_COLORED_SHADER] =
std::make_shared<gfx::GenericShader>(
gfx::GenericShader::Flag::VertexColored);
std::make_shared<Magnum::Shaders::Flat3D>(
Magnum::Shaders::Flat3D::Flag::ObjectId |
Magnum::Shaders::Flat3D::Flag::VertexColor);
} break;

case TEXTURED_SHADER: {
shaderPrograms_[TEXTURED_SHADER] = std::make_shared<gfx::GenericShader>(
gfx::GenericShader::Flag::Textured);
shaderPrograms_[TEXTURED_SHADER] =
std::make_shared<Magnum::Shaders::Flat3D>(
Magnum::Shaders::Flat3D::Flag::ObjectId |
Magnum::Shaders::Flat3D::Flag::Textured);
} break;

default:
Expand Down Expand Up @@ -460,9 +464,14 @@ gfx::Drawable& ResourceManager::createDrawable(
ASSERT(shaderType != PTEX_MESH_SHADER);
// NOTE: this is a runtime error and will never return
return *drawable;
} else if (shaderType == INSTANCE_MESH_SHADER) {
auto* shader = static_cast<gfx::PrimitiveIDTexturedShader*>(
getShaderProgram(shaderType));
drawable = new gfx::PrimitiveIDTexturedDrawable{node, *shader, mesh, group,
texture};
} else { // all other shaders use GenericShader
auto* shader =
static_cast<gfx::GenericShader*>(getShaderProgram(shaderType));
static_cast<Magnum::Shaders::Flat3D*>(getShaderProgram(shaderType));
drawable = new gfx::GenericDrawable{node, *shader, mesh, group,
texture, objectId, color};
}
Expand Down
20 changes: 9 additions & 11 deletions src/esp/gfx/GenericDrawable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@
// LICENSE file in the root directory of this source tree.

#include "GenericDrawable.h"
#include "GenericShader.h"

#include <Magnum/Shaders/Flat.h>

#include "esp/scene/SceneNode.h"

namespace esp {
namespace gfx {

GenericDrawable::GenericDrawable(
scene::SceneNode& node,
GenericShader& shader,
Magnum::Shaders::Flat3D& shader,
Magnum::GL::Mesh& mesh,
Magnum::SceneGraph::DrawableGroup3D* group /* = nullptr */,
Magnum::GL::Texture2D* texture /* = nullptr */,
Expand All @@ -24,24 +26,20 @@ GenericDrawable::GenericDrawable(

void GenericDrawable::draw(const Magnum::Matrix4& transformationMatrix,
Magnum::SceneGraph::Camera3D& camera) {
GenericShader& shader = static_cast<GenericShader&>(shader_);
Magnum::Shaders::Flat3D& shader =
static_cast<Magnum::Shaders::Flat3D&>(shader_);
shader.setTransformationProjectionMatrix(camera.projectionMatrix() *
transformationMatrix);

if (((shader.flags() & GenericShader::Flag::Textured) ||
(shader.flags() & GenericShader::Flag::PrimitiveIDTextured)) &&
texture_) {
if ((shader.flags() & Magnum::Shaders::Flat3D::Flag::Textured) && texture_) {
shader.bindTexture(*texture_);
}

if (!(shader.flags() & GenericShader::Flag::VertexColored)) {
if (!(shader.flags() & Magnum::Shaders::Flat3D::Flag::VertexColor)) {
shader.setColor(color_);
}

if (!(shader.flags() & GenericShader::Flag::PerVertexIds) &&
!(shader.flags() & GenericShader::Flag::PrimitiveIDTextured)) {
shader.setObjectId(node_.getId());
}
shader.setObjectId(node_.getId());
mesh_.draw(shader_);
}

Expand Down
6 changes: 3 additions & 3 deletions src/esp/gfx/GenericDrawable.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@

#pragma once

#include <Magnum/Shaders/Shaders.h>

#include "Drawable.h"

namespace esp {
namespace gfx {

class GenericShader;

class GenericDrawable : public Drawable {
public:
//! Create a GenericDrawable for the given object using shader and mesh.
//! Adds drawable to given group and uses provided texture, objectId, and
//! color for textured, object id buffer and color shader output respectively
explicit GenericDrawable(scene::SceneNode& node,
GenericShader& shader,
Magnum::Shaders::Flat3D& shader,
Magnum::GL::Mesh& mesh,
Magnum::SceneGraph::DrawableGroup3D* group = nullptr,
Magnum::GL::Texture2D* texture = nullptr,
Expand Down
114 changes: 0 additions & 114 deletions src/esp/gfx/GenericShader.h

This file was deleted.

34 changes: 34 additions & 0 deletions src/esp/gfx/PrimitiveIDTexturedDrawable.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (c) Facebook, Inc. and its affiliates.
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.

#include "PrimitiveIDTexturedDrawable.h"
#include "PrimitiveIDTexturedShader.h"
#include "esp/scene/SceneNode.h"

namespace esp {
namespace gfx {

PrimitiveIDTexturedDrawable::PrimitiveIDTexturedDrawable(
scene::SceneNode& node,
PrimitiveIDTexturedShader& shader,
Magnum::GL::Mesh& mesh,
Magnum::SceneGraph::DrawableGroup3D* group /* = nullptr */,
Magnum::GL::Texture2D* texture /* = nullptr */)
: Drawable{node, shader, mesh, group}, texture_(texture) {}

void PrimitiveIDTexturedDrawable::draw(
const Magnum::Matrix4& transformationMatrix,
Magnum::SceneGraph::Camera3D& camera) {
PrimitiveIDTexturedShader& shader =
static_cast<PrimitiveIDTexturedShader&>(shader_);
shader
.setTransformationProjectionMatrix(camera.projectionMatrix() *
transformationMatrix)
.bindTexture(*texture_);

mesh_.draw(shader_);
}

} // namespace gfx
} // namespace esp
34 changes: 34 additions & 0 deletions src/esp/gfx/PrimitiveIDTexturedDrawable.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (c) Facebook, Inc. and its affiliates.
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.

#pragma once

#include "Drawable.h"

namespace esp {
namespace gfx {

class PrimitiveIDTexturedShader;

class PrimitiveIDTexturedDrawable : public Drawable {
public:
//! Create a GenericDrawable for the given object using shader and mesh.
//! Adds drawable to given group and uses provided texture, objectId, and
//! color for textured, object id buffer and color shader output respectively
explicit PrimitiveIDTexturedDrawable(
scene::SceneNode& node,
PrimitiveIDTexturedShader& shader,
Magnum::GL::Mesh& mesh,
Magnum::SceneGraph::DrawableGroup3D* group = nullptr,
Magnum::GL::Texture2D* texture = nullptr);

protected:
virtual void draw(const Magnum::Matrix4& transformationMatrix,
Magnum::SceneGraph::Camera3D& camera) override;

Magnum::GL::Texture2D* texture_;
};

} // namespace gfx
} // namespace esp
Loading

0 comments on commit 009146b

Please sign in to comment.