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

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

Issue 10797055: gpu in-memory program cache implementation with a memory limit + lru eviction. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: simplified binary loading logic 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 if (cache) {
473 ProgramCache::LinkedProgramStatus status = cache->GetLinkedProgramStatus(
474 *attached_shaders_[0]->deferred_compilation_source(),
475 *attached_shaders_[1]->deferred_compilation_source(),
476 &bind_attrib_location_map_);
477
478 if(status == ProgramCache::LINK_SUCCEEDED) {
479 ProgramCache::ProgramLoadResult success = cache->LoadLinkedProgram(
480 service_id(),
481 attached_shaders_[0],
482 attached_shaders_[1],
483 &bind_attrib_location_map_);
484 link = success != ProgramCache::PROGRAM_LOAD_SUCCESS;
485 }
486
487 if (link) {
488 // compile our shaders if they're pending
489 const int kShaders = ProgramManager::ProgramInfo::kMaxAttachedShaders;
490 for (int i = 0; i < kShaders; ++i) {
491 ShaderManager::ShaderInfo* info = attached_shaders_[i].get();
492 if (!info->source_compiled()) {
493 ShaderTranslator* translator = ShaderIndexToTranslator(
494 i,
495 vertex_translator,
496 fragment_translator);
497 manager_->ForceCompileShader(info->deferred_compilation_source(),
498 attached_shaders_[i],
499 translator,
500 feature_info);
501 CHECK(info->IsValid());
502 }
503 }
504 }
505 }
506
507 if (link) {
508 if (cache && gfx::g_GL_ARB_get_program_binary) {
509 glProgramParameteri(service_id(),
510 PROGRAM_BINARY_RETRIEVABLE_HINT,
511 GL_TRUE);
512 }
513 glLinkProgram(service_id());
514 }
515
375 GLint success = 0; 516 GLint success = 0;
376 glGetProgramiv(service_id(), GL_LINK_STATUS, &success); 517 glGetProgramiv(service_id(), GL_LINK_STATUS, &success);
377 if (success == GL_TRUE) { 518 if (success == GL_TRUE) {
378 Update(); 519 Update();
520 if (cache && link) {
521 cache->SaveLinkedProgram(service_id(),
522 attached_shaders_[0],
523 attached_shaders_[1],
524 &bind_attrib_location_map_);
525 }
379 } else { 526 } else {
380 UpdateLogInfo(); 527 UpdateLogInfo();
381 } 528 }
382 return success == GL_TRUE; 529 return success == GL_TRUE;
383 } 530 }
384 531
385 void ProgramManager::ProgramInfo::Validate() { 532 void ProgramManager::ProgramInfo::Validate() {
386 if (!IsValid()) { 533 if (!IsValid()) {
387 set_log_info("program not linked"); 534 set_log_info("program not linked");
388 return; 535 return;
(...skipping 452 matching lines...) Expand 10 before | Expand all | Expand 10 after
841 ProgramManager::ProgramInfo::~ProgramInfo() { 988 ProgramManager::ProgramInfo::~ProgramInfo() {
842 if (manager_) { 989 if (manager_) {
843 if (manager_->have_context_) { 990 if (manager_->have_context_) {
844 glDeleteProgram(service_id()); 991 glDeleteProgram(service_id());
845 } 992 }
846 manager_->StopTracking(this); 993 manager_->StopTracking(this);
847 manager_ = NULL; 994 manager_ = NULL;
848 } 995 }
849 } 996 }
850 997
851 ProgramManager::ProgramManager() 998
999 ProgramManager::ProgramManager(ProgramCache* program_cache)
852 : program_info_count_(0), 1000 : program_info_count_(0),
853 have_context_(true), 1001 have_context_(true),
854 disable_workarounds_( 1002 disable_workarounds_(
855 CommandLine::ForCurrentProcess()->HasSwitch( 1003 CommandLine::ForCurrentProcess()->HasSwitch(
856 switches::kDisableGpuDriverBugWorkarounds)) { 1004 switches::kDisableGpuDriverBugWorkarounds)),
857 } 1005 program_cache_(program_cache) { }
858 1006
859 ProgramManager::~ProgramManager() { 1007 ProgramManager::~ProgramManager() {
860 DCHECK(program_infos_.empty()); 1008 DCHECK(program_infos_.empty());
861 } 1009 }
862 1010
863 void ProgramManager::Destroy(bool have_context) { 1011 void ProgramManager::Destroy(bool have_context) {
864 have_context_ = have_context; 1012 have_context_ = have_context;
865 program_infos_.clear(); 1013 program_infos_.clear();
866 } 1014 }
867 1015
(...skipping 25 matching lines...) Expand all
893 for (ProgramInfoMap::const_iterator it = program_infos_.begin(); 1041 for (ProgramInfoMap::const_iterator it = program_infos_.begin();
894 it != program_infos_.end(); ++it) { 1042 it != program_infos_.end(); ++it) {
895 if (it->second->service_id() == service_id) { 1043 if (it->second->service_id() == service_id) {
896 *client_id = it->first; 1044 *client_id = it->first;
897 return true; 1045 return true;
898 } 1046 }
899 } 1047 }
900 return false; 1048 return false;
901 } 1049 }
902 1050
1051 ProgramCache* ProgramManager::program_cache() const {
1052 return program_cache_;
1053 }
1054
903 bool ProgramManager::IsOwned(ProgramManager::ProgramInfo* info) { 1055 bool ProgramManager::IsOwned(ProgramManager::ProgramInfo* info) {
904 for (ProgramInfoMap::iterator it = program_infos_.begin(); 1056 for (ProgramInfoMap::iterator it = program_infos_.begin();
905 it != program_infos_.end(); ++it) { 1057 it != program_infos_.end(); ++it) {
906 if (it->second.get() == info) { 1058 if (it->second.get() == info) {
907 return true; 1059 return true;
908 } 1060 }
909 } 1061 }
910 return false; 1062 return false;
911 } 1063 }
912 1064
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
963 } 1115 }
964 1116
965 int32 ProgramManager::MakeFakeLocation(int32 index, int32 element) { 1117 int32 ProgramManager::MakeFakeLocation(int32 index, int32 element) {
966 return index + element * 0x10000; 1118 return index + element * 0x10000;
967 } 1119 }
968 1120
969 } // namespace gles2 1121 } // namespace gles2
970 } // namespace gpu 1122 } // namespace gpu
971 1123
972 1124
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698