Beatmup
program_bank.cpp
Go to the documentation of this file.
1 /*
2  Beatmup image and signal processing library
3  Copyright (C) 2020, lnstadrum
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #include "program_bank.h"
20 #include "../gpu/recycle_bin.h"
21 #include <vector>
22 
23 using namespace Beatmup;
24 
25 
27  for (auto& it : this->programs)
28  context.getGpuRecycleBin()->put(it.second.program);
29 }
30 
31 
32 GL::RenderingProgram* GL::ProgramBank::operator()(GraphicPipeline& gpu, const std::string& code, bool enableExternalTextures) {
33  auto& cache = enableExternalTextures ? programsWithExtTex : programs;
34 
35  auto search = cache.find(code);
36  if (search != cache.end()) {
37  auto& holder = search->second;
38  holder.userCount++;
39  holder.program->enable(gpu);
40  return holder.program;
41  }
42 
43  // not found; create
45  GL::FragmentShader fragmentShader(gpu, code, Extensions::BEATMUP_DIALECT + ext);
46  GL::RenderingProgram* program = new GL::RenderingProgram(gpu, fragmentShader);
47  cache.emplace(std::make_pair(code, ProgramHolder{ program, 1 }));
48  return program;
49 }
50 
51 
52 bool GL::ProgramBank::releaseProgram(GL::RenderingProgram* program, std::map<std::string, ProgramHolder>& cache) {
53  for (auto it = cache.begin(); it != cache.end(); ++it) {
54  auto& holder = it->second;
55  if (holder.program == program) {
56  holder.userCount--;
57  if (holder.userCount == 0) {
58  delete program;
59  cache.erase(it);
60  }
61  return true;
62  }
63  }
64  return false;
65 }
66 
67 
69  if (!releaseProgram(program, programs) && !releaseProgram(program, programsWithExtTex))
70  throw RuntimeError("No program found in a program bank");
71 }
GL::RecycleBin * getGpuRecycleBin() const
Definition: context.cpp:340
void enable(const GraphicPipeline &gpu)
Definition: program.cpp:250
GLSL fragment shader.
Definition: program.h:107
GL::RenderingProgram * operator()(GraphicPipeline &gpu, const std::string &code, bool enableExternalTextures=false)
Provides a program given a fragment shader source code.
std::map< std::string, ProgramHolder > programs
map of source code to programs without external texture extension
Definition: program_bank.h:38
bool releaseProgram(GL::RenderingProgram *program, std::map< std::string, ProgramHolder > &cache)
void release(GraphicPipeline &gpu, GL::RenderingProgram *program)
Marks a program as unused any more.
void put(Item *item)
Puts an item into the recycle bin.
Definition: recycle_bin.cpp:73
GLSL program to render images Makes use of default vertex attributes to pass the texture coordinates ...
Definition: program.h:240
Internal low-level GPU control API.
Definition: pipeline.h:33
Extensions
Supported OpenGL estensions.
Definition: program.h:63
@ NONE
no extension
Definition: program.h:64
@ BEATMUP_DIALECT
pseudo-extension enabling Beatmup GLSL dialect
Definition: program.h:65
@ EXTERNAL_TEXTURE
GL_OES_EGL_image_external_essl3 if available or GL_OES_EGL_image_external.
Definition: program.h:66