Beatmup
Beatmup::GL::ProgramBank Class Reference

Stores linked GLSL programs and their associated fragment shader codes. More...

#include <program_bank.h>

Inheritance diagram for Beatmup::GL::ProgramBank:
Beatmup::Object Beatmup::NNets::Model Beatmup::NNets::Classifier Beatmup::NNets::DeserializedModel

Classes

struct  ProgramHolder
 

Public Member Functions

 ProgramBank (Context &context)
 
 ~ProgramBank ()
 
GL::RenderingProgramoperator() (GraphicPipeline &gpu, const std::string &code, bool enableExternalTextures=false)
 Provides a program given a fragment shader source code. More...
 
void release (GraphicPipeline &gpu, GL::RenderingProgram *program)
 Marks a program as unused any more. More...
 
- Public Member Functions inherited from Beatmup::Object
virtual ~Object ()
 

Protected Attributes

Contextcontext
 

Private Member Functions

bool releaseProgram (GL::RenderingProgram *program, std::map< std::string, ProgramHolder > &cache)
 

Private Attributes

std::map< std::string, ProgramHolderprograms
 map of source code to programs without external texture extension More...
 
std::map< std::string, ProgramHolderprogramsWithExtTex
 map of source code to programs with external texture extension More...
 

Detailed Description

Stores linked GLSL programs and their associated fragment shader codes.

Definition at line 31 of file program_bank.h.

Constructor & Destructor Documentation

◆ ProgramBank()

Beatmup::GL::ProgramBank::ProgramBank ( Context context)
inline

Definition at line 46 of file program_bank.h.

46 : context(context) {}

◆ ~ProgramBank()

GL::ProgramBank::~ProgramBank ( )

Definition at line 26 of file program_bank.cpp.

26  {
27  for (auto& it : this->programs)
28  context.getGpuRecycleBin()->put(it.second.program);
29 }
GL::RecycleBin * getGpuRecycleBin() const
Definition: context.cpp:340
std::map< std::string, ProgramHolder > programs
map of source code to programs without external texture extension
Definition: program_bank.h:38
void put(Item *item)
Puts an item into the recycle bin.
Definition: recycle_bin.cpp:73

Member Function Documentation

◆ releaseProgram()

bool GL::ProgramBank::releaseProgram ( GL::RenderingProgram program,
std::map< std::string, ProgramHolder > &  cache 
)
private

Definition at line 52 of file program_bank.cpp.

52  {
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 }

◆ operator()()

GL::RenderingProgram * GL::ProgramBank::operator() ( GraphicPipeline gpu,
const std::string &  code,
bool  enableExternalTextures = false 
)

Provides a program given a fragment shader source code.

Creates a new program or returns an available one increasing its user count (do not call this too often).

Parameters
[in]gpuA graphic pipeline instance
[in]codeThe fragment shader code of the program
[in]enableExternalTexturesIf true, external texture extension is enabled in the program, for example, to access camera image in Android
Returns
Linked program.

Definition at line 32 of file program_bank.cpp.

32  {
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 }
GLSL fragment shader.
Definition: program.h:107
std::map< std::string, ProgramHolder > programsWithExtTex
map of source code to programs with external texture extension
Definition: program_bank.h:39
GLSL program to render images Makes use of default vertex attributes to pass the texture coordinates ...
Definition: program.h:240
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

◆ release()

void GL::ProgramBank::release ( GraphicPipeline gpu,
GL::RenderingProgram program 
)

Marks a program as unused any more.

If the program has no other users, its is destroyed.

Parameters
[in]gpuA graphic pipeline instance
[in]programThe program

Definition at line 68 of file program_bank.cpp.

68  {
69  if (!releaseProgram(program, programs) && !releaseProgram(program, programsWithExtTex))
70  throw RuntimeError("No program found in a program bank");
71 }
bool releaseProgram(GL::RenderingProgram *program, std::map< std::string, ProgramHolder > &cache)

Member Data Documentation

◆ programs

std::map<std::string, ProgramHolder> Beatmup::GL::ProgramBank::programs
private

map of source code to programs without external texture extension

Definition at line 38 of file program_bank.h.

◆ programsWithExtTex

std::map<std::string, ProgramHolder> Beatmup::GL::ProgramBank::programsWithExtTex
private

map of source code to programs with external texture extension

Definition at line 39 of file program_bank.h.

◆ context

Context& Beatmup::GL::ProgramBank::context
protected

Definition at line 44 of file program_bank.h.


The documentation for this class was generated from the following files: