Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(972)

Side by Side Diff: gpu/command_buffer/service/program_manager.cc

Issue 10534173: GPU Program Caching (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: fixed windows git try error Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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>
7 #include <set> 8 #include <set>
9 #include <utility>
8 #include <vector> 10 #include <vector>
9 11
10 #include "base/basictypes.h" 12 #include "base/basictypes.h"
11 #include "base/command_line.h" 13 #include "base/command_line.h"
12 #include "base/logging.h" 14 #include "base/logging.h"
13 #include "base/memory/scoped_ptr.h" 15 #include "base/memory/scoped_ptr.h"
14 #include "base/string_number_conversions.h" 16 #include "base/string_number_conversions.h"
15 #include "gpu/command_buffer/common/gles2_cmd_format.h" 17 #include "gpu/command_buffer/common/gles2_cmd_format.h"
16 #include "gpu/command_buffer/common/gles2_cmd_utils.h" 18 #include "gpu/command_buffer/common/gles2_cmd_utils.h"
19 #include "gpu/command_buffer/service/feature_info.h"
17 #include "gpu/command_buffer/service/gles2_cmd_decoder.h" 20 #include "gpu/command_buffer/service/gles2_cmd_decoder.h"
18 #include "gpu/command_buffer/service/gpu_switches.h" 21 #include "gpu/command_buffer/service/gpu_switches.h"
22 #include "gpu/command_buffer/service/program_cache.h"
19 23
20 namespace gpu { 24 namespace gpu {
21 namespace gles2 { 25 namespace gles2 {
22 26
23 namespace { 27 namespace {
24 28
25 int ShaderTypeToIndex(GLenum shader_type) { 29 int ShaderTypeToIndex(GLenum shader_type) {
26 switch (shader_type) { 30 switch (shader_type) {
27 case GL_VERTEX_SHADER: 31 case GL_VERTEX_SHADER:
28 return 0; 32 return 0;
29 case GL_FRAGMENT_SHADER: 33 case GL_FRAGMENT_SHADER:
30 return 1; 34 return 1;
31 default: 35 default:
32 NOTREACHED(); 36 NOTREACHED();
33 return 0; 37 return 0;
34 } 38 }
35 } 39 }
36 40
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
37 // Given a name like "foo.bar[123].moo[456]" sets new_name to "foo.bar[123].moo" 56 // Given a name like "foo.bar[123].moo[456]" sets new_name to "foo.bar[123].moo"
38 // and sets element_index to 456. returns false if element expression was not a 57 // and sets element_index to 456. returns false if element expression was not a
39 // whole decimal number. For example: "foo[1b2]" 58 // whole decimal number. For example: "foo[1b2]"
40 bool GetUniformNameSansElement( 59 bool GetUniformNameSansElement(
41 const std::string name, int* element_index, std::string* new_name) { 60 const std::string name, int* element_index, std::string* new_name) {
42 DCHECK(element_index); 61 DCHECK(element_index);
43 DCHECK(new_name); 62 DCHECK(new_name);
44 if (name.size() < 3 || name[name.size() - 1] != ']') { 63 if (name.size() < 3 || name[name.size() - 1] != ']') {
45 *element_index = 0; 64 *element_index = 0;
46 *new_name = name; 65 *new_name = name;
(...skipping 306 matching lines...) Expand 10 before | Expand all | Expand 10 after
353 372
354 void ProgramManager::ProgramInfo::ExecuteBindAttribLocationCalls() { 373 void ProgramManager::ProgramInfo::ExecuteBindAttribLocationCalls() {
355 for (LocationMap::const_iterator it = bind_attrib_location_map_.begin(); 374 for (LocationMap::const_iterator it = bind_attrib_location_map_.begin();
356 it != bind_attrib_location_map_.end(); ++it) { 375 it != bind_attrib_location_map_.end(); ++it) {
357 const std::string* mapped_name = GetAttribMappedName(it->first); 376 const std::string* mapped_name = GetAttribMappedName(it->first);
358 if (mapped_name && *mapped_name != it->first) 377 if (mapped_name && *mapped_name != it->first)
359 glBindAttribLocation(service_id_, it->second, mapped_name->c_str()); 378 glBindAttribLocation(service_id_, it->second, mapped_name->c_str());
360 } 379 }
361 } 380 }
362 381
363 bool ProgramManager::ProgramInfo::Link() { 382 void ProgramManager::DoCompileShader(ShaderManager::ShaderInfo* info,
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) {
364 ClearLinkStatus(); 459 ClearLinkStatus();
365 if (!CanLink()) { 460 if (!CanLink()) {
366 set_log_info("missing shaders"); 461 set_log_info("missing shaders");
367 return false; 462 return false;
368 } 463 }
369 if (DetectAttribLocationBindingConflicts()) { 464 if (DetectAttribLocationBindingConflicts()) {
370 set_log_info("glBindAttribLocation() conflicts"); 465 set_log_info("glBindAttribLocation() conflicts");
371 return false; 466 return false;
372 } 467 }
373 ExecuteBindAttribLocationCalls(); 468 ExecuteBindAttribLocationCalls();
374 glLinkProgram(service_id()); 469
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 if (cache && gfx::g_GL_ARB_get_program_binary) {
521 glProgramParameteri(service_id(),
522 PROGRAM_BINARY_RETRIEVABLE_HINT,
523 GL_TRUE);
524 }
525 glLinkProgram(service_id());
526 }
527
375 GLint success = 0; 528 GLint success = 0;
376 glGetProgramiv(service_id(), GL_LINK_STATUS, &success); 529 glGetProgramiv(service_id(), GL_LINK_STATUS, &success);
377 if (success == GL_TRUE) { 530 if (success == GL_TRUE) {
378 Update(); 531 Update();
532 if (cache && link) {
533 cache->SaveLinkedProgram(service_id(),
534 attached_shaders_[0],
535 attached_shaders_[1],
536 &bind_attrib_location_map_);
537 }
379 } else { 538 } else {
380 UpdateLogInfo(); 539 UpdateLogInfo();
381 } 540 }
382 return success == GL_TRUE; 541 return success == GL_TRUE;
383 } 542 }
384 543
385 void ProgramManager::ProgramInfo::Validate() { 544 void ProgramManager::ProgramInfo::Validate() {
386 if (!IsValid()) { 545 if (!IsValid()) {
387 set_log_info("program not linked"); 546 set_log_info("program not linked");
388 return; 547 return;
(...skipping 452 matching lines...) Expand 10 before | Expand all | Expand 10 after
841 ProgramManager::ProgramInfo::~ProgramInfo() { 1000 ProgramManager::ProgramInfo::~ProgramInfo() {
842 if (manager_) { 1001 if (manager_) {
843 if (manager_->have_context_) { 1002 if (manager_->have_context_) {
844 glDeleteProgram(service_id()); 1003 glDeleteProgram(service_id());
845 } 1004 }
846 manager_->StopTracking(this); 1005 manager_->StopTracking(this);
847 manager_ = NULL; 1006 manager_ = NULL;
848 } 1007 }
849 } 1008 }
850 1009
851 ProgramManager::ProgramManager() 1010
1011 ProgramManager::ProgramManager(ProgramCache* program_cache)
852 : program_info_count_(0), 1012 : program_info_count_(0),
853 have_context_(true), 1013 have_context_(true),
854 disable_workarounds_( 1014 disable_workarounds_(
855 CommandLine::ForCurrentProcess()->HasSwitch( 1015 CommandLine::ForCurrentProcess()->HasSwitch(
856 switches::kDisableGpuDriverBugWorkarounds)) { 1016 switches::kDisableGpuDriverBugWorkarounds)),
857 } 1017 program_cache_(program_cache) { }
858 1018
859 ProgramManager::~ProgramManager() { 1019 ProgramManager::~ProgramManager() {
860 DCHECK(program_infos_.empty()); 1020 DCHECK(program_infos_.empty());
861 } 1021 }
862 1022
863 void ProgramManager::Destroy(bool have_context) { 1023 void ProgramManager::Destroy(bool have_context) {
864 have_context_ = have_context; 1024 have_context_ = have_context;
865 program_infos_.clear(); 1025 program_infos_.clear();
866 } 1026 }
867 1027
(...skipping 25 matching lines...) Expand all
893 for (ProgramInfoMap::const_iterator it = program_infos_.begin(); 1053 for (ProgramInfoMap::const_iterator it = program_infos_.begin();
894 it != program_infos_.end(); ++it) { 1054 it != program_infos_.end(); ++it) {
895 if (it->second->service_id() == service_id) { 1055 if (it->second->service_id() == service_id) {
896 *client_id = it->first; 1056 *client_id = it->first;
897 return true; 1057 return true;
898 } 1058 }
899 } 1059 }
900 return false; 1060 return false;
901 } 1061 }
902 1062
1063 ProgramCache* ProgramManager::program_cache() const {
1064 return program_cache_;
1065 }
1066
903 bool ProgramManager::IsOwned(ProgramManager::ProgramInfo* info) { 1067 bool ProgramManager::IsOwned(ProgramManager::ProgramInfo* info) {
904 for (ProgramInfoMap::iterator it = program_infos_.begin(); 1068 for (ProgramInfoMap::iterator it = program_infos_.begin();
905 it != program_infos_.end(); ++it) { 1069 it != program_infos_.end(); ++it) {
906 if (it->second.get() == info) { 1070 if (it->second.get() == info) {
907 return true; 1071 return true;
908 } 1072 }
909 } 1073 }
910 return false; 1074 return false;
911 } 1075 }
912 1076
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
963 } 1127 }
964 1128
965 int32 ProgramManager::MakeFakeLocation(int32 index, int32 element) { 1129 int32 ProgramManager::MakeFakeLocation(int32 index, int32 element) {
966 return index + element * 0x10000; 1130 return index + element * 0x10000;
967 } 1131 }
968 1132
969 } // namespace gles2 1133 } // namespace gles2
970 } // namespace gpu 1134 } // namespace gpu
971 1135
972 1136
OLDNEW
« no previous file with comments | « gpu/command_buffer/service/program_manager.h ('k') | gpu/command_buffer/service/program_manager_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698