Merge branch 'Feat/dev-es-shittily-draw' into dev-es without ShittilyDraw

# Conflict:
#	MG/Includes.h

# Changes:
#      Remove ShittilyDraw, ready to implement MG_RHI...
#      Update progress.
This commit is contained in:
BZLZHH
2025-05-17 11:50:47 +08:00
40 changed files with 3163 additions and 321 deletions
+134 -64
View File
@@ -11,7 +11,7 @@
// TextureObject
bool TextureObject::IsImmutable() const {
bool immutable = params.texPropertiesInt.count(GL_TEXTURE_IMMUTABLE_FORMAT);
bool immutable = params.texPropertiesInt.find(GL_TEXTURE_IMMUTABLE_FORMAT) != params.texPropertiesInt.end();
MG_Util::Debug::LogD("MG_State: Texture: TextureObject::IsImmutable returns %d", immutable);
return immutable;
}
@@ -54,7 +54,7 @@ bool TextureState::IsTextureGenerated(GLuint texture) {
if (!isValidTexture) {
MG_Util::Debug::LogD("MG_State: Texture: IsTextureGenerated invalid texture %d", texture);
} else {
generated = textures.at(texture).generated;
generated = textures[texture].generated;
if (!generated) {
MG_Util::Debug::LogD("MG_State: Texture: IsTextureGenerated texture %d not generated", texture, generated);
}
@@ -101,12 +101,12 @@ GLenum TextureState::CreateN(GLsizei n, GLuint* textures) {
for (GLsizei i = 0; i < n; ++i) {
GLuint id = 0;
if (!freeIDs_.empty()) {
id = *freeIDs_.begin();
freeIDs_.erase(freeIDs_.begin());
if (!freeID_.empty()) {
id = freeID_.back();
freeID_.pop_back();
MG_Util::Debug::LogD("MG_State: Texture: CreateN reusing free id=%u", id);
} else {
id = ++lastUsedID_;
id = lastUsedID_++;
MG_Util::Debug::LogD("MG_State: Texture: CreateN new id=%u", id);
}
TextureObject obj;
@@ -258,7 +258,7 @@ GLenum TextureState::Upload2D(GLenum target, GLint level, GLint internalFormat,
if (isProxyTexture) {
MG_Util::Debug::LogD("MG_State: Texture: Upload2D proxy texture detected");
TextureObject& proxyTex = proxyTextures_[target];
TextureParams::MipmapLevel mip{};
auto& mip = proxyTex.params.mipmapData[level];
mip.width = width;
mip.height = height;
mip.internalFormat = internalFormat;
@@ -266,19 +266,25 @@ GLenum TextureState::Upload2D(GLenum target, GLint level, GLint internalFormat,
mip.type = type;
proxyTex.generated = true;
proxyTex.target = target;
proxyTex.params.mipmapData[level] = mip;
return GL_NO_ERROR;
}
GLuint boundTex = textureUnits_[activeTextureUnit_].GetBoundTexture(target);
TextureObject& tex = textures[boundTex];
TextureParams::MipmapLevel mip{};
// TextureParams::MipmapLevel mip{};
auto& mip = tex.params.mipmapData[level];
mip.width = width;
mip.height = height;
mip.internalFormat = internalFormat;
mip.format = format;
mip.type = type;
if (data != nullptr) {
mip.dirty = true;
if (data == nullptr) {
mip.hasData = false;
MG_Util::Debug::LogD("MG_State: Texture: Upload2D data pointer is null");
} else {
GLint unpackSwapBytes = GetUnpackParam_(GL_UNPACK_SWAP_BYTES);
GLint unpackLSBFirst = GetUnpackParam_(GL_UNPACK_LSB_FIRST);
GLint unpackSkipPixels = GetUnpackParam_(GL_UNPACK_SKIP_PIXELS);
@@ -288,47 +294,69 @@ GLenum TextureState::Upload2D(GLenum target, GLint level, GLint internalFormat,
GLint unpackImageHeight = GetUnpackParam_(GL_UNPACK_IMAGE_HEIGHT);
GLint unpackSkipImages = GetUnpackParam_(GL_UNPACK_SKIP_IMAGES);
GLsizei rowLength = (unpackRowLength > 0) ? unpackRowLength : width;
bool isDefaultUnpack = (unpackSwapBytes == 0) && (unpackLSBFirst == 0) &&
(unpackSkipPixels == 0) && (unpackSkipRows == 0) &&
(unpackRowLength == 0) && (unpackAlignment == 4) &&
(unpackImageHeight == 0) && (unpackSkipImages == 0);
size_t bytesPerPixel = CalculateBytesPerPixel_(format, type);
size_t componentSize = GetComponentSize_(type);
size_t srcRowSize = rowLength * bytesPerPixel;
size_t srcRowStride = (srcRowSize + unpackAlignment - 1) & ~(unpackAlignment - 1);
size_t srcImageStride = (unpackImageHeight > 0) ?
srcRowStride * unpackImageHeight :
srcRowStride * height;
const GLubyte* srcData = static_cast<const GLubyte*>(data);
srcData += unpackSkipImages * srcImageStride;
srcData += unpackSkipRows * srcRowStride;
srcData += unpackSkipPixels * bytesPerPixel;
size_t dstRowStride = width * bytesPerPixel;
size_t dstSize = dstRowStride * height;
mip.pixelData.resize(dstSize);
GLubyte* dstData = mip.pixelData.data();
GLubyte *dstData = mip.pixelData.data();
for (GLsizei y = 0; y < height; ++y) {
const GLubyte* srcRow = srcData + y * srcRowStride;
GLubyte* dstRow = dstData + y * dstRowStride;
if (isDefaultUnpack) {
const GLubyte *srcData = static_cast<const GLubyte *>(data);
size_t srcRowSize = width * bytesPerPixel;
size_t srcRowStride = (srcRowSize + unpackAlignment - 1) & ~(unpackAlignment - 1);
if (unpackSwapBytes) {
SwapBytesForTexture_(format, type, srcRow, dstRow, width);
if (srcRowStride == dstRowStride) {
memcpy(dstData, srcData, dstSize);
MG_Util::Debug::LogD(
"MG_State: Texture: Upload2D uploaded %zu bytes with fast path - block memcpy", dstSize);
} else {
for (GLsizei y = 0; y < height; ++y) {
const GLubyte *srcRow = srcData + y * srcRowStride;
GLubyte *dstRow = dstData + y * dstRowStride;
memcpy(dstRow, srcRow, srcRowSize);
}
MG_Util::Debug::LogD(
"MG_State: Texture: Upload2D uploaded %zu bytes with fast path - row memcpy", dstSize);
}
else if (unpackLSBFirst && componentSize == 1) {
ReverseBitOrder_(srcRow, dstRow, width * bytesPerPixel);
}
else {
memcpy(dstRow, srcRow, width * bytesPerPixel);
} else {
GLsizei rowLength = (unpackRowLength > 0) ? unpackRowLength : width;
size_t srcRowSize = rowLength * bytesPerPixel;
size_t srcRowStride = (srcRowSize + unpackAlignment - 1) & ~(unpackAlignment - 1);
size_t srcImageStride = (unpackImageHeight > 0) ?
srcRowStride * unpackImageHeight :
srcRowStride * height;
const GLubyte *srcData = static_cast<const GLubyte *>(data);
srcData += unpackSkipImages * srcImageStride;
srcData += unpackSkipRows * srcRowStride;
srcData += unpackSkipPixels * bytesPerPixel;
for (GLsizei y = 0; y < height; ++y) {
const GLubyte *srcRow = srcData + y * srcRowStride;
GLubyte *dstRow = dstData + y * dstRowStride;
if (unpackSwapBytes) {
SwapBytesForTexture_(format, type, srcRow, dstRow, width);
} else if (unpackLSBFirst && componentSize == 1) {
ReverseBitOrder_(srcRow, dstRow, width * bytesPerPixel);
} else {
memcpy(dstRow, srcRow, width * bytesPerPixel);
}
}
mip.hasData = true;
MG_Util::Debug::LogD(
"MG_State: Texture: Upload2D uploaded %zu bytes with unpack params", dstSize);
}
mip.hasData = true;
MG_Util::Debug::LogD("MG_State: Texture: Upload2D uploaded %zu bytes with unpack params", dstSize);
} else {
mip.hasData = false;
MG_Util::Debug::LogD("MG_State: Texture: Upload2D data pointer is null");
}
tex.params.mipmapData[level] = mip;
// tex.params.mipmapData[level] = mip;
return GL_NO_ERROR;
}
@@ -364,7 +392,7 @@ GLenum TextureState::UpdateRegion2D(GLenum target, GLint level, GLint xoffset,
const size_t bytesPerPixel = CalculateBytesPerPixel_(format, type);
const size_t srcSize = CalculatePixelDataSize_(format, type, width, height);
if (bytesPerPixel == 0) {
MG_Util::Debug::LogE("MG_State: Texture: Invalid format/type combination");
return GL_INVALID_ENUM;
@@ -402,27 +430,68 @@ GLenum TextureState::UpdateRegion2D(GLenum target, GLint level, GLint xoffset,
MG_Util::Debug::LogD("MG_State: Texture: UpdateRegion2D srcData=%p, dstData=%p", srcData, dstData);
MG_Util::Debug::LogD("MG_State: Texture: UpdateRegion2D bytesPerPixel=%zu", bytesPerPixel);
for (GLsizei y = 0; y < height; ++y) {
const GLubyte* srcRow = srcData + y * srcRowStride;
GLubyte* dstRow = dstData + y * dstRowStride;
// Check if fast path is applicable
const bool isDefaultUnpack = (unpackSwapBytes == 0) && (unpackLSBFirst == 0) &&
(unpackRowLength == 0) && (unpackAlignment == 4) &&
(unpackSkipPixels == 0) && (unpackSkipRows == 0) &&
(unpackImageHeight == 0) && (unpackSkipImages == 0);
if (unpackSwapBytes) {
for (GLsizei x = 0; x < width; ++x) {
const GLubyte* srcPixel = srcRow + x * bytesPerPixel;
GLubyte* dstPixel = dstRow + x * bytesPerPixel;
SwapPixelBytes_(format, type, srcPixel, dstPixel);
}
} else if (unpackLSBFirst && GetComponentSize_(type) == 1) {
for (size_t i = 0; i < width * bytesPerPixel; ++i) {
dstRow[i] = ReverseBits_(srcRow[i]);
}
if (isDefaultUnpack) {
const size_t copySize = width * bytesPerPixel;
if (srcRowStride == dstRowStride) {
MG_Util::Debug::LogD("MG_State: Texture: UpdateRegion2D block memcpy(dst=%p, src=%p, size=%zu) called.", dstData, srcData, width * bytesPerPixel);
memcpy(dstData, srcData, copySize * height);
} else {
MG_Util::Debug::LogD("MG_State: Texture: UpdateRegion2D memcpy(dst=%p, src=%p, size=%zu) called.", dstRow, srcRow, width * bytesPerPixel);
memcpy(dstRow, srcRow, width * bytesPerPixel);
for (GLsizei y = 0; y < height; ++y) {
const GLubyte* srcRow = srcData + y * srcRowStride;
GLubyte* dstRow = dstData + y * dstRowStride;
memcpy(dstRow, srcRow, copySize);
MG_Util::Debug::LogD("MG_State: Texture: UpdateRegion2D row memcpy(dst=%p, src=%p, size=%zu) called.", dstRow, srcRow, width * bytesPerPixel);
}
}
} else {
const size_t componentSize = GetComponentSize_(type);
for (GLsizei y = 0; y < height; ++y) {
const GLubyte* srcRow = srcData + y * srcRowStride;
GLubyte* dstRow = dstData + y * dstRowStride;
if (unpackSwapBytes) {
for (GLsizei x = 0; x < width; ++x) {
SwapPixelBytes_(format, type, srcRow + x*bytesPerPixel, dstRow + x*bytesPerPixel);
}
} else if (unpackLSBFirst && componentSize == 1) {
for (size_t i = 0; i < width * bytesPerPixel; ++i) {
dstRow[i] = ReverseBits_(srcRow[i]);
}
} else {
memcpy(dstRow, srcRow, width * bytesPerPixel);
}
}
}
// for (GLsizei y = 0; y < height; ++y) {
// const GLubyte* srcRow = srcData + y * srcRowStride;
// GLubyte* dstRow = dstData + y * dstRowStride;
//
// if (unpackSwapBytes) {
// for (GLsizei x = 0; x < width; ++x) {
// const GLubyte* srcPixel = srcRow + x * bytesPerPixel;
// GLubyte* dstPixel = dstRow + x * bytesPerPixel;
// SwapPixelBytes_(format, type, srcPixel, dstPixel);
// }
// } else if (unpackLSBFirst && GetComponentSize_(type) == 1) {
// for (size_t i = 0; i < width * bytesPerPixel; ++i) {
// dstRow[i] = ReverseBits_(srcRow[i]);
// }
// } else {
// MG_Util::Debug::LogD("MG_State: Texture: UpdateRegion2D memcpy(dst=%p, src=%p, size=%zu) called.", dstRow, srcRow, width * bytesPerPixel);
// memcpy(dstRow, srcRow, width * bytesPerPixel);
// }
// }
mip.hasData = true;
mip.dirty = true;
MG_Util::Debug::LogD("MG_State: Texture: UpdateRegion2D succeeded");
return GL_NO_ERROR;
}
@@ -541,7 +610,7 @@ GLenum TextureState::Delete(GLuint texture) {
if (it != this->textures.end()) {
InvalidateTextureInAllUnits_(texture);
this->textures.erase(it);
freeIDs_.insert(texture);
//freeID_.emplace_back(texture);
MG_Util::Debug::LogD("MG_State: Texture: Delete succeeded for texture=%u", texture);
} else {
MG_Util::Debug::LogW("MG_State: Texture: Delete texture %u not found", texture);
@@ -563,7 +632,8 @@ GLenum TextureState::DeleteN(GLsizei n, const GLuint* textures) {
if (it != this->textures.end()) {
InvalidateTextureInAllUnits_(id);
this->textures.erase(it);
freeIDs_.insert(id);
// TODO: Prevent the object that uses a freeId from conflicting with legacy object in the backend.
//freeID_.emplace_back(id);
MG_Util::Debug::LogD("MG_State: Texture: DeleteN deleted texture=%u", id);
} else {
MG_Util::Debug::LogW("MG_State: Texture: DeleteN texture %u not found", id);
@@ -630,7 +700,7 @@ GLenum TextureState::QueryLevelPropertyIntVector(GLenum target, GLint level, GLe
return GL_NO_ERROR;
}
std::unordered_map<GLuint, TextureObject>::iterator texIt;
unordered_map<GLuint, TextureObject>::iterator texIt;
if (!isProxyTexture) {
texIt = textures.find(boundTexture);
@@ -803,10 +873,10 @@ GLenum TextureState::CheckUploadingTexture2DValidity_(GLenum target, GLint level
return GL_INVALID_OPERATION;
}
bool isDepthInternal = (internalFormat == GL_DEPTH_COMPONENT32F || internalFormat == GL_DEPTH_COMPONENT24
|| internalFormat == GL_DEPTH_COMPONENT16 || internalFormat == GL_DEPTH32F_STENCIL8
|| internalFormat == GL_DEPTH24_STENCIL8 || internalFormat == GL_DEPTH_COMPONENT
|| internalFormat == GL_DEPTH_STENCIL);
bool isDepthInternal = (internalFormat == GL_DEPTH_COMPONENT32 || internalFormat == GL_DEPTH_COMPONENT32F ||
internalFormat == GL_DEPTH_COMPONENT24 || internalFormat == GL_DEPTH_COMPONENT16 ||
internalFormat == GL_DEPTH32F_STENCIL8 || internalFormat == GL_DEPTH24_STENCIL8 ||
internalFormat == GL_DEPTH_COMPONENT || internalFormat == GL_DEPTH_STENCIL);
bool isDepthFormat = (format == GL_DEPTH_COMPONENT || format == GL_DEPTH_STENCIL);
if (isDepthInternal != isDepthFormat) {
MG_Util::Debug::LogE("MG_State: Texture: CheckUploadingTexture2DValidity_ depth internal/format mismatch");
@@ -895,7 +965,7 @@ GLenum TextureState::CheckUpdatingTextureRegion2DValidity_(GLenum target, GLint
return GL_INVALID_OPERATION;
}
auto tex = textures[boundTex];
const auto& tex = textures[boundTex];
if (tex.IsImmutable()) {
MG_Util::Debug::LogE("MG_State: Texture: CheckUpdatingTextureRegion2DValidity_ texture is immutable");
@@ -934,7 +1004,7 @@ GLenum TextureState::CheckUpdatingTextureRegion2DValidity_(GLenum target, GLint
return GL_INVALID_OPERATION;
}
TextureParams::MipmapLevel& mip = mipIt->second;
const auto& mip = mipIt->second;
if (xoffset + width > mip.width || yoffset + height > mip.height) {
MG_Util::Debug::LogE("MG_State: Texture: CheckUpdatingTextureRegion2DValidity_ region out of bounds: "
"x=%d+%d > %d or y=%d+%d > %d",
+18 -10
View File
@@ -19,8 +19,10 @@ struct ComponentSizes {
};
struct TextureParams {
std::unordered_map<GLenum, GLfloat> texPropertiesFloat;
std::unordered_map<GLenum, GLint> texPropertiesInt;
template <typename K, typename V>
using unordered_map = ankerl::unordered_dense::map<K, V>;
unordered_map<GLenum, GLfloat> texPropertiesFloat;
unordered_map<GLenum, GLint> texPropertiesInt;
struct MipmapLevel {
GLsizei width = 0;
GLsizei height = 0;
@@ -29,8 +31,9 @@ struct TextureParams {
GLenum type = GL_UNSIGNED_BYTE;
std::vector<GLubyte> pixelData;
bool hasData = false;
bool dirty = false; // TODO: encapsulate this with an public API to RHI
};
std::unordered_map<GLint, MipmapLevel> mipmapData;
unordered_map<GLint, MipmapLevel> mipmapData;
};
class TextureObject {
@@ -44,24 +47,27 @@ public:
};
class TextureUnitState {
template <typename K, typename V>
using unordered_map = ankerl::unordered_dense::map<K, V>;
private:
GLenum activeTarget = GL_TEXTURE_2D;
public:
std::unordered_map<GLenum, GLuint> boundTextures;
unordered_map<GLenum, GLuint> boundTextures;
void Bind(GLenum target, GLuint texture);
GLuint GetBoundTexture(GLenum target);
};
class TextureState {
template <typename K, typename V>
using unordered_map = ankerl::unordered_dense::map<K, V>;
private:
std::vector<TextureUnitState> textureUnits_;
GLuint activeTextureUnit_ = 0;
GLuint lastUsedID_ = 0;
std::set<GLuint> freeIDs_;
GLuint lastUsedID_ = 1;
// ankerl::unordered_set<GLuint> freeIDs_;
std::vector<GLuint> freeID_;
std::unordered_map<GLenum, TextureObject> proxyTextures_;
unordered_map<GLenum, TextureObject> proxyTextures_;
static GLint GetUnpackParam_(GLenum pname);
public:
@@ -69,7 +75,7 @@ public:
bool IsTextureGenerated(GLuint texture);
bool IsTexture(GLuint texture);
std::unordered_map<GLuint, TextureObject> textures;
unordered_map<GLuint, TextureObject> textures;
// Return: the validity of the operation, according to OpenGL 3 standard
GLenum BindUnit(GLenum textureUnit);
@@ -88,6 +94,8 @@ public:
GLenum DeleteN(GLsizei n, const GLuint* textures);
GLenum QueryLevelPropertyIntVector(GLenum target, GLint level, GLenum pname, GLint* params);
std::vector<TextureUnitState> textureUnits_;
GLuint activeTextureUnit_ = 0;
private:
size_t CalculatePixelDataSize_(GLenum format, GLenum type, GLsizei width, GLsizei height);
void InvalidateTextureInAllUnits_(GLuint texture);