Skip to content

Commit

Permalink
Usage of shaders library
Browse files Browse the repository at this point in the history
  • Loading branch information
r.kuznetsov authored and darina committed Jun 28, 2018
1 parent fa65b1a commit 5366ecf
Show file tree
Hide file tree
Showing 91 changed files with 685 additions and 1,776 deletions.
3 changes: 0 additions & 3 deletions drape/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,6 @@ set(
${DRAPE_ROOT}/gpu_buffer.hpp
${DRAPE_ROOT}/gpu_program.cpp
${DRAPE_ROOT}/gpu_program.hpp
${DRAPE_ROOT}/gpu_program_info.hpp
${DRAPE_ROOT}/gpu_program_manager.cpp
${DRAPE_ROOT}/gpu_program_manager.hpp
${DRAPE_ROOT}/hw_texture.cpp
${DRAPE_ROOT}/hw_texture.hpp
${DRAPE_ROOT}/index_buffer.cpp
Expand Down
15 changes: 8 additions & 7 deletions drape/debug_rect_renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@

#include "drape/glextensions_list.hpp"
#include "drape/glfunctions.hpp"
#include "drape/gpu_program_manager.hpp"

#include <array>

namespace dp
{
namespace
{
m2::PointF PixelPointToScreenSpace(ScreenBase const & screen, m2::PointF const & pt)
{
float const szX = static_cast<float>(screen.PixelRectIn3d().SizeX());
float const szY = static_cast<float>(screen.PixelRectIn3d().SizeY());
auto const szX = static_cast<float>(screen.PixelRectIn3d().SizeX());
auto const szY = static_cast<float>(screen.PixelRectIn3d().SizeY());
return m2::PointF(pt.x / szX - 0.5f, -pt.y / szY + 0.5f) * 2.0f;
}
} // namespace
Expand All @@ -34,7 +35,7 @@ DebugRectRenderer::~DebugRectRenderer()
ASSERT_EQUAL(m_vertexBuffer, 0, ());
}

void DebugRectRenderer::Init(ref_ptr<dp::GpuProgramManager> mng, int programId)
void DebugRectRenderer::Init(ref_ptr<dp::GpuProgram> program)
{
if (dp::GLExtensionsList::Instance().IsSupported(dp::GLExtensionsList::VertexArrayObject))
{
Expand All @@ -44,7 +45,7 @@ void DebugRectRenderer::Init(ref_ptr<dp::GpuProgramManager> mng, int programId)

m_vertexBuffer = GLFunctions::glGenBuffer();
GLFunctions::glBindBuffer(m_vertexBuffer, gl_const::GLArrayBuffer);
m_program = mng->GetProgram(programId);
m_program = program;
int8_t attributeLocation = m_program->GetAttributeLocation("a_position");
ASSERT_NOT_EQUAL(attributeLocation, -1, ());
GLFunctions::glEnableVertexAttribute(attributeLocation);
Expand Down Expand Up @@ -98,7 +99,7 @@ void DebugRectRenderer::DrawRect(ScreenBase const & screen, m2::RectF const & re
if (m_VAO != 0)
GLFunctions::glBindVertexArray(m_VAO);

array<m2::PointF, 5> vertices;
std::array<m2::PointF, 5> vertices;
vertices[0] = PixelPointToScreenSpace(screen, rect.LeftBottom());
vertices[1] = PixelPointToScreenSpace(screen, rect.LeftTop());
vertices[2] = PixelPointToScreenSpace(screen, rect.RightTop());
Expand Down Expand Up @@ -141,7 +142,7 @@ void DebugRectRenderer::DrawArrow(ScreenBase const & screen,
if (m_VAO != 0)
GLFunctions::glBindVertexArray(m_VAO);

array<m2::PointF, 5> vertices;
std::array<m2::PointF, 5> vertices;
m2::PointF const dir = (data.m_arrowEnd - data.m_arrowStart).Normalize();
m2::PointF const side = m2::PointF(-dir.y, dir.x);
vertices[0] = PixelPointToScreenSpace(screen, data.m_arrowStart);
Expand Down
8 changes: 3 additions & 5 deletions drape/debug_rect_renderer.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include "drape/gpu_program_manager.hpp"
#include "drape/gpu_program.hpp"
#include "drape/overlay_tree.hpp"
#include "drape/pointers.hpp"

Expand All @@ -13,12 +13,11 @@

namespace dp
{

class DebugRectRenderer
{
public:
static DebugRectRenderer & Instance();
void Init(ref_ptr<dp::GpuProgramManager> mng, int programId);
void Init(ref_ptr<dp::GpuProgram> program);
void Destroy();

bool IsEnabled() const;
Expand All @@ -36,6 +35,5 @@ class DebugRectRenderer
ref_ptr<dp::GpuProgram> m_program;
bool m_isEnabled;
};

} // namespace dp
} // namespace dp

1 change: 0 additions & 1 deletion drape/drape_tests/batcher_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include "drape/drape_tests/glmock_functions.hpp"
#include "drape/drape_tests/memory_comparer.hpp"
#include "drape/glconstants.hpp"
#include "drape/gpu_program_manager.hpp"
#include "drape/index_storage.hpp"
#include "drape/vertex_array_buffer.hpp"

Expand Down
2 changes: 1 addition & 1 deletion drape/drape_tests/uniform_value_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ UNIT_TEST(UniformValueTest)
drape_ptr<Shader> fs = make_unique_dp<Shader>("", "void main() { gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0); }",
"", Shader::Type::FragmentShader);

drape_ptr<GpuProgram> program = make_unique_dp<GpuProgram>("", make_ref(vs), make_ref(fs), 0);
drape_ptr<GpuProgram> program = make_unique_dp<GpuProgram>("", make_ref(vs), make_ref(fs));

program->Bind();

Expand Down
7 changes: 1 addition & 6 deletions drape/glsl_func.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
#include <glm_config.hpp>
#include <glm/geometric.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>

#include <glm/gtx/rotate_vector.hpp>

namespace glsl
{

using glm::dot;
using glm::cross;
using glm::normalize;
Expand All @@ -20,7 +18,4 @@ using glm::translate;
using glm::rotate;
using glm::scale;
using glm::transpose;

using glm::value_ptr;

} // namespace glsl
} // namespace glsl
38 changes: 9 additions & 29 deletions drape/glstate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,9 @@ void Blending::Apply() const
GLFunctions::glDisable(gl_const::GLBlending);
}

bool Blending::operator < (Blending const & other) const
{
return m_isEnabled < other.m_isEnabled;
}
bool Blending::operator<(Blending const & other) const { return m_isEnabled < other.m_isEnabled; }

bool Blending::operator == (Blending const & other) const
{
return m_isEnabled == other.m_isEnabled;
}

GLState::GLState(int gpuProgramIndex, ref_ptr<BaseRenderState> renderState)
: m_renderState(renderState)
, m_gpuProgramIndex(gpuProgramIndex)
, m_gpuProgram3dIndex(gpuProgramIndex)
, m_depthFunction(gl_const::GLLessOrEqual)
, m_textureFilter(gl_const::GLLinear)
, m_colorTexture(nullptr)
, m_maskTexture(nullptr)
, m_drawAsLine(false)
, m_lineWidth(1)
{
ASSERT(m_renderState != nullptr, ());
}
bool Blending::operator==(Blending const & other) const { return m_isEnabled == other.m_isEnabled; }

glConst GLState::GetDepthFunction() const
{
Expand Down Expand Up @@ -99,10 +79,10 @@ bool GLState::operator<(GLState const & other) const
return m_renderState->Less(other.m_renderState);
if (!(m_blending == other.m_blending))
return m_blending < other.m_blending;
if (m_gpuProgramIndex != other.m_gpuProgramIndex)
return m_gpuProgramIndex < other.m_gpuProgramIndex;
if (m_gpuProgram3dIndex != other.m_gpuProgram3dIndex)
return m_gpuProgram3dIndex < other.m_gpuProgram3dIndex;
if (m_gpuProgram != other.m_gpuProgram)
return m_gpuProgram < other.m_gpuProgram;
if (m_gpuProgram3d != other.m_gpuProgram3d)
return m_gpuProgram3d < other.m_gpuProgram3d;
if (m_depthFunction != other.m_depthFunction)
return m_depthFunction < other.m_depthFunction;
if (m_colorTexture != other.m_colorTexture)
Expand All @@ -120,8 +100,8 @@ bool GLState::operator<(GLState const & other) const
bool GLState::operator==(GLState const & other) const
{
return m_renderState->Equal(other.m_renderState) &&
m_gpuProgramIndex == other.m_gpuProgramIndex &&
m_gpuProgram3dIndex == other.m_gpuProgram3dIndex &&
m_gpuProgram == other.m_gpuProgram &&
m_gpuProgram3d == other.m_gpuProgram3d &&
m_blending == other.m_blending &&
m_colorTexture == other.m_colorTexture &&
m_maskTexture == other.m_maskTexture &&
Expand Down Expand Up @@ -152,7 +132,7 @@ struct UniformApplier

uint8_t TextureState::m_usedSlots = 0;

void TextureState::ApplyTextures(GLState state, ref_ptr<GpuProgram> program)
void TextureState::ApplyTextures(GLState const & state, ref_ptr<GpuProgram> program)
{
m_usedSlots = 0;

Expand Down
49 changes: 32 additions & 17 deletions drape/glstate.hpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
#pragma once

#include "drape/glconstants.hpp"
#include "drape/gpu_program.hpp"
#include "drape/pointers.hpp"
#include "drape/texture.hpp"
#include "drape/gpu_program.hpp"
#include "drape/uniform_values_storage.hpp"

#include "base/assert.hpp"

#include <utility>

namespace dp
{

struct BlendingParams
{
BlendingParams();
Expand All @@ -25,8 +29,8 @@ struct Blending

void Apply() const;

bool operator < (Blending const & other) const;
bool operator == (Blending const & other) const;
bool operator<(Blending const & other) const;
bool operator==(Blending const & other) const;

bool m_isEnabled;
};
Expand All @@ -42,9 +46,16 @@ class BaseRenderState
class GLState
{
public:
GLState(int gpuProgramIndex, ref_ptr<BaseRenderState> renderState);
template<typename ProgramType>
GLState(ProgramType gpuProgram, ref_ptr<BaseRenderState> renderState)
: m_renderState(std::move(renderState))
, m_gpuProgram(static_cast<size_t>(gpuProgram))
, m_gpuProgram3d(static_cast<size_t>(gpuProgram))
{
ASSERT(m_renderState != nullptr, ());
}

template <typename RenderStateType>
template<typename RenderStateType>
ref_ptr<RenderStateType> GetRenderState() const
{
ASSERT(dynamic_cast<RenderStateType *>(m_renderState.get()) != nullptr, ());
Expand All @@ -60,10 +71,14 @@ class GLState
void SetBlending(Blending const & blending) { m_blending = blending; }
Blending const & GetBlending() const { return m_blending; }

int GetProgramIndex() const { return m_gpuProgramIndex; }
template<typename ProgramType>
ProgramType GetProgram() const { return static_cast<ProgramType>(m_gpuProgram); }

template<typename ProgramType>
void SetProgram3d(ProgramType gpuProgram3d) { m_gpuProgram3d = static_cast<size_t>(gpuProgram3d); }

void SetProgram3dIndex(int gpuProgram3dIndex) { m_gpuProgram3dIndex = gpuProgram3dIndex; }
int GetProgram3dIndex() const { return m_gpuProgram3dIndex; }
template<typename ProgramType>
ProgramType GetProgram3d() const { return static_cast<ProgramType>(m_gpuProgram3d); }

glConst GetDepthFunction() const;
void SetDepthFunction(glConst functionName);
Expand All @@ -82,23 +97,23 @@ class GLState

private:
ref_ptr<BaseRenderState> m_renderState;
int m_gpuProgramIndex;
int m_gpuProgram3dIndex;
size_t m_gpuProgram;
size_t m_gpuProgram3d;
Blending m_blending;
glConst m_depthFunction;
glConst m_textureFilter;
glConst m_depthFunction = gl_const::GLLessOrEqual;
glConst m_textureFilter = gl_const::GLLinear;

ref_ptr<Texture> m_colorTexture;
ref_ptr<Texture> m_maskTexture;

bool m_drawAsLine;
int m_lineWidth;
bool m_drawAsLine = false;
int m_lineWidth = 1;
};

class TextureState
{
public:
static void ApplyTextures(GLState state, ref_ptr<GpuProgram> program);
static void ApplyTextures(GLState const & state, ref_ptr<GpuProgram> program);
static uint8_t GetLastUsedSlots();

private:
Expand All @@ -107,5 +122,5 @@ class TextureState

void ApplyUniforms(UniformValuesStorage const & uniforms, ref_ptr<GpuProgram> program);
void ApplyState(GLState const & state, ref_ptr<GpuProgram> program);
void ApplyBlending(GLState const & sstate);
void ApplyBlending(GLState const & state);
} // namespace dp
24 changes: 12 additions & 12 deletions drape/gpu_program.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@

#include "base/logging.hpp"

#include <set>

namespace dp
{
GpuProgram::GpuProgram(std::string const & programName,
ref_ptr<Shader> vertexShader, ref_ptr<Shader> fragmentShader,
uint8_t textureSlotsCount)
ref_ptr<Shader> vertexShader, ref_ptr<Shader> fragmentShader)
: m_programName(programName)
, m_vertexShader(vertexShader)
, m_fragmentShader(fragmentShader)
, m_textureSlotsCount(textureSlotsCount)
{
m_programID = GLFunctions::glCreateProgram();
GLFunctions::glAttachShader(m_programID, m_vertexShader->GetID());
GLFunctions::glAttachShader(m_programID, m_fragmentShader->GetID());

string errorLog;
std::string errorLog;
if (!GLFunctions::glLinkProgram(m_programID, errorLog))
LOG(LERROR, ("Program ", programName, " link error = ", errorLog));

Expand Down Expand Up @@ -96,26 +96,26 @@ GpuProgram::UniformsInfo const & GpuProgram::GetUniformsInfo() const

void GpuProgram::LoadUniformLocations()
{
static std::set<glConst> const kSupportedTypes = {
gl_const::GLFloatType, gl_const::GLFloatVec2, gl_const::GLFloatVec3, gl_const::GLFloatVec4,
gl_const::GLIntType, gl_const::GLIntVec2, gl_const::GLIntVec3, gl_const::GLIntVec4,
gl_const::GLFloatMat4, gl_const::GLSampler2D};

auto const uniformsCount = GLFunctions::glGetProgramiv(m_programID, gl_const::GLActiveUniforms);
for (int i = 0; i < uniformsCount; ++i)
{
int32_t size = 0;
UniformInfo info;
std::string name;
GLFunctions::glGetActiveUniform(m_programID, static_cast<uint32_t>(i), &size, &info.m_type, name);
if (info.m_type != gl_const::GLFloatType && info.m_type != gl_const::GLFloatVec2 &&
info.m_type != gl_const::GLFloatVec3 && info.m_type != gl_const::GLFloatVec4 &&
info.m_type != gl_const::GLIntType && info.m_type != gl_const::GLIntVec2 &&
info.m_type != gl_const::GLIntVec3 && info.m_type != gl_const::GLIntVec4 &&
info.m_type != gl_const::GLFloatMat4 && info.m_type != gl_const::GLSampler2D)
{
CHECK(false, ("Used uniform has unsupported type. Program =", m_programName, "Type =", info.m_type));
}
CHECK(kSupportedTypes.find(info.m_type) != kSupportedTypes.cend(),
("Used uniform has unsupported type. Program =", m_programName, "Type =", info.m_type));

info.m_location = GLFunctions::glGetUniformLocation(m_programID, name);
m_uniforms[name] = std::move(info);
}
m_numericUniformsCount = CalculateNumericUniformsCount();
m_textureSlotsCount = static_cast<uint8_t>(m_uniforms.size() - m_numericUniformsCount);
}

uint32_t GpuProgram::CalculateNumericUniformsCount() const
Expand Down
5 changes: 2 additions & 3 deletions drape/gpu_program.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ class GpuProgram
{
public:
GpuProgram(std::string const & programName,
ref_ptr<Shader> vertexShader, ref_ptr<Shader> fragmentShader,
uint8_t textureSlotsCount);
ref_ptr<Shader> vertexShader, ref_ptr<Shader> fragmentShader);
~GpuProgram();

std::string const & GetName() const { return m_programName; }
Expand Down Expand Up @@ -49,7 +48,7 @@ class GpuProgram
ref_ptr<Shader> m_fragmentShader;

UniformsInfo m_uniforms;
uint8_t const m_textureSlotsCount;
uint8_t m_textureSlotsCount = 0;
uint32_t m_numericUniformsCount = 0;
};
} // namespace dp
Loading

0 comments on commit 5366ecf

Please sign in to comment.