Beatmup
Beatmup::ShaderApplicator Class Reference

A task applying an image shader to a bitmap. More...

#include <shader_applicator.h>

Inheritance diagram for Beatmup::ShaderApplicator:
Beatmup::GpuTask Beatmup::BitmapContentLock Beatmup::AbstractTask Beatmup::Object

Public Member Functions

 ShaderApplicator ()
 
void addSampler (AbstractBitmap *bitmap, const std::string uniformName=ImageShader::INPUT_IMAGE_ID)
 Connects a bitmap to a shader uniform variable. More...
 
bool removeSampler (const std::string uniformName)
 Removes a sampler with a uniform variable name. More...
 
void clearSamplers ()
 Clears all connections of bitmaps to samplers. More...
 
void setOutputBitmap (AbstractBitmap *bitmap)
 
void setShader (ImageShader *shader)
 
AbstractBitmapgetOutputBitmap () const
 
ImageShadergetShader () const
 
const size_t getSamplersCount () const
 
- Public Member Functions inherited from Beatmup::Object
virtual ~Object ()
 

Private Member Functions

bool processOnGPU (GraphicPipeline &gpu, TaskThread &thread)
 Executes the task on GPU. More...
 
void beforeProcessing (ThreadIndex threadCount, ProcessingTarget target, GraphicPipeline *gpu)
 Instruction called before the task is executed. More...
 
void afterProcessing (ThreadIndex threadCount, GraphicPipeline *gpu, bool aborted)
 Instruction called after the task is executed. More...
 
- Private Member Functions inherited from Beatmup::BitmapContentLock
 BitmapContentLock ()
 
 ~BitmapContentLock ()
 
void readLock (GraphicPipeline *gpu, AbstractBitmap *bitmap, ProcessingTarget target)
 Locks content of a bitmap for reading using a specific processing target device. More...
 
void writeLock (GraphicPipeline *gpu, AbstractBitmap *bitmap, ProcessingTarget target)
 Locks content of a bitmap for writing using a specific processing target device. More...
 
void unlock (AbstractBitmap *bitmap)
 Drops a lock to the bitmap. More...
 
void unlockAll ()
 Unlocks all the locked bitmaps unconditionally. More...
 
template<const ProcessingTarget target>
void lock (GraphicPipeline *gpu, AbstractBitmap *input, AbstractBitmap *output)
 
void lock (GraphicPipeline *gpu, ProcessingTarget target, AbstractBitmap *input, AbstractBitmap *output)
 
template<const ProcessingTarget target>
void lock (GraphicPipeline *gpu, std::initializer_list< AbstractBitmap * > read, std::initializer_list< AbstractBitmap * > write)
 
template<typename ... Args>
void unlock (AbstractBitmap *first, Args ... others)
 

Private Attributes

std::map< std::string, AbstractBitmap * > samplers
 
ImageShadershader
 
AbstractBitmapmainInput
 
AbstractBitmapoutput
 
AffineMapping mapping
 

Additional Inherited Members

- Public Types inherited from Beatmup::AbstractTask
enum class  TaskDeviceRequirement { CPU_ONLY , GPU_OR_CPU , GPU_ONLY }
 Specifies which device (CPU and/or GPU) is used to run the task. More...
 
- Static Public Member Functions inherited from Beatmup::AbstractTask
static ThreadIndex validThreadCount (int number)
 Valid thread count from a given integer value. More...
 

Detailed Description

A task applying an image shader to a bitmap.

Definition at line 31 of file shader_applicator.h.

Constructor & Destructor Documentation

◆ ShaderApplicator()

ShaderApplicator::ShaderApplicator ( )

Definition at line 77 of file shader_applicator.cpp.

77  :
78  shader(nullptr), mainInput(nullptr), output(nullptr)
79 {}

Member Function Documentation

◆ processOnGPU()

bool ShaderApplicator::processOnGPU ( GraphicPipeline gpu,
TaskThread thread 
)
privatevirtual

Executes the task on GPU.

Parameters
gpugraphic pipeline instance
threadassociated task execution context
Returns
true if the execution is finished correctly, false otherwise

Reimplemented from Beatmup::AbstractTask.

Definition at line 25 of file shader_applicator.cpp.

25  {
26  // distribute texture units over samplers
27  {
28  int unit = mainInput ? 1 : 0;
29  for (auto input : samplers)
30  if (input.first != ImageShader::INPUT_IMAGE_ID) {
31  RuntimeError::check(input.second->getTextureFormat() != GL::TextureHandler::TextureFormat::OES_Ext,
32  "OES_EXT samplers are only supported when bound to '" + ImageShader::INPUT_IMAGE_ID + "' sampler variable of type " + ImageShader::INPUT_IMAGE_DECL_TYPE);
33  shader->setInteger(input.first, unit);
34  unit++;
35  }
36  }
37 
38  // prepare shader
40 
41  // bind textures
42  {
43  size_t unit = mainInput ? 1 : 0;
44  for (auto input : samplers)
45  if (input.first != ImageShader::INPUT_IMAGE_ID) {
46  gpu.bind(*input.second, unit, TextureParam::INTERP_LINEAR);
47  unit++;
48  }
49  }
50 
51  // process
52  shader->process(gpu);
53  return true;
54 }
void setInteger(std::string name, int value)
Sets a scalar integer uniform value.
void bind(GL::TextureHandler &texture, size_t texUnit, const TextureParam param)
Definition: pipeline.cpp:881
void process(GraphicPipeline &gpu)
Apply the shader to produce an image.
void prepare(GraphicPipeline &gpu, GL::TextureHandler *input, const TextureParam texParam, AbstractBitmap *output, const AffineMapping &mapping)
Conducts required preparations for blending.
static const std::string INPUT_IMAGE_DECL_TYPE
A virtual input image type defined at shader compile time by ordinary texture or OES texture sampler ...
Definition: image_shader.h:104
static const std::string INPUT_IMAGE_ID
Shader variable name referring to the input image.
Definition: image_shader.h:109
static void check(const bool condition, const std::string &message)
Definition: exception.h:64
std::map< std::string, AbstractBitmap * > samplers
@ INTERP_LINEAR
bilinear pixel interpolation

◆ beforeProcessing()

void ShaderApplicator::beforeProcessing ( ThreadIndex  threadCount,
ProcessingTarget  target,
GraphicPipeline gpu 
)
privatevirtual

Instruction called before the task is executed.

Parameters
threadCountNumber of threads used to perform the task
targetDevice used to perform the task
gpuA graphic pipeline instance; may be null.

Reimplemented from Beatmup::AbstractTask.

Definition at line 57 of file shader_applicator.cpp.

57  {
58  NullTaskInput::check(output, "output bitmap");
59  NullTaskInput::check(shader, "image shader");
60  if (mainInput)
62  for (auto _ : samplers)
63  readLock(gpu, _.second, ProcessingTarget::GPU);
65 }
void readLock(GraphicPipeline *gpu, AbstractBitmap *bitmap, ProcessingTarget target)
Locks content of a bitmap for reading using a specific processing target device.
void writeLock(GraphicPipeline *gpu, AbstractBitmap *bitmap, ProcessingTarget target)
Locks content of a bitmap for writing using a specific processing target device.
static void check(const void *pointer, const char *which)
Definition: exception.h:115

◆ afterProcessing()

void ShaderApplicator::afterProcessing ( ThreadIndex  threadCount,
GraphicPipeline gpu,
bool  aborted 
)
privatevirtual

Instruction called after the task is executed.

Parameters
threadCountNumber of threads used to perform the task
gpuGPU to be used to execute the task; may be null.
abortedtrue if the task was aborted

Reimplemented from Beatmup::AbstractTask.

Definition at line 68 of file shader_applicator.cpp.

68  {
69  if (mainInput)
71  for (auto _ : samplers)
72  unlock(_.second);
73  unlock(output);
74 }
void unlock(AbstractBitmap *bitmap)
Drops a lock to the bitmap.

◆ addSampler()

void ShaderApplicator::addSampler ( AbstractBitmap bitmap,
const std::string  uniformName = ImageShader::INPUT_IMAGE_ID 
)

Connects a bitmap to a shader uniform variable.

The bitmap connected to ImageShader::INPUT_IMAGE_ID is used to resolve the sampler type (ImageShader::INPUT_IMAGE_DECL_TYPE).

Definition at line 82 of file shader_applicator.cpp.

82  {
83  if (uniformName == ImageShader::INPUT_IMAGE_ID)
84  mainInput = bitmap;
85  else
86  samplers[uniformName] = bitmap;
87 }
Beatmup::InternalBitmap * bitmap

◆ removeSampler()

bool ShaderApplicator::removeSampler ( const std::string  uniformName)

Removes a sampler with a uniform variable name.

Parameters
[in]uniformNameThe uniform variable
Returns
true if a sampler associated to the given variable existed and was removed, false otherwise.

Definition at line 90 of file shader_applicator.cpp.

90  {
91  if (uniformName == ImageShader::INPUT_IMAGE_ID) {
92  if (mainInput) {
93  mainInput = nullptr;
94  return true;
95  }
96  else
97  return false;
98  }
99 
100  auto sampler = samplers.find(uniformName);
101  if (sampler == samplers.end()) {
102  return false;
103  }
104  else {
105  samplers.erase(sampler);
106  return true;
107  }
108 }

◆ clearSamplers()

void ShaderApplicator::clearSamplers ( )

Clears all connections of bitmaps to samplers.

Definition at line 111 of file shader_applicator.cpp.

111  {
112  mainInput = nullptr;
113  samplers.clear();
114 }

◆ setOutputBitmap()

void ShaderApplicator::setOutputBitmap ( AbstractBitmap bitmap)

Definition at line 117 of file shader_applicator.cpp.

117  {
118  this->output = bitmap;
119 }

◆ setShader()

void ShaderApplicator::setShader ( ImageShader shader)

Definition at line 122 of file shader_applicator.cpp.

122  {
123  this->shader = shader;
124 }

◆ getOutputBitmap()

AbstractBitmap* Beatmup::ShaderApplicator::getOutputBitmap ( ) const
inline

Definition at line 66 of file shader_applicator.h.

66 { return output; }

◆ getShader()

ImageShader* Beatmup::ShaderApplicator::getShader ( ) const
inline

Definition at line 67 of file shader_applicator.h.

67 { return shader; }

◆ getSamplersCount()

const size_t Beatmup::ShaderApplicator::getSamplersCount ( ) const
inline

Definition at line 68 of file shader_applicator.h.

68 { return samplers.size(); }

Member Data Documentation

◆ samplers

std::map<std::string, AbstractBitmap*> Beatmup::ShaderApplicator::samplers
private

Definition at line 33 of file shader_applicator.h.

◆ shader

ImageShader* Beatmup::ShaderApplicator::shader
private

Definition at line 34 of file shader_applicator.h.

◆ mainInput

AbstractBitmap* Beatmup::ShaderApplicator::mainInput
private

Definition at line 35 of file shader_applicator.h.

◆ output

AbstractBitmap * Beatmup::ShaderApplicator::output
private

Definition at line 35 of file shader_applicator.h.

◆ mapping

AffineMapping Beatmup::ShaderApplicator::mapping
private

Definition at line 36 of file shader_applicator.h.


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