Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch OpenGL code to use a Core 4.5 profile #966

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Expand Up @@ -9,3 +9,6 @@
build
CMakeFiles
casparcg_server

src/cmake-build-*
src/.idea
49 changes: 31 additions & 18 deletions src/accelerator/ogl/image/image_kernel.cpp
Expand Up @@ -310,10 +310,10 @@ struct image_kernel::impl
double h = static_cast<double>(params.background->height());

GL(glEnable(GL_SCISSOR_TEST));
glScissor(static_cast<int>(m_p[0] * w),
GL(glScissor(static_cast<int>(m_p[0] * w),
static_cast<int>(m_p[1] * h),
std::max(0, static_cast<int>(m_s[0] * w)),
std::max(0, static_cast<int>(m_s[1] * h)));
std::max(0, static_cast<int>(m_s[1] * h))));
}

// Set render target
Expand Down Expand Up @@ -364,17 +364,18 @@ struct image_kernel::impl

// Draw
switch (params.geometry.type()) {
case core::frame_geometry::geometry_type::quad:
case core::frame_geometry::geometry_type::quad_list: {
glClientActiveTexture(GL_TEXTURE0);
case core::frame_geometry::geometry_type::triangles: {

glDisableClientState(GL_EDGE_FLAG_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_INDEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
GLuint vao;
GLuint vbo;

glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
GL(glGenVertexArrays(1, &vao));
GL(glGenBuffers(1, &vbo));

GL(glBindVertexArray(vao));
GL(glBindBuffer(GL_ARRAY_BUFFER, vbo));

GL(glBufferData(GL_ARRAY_BUFFER, static_cast<GLsizei>(sizeof(core::frame_geometry::coord)) * coords.size(), coords.data(), GL_STATIC_DRAW));

auto stride = static_cast<GLsizei>(sizeof(core::frame_geometry::coord));
auto vertex_coord_member = &core::frame_geometry::coord::vertex_x;
Expand All @@ -383,16 +384,28 @@ struct image_kernel::impl
auto vertex_coord_ptr = &(data_ptr->*vertex_coord_member);
auto texture_coord_ptr = &(data_ptr->*texture_coord_member);

glVertexPointer(2, GL_DOUBLE, stride, vertex_coord_ptr);
glTexCoordPointer(4, GL_DOUBLE, stride, texture_coord_ptr);
auto vtx_loc = shader_->get_attrib_location("Position");
auto tex_loc = shader_->get_attrib_location("TexCoordIn");

GL(glEnableVertexAttribArray(vtx_loc));
GL(glEnableVertexAttribArray(tex_loc));

for (int i = 0; i < coords.size(); i += 4) {
glDrawArrays(GL_QUADS, i, 4);
glTextureBarrier();
GL(glVertexAttribPointer(vtx_loc, 2, GL_DOUBLE, GL_FALSE, stride, nullptr));
GL(glVertexAttribPointer(tex_loc, 4, GL_DOUBLE, GL_FALSE, stride, nullptr));

for (int i = 0; i < coords.size(); i += 3) {
GL(glDrawArrays(GL_TRIANGLES, i, 3));
GL(glTextureBarrier());
}

glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
GL(glDisableVertexAttribArray(vtx_loc));
GL(glDisableVertexAttribArray(tex_loc));

GL(glBindVertexArray(0));
GL(glBindBuffer(GL_ARRAY_BUFFER, 0));

GL(glDeleteVertexArrays(1, &vao));
GL(glDeleteBuffers(1, &vbo));
break;
}
default:
Expand Down
62 changes: 38 additions & 24 deletions src/accelerator/ogl/image/image_shader.cpp
Expand Up @@ -87,7 +87,7 @@ std::string get_blend_color_func()

vec4 blend(vec4 fore)
{
vec4 back = texture2D(background, gl_TexCoord[1].st).bgra;
vec4 back = texture(background, TexCoord2.st).bgra;
if(blend_mode != 0)
fore.rgb = get_blend_color(back.rgb/(back.a+0.0000001), fore.rgb/(fore.a+0.0000001))*fore.a;
switch(keyer)
Expand Down Expand Up @@ -118,12 +118,22 @@ std::string get_chroma_func()
std::string get_vertex()
{
return R"shader(

#version 450
in vec4 TexCoordIn;
in vec2 Position;
uniform mat4 projTrans;

out vec4 TexCoord;
out vec4 TexCoord2;

void main()
{
gl_TexCoord[0] = gl_MultiTexCoord0;
// gl_TexCoord[1] = gl_MultiTexCoord1;
vec4 pos = ftransform();
gl_TexCoord[1] = vec4(pos.xy, 0.0, 0.0);
TexCoord = TexCoordIn;
// vec4 pos = ftransform();
//vec4 pos = projTrans * vec4(Position, 0, 1);
vec4 pos = vec4(Position, 0, 1);
TexCoord2 = vec4(pos.xy, 0.0, 0.0);
pos.x = pos.x*2.0 - 1.0;
pos.y = pos.y*2.0 - 1.0;
gl_Position = pos;
Expand All @@ -135,7 +145,11 @@ std::string get_fragment()
{
return R"shader(

#version 130
#version 450
in vec4 TexCoord;
in vec4 TexCoord2;
out vec4 fragColor;

uniform sampler2D background;
uniform sampler2D plane[4];
uniform sampler2D local_key;
Expand Down Expand Up @@ -261,39 +275,39 @@ std::string get_fragment()
switch(pixel_format)
{
case 0: //gray
return vec4(get_sample(plane[0], gl_TexCoord[0].st / gl_TexCoord[0].q).rrr, 1.0);
return vec4(get_sample(plane[0], TexCoord.st).rrr, 1.0);
case 1: //bgra,
return get_sample(plane[0], gl_TexCoord[0].st / gl_TexCoord[0].q).bgra;
return get_sample(plane[0], TexCoord.st).bgra;
case 2: //rgba,
return get_sample(plane[0], gl_TexCoord[0].st / gl_TexCoord[0].q).rgba;
return get_sample(plane[0], TexCoord.st).rgba;
case 3: //argb,
return get_sample(plane[0], gl_TexCoord[0].st / gl_TexCoord[0].q).argb;
return get_sample(plane[0], TexCoord.st).argb;
case 4: //abgr,
return get_sample(plane[0], gl_TexCoord[0].st / gl_TexCoord[0].q).gbar;
return get_sample(plane[0], TexCoord.st).gbar;
case 5: //ycbcr,
{
float y = get_sample(plane[0], gl_TexCoord[0].st / gl_TexCoord[0].q).r;
float cb = get_sample(plane[1], gl_TexCoord[0].st / gl_TexCoord[0].q).r;
float cr = get_sample(plane[2], gl_TexCoord[0].st / gl_TexCoord[0].q).r;
float y = get_sample(plane[0], TexCoord.st).r;
float cb = get_sample(plane[1], TexCoord.st).r;
float cr = get_sample(plane[2], TexCoord.st).r;
return ycbcra_to_rgba(y, cb, cr, 1.0);
}
case 6: //ycbcra
{
float y = get_sample(plane[0], gl_TexCoord[0].st / gl_TexCoord[0].q).r;
float cb = get_sample(plane[1], gl_TexCoord[0].st / gl_TexCoord[0].q).r;
float cr = get_sample(plane[2], gl_TexCoord[0].st / gl_TexCoord[0].q).r;
float a = get_sample(plane[3], gl_TexCoord[0].st / gl_TexCoord[0].q).r;
float y = get_sample(plane[0], TexCoord.st).r;
float cb = get_sample(plane[1], TexCoord.st).r;
float cr = get_sample(plane[2], TexCoord.st).r;
float a = get_sample(plane[3], TexCoord.st).r;
return ycbcra_to_rgba(y, cb, cr, a);
}
case 7: //luma
{
vec3 y3 = get_sample(plane[0], gl_TexCoord[0].st / gl_TexCoord[0].q).rrr;
vec3 y3 = get_sample(plane[0], TexCoord.st).rrr;
return vec4((y3-0.065)/0.859, 1.0);
}
case 8: //bgr,
return vec4(get_sample(plane[0], gl_TexCoord[0].st / gl_TexCoord[0].q).bgr, 1.0);
return vec4(get_sample(plane[0], TexCoord.st).bgr, 1.0);
case 9: //rgb,
return vec4(get_sample(plane[0], gl_TexCoord[0].st / gl_TexCoord[0].q).rgb, 1.0);
return vec4(get_sample(plane[0], TexCoord.st).rgb, 1.0);
}
return vec4(0.0, 0.0, 0.0, 0.0);
}
Expand All @@ -308,13 +322,13 @@ std::string get_fragment()
if(csb)
color.rgb = ContrastSaturationBrightness(color, brt, sat, con);
if(has_local_key)
color *= texture2D(local_key, gl_TexCoord[1].st).r;
color *= texture(local_key, TexCoord2.st).r;
if(has_layer_key)
color *= texture2D(layer_key, gl_TexCoord[1].st).r;
color *= texture(layer_key, TexCoord2.st).r;
color *= opacity;
if (blend_mode >= 0)
color = blend(color);
gl_FragColor = color.bgra;
fragColor = color.bgra;
}
)shader";
}
Expand Down
13 changes: 7 additions & 6 deletions src/accelerator/ogl/util/device.cpp
Expand Up @@ -74,6 +74,7 @@ struct device::impl : public std::enable_shared_from_this<impl>

impl()
: work_(make_work_guard(service_))
, device_(sf::ContextSettings(0, 0, 0, 4, 5, sf::ContextSettings::Attribute::Core), 1, 1)
{
CASPAR_LOG(info) << L"Initializing OpenGL Device.";

Expand All @@ -83,17 +84,17 @@ struct device::impl : public std::enable_shared_from_this<impl>
CASPAR_THROW_EXCEPTION(gl::ogl_exception() << msg_info("Failed to initialize GLEW."));
}

if (!GLEW_VERSION_4_5) {
version_ = u16(reinterpret_cast<const char*>(GL2(glGetString(GL_VERSION)))) + L" " +
u16(reinterpret_cast<const char*>(GL2(glGetString(GL_VENDOR))));

CASPAR_LOG(info) << L"Initialized OpenGL " << version();

if (!GLEW_VERSION_4_5 && !glewIsSupported("GL_ARB_sync GL_ARB_shader_objects GL_ARB_multitexture GL_ARB_direct_state_access GL_ARB_texture_barrier")) {
CASPAR_THROW_EXCEPTION(not_supported()
<< msg_info("Your graphics card does not meet the minimum hardware requirements "
"since it does not support OpenGL 4.5 or higher."));
}

version_ = u16(reinterpret_cast<const char*>(GL2(glGetString(GL_VERSION)))) + L" " +
u16(reinterpret_cast<const char*>(GL2(glGetString(GL_VENDOR))));

CASPAR_LOG(info) << L"Successfully initialized OpenGL " << version();

GL(glCreateFramebuffers(1, &fbo_));
GL(glBindFramebuffer(GL_FRAMEBUFFER, fbo_));

Expand Down
28 changes: 19 additions & 9 deletions src/accelerator/ogl/util/shader.cpp
Expand Up @@ -31,7 +31,8 @@ namespace caspar { namespace accelerator { namespace ogl {
struct shader::impl : boost::noncopyable
{
GLuint program_;
std::unordered_map<std::string, GLint> locations_;
std::unordered_map<std::string, GLint> uniform_locations_;
std::unordered_map<std::string, GLint> attrib_locations_;

public:
impl(const std::string& vertex_source_str, const std::string& fragment_source_str)
Expand Down Expand Up @@ -97,28 +98,36 @@ struct shader::impl : boost::noncopyable

~impl() { glDeleteProgram(program_); }

GLint get_location(const char* name)
GLint get_uniform_location(const char* name)
{
auto it = locations_.find(name);
if (it == locations_.end())
it = locations_.insert(std::make_pair(name, glGetUniformLocation(program_, name))).first;
auto it = uniform_locations_.find(name);
if (it == uniform_locations_.end())
it = uniform_locations_.insert(std::make_pair(name, glGetUniformLocation(program_, name))).first;
return it->second;
}

GLint get_attrib_location(const char* name)
{
auto it = attrib_locations_.find(name);
if (it == attrib_locations_.end())
it = attrib_locations_.insert(std::make_pair(name, glGetAttribLocation(program_, name))).first;
return it->second;
}

void set(const std::string& name, bool value) { set(name, value ? 1 : 0); }

void set(const std::string& name, int value) { GL(glUniform1i(get_location(name.c_str()), value)); }
void set(const std::string& name, int value) { GL(glUniform1i(get_uniform_location(name.c_str()), value)); }

void set(const std::string& name, float value) { GL(glUniform1f(get_location(name.c_str()), value)); }
void set(const std::string& name, float value) { GL(glUniform1f(get_uniform_location(name.c_str()), value)); }

void set(const std::string& name, double value0, double value1)
{
GL(glUniform2f(get_location(name.c_str()), static_cast<float>(value0), static_cast<float>(value1)));
GL(glUniform2f(get_uniform_location(name.c_str()), static_cast<float>(value0), static_cast<float>(value1)));
}

void set(const std::string& name, double value)
{
GL(glUniform1f(get_location(name.c_str()), static_cast<float>(value)));
GL(glUniform1f(get_uniform_location(name.c_str()), static_cast<float>(value)));
}

void use() { GL(glUseProgramObjectARB(program_)); }
Expand All @@ -134,6 +143,7 @@ void shader::set(const std::string& name, int value) { impl_->set(name, value);
void shader::set(const std::string& name, float value) { impl_->set(name, value); }
void shader::set(const std::string& name, double value0, double value1) { impl_->set(name, value0, value1); }
void shader::set(const std::string& name, double value) { impl_->set(name, value); }
GLint shader::get_attrib_location(const char* name) { return impl_->get_attrib_location(name); }
int shader::id() const { return impl_->program_; }
void shader::use() const { impl_->use(); }

Expand Down
2 changes: 2 additions & 0 deletions src/accelerator/ogl/util/shader.h
Expand Up @@ -42,6 +42,8 @@ class shader final
void set(const std::string& name, double value0, double value1);
void set(const std::string& name, double value);

GLint get_attrib_location(const char* name);

template <typename E>
typename std::enable_if<std::is_enum<E>::value, void>::type set(const std::string& name, E value)
{
Expand Down
13 changes: 6 additions & 7 deletions src/core/frame/geometry.cpp
Expand Up @@ -43,13 +43,10 @@ struct frame_geometry::impl
impl(frame_geometry::geometry_type type, std::vector<coord> data)
: type_(type)
{
if (type == geometry_type::quad && data.size() != 4)
CASPAR_THROW_EXCEPTION(invalid_argument() << msg_info("The number of coordinates needs to be 4"));

if (type == geometry_type::quad_list) {
if (data.size() % 4 != 0)
if (type == geometry_type::triangles) {
if (data.size() % 3 != 0)
CASPAR_THROW_EXCEPTION(invalid_argument()
<< msg_info("The number of coordinates needs to be a multiple of 4"));
<< msg_info("The number of coordinates needs to be a multiple of 3"));
}

data_ = std::move(data);
Expand All @@ -74,9 +71,11 @@ const frame_geometry& frame_geometry::get_default()
{0.0, 0.0, 0.0, 0.0}, // upper left
{1.0, 0.0, 1.0, 0.0}, // upper right
{1.0, 1.0, 1.0, 1.0}, // lower right
{0.0, 0.0, 0.0, 0.0}, // upper left
{1.0, 1.0, 1.0, 1.0}, // lower right
{0.0, 1.0, 0.0, 1.0} // lower left
};
static const frame_geometry g(frame_geometry::geometry_type::quad, data);
static const frame_geometry g(frame_geometry::geometry_type::triangles, data);

return g;
}
Expand Down
3 changes: 1 addition & 2 deletions src/core/frame/geometry.h
Expand Up @@ -32,8 +32,7 @@ class frame_geometry
public:
enum class geometry_type
{
quad,
quad_list
triangles
};

struct coord
Expand Down