diff options
-rw-r--r-- | DEPS | 2 | ||||
-rw-r--r-- | gpu/command_buffer/service/gles2_cmd_decoder.cc | 61 | ||||
-rw-r--r-- | gpu/command_buffer/service/shader_translator.cc | 85 | ||||
-rw-r--r-- | gpu/command_buffer/service/shader_translator.h | 50 | ||||
-rw-r--r-- | gpu/gpu.gyp | 2 |
5 files changed, 152 insertions, 48 deletions
@@ -32,7 +32,7 @@ deps = { (Var("googlecode_url") % "googletest") + "/trunk@435", "src/third_party/angle": - (Var("googlecode_url") % "angleproject") + "/trunk@419", + (Var("googlecode_url") % "angleproject") + "/trunk@425", "src/third_party/WebKit": "/trunk/deps/third_party/WebKit@33467", diff --git a/gpu/command_buffer/service/gles2_cmd_decoder.cc b/gpu/command_buffer/service/gles2_cmd_decoder.cc index 183fb03..b94d92a 100644 --- a/gpu/command_buffer/service/gles2_cmd_decoder.cc +++ b/gpu/command_buffer/service/gles2_cmd_decoder.cc @@ -32,9 +32,9 @@ #include "gpu/command_buffer/service/program_manager.h" #include "gpu/command_buffer/service/renderbuffer_manager.h" #include "gpu/command_buffer/service/shader_manager.h" +#include "gpu/command_buffer/service/shader_translator.h" #include "gpu/command_buffer/service/texture_manager.h" #include "gpu/GLES2/gles2_command_buffer.h" -#include "third_party/angle/include/GLSLANG/ShaderLang.h" #if !defined(GL_DEPTH24_STENCIL8) #define GL_DEPTH24_STENCIL8 0x88F0 @@ -268,19 +268,6 @@ class FrameBuffer { GLuint id_; DISALLOW_COPY_AND_ASSIGN(FrameBuffer); }; - -void FinalizeShaderTranslator(void* /* dummy */) { - ShFinalize(); -} - -bool InitializeShaderTranslator() { - static bool initialized = false; - if (!initialized && ShInitialize()) { - base::AtExitManager::RegisterCallback(&FinalizeShaderTranslator, NULL); - initialized = true; - } - return initialized; -} // } // anonymous namespace. GLES2Decoder::GLES2Decoder(ContextGroup* group) @@ -1272,8 +1259,8 @@ class GLES2DecoderImpl : public base::SupportsWeakPtr<GLES2DecoderImpl>, error::Error current_decoder_error_; bool use_shader_translator_; - ShHandle vertex_compiler_; - ShHandle fragment_compiler_; + scoped_ptr<ShaderTranslator> vertex_translator_; + scoped_ptr<ShaderTranslator> fragment_translator_; // Cached from the context group. const Validators* validators_; @@ -1526,8 +1513,6 @@ GLES2DecoderImpl::GLES2DecoderImpl(ContextGroup* group) anti_aliased_(false), current_decoder_error_(error::kNoError), use_shader_translator_(true), - vertex_compiler_(NULL), - fragment_compiler_(NULL), validators_(group->validators()), depth24_stencil8_oes_supported_(false) { attrib_0_value_.v[0] = 0.0f; @@ -1656,12 +1641,6 @@ bool GLES2DecoderImpl::Initialize(gfx::GLContext* context, } if (use_shader_translator_) { - if (!InitializeShaderTranslator()) { - DLOG(ERROR) << "Could not initialize shader translator."; - Destroy(); - return false; - } - TBuiltInResource resources; ShInitBuiltInResource(&resources); resources.MaxVertexAttribs = group_->max_vertex_attribs(); @@ -1678,17 +1657,15 @@ bool GLES2DecoderImpl::Initialize(gfx::GLContext* context, // TODO(alokp): Figure out if OES_standard_derivatives extension is // available. resources.OES_standard_derivatives = 0; - vertex_compiler_ = ShConstructCompiler(EShLangVertex, EShSpecGLES2, - &resources); - if (vertex_compiler_ == NULL) { - DLOG(ERROR) << "Could not create vertex shader translator."; + vertex_translator_.reset(new ShaderTranslator); + if (!vertex_translator_->Init(EShLangVertex, &resources)) { + DLOG(ERROR) << "Could not initialize vertex shader translator."; Destroy(); return false; } - fragment_compiler_ = ShConstructCompiler(EShLangFragment, EShSpecGLES2, - &resources); - if (fragment_compiler_ == NULL) { - DLOG(ERROR) << "Could not create fragment shader translator."; + fragment_translator_.reset(new ShaderTranslator); + if (!fragment_translator_->Init(EShLangFragment, &resources)) { + DLOG(ERROR) << "Could not initialize fragment shader translator."; Destroy(); return false; } @@ -2076,15 +2053,6 @@ void GLES2DecoderImpl::SetSwapBuffersCallback(Callback0::Type* callback) { } void GLES2DecoderImpl::Destroy() { - if (vertex_compiler_ != NULL) { - ShDestruct(vertex_compiler_); - vertex_compiler_ = NULL; - } - if (fragment_compiler_ != NULL) { - ShDestruct(fragment_compiler_); - fragment_compiler_ = NULL; - } - if (context_.get()) { MakeCurrent(); @@ -3746,15 +3714,14 @@ void GLES2DecoderImpl::DoCompileShader(GLuint client_id) { // glShaderSource and then glCompileShader. const char* shader_src = info->source().c_str(); if (use_shader_translator_) { - int dbg_options = 0; - ShHandle compiler = info->shader_type() == GL_VERTEX_SHADER ? - vertex_compiler_ : fragment_compiler_; + ShaderTranslator* translator = info->shader_type() == GL_VERTEX_SHADER ? + vertex_translator_.get() : fragment_translator_.get(); - if (!ShCompile(compiler, &shader_src, 1, EShOptNone, dbg_options)) { - info->SetStatus(false, ShGetInfoLog(compiler)); + if (!translator->Translate(shader_src)) { + info->SetStatus(false, translator->info_log()); return; } - shader_src = ShGetObjectCode(compiler); + shader_src = translator->translated_shader(); } glShaderSource(info->service_id(), 1, &shader_src, NULL); diff --git a/gpu/command_buffer/service/shader_translator.cc b/gpu/command_buffer/service/shader_translator.cc new file mode 100644 index 0000000..dd3ea24 --- /dev/null +++ b/gpu/command_buffer/service/shader_translator.cc @@ -0,0 +1,85 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "gpu/command_buffer/service/shader_translator.h" + +#include "base/at_exit.h" +#include "base/logging.h" + +namespace { +void FinalizeShaderTranslator(void* /* dummy */) { + ShFinalize(); +} + +bool InitializeShaderTranslator() { + static bool initialized = false; + if (!initialized && ShInitialize()) { + base::AtExitManager::RegisterCallback(&FinalizeShaderTranslator, NULL); + initialized = true; + } + return initialized; +} +} // namespace + +namespace gpu { +namespace gles2 { + +ShaderTranslator::ShaderTranslator() : compiler_(NULL) { +} + +ShaderTranslator::~ShaderTranslator() { + if (compiler_ != NULL) + ShDestruct(compiler_); +} + +bool ShaderTranslator::Init(EShLanguage language, + const TBuiltInResource* resources) { + // Make sure Init is called only once. + DCHECK(compiler_ == NULL); + DCHECK(language == EShLangVertex || language == EShLangFragment); + DCHECK(resources != NULL); + + if (!InitializeShaderTranslator()) + return false; + + compiler_ = ShConstructCompiler(language, EShSpecGLES2, resources); + return compiler_ != NULL; +} + +bool ShaderTranslator::Translate(const char* shader) { + // Make sure this instance is initialized. + DCHECK(compiler_ != NULL); + DCHECK(shader != NULL); + ClearResults(); + + bool success = false; + int compile_options = EShOptObjectCode; + if (ShCompile(compiler_, &shader, 1, compile_options)) { + // Get translated shader. + int obj_code_len = 0; + ShGetInfo(compiler_, SH_OBJECT_CODE_LENGTH, &obj_code_len); + translated_shader_.reset(new char[obj_code_len]); + ShGetObjectCode(compiler_, translated_shader_.get()); + + // TODO(alokp): Get attribs and uniforms. + success = true; + } + + // Get info log. + int info_log_len = 0; + ShGetInfo(compiler_, SH_INFO_LOG_LENGTH, &info_log_len); + info_log_.reset(new char[info_log_len]); + ShGetInfoLog(compiler_, info_log_.get()); + + return success; +} + +void ShaderTranslator::ClearResults() { + translated_shader_.reset(); + info_log_.reset(); +} + +} // namespace gles2 +} // namespace gpu + diff --git a/gpu/command_buffer/service/shader_translator.h b/gpu/command_buffer/service/shader_translator.h new file mode 100644 index 0000000..3dc0a05 --- /dev/null +++ b/gpu/command_buffer/service/shader_translator.h @@ -0,0 +1,50 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef GPU_COMMAND_BUFFER_SERVICE_SHADER_TRANSLATOR_H_ +#define GPU_COMMAND_BUFFER_SERVICE_SHADER_TRANSLATOR_H_ + +#include "base/basictypes.h" +#include "base/scoped_ptr.h" +#include "third_party/angle/include/GLSLANG/ShaderLang.h" + +namespace gpu { +namespace gles2 { + +// Translates GLSL ES 2.0 shader to desktop GLSL shader. +class ShaderTranslator { + public: + ShaderTranslator(); + ~ShaderTranslator(); + + // Initializes the translator. + // Must be called once before using the translator object. + bool Init(EShLanguage language, const TBuiltInResource* resources); + // Translates the given shader source. + // Returns true if translation is successful, false otherwise. + bool Translate(const char* shader); + + // The following functions return results from the last translation. + // The results are NULL/empty if the translation was unsuccessful. + // A valid info-log is always returned irrespective of whether translation + // was successful or not. + const char* translated_shader() { return translated_shader_.get(); } + const char* info_log() { return info_log_.get(); } + + // TODO(alokp): Add functions for returning attribs and uniforms. + + private: + void ClearResults(); + DISALLOW_COPY_AND_ASSIGN(ShaderTranslator); + + ShHandle compiler_; + scoped_array<char> translated_shader_; + scoped_array<char> info_log_; +}; + +} // namespace gles2 +} // namespace gpu + +#endif // GPU_COMMAND_BUFFER_SERVICE_SHADER_TRANSLATOR_H_ + diff --git a/gpu/gpu.gyp b/gpu/gpu.gyp index 4836307..f576bfe 100644 --- a/gpu/gpu.gyp +++ b/gpu/gpu.gyp @@ -172,6 +172,8 @@ 'command_buffer/service/renderbuffer_manager.cc', 'command_buffer/service/shader_manager.h', 'command_buffer/service/shader_manager.cc', + 'command_buffer/service/shader_translator.h', + 'command_buffer/service/shader_translator.cc', 'command_buffer/service/texture_manager.h', 'command_buffer/service/texture_manager.cc', ], |