| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "gpu/command_buffer/service/program_manager.h" | 5 #include "gpu/command_buffer/service/program_manager.h" |
| 6 | 6 |
| 7 #include <algorithm> | |
| 8 #include <set> | 7 #include <set> |
| 9 #include <utility> | |
| 10 #include <vector> | 8 #include <vector> |
| 11 | 9 |
| 12 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 13 #include "base/command_line.h" | 11 #include "base/command_line.h" |
| 14 #include "base/logging.h" | 12 #include "base/logging.h" |
| 15 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 16 #include "base/string_number_conversions.h" | 14 #include "base/string_number_conversions.h" |
| 17 #include "gpu/command_buffer/common/gles2_cmd_format.h" | 15 #include "gpu/command_buffer/common/gles2_cmd_format.h" |
| 18 #include "gpu/command_buffer/common/gles2_cmd_utils.h" | 16 #include "gpu/command_buffer/common/gles2_cmd_utils.h" |
| 19 #include "gpu/command_buffer/service/feature_info.h" | |
| 20 #include "gpu/command_buffer/service/gles2_cmd_decoder.h" | 17 #include "gpu/command_buffer/service/gles2_cmd_decoder.h" |
| 21 #include "gpu/command_buffer/service/gpu_switches.h" | 18 #include "gpu/command_buffer/service/gpu_switches.h" |
| 22 #include "gpu/command_buffer/service/program_cache.h" | |
| 23 | 19 |
| 24 namespace gpu { | 20 namespace gpu { |
| 25 namespace gles2 { | 21 namespace gles2 { |
| 26 | 22 |
| 27 namespace { | 23 namespace { |
| 28 | 24 |
| 29 int ShaderTypeToIndex(GLenum shader_type) { | 25 int ShaderTypeToIndex(GLenum shader_type) { |
| 30 switch (shader_type) { | 26 switch (shader_type) { |
| 31 case GL_VERTEX_SHADER: | 27 case GL_VERTEX_SHADER: |
| 32 return 0; | 28 return 0; |
| 33 case GL_FRAGMENT_SHADER: | 29 case GL_FRAGMENT_SHADER: |
| 34 return 1; | 30 return 1; |
| 35 default: | 31 default: |
| 36 NOTREACHED(); | 32 NOTREACHED(); |
| 37 return 0; | 33 return 0; |
| 38 } | 34 } |
| 39 } | 35 } |
| 40 | 36 |
| 41 ShaderTranslator* ShaderIndexToTranslator( | |
| 42 int index, | |
| 43 ShaderTranslator* vertex_translator, | |
| 44 ShaderTranslator* fragment_translator) { | |
| 45 switch (index) { | |
| 46 case 0: | |
| 47 return vertex_translator; | |
| 48 case 1: | |
| 49 return fragment_translator; | |
| 50 default: | |
| 51 NOTREACHED(); | |
| 52 return NULL; | |
| 53 } | |
| 54 } | |
| 55 | |
| 56 // Given a name like "foo.bar[123].moo[456]" sets new_name to "foo.bar[123].moo" | 37 // Given a name like "foo.bar[123].moo[456]" sets new_name to "foo.bar[123].moo" |
| 57 // and sets element_index to 456. returns false if element expression was not a | 38 // and sets element_index to 456. returns false if element expression was not a |
| 58 // whole decimal number. For example: "foo[1b2]" | 39 // whole decimal number. For example: "foo[1b2]" |
| 59 bool GetUniformNameSansElement( | 40 bool GetUniformNameSansElement( |
| 60 const std::string name, int* element_index, std::string* new_name) { | 41 const std::string name, int* element_index, std::string* new_name) { |
| 61 DCHECK(element_index); | 42 DCHECK(element_index); |
| 62 DCHECK(new_name); | 43 DCHECK(new_name); |
| 63 if (name.size() < 3 || name[name.size() - 1] != ']') { | 44 if (name.size() < 3 || name[name.size() - 1] != ']') { |
| 64 *element_index = 0; | 45 *element_index = 0; |
| 65 *new_name = name; | 46 *new_name = name; |
| (...skipping 306 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 372 | 353 |
| 373 void ProgramManager::ProgramInfo::ExecuteBindAttribLocationCalls() { | 354 void ProgramManager::ProgramInfo::ExecuteBindAttribLocationCalls() { |
| 374 for (LocationMap::const_iterator it = bind_attrib_location_map_.begin(); | 355 for (LocationMap::const_iterator it = bind_attrib_location_map_.begin(); |
| 375 it != bind_attrib_location_map_.end(); ++it) { | 356 it != bind_attrib_location_map_.end(); ++it) { |
| 376 const std::string* mapped_name = GetAttribMappedName(it->first); | 357 const std::string* mapped_name = GetAttribMappedName(it->first); |
| 377 if (mapped_name && *mapped_name != it->first) | 358 if (mapped_name && *mapped_name != it->first) |
| 378 glBindAttribLocation(service_id_, it->second, mapped_name->c_str()); | 359 glBindAttribLocation(service_id_, it->second, mapped_name->c_str()); |
| 379 } | 360 } |
| 380 } | 361 } |
| 381 | 362 |
| 382 void ProgramManager::DoCompileShader(ShaderManager::ShaderInfo* info, | 363 bool ProgramManager::ProgramInfo::Link() { |
| 383 ShaderTranslator* translator, | |
| 384 FeatureInfo* feature_info) { | |
| 385 if (program_cache_ && | |
| 386 program_cache_->GetShaderCompilationStatus(*info->source()) == | |
| 387 ProgramCache::COMPILATION_SUCCEEDED) { | |
| 388 info->SetStatus(true, "", translator); | |
| 389 info->FlagSourceAsCompiled(false); | |
| 390 return; | |
| 391 } | |
| 392 ForceCompileShader(info->source(), info, translator, feature_info); | |
| 393 } | |
| 394 | |
| 395 void ProgramManager::ForceCompileShader(const std::string* source, | |
| 396 ShaderManager::ShaderInfo* info, | |
| 397 ShaderTranslator* translator, | |
| 398 FeatureInfo* feature_info) { | |
| 399 info->FlagSourceAsCompiled(true); | |
| 400 | |
| 401 // Translate GL ES 2.0 shader to Desktop GL shader and pass that to | |
| 402 // glShaderSource and then glCompileShader. | |
| 403 const char* shader_src = source ? source->c_str() : ""; | |
| 404 if (translator) { | |
| 405 if (!translator->Translate(shader_src)) { | |
| 406 info->SetStatus(false, translator->info_log(), NULL); | |
| 407 return; | |
| 408 } | |
| 409 shader_src = translator->translated_shader(); | |
| 410 if (!feature_info->feature_flags().angle_translated_shader_source) | |
| 411 info->UpdateTranslatedSource(shader_src); | |
| 412 } | |
| 413 | |
| 414 glShaderSource(info->service_id(), 1, &shader_src, NULL); | |
| 415 glCompileShader(info->service_id()); | |
| 416 if (feature_info->feature_flags().angle_translated_shader_source) { | |
| 417 GLint max_len = 0; | |
| 418 glGetShaderiv(info->service_id(), | |
| 419 GL_TRANSLATED_SHADER_SOURCE_LENGTH_ANGLE, | |
| 420 &max_len); | |
| 421 scoped_array<char> temp(new char[max_len]); | |
| 422 GLint len = 0; | |
| 423 glGetTranslatedShaderSourceANGLE( | |
| 424 info->service_id(), max_len, &len, temp.get()); | |
| 425 DCHECK(max_len == 0 || len < max_len); | |
| 426 DCHECK(len == 0 || temp[len] == '\0'); | |
| 427 info->UpdateTranslatedSource(temp.get()); | |
| 428 } | |
| 429 | |
| 430 GLint status = GL_FALSE; | |
| 431 glGetShaderiv(info->service_id(), GL_COMPILE_STATUS, &status); | |
| 432 if (status) { | |
| 433 info->SetStatus(true, "", translator); | |
| 434 if (program_cache_) { | |
| 435 const char* untranslated_source = source ? source->c_str() : ""; | |
| 436 program_cache_->ShaderCompilationSucceeded(untranslated_source); | |
| 437 } | |
| 438 } else { | |
| 439 // We cannot reach here if we are using the shader translator. | |
| 440 // All invalid shaders must be rejected by the translator. | |
| 441 // All translated shaders must compile. | |
| 442 LOG_IF(ERROR, translator) | |
| 443 << "Shader translator allowed/produced an invalid shader."; | |
| 444 GLint max_len = 0; | |
| 445 glGetShaderiv(info->service_id(), GL_INFO_LOG_LENGTH, &max_len); | |
| 446 scoped_array<char> temp(new char[max_len]); | |
| 447 GLint len = 0; | |
| 448 glGetShaderInfoLog(info->service_id(), max_len, &len, temp.get()); | |
| 449 DCHECK(max_len == 0 || len < max_len); | |
| 450 DCHECK(len == 0 || temp[len] == '\0'); | |
| 451 info->SetStatus(false, std::string(temp.get(), len).c_str(), NULL); | |
| 452 } | |
| 453 } | |
| 454 | |
| 455 bool ProgramManager::ProgramInfo::Link(ShaderManager* manager, | |
| 456 ShaderTranslator* vertex_translator, | |
| 457 ShaderTranslator* fragment_translator, | |
| 458 FeatureInfo* feature_info) { | |
| 459 ClearLinkStatus(); | 364 ClearLinkStatus(); |
| 460 if (!CanLink()) { | 365 if (!CanLink()) { |
| 461 set_log_info("missing shaders"); | 366 set_log_info("missing shaders"); |
| 462 return false; | 367 return false; |
| 463 } | 368 } |
| 464 if (DetectAttribLocationBindingConflicts()) { | 369 if (DetectAttribLocationBindingConflicts()) { |
| 465 set_log_info("glBindAttribLocation() conflicts"); | 370 set_log_info("glBindAttribLocation() conflicts"); |
| 466 return false; | 371 return false; |
| 467 } | 372 } |
| 468 ExecuteBindAttribLocationCalls(); | 373 ExecuteBindAttribLocationCalls(); |
| 469 | 374 glLinkProgram(service_id()); |
| 470 bool link = true; | |
| 471 ProgramCache* cache = manager_->program_cache_; | |
| 472 const std::string* shader_a = | |
| 473 attached_shaders_[0]->deferred_compilation_source(); | |
| 474 const std::string* shader_b = | |
| 475 attached_shaders_[1]->deferred_compilation_source(); | |
| 476 if (cache) { | |
| 477 ProgramCache::LinkedProgramStatus status = cache->GetLinkedProgramStatus( | |
| 478 *shader_a, | |
| 479 *shader_b, | |
| 480 &bind_attrib_location_map_); | |
| 481 switch (status) { | |
| 482 case ProgramCache::LINK_SUCCEEDED: { | |
| 483 ProgramCache::ProgramLoadResult success = cache->LoadLinkedProgram( | |
| 484 service_id(), | |
| 485 attached_shaders_[0], | |
| 486 attached_shaders_[1], | |
| 487 &bind_attrib_location_map_); | |
| 488 if (success == ProgramCache::PROGRAM_LOAD_SUCCESS) { | |
| 489 link = false; | |
| 490 break; | |
| 491 } | |
| 492 } | |
| 493 // no break | |
| 494 case ProgramCache::LINK_UNKNOWN: { | |
| 495 // compile our shaders + attach | |
| 496 const int kShaders = ProgramManager::ProgramInfo::kMaxAttachedShaders; | |
| 497 for (int i = 0; i < kShaders; ++i) { | |
| 498 ShaderManager::ShaderInfo* info = attached_shaders_[i].get(); | |
| 499 if (!info->source_compiled()) { | |
| 500 ShaderTranslator* translator = ShaderIndexToTranslator( | |
| 501 i, | |
| 502 vertex_translator, | |
| 503 fragment_translator); | |
| 504 manager_->ForceCompileShader(info->deferred_compilation_source(), | |
| 505 attached_shaders_[i], | |
| 506 translator, | |
| 507 feature_info); | |
| 508 CHECK(info->IsValid()); | |
| 509 } | |
| 510 } | |
| 511 link = true; | |
| 512 break; | |
| 513 } | |
| 514 default: | |
| 515 NOTREACHED(); | |
| 516 } | |
| 517 } | |
| 518 | |
| 519 if (link) { | |
| 520 glLinkProgram(service_id()); | |
| 521 } | |
| 522 | |
| 523 GLint success = 0; | 375 GLint success = 0; |
| 524 glGetProgramiv(service_id(), GL_LINK_STATUS, &success); | 376 glGetProgramiv(service_id(), GL_LINK_STATUS, &success); |
| 525 if (success == GL_TRUE) { | 377 if (success == GL_TRUE) { |
| 526 Update(); | 378 Update(); |
| 527 if (cache && link) { | |
| 528 cache->SaveLinkedProgram(service_id(), | |
| 529 attached_shaders_[0], | |
| 530 attached_shaders_[1], | |
| 531 &bind_attrib_location_map_); | |
| 532 } | |
| 533 } else { | 379 } else { |
| 534 UpdateLogInfo(); | 380 UpdateLogInfo(); |
| 535 } | 381 } |
| 536 return success == GL_TRUE; | 382 return success == GL_TRUE; |
| 537 } | 383 } |
| 538 | 384 |
| 539 void ProgramManager::ProgramInfo::Validate() { | 385 void ProgramManager::ProgramInfo::Validate() { |
| 540 if (!IsValid()) { | 386 if (!IsValid()) { |
| 541 set_log_info("program not linked"); | 387 set_log_info("program not linked"); |
| 542 return; | 388 return; |
| (...skipping 452 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 995 ProgramManager::ProgramInfo::~ProgramInfo() { | 841 ProgramManager::ProgramInfo::~ProgramInfo() { |
| 996 if (manager_) { | 842 if (manager_) { |
| 997 if (manager_->have_context_) { | 843 if (manager_->have_context_) { |
| 998 glDeleteProgram(service_id()); | 844 glDeleteProgram(service_id()); |
| 999 } | 845 } |
| 1000 manager_->StopTracking(this); | 846 manager_->StopTracking(this); |
| 1001 manager_ = NULL; | 847 manager_ = NULL; |
| 1002 } | 848 } |
| 1003 } | 849 } |
| 1004 | 850 |
| 1005 | 851 ProgramManager::ProgramManager() |
| 1006 ProgramManager::ProgramManager(ProgramCache* program_cache) | |
| 1007 : program_info_count_(0), | 852 : program_info_count_(0), |
| 1008 have_context_(true), | 853 have_context_(true), |
| 1009 disable_workarounds_( | 854 disable_workarounds_( |
| 1010 CommandLine::ForCurrentProcess()->HasSwitch( | 855 CommandLine::ForCurrentProcess()->HasSwitch( |
| 1011 switches::kDisableGpuDriverBugWorkarounds)), | 856 switches::kDisableGpuDriverBugWorkarounds)) { |
| 1012 program_cache_(program_cache) { } | 857 } |
| 1013 | 858 |
| 1014 ProgramManager::~ProgramManager() { | 859 ProgramManager::~ProgramManager() { |
| 1015 DCHECK(program_infos_.empty()); | 860 DCHECK(program_infos_.empty()); |
| 1016 } | 861 } |
| 1017 | 862 |
| 1018 void ProgramManager::Destroy(bool have_context) { | 863 void ProgramManager::Destroy(bool have_context) { |
| 1019 have_context_ = have_context; | 864 have_context_ = have_context; |
| 1020 program_infos_.clear(); | 865 program_infos_.clear(); |
| 1021 } | 866 } |
| 1022 | 867 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 1048 for (ProgramInfoMap::const_iterator it = program_infos_.begin(); | 893 for (ProgramInfoMap::const_iterator it = program_infos_.begin(); |
| 1049 it != program_infos_.end(); ++it) { | 894 it != program_infos_.end(); ++it) { |
| 1050 if (it->second->service_id() == service_id) { | 895 if (it->second->service_id() == service_id) { |
| 1051 *client_id = it->first; | 896 *client_id = it->first; |
| 1052 return true; | 897 return true; |
| 1053 } | 898 } |
| 1054 } | 899 } |
| 1055 return false; | 900 return false; |
| 1056 } | 901 } |
| 1057 | 902 |
| 1058 ProgramCache* ProgramManager::program_cache() const { | |
| 1059 return program_cache_; | |
| 1060 } | |
| 1061 | |
| 1062 bool ProgramManager::IsOwned(ProgramManager::ProgramInfo* info) { | 903 bool ProgramManager::IsOwned(ProgramManager::ProgramInfo* info) { |
| 1063 for (ProgramInfoMap::iterator it = program_infos_.begin(); | 904 for (ProgramInfoMap::iterator it = program_infos_.begin(); |
| 1064 it != program_infos_.end(); ++it) { | 905 it != program_infos_.end(); ++it) { |
| 1065 if (it->second.get() == info) { | 906 if (it->second.get() == info) { |
| 1066 return true; | 907 return true; |
| 1067 } | 908 } |
| 1068 } | 909 } |
| 1069 return false; | 910 return false; |
| 1070 } | 911 } |
| 1071 | 912 |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1122 } | 963 } |
| 1123 | 964 |
| 1124 int32 ProgramManager::MakeFakeLocation(int32 index, int32 element) { | 965 int32 ProgramManager::MakeFakeLocation(int32 index, int32 element) { |
| 1125 return index + element * 0x10000; | 966 return index + element * 0x10000; |
| 1126 } | 967 } |
| 1127 | 968 |
| 1128 } // namespace gles2 | 969 } // namespace gles2 |
| 1129 } // namespace gpu | 970 } // namespace gpu |
| 1130 | 971 |
| 1131 | 972 |
| OLD | NEW |