Skip to content

Commit

Permalink
Added 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 68f4e87 commit fa65b1a
Show file tree
Hide file tree
Showing 45 changed files with 2,896 additions and 139 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -274,20 +274,21 @@ add_subdirectory(editor)
add_subdirectory(generator/mwm_diff)
add_subdirectory(geometry)
add_subdirectory(indexer)
add_subdirectory(kml)
add_subdirectory(local_ads)
add_subdirectory(map)
add_subdirectory(partners_api)
add_subdirectory(platform)
add_subdirectory(routing)
add_subdirectory(routing_common)
add_subdirectory(search)
add_subdirectory(shaders)
add_subdirectory(stats)
add_subdirectory(storage)
add_subdirectory(tracking)
add_subdirectory(traffic)
add_subdirectory(transit)
add_subdirectory(ugc)
add_subdirectory(kml)

if (PLATFORM_DESKTOP)
if (BUILD_MAPSHOT)
Expand Down
1 change: 1 addition & 0 deletions android/jni/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ target_link_libraries(
routing_common
transit
drape_frontend
shaders
search
storage
ugc
Expand Down
95 changes: 33 additions & 62 deletions drape/drape_tests/uniform_value_tests.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#include "testing/testing.hpp"

#include "drape/drape_global.hpp"
#include "drape/gpu_program.hpp"
#include "drape/gpu_program_info.hpp"
#include "drape/gpu_program_manager.hpp"
#include "drape/uniform_value.hpp"

#include "drape/drape_tests/glmock_functions.hpp"
Expand All @@ -12,7 +11,6 @@
#include <utility>

#include <gmock/gmock.h>
#include <drape/drape_global.hpp>

using ::testing::_;
using ::testing::Return;
Expand All @@ -21,11 +19,9 @@ using ::testing::IgnoreResult;
using ::testing::Invoke;
using ::testing::InSequence;
using namespace dp;
using namespace std;

namespace
{

template<typename T>
class MemoryComparer
{
Expand All @@ -34,8 +30,7 @@ class MemoryComparer
: m_result(false)
, m_memory(memory)
, m_size(size)
{
}
{}

void Compare(int32_t id, T const * memory)
{
Expand All @@ -53,57 +48,33 @@ class MemoryComparer
uint32_t m_size;
};

void mock_glGetActiveUniform(uint32_t programID,
uint32_t index,
int32_t * size,
glConst * type,
string & name)
void mock_glGetActiveUniform(uint32_t programID, uint32_t index, int32_t * size,
glConst * type, std::string & name)
{
*size = 1;
if (index < 9)
{
static pair<string, glConst> mockUniforms[9] =
static std::pair<string, glConst> mockUniforms[9] =
{
make_pair("position0", gl_const::GLIntType),
make_pair("position1", gl_const::GLIntVec2),
make_pair("position2", gl_const::GLIntVec3),
make_pair("position3", gl_const::GLIntVec4),
make_pair("position4", gl_const::GLFloatType),
make_pair("position5", gl_const::GLFloatVec2),
make_pair("position6", gl_const::GLFloatVec3),
make_pair("position7", gl_const::GLFloatVec4),
make_pair("viewModel", gl_const::GLFloatMat4)
std::make_pair("position0", gl_const::GLIntType),
std::make_pair("position1", gl_const::GLIntVec2),
std::make_pair("position2", gl_const::GLIntVec3),
std::make_pair("position3", gl_const::GLIntVec4),
std::make_pair("position4", gl_const::GLFloatType),
std::make_pair("position5", gl_const::GLFloatVec2),
std::make_pair("position6", gl_const::GLFloatVec3),
std::make_pair("position7", gl_const::GLFloatVec4),
std::make_pair("viewModel", gl_const::GLFloatMat4)
};
name = mockUniforms[index].first;
*type = mockUniforms[index].second;
}
else
ASSERT(false, ("Undefined index:", index));
}

class TestShaderMapper : public gpu::GpuProgramGetter
{
public:
TestShaderMapper()
{
m_vertexShader = "void main() { gl_Position = vec4(0.0, 0.0, 0.0, 1.0); }";
m_fragmentShader = "void main() { gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0); }";
m_info.m_vertexIndex = 0;
m_info.m_fragmentIndex = 1;
m_info.m_textureSlotsCount = 0;
m_info.m_vertexSource = m_vertexShader.c_str();
m_info.m_fragmentSource = m_fragmentShader.c_str();
}
gpu::GpuProgramInfo const & GetProgramInfo(int program) const override
{
return m_info;
ASSERT(false, ("Undefined index:", index));
}
private:
gpu::GpuProgramInfo m_info;
std::string m_vertexShader;
std::string m_fragmentShader;
};
} // namespace
}
} // namespace

UNIT_TEST(UniformValueTest)
{
Expand All @@ -114,7 +85,6 @@ UNIT_TEST(UniformValueTest)
int32_t const positionLoc = 10;
int32_t const modelViewLoc = 11;


float matrix[16] =
{
1.0f, 0.0f, 0.0f, 0.0f,
Expand All @@ -127,12 +97,9 @@ UNIT_TEST(UniformValueTest)

{
InSequence seq;
EXPECTGL(glGetInteger(gl_const::GLMaxVertexTextures)).Times(1);
// vertexShader->Ref()
EXPECTGL(glCreateShader(gl_const::GLVertexShader)).WillOnce(Return(VertexShaderID));
EXPECTGL(glShaderSource(VertexShaderID, _)).Times(1);
EXPECTGL(glCompileShader(VertexShaderID, _)).WillOnce(Return(true));
// fragmentShader->Ref()
EXPECTGL(glCreateShader(gl_const::GLFragmentShader)).WillOnce(Return(FragmentShaderID));
EXPECTGL(glShaderSource(FragmentShaderID, _)).Times(1);
EXPECTGL(glCompileShader(FragmentShaderID, _)).WillOnce(Return(true));
Expand Down Expand Up @@ -179,54 +146,58 @@ UNIT_TEST(UniformValueTest)
EXPECTGL(glDeleteShader(AnyOf(VertexShaderID, FragmentShaderID))).Times(2);
}

drape_ptr<GpuProgramManager> manager = make_unique_dp<GpuProgramManager>();
manager->Init(make_unique_dp<TestShaderMapper>());
ref_ptr<GpuProgram> program = manager->GetProgram(0);
drape_ptr<Shader> vs = make_unique_dp<Shader>("", "void main() { gl_Position = vec4(0.0, 0.0, 0.0, 1.0); }",
"", Shader::Type::VertexShader);

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);

program->Bind();

{
UniformValue v("position0", 1);
v.Apply(program);
v.Apply(make_ref(program));
}

{
UniformValue v("position1", 1, 2);
v.Apply(program);
v.Apply(make_ref(program));
}

{
UniformValue v("position2", 1, 2, 3);
v.Apply(program);
v.Apply(make_ref(program));
}

{
UniformValue v("position3", 1, 2, 3, 4);
v.Apply(program);
v.Apply(make_ref(program));
}

{
UniformValue v("position4", 1.0f);
v.Apply(program);
v.Apply(make_ref(program));
}

{
UniformValue v("position5", 1.0f, 2.0f);
v.Apply(program);
v.Apply(make_ref(program));
}

{
UniformValue v("position6", 1.0f, 2.0f, 3.0f);
v.Apply(program);
v.Apply(make_ref(program));
}

{
UniformValue v("position7", 1.0f, 2.0f, 3.0f, 4.0f);
v.Apply(program);
v.Apply(make_ref(program));
}

{
UniformValue v("viewModel", matrix);
v.Apply(program);
v.Apply(make_ref(program));
}
}
2 changes: 2 additions & 0 deletions drape/glconstants.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ const glConst GLIntVec4 = GL_INT_VEC4;

const glConst GLFloatMat4 = GL_FLOAT_MAT4;

const glConst GLSampler2D = GL_SAMPLER_2D;

const glConst GLAddBlend = GL_FUNC_ADD;
const glConst GLSubstractBlend = GL_FUNC_SUBTRACT;
const glConst GLReverseSubstrBlend = GL_FUNC_REVERSE_SUBTRACT;
Expand Down
2 changes: 2 additions & 0 deletions drape/glconstants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ extern const glConst GLIntVec4;

extern const glConst GLFloatMat4;

extern const glConst GLSampler2D;

/// Blend Functions
extern const glConst GLAddBlend;
extern const glConst GLSubstractBlend;
Expand Down
15 changes: 8 additions & 7 deletions drape/glsl_types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
#include <glm/mat4x2.hpp>
#include <glm/mat4x3.hpp>

#include <glm/gtc/type_ptr.hpp>

namespace glsl
{

using glm::vec2;
using glm::vec3;
using glm::vec4;
Expand All @@ -28,6 +29,10 @@ using glm::dvec2;
using glm::dvec3;
using glm::dvec4;

using glm::ivec2;
using glm::ivec3;
using glm::ivec4;

using glm::mat3;
using glm::mat4;
using glm::mat4x2;
Expand All @@ -38,10 +43,7 @@ using glm::dmat4;
using glm::dmat4x2;
using glm::dmat4x3;

typedef vec4 Quad1;
typedef mat4x2 Quad2;
typedef mat4x3 Quad3;
typedef mat4 Quad4;
using glm::value_ptr;

inline m2::PointF ToPoint(vec2 const & v)
{
Expand Down Expand Up @@ -106,5 +108,4 @@ inline uint8_t GetComponentCount<vec4>()
{
return 4;
}

} // namespace glsl
} // namespace glsl
58 changes: 48 additions & 10 deletions drape/gpu_program.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@

namespace dp
{
GpuProgram::GpuProgram(int programIndex, ref_ptr<Shader> vertexShader,
ref_ptr<Shader> fragmentShader, uint8_t textureSlotsCount)
: m_vertexShader(vertexShader)
GpuProgram::GpuProgram(std::string const & programName,
ref_ptr<Shader> vertexShader, ref_ptr<Shader> fragmentShader,
uint8_t textureSlotsCount)
: m_programName(programName)
, m_vertexShader(vertexShader)
, m_fragmentShader(fragmentShader)
, m_textureSlotsCount(textureSlotsCount)
{
Expand All @@ -19,7 +21,7 @@ GpuProgram::GpuProgram(int programIndex, ref_ptr<Shader> vertexShader,

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

// On Tegra3 glGetActiveUniform isn't work if you detach shaders after linking.
LoadUniformLocations();
Expand Down Expand Up @@ -75,19 +77,55 @@ int8_t GpuProgram::GetUniformLocation(std::string const & uniformName) const
if (it == m_uniforms.end())
return -1;

return it->second;
return it->second.m_location;
}

glConst GpuProgram::GetUniformType(std::string const & uniformName) const
{
auto const it = m_uniforms.find(uniformName);
if (it == m_uniforms.end())
return -1;

return it->second.m_type;
}

GpuProgram::UniformsInfo const & GpuProgram::GetUniformsInfo() const
{
return m_uniforms;
}

void GpuProgram::LoadUniformLocations()
{
int32_t uniformsCount = GLFunctions::glGetProgramiv(m_programID, gl_const::GLActiveUniforms);
for (int32_t i = 0; i < uniformsCount; ++i)
auto const uniformsCount = GLFunctions::glGetProgramiv(m_programID, gl_const::GLActiveUniforms);
for (int i = 0; i < uniformsCount; ++i)
{
int32_t size = 0;
glConst type = gl_const::GLFloatVec4;
UniformInfo info;
std::string name;
GLFunctions::glGetActiveUniform(m_programID, i, &size, &type, name);
m_uniforms[name] = GLFunctions::glGetUniformLocation(m_programID, 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));
}

info.m_location = GLFunctions::glGetUniformLocation(m_programID, name);
m_uniforms[name] = std::move(info);
}
m_numericUniformsCount = CalculateNumericUniformsCount();
}

uint32_t GpuProgram::CalculateNumericUniformsCount() const
{
uint32_t counter = 0;
for (auto const & u : m_uniforms)
{
if (u.second.m_type != gl_const::GLSampler2D)
counter++;
}
return counter;
}
} // namespace dp
Loading

0 comments on commit fa65b1a

Please sign in to comment.