added cache VFS to store all these offline-converted formats, repaired opengl renderer because of some annoying bug of implicitly casted vectors of different dimensions......
This commit is contained in:
parent
44e0caca5b
commit
d481d8ae4a
2
.gitignore
vendored
2
.gitignore
vendored
@ -64,6 +64,8 @@ default/
|
||||
*.vtf
|
||||
*.ztmp
|
||||
*.adp
|
||||
*.mis
|
||||
graph.json
|
||||
|
||||
maps/
|
||||
models/
|
||||
|
||||
@ -424,7 +424,7 @@
|
||||
"auto validate": false
|
||||
},
|
||||
"loader": {
|
||||
"assert": true,
|
||||
"assert": false,
|
||||
"async": false // to-do: fix
|
||||
},
|
||||
"hooks": {
|
||||
|
||||
@ -16,7 +16,7 @@
|
||||
"events": {
|
||||
"click": {
|
||||
"name": "game:Scene.Load",
|
||||
"payload": { "scene": "SourceEngine" },
|
||||
"payload": { "scene": "Dark" },
|
||||
"delay": 0.125
|
||||
}
|
||||
},
|
||||
|
||||
@ -1,8 +1,8 @@
|
||||
{
|
||||
"import": "./base_dark.json",
|
||||
"assets": [
|
||||
{ "filename": "game://Data/MEDSCI1.MIS" }
|
||||
// { "filename": "./maps/medsci1.mis" }
|
||||
// { "filename": "./maps/medsci1/graph.json" }
|
||||
// { "filename": "game://Data/MEDSCI1.MIS" }
|
||||
// { "filename": "./mis/medsci1.mis" }
|
||||
{ "filename": "./mis/medsci1/graph.json" }
|
||||
]
|
||||
}
|
||||
@ -9,6 +9,8 @@ namespace ext {
|
||||
// RAII
|
||||
virtual const uf::stl::string getType() const;
|
||||
|
||||
virtual GraphicDescriptor bindGraphicDescriptor( const GraphicDescriptor&, size_t = 0 );
|
||||
|
||||
virtual void createCommandBuffers( const uf::stl::vector<ext::opengl::Graphic*>& graphics );
|
||||
virtual void initialize( Device& device );
|
||||
virtual void tick();
|
||||
|
||||
@ -183,7 +183,7 @@
|
||||
#define UF_DEBUG 1
|
||||
#endif
|
||||
|
||||
|
||||
#define UF_CONVERSION_TO_CACHE 1
|
||||
|
||||
#if UF_DEBUG
|
||||
#include <uf/utils/io/fmt.h>
|
||||
|
||||
@ -50,7 +50,7 @@ pod::Vector<T, N> uf::vector::copy( const T* p ) {
|
||||
}
|
||||
template<typename T, size_t N, typename U>
|
||||
pod::Vector<T, N> uf::vector::cast( const U& from ) {
|
||||
pod::Vector<T, N> to;
|
||||
pod::Vector<T, N> to = {};
|
||||
#pragma unroll // GCC unroll N
|
||||
for ( auto i = 0; i < N && i < U::size; ++i )
|
||||
to[i] = from[i];
|
||||
@ -678,7 +678,7 @@ template<typename T>
|
||||
T uf::vector::round( const T& vector ) {
|
||||
T res;
|
||||
FOR_EACH(T::size, {
|
||||
res[i] = ::round( vector[i] );
|
||||
res[i] = std::round( vector[i] );
|
||||
});
|
||||
return res;
|
||||
}
|
||||
@ -753,11 +753,12 @@ template<typename T>
|
||||
T uf::vector::slerp( const T& from, const T& to, double delta, bool clamp ) {
|
||||
if ( clamp ) delta = fmax( 0, fmin(1,delta) );
|
||||
typename T::type_t dot = uf::vector::dot(from, to);
|
||||
if ( dot > 0.9995f ) return uf::vector::lerp(from, to, delta, clamp);
|
||||
typename T::type_t theta = acos(dot);
|
||||
typename T::type_t sTheta = sin(theta);
|
||||
|
||||
typename T::type_t w1 = sin((1.0f - delta) * theta / sTheta);
|
||||
typename T::type_t w2 = sin( delta * theta / sTheta );
|
||||
typename T::type_t w1 = sin((1.0f - delta) * theta) / sTheta;
|
||||
typename T::type_t w2 = sin( delta * theta ) / sTheta;
|
||||
#if UF_USE_SIMD
|
||||
if constexpr ( simd_able_v<typename T::type_t> ) {
|
||||
return uf::simd::add( uf::simd::mul( from, w1 ), uf::simd::mul( to, w2 ) );
|
||||
@ -824,7 +825,7 @@ T uf::vector::normalize( const T& vector ) {
|
||||
typename T::type_t norm = uf::vector::norm(vector);
|
||||
if ( norm < 1.0e-6 ) return vector;
|
||||
#if UF_ENV_DREAMCAST && UF_ENV_DREAMCAST_SIMD
|
||||
if constexpr ( std::is_same_v<T,float> ) {
|
||||
if constexpr ( std::is_same_v<typename T::type_t, float> ) {
|
||||
return uf::vector::multiply(vector, MATH_fsrra(norm));
|
||||
}
|
||||
#endif
|
||||
|
||||
@ -364,11 +364,15 @@ uf::stl::string uf::asset::load( uf::asset::Payload& payload ) {
|
||||
|
||||
#if UF_USE_DC_TEXCONV
|
||||
uf::stl::string target = "dtex";
|
||||
#if !UF_CONVERSION_TO_CACHE
|
||||
uf::stl::string output = uf::io::replace_extension( filename, target );
|
||||
bool exists = uf::io::exists( output );
|
||||
bool physical = uf::vfs::isPhysical( filename );
|
||||
bool stale = exists ? uf::io::mtime( filename ) > uf::io::mtime( output ) : true;
|
||||
if ( extension != target && !exists && physical && stale ) {
|
||||
if ( !uf::vfs::isPhysical( filename ) ) output = "cache://" + output;
|
||||
#else
|
||||
uf::stl::string output = "cache://" + uf::io::replace_extension( filename, target );
|
||||
#endif
|
||||
bool stale = uf::io::exists( output ) ? uf::io::mtime( filename ) > uf::io::mtime( output ) : true;
|
||||
if ( extension != target && stale ) {
|
||||
UF_MSG_DEBUG("Converting image: {} => {}", filename, output );
|
||||
pod::Vector2ui size = { 32, 32 };
|
||||
uf::stl::string filter = "linear";
|
||||
uf::stl::string format = "auto";
|
||||
@ -376,7 +380,6 @@ uf::stl::string uf::asset::load( uf::asset::Payload& payload ) {
|
||||
auto img = asset.scale( size, filter);
|
||||
auto dtex = ext::texconv::convert( img, format );
|
||||
ext::texconv::save( dtex, output );
|
||||
UF_MSG_DEBUG("Converting image: {} => {}", filename, output );
|
||||
}
|
||||
#endif
|
||||
} break;
|
||||
@ -387,11 +390,14 @@ uf::stl::string uf::asset::load( uf::asset::Payload& payload ) {
|
||||
|
||||
#if UF_USE_ADP_ENCODER
|
||||
uf::stl::string target = "adp";
|
||||
#if !UF_CONVERSION_TO_CACHE
|
||||
uf::stl::string output = uf::io::replace_extension( filename, target );
|
||||
bool exists = uf::io::exists( output );
|
||||
bool physical = uf::vfs::isPhysical( filename );
|
||||
bool stale = exists ? uf::io::mtime( filename ) > uf::io::mtime( output ) : true;
|
||||
if ( extension != target && !exists && physical && stale ) {
|
||||
if ( !uf::vfs::isPhysical( filename ) ) output = "cache://" + output;
|
||||
#else
|
||||
uf::stl::string output = "cache://" + uf::io::replace_extension( filename, target );
|
||||
#endif
|
||||
bool stale = uf::io::exists( output ) ? uf::io::mtime( filename ) > uf::io::mtime( output ) : true;
|
||||
if ( extension != target && stale ) {
|
||||
pod::PCM pcm;
|
||||
uf::stl::vector<uint8_t> encoded;
|
||||
if ( uf::audio::decode( filename, pcm ) ) {
|
||||
|
||||
@ -101,24 +101,29 @@ namespace {
|
||||
#if EXT_COLOR_FLOATS
|
||||
auto& c = color;
|
||||
#else
|
||||
//pod::Vector4ub c = (color * 255);
|
||||
pod::Vector4ub c = uf::vector::clamp( color, 0, 1 ) * 255;
|
||||
auto& cent = center;
|
||||
|
||||
/*
|
||||
pod::Vector4ub c = {
|
||||
(uint8_t)(color[0] * 255),
|
||||
(uint8_t)(color[1] * 255),
|
||||
(uint8_t)(color[2] * 255),
|
||||
(uint8_t)(color[3] * 255),
|
||||
(uint8_t)(CLAMP(color[0], 0, 1) * 255),
|
||||
(uint8_t)(CLAMP(color[1], 0, 1) * 255),
|
||||
(uint8_t)(CLAMP(color[2], 0, 1) * 255),
|
||||
(uint8_t)(CLAMP(color[3], 0, 1) * 255),
|
||||
};
|
||||
pod::Vector3f cent = { center.x, center.y, 0.0f };
|
||||
*/
|
||||
#endif
|
||||
mesh.bind<::GuiVertex, uint16_t>();
|
||||
mesh.insertVertices<::GuiVertex>({
|
||||
#if UF_USE_OPENGL
|
||||
{ pod::Vector3f{-1.0f, 1.0f, 0.0f} - center, pod::Vector2f{0.0f, 0.0f}, c },
|
||||
{ pod::Vector3f{-1.0f, -1.0f, 0.0f} - center, pod::Vector2f{0.0f, 1.0f}, c },
|
||||
{ pod::Vector3f{ 1.0f, -1.0f, 0.0f} - center, pod::Vector2f{1.0f, 1.0f}, c },
|
||||
{ pod::Vector3f{-1.0f, 1.0f, 0.0f} - cent, pod::Vector2f{0.0f, 0.0f}, c },
|
||||
{ pod::Vector3f{-1.0f, -1.0f, 0.0f} - cent, pod::Vector2f{0.0f, 1.0f}, c },
|
||||
{ pod::Vector3f{ 1.0f, -1.0f, 0.0f} - cent, pod::Vector2f{1.0f, 1.0f}, c },
|
||||
|
||||
{ pod::Vector3f{ 1.0f, -1.0f, 0.0f} - center, pod::Vector2f{1.0f, 1.0f}, c },
|
||||
{ pod::Vector3f{ 1.0f, 1.0f, 0.0f} - center, pod::Vector2f{1.0f, 0.0f}, c },
|
||||
{ pod::Vector3f{-1.0f, 1.0f, 0.0f} - center, pod::Vector2f{0.0f, 0.0f}, c },
|
||||
{ pod::Vector3f{ 1.0f, -1.0f, 0.0f} - cent, pod::Vector2f{1.0f, 1.0f}, c },
|
||||
{ pod::Vector3f{ 1.0f, 1.0f, 0.0f} - cent, pod::Vector2f{1.0f, 0.0f}, c },
|
||||
{ pod::Vector3f{-1.0f, 1.0f, 0.0f} - cent, pod::Vector2f{0.0f, 0.0f}, c },
|
||||
#else
|
||||
{ pod::Vector3f{-1.0f, 1.0f, 0.0f}, pod::Vector2f{0.0f, 0.0f}, c, center },
|
||||
{ pod::Vector3f{-1.0f, -1.0f, 0.0f}, pod::Vector2f{0.0f, 1.0f}, c, center },
|
||||
@ -202,7 +207,7 @@ void ext::GuiBehavior::initialize( uf::Object& self ) {
|
||||
if ( attribute.descriptor.name == "uv" ) uvAttribute = attribute;
|
||||
}
|
||||
|
||||
#if UF_USE_OPENGL
|
||||
#if 0 && UF_USE_OPENGL
|
||||
if ( uf::matrix::reverseInfiniteProjection ) metadata.depth = 1 - metadata.depth;
|
||||
|
||||
// set depth
|
||||
@ -271,7 +276,8 @@ void ext::GuiBehavior::initialize( uf::Object& self ) {
|
||||
auto& image = uf::asset::get<uf::Image>( payload );
|
||||
|
||||
// generate default mesh
|
||||
::generateMesh( mesh, metadata.color, metadata.alignment );
|
||||
auto color = metadata.color; color[3] = 1.0f; // force opaque
|
||||
::generateMesh( mesh, color, metadata.alignment );
|
||||
|
||||
{
|
||||
ext::payloads::GuiInitializationPayload payload;
|
||||
|
||||
@ -230,6 +230,7 @@ void uf::graph::preprocess( pod::Graph& graph, const uf::Serializer& metadata, c
|
||||
graph.root.index = -1;
|
||||
}
|
||||
|
||||
// to-do: fix gltf importer again
|
||||
void uf::graph::postprocess( pod::Graph& graph ) {
|
||||
auto& storage = uf::graph::getStorage( graph );
|
||||
// post-processing
|
||||
|
||||
@ -800,7 +800,6 @@ void uf::graph::initializeGraphics( pod::Graph& graph, uf::Object& entity, uf::M
|
||||
else UF_MSG_WARNING("Invalid CullMode enum string specified: {}", mode);
|
||||
}
|
||||
|
||||
|
||||
::bindTextures( graph, graphic );
|
||||
::bindShaders( graph, entity, mesh, primitives );
|
||||
::bindBuffers( graph, graphic, mesh );
|
||||
@ -1573,9 +1572,11 @@ void uf::graph::process( pod::Graph& graph, int32_t index, uf::Object& parent )
|
||||
metadataLight["shadows"] = graphMetadataJson["lights"]["shadows"].as<bool>();
|
||||
metadataLight["dynamic"] = false;
|
||||
|
||||
/*
|
||||
if ( uf::string::matched( node.name, R"(/\bspot\b/)" ) ) {
|
||||
metadataLight["type"] = "spot";
|
||||
}
|
||||
*/
|
||||
/*
|
||||
if ( ext::json::isArray( graphMetadataJson["lights"]["radius"] ) ) {
|
||||
metadataLight["radius"] = graphMetadataJson["lights"]["radius"];
|
||||
@ -2574,9 +2575,11 @@ void uf::graph::reload( pod::Graph& graph ) {
|
||||
|
||||
uf::image::open( image, pending.buffer, formatHint, false );
|
||||
|
||||
#if UF_USE_OPENGL && !UF_ENV_DREAMCAST
|
||||
if ( key == "lightmap_atlas" ) {
|
||||
::convertLightmap( image );
|
||||
}
|
||||
#endif
|
||||
pending.buffer.clear();
|
||||
}
|
||||
|
||||
|
||||
@ -10,9 +10,16 @@
|
||||
#include <uf/utils/mesh/mesh.h>
|
||||
#include <uf/engine/graph/graph.h>
|
||||
|
||||
#define STATE_COND(...) (::shadowState.invalidated || (__VA_ARGS__))
|
||||
|
||||
namespace {
|
||||
bool matrix_equals( const pod::Matrix4f* a, const pod::Matrix4f* b ) {
|
||||
if ( !a || !b ) return false;
|
||||
return uf::matrix::equals( *a, *b );
|
||||
}
|
||||
struct State {
|
||||
bool initialized = false;
|
||||
bool invalidated = true;
|
||||
|
||||
// matricies
|
||||
const pod::Matrix4f* lastProjPtr = nullptr;
|
||||
@ -42,6 +49,9 @@ namespace {
|
||||
// depth
|
||||
bool depthTestEnabled = false;
|
||||
bool depthWriteEnabled = true;
|
||||
float depthMin = 0.0f;
|
||||
float depthMax = 1.0f;
|
||||
GLenum depthOp = GL_LEQUAL;
|
||||
|
||||
// textures
|
||||
GLuint boundTexture0 = 0;
|
||||
@ -65,11 +75,10 @@ namespace {
|
||||
lastModelPtr = nullptr;
|
||||
modelViewDirty = true;
|
||||
projectionDirty = true;
|
||||
|
||||
blendEnabled = false;
|
||||
alphaTestEnabled = false;
|
||||
alphaCutoff = -1.0f;
|
||||
|
||||
|
||||
color = {1,1,1,1};
|
||||
lineWidth = 1.0f;
|
||||
|
||||
@ -79,6 +88,9 @@ namespace {
|
||||
|
||||
depthTestEnabled = false;
|
||||
depthWriteEnabled = true;
|
||||
depthMin = 0.0f;
|
||||
depthMax = 1.0f;
|
||||
depthOp = GL_LEQUAL;
|
||||
|
||||
boundTexture0 = 0;
|
||||
boundTexture1 = 0;
|
||||
@ -91,30 +103,60 @@ namespace {
|
||||
colorArrayEnabled = false;
|
||||
texCoord0ArrayEnabled = false;
|
||||
texCoord1ArrayEnabled = false;
|
||||
|
||||
/*
|
||||
GL_ERROR_CHECK(glDisable(GL_BLEND));
|
||||
GL_ERROR_CHECK(glDisable(GL_ALPHA_TEST));
|
||||
|
||||
GL_ERROR_CHECK(glDisable(GL_CULL_FACE));
|
||||
GL_ERROR_CHECK(glCullFace(GL_BACK));
|
||||
GL_ERROR_CHECK(glFrontFace(GL_CCW));
|
||||
|
||||
GL_ERROR_CHECK(glDisable(GL_DEPTH_TEST));
|
||||
GL_ERROR_CHECK(glDepthMask(GL_TRUE));
|
||||
GL_ERROR_CHECK(glDepthRange(0.0f, 1.0f));
|
||||
GL_ERROR_CHECK(glDepthFunc(GL_LEQUAL));
|
||||
|
||||
GL_ERROR_CHECK(glColor4f(1.0f, 1.0f, 1.0f, 1.0f));
|
||||
GL_ERROR_CHECK(glLineWidth(1.0f));
|
||||
|
||||
GL_ERROR_CHECK(glActiveTexture(GL_TEXTURE0));
|
||||
GL_ERROR_CHECK(glClientActiveTexture(GL_TEXTURE0));
|
||||
GL_ERROR_CHECK(glDisable(GL_TEXTURE_2D));
|
||||
GL_ERROR_CHECK(glBindTexture(GL_TEXTURE_2D, 0));
|
||||
GL_ERROR_CHECK(glDisableClientState(GL_TEXTURE_COORD_ARRAY));
|
||||
|
||||
GL_ERROR_CHECK(glActiveTexture(GL_TEXTURE1));
|
||||
GL_ERROR_CHECK(glClientActiveTexture(GL_TEXTURE1));
|
||||
GL_ERROR_CHECK(glDisable(GL_TEXTURE_2D));
|
||||
GL_ERROR_CHECK(glBindTexture(GL_TEXTURE_2D, 0));
|
||||
GL_ERROR_CHECK(glDisableClientState(GL_TEXTURE_COORD_ARRAY));
|
||||
|
||||
GL_ERROR_CHECK(glDisableClientState(GL_NORMAL_ARRAY));
|
||||
GL_ERROR_CHECK(glDisableClientState(GL_COLOR_ARRAY));
|
||||
GL_ERROR_CHECK(glDisableClientState(GL_VERTEX_ARRAY));
|
||||
*/
|
||||
}
|
||||
|
||||
void update( const ext::opengl::CommandBuffer::InfoDraw::Matrices& matrices ) {
|
||||
bool projectionChanged = lastProjPtr != matrices.projection;
|
||||
bool viewChanged = lastViewPtr != matrices.view;
|
||||
bool modelChanged = lastModelPtr != matrices.model;
|
||||
pod::Matrix4f inProj = matrices.projection ? *matrices.projection : uf::matrix::identity();
|
||||
pod::Matrix4f inView = matrices.view ? *matrices.view : uf::matrix::identity();
|
||||
pod::Matrix4f inModel = matrices.model ? *matrices.model : uf::matrix::identity();
|
||||
|
||||
if ( projectionChanged || viewChanged || modelChanged ) {
|
||||
bool projectionChanged = !uf::matrix::equals(projection, inProj);
|
||||
|
||||
pod::Matrix4f inModelView = inView * inModel;
|
||||
bool modelViewChanged = !uf::matrix::equals(modelView, inModelView);
|
||||
|
||||
if ( projectionChanged || modelViewChanged ) {
|
||||
if ( projectionChanged ) {
|
||||
projectionDirty = true;
|
||||
projection = matrices.projection ? *matrices.projection : uf::matrix::identity();
|
||||
lastProjPtr = matrices.projection;
|
||||
projection = inProj;
|
||||
}
|
||||
|
||||
if ( viewChanged || modelChanged ) {
|
||||
if ( modelViewChanged ) {
|
||||
modelViewDirty = true;
|
||||
|
||||
pod::Matrix4f view = matrices.view ? *matrices.view : uf::matrix::identity();
|
||||
pod::Matrix4f model = matrices.model ? *matrices.model : uf::matrix::identity();
|
||||
|
||||
modelView = view * model;
|
||||
|
||||
lastViewPtr = matrices.view;
|
||||
lastModelPtr = matrices.model;
|
||||
modelView = inModelView;
|
||||
}
|
||||
|
||||
ndc = projection * modelView;
|
||||
@ -284,6 +326,10 @@ void ext::opengl::CommandBuffer::submit() {
|
||||
switch ( header->type ) {
|
||||
case ext::opengl::enums::Command::CLEAR: {
|
||||
InfoClear* info = (InfoClear*) header;
|
||||
if ( !::shadowState.depthWriteEnabled ) {
|
||||
GL_ERROR_CHECK(glDepthMask(GL_TRUE));
|
||||
::shadowState.depthWriteEnabled = true;
|
||||
}
|
||||
GL_ERROR_CHECK(glClearColor(info->color[0], info->color[1], info->color[2], info->color[3]));
|
||||
GL_ERROR_CHECK(glClearDepth(info->depth));
|
||||
GL_ERROR_CHECK(glClear(info->bits));
|
||||
@ -301,11 +347,13 @@ void ext::opengl::CommandBuffer::submit() {
|
||||
InfoDraw* info = (InfoDraw*) header;
|
||||
::shadowState.update( info->matrices );
|
||||
|
||||
/*
|
||||
if ( ext::opengl::settings::pipelines::culling && info->attributes.instance.pointer && info->attributes.instance.length == sizeof(pod::Instance) ) {
|
||||
const pod::Instance& instance = *(pod::Instance*) info->attributes.instance.pointer;
|
||||
|
||||
if ( !::inside( instance, ::shadowState.ndc ) ) continue;
|
||||
}
|
||||
*/
|
||||
|
||||
drawIndexed( *info );
|
||||
} break;
|
||||
@ -352,7 +400,7 @@ size_t ext::opengl::CommandBuffer::size() const {
|
||||
}
|
||||
|
||||
pod::Matrix4f ext::opengl::CommandBuffer::bindUniform( const ext::opengl::Buffer::Descriptor& descriptor ) {
|
||||
#if UF_USE_OPENGL_FIXED_FUNCTION
|
||||
#if 0 && UF_USE_OPENGL_FIXED_FUNCTION
|
||||
pod::Uniform* uniform = (pod::Uniform*) ((uint8_t*) device->getBuffer( descriptor.buffer ) + descriptor.offset);
|
||||
|
||||
GL_ERROR_CHECK(glMatrixMode(GL_MODELVIEW));
|
||||
@ -363,6 +411,7 @@ pod::Matrix4f ext::opengl::CommandBuffer::bindUniform( const ext::opengl::Buffer
|
||||
|
||||
return uniform->projection * uniform->modelView;
|
||||
#endif
|
||||
return uf::matrix::identity();
|
||||
}
|
||||
|
||||
void ext::opengl::CommandBuffer::drawIndexed( const ext::opengl::CommandBuffer::InfoDraw& drawInfo ) {
|
||||
@ -375,20 +424,9 @@ void ext::opengl::CommandBuffer::drawIndexed( const ext::opengl::CommandBuffer::
|
||||
if ( ::shadowState.projectionDirty ) {
|
||||
GL_ERROR_CHECK(glMatrixMode(GL_PROJECTION));
|
||||
GL_ERROR_CHECK(glLoadMatrixf( &::shadowState.projection[0] ));
|
||||
::shadowState.projectionDirty = false;
|
||||
::shadowState.projectionDirty = false;
|
||||
}
|
||||
|
||||
#if UF_ENV_DREAMCAST
|
||||
// washingtondc ~~has~~ had a regression where non-alpha-tested polys do not render
|
||||
// more convenient to just work around the regression since later builds have working opengl backends
|
||||
GL_ERROR_CHECK(glEnable(GL_ALPHA_TEST));
|
||||
GL_ERROR_CHECK(glAlphaFunc(GL_GREATER, drawInfo.blend.alphaCutoff));
|
||||
|
||||
GL_ERROR_CHECK(glEnable(GL_BLEND));
|
||||
GL_ERROR_CHECK(glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));
|
||||
|
||||
GL_ERROR_CHECK(glDisable(GL_DEPTH_TEST));
|
||||
#else
|
||||
if ( drawInfo.blend.modeAlpha > 0 ) {
|
||||
if ( !::shadowState.alphaTestEnabled ) {
|
||||
GL_ERROR_CHECK(glEnable(GL_ALPHA_TEST));
|
||||
@ -413,7 +451,6 @@ void ext::opengl::CommandBuffer::drawIndexed( const ext::opengl::CommandBuffer::
|
||||
::shadowState.blendEnabled = false;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
if ( drawInfo.descriptor.cullMode != GL_NONE ) {
|
||||
if ( !::shadowState.cullEnabled ) {
|
||||
@ -435,6 +472,16 @@ void ext::opengl::CommandBuffer::drawIndexed( const ext::opengl::CommandBuffer::
|
||||
}
|
||||
}
|
||||
|
||||
if ( drawInfo.descriptor.depth.min != ::shadowState.depthMin || drawInfo.descriptor.depth.max != ::shadowState.depthMax ) {
|
||||
GL_ERROR_CHECK(glDepthRange(drawInfo.descriptor.depth.min, drawInfo.descriptor.depth.max));
|
||||
::shadowState.depthMin = drawInfo.descriptor.depth.min;
|
||||
::shadowState.depthMax = drawInfo.descriptor.depth.max;
|
||||
}
|
||||
if ( drawInfo.descriptor.depth.operation != ::shadowState.depthOp ) {
|
||||
GL_ERROR_CHECK(glDepthFunc(drawInfo.descriptor.depth.operation));
|
||||
::shadowState.depthOp = drawInfo.descriptor.depth.operation;
|
||||
}
|
||||
|
||||
if ( drawInfo.descriptor.depth.test != ::shadowState.depthTestEnabled ) {
|
||||
if ( drawInfo.descriptor.depth.test ) {
|
||||
GL_ERROR_CHECK(glEnable(GL_DEPTH_TEST));
|
||||
@ -450,12 +497,10 @@ void ext::opengl::CommandBuffer::drawIndexed( const ext::opengl::CommandBuffer::
|
||||
}
|
||||
|
||||
pod::Vector4f color = {1,1,1,1};
|
||||
/*
|
||||
if ( drawInfo.color.enabled ) {
|
||||
color = drawInfo.color.pointer ? *drawInfo.color.pointer : drawInfo.color.value;
|
||||
color = uf::vector::clamp( color, pod::Vector4f{0,0,0,0}, pod::Vector4f{1, 1, 1, 1} );
|
||||
}
|
||||
*/
|
||||
if ( color != ::shadowState.color ) {
|
||||
GL_ERROR_CHECK(glColor4f( color[0], color[1], color[2], color[3] ));
|
||||
::shadowState.color = color;
|
||||
@ -537,6 +582,7 @@ void ext::opengl::CommandBuffer::drawIndexed( const ext::opengl::CommandBuffer::
|
||||
uint8_t* uvPtr = drawInfo.attributes.uv.pointer ? (static_cast<uint8_t*>(drawInfo.attributes.uv.pointer) + drawInfo.attributes.uv.stride * drawInfo.descriptor.inputs.vertex.first) : NULL;
|
||||
uint8_t* stPtr = drawInfo.attributes.st.pointer ? (static_cast<uint8_t*>(drawInfo.attributes.st.pointer) + drawInfo.attributes.st.stride * drawInfo.descriptor.inputs.vertex.first) : NULL;
|
||||
uint8_t* vertexPtr = drawInfo.attributes.position.pointer ? (static_cast<uint8_t*>(drawInfo.attributes.position.pointer) + drawInfo.attributes.position.stride * drawInfo.descriptor.inputs.vertex.first) : NULL;
|
||||
uint8_t* indicesPtr = drawInfo.attributes.index.pointer ? (static_cast<uint8_t*>(drawInfo.attributes.index.pointer) + drawInfo.attributes.index.stride * drawInfo.descriptor.inputs.index.first) : NULL;
|
||||
|
||||
auto vertexStride = drawInfo.attributes.position.stride;
|
||||
auto normalStride = drawInfo.attributes.normal.stride;
|
||||
@ -605,9 +651,7 @@ void ext::opengl::CommandBuffer::drawIndexed( const ext::opengl::CommandBuffer::
|
||||
}
|
||||
|
||||
GL_ERROR_CHECK(glTexCoordPointer(2, uvType, uvStride, uvPtr));
|
||||
//GL_ERROR_CHECK(glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, drawInfo.attributes.color.pointer ? GL_MODULATE : GL_REPLACE));
|
||||
// washdc works fine, flycast renders nothing if GL_MODULATE is set, although GL_REPLACE just renders the background
|
||||
GL_ERROR_CHECK(glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE));
|
||||
GL_ERROR_CHECK(glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, drawInfo.attributes.color.pointer ? GL_MODULATE : GL_REPLACE));
|
||||
} else {
|
||||
if ( ::shadowState.tex0Enabled ) {
|
||||
GL_ERROR_CHECK(glClientActiveTexture(GL_TEXTURE0));
|
||||
@ -679,7 +723,7 @@ void ext::opengl::CommandBuffer::drawIndexed( const ext::opengl::CommandBuffer::
|
||||
}
|
||||
|
||||
if ( drawInfo.descriptor.inputs.index.count ) {
|
||||
GL_ERROR_CHECK(glDrawElements(mode, drawInfo.descriptor.inputs.index.count, indicesType, (static_cast<uint8_t*>(drawInfo.attributes.index.pointer) + drawInfo.attributes.index.stride * drawInfo.descriptor.inputs.index.first)));
|
||||
GL_ERROR_CHECK(glDrawElements(mode, drawInfo.descriptor.inputs.index.count, indicesType, indicesPtr));
|
||||
} else {
|
||||
GL_ERROR_CHECK(glDrawArrays(mode, drawInfo.descriptor.inputs.vertex.first, drawInfo.descriptor.inputs.vertex.count));
|
||||
}
|
||||
|
||||
@ -351,9 +351,15 @@ void ext::opengl::Graphic::record( CommandBuffer& commandBuffer, const GraphicDe
|
||||
for ( auto shader : shaders ) {
|
||||
// bind aliased buffers
|
||||
for ( auto& descriptor : shader->metadata.aliases.buffers ) {
|
||||
auto matches = uf::string::match(descriptor.name, R"(/^(.+?)\[(\d+)\]$/)");
|
||||
auto name = matches.size() == 2 ? matches[0] : descriptor.name;
|
||||
auto view = matches.size() == 2 ? stoi(matches[1]) : -1;
|
||||
uf::stl::string name = descriptor.name;
|
||||
int view = -1;
|
||||
size_t bOpen = name.find_last_of('[');
|
||||
size_t bClose = name.find_last_of(']');
|
||||
|
||||
if ( bOpen != uf::stl::string::npos && bClose != uf::stl::string::npos && bClose > bOpen ) {
|
||||
view = std::stoi(name.substr(bOpen + 1, bClose - bOpen - 1));
|
||||
name = name.substr(0, bOpen);
|
||||
}
|
||||
const ext::opengl::Buffer* buffer = &descriptor.fallback;
|
||||
if ( descriptor.renderMode ) {
|
||||
if ( descriptor.renderMode->hasBuffer(name) )
|
||||
|
||||
@ -499,6 +499,7 @@ void ext::opengl::render(){
|
||||
#endif
|
||||
auto transient = std::move(ext::opengl::device.transient);
|
||||
|
||||
/*
|
||||
{
|
||||
auto& scene = uf::scene::getCurrentScene();
|
||||
auto& sceneMetadata = scene.getComponent<uf::Serializer>();
|
||||
@ -523,6 +524,7 @@ void ext::opengl::render(){
|
||||
GL_ERROR_CHECK(glClearDepth(clearCommandInfo.depth));
|
||||
GL_ERROR_CHECK(glClear(clearCommandInfo.bits));
|
||||
}
|
||||
*/
|
||||
|
||||
for ( auto& renderMode : renderModes ) {
|
||||
if ( !renderMode || !renderMode->execute ) continue;
|
||||
|
||||
@ -63,6 +63,7 @@ void ext::opengl::BaseRenderMode::initialize( Device& device ) {
|
||||
this->width = windowSize.x;
|
||||
this->height = windowSize.y;
|
||||
|
||||
/*
|
||||
glDepthRange(0, 1);
|
||||
if ( uf::matrix::reverseInfiniteProjection ) {
|
||||
GL_ERROR_CHECK(glDepthFunc(GL_GEQUAL));
|
||||
@ -74,6 +75,7 @@ void ext::opengl::BaseRenderMode::initialize( Device& device ) {
|
||||
|
||||
GL_ERROR_CHECK(glDisable(GL_LIGHTING));
|
||||
GL_ERROR_CHECK(glDisable(GL_COLOR_MATERIAL));
|
||||
*/
|
||||
|
||||
#if 0
|
||||
GL_ERROR_CHECK(glEnable(GL_ALPHA_TEST));
|
||||
|
||||
@ -31,17 +31,48 @@ void ext::opengl::DeferredRenderMode::render() {
|
||||
void ext::opengl::DeferredRenderMode::destroy() {
|
||||
ext::opengl::RenderMode::destroy();
|
||||
}
|
||||
ext::opengl::GraphicDescriptor ext::opengl::DeferredRenderMode::bindGraphicDescriptor( const ext::opengl::GraphicDescriptor& reference, size_t pass ) {
|
||||
ext::opengl::GraphicDescriptor descriptor = ext::opengl::RenderMode::bindGraphicDescriptor(reference, pass);
|
||||
if ( descriptor.renderMode != "" ) descriptor.invalidated = true;
|
||||
if ( metadata.json["reverse depth"].as<bool>(uf::matrix::reverseInfiniteProjection) ) {
|
||||
descriptor.depth.operation = ext::opengl::enums::Compare::GREATER_OR_EQUAL;
|
||||
descriptor.depth.min = 1.0f;
|
||||
descriptor.depth.max = 0.0f;
|
||||
} else {
|
||||
descriptor.depth.operation = ext::opengl::enums::Compare::LESS;
|
||||
descriptor.depth.min = 0.0f;
|
||||
descriptor.depth.max = 1.0f;
|
||||
}
|
||||
return descriptor;
|
||||
}
|
||||
void ext::opengl::DeferredRenderMode::createCommandBuffers( const uf::stl::vector<ext::opengl::Graphic*>& graphics ) {
|
||||
float width = this->width > 0 ? this->width : ext::opengl::settings::width;
|
||||
float height = this->height > 0 ? this->height : ext::opengl::settings::height;
|
||||
|
||||
float depthClear = uf::matrix::reverseInfiniteProjection ? 0.0f : 1.0f;
|
||||
for ( auto graphic : graphics ) {
|
||||
auto descriptor = bindGraphicDescriptor(graphic->descriptor);
|
||||
depthClear = descriptor.depth.max;
|
||||
break;
|
||||
}
|
||||
|
||||
auto& commands = getCommands();
|
||||
commands.start(); {
|
||||
CommandBuffer::InfoViewport viewportCommandInfo = {};
|
||||
viewportCommandInfo.type = enums::Command::VIEWPORT;
|
||||
viewportCommandInfo.corner = pod::Vector2ui{0, 0};
|
||||
viewportCommandInfo.size = pod::Vector2ui{width, height};
|
||||
commands.record(viewportCommandInfo);
|
||||
{
|
||||
CommandBuffer::InfoViewport viewportCommandInfo = {};
|
||||
viewportCommandInfo.type = enums::Command::VIEWPORT;
|
||||
viewportCommandInfo.corner = pod::Vector2ui{0, 0};
|
||||
viewportCommandInfo.size = pod::Vector2ui{width, height};
|
||||
commands.record(viewportCommandInfo);
|
||||
}
|
||||
{
|
||||
CommandBuffer::InfoClear clearCommandInfo = {};
|
||||
clearCommandInfo.type = enums::Command::CLEAR;
|
||||
clearCommandInfo.color = {0.0f, 0.0f, 0.0f, 0.0f};
|
||||
clearCommandInfo.bits = GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT;
|
||||
clearCommandInfo.depth = depthClear;
|
||||
commands.record(clearCommandInfo);
|
||||
}
|
||||
|
||||
size_t currentSubpass = 0;
|
||||
size_t currentPass = 0;
|
||||
@ -58,7 +89,7 @@ void ext::opengl::DeferredRenderMode::createCommandBuffers( const uf::stl::vecto
|
||||
CommandBuffer::InfoClear clearCommandInfo = {};
|
||||
clearCommandInfo.type = enums::Command::CLEAR;
|
||||
clearCommandInfo.bits = GL_DEPTH_BUFFER_BIT;
|
||||
clearCommandInfo.depth = uf::matrix::reverseInfiniteProjection ? 0.0f : 1.0f;
|
||||
clearCommandInfo.depth = depthClear;
|
||||
commands.record(clearCommandInfo);
|
||||
}
|
||||
// draw normal geometry
|
||||
|
||||
@ -50,24 +50,30 @@ void ext::opengl::RenderTargetRenderMode::pipelineBarrier( GLhandle(VkCommandBuf
|
||||
void ext::opengl::RenderTargetRenderMode::createCommandBuffers( const uf::stl::vector<ext::opengl::Graphic*>& graphics ) {
|
||||
float width = this->width > 0 ? this->width : ext::opengl::settings::width;
|
||||
float height = this->height > 0 ? this->height : ext::opengl::settings::height;
|
||||
|
||||
float depthClear = uf::matrix::reverseInfiniteProjection ? 0.0f : 1.0f;
|
||||
for ( auto graphic : graphics ) {
|
||||
auto descriptor = bindGraphicDescriptor(graphic->descriptor);
|
||||
depthClear = descriptor.depth.max;
|
||||
break;
|
||||
}
|
||||
|
||||
auto& commands = getCommands();
|
||||
commands.start(); {
|
||||
#if 0
|
||||
CommandBuffer::InfoClear clearCommandInfo = {};
|
||||
clearCommandInfo.type = enums::Command::CLEAR;
|
||||
clearCommandInfo.color = {0.0f, 0.0f, 0.0f, 0.0f};
|
||||
clearCommandInfo.depth = uf::Camera::USE_REVERSE_INFINITE_PROJECTION ? 0.0f : 1.0f;
|
||||
clearCommandInfo.bits = GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT;
|
||||
commands.record(clearCommandInfo);
|
||||
#endif
|
||||
#if 1
|
||||
if ( !ext::opengl::hasRenderMode("", true) ) {
|
||||
CommandBuffer::InfoClear clearCommandInfo = {};
|
||||
clearCommandInfo.type = enums::Command::CLEAR;
|
||||
clearCommandInfo.color = {0.0f, 0.0f, 0.0f, 0.0f};
|
||||
clearCommandInfo.depth = depthClear;
|
||||
clearCommandInfo.bits = GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT;
|
||||
commands.record(clearCommandInfo);
|
||||
}
|
||||
|
||||
CommandBuffer::InfoViewport viewportCommandInfo = {};
|
||||
viewportCommandInfo.type = enums::Command::VIEWPORT;
|
||||
viewportCommandInfo.corner = pod::Vector2ui{0, 0};
|
||||
viewportCommandInfo.size = pod::Vector2ui{width, height};
|
||||
commands.record(viewportCommandInfo);
|
||||
#endif
|
||||
|
||||
size_t currentSubpass = 0;
|
||||
size_t currentPass = 0;
|
||||
|
||||
@ -657,9 +657,15 @@ void ext::vulkan::DescriptorSets::update( const Graphic& graphic, const GraphicD
|
||||
texture.sampler.descriptor.filter.min = descriptor.filter;
|
||||
texture.sampler.descriptor.filter.mag = descriptor.filter;
|
||||
|
||||
auto matches = uf::string::match(descriptor.name, R"(/^(.+?)\[(\d+)\]$/)");
|
||||
auto name = matches.size() == 2 ? matches[0] : descriptor.name;
|
||||
auto view = matches.size() == 2 ? stoi(matches[1]) : -1;
|
||||
uf::stl::string name = descriptor.name;
|
||||
int view = -1;
|
||||
size_t bOpen = name.find_last_of('[');
|
||||
size_t bClose = name.find_last_of(']');
|
||||
|
||||
if ( bOpen != uf::stl::string::npos && bClose != uf::stl::string::npos && bClose > bOpen ) {
|
||||
view = std::stoi(name.substr(bOpen + 1, bClose - bOpen - 1));
|
||||
name = name.substr(0, bOpen);
|
||||
}
|
||||
const ext::vulkan::RenderTarget::Attachment* attachment = NULL;
|
||||
if ( descriptor.renderMode ) {
|
||||
if ( descriptor.renderMode->hasAttachment(name) )
|
||||
@ -1011,9 +1017,15 @@ PIPELINE_UPDATE_INVALID:
|
||||
void ext::vulkan::DescriptorSets::collectBuffers( const Shader& shader, const RenderMode& renderMode, const Graphic& graphic, const std::function<void(const Buffer&)>& lambda ) const {
|
||||
// add aliased-by-name buffers
|
||||
for ( auto& descriptor : shader.metadata.aliases.buffers ) {
|
||||
auto matches = uf::string::match(descriptor.name, R"(/^(.+?)\[(\d+)\]$/)");
|
||||
auto name = matches.size() == 2 ? matches[0] : descriptor.name;
|
||||
auto view = matches.size() == 2 ? stoi(matches[1]) : -1;
|
||||
uf::stl::string name = descriptor.name;
|
||||
int view = -1;
|
||||
size_t bOpen = name.find_last_of('[');
|
||||
size_t bClose = name.find_last_of(']');
|
||||
|
||||
if ( bOpen != uf::stl::string::npos && bClose != uf::stl::string::npos && bClose > bOpen ) {
|
||||
view = std::stoi(name.substr(bOpen + 1, bClose - bOpen - 1));
|
||||
name = name.substr(0, bOpen);
|
||||
}
|
||||
const ext::vulkan::Buffer* buffer = &descriptor.fallback;
|
||||
if ( descriptor.renderMode ) {
|
||||
if ( descriptor.renderMode->hasBuffer(name) )
|
||||
|
||||
@ -3,7 +3,11 @@
|
||||
#include <uf/utils/renderer/renderer.h>
|
||||
#include <uf/ext/openvr/openvr.h>
|
||||
|
||||
#if UF_USE_OPENGL
|
||||
bool uf::matrix::reverseInfiniteProjection = false;
|
||||
#else
|
||||
bool uf::matrix::reverseInfiniteProjection = true;
|
||||
#endif
|
||||
|
||||
pod::Vector3f uf::camera::eye( const pod::Camera& camera, int i ) {
|
||||
if ( i < 0 ) i = 0;
|
||||
|
||||
@ -40,20 +40,41 @@ void uf::console::initialize() {
|
||||
});
|
||||
|
||||
uf::console::registerCommand("callHook", "Calls a hook, passing the arguments as a JSON object", [&]( const uf::stl::string& arguments )->uf::stl::string{
|
||||
auto match = uf::string::match( arguments, "/^\"?(.+?)\"?(?: (.+?))?$/" );
|
||||
if ( match.empty() ) return "invalid invocation";
|
||||
if ( arguments.empty() ) return "invalid invocation";
|
||||
|
||||
uf::stl::vector<pod::Hook::userdata_t> results;
|
||||
if ( match.size() > 2 ) {
|
||||
ext::json::Value json;
|
||||
ext::json::decode( json, match[2] );
|
||||
uf::stl::string hookName;
|
||||
uf::stl::string jsonArgs;
|
||||
size_t spaceIdx = uf::stl::string::npos;
|
||||
|
||||
results = uf::hooks.call( match[1], json );
|
||||
// quoted hook names
|
||||
if ( arguments[0] == '"' ) {
|
||||
size_t endQuote = arguments.find('"', 1);
|
||||
if ( endQuote != uf::stl::string::npos ) {
|
||||
hookName = arguments.substr(1, endQuote - 1);
|
||||
spaceIdx = arguments.find_first_not_of(' ', endQuote + 1);
|
||||
if ( spaceIdx != uf::stl::string::npos ) jsonArgs = arguments.substr(spaceIdx);
|
||||
}
|
||||
} else {
|
||||
results = uf::hooks.call( match[1] );
|
||||
// unquoted hook names
|
||||
spaceIdx = arguments.find(' ');
|
||||
if ( spaceIdx != uf::stl::string::npos ) {
|
||||
hookName = arguments.substr(0, spaceIdx);
|
||||
size_t nextChar = arguments.find_first_not_of(' ', spaceIdx);
|
||||
if ( nextChar != uf::stl::string::npos ) jsonArgs = arguments.substr(nextChar);
|
||||
} else {
|
||||
hookName = arguments;
|
||||
}
|
||||
}
|
||||
|
||||
uf::stl::vector<pod::Hook::userdata_t> results;
|
||||
if ( !jsonArgs.empty() ) {
|
||||
ext::json::Value json;
|
||||
ext::json::decode( json, jsonArgs );
|
||||
results = uf::hooks.call( hookName, json );
|
||||
} else {
|
||||
results = uf::hooks.call( hookName );
|
||||
}
|
||||
|
||||
// this could probably be its own function
|
||||
uf::stl::string s_result = "";
|
||||
for ( auto i = 0; i < results.size(); ++i ) {
|
||||
auto& res = results[i];
|
||||
@ -62,20 +83,29 @@ void uf::console::initialize() {
|
||||
else s_result += FMT_FORMAT("\n[{}] => Userdata: {}", i, (void*) res);
|
||||
}
|
||||
|
||||
return "Hook executed: " + match[1] + s_result;
|
||||
return "Hook executed: " + hookName + s_result;
|
||||
});
|
||||
|
||||
uf::console::registerCommand("json", "Modifies the gamestate by setting a JSON value", [&]( const uf::stl::string& arguments )->uf::stl::string{
|
||||
auto match = uf::string::match( arguments, "/^(.+?) *= *(.+?)$/" );
|
||||
if ( match.empty() ) {
|
||||
size_t eqPos = arguments.find('=');
|
||||
|
||||
// query
|
||||
if ( eqPos == uf::stl::string::npos ) {
|
||||
uf::Serializer target = uf::config;
|
||||
return ext::json::encode( arguments == "" ? target : target.path( arguments ), {
|
||||
uf::stl::string query = arguments;
|
||||
query.erase(query.find_last_not_of(" \t") + 1);
|
||||
|
||||
return ext::json::encode( query == "" ? target : target.path( query ), {
|
||||
.pretty = true
|
||||
} );
|
||||
}
|
||||
|
||||
uf::stl::string keyString = match[1];
|
||||
uf::stl::string valueString = match[2];
|
||||
|
||||
// set mode
|
||||
uf::stl::string keyString = arguments.substr(0, eqPos);
|
||||
keyString.erase(keyString.find_last_not_of(" \t") + 1);
|
||||
|
||||
uf::stl::string valueString = arguments.substr(eqPos + 1);
|
||||
valueString.erase(0, valueString.find_first_not_of(" \t"));
|
||||
|
||||
uf::Serializer value;
|
||||
value.deserialize(valueString);
|
||||
@ -108,27 +138,41 @@ void uf::console::initialize() {
|
||||
});
|
||||
|
||||
uf::console::registerCommand("entity", "Modifies the gamestate by setting a JSON value for an entity", [&]( const uf::stl::string& arguments )->uf::stl::string{
|
||||
auto match = uf::string::match( arguments, "/^(\\d+)/" );
|
||||
if ( match.empty() ) return "invalid invocation";
|
||||
|
||||
uf::stl::string IDstring = match[1];
|
||||
size_t firstSpace = arguments.find(' ');
|
||||
uf::stl::string IDstring = arguments.substr(0, firstSpace);
|
||||
if ( IDstring.empty() ) return "invalid invocation";
|
||||
|
||||
size_t ID = std::stoi( IDstring );
|
||||
uf::Object* entity = (uf::Object*) uf::Entity::globalFindByUid( ID );
|
||||
if ( !entity ) return "entity not found: " + IDstring;
|
||||
|
||||
entity->callHook( "object:Serialize.%UID%" );
|
||||
auto& metadata = entity->getComponent<uf::Serializer>();
|
||||
|
||||
match = uf::string::match( arguments, "/^\\d+ (.+?) *= *(.+?)$/" );
|
||||
if ( match.empty() ) {
|
||||
match = uf::string::match( arguments, "/^\\d+ (.+?)$/" );
|
||||
// only ID provided
|
||||
if ( firstSpace == uf::stl::string::npos ) {
|
||||
uf::Serializer target = metadata;
|
||||
return ext::json::encode( !match.empty() && match[1] != "" ? target.path( match[1] ) : target, {
|
||||
.pretty = true
|
||||
} );
|
||||
return ext::json::encode( target, { .pretty = true } );
|
||||
}
|
||||
|
||||
uf::stl::string keyString = match[1];
|
||||
uf::stl::string valueString = match[2];
|
||||
uf::stl::string remainder = arguments.substr(firstSpace + 1);
|
||||
remainder.erase(0, remainder.find_first_not_of(" \t"));
|
||||
|
||||
size_t eqPos = remainder.find('=');
|
||||
|
||||
// query mode
|
||||
if ( eqPos == uf::stl::string::npos ) {
|
||||
uf::Serializer target = metadata;
|
||||
remainder.erase(remainder.find_last_not_of(" \t") + 1);
|
||||
return ext::json::encode( remainder != "" ? target.path( remainder ) : target, { .pretty = true } );
|
||||
}
|
||||
|
||||
// set mode
|
||||
uf::stl::string keyString = remainder.substr(0, eqPos);
|
||||
keyString.erase(keyString.find_last_not_of(" \t") + 1);
|
||||
|
||||
uf::stl::string valueString = remainder.substr(eqPos + 1);
|
||||
valueString.erase(0, valueString.find_first_not_of(" \t"));
|
||||
|
||||
uf::Serializer value;
|
||||
value.deserialize(valueString);
|
||||
@ -151,10 +195,26 @@ uf::stl::string uf::console::execute( const uf::stl::string& input ) {
|
||||
uf::console::print("> " + input);
|
||||
|
||||
uf::stl::string output;
|
||||
auto match = uf::string::match( input, "/^(.+?)(?: (.+?))?$/" );
|
||||
if ( !match.empty() ) {
|
||||
uf::stl::string command = match[1];
|
||||
uf::stl::string arguments = match.size() > 2 ? match[2] : "";
|
||||
|
||||
size_t firstChar = input.find_first_not_of(" \t");
|
||||
|
||||
if ( firstChar != uf::stl::string::npos ) {
|
||||
size_t spacePos = input.find(' ', firstChar);
|
||||
|
||||
uf::stl::string command;
|
||||
uf::stl::string arguments = "";
|
||||
|
||||
if ( spacePos != uf::stl::string::npos ) {
|
||||
command = input.substr(firstChar, spacePos - firstChar);
|
||||
|
||||
size_t argStart = input.find_first_not_of(" \t", spacePos);
|
||||
if ( argStart != uf::stl::string::npos ) {
|
||||
arguments = input.substr(argStart);
|
||||
}
|
||||
} else {
|
||||
command = input.substr(firstChar);
|
||||
}
|
||||
|
||||
output = uf::console::execute( command, arguments );
|
||||
} else {
|
||||
output = "Unknown command: " + input;
|
||||
|
||||
@ -407,8 +407,8 @@ uf::stl::string uf::io::preferred( const uf::stl::string& filename ) {
|
||||
uf::stl::string preferredPath = uf::string::replace( filename, extension, preferredExtension );
|
||||
// pick it if exists
|
||||
if ( uf::io::exists( preferredPath ) ) return preferredPath;
|
||||
// create cached path (for virtual sources)
|
||||
uf::stl::string cachePath = "cache/" + preferredPath;
|
||||
// prefer cached path
|
||||
uf::stl::string cachePath = "cache://" + preferredPath;
|
||||
// pick it if it exists
|
||||
if ( uf::io::exists( cachePath ) ) return cachePath;
|
||||
|
||||
|
||||
@ -1,5 +1,7 @@
|
||||
#include <uf/utils/io/vfs.h>
|
||||
#include <uf/utils/io/file.h>
|
||||
#include <uf/utils/string/ext.h>
|
||||
#include <uf/utils/string/hash.h>
|
||||
#include <uf/utils/math/hash.h>
|
||||
#include <algorithm>
|
||||
#include <sys/stat.h>
|
||||
@ -12,6 +14,20 @@
|
||||
#endif
|
||||
|
||||
namespace {
|
||||
inline bool valid_prefix( const pod::Mount& mount, const uf::stl::string& prefix, bool e = false ) {
|
||||
if ( e ) {
|
||||
if ( prefix.empty() ) {
|
||||
if ( !mount.prefix.empty() ) return false;
|
||||
} else {
|
||||
if ( mount.prefix != prefix ) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
if ( prefix.empty() && mount.priority < 0 ) return false;
|
||||
return prefix.empty() || mount.prefix == prefix;
|
||||
}
|
||||
|
||||
#if UF_ENV_DREAMCAST
|
||||
void ls_kos( const uf::stl::string& basePath, const uf::stl::string& currentSubDir, const uf::stl::string& extension, bool recursive, uf::stl::vector<uf::stl::string>& files ) {
|
||||
uf::stl::string fullPath = basePath + currentSubDir;
|
||||
@ -40,8 +56,8 @@ namespace {
|
||||
}
|
||||
fs_close(fd);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
bool vfs_exists( pod::Mount& mount, const uf::stl::string& file ) {
|
||||
uf::stl::string path = mount.path + file;
|
||||
#if UF_ENV_DREAMCAST
|
||||
@ -367,10 +383,8 @@ bool uf::vfs::exists( const uf::stl::string& path ) {
|
||||
uf::io::splitUri(path, prefix, relative);
|
||||
|
||||
for ( auto& mount : mounts ) {
|
||||
if ( prefix.empty() && mount.priority < 0 ) continue;
|
||||
if ( prefix.empty() || mount.prefix == prefix ) {
|
||||
if ( mount.exists( mount, relative ) ) return true;
|
||||
}
|
||||
if ( !::valid_prefix( mount, prefix ) ) continue;
|
||||
if ( mount.exists( mount, relative ) ) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@ -383,13 +397,10 @@ uf::stl::vector<uf::stl::string> uf::vfs::list( const uf::stl::string& path, con
|
||||
if ( !relative.empty() && relative.back() != '/' ) relative += '/';
|
||||
|
||||
for ( auto& mount : mounts ) {
|
||||
if ( prefix.empty() && mount.priority < 0 ) continue;
|
||||
if ( prefix.empty() || mount.prefix == prefix ) {
|
||||
if ( mount.list ) {
|
||||
auto files = mount.list( mount, relative, extension, recursive );
|
||||
results.insert(results.end(), files.begin(), files.end());
|
||||
}
|
||||
}
|
||||
if ( !::valid_prefix( mount, prefix ) ) continue;
|
||||
if ( !mount.list ) continue;
|
||||
auto files = mount.list( mount, relative, extension, recursive );
|
||||
results.insert(results.end(), files.begin(), files.end());
|
||||
}
|
||||
|
||||
return results;
|
||||
@ -400,10 +411,8 @@ bool uf::vfs::mkdir( const uf::stl::string& path ) {
|
||||
uf::io::splitUri(path, prefix, relative);
|
||||
|
||||
for ( auto& mount : mounts ) {
|
||||
if ( prefix.empty() && mount.priority < 0 ) continue;
|
||||
if ( prefix.empty() || mount.prefix == prefix ) {
|
||||
if ( mount.mkdir( mount, relative ) ) return true;
|
||||
}
|
||||
if ( !::valid_prefix( mount, prefix, true ) ) continue;
|
||||
if ( mount.mkdir && mount.mkdir( mount, relative ) ) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@ -413,10 +422,8 @@ size_t uf::vfs::size( const uf::stl::string& path ) {
|
||||
uf::io::splitUri(path, prefix, relative);
|
||||
|
||||
for ( auto& mount : mounts ) {
|
||||
if ( prefix.empty() && mount.priority < 0 ) continue;
|
||||
if ( prefix.empty() || mount.prefix == prefix ) {
|
||||
if ( mount.exists( mount, relative ) ) return mount.size( mount, relative );
|
||||
}
|
||||
if ( !::valid_prefix( mount, prefix ) ) continue;
|
||||
if ( mount.size && mount.exists( mount, relative ) ) return mount.size( mount, relative );
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@ -426,10 +433,8 @@ size_t uf::vfs::mtime( const uf::stl::string& path ) {
|
||||
uf::io::splitUri(path, prefix, relative);
|
||||
|
||||
for ( auto& mount : mounts ) {
|
||||
if ( prefix.empty() && mount.priority < 0 ) continue;
|
||||
if ( prefix.empty() || mount.prefix == prefix ) {
|
||||
if ( mount.exists( mount, relative ) ) return mount.mtime( mount, relative );
|
||||
}
|
||||
if ( !::valid_prefix( mount, prefix ) ) continue;
|
||||
if ( mount.mtime && mount.exists( mount, relative ) ) return mount.mtime( mount, relative );
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@ -439,11 +444,8 @@ bool uf::vfs::read( const uf::stl::string& path, uf::stl::vector<uint8_t>& buffe
|
||||
uf::io::splitUri(path, prefix, relative);
|
||||
|
||||
for ( auto& mount : mounts ) {
|
||||
if ( prefix.empty() && mount.priority < 0 ) continue;
|
||||
if ( prefix.empty() || mount.prefix == prefix ) {
|
||||
bool res = mount.exists( mount, relative );
|
||||
if ( mount.exists( mount, relative ) && mount.read( mount, relative, buffer ) ) return true;;
|
||||
}
|
||||
if ( !::valid_prefix( mount, prefix ) ) continue;
|
||||
if ( mount.exists( mount, relative ) && mount.read( mount, relative, buffer ) ) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@ -453,10 +455,10 @@ size_t uf::vfs::write( const uf::stl::string& path, const void* buffer, size_t s
|
||||
uf::io::splitUri(path, prefix, relative);
|
||||
|
||||
for ( auto& mount : mounts ) {
|
||||
if ( prefix.empty() && mount.priority < 0 ) continue;
|
||||
if ( prefix.empty() || mount.prefix == prefix ) {
|
||||
if ( mount.write && mount.write( mount, relative, buffer, size ) ) return true;
|
||||
}
|
||||
if ( !::valid_prefix( mount, prefix, true ) ) continue;
|
||||
if ( !mount.write ) continue;
|
||||
size_t written = mount.write( mount, relative, buffer, size );
|
||||
if ( written > 0 ) return written;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@ -469,24 +471,22 @@ bool uf::vfs::readRange( const uf::stl::string& path, size_t start, size_t len,
|
||||
uf::stl::string prefix, relative;
|
||||
uf::io::splitUri(path, prefix, relative);
|
||||
for ( auto& mount : mounts ) {
|
||||
if ( prefix.empty() && mount.priority < 0 ) continue;
|
||||
if ( prefix.empty() || mount.prefix == prefix ) {
|
||||
if ( !mount.exists( mount, relative ) ) continue;
|
||||
if ( mount.readRange ) return mount.readRange( mount, relative, start, len, buffer );
|
||||
if ( !mount.read ) continue;
|
||||
|
||||
uf::stl::vector<uint8_t> fullBuffer;
|
||||
if ( !mount.read( mount, relative, fullBuffer ) ) continue;
|
||||
//UF_MSG_DEBUG("hitting fallback: {}", path);
|
||||
|
||||
if ( start < fullBuffer.size() ) {
|
||||
size_t actualLen = std::min(len, fullBuffer.size() - start);
|
||||
buffer.assign(fullBuffer.begin() + start, fullBuffer.begin() + start + actualLen);
|
||||
} else {
|
||||
buffer.clear();
|
||||
}
|
||||
return true;
|
||||
if ( !::valid_prefix( mount, prefix ) ) continue;
|
||||
if ( !mount.exists( mount, relative ) ) continue;
|
||||
if ( mount.readRange ) return mount.readRange( mount, relative, start, len, buffer );
|
||||
if ( !mount.read ) continue;
|
||||
|
||||
uf::stl::vector<uint8_t> fullBuffer;
|
||||
if ( !mount.read( mount, relative, fullBuffer ) ) continue;
|
||||
//UF_MSG_DEBUG("hitting fallback: {}", path);
|
||||
|
||||
if ( start < fullBuffer.size() ) {
|
||||
size_t actualLen = std::min(len, fullBuffer.size() - start);
|
||||
buffer.assign(fullBuffer.begin() + start, fullBuffer.begin() + start + actualLen);
|
||||
} else {
|
||||
buffer.clear();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@ -495,34 +495,32 @@ bool uf::vfs::readRanges( const uf::stl::string& path, const uf::stl::vector<pod
|
||||
uf::stl::string prefix, relative;
|
||||
uf::io::splitUri(path, prefix, relative);
|
||||
for ( auto& mount : mounts ) {
|
||||
if ( prefix.empty() && mount.priority < 0 ) continue;
|
||||
if ( prefix.empty() || mount.prefix == prefix ) {
|
||||
if ( !mount.exists( mount, relative ) ) continue;
|
||||
if ( mount.readRanges ) return mount.readRanges( mount, relative, ranges, buffer );
|
||||
if ( !mount.read ) continue;
|
||||
|
||||
uf::stl::vector<uint8_t> fullBuffer;
|
||||
if ( !mount.read( mount, relative, fullBuffer ) ) continue;
|
||||
//UF_MSG_DEBUG("hitting fallback: {}", path);
|
||||
if ( !::valid_prefix( mount, prefix ) ) continue;
|
||||
if ( !mount.exists( mount, relative ) ) continue;
|
||||
if ( mount.readRanges ) return mount.readRanges( mount, relative, ranges, buffer );
|
||||
if ( !mount.read ) continue;
|
||||
|
||||
uf::stl::vector<uint8_t> fullBuffer;
|
||||
if ( !mount.read( mount, relative, fullBuffer ) ) continue;
|
||||
//UF_MSG_DEBUG("hitting fallback: {}", path);
|
||||
|
||||
size_t totalBytes = 0;
|
||||
for ( const auto& r : ranges ) {
|
||||
if ( r.start < fullBuffer.size() ) {
|
||||
totalBytes += std::min(r.len, fullBuffer.size() - r.start);
|
||||
}
|
||||
size_t totalBytes = 0;
|
||||
for ( const auto& r : ranges ) {
|
||||
if ( r.start < fullBuffer.size() ) {
|
||||
totalBytes += std::min(r.len, fullBuffer.size() - r.start);
|
||||
}
|
||||
|
||||
buffer.clear();
|
||||
buffer.reserve(totalBytes);
|
||||
|
||||
for ( const auto& r : ranges ) {
|
||||
if ( r.start < fullBuffer.size() ) {
|
||||
size_t actualLen = std::min(r.len, fullBuffer.size() - r.start);
|
||||
buffer.insert(buffer.end(), fullBuffer.begin() + r.start, fullBuffer.begin() + r.start + actualLen);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
buffer.clear();
|
||||
buffer.reserve(totalBytes);
|
||||
|
||||
for ( const auto& r : ranges ) {
|
||||
if ( r.start < fullBuffer.size() ) {
|
||||
size_t actualLen = std::min(r.len, fullBuffer.size() - r.start);
|
||||
buffer.insert(buffer.end(), fullBuffer.begin() + r.start, fullBuffer.begin() + r.start + actualLen);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@ -530,28 +528,26 @@ bool uf::vfs::stream( const uf::stl::string& path, size_t chunkSize, std::functi
|
||||
uf::stl::string prefix, relative;
|
||||
uf::io::splitUri(path, prefix, relative);
|
||||
for ( auto& mount : mounts ) {
|
||||
if ( prefix.empty() && mount.priority < 0 ) continue;
|
||||
if ( prefix.empty() || mount.prefix == prefix ) {
|
||||
if ( !mount.exists( mount, relative ) ) continue;
|
||||
if ( mount.stream ) return mount.stream( mount, relative, chunkSize, callback );
|
||||
if ( !::valid_prefix( mount, prefix ) ) continue;
|
||||
if ( !mount.exists( mount, relative ) ) continue;
|
||||
if ( mount.stream ) return mount.stream( mount, relative, chunkSize, callback );
|
||||
|
||||
if ( !mount.read ) continue;
|
||||
//UF_MSG_DEBUG("hitting fallback: {}", path);
|
||||
if ( !mount.read ) continue;
|
||||
//UF_MSG_DEBUG("hitting fallback: {}", path);
|
||||
|
||||
uf::stl::vector<uint8_t> fullBuffer;
|
||||
if ( !mount.read( mount, relative, fullBuffer ) ) continue;
|
||||
uf::stl::vector<uint8_t> fullBuffer;
|
||||
if ( !mount.read( mount, relative, fullBuffer ) ) continue;
|
||||
|
||||
size_t offset = 0;
|
||||
size_t totalSize = fullBuffer.size();
|
||||
size_t offset = 0;
|
||||
size_t totalSize = fullBuffer.size();
|
||||
|
||||
while ( offset < totalSize ) {
|
||||
size_t currentChunkSize = std::min(chunkSize, totalSize - offset);
|
||||
if ( !callback(fullBuffer.data() + offset, currentChunkSize) ) break;
|
||||
offset += currentChunkSize;
|
||||
}
|
||||
|
||||
return true;
|
||||
while ( offset < totalSize ) {
|
||||
size_t currentChunkSize = std::min(chunkSize, totalSize - offset);
|
||||
if ( !callback(fullBuffer.data() + offset, currentChunkSize) ) break;
|
||||
offset += currentChunkSize;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@ -561,31 +557,28 @@ pod::File uf::vfs::open( const uf::stl::string& path ) {
|
||||
uf::io::splitUri(path, prefix, relative);
|
||||
|
||||
for ( auto& mount : mounts ) {
|
||||
if ( prefix.empty() && mount.priority < 0 ) continue;
|
||||
if ( prefix.empty() || mount.prefix == prefix ) {
|
||||
if ( mount.open ) {
|
||||
pod::File file = mount.open( mount, relative );
|
||||
if ( file ) return file;
|
||||
}
|
||||
if ( mount.exists && mount.exists(mount, relative) ) {
|
||||
if ( mount.readRange && mount.size ) {
|
||||
FallbackFileState* state = new FallbackFileState{
|
||||
&mount,
|
||||
relative,
|
||||
0,
|
||||
mount.size(mount, relative)
|
||||
};
|
||||
|
||||
return pod::File{
|
||||
.handle = state,
|
||||
.read = fallback_file_read,
|
||||
.seek = fallback_file_seek,
|
||||
.tell = fallback_file_tell,
|
||||
.close = fallback_file_close
|
||||
};
|
||||
}
|
||||
}
|
||||
if ( !::valid_prefix( mount, prefix ) ) continue;
|
||||
if ( mount.open ) {
|
||||
pod::File file = mount.open( mount, relative );
|
||||
if ( file ) return file;
|
||||
}
|
||||
if ( !mount.exists || !mount.exists(mount, relative) ) continue;
|
||||
if ( !mount.readRange || !mount.size ) continue;
|
||||
|
||||
FallbackFileState* state = new FallbackFileState{
|
||||
&mount,
|
||||
relative,
|
||||
0,
|
||||
mount.size(mount, relative)
|
||||
};
|
||||
|
||||
return pod::File{
|
||||
.handle = state,
|
||||
.read = fallback_file_read,
|
||||
.seek = fallback_file_seek,
|
||||
.tell = fallback_file_tell,
|
||||
.close = fallback_file_close
|
||||
};
|
||||
}
|
||||
return pod::File{};
|
||||
}
|
||||
@ -596,7 +589,7 @@ uf::stl::string uf::vfs::resolveBase( const uf::stl::string& path ) {
|
||||
|
||||
for ( auto& mount : mounts ) {
|
||||
if ( prefix.empty() && mount.priority < 0 ) continue;
|
||||
if ( prefix.empty() || mount.prefix == prefix ) {
|
||||
if ( ::valid_prefix( mount, prefix ) ) {
|
||||
uf::stl::string resolved = mount.path + relative;
|
||||
return resolved;
|
||||
}
|
||||
@ -611,7 +604,7 @@ bool uf::vfs::isPhysical( const uf::stl::string& path ) {
|
||||
|
||||
for ( const auto& mount : mounts ) {
|
||||
if ( prefix.empty() && mount.priority < 0 ) continue;
|
||||
if ( prefix.empty() || mount.prefix == prefix ) {
|
||||
if ( ::valid_prefix( mount, prefix ) ) {
|
||||
return mount.exists == ::vfs_exists;
|
||||
}
|
||||
}
|
||||
@ -625,6 +618,7 @@ bool uf::vfs::isPhysical( const uf::stl::string& path ) {
|
||||
#endif
|
||||
|
||||
UF_VFS_MOUNT_CPP( uf::vfs::createDiskMount, "sys://", 999 );
|
||||
|
||||
UF_VFS_MOUNT_CPP( uf::vfs::createDiskMount, "mdl://" VFS_BASE "models", 100 );
|
||||
UF_VFS_MOUNT_CPP( uf::vfs::createDiskMount, "scene://" VFS_BASE "scenes", 100 );
|
||||
UF_VFS_MOUNT_CPP( uf::vfs::createDiskMount, "ent://" VFS_BASE "entities", 100 );
|
||||
@ -633,4 +627,111 @@ UF_VFS_MOUNT_CPP( uf::vfs::createDiskMount, "snd://" VFS_BASE "audio", 100 );
|
||||
UF_VFS_MOUNT_CPP( uf::vfs::createDiskMount, "spv://" VFS_BASE "shaders", 100 );
|
||||
UF_VFS_MOUNT_CPP( uf::vfs::createDiskMount, "lua://" VFS_BASE "scripts", 100 );
|
||||
UF_VFS_MOUNT_CPP( uf::vfs::createDiskMount, "data://" VFS_BASE, 50 );
|
||||
UF_VFS_MOUNT_CPP( uf::vfs::createDiskMount, "", 0 );
|
||||
UF_VFS_MOUNT_CPP( uf::vfs::createDiskMount, "", 0 );
|
||||
|
||||
|
||||
namespace {
|
||||
struct Guard {
|
||||
static thread_local bool active;
|
||||
bool owner;
|
||||
|
||||
Guard() {
|
||||
owner = !active;
|
||||
if ( owner ) active = true;
|
||||
}
|
||||
|
||||
~Guard() {
|
||||
if ( owner ) active = false;
|
||||
}
|
||||
|
||||
explicit operator bool() const { return owner; }
|
||||
};
|
||||
thread_local bool Guard::active = false;
|
||||
|
||||
uf::stl::string cached_path( const uf::stl::string& path ) {
|
||||
/*
|
||||
uf::stl::string ext = uf::io::extension( path );
|
||||
uf::stl::string p = uf::io::remove_extension( path );
|
||||
*/
|
||||
size_t last_slash = path.find_last_of('/');
|
||||
size_t name_start = (last_slash == uf::stl::string::npos) ? 0 : last_slash + 1;
|
||||
|
||||
size_t first_dot = path.find('.', name_start);
|
||||
|
||||
uf::stl::string p = path;
|
||||
uf::stl::string ext = "";
|
||||
|
||||
if (first_dot != uf::stl::string::npos) {
|
||||
p = path.substr(0, first_dot);
|
||||
ext = path.substr(first_dot + 1);
|
||||
}
|
||||
|
||||
auto hash = uf::algo::fnv1a( p );
|
||||
|
||||
if ( ext.empty() ) return FMT_FORMAT("$://{}", hash);
|
||||
return FMT_FORMAT("$://{}.{}", hash, ext);
|
||||
}
|
||||
|
||||
bool cache_exists( pod::Mount& mount, const uf::stl::string& file ) {
|
||||
Guard guard; if ( !guard ) return false;
|
||||
|
||||
uf::stl::string cachedUri = cached_path( file );
|
||||
return uf::vfs::exists( cachedUri );
|
||||
}
|
||||
|
||||
size_t cache_size( pod::Mount& mount, const uf::stl::string& file ) {
|
||||
Guard guard; if ( !guard ) return 0;
|
||||
|
||||
uf::stl::string cachedUri = cached_path( file );
|
||||
return uf::vfs::size( cachedUri );
|
||||
}
|
||||
|
||||
size_t cache_mtime(pod::Mount& mount, const uf::stl::string& file) {
|
||||
Guard guard; if ( !guard ) return 0;
|
||||
|
||||
uf::stl::string cachedUri = cached_path( file );
|
||||
return uf::vfs::mtime( cachedUri );
|
||||
}
|
||||
|
||||
bool cache_read(pod::Mount& mount, const uf::stl::string& file, uf::stl::vector<uint8_t>& buffer) {
|
||||
Guard guard; if ( !guard ) return false;
|
||||
|
||||
uf::stl::string cachedUri = cached_path( file );
|
||||
return uf::vfs::read( cachedUri, buffer );
|
||||
}
|
||||
|
||||
size_t cache_write( pod::Mount& mount, const uf::stl::string& file, const void* buffer, size_t size ) {
|
||||
Guard guard; if ( !guard ) return 0;
|
||||
|
||||
uf::stl::string cachedUri = cached_path( file );
|
||||
return uf::vfs::write( cachedUri, buffer, size );
|
||||
}
|
||||
|
||||
pod::File cache_open( pod::Mount& mount, const uf::stl::string& file ) {
|
||||
Guard guard; if ( !guard ) return {};
|
||||
|
||||
uf::stl::string cachedUri = cached_path( file );
|
||||
return uf::vfs::open( cachedUri );
|
||||
}
|
||||
|
||||
pod::Mount createCacheMount( const uf::stl::string& uri, int priority) {
|
||||
uf::stl::string prefix;
|
||||
uf::stl::string path;
|
||||
uf::io::splitUri( uri, prefix, path );
|
||||
|
||||
return pod::Mount{
|
||||
.prefix = prefix,
|
||||
.path = path,
|
||||
.priority = priority,
|
||||
.exists = ::cache_exists,
|
||||
.size = ::cache_size,
|
||||
.mtime = ::cache_mtime,
|
||||
.read = ::cache_read,
|
||||
.write = ::cache_write,
|
||||
.open = ::cache_open,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
UF_VFS_MOUNT_CPP( ::createCacheMount, "cache://", 2000 );
|
||||
UF_VFS_MOUNT_CPP( uf::vfs::createDiskMount, "$://" VFS_BASE "cache", -1000 );
|
||||
|
||||
@ -44,17 +44,32 @@ uf::stl::string uf::Serializer::resolveFilename( const uf::stl::string& filename
|
||||
}
|
||||
uf::stl::string uf::Serializer::resolveFilename( const uf::stl::string& filename, const ext::json::EncodingSettings& settings, bool compareTimes ) {
|
||||
uf::stl::string _filename = filename;
|
||||
|
||||
if ( settings.encoding != "" && settings.encoding != "json" ) {
|
||||
_filename = uf::string::replace( _filename, "/\\.json$/", "." + settings.encoding );
|
||||
if ( uf::io::extension( _filename ) == "json" ) {
|
||||
_filename = uf::io::replace_extension( _filename, settings.encoding );
|
||||
}
|
||||
}
|
||||
if ( settings.compression != "" ) {
|
||||
_filename = _filename + "." + settings.compression;
|
||||
}
|
||||
if ( !compareTimes ) return _filename;
|
||||
|
||||
uf::stl::string cache_filename = _filename;
|
||||
#if !UF_CONVERSION_TO_CACHE
|
||||
if ( !uf::vfs::isPhysical( filename ) )
|
||||
#endif
|
||||
cache_filename = "cache://" + _filename;
|
||||
|
||||
if ( uf::io::exists( cache_filename ) ) {
|
||||
bool should = !uf::io::exists( filename );
|
||||
if ( !should ) should = uf::io::mtime( cache_filename ) >= uf::io::mtime( filename );
|
||||
if ( should ) return cache_filename;
|
||||
}
|
||||
|
||||
if ( uf::io::exists( _filename ) ) {
|
||||
bool should = !uf::io::exists( filename ); // implicit load if our requested filename doesnt exist anyways, but our preferred source does
|
||||
if ( !should ) should = uf::io::mtime( _filename ) >= uf::io::mtime( filename ); // implicit load the preferred source is newer than the requested filename
|
||||
bool should = !uf::io::exists( filename );
|
||||
if ( !should ) should = uf::io::mtime( _filename ) >= uf::io::mtime( filename );
|
||||
if ( should ) return _filename;
|
||||
}
|
||||
return filename;
|
||||
@ -62,8 +77,7 @@ uf::stl::string uf::Serializer::resolveFilename( const uf::stl::string& filename
|
||||
|
||||
bool uf::Serializer::readFromFile( const uf::stl::string& filename, const uf::stl::string& hash ) {
|
||||
#if UF_SERIALIZER_IMPLICIT_LOAD
|
||||
// implicitly check for optimal format for plain .json requests
|
||||
if ( uf::string::matched( filename, "/\\.json$/" ) ) {
|
||||
if ( uf::io::extension( filename ) == "json" ) {
|
||||
#if 0 && UF_USE_TOML
|
||||
uf::stl::string toml_filename = uf::string::replace( filename, "/\\.json$/", ".toml" ); // load from toml if newer
|
||||
if ( uf::io::mtime( toml_filename ) > uf::io::mtime( filename ) ) {
|
||||
@ -87,13 +101,19 @@ bool uf::Serializer::readFromFile( const uf::stl::string& filename, const uf::st
|
||||
}
|
||||
|
||||
ext::json::DecodingSettings settings;
|
||||
if ( uf::string::matched( filename, "/\\.bson/" ) ) settings.encoding = "bson";
|
||||
else if ( uf::string::matched( filename, "/\\.cbor/" ) ) settings.encoding = "cbor";
|
||||
else if ( uf::string::matched( filename, "/\\.msgpack/" ) ) settings.encoding = "msgpack";
|
||||
else if ( uf::string::matched( filename, "/\\.ubjson/" ) ) settings.encoding = "ubjson";
|
||||
else if ( uf::string::matched( filename, "/\\.bjdata/" ) ) settings.encoding = "bjdata";
|
||||
else if ( uf::string::matched( filename, "/\\.toml/" ) ) settings.encoding = "toml";
|
||||
else if ( uf::string::matched( filename, "/\\.json/" ) ) settings.encoding = "json";
|
||||
uf::stl::string ext = uf::io::extension( filename );
|
||||
|
||||
if ( ext == "gz" || ext == "lz4" ) {
|
||||
ext = uf::io::extension( uf::io::remove_extension( filename ) );
|
||||
}
|
||||
|
||||
if ( ext == "bson" ) settings.encoding = "bson";
|
||||
else if ( ext == "cbor" ) settings.encoding = "cbor";
|
||||
else if ( ext == "msgpack" ) settings.encoding = "msgpack";
|
||||
else if ( ext == "ubjson" ) settings.encoding = "ubjson";
|
||||
else if ( ext == "bjdata" ) settings.encoding = "bjdata";
|
||||
else if ( ext == "toml" ) settings.encoding = "toml";
|
||||
else if ( ext == "json" ) settings.encoding = "json";
|
||||
else UF_MSG_DEBUG( "invalid encoding filetype requested: {}", filename );
|
||||
|
||||
this->deserialize( buffer, settings );
|
||||
@ -116,11 +136,9 @@ bool uf::Serializer::readFromFile( const uf::stl::string& filename, const uf::st
|
||||
}
|
||||
}
|
||||
#endif
|
||||
if ( uf::string::matched( filename, "/\\.(json|toml)$/" ) ) {
|
||||
// auto convert read JSON file to TOML
|
||||
|
||||
if ( ext == "json" || ext == "toml" ) {
|
||||
// auto convert read file to preferred filetype
|
||||
if ( ext::json::PREFERRED_COMPRESSION != "" || (ext::json::PREFERRED_ENCODING != "" || ext::json::PREFERRED_ENCODING != "json") ) {
|
||||
if ( ext::json::PREFERRED_COMPRESSION != "" || (ext::json::PREFERRED_ENCODING != "" && ext::json::PREFERRED_ENCODING != "json") ) {
|
||||
ext::json::EncodingSettings _settings;
|
||||
_settings.encoding = ext::json::PREFERRED_ENCODING;
|
||||
_settings.compression = ext::json::PREFERRED_COMPRESSION;
|
||||
@ -128,14 +146,23 @@ bool uf::Serializer::readFromFile( const uf::stl::string& filename, const uf::st
|
||||
|
||||
if ( ext::json::PREFERRED_ENCODING != "" && ext::json::PREFERRED_ENCODING != "json" ) {
|
||||
_settings.encoding = ext::json::PREFERRED_ENCODING;
|
||||
_filename = uf::string::replace( _filename, "/\\.json$/", "." + ext::json::PREFERRED_ENCODING );
|
||||
if ( uf::io::extension(_filename) == "json" ) {
|
||||
_filename = uf::io::replace_extension( _filename, ext::json::PREFERRED_ENCODING );
|
||||
}
|
||||
}
|
||||
if ( ext::json::PREFERRED_COMPRESSION != "" ) {
|
||||
_settings.compression = ext::json::PREFERRED_COMPRESSION;
|
||||
_filename = _filename + "." + ext::json::PREFERRED_COMPRESSION;
|
||||
}
|
||||
bool should = !uf::io::exists( _filename ); // auto convert if preferred file doesn't already exist
|
||||
if ( !should ) should = uf::io::mtime( _filename ) < uf::io::mtime( filename ); // auto convert if preferred file is older than source file
|
||||
|
||||
#if !UF_CONVERSION_TO_CACHE
|
||||
if ( !uf::vfs::isPhysical( filename ) ) _filename = "cache://" + _filename;
|
||||
#else
|
||||
_filename = "cache://" + _filename;
|
||||
#endif
|
||||
|
||||
bool should = !uf::io::exists( _filename );
|
||||
if ( !should ) should = uf::io::mtime( _filename ) < uf::io::mtime( filename );
|
||||
if ( should ) {
|
||||
writeToFile( _filename, _settings );
|
||||
}
|
||||
@ -147,17 +174,20 @@ bool uf::Serializer::readFromFile( const uf::stl::string& filename, const uf::st
|
||||
}
|
||||
bool uf::Serializer::writeToFile( const uf::stl::string& filename, const ext::json::EncodingSettings& settings ) const {
|
||||
uf::stl::string output = filename;
|
||||
|
||||
if ( settings.encoding != "" && settings.encoding != "json" )
|
||||
output = uf::string::replace( output, "/\\.json/", "." + settings.encoding );
|
||||
if ( settings.compression != "" && !uf::string::matched( output, "/."+settings.compression+"/" ) )
|
||||
|
||||
if ( settings.encoding != "" && settings.encoding != "json" ) {
|
||||
output = uf::string::replace( output, ".json", "." + settings.encoding, false );
|
||||
}
|
||||
|
||||
if ( settings.compression != "" && uf::io::extension(output) != settings.compression ) {
|
||||
output += "." + settings.compression;
|
||||
}
|
||||
|
||||
uf::stl::string buffer = this->serialize( settings );
|
||||
size_t written = uf::io::write( output, buffer.data(), buffer.size() );
|
||||
|
||||
#if UF_SERIALIZER_AUTO_CONVERT
|
||||
// implicitly check for optimal format for plain .json requests
|
||||
if ( uf::string::matched( output, "/\\.json$/" ) && settings.compression != ext::json::PREFERRED_COMPRESSION ) {
|
||||
if ( uf::io::extension(output) == "json" && settings.compression != ext::json::PREFERRED_COMPRESSION ) {
|
||||
uf::stl::string _filename = output;
|
||||
auto _settings = settings;
|
||||
|
||||
|
||||
@ -15,7 +15,6 @@ bool uf::string::matched( const uf::stl::string& str, const uf::stl::string& r )
|
||||
|
||||
int match_length;
|
||||
int match_idx = re_match(pattern.c_str(), str.c_str(), &match_length);
|
||||
|
||||
return (match_idx != -1);
|
||||
}
|
||||
|
||||
|
||||
@ -312,16 +312,26 @@ void uf::glyph::generateMesh( const uf::stl::vector<pod::GlyphBox>& layout, cons
|
||||
for ( const auto& g : layout ) {
|
||||
auto hash = FMT_FORMAT( "{}", uf::glyph::hashSettings(g.code, metadata) );
|
||||
|
||||
auto& anchor = g.anchor;
|
||||
#if EXT_COLOR_FLOATS
|
||||
auto& color = g.color;
|
||||
#else
|
||||
pod::Vector4ub color = uf::vector::clamp( g.color, 0, 1 ) * 255;
|
||||
#endif
|
||||
|
||||
/*
|
||||
pod::Vector3f anchor = { g.anchor[0], g.anchor[1], 0.0f };
|
||||
#if EXT_COLOR_FLOATS
|
||||
auto& color = g.color;
|
||||
#else
|
||||
pod::Vector4ub color = {
|
||||
(uint8_t)(g.color[0] * 255),
|
||||
(uint8_t)(g.color[1] * 255),
|
||||
(uint8_t)(g.color[2] * 255),
|
||||
(uint8_t)(g.color[3] * 255)
|
||||
(uint8_t)(CLAMP(g.color[0], 0, 1) * 255),
|
||||
(uint8_t)(CLAMP(g.color[1], 0, 1) * 255),
|
||||
(uint8_t)(CLAMP(g.color[2], 0, 1) * 255),
|
||||
(uint8_t)(CLAMP(g.color[3], 0, 1) * 255)
|
||||
};
|
||||
#endif
|
||||
*/
|
||||
// insert indices
|
||||
uint16_t idx = (uint16_t) vertices.size();
|
||||
indices.insert( indices.end(), { idx, idx + 1, idx + 2, idx, idx + 2, idx + 3 });
|
||||
@ -333,15 +343,15 @@ void uf::glyph::generateMesh( const uf::stl::vector<pod::GlyphBox>& layout, cons
|
||||
auto p3 = pod::Vector2f{ g.box.x + g.box.w, g.box.y + g.box.h };
|
||||
|
||||
#if UF_USE_OPENGL
|
||||
vertices.emplace_back(::GlyphVertex{g.anchor + p0, atlas.mapUv(pod::Vector2f{ 0.0f, 1.0f }, hash), color});
|
||||
vertices.emplace_back(::GlyphVertex{g.anchor + p1, atlas.mapUv(pod::Vector2f{ 0.0f, 0.0f }, hash), color});
|
||||
vertices.emplace_back(::GlyphVertex{g.anchor + p2, atlas.mapUv(pod::Vector2f{ 1.0f, 0.0f }, hash), color});
|
||||
vertices.emplace_back(::GlyphVertex{g.anchor + p3, atlas.mapUv(pod::Vector2f{ 1.0f, 1.0f }, hash), color});
|
||||
vertices.emplace_back(::GlyphVertex{anchor + p0, atlas.mapUv(pod::Vector2f{ 0.0f, 1.0f }, hash), color});
|
||||
vertices.emplace_back(::GlyphVertex{anchor + p1, atlas.mapUv(pod::Vector2f{ 0.0f, 0.0f }, hash), color});
|
||||
vertices.emplace_back(::GlyphVertex{anchor + p2, atlas.mapUv(pod::Vector2f{ 1.0f, 0.0f }, hash), color});
|
||||
vertices.emplace_back(::GlyphVertex{anchor + p3, atlas.mapUv(pod::Vector2f{ 1.0f, 1.0f }, hash), color});
|
||||
#else
|
||||
vertices.emplace_back(::GlyphVertex{g.anchor, atlas.mapUv(pod::Vector2f{ 0.0f, 1.0f }, hash), color, p0});
|
||||
vertices.emplace_back(::GlyphVertex{g.anchor, atlas.mapUv(pod::Vector2f{ 0.0f, 0.0f }, hash), color, p1});
|
||||
vertices.emplace_back(::GlyphVertex{g.anchor, atlas.mapUv(pod::Vector2f{ 1.0f, 0.0f }, hash), color, p2});
|
||||
vertices.emplace_back(::GlyphVertex{g.anchor, atlas.mapUv(pod::Vector2f{ 1.0f, 1.0f }, hash), color, p3});
|
||||
vertices.emplace_back(::GlyphVertex{anchor, atlas.mapUv(pod::Vector2f{ 0.0f, 1.0f }, hash), color, p0});
|
||||
vertices.emplace_back(::GlyphVertex{anchor, atlas.mapUv(pod::Vector2f{ 0.0f, 0.0f }, hash), color, p1});
|
||||
vertices.emplace_back(::GlyphVertex{anchor, atlas.mapUv(pod::Vector2f{ 1.0f, 0.0f }, hash), color, p2});
|
||||
vertices.emplace_back(::GlyphVertex{anchor, atlas.mapUv(pod::Vector2f{ 1.0f, 1.0f }, hash), color, p3});
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user