Skip to content

Commit

Permalink
opengl: infer fbo format name in gl_fb_query
Browse files Browse the repository at this point in the history
  • Loading branch information
ruihe774 committed May 27, 2024
1 parent 4aad31a commit 742bb64
Showing 1 changed file with 66 additions and 1 deletion.
67 changes: 66 additions & 1 deletion src/opengl/gpu_tex.c
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,56 @@ pl_tex gl_tex_create(pl_gpu gpu, const struct pl_tex_params *params)
return NULL;
}

struct fmt_mapping {
const char *name;
GLenum format;
GLenum type;
};

// taken from OpenGL 4.6 Spec table 8.27
static const struct fmt_mapping fmt_mappings[] = {
{"rgba32f", GL_RGBA, GL_FLOAT},
{"rgba16f", GL_RGBA, GL_HALF_FLOAT},
{"rg32f", GL_RG, GL_FLOAT},
{"rg16f", GL_RG, GL_HALF_FLOAT},
{"r11f_g11f_b10f", GL_RGB, GL_UNSIGNED_INT_10F_11F_11F_REV},
{"r32f", GL_RED, GL_FLOAT},
{"r16f", GL_RED, GL_HALF_FLOAT},
{"rgba32ui", GL_RGBA_INTEGER, GL_UNSIGNED_INT},
{"rgba16ui", GL_RGBA_INTEGER, GL_UNSIGNED_SHORT},
{"rgb10_a2ui", GL_RGBA_INTEGER, GL_UNSIGNED_INT_2_10_10_10_REV},
{"rgba8ui", GL_RGBA_INTEGER, GL_UNSIGNED_BYTE},
{"rg32ui", GL_RG_INTEGER, GL_UNSIGNED_INT},
{"rg16ui", GL_RG_INTEGER, GL_UNSIGNED_SHORT},
{"rg8ui", GL_RG_INTEGER, GL_UNSIGNED_BYTE},
{"r32ui", GL_RED_INTEGER, GL_UNSIGNED_INT},
{"r16ui", GL_RED_INTEGER, GL_UNSIGNED_SHORT},
{"r8ui", GL_RED_INTEGER, GL_UNSIGNED_BYTE},
{"rgba32i", GL_RGBA_INTEGER, GL_INT},
{"rgba16i", GL_RGBA_INTEGER, GL_SHORT},
{"rgba8i", GL_RGBA_INTEGER, GL_BYTE},
{"rg32i", GL_RG_INTEGER, GL_INT},
{"rg16i", GL_RG_INTEGER, GL_SHORT},
{"rg8i", GL_RG_INTEGER, GL_BYTE},
{"r32i", GL_RED_INTEGER, GL_INT},
{"r16i", GL_RED_INTEGER, GL_SHORT},
{"r8", GL_RED_INTEGER, GL_BYTE},
{"rgba16", GL_RGBA, GL_UNSIGNED_SHORT},
{"rgb10_a2", GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV},
{"rgba8", GL_RGBA, GL_UNSIGNED_BYTE},
{"rg16", GL_RG, GL_UNSIGNED_SHORT},
{"rg8", GL_RG, GL_UNSIGNED_BYTE},
{"r16", GL_RED, GL_UNSIGNED_SHORT},
{"r8", GL_RED, GL_UNSIGNED_BYTE},
{"rgba16_snorm", GL_RGBA, GL_SHORT},
{"rgba8_snorm", GL_RGBA, GL_BYTE},
{"rg16_snorm", GL_RG, GL_SHORT},
{"rg8_snorm", GL_RG, GL_BYTE},
{"r16_snorm", GL_RED, GL_SHORT},
{"r8_snorm", GL_RED, GL_BYTE},
{NULL, 0, 0}
};

static bool gl_fb_query(pl_gpu gpu, int fbo, struct pl_fmt_t *fmt,
struct gl_format *glfmt)
{
Expand Down Expand Up @@ -489,7 +539,7 @@ static bool gl_fb_query(pl_gpu gpu, int fbo, struct pl_fmt_t *fmt,
if (fbo != 0)
obj = GL_COLOR_ATTACHMENT0;

GLint type = 0;
GLint format = 0, type = 0;
gl->GetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, obj,
GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE, &type);
switch (type) {
Expand All @@ -510,6 +560,21 @@ static bool gl_fb_query(pl_gpu gpu, int fbo, struct pl_fmt_t *fmt,
gl->GetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, obj,
GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE, &fmt->component_depth[3]);

if (p->gl_ver >= 45) {
// Try to infer the format name.
// We could use GetTexLevelParameter to query the internal format
// but it requires obtaining the texture using ARB_multi_bind or ARB_direct_state_access.
// So instead we infer it using read format and type.
gl->GetFramebufferParameteriv(GL_DRAW_FRAMEBUFFER, GL_IMPLEMENTATION_COLOR_READ_FORMAT, &format);
gl->GetFramebufferParameteriv(GL_DRAW_FRAMEBUFFER, GL_IMPLEMENTATION_COLOR_READ_TYPE, &type);
for (const struct fmt_mapping *f = fmt_mappings; f->name; f++) {
if (format == f->format && type == f->type) {
fmt->name = f->name;
break;
}
}
}

gl->BindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
gl_check_err(gpu, "gl_fb_query");

Expand Down

0 comments on commit 742bb64

Please sign in to comment.