X-Git-Url: https://git.sesse.net/?p=movit;a=blobdiff_plain;f=resource_pool.cpp;h=7739d32790198349fba99bac3dd00910cbaf8788;hp=6228fa2d259c5fa068862409ce8c335fb9b5224a;hb=HEAD;hpb=2d8043bb837b45c9ae509450b3e1b1eb545e44b9 diff --git a/resource_pool.cpp b/resource_pool.cpp index 6228fa2..7739d32 100644 --- a/resource_pool.cpp +++ b/resource_pool.cpp @@ -26,27 +26,23 @@ ResourcePool::ResourcePool(size_t program_freelist_max_length, vao_freelist_max_length(vao_freelist_max_length), texture_freelist_bytes(0) { - pthread_mutex_init(&lock, NULL); + pthread_mutex_init(&lock, nullptr); } ResourcePool::~ResourcePool() { assert(program_refcount.empty()); - for (list::const_iterator freelist_it = program_freelist.begin(); - freelist_it != program_freelist.end(); - ++freelist_it) { - delete_program(*freelist_it); + for (GLuint program : program_freelist) { + delete_program(program); } assert(programs.empty()); assert(program_shaders.empty()); - for (list::const_iterator freelist_it = texture_freelist.begin(); - freelist_it != texture_freelist.end(); - ++freelist_it) { - GLuint free_texture_num = *freelist_it; + for (GLuint free_texture_num : texture_freelist) { assert(texture_formats.count(free_texture_num) != 0); texture_freelist_bytes -= estimate_texture_size(texture_formats[free_texture_num]); + glDeleteSync(texture_formats[free_texture_num].no_reuse_before); texture_formats.erase(free_texture_num); glDeleteTextures(1, &free_texture_num); check_error(); @@ -57,18 +53,13 @@ ResourcePool::~ResourcePool() void *context = get_gl_context_identifier(); cleanup_unlinked_fbos(context); - for (map >::iterator context_it = fbo_freelist.begin(); - context_it != fbo_freelist.end(); - ++context_it) { - if (context_it->first != context) { + for (const auto &context_and_fbos : fbo_freelist) { + if (context_and_fbos.first != context) { // If this does not hold, the client should have called clean_context() earlier. - assert(context_it->second.empty()); + assert(context_and_fbos.second.empty()); continue; } - for (list::const_iterator freelist_it = context_it->second.begin(); - freelist_it != context_it->second.end(); - ++freelist_it) { - FBOFormatIterator fbo_it = *freelist_it; + for (FBOFormatIterator fbo_it : context_and_fbos.second) { glDeleteFramebuffers(1, &fbo_it->second.fbo_num); check_error(); fbo_formats.erase(fbo_it); @@ -90,9 +81,18 @@ void ResourcePool::delete_program(GLuint glsl_program_num) break; } } + for (map::iterator program_it = compute_programs.begin(); + program_it != compute_programs.end(); + ++program_it) { + if (program_it->second == glsl_program_num) { + compute_programs.erase(program_it); + found_program = true; + break; + } + } assert(found_program); - map >::iterator instance_list_it = program_instances.find(glsl_program_num); + map>::iterator instance_list_it = program_instances.find(glsl_program_num); assert(instance_list_it != program_instances.end()); while (!instance_list_it->second.empty()) { @@ -105,11 +105,19 @@ void ResourcePool::delete_program(GLuint glsl_program_num) map::iterator shader_it = program_shaders.find(glsl_program_num); - assert(shader_it != program_shaders.end()); - - glDeleteShader(shader_it->second.vs_obj); - glDeleteShader(shader_it->second.fs_obj); - program_shaders.erase(shader_it); + if (shader_it == program_shaders.end()) { + // Should be a compute shader. + map::iterator compute_shader_it = + compute_program_shaders.find(glsl_program_num); + assert(compute_shader_it != compute_program_shaders.end()); + + glDeleteShader(compute_shader_it->second.cs_obj); + compute_program_shaders.erase(compute_shader_it); + } else { + glDeleteShader(shader_it->second.vs_obj); + glDeleteShader(shader_it->second.fs_obj); + program_shaders.erase(shader_it); + } } GLuint ResourcePool::compile_glsl_program(const string& vertex_shader, @@ -130,19 +138,9 @@ GLuint ResourcePool::compile_glsl_program(const string& vertex_shader, const pair key(vertex_shader, fragment_shader_processed); if (programs.count(key)) { - // Already in the cache. Increment the refcount, or take it off the freelist - // if it's zero. + // Already in the cache. glsl_program_num = programs[key]; - map::iterator refcount_it = program_refcount.find(glsl_program_num); - if (refcount_it != program_refcount.end()) { - ++refcount_it->second; - } else { - list::iterator freelist_it = - find(program_freelist.begin(), program_freelist.end(), glsl_program_num); - assert(freelist_it != program_freelist.end()); - program_freelist.erase(freelist_it); - program_refcount.insert(make_pair(glsl_program_num, 1)); - } + increment_program_refcount(glsl_program_num); } else { // Not in the cache. Compile the shaders. GLuint vs_obj = compile_shader(vertex_shader, GL_VERTEX_SHADER); @@ -151,31 +149,16 @@ GLuint ResourcePool::compile_glsl_program(const string& vertex_shader, check_error(); glsl_program_num = link_program(vs_obj, fs_obj, fragment_shader_outputs); - if (movit_debug_level == MOVIT_DEBUG_ON) { - // Output shader to a temporary file, for easier debugging. - static int compiled_shader_num = 0; - char filename[256]; - sprintf(filename, "chain-%03d.frag", compiled_shader_num++); - FILE *fp = fopen(filename, "w"); - if (fp == NULL) { - perror(filename); - exit(1); - } - fprintf(fp, "%s\n", fragment_shader_processed.c_str()); - fclose(fp); - } + output_debug_shader(fragment_shader_processed, "frag"); programs.insert(make_pair(key, glsl_program_num)); - program_refcount.insert(make_pair(glsl_program_num, 1)); + add_master_program(glsl_program_num); + ShaderSpec spec; spec.vs_obj = vs_obj; spec.fs_obj = fs_obj; spec.fragment_shader_outputs = fragment_shader_outputs; program_shaders.insert(make_pair(glsl_program_num, spec)); - stack instances; - instances.push(glsl_program_num); - program_instances.insert(make_pair(glsl_program_num, instances)); - program_masters.insert(make_pair(glsl_program_num, glsl_program_num)); } pthread_mutex_unlock(&lock); return glsl_program_num; @@ -207,7 +190,7 @@ GLuint ResourcePool::link_program(GLuint vs_obj, glGetProgramiv(glsl_program_num, GL_LINK_STATUS, &success); if (success == GL_FALSE) { GLchar error_log[1024] = {0}; - glGetProgramInfoLog(glsl_program_num, 1024, NULL, error_log); + glGetProgramInfoLog(glsl_program_num, 1024, nullptr, error_log); fprintf(stderr, "Error linking program: %s\n", error_log); exit(1); } @@ -235,6 +218,56 @@ void ResourcePool::release_glsl_program(GLuint glsl_program_num) pthread_mutex_unlock(&lock); } +GLuint ResourcePool::compile_glsl_compute_program(const string& compute_shader) +{ + GLuint glsl_program_num; + pthread_mutex_lock(&lock); + + const string &key = compute_shader; + if (compute_programs.count(key)) { + // Already in the cache. + glsl_program_num = compute_programs[key]; + increment_program_refcount(glsl_program_num); + } else { + // Not in the cache. Compile the shader. + GLuint cs_obj = compile_shader(compute_shader, GL_COMPUTE_SHADER); + check_error(); + glsl_program_num = link_compute_program(cs_obj); + + output_debug_shader(compute_shader, "comp"); + + compute_programs.insert(make_pair(key, glsl_program_num)); + add_master_program(glsl_program_num); + + ComputeShaderSpec spec; + spec.cs_obj = cs_obj; + compute_program_shaders.insert(make_pair(glsl_program_num, spec)); + } + pthread_mutex_unlock(&lock); + return glsl_program_num; +} + +GLuint ResourcePool::link_compute_program(GLuint cs_obj) +{ + GLuint glsl_program_num = glCreateProgram(); + check_error(); + glAttachShader(glsl_program_num, cs_obj); + check_error(); + glLinkProgram(glsl_program_num); + check_error(); + + GLint success; + glGetProgramiv(glsl_program_num, GL_LINK_STATUS, &success); + if (success == GL_FALSE) { + GLchar error_log[1024] = {0}; + glGetProgramInfoLog(glsl_program_num, 1024, nullptr, error_log); + fprintf(stderr, "Error linking program: %s\n", error_log); + exit(1); + } + + return glsl_program_num; +} + GLuint ResourcePool::use_glsl_program(GLuint glsl_program_num) { pthread_mutex_lock(&lock); @@ -251,12 +284,19 @@ GLuint ResourcePool::use_glsl_program(GLuint glsl_program_num) // will later put it onto the list.) map::iterator shader_it = program_shaders.find(glsl_program_num); - assert(shader_it != program_shaders.end()); - - instance_program_num = link_program( - shader_it->second.vs_obj, - shader_it->second.fs_obj, - shader_it->second.fragment_shader_outputs); + if (shader_it == program_shaders.end()) { + // Should be a compute shader. + map::iterator compute_shader_it = + compute_program_shaders.find(glsl_program_num); + instance_program_num = link_compute_program( + compute_shader_it->second.cs_obj); + } else { + // A regular fragment shader. + instance_program_num = link_program( + shader_it->second.vs_obj, + shader_it->second.fs_obj, + shader_it->second.fragment_shader_outputs); + } program_masters.insert(make_pair(instance_program_num, glsl_program_num)); } pthread_mutex_unlock(&lock); @@ -269,7 +309,7 @@ void ResourcePool::unuse_glsl_program(GLuint instance_program_num) { pthread_mutex_lock(&lock); - map::const_iterator master_it = program_masters.find(instance_program_num); + auto master_it = program_masters.find(instance_program_num); assert(master_it != program_masters.end()); assert(program_instances.count(master_it->second)); @@ -287,7 +327,7 @@ GLuint ResourcePool::create_2d_texture(GLint internal_format, GLsizei width, GLs pthread_mutex_lock(&lock); // See if there's a texture on the freelist we can use. - for (list::iterator freelist_it = texture_freelist.begin(); + for (auto freelist_it = texture_freelist.begin(); freelist_it != texture_freelist.end(); ++freelist_it) { GLuint texture_num = *freelist_it; @@ -298,13 +338,16 @@ GLuint ResourcePool::create_2d_texture(GLint internal_format, GLsizei width, GLs format_it->second.height == height) { texture_freelist_bytes -= estimate_texture_size(format_it->second); texture_freelist.erase(freelist_it); + GLsync sync = format_it->second.no_reuse_before; pthread_mutex_unlock(&lock); + glWaitSync(sync, 0, GL_TIMEOUT_IGNORED); + glDeleteSync(sync); return texture_num; } } // Find any reasonable format given the internal format; OpenGL validates it - // even though we give NULL as pointer. + // even though we give nullptr as pointer. GLenum format; switch (internal_format) { case GL_RGBA32F_ARB: @@ -388,7 +431,7 @@ GLuint ResourcePool::create_2d_texture(GLint internal_format, GLsizei width, GLs check_error(); glBindTexture(GL_TEXTURE_2D, texture_num); check_error(); - glTexImage2D(GL_TEXTURE_2D, 0, internal_format, width, height, 0, format, type, NULL); + glTexImage2D(GL_TEXTURE_2D, 0, internal_format, width, height, 0, format, type, nullptr); check_error(); glBindTexture(GL_TEXTURE_2D, 0); check_error(); @@ -410,12 +453,14 @@ void ResourcePool::release_2d_texture(GLuint texture_num) texture_freelist.push_front(texture_num); assert(texture_formats.count(texture_num) != 0); texture_freelist_bytes += estimate_texture_size(texture_formats[texture_num]); + texture_formats[texture_num].no_reuse_before = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0); while (texture_freelist_bytes > texture_freelist_max_bytes) { GLuint free_texture_num = texture_freelist.back(); texture_freelist.pop_back(); assert(texture_formats.count(free_texture_num) != 0); texture_freelist_bytes -= estimate_texture_size(texture_formats[free_texture_num]); + glDeleteSync(texture_formats[free_texture_num].no_reuse_before); texture_formats.erase(free_texture_num); glDeleteTextures(1, &free_texture_num); check_error(); @@ -424,12 +469,10 @@ void ResourcePool::release_2d_texture(GLuint texture_num) // not be in the right context, so don't delete it right away; // the cleanup in release_fbo() (which calls cleanup_unlinked_fbos()) // will take care of actually doing that later. - for (map, FBO>::iterator format_it = fbo_formats.begin(); - format_it != fbo_formats.end(); - ++format_it) { + for (auto &key_and_fbo : fbo_formats) { for (unsigned i = 0; i < num_fbo_attachments; ++i) { - if (format_it->second.texture_num[i] == free_texture_num) { - format_it->second.texture_num[i] = GL_INVALID_INDEX; + if (key_and_fbo.second.texture_num[i] == free_texture_num) { + key_and_fbo.second.texture_num[i] = GL_INVALID_INDEX; } } } @@ -453,9 +496,8 @@ GLuint ResourcePool::create_fbo(GLuint texture0_num, GLuint texture1_num, GLuint pthread_mutex_lock(&lock); if (fbo_freelist.count(context) != 0) { // See if there's an FBO on the freelist we can use. - list::iterator end = fbo_freelist[context].end(); - for (list::iterator freelist_it = fbo_freelist[context].begin(); - freelist_it != end; ++freelist_it) { + auto end = fbo_freelist[context].end(); + for (auto freelist_it = fbo_freelist[context].begin(); freelist_it != end; ++freelist_it) { FBOFormatIterator fbo_it = *freelist_it; if (fbo_it->second.texture_num[0] == texture0_num && fbo_it->second.texture_num[1] == texture1_num && @@ -536,9 +578,8 @@ GLuint ResourcePool::create_vec2_vao(const set &attribute_indices, GLuint pthread_mutex_lock(&lock); if (vao_freelist.count(context) != 0) { // See if there's a VAO the freelist we can use. - list::iterator end = vao_freelist[context].end(); - for (list::iterator freelist_it = vao_freelist[context].begin(); - freelist_it != end; ++freelist_it) { + auto end = vao_freelist[context].end(); + for (auto freelist_it = vao_freelist[context].begin(); freelist_it != end; ++freelist_it) { VAOFormatIterator vao_it = *freelist_it; if (vao_it->second.vbo_num == vbo_num && vao_it->second.attribute_indices == attribute_indices) { @@ -548,7 +589,6 @@ GLuint ResourcePool::create_vec2_vao(const set &attribute_indices, GLuint } } } - pthread_mutex_unlock(&lock); // Create a new one. VAO vao_format; @@ -562,10 +602,10 @@ GLuint ResourcePool::create_vec2_vao(const set &attribute_indices, GLuint glBindBuffer(GL_ARRAY_BUFFER, vbo_num); check_error(); - for (set::const_iterator attr_it = attribute_indices.begin(); attr_it != attribute_indices.end(); ++attr_it) { - glEnableVertexAttribArray(*attr_it); + for (GLint attr : attribute_indices) { + glEnableVertexAttribArray(attr); check_error(); - glVertexAttribPointer(*attr_it, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0)); + glVertexAttribPointer(attr, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0)); check_error(); } @@ -610,8 +650,8 @@ void ResourcePool::clean_context() void ResourcePool::cleanup_unlinked_fbos(void *context) { - list::iterator end = fbo_freelist[context].end(); - for (list::iterator freelist_it = fbo_freelist[context].begin(); freelist_it != end; ) { + auto end = fbo_freelist[context].end(); + for (auto freelist_it = fbo_freelist[context].begin(); freelist_it != end; ) { FBOFormatIterator fbo_it = *freelist_it; bool all_unlinked = true; @@ -657,6 +697,46 @@ void ResourcePool::shrink_vao_freelist(void *context, size_t max_length) } } +void ResourcePool::increment_program_refcount(GLuint program_num) +{ + map::iterator refcount_it = program_refcount.find(program_num); + if (refcount_it != program_refcount.end()) { + ++refcount_it->second; + } else { + list::iterator freelist_it = + find(program_freelist.begin(), program_freelist.end(), program_num); + assert(freelist_it != program_freelist.end()); + program_freelist.erase(freelist_it); + program_refcount.insert(make_pair(program_num, 1)); + } +} + +void ResourcePool::output_debug_shader(const string &shader_src, const string &suffix) +{ + if (movit_debug_level == MOVIT_DEBUG_ON) { + // Output shader to a temporary file, for easier debugging. + static int compiled_shader_num = 0; + char filename[256]; + sprintf(filename, "chain-%03d.%s", compiled_shader_num++, suffix.c_str()); + FILE *fp = fopen(filename, "w"); + if (fp == nullptr) { + perror(filename); + exit(1); + } + fprintf(fp, "%s\n", shader_src.c_str()); + fclose(fp); + } +} + +void ResourcePool::add_master_program(GLuint program_num) +{ + program_refcount.insert(make_pair(program_num, 1)); + stack instances; + instances.push(program_num); + program_instances.insert(make_pair(program_num, instances)); + program_masters.insert(make_pair(program_num, program_num)); +} + size_t ResourcePool::estimate_texture_size(const Texture2D &texture_format) { size_t bytes_per_pixel;