LibGL: Implement Texture State Management
Some very primitive Texture State management. Data can now be uploaded to textures.
This commit is contained in:
parent
21dff6d40b
commit
4f324ba4d7
Notes:
sideshowbarker
2024-07-18 17:22:13 +09:00
Author: https://github.com/Quaker762 Commit: https://github.com/SerenityOS/serenity/commit/4f324ba4d72 Pull-request: https://github.com/SerenityOS/serenity/pull/7024 Reviewed-by: https://github.com/alimpfard Reviewed-by: https://github.com/ccapitalK Reviewed-by: https://github.com/predmond Reviewed-by: https://github.com/sunverwerth
8 changed files with 309 additions and 0 deletions
|
@ -1,5 +1,6 @@
|
||||||
set(SOURCES
|
set(SOURCES
|
||||||
Tex/NameAllocator.cpp
|
Tex/NameAllocator.cpp
|
||||||
|
Tex/Texture.cpp
|
||||||
Clipper.cpp
|
Clipper.cpp
|
||||||
GLBlend.cpp
|
GLBlend.cpp
|
||||||
GLColor.cpp
|
GLColor.cpp
|
||||||
|
|
|
@ -148,6 +148,24 @@ extern "C" {
|
||||||
#define GL_CONSTANT_ALPHA 0x8003
|
#define GL_CONSTANT_ALPHA 0x8003
|
||||||
#define GL_ONE_MINUS_CONSTANT_ALPHA 0x8004
|
#define GL_ONE_MINUS_CONSTANT_ALPHA 0x8004
|
||||||
|
|
||||||
|
// Pixel formats
|
||||||
|
#define GL_RGB 0x1907
|
||||||
|
#define GL_RGBA 0x1908
|
||||||
|
#define GL_BGR 0x190B
|
||||||
|
#define GL_BGRA 0x190C
|
||||||
|
|
||||||
|
// Source pixel data format
|
||||||
|
#define GL_UNSIGNED_BYTE 0x1401
|
||||||
|
|
||||||
|
// Texture targets
|
||||||
|
#define GL_TEXTURE_2D 0x0DE1
|
||||||
|
|
||||||
|
// Texture Environment and Parameters
|
||||||
|
#define GL_NEAREST 0x2600
|
||||||
|
#define GL_LINEAR 0x2601
|
||||||
|
#define GL_NEAREST_MIPMAP_LINEAR 0x2602
|
||||||
|
#define GL_REPEAT 0x2603
|
||||||
|
|
||||||
//
|
//
|
||||||
// OpenGL typedefs
|
// OpenGL typedefs
|
||||||
//
|
//
|
||||||
|
@ -237,6 +255,7 @@ GLAPI void glAlphaFunc(GLenum func, GLclampf ref);
|
||||||
GLAPI void glHint(GLenum target, GLenum mode);
|
GLAPI void glHint(GLenum target, GLenum mode);
|
||||||
GLAPI void glReadBuffer(GLenum mode);
|
GLAPI void glReadBuffer(GLenum mode);
|
||||||
GLAPI void glReadPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, GLvoid* pixels);
|
GLAPI void glReadPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, GLvoid* pixels);
|
||||||
|
GLAPI void glTexImage2D(GLenum target, GLint level, GLint internalFormat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const GLvoid* data);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
|
|
@ -57,6 +57,7 @@ public:
|
||||||
virtual void gl_hint(GLenum target, GLenum mode) = 0;
|
virtual void gl_hint(GLenum target, GLenum mode) = 0;
|
||||||
virtual void gl_read_buffer(GLenum mode) = 0;
|
virtual void gl_read_buffer(GLenum mode) = 0;
|
||||||
virtual void gl_read_pixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, GLvoid* pixels) = 0;
|
virtual void gl_read_pixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, GLvoid* pixels) = 0;
|
||||||
|
virtual void gl_tex_image_2d(GLenum target, GLint level, GLint internal_format, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const GLvoid* data) = 0;
|
||||||
|
|
||||||
virtual void present() = 0;
|
virtual void present() = 0;
|
||||||
};
|
};
|
||||||
|
|
|
@ -18,3 +18,8 @@ void glDeleteTextures(GLsizei n, const GLuint* textures)
|
||||||
{
|
{
|
||||||
g_gl_context->gl_delete_textures(n, textures);
|
g_gl_context->gl_delete_textures(n, textures);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void glTexImage2D(GLenum target, GLint level, GLint internalFormat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const GLvoid* data)
|
||||||
|
{
|
||||||
|
g_gl_context->gl_tex_image_2d(target, level, internalFormat, width, height, border, format, type, data);
|
||||||
|
}
|
||||||
|
|
|
@ -755,6 +755,13 @@ void SoftwareGLContext::gl_gen_textures(GLsizei n, GLuint* textures)
|
||||||
}
|
}
|
||||||
|
|
||||||
m_name_allocator.allocate(n, textures);
|
m_name_allocator.allocate(n, textures);
|
||||||
|
|
||||||
|
// Let's allocate a new texture for each texture name
|
||||||
|
for (auto i = 0; i < n; i++) {
|
||||||
|
GLuint name = textures[i];
|
||||||
|
|
||||||
|
m_allocated_textures.set(name, adopt_ref(*new Texture()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void SoftwareGLContext::gl_delete_textures(GLsizei n, const GLuint* textures)
|
void SoftwareGLContext::gl_delete_textures(GLsizei n, const GLuint* textures)
|
||||||
|
@ -770,6 +777,62 @@ void SoftwareGLContext::gl_delete_textures(GLsizei n, const GLuint* textures)
|
||||||
}
|
}
|
||||||
|
|
||||||
m_name_allocator.free(n, textures);
|
m_name_allocator.free(n, textures);
|
||||||
|
|
||||||
|
// Let's allocate a new texture for each texture name
|
||||||
|
for (auto i = 0; i < n; i++) {
|
||||||
|
GLuint name = textures[i];
|
||||||
|
|
||||||
|
m_allocated_textures.remove(name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void SoftwareGLContext::gl_tex_image_2d(GLenum target, GLint level, GLint internal_format, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const GLvoid* data)
|
||||||
|
{
|
||||||
|
if (m_in_draw_state) {
|
||||||
|
m_error = GL_INVALID_OPERATION;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// We only support GL_TEXTURE_2D for now
|
||||||
|
if (target != GL_TEXTURE_2D) {
|
||||||
|
m_error = GL_INVALID_ENUM;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// We only support symbolic constants for now
|
||||||
|
if (!(internal_format == GL_RGB || internal_format == GL_RGBA)) {
|
||||||
|
m_error = GL_INVALID_VALUE;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (type != GL_UNSIGNED_BYTE) {
|
||||||
|
m_error = GL_INVALID_VALUE;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (level < 0 || level > Texture::LOG2_MAX_TEXTURE_SIZE) {
|
||||||
|
m_error = GL_INVALID_VALUE;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (width < 0 || height < 0 || width > (2 + Texture::MAX_TEXTURE_SIZE) || height > (2 + Texture::MAX_TEXTURE_SIZE)) {
|
||||||
|
m_error = GL_INVALID_VALUE;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((width & 2) != 0 || (height & 2) != 0) {
|
||||||
|
m_error = GL_INVALID_VALUE;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (border < 0 || border > 1) {
|
||||||
|
m_error = GL_INVALID_VALUE;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Load texture from the currently active texture unit
|
||||||
|
// This is to test the functionality of texture data upload
|
||||||
|
m_allocated_textures.find(1)->value->upload_texture_data(target, level, internal_format, width, height, border, format, type, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
void SoftwareGLContext::gl_front_face(GLenum face)
|
void SoftwareGLContext::gl_front_face(GLenum face)
|
||||||
|
|
|
@ -10,6 +10,9 @@
|
||||||
#include "GLContext.h"
|
#include "GLContext.h"
|
||||||
#include "GLStruct.h"
|
#include "GLStruct.h"
|
||||||
#include "SoftwareRasterizer.h"
|
#include "SoftwareRasterizer.h"
|
||||||
|
#include "Tex/NameAllocator.h"
|
||||||
|
#include "Tex/Texture.h"
|
||||||
|
#include <AK/HashMap.h>
|
||||||
#include <AK/RefPtr.h>
|
#include <AK/RefPtr.h>
|
||||||
#include <AK/Tuple.h>
|
#include <AK/Tuple.h>
|
||||||
#include <AK/Variant.h>
|
#include <AK/Variant.h>
|
||||||
|
@ -63,6 +66,7 @@ public:
|
||||||
virtual void gl_hint(GLenum target, GLenum mode) override;
|
virtual void gl_hint(GLenum target, GLenum mode) override;
|
||||||
virtual void gl_read_buffer(GLenum mode) override;
|
virtual void gl_read_buffer(GLenum mode) override;
|
||||||
virtual void gl_read_pixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, GLvoid* pixels) override;
|
virtual void gl_read_pixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, GLvoid* pixels) override;
|
||||||
|
virtual void gl_tex_image_2d(GLenum target, GLint level, GLint internal_format, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const GLvoid* data) override;
|
||||||
|
|
||||||
virtual void present() override;
|
virtual void present() override;
|
||||||
|
|
||||||
|
@ -126,7 +130,10 @@ private:
|
||||||
NonnullRefPtr<Gfx::Bitmap> m_frontbuffer;
|
NonnullRefPtr<Gfx::Bitmap> m_frontbuffer;
|
||||||
|
|
||||||
Clipper m_clipper;
|
Clipper m_clipper;
|
||||||
|
|
||||||
|
// Texture objects
|
||||||
TextureNameAllocator m_name_allocator;
|
TextureNameAllocator m_name_allocator;
|
||||||
|
HashMap<GLuint, RefPtr<Texture>> m_allocated_textures;
|
||||||
|
|
||||||
SoftwareRasterizer m_rasterizer;
|
SoftwareRasterizer m_rasterizer;
|
||||||
|
|
||||||
|
|
135
Userland/Libraries/LibGL/Tex/Texture.cpp
Normal file
135
Userland/Libraries/LibGL/Tex/Texture.cpp
Normal file
|
@ -0,0 +1,135 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2021, Jesse Buhagiar <jooster669@gmail.com>
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <AK/Format.h>
|
||||||
|
#include <LibGL/GL/gl.h>
|
||||||
|
#include <LibGL/Tex/Texture.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
namespace GL {
|
||||||
|
|
||||||
|
void Texture::upload_texture_data(GLenum, GLint lod, GLint internal_format, GLsizei width, GLsizei height, GLint, GLenum format, GLenum, const GLvoid* pixels)
|
||||||
|
{
|
||||||
|
// NOTE: Some target, format, and internal formats are currently unsupported.
|
||||||
|
// Considering we control this library, and `gl.h` itself, we don't need to add any
|
||||||
|
// checks here to see if we support them; the program will simply fail to compile..
|
||||||
|
|
||||||
|
// Somebody passed us in nullptr...
|
||||||
|
// Apparently this allocates memory on the GPU (according to Khronos docs..)?
|
||||||
|
if (pixels == nullptr) {
|
||||||
|
dbgln("LibGL: pixels == nullptr when uploading texture data.");
|
||||||
|
VERIFY_NOT_REACHED();
|
||||||
|
}
|
||||||
|
|
||||||
|
m_internal_format = internal_format;
|
||||||
|
|
||||||
|
// Get reference to the mip
|
||||||
|
auto& mip = m_mipmaps[lod];
|
||||||
|
const u8* pixel_byte_array = reinterpret_cast<const u8*>(pixels);
|
||||||
|
|
||||||
|
// Copy pixel data to storage
|
||||||
|
|
||||||
|
// Pixels are already 32-bits wide
|
||||||
|
if (format == GL_RGBA || format == GL_BGRA) {
|
||||||
|
mip.pixel_data().resize(width * height * sizeof(u32));
|
||||||
|
memcpy(mip.pixel_data().data(), pixels, width * height * sizeof(u32));
|
||||||
|
} else {
|
||||||
|
mip.pixel_data().resize(width * height * 3);
|
||||||
|
// Copy RGB or BGR pixel data
|
||||||
|
for (auto i = 0; i < width * height * 3; i += 3) {
|
||||||
|
u32 b0 = pixel_byte_array[i]; // B or R
|
||||||
|
u32 b1 = pixel_byte_array[i + 1]; // G
|
||||||
|
u32 b2 = pixel_byte_array[i + 2]; // R or B
|
||||||
|
|
||||||
|
u32 pixel = ((0xffu << 24) | (b0 << 16) | (b1 << 8) | b2);
|
||||||
|
mip.pixel_data().append(pixel);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Now we need to swizzle the texture data from `format` to `internal_format`
|
||||||
|
switch (format) {
|
||||||
|
case GL_BGR: {
|
||||||
|
if (internal_format == GL_RGB) {
|
||||||
|
swizzle(mip.pixel_data(), [](u32 pixel) -> u32 {
|
||||||
|
u8 r = pixel & 0xff;
|
||||||
|
u8 g = (pixel >> 8) & 0xff;
|
||||||
|
u8 b = (pixel >> 16) & 0xff;
|
||||||
|
|
||||||
|
return (0xff << 24) | (r << 16) | (g << 8) | b;
|
||||||
|
});
|
||||||
|
} else if (internal_format == GL_RGBA) {
|
||||||
|
swizzle(mip.pixel_data(), [](u32 pixel) -> u32 {
|
||||||
|
u8 r = pixel & 0xff;
|
||||||
|
u8 g = (pixel >> 8) & 0xff;
|
||||||
|
u8 b = (pixel >> 16) & 0xff;
|
||||||
|
|
||||||
|
return (r << 24) | (g << 16) | (b << 8) | 0xff;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} break;
|
||||||
|
case GL_BGRA: {
|
||||||
|
if (internal_format == GL_RGB) {
|
||||||
|
swizzle(mip.pixel_data(), [](u32 pixel) -> u32 {
|
||||||
|
u8 r = (pixel >> 8) & 0xff;
|
||||||
|
u8 g = (pixel >> 16) & 0xff;
|
||||||
|
u8 b = (pixel >> 24) & 0xff;
|
||||||
|
|
||||||
|
return (0xff << 24) | (r << 16) | (g << 8) | b;
|
||||||
|
});
|
||||||
|
} else if (internal_format == GL_RGBA) {
|
||||||
|
swizzle(mip.pixel_data(), [](u32 pixel) -> u32 {
|
||||||
|
u8 a = pixel & 0xff;
|
||||||
|
u8 r = (pixel >> 8) & 0xff;
|
||||||
|
u8 g = (pixel >> 16) & 0xff;
|
||||||
|
u8 b = (pixel >> 24) & 0xff;
|
||||||
|
|
||||||
|
return (r << 24) | (g << 16) | (b << 8) | a;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} break;
|
||||||
|
case GL_RGB: {
|
||||||
|
if (internal_format == GL_RGBA) {
|
||||||
|
swizzle(mip.pixel_data(), [](u32 pixel) -> u32 {
|
||||||
|
u8 r = pixel & 0xff;
|
||||||
|
u8 g = (pixel >> 8) & 0xff;
|
||||||
|
u8 b = (pixel >> 16) & 0xff;
|
||||||
|
|
||||||
|
return (r << 24) | (g << 16) | (b << 8) | 0xff;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} break;
|
||||||
|
case GL_RGBA:
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
// Let's crash for now so we can implement format by format
|
||||||
|
VERIFY_NOT_REACHED();
|
||||||
|
}
|
||||||
|
|
||||||
|
mip.set_width(width);
|
||||||
|
mip.set_height(height);
|
||||||
|
}
|
||||||
|
|
||||||
|
FloatVector4 Texture::sample_texel(const FloatVector2& uv) const
|
||||||
|
{
|
||||||
|
auto& mip = m_mipmaps.at(0);
|
||||||
|
|
||||||
|
// FIXME: Remove this to prevent a crash when we have proper texture binding
|
||||||
|
if (mip.width() == 0 || mip.height() == 0)
|
||||||
|
return { 1.0f, 1.0f, 1.0f, 1.0f };
|
||||||
|
|
||||||
|
u32 u = static_cast<u32>(uv.x() * mip.width());
|
||||||
|
u32 v = static_cast<u32>(uv.y() * mip.height());
|
||||||
|
|
||||||
|
u32 pixel = mip.pixel_data().at(v * mip.width() + u);
|
||||||
|
|
||||||
|
float b0 = ((pixel)&0xff) / 255.0f;
|
||||||
|
float b1 = ((pixel >> 8) & 0xff) / 255.0f;
|
||||||
|
float b2 = ((pixel >> 16) & 0xff) / 255.0f;
|
||||||
|
|
||||||
|
return { b0, b1, b2, 1.0f };
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
78
Userland/Libraries/LibGL/Tex/Texture.h
Normal file
78
Userland/Libraries/LibGL/Tex/Texture.h
Normal file
|
@ -0,0 +1,78 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2021, Jesse Buhagiar <jooster669@gmail.com>
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <AK/Array.h>
|
||||||
|
#include <AK/RefCounted.h>
|
||||||
|
#include <AK/Vector.h>
|
||||||
|
#include <LibGL/GL/gl.h>
|
||||||
|
#include <LibGfx/Vector2.h>
|
||||||
|
#include <LibGfx/Vector4.h>
|
||||||
|
|
||||||
|
namespace GL {
|
||||||
|
|
||||||
|
class Texture final : public RefCounted<Texture> {
|
||||||
|
public:
|
||||||
|
// FIXME: These shouldn't really belong here, they're context specific.
|
||||||
|
static constexpr u16 MAX_TEXTURE_SIZE = 2048;
|
||||||
|
static constexpr u8 LOG2_MAX_TEXTURE_SIZE = 11;
|
||||||
|
|
||||||
|
class MipMap {
|
||||||
|
public:
|
||||||
|
MipMap() = default;
|
||||||
|
~MipMap() = default;
|
||||||
|
|
||||||
|
void set_width(GLsizei width) { m_width = width; }
|
||||||
|
void set_height(GLsizei height) { m_height = height; }
|
||||||
|
GLsizei width() const { return m_width; }
|
||||||
|
GLsizei height() const { return m_height; }
|
||||||
|
|
||||||
|
Vector<u32>& pixel_data() { return m_pixel_data; }
|
||||||
|
const Vector<u32>& pixel_data() const { return m_pixel_data; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
GLsizei m_width;
|
||||||
|
GLsizei m_height;
|
||||||
|
Vector<u32> m_pixel_data;
|
||||||
|
};
|
||||||
|
|
||||||
|
// To quote the Khronos documentation:
|
||||||
|
// "You could say that a texture object contains a sampler object, which you access through the texture interface."
|
||||||
|
// FIXME: Better name?
|
||||||
|
struct TextureSamplerParamaters {
|
||||||
|
GLint m_min_filter { GL_NEAREST_MIPMAP_LINEAR };
|
||||||
|
GLint m_mag_filter { GL_LINEAR };
|
||||||
|
GLint m_wrap_s_mode { GL_REPEAT };
|
||||||
|
GLint m_wrap_t_mode { GL_REPEAT };
|
||||||
|
};
|
||||||
|
|
||||||
|
public:
|
||||||
|
Texture() = default;
|
||||||
|
~Texture() { }
|
||||||
|
|
||||||
|
void upload_texture_data(GLenum target, GLint lod, GLint internal_format, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const GLvoid* pixels);
|
||||||
|
void replace_sub_texture_data(GLint lod, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, const GLvoid* data);
|
||||||
|
FloatVector4 sample_texel(const FloatVector2& uv) const;
|
||||||
|
|
||||||
|
GLenum internal_format() const { return m_internal_format; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
template<typename TCallback>
|
||||||
|
void swizzle(Vector<u32>& pixels, TCallback&& callback)
|
||||||
|
{
|
||||||
|
for (auto& pixel : pixels)
|
||||||
|
pixel = callback(pixel);
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
// FIXME: Mipmaps are currently unused, but we have the plumbing for it at least
|
||||||
|
Array<MipMap, LOG2_MAX_TEXTURE_SIZE> m_mipmaps;
|
||||||
|
GLenum m_internal_format;
|
||||||
|
TextureSamplerParamaters m_sampler_params;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue