Beatmup
shader_applicator.cpp
Go to the documentation of this file.
1 /*
2  Beatmup image and signal processing library
3  Copyright (C) 2019, 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 "shader_applicator.h"
20 #include "../gpu/pipeline.h"
21 #include "../debug.h"
22 
23 using namespace Beatmup;
24 
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 }
55 
56 
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 }
66 
67 
68 void ShaderApplicator::afterProcessing(ThreadIndex threadCount, GraphicPipeline* gpu, bool aborted) {
69  if (mainInput)
71  for (auto _ : samplers)
72  unlock(_.second);
73  unlock(output);
74 }
75 
76 
78  shader(nullptr), mainInput(nullptr), output(nullptr)
79 {}
80 
81 
82 void ShaderApplicator::addSampler(AbstractBitmap* bitmap, const std::string uniformName) {
83  if (uniformName == ImageShader::INPUT_IMAGE_ID)
84  mainInput = bitmap;
85  else
86  samplers[uniformName] = bitmap;
87 }
88 
89 
90 bool ShaderApplicator::removeSampler(const std::string uniformName) {
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 }
109 
110 
112  mainInput = nullptr;
113  samplers.clear();
114 }
115 
116 
118  this->output = bitmap;
119 }
120 
121 
123  this->shader = shader;
124 }
A very basic class for any image.
void unlock(AbstractBitmap *bitmap)
Drops a lock to the bitmap.
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.
void setInteger(std::string name, int value)
Sets a scalar integer uniform value.
Internal low-level GPU control API.
Definition: pipeline.h:33
void bind(GL::TextureHandler &texture, size_t texUnit, const TextureParam param)
Definition: pipeline.cpp:881
A GLSL program to process images.
Definition: image_shader.h:34
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 void *pointer, const char *which)
Definition: exception.h:115
static void check(const bool condition, const std::string &message)
Definition: exception.h:64
std::map< std::string, AbstractBitmap * > samplers
void clearSamplers()
Clears all connections of bitmaps to samplers.
void setShader(ImageShader *shader)
void addSampler(AbstractBitmap *bitmap, const std::string uniformName=ImageShader::INPUT_IMAGE_ID)
Connects a bitmap to a shader uniform variable.
void beforeProcessing(ThreadIndex threadCount, ProcessingTarget target, GraphicPipeline *gpu)
Instruction called before the task is executed.
bool processOnGPU(GraphicPipeline &gpu, TaskThread &thread)
Executes the task on GPU.
void setOutputBitmap(AbstractBitmap *bitmap)
void afterProcessing(ThreadIndex threadCount, GraphicPipeline *gpu, bool aborted)
Instruction called after the task is executed.
bool removeSampler(const std::string uniformName)
Removes a sampler with a uniform variable name.
Thread executing tasks.
Definition: parallelism.h:154
unsigned char ThreadIndex
number of threads / thread index
Definition: parallelism.h:68
ProcessingTarget
Definition: basic_types.h:55
@ INTERP_LINEAR
bilinear pixel interpolation
Beatmup::InternalBitmap * bitmap