mirror of
https://github.com/MobileGL-Dev/MobileGL
synced 2026-09-12 06:08:30 +09:00
[Feat] (MG_State/Program): retrieve/generate layout location from/for program, and retrieve generated ubo size and all the offsets of members
This commit is contained in:
@@ -23,6 +23,7 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
#include <cstdarg>
|
#include <cstdarg>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
#include <numeric>
|
||||||
#include <expected>
|
#include <expected>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
|
|||||||
@@ -265,7 +265,7 @@ namespace MobileGL {
|
|||||||
std::string src;
|
std::string src;
|
||||||
for (GLsizei i = 0; i < count; i++) {
|
for (GLsizei i = 0; i < count; i++) {
|
||||||
src +=
|
src +=
|
||||||
(length[i] <= 0) ? string[i] : std::string(string[i], length[i]);
|
(length == nullptr || length[i] <= 0) ? string[i] : std::string(string[i], length[i]);
|
||||||
}
|
}
|
||||||
shaderObject->SetShaderSource(Move(src));
|
shaderObject->SetShaderSource(Move(src));
|
||||||
}
|
}
|
||||||
@@ -377,6 +377,7 @@ namespace MobileGL {
|
|||||||
GLuint CreateProgram(void) {
|
GLuint CreateProgram(void) {
|
||||||
return CreateProgram_State();
|
return CreateProgram_State();
|
||||||
}
|
}
|
||||||
|
|
||||||
GLuint CreateShader(GLenum type) {
|
GLuint CreateShader(GLenum type) {
|
||||||
return CreateShader_State(type);
|
return CreateShader_State(type);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,6 +30,8 @@ namespace MobileGL {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void ProgramObject::Link() {
|
void ProgramObject::Link() {
|
||||||
|
PreLink();
|
||||||
|
|
||||||
Vector<GLenum> shaderTypes(m_shaders.size());
|
Vector<GLenum> shaderTypes(m_shaders.size());
|
||||||
Vector<SharedPtr<glslang::TShader>> shaders(m_shaders.size());
|
Vector<SharedPtr<glslang::TShader>> shaders(m_shaders.size());
|
||||||
for (SizeT i = 0; i < m_shaders.size(); i++) {
|
for (SizeT i = 0; i < m_shaders.size(); i++) {
|
||||||
@@ -54,6 +56,8 @@ namespace MobileGL {
|
|||||||
result.error().log);
|
result.error().log);
|
||||||
THROW_EXCEPTION(e);
|
THROW_EXCEPTION(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
PostLink();
|
||||||
}
|
}
|
||||||
|
|
||||||
void ProgramObject::MarkAsDeleted() {
|
void ProgramObject::MarkAsDeleted() {
|
||||||
@@ -64,6 +68,82 @@ namespace MobileGL {
|
|||||||
return m_shaders;
|
return m_shaders;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ProgramObject::PreLink() {
|
||||||
|
m_uniforms.clear();
|
||||||
|
m_uniformOffsets.clear();
|
||||||
|
|
||||||
|
for (const auto& shader : m_shaders) {
|
||||||
|
for (const auto& [name, loc] : shader->GetUniformLocations()) {
|
||||||
|
// collect all the names to map
|
||||||
|
m_uniforms[name] = loc;
|
||||||
|
|
||||||
|
// set a flag for those who have an explicit location
|
||||||
|
if (loc != 4095) {
|
||||||
|
if (loc >= m_uniformOffsets.size()) {
|
||||||
|
m_uniformOffsets.reserve(std::bit_ceil(loc + 1));
|
||||||
|
m_uniformOffsets.resize(loc + 1, 0);
|
||||||
|
}
|
||||||
|
assert(m_uniformOffsets[loc] == 0);
|
||||||
|
m_uniformOffsets[loc] = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Let's find a location for those who doesn't have one yet
|
||||||
|
Uint nextLocation = 0;
|
||||||
|
|
||||||
|
// Find first empty location
|
||||||
|
for (SizeT i = 0; i < m_uniformOffsets.size(); i++) {
|
||||||
|
if (m_uniformOffsets[i] == 0) {
|
||||||
|
nextLocation = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto& [name, loc] : m_uniforms) {
|
||||||
|
if (loc == 4095) {
|
||||||
|
// check if we drained all the holes already
|
||||||
|
if (nextLocation >= m_uniformOffsets.size()) {
|
||||||
|
loc = nextLocation;
|
||||||
|
m_uniformOffsets.push_back(1);
|
||||||
|
nextLocation++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// assign an empty location
|
||||||
|
loc = nextLocation;
|
||||||
|
m_uniformOffsets[loc] = 1;
|
||||||
|
|
||||||
|
// Find next empty location
|
||||||
|
for (nextLocation++; nextLocation < m_uniformOffsets.size(); nextLocation++) {
|
||||||
|
if (m_uniformOffsets[nextLocation] == 0)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ProgramObject::PostLink() {
|
||||||
|
if (m_programBinary.empty()) {
|
||||||
|
assert(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
MG_Util::ShaderTranspiler::SpvcSession session(m_programBinary[0]);
|
||||||
|
const char* src = nullptr; // we dont care the source atm
|
||||||
|
auto result = session.Compile(&src);
|
||||||
|
if (result != SPVC_SUCCESS) {
|
||||||
|
assert(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
auto& metadata = session.GetMetadata();
|
||||||
|
auto& uniformOffsets = metadata.plainUniformOffsetsInUBO;
|
||||||
|
for (const auto& [name, offset] : uniformOffsets) {
|
||||||
|
assert(m_uniforms.find(name) != m_uniforms.end());
|
||||||
|
assert(m_uniforms[name] < m_uniformOffsets.size());
|
||||||
|
m_uniformOffsets[m_uniforms[name]] = offset;
|
||||||
|
}
|
||||||
|
m_uboScratch.resize(metadata.uboSize);
|
||||||
|
}
|
||||||
} // namespace GLState
|
} // namespace GLState
|
||||||
} // namespace MG_State
|
} // namespace MG_State
|
||||||
} // namespace MobileGL
|
} // namespace MobileGL
|
||||||
@@ -16,10 +16,21 @@ namespace MobileGL {
|
|||||||
Vector<SharedPtr<ShaderObject>>& GetAttachedShaders();
|
Vector<SharedPtr<ShaderObject>>& GetAttachedShaders();
|
||||||
const String& GetInfoLog() const { return m_infoLog; }
|
const String& GetInfoLog() const { return m_infoLog; }
|
||||||
private:
|
private:
|
||||||
|
void PreLink();
|
||||||
|
void PostLink();
|
||||||
|
|
||||||
const Uint m_id = 0;
|
const Uint m_id = 0;
|
||||||
Vector<SharedPtr<ShaderObject>> m_shaders;
|
Vector<SharedPtr<ShaderObject>> m_shaders;
|
||||||
// basically this contains SPIR-V in binary format
|
// basically this contains SPIR-V in binary format
|
||||||
Vector<Vector<Uint>> m_programBinary;
|
Vector<Vector<Uint>> m_programBinary;
|
||||||
|
|
||||||
|
// Uniforms
|
||||||
|
UnorderedMap<String, Uint> m_uniforms;
|
||||||
|
// 0 or 1 for if the location is explicitly specified at PreLink stage,
|
||||||
|
// offsets into global ubo for PostLink
|
||||||
|
Vector<Uint> m_uniformOffsets;
|
||||||
|
Vector<Uint8> m_uboScratch;
|
||||||
|
|
||||||
String m_infoLog;
|
String m_infoLog;
|
||||||
Bool m_deleteStatus = false;
|
Bool m_deleteStatus = false;
|
||||||
Bool m_linkStatus = true;
|
Bool m_linkStatus = true;
|
||||||
|
|||||||
@@ -67,13 +67,14 @@ namespace MobileGL {
|
|||||||
const String& GetShaderSource() const { return m_source; }
|
const String& GetShaderSource() const { return m_source; }
|
||||||
SharedPtr<glslang::TShader> GetCompiledShader() const { return m_shader; }
|
SharedPtr<glslang::TShader> GetCompiledShader() const { return m_shader; }
|
||||||
const String& GetInfoLog() const { return m_infoLog; }
|
const String& GetInfoLog() const { return m_infoLog; }
|
||||||
|
const UnorderedMap<String, Uint> GetUniformLocations() const { return m_uniforms; }
|
||||||
private:
|
private:
|
||||||
bool DoReflection();
|
bool DoReflection();
|
||||||
const Uint m_id = 0;
|
const Uint m_id = 0;
|
||||||
const ShaderStage m_stage;
|
const ShaderStage m_stage;
|
||||||
String m_source;
|
String m_source;
|
||||||
SharedPtr<glslang::TShader> m_shader;
|
SharedPtr<glslang::TShader> m_shader;
|
||||||
UnorderedMap<String, Int> m_uniforms;
|
UnorderedMap<String, Uint> m_uniforms;
|
||||||
|
|
||||||
String m_infoLog;
|
String m_infoLog;
|
||||||
Bool m_deleteStatus = false;
|
Bool m_deleteStatus = false;
|
||||||
|
|||||||
@@ -5,10 +5,13 @@ namespace MobileGL {
|
|||||||
template <typename IndexType>
|
template <typename IndexType>
|
||||||
class IndexGenerator {
|
class IndexGenerator {
|
||||||
public:
|
public:
|
||||||
explicit IndexGenerator(SizeT initial_capacity = 1024, IndexType first_index = 0) : next_index_(first_index) {
|
explicit IndexGenerator(SizeT initial_capacity = 1024, IndexType first_index = 1) {
|
||||||
const SizeT words_needed = (initial_capacity + 63) / 64;
|
const SizeT words_needed = (initial_capacity + 63) / 64;
|
||||||
is_valid_.resize(words_needed, ~0ull);
|
is_valid_.resize(words_needed, ~0ull);
|
||||||
freed_indices_.reserve(initial_capacity);
|
freed_indices_.reserve(initial_capacity);
|
||||||
|
std::vector<IndexType> valuesBeforeFirst(first_index);
|
||||||
|
std::iota(valuesBeforeFirst.begin(), valuesBeforeFirst.end(), 0);
|
||||||
|
Generate(valuesBeforeFirst.size(), valuesBeforeFirst.data());
|
||||||
}
|
}
|
||||||
|
|
||||||
void Generate(SizeT n, IndexType* indices) {
|
void Generate(SizeT n, IndexType* indices) {
|
||||||
|
|||||||
@@ -84,6 +84,7 @@ namespace MobileGL {
|
|||||||
|
|
||||||
if (strcmp(list[i].name, GLOBAL_UBO_NAME) == 0) {
|
if (strcmp(list[i].name, GLOBAL_UBO_NAME) == 0) {
|
||||||
spvc_type type = spvc_compiler_get_type_handle(compiler, list[i].base_type_id);
|
spvc_type type = spvc_compiler_get_type_handle(compiler, list[i].base_type_id);
|
||||||
|
spvc_compiler_get_declared_struct_size(compiler, type, &metadata.uboSize);
|
||||||
size_t num_members = spvc_type_get_num_member_types(type);
|
size_t num_members = spvc_type_get_num_member_types(type);
|
||||||
for (size_t j = 0; j < num_members; ++j) {
|
for (size_t j = 0; j < num_members; ++j) {
|
||||||
const char* memberName = spvc_compiler_get_member_name(compiler, list[i].base_type_id, j);
|
const char* memberName = spvc_compiler_get_member_name(compiler, list[i].base_type_id, j);
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ namespace MobileGL {
|
|||||||
namespace ShaderTranspiler {
|
namespace ShaderTranspiler {
|
||||||
struct SpvcMetadata {
|
struct SpvcMetadata {
|
||||||
UnorderedMap<String, unsigned> plainUniformOffsetsInUBO;
|
UnorderedMap<String, unsigned> plainUniformOffsetsInUBO;
|
||||||
|
SizeT uboSize = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
class SpvcSession {
|
class SpvcSession {
|
||||||
|
|||||||
Reference in New Issue
Block a user