From 0b02ded37c8e01e4f9e37931c9c56982d23bb511 Mon Sep 17 00:00:00 2001 From: ExMix Date: Wed, 13 Nov 2013 12:43:52 +0300 Subject: [PATCH] [drape] compiling shader for desktop build --- drape/shader_def.cpp | 34 +++++++++++------ drape/shaders/simple_vertex_shader.vsh | 9 +++-- drape/utils/list_generator.cpp | 51 +------------------------- drape_head/glwidget.cpp | 13 +++++-- tools/autobuild/shader_preprocessor.py | 24 +++++++++++- 5 files changed, 60 insertions(+), 71 deletions(-) diff --git a/drape/shader_def.cpp b/drape/shader_def.cpp index 5520b69ec6..6c1bec7b0b 100644 --- a/drape/shader_def.cpp +++ b/drape/shader_def.cpp @@ -2,22 +2,34 @@ #include "../std/utility.hpp" + +#if defined(OMIM_OS_DESKTOP) + #define LOW_P + #define MEDIUM_P + #define HIGH_P +#else + #define LOW_P lowp + #define MEDIUM_P mediump + #define HIGH_P highp +#endif + namespace gpu { static const char SIMPLE_VERTEX_SHADER_VSH[] = " \ - attribute mediump vec2 position; \ - attribute mediump float depth; \ + attribute vec2 position; \ + attribute float depth; \ \ - uniform mediump mat4 modelViewProjectionMatrix; \ + uniform mat4 modelView; \ + uniform mat4 projection; \ \ void main(void) \ { \ - gl_Position = modelViewProjectionMatrix * vec4(position, depth, 1.0); \ + gl_Position = vec4(position, depth, 1.0) * modelView * projection; \ } \ "; static const char SOLID_AREA_FRAGMENT_SHADER_FSH[] = " \ - uniform lowp vec4 color; \ + uniform " LOW_P " vec4 color; \ \ void main(void) \ { \ @@ -27,7 +39,7 @@ namespace gpu static const char TEXTURING_FRAGMENT_SHADER_FSH[] = " \ uniform sampler2D textureUnit; \ - varying highp vec4 varTexCoords; \ + varying " HIGH_P " vec4 varTexCoords; \ \ void main(void) \ { \ @@ -36,13 +48,13 @@ namespace gpu "; static const char TEXTURING_VERTEX_SHADER_VSH[] = " \ - attribute mediump vec2 position; \ - attribute mediump float depth; \ - attribute mediump vec4 texCoords; \ + attribute " MEDIUM_P " vec2 position; \ + attribute " MEDIUM_P " float depth; \ + attribute " MEDIUM_P " vec4 texCoords; \ \ - uniform highp mat4 modelViewProjectionMatrix; \ + uniform " HIGH_P " mat4 modelViewProjectionMatrix; \ \ - varying highp vec4 varTexCoords; \ + varying " HIGH_P " vec4 varTexCoords; \ \ void main(void) \ { \ diff --git a/drape/shaders/simple_vertex_shader.vsh b/drape/shaders/simple_vertex_shader.vsh index bd24df16fb..7d7519f3f0 100644 --- a/drape/shaders/simple_vertex_shader.vsh +++ b/drape/shaders/simple_vertex_shader.vsh @@ -1,9 +1,10 @@ -attribute mediump vec2 position; -attribute mediump float depth; +attribute vec2 position; +attribute float depth; -uniform mediump mat4 modelViewProjectionMatrix; +uniform mat4 modelView; +uniform mat4 projection; void main(void) { - gl_Position = modelViewProjectionMatrix * vec4(position, depth, 1.0); + gl_Position = vec4(position, depth, 1.0) * modelView * projection; } diff --git a/drape/utils/list_generator.cpp b/drape/utils/list_generator.cpp index 146b4fde36..d0cafd4bf1 100644 --- a/drape/utils/list_generator.cpp +++ b/drape/utils/list_generator.cpp @@ -18,23 +18,6 @@ struct Vertex float m_y; }; -struct Color -{ - Color() {} - Color(float r, float g, float b, float a) - : m_r(r) - , m_g(g) - , m_b(b) - , m_a(a) - { - } - - float m_r; - float m_g; - float m_b; - float m_a; -}; - ListGenerator::ListGenerator() : m_depth(0.0f) , m_x(0.0f) @@ -67,19 +50,6 @@ void ListGenerator::SetUniforms(const vector & uniforms) m_uniforms = uniforms; } -namespace -{ - float clamp01(float value) - { - return min(max(value, 0.0f), 1.0f); - } - - float colorChannel() - { - return clamp01((rand() % 100) / 99.0f); - } -} - void ListGenerator::Generate(int count, Batcher & batcher) { float quadCount = count /2.0; @@ -111,15 +81,7 @@ void ListGenerator::Generate(int count, Batcher & batcher) } } - vector colors; - colors.reserve(vertexCount); - for (int i = 0; i < vertexCount; ++i) - colors.push_back(Color( colorChannel() - , colorChannel() - , colorChannel() - , colorChannel())); - - AttributeProvider provider(3, vertexCount); + AttributeProvider provider(2, vertexCount); { BindingInfo info(1); BindingDecl & decl = info.GetBindingDecl(0); @@ -143,17 +105,6 @@ void ListGenerator::Generate(int count, Batcher & batcher) provider.InitStream(1, info, &depthMemory[0]); } - { - BindingInfo info(1); - BindingDecl & decl = info.GetBindingDecl(0); - decl.m_attributeName = "color"; - decl.m_componentCount = 4; - decl.m_componentType = GLConst::GLFloatType; - decl.m_offset = 0; - decl.m_stride = 0; - provider.InitStream(2, info, &colors[0]); - } - TextureBinding textureBinding("", false, 0, ReferencePoiner(NULL)); GLState state(m_programIndex, (int16_t)m_depth, textureBinding); state.GetUniformValues() = m_uniforms; diff --git a/drape_head/glwidget.cpp b/drape_head/glwidget.cpp index 9c46042bce..7f2476750b 100644 --- a/drape_head/glwidget.cpp +++ b/drape_head/glwidget.cpp @@ -1,6 +1,8 @@ #include "glwidget.hpp" #include "../drape/utils/list_generator.hpp" +#include "../drape/shader_def.hpp" + GLWidget::GLWidget() { m_batcher = new Batcher(ReferencePoiner(this)); @@ -40,7 +42,7 @@ void GLWidget::initializeGL() 0.0, 0.0, -1.0, 1.0 }; - uniforms.push_back(UniformValue("modelViewMatrix", model)); + uniforms.push_back(UniformValue("modelView", model)); float p[16] = { 0.5, 0.0, 0.0, 0.0, @@ -49,14 +51,17 @@ void GLWidget::initializeGL() 0.0, 0.0, 0.0, 1.0 }; - uniforms.push_back(UniformValue("projectionMatrix", p)); + uniforms.push_back(UniformValue("projection", p)); + + float color[4] = { 1.0, 0.0, 0.0, 1.0 }; + uniforms.push_back(UniformValue("color", color[0], color[1], color[2], color[3])); ListGenerator gen; gen.SetDepth(0.5); - gen.SetProgram(1); + gen.SetProgram(gpu::SOLID_AREA_PROGRAM); gen.SetViewport(-1.0f, -1.0f, 2.0f, 2.0f); gen.SetUniforms(uniforms); - gen.Generate(3000, *m_batcher); + gen.Generate(30, *m_batcher); } void GLWidget::paintGL() diff --git a/tools/autobuild/shader_preprocessor.py b/tools/autobuild/shader_preprocessor.py index 33d27a0c89..77ae7f5b0e 100644 --- a/tools/autobuild/shader_preprocessor.py +++ b/tools/autobuild/shader_preprocessor.py @@ -1,6 +1,13 @@ import os import sys +lowPDefine = "LOW_P" +lowPSearch = "lowp" +mediumPDefine = "MEDIUM_P" +mediumPSearch = "mediump" +highPDefine = "HIGH_P" +highPSearch = "highp" + def formatOutFilePath(baseDir, fileName): return os.path.join(baseDir, "..", fileName) @@ -87,7 +94,10 @@ def writeDefinitionFile(programIndex, defFilePath): def writeShader(outputFile, shaderFile, shaderDir): outputFile.write(" static const char %s[] = \" \\\n" % (formatShaderSourceName(shaderFile))); for line in open(os.path.join(shaderDir, shaderFile)): - outputFile.write(" %s \\\n" % (line.rstrip())) + outputLine = line.rstrip().replace(lowPSearch, "\" " + lowPDefine + " \"") + outputLine = outputLine.replace(mediumPSearch, "\" " + mediumPDefine + " \"") + outputLine = outputLine.replace(highPSearch, "\" " + highPDefine+ " \"") + outputFile.write(" %s \\\n" % (outputLine)) outputFile.write(" \";\n\n") def writeShadersIndex(outputFile, shaderIndex): @@ -97,7 +107,17 @@ def writeShadersIndex(outputFile, shaderIndex): def writeImplementationFile(programsDef, programIndex, shaderIndex, shaderDir, implFile, defFile): file = open(formatOutFilePath(shaderDir, implFile), 'w') file.write("#include \"%s\"\n\n" % (defFile)) - file.write("#include \"../std/utility.hpp\"\n\n") + file.write("#include \"../std/utility.hpp\"\n\n\n") + file.write("#if defined(OMIM_OS_DESKTOP)\n") + file.write(" #define %s\n" % (lowPDefine)) + file.write(" #define %s\n" % (mediumPDefine)) + file.write(" #define %s\n" % (highPDefine)) + file.write("#else\n") + file.write(" #define %s %s\n" % (lowPDefine, lowPSearch)) + file.write(" #define %s %s\n" % (mediumPDefine, mediumPSearch)) + file.write(" #define %s %s\n" % (highPDefine, highPSearch)) + file.write("#endif\n\n") + file.write("namespace gpu\n") file.write("{\n")