openMSX
GLScaler.cc
Go to the documentation of this file.
1#include "GLScaler.hh"
2#include "GLContext.hh"
3#include "gl_vec.hh"
4#include "narrow.hh"
5#include "strCat.hh"
6#include "xrange.hh"
7
8using namespace gl;
9
10namespace openmsx {
11
12GLScaler::GLScaler(const std::string& progName)
13{
14 for (auto i : xrange(2)) {
15 auto header = tmpStrCat("#define SUPERIMPOSE ", char('0' + i), '\n');
16 VertexShader vShader(header, progName + ".vert");
17 FragmentShader fShader(header, progName + ".frag");
18 program[i].attach(vShader);
19 program[i].attach(fShader);
20 program[i].bindAttribLocation(0, "a_position");
21 program[i].bindAttribLocation(1, "a_texCoord");
22 program[i].link();
23 program[i].activate();
24 glUniform1i(program[i].getUniformLocation("tex"), 0);
25 if (i == 1) {
26 glUniform1i(program[i].getUniformLocation("videoTex"), 1);
27 }
28 unifTexSize[i] = program[i].getUniformLocation("texSize");
29 glUniformMatrix4fv(program[i].getUniformLocation("u_mvpMatrix"),
30 1, GL_FALSE, &gl::context->pixelMvp[0][0]);
31 }
32}
33
35 unsigned /*srcStartY*/, unsigned /*srcEndY*/,
36 unsigned /*lineWidth*/, FrameSource& /*paintFrame*/)
37{
38}
39
40void GLScaler::setup(bool superImpose)
41{
42 int i = superImpose ? 1 : 0;
43 program[i].activate();
44}
45
47 ColorTexture& src, ColorTexture* superImpose,
48 unsigned srcStartY, unsigned srcEndY, unsigned srcWidth,
49 unsigned dstStartY, unsigned dstEndY, unsigned dstWidth,
50 unsigned logSrcHeight, bool textureFromZero)
51{
52 auto srcStartYF = narrow<float>(srcStartY);
53 auto srcEndYF = narrow<float>(srcEndY);
54 auto dstStartYF = narrow<float>(dstStartY);
55 auto dstEndYF = narrow<float>(dstEndY);
56 auto dstWidthF = narrow<float>(dstWidth);
57 auto srcWidthF = narrow<float>(srcWidth);
58 auto srcHeightF = narrow<float>(src.getHeight());
59 auto logSrcHeightF = narrow<float>(logSrcHeight);
60
61 if (superImpose) {
62 glActiveTexture(GL_TEXTURE1);
63 superImpose->bind();
64 glActiveTexture(GL_TEXTURE0);
65 }
66 int i = superImpose ? 1 : 0;
67 glUniform3f(unifTexSize[i], srcWidthF, srcHeightF, logSrcHeightF);
68
69 src.bind();
70 // Note: hShift is pre-divided by srcWidth, while vShift will be divided
71 // by srcHeight later on.
72 // Note: The coordinate is put just past zero, to avoid fract() in the
73 // fragment shader to wrap around on rounding errors.
74 static constexpr float BIAS = 0.001f;
75 float samplePos = (textureFromZero ? 0.5f : 0.0f) + BIAS;
76 float hShift = samplePos / dstWidthF;
77 float yRatio = (srcEndYF - srcStartYF) / (dstEndYF - dstStartYF);
78 float vShift = samplePos * yRatio;
79
80 // vertex positions
81 std::array pos = {
82 vec2( 0.0f, dstStartYF),
83 vec2(dstWidthF, dstStartYF),
84 vec2(dstWidthF, dstEndYF ),
85 vec2( 0.0f, dstEndYF ),
86 };
87 // texture coordinates, X-coord shared, Y-coord separate for tex0 and tex1
88 float tex0StartY = (srcStartYF + vShift) / srcHeightF;
89 float tex0EndY = (srcEndYF + vShift) / srcHeightF;
90 float tex1StartY = (srcStartYF + vShift) / logSrcHeightF;
91 float tex1EndY = (srcEndYF + vShift) / logSrcHeightF;
92 std::array tex = {
93 vec3(0.0f + hShift, tex0StartY, tex1StartY),
94 vec3(1.0f + hShift, tex0StartY, tex1StartY),
95 vec3(1.0f + hShift, tex0EndY , tex1EndY ),
96 vec3(0.0f + hShift, tex0EndY , tex1EndY ),
97 };
98
99 glBindBuffer(GL_ARRAY_BUFFER, vbo[0].get());
100 glBufferData(GL_ARRAY_BUFFER, sizeof(pos), pos.data(), GL_STREAM_DRAW);
101 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, nullptr);
102
103 glBindBuffer(GL_ARRAY_BUFFER, vbo[1].get());
104 glBufferData(GL_ARRAY_BUFFER, sizeof(tex), tex.data(), GL_STREAM_DRAW);
105 glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
106
107 glEnableVertexAttribArray(0);
108 glEnableVertexAttribArray(1);
109
110 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
111
112 glDisableVertexAttribArray(1);
113 glDisableVertexAttribArray(0);
114 glBindBuffer(GL_ARRAY_BUFFER, 0);
115}
116
117} // namespace openmsx
GLsizei getHeight() const
Definition: GLUtil.hh:111
Wrapper around an OpenGL fragment shader: a program executed on the GPU that computes the colors of p...
Definition: GLUtil.hh:372
void bind()
Makes this texture the active GL texture.
Definition: GLUtil.hh:81
Wrapper around an OpenGL vertex shader: a program executed on the GPU that computes per-vertex stuff.
Definition: GLUtil.hh:357
Interface for getting lines from a video frame.
Definition: FrameSource.hh:20
void setup(bool superImpose)
Definition: GLScaler.cc:40
GLScaler(const std::string &progName)
Definition: GLScaler.cc:12
std::array< gl::ShaderProgram, 2 > program
Definition: GLScaler.hh:80
std::array< GLint, 2 > unifTexSize
Definition: GLScaler.hh:81
std::array< gl::BufferObject, 2 > vbo
Definition: GLScaler.hh:79
void execute(gl::ColorTexture &src, gl::ColorTexture *superImpose, unsigned srcStartY, unsigned srcEndY, unsigned srcWidth, unsigned dstStartY, unsigned dstEndY, unsigned dstWidth, unsigned logSrcHeight, bool textureFromZero=false)
Helper method to draw a rectangle with multiple texture coordinates.
Definition: GLScaler.cc:46
virtual void uploadBlock(unsigned srcStartY, unsigned srcEndY, unsigned lineWidth, FrameSource &paintFrame)
Definition: GLScaler.cc:34
Definition: gl_mat.hh:23
vecN< 3, float > vec3
Definition: gl_vec.hh:150
vecN< 2, float > vec2
Definition: gl_vec.hh:149
std::optional< Context > context
Definition: GLContext.cc:10
This file implemented 3 utility functions:
Definition: Autofire.cc:9
const T & get(const Event &event)
Definition: Event.hh:727
TemporaryString tmpStrCat(Ts &&... ts)
Definition: strCat.hh:610
constexpr auto xrange(T e)
Definition: xrange.hh:133