Beatmup
Beatmup::GL::FragmentShader Class Reference

GLSL fragment shader. More...

#include <program.h>

Inheritance diagram for Beatmup::GL::FragmentShader:
Beatmup::GL::Shader

Public Member Functions

 FragmentShader (const GraphicPipeline &gpu)
 
 FragmentShader (const GraphicPipeline &gpu, const std::string &source, Extensions extensions=Extensions::NONE)
 
void compile (const GraphicPipeline &gpu, const std::string &source, Extensions extensions=Extensions::NONE)
 
- Public Member Functions inherited from Beatmup::GL::Shader
 ~Shader ()
 

Static Public Attributes

static const char * DIALECT_SAMPLER_DECL_TYPE = "beatmupSampler"
 glsl type name to declare a texture in Beatmup dialect More...
 
static const char * DIALECT_TEXTURE_SAMPLING_FUNC = "beatmupTexture"
 glsl function name to sample a texture in Beatmup dialect More...
 

Additional Inherited Members

- Protected Member Functions inherited from Beatmup::GL::Shader
 Shader (const GraphicPipeline &gpu, const uint32_t type)
 
handle_t getHandle () const
 
uint32_t getType () const
 
void compile (const GraphicPipeline &gpu, const char *source)
 

Detailed Description

GLSL fragment shader.

Definition at line 107 of file program.h.

Constructor & Destructor Documentation

◆ FragmentShader() [1/2]

FragmentShader::FragmentShader ( const GraphicPipeline gpu)

Definition at line 118 of file program.cpp.

118  : Shader(gpu, GL_FRAGMENT_SHADER) {
119 }
Shader(const Shader &)=delete
disabling copying constructor

◆ FragmentShader() [2/2]

Beatmup::GL::FragmentShader::FragmentShader ( const GraphicPipeline gpu,
const std::string &  source,
Extensions  extensions = Extensions::NONE 
)
inline

Definition at line 113 of file program.h.

113  : FragmentShader(gpu)
114  {
115  compile(gpu, source.c_str(), extensions);
116  }
FragmentShader(const GraphicPipeline &gpu)
Definition: program.cpp:118
void compile(const GraphicPipeline &gpu, const std::string &source, Extensions extensions=Extensions::NONE)
Definition: program.cpp:121

Member Function Documentation

◆ compile()

void FragmentShader::compile ( const GraphicPipeline gpu,
const std::string &  source,
Extensions  extensions = Extensions::NONE 
)

Definition at line 121 of file program.cpp.

121  {
122  std::string src(source);
123 
124  // process extensions
125  Extensions ext = extensions;
126  if (ext & Extensions::BEATMUP_DIALECT) {
128 
129  // add version and dialect mapping preprocessor directives
130  bool mapToModernGlsl = false;
131  if (gpu.isGlEsCompliant())
132  if (gpu.getGlslVersion() == 100)
133  // ES 2.0 case
134  src = "#version 100\n";
135  else {
136  // ES 3.0+
137  src = "#version 300 es\n";
138  mapToModernGlsl = true;
139  }
140  else
141  if (gpu.getGlslVersion() < 130)
142  // GLSL < 1.20 not supported; falling back to ES 2.0
143  src = "#version 100\n";
144  else {
145  // 1.30+: capping at 1.30
146  src = "#version 130\n";
147  mapToModernGlsl = true;
148  }
149 
150  // declare sampler type, add external texture extension header if needed
151  if (ext & Extensions::EXTERNAL_TEXTURE) {
153  src += "#ifdef GL_OES_EGL_image_external_essl3\n"
154  "#extension GL_OES_EGL_image_external_essl3 : require\n" // use GL_OES_EGL_image_external_essl3 is available
155  "#define " + std::string(DIALECT_TEXTURE_SAMPLING_FUNC) + "(S, C) texture(S, C)\n" // sampling function is texture()
156  "#else\n"
157  "#extension GL_OES_EGL_image_external : require\n" // fall back to GL_OES_EGL_image_external
158  "lowp vec4 " + std::string(DIALECT_TEXTURE_SAMPLING_FUNC) + "(samplerExternalOES sampler, mediump vec2 coord) { return texture2D(sampler, coord); }\n"
159  "#endif\n"
160  "#define " + std::string(DIALECT_SAMPLER_DECL_TYPE) + " samplerExternalOES\n";
161  }
162  else
163  src += "#define " + std::string(DIALECT_SAMPLER_DECL_TYPE) + " sampler2D\n"
164  "#define " + std::string(DIALECT_TEXTURE_SAMPLING_FUNC) + "(S, C) texture2D(S, C)\n";
165 
166  // add the rest after declaring extensions
167  if (mapToModernGlsl)
168  src += "#define varying in\n"
169  "#define texture2D(S, C) texture(S, C)\n"
170  "#define gl_FragColor beatmupFrgClrVar\n"
171  "out mediump vec4 beatmupFrgClrVar;\n";
172 
173  // append the source code
174  src += "#line 0\n" + source;
175  }
176 
177  // check for external texture extension (not supported without dialect)
179  throw GL::GLException("External texture extension is only supported with Beatmup dialect extension");
180 
181  // check if all extensions are processed
182  if (ext)
183  throw GL::GLException("Cannot interpret extensions set " + std::to_string(extensions));
184 
185  // compile
186  Shader::compile(gpu, src.c_str());
187 }
static const char * DIALECT_SAMPLER_DECL_TYPE
glsl type name to declare a texture in Beatmup dialect
Definition: program.h:109
static const char * DIALECT_TEXTURE_SAMPLING_FUNC
glsl function name to sample a texture in Beatmup dialect
Definition: program.h:110
GPU exception.
Definition: bgl.h:56
void compile(const GraphicPipeline &gpu, const char *source)
Definition: program.cpp:50
int getGlslVersion() const
Returns GLSL language version supported by the GPU context being used.
Definition: pipeline.cpp:956
bool isGlEsCompliant() const
Returns true if the GPU context is OpenGL ES-compliant.
Definition: pipeline.cpp:961
Extensions
Supported OpenGL estensions.
Definition: program.h:63
@ 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
std::string to_string(Beatmup::NNets::ActivationFunction function)
return(jlong) new Beatmup jlong jstring src

Member Data Documentation

◆ DIALECT_SAMPLER_DECL_TYPE

const char * FragmentShader::DIALECT_SAMPLER_DECL_TYPE = "beatmupSampler"
static

glsl type name to declare a texture in Beatmup dialect

Definition at line 109 of file program.h.

◆ DIALECT_TEXTURE_SAMPLING_FUNC

const char * FragmentShader::DIALECT_TEXTURE_SAMPLING_FUNC = "beatmupTexture"
static

glsl function name to sample a texture in Beatmup dialect

Definition at line 110 of file program.h.


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