Route cube textures and samplers through renderer GL
This commit is contained in:
207
src/texture.cpp
207
src/texture.cpp
@@ -9,11 +9,6 @@
|
||||
|
||||
namespace {
|
||||
|
||||
[[nodiscard]] GLenum texture_cube_map_target() noexcept
|
||||
{
|
||||
return static_cast<GLenum>(pp::renderer::gl::texture_cube_map_target());
|
||||
}
|
||||
|
||||
void gen_opengl_textures(std::uint32_t count, std::uint32_t* ids) noexcept
|
||||
{
|
||||
glGenTextures(static_cast<GLsizei>(count), reinterpret_cast<GLuint*>(ids));
|
||||
@@ -139,6 +134,61 @@ void read_opengl_pixels(
|
||||
pixels);
|
||||
}
|
||||
|
||||
void gen_opengl_samplers(std::uint32_t count, std::uint32_t* ids) noexcept
|
||||
{
|
||||
#if USE_SAMPLER
|
||||
glGenSamplers(static_cast<GLsizei>(count), reinterpret_cast<GLuint*>(ids));
|
||||
#else
|
||||
for (std::uint32_t i = 0U; i < count; ++i) {
|
||||
ids[i] = 0U;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void set_opengl_sampler_parameter_i(
|
||||
std::uint32_t sampler,
|
||||
std::uint32_t parameter,
|
||||
std::int32_t value) noexcept
|
||||
{
|
||||
#if USE_SAMPLER
|
||||
glSamplerParameteri(
|
||||
static_cast<GLuint>(sampler),
|
||||
static_cast<GLenum>(parameter),
|
||||
static_cast<GLint>(value));
|
||||
#else
|
||||
(void)sampler;
|
||||
(void)parameter;
|
||||
(void)value;
|
||||
#endif
|
||||
}
|
||||
|
||||
void set_opengl_sampler_parameter_fv(
|
||||
std::uint32_t sampler,
|
||||
std::uint32_t parameter,
|
||||
const float* values) noexcept
|
||||
{
|
||||
#if USE_SAMPLER && !defined(__GLES__)
|
||||
glSamplerParameterfv(
|
||||
static_cast<GLuint>(sampler),
|
||||
static_cast<GLenum>(parameter),
|
||||
values);
|
||||
#else
|
||||
(void)sampler;
|
||||
(void)parameter;
|
||||
(void)values;
|
||||
#endif
|
||||
}
|
||||
|
||||
void bind_opengl_sampler(std::uint32_t unit, std::uint32_t sampler) noexcept
|
||||
{
|
||||
#if USE_SAMPLER
|
||||
glBindSampler(static_cast<GLuint>(unit), static_cast<GLuint>(sampler));
|
||||
#else
|
||||
(void)unit;
|
||||
(void)sampler;
|
||||
#endif
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
std::map<uint16_t, Texture2D> TextureManager::m_textures;
|
||||
@@ -176,27 +226,25 @@ bool TextureCube::create(int resolution) noexcept
|
||||
{
|
||||
destroy();
|
||||
m_resolution = resolution;
|
||||
glGenTextures(1, &m_cubetex_id);
|
||||
|
||||
if (!m_cubetex_id)
|
||||
return;
|
||||
|
||||
glBindTexture(texture_cube_map_target(), m_cubetex_id);
|
||||
const auto format = pp::renderer::gl::texture_format_for_channel_count(4U);
|
||||
const auto component_type = static_cast<GLenum>(pp::renderer::gl::unsigned_byte_component_type());
|
||||
for (GLuint i = 0; i < 6; i++)
|
||||
const auto texture = pp::renderer::gl::allocate_opengl_texture_cube(
|
||||
pp::renderer::gl::OpenGlTextureCubeAllocation {
|
||||
.resolution = m_resolution,
|
||||
.internal_format = static_cast<std::int32_t>(format.internal_format),
|
||||
.pixel_format = format.pixel_format,
|
||||
.component_type = pp::renderer::gl::unsigned_byte_component_type(),
|
||||
},
|
||||
pp::renderer::gl::OpenGlTextureCubeAllocationDispatch {
|
||||
.gen_textures = gen_opengl_textures,
|
||||
.bind_texture = bind_opengl_texture,
|
||||
.tex_image_2d = upload_opengl_texture_2d,
|
||||
});
|
||||
if (!texture.ok())
|
||||
{
|
||||
glTexImage2D(
|
||||
static_cast<GLenum>(pp::renderer::gl::cube_map_allocation_face_texture_target(i)),
|
||||
0,
|
||||
static_cast<GLint>(format.internal_format),
|
||||
m_resolution,
|
||||
m_resolution,
|
||||
0,
|
||||
static_cast<GLenum>(format.pixel_format),
|
||||
component_type,
|
||||
nullptr);
|
||||
LOG("TextureCube::create() failed because: %s", texture.status().message);
|
||||
return;
|
||||
}
|
||||
m_cubetex_id = static_cast<GLuint>(texture.value());
|
||||
});
|
||||
return m_cubetex_id != 0;
|
||||
}
|
||||
@@ -207,8 +255,17 @@ void TextureCube::destroy() noexcept
|
||||
{
|
||||
App::I->render_task([f=m_faces, id=m_cubetex_id]
|
||||
{
|
||||
glDeleteTextures(static_cast<GLsizei>(f.size()), f.data());
|
||||
glDeleteTextures(1, &id);
|
||||
std::array<std::uint32_t, 7> texture_ids {};
|
||||
for (std::size_t i = 0U; i < f.size(); ++i)
|
||||
texture_ids[i] = static_cast<std::uint32_t>(f[i]);
|
||||
texture_ids[f.size()] = static_cast<std::uint32_t>(id);
|
||||
const auto status = pp::renderer::gl::delete_opengl_texture_objects(
|
||||
texture_ids,
|
||||
pp::renderer::gl::OpenGlTexture2DDeleteDispatch {
|
||||
.delete_textures = delete_opengl_textures,
|
||||
});
|
||||
if (!status.ok())
|
||||
LOG("TextureCube::destroy() failed because: %s", status.message);
|
||||
});
|
||||
m_cubetex_id = 0;
|
||||
m_faces.fill(0);
|
||||
@@ -219,7 +276,13 @@ void TextureCube::destroy() noexcept
|
||||
void TextureCube::bind() const noexcept
|
||||
{
|
||||
assert(App::I->is_render_thread());
|
||||
glBindTexture(texture_cube_map_target(), m_cubetex_id);
|
||||
const auto status = pp::renderer::gl::bind_opengl_texture_cube(
|
||||
static_cast<std::uint32_t>(m_cubetex_id),
|
||||
pp::renderer::gl::OpenGlTexture2DBindDispatch {
|
||||
.bind_texture = bind_opengl_texture,
|
||||
});
|
||||
if (!status.ok())
|
||||
LOG("TextureCube::bind() failed because: %s", status.message);
|
||||
}
|
||||
|
||||
bool TextureManager::load(const char* path, bool generate_mipmaps)
|
||||
@@ -525,15 +588,22 @@ bool Sampler::create(GLint filter, GLint wrap)
|
||||
bool ret = false;
|
||||
App::I->render_task([this, &ret, filter, wrap]
|
||||
{
|
||||
#if USE_SAMPLER
|
||||
glGenSamplers(1, &id);
|
||||
#endif // USE_SAMPLER
|
||||
if (id == 0)
|
||||
const auto parameters = pp::renderer::gl::sampler_parameters_for_filter_wrap(
|
||||
static_cast<std::uint32_t>(filter),
|
||||
static_cast<std::uint32_t>(wrap));
|
||||
const auto sampler = pp::renderer::gl::create_opengl_sampler(
|
||||
parameters,
|
||||
pp::renderer::gl::OpenGlSamplerCreateDispatch {
|
||||
.gen_samplers = gen_opengl_samplers,
|
||||
.sampler_parameter_i = set_opengl_sampler_parameter_i,
|
||||
});
|
||||
if (!sampler.ok())
|
||||
{
|
||||
ret = false;
|
||||
LOG("Sampler::create() failed because: %s", sampler.status().message);
|
||||
return;
|
||||
}
|
||||
set(filter, wrap);
|
||||
id = static_cast<GLuint>(sampler.value());
|
||||
ret = true;
|
||||
});
|
||||
return ret;
|
||||
@@ -555,14 +625,17 @@ void Sampler::set(GLint filter, GLint wrap)
|
||||
{
|
||||
App::I->render_task([=]
|
||||
{
|
||||
#if USE_SAMPLER
|
||||
for (const auto parameter : pp::renderer::gl::sampler_parameters_for_filter_wrap(
|
||||
static_cast<std::uint32_t>(filter),
|
||||
static_cast<std::uint32_t>(wrap)))
|
||||
{
|
||||
glSamplerParameteri(id, static_cast<GLenum>(parameter.name), static_cast<GLint>(parameter.value));
|
||||
}
|
||||
#endif // USE_SAMPLER
|
||||
const auto parameters = pp::renderer::gl::sampler_parameters_for_filter_wrap(
|
||||
static_cast<std::uint32_t>(filter),
|
||||
static_cast<std::uint32_t>(wrap));
|
||||
const auto status = pp::renderer::gl::set_opengl_sampler_parameters(
|
||||
static_cast<std::uint32_t>(id),
|
||||
parameters,
|
||||
pp::renderer::gl::OpenGlSamplerParameterDispatch {
|
||||
.sampler_parameter_i = set_opengl_sampler_parameter_i,
|
||||
});
|
||||
if (!status.ok())
|
||||
LOG("Sampler::set() failed because: %s", status.message);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -582,41 +655,57 @@ void Sampler::set_filter(GLint filter_min, GLint filter_mag)
|
||||
{
|
||||
App::I->render_task([=]
|
||||
{
|
||||
#if USE_SAMPLER
|
||||
for (const auto parameter : pp::renderer::gl::sampler_filter_parameters(
|
||||
static_cast<std::uint32_t>(filter_min),
|
||||
static_cast<std::uint32_t>(filter_mag)))
|
||||
{
|
||||
glSamplerParameteri(id, static_cast<GLenum>(parameter.name), static_cast<GLint>(parameter.value));
|
||||
}
|
||||
#endif // USE_SAMPLER
|
||||
const auto parameters = pp::renderer::gl::sampler_filter_parameters(
|
||||
static_cast<std::uint32_t>(filter_min),
|
||||
static_cast<std::uint32_t>(filter_mag));
|
||||
const auto status = pp::renderer::gl::set_opengl_sampler_parameters(
|
||||
static_cast<std::uint32_t>(id),
|
||||
parameters,
|
||||
pp::renderer::gl::OpenGlSamplerParameterDispatch {
|
||||
.sampler_parameter_i = set_opengl_sampler_parameter_i,
|
||||
});
|
||||
if (!status.ok())
|
||||
LOG("Sampler::set_filter() failed because: %s", status.message);
|
||||
});
|
||||
}
|
||||
void Sampler::set_border(glm::vec4 rgba)
|
||||
{
|
||||
App::I->render_task([this, rgba]
|
||||
{
|
||||
#if USE_SAMPLER && !defined(__GLES__)
|
||||
glSamplerParameterfv(
|
||||
id,
|
||||
static_cast<GLenum>(pp::renderer::gl::sampler_border_color_parameter_name()),
|
||||
glm::value_ptr(rgba));
|
||||
#endif // USE_SAMPLER
|
||||
const auto status = pp::renderer::gl::set_opengl_sampler_border_color(
|
||||
static_cast<std::uint32_t>(id),
|
||||
pp::renderer::gl::sampler_border_color_parameter_name(),
|
||||
glm::value_ptr(rgba),
|
||||
pp::renderer::gl::OpenGlSamplerBorderDispatch {
|
||||
.sampler_parameter_fv = set_opengl_sampler_parameter_fv,
|
||||
});
|
||||
if (!status.ok())
|
||||
LOG("Sampler::set_border() failed because: %s", status.message);
|
||||
});
|
||||
}
|
||||
void Sampler::bind(int unit) const
|
||||
{
|
||||
assert(App::I->is_render_thread());
|
||||
current_unit = unit;
|
||||
#if USE_SAMPLER
|
||||
glBindSampler(unit, id);
|
||||
#endif // USE_SAMPLER
|
||||
const auto status = pp::renderer::gl::bind_opengl_sampler_object(
|
||||
static_cast<std::uint32_t>(unit),
|
||||
static_cast<std::uint32_t>(id),
|
||||
pp::renderer::gl::OpenGlSamplerBindDispatch {
|
||||
.bind_sampler = bind_opengl_sampler,
|
||||
});
|
||||
if (!status.ok())
|
||||
LOG("Sampler::bind() failed because: %s", status.message);
|
||||
}
|
||||
void Sampler::unbind()
|
||||
{
|
||||
assert(App::I->is_render_thread());
|
||||
#if USE_SAMPLER
|
||||
glBindSampler(current_unit, 0);
|
||||
#endif // USE_SAMPLER
|
||||
const auto status = pp::renderer::gl::bind_opengl_sampler_object(
|
||||
static_cast<std::uint32_t>(current_unit),
|
||||
0U,
|
||||
pp::renderer::gl::OpenGlSamplerBindDispatch {
|
||||
.bind_sampler = bind_opengl_sampler,
|
||||
});
|
||||
if (!status.ok())
|
||||
LOG("Sampler::unbind() failed because: %s", status.message);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user