18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-24 13:09:16 +01:00

Merge branch 'master' into feat/renderers-cleanup

This commit is contained in:
2026-11-23 18:27:27 +01:00
70 changed files with 2865 additions and 378 deletions

View File

@@ -609,6 +609,10 @@ void TAnimModel::RaAnimate( unsigned int const Framestamp ) {
if( Framestamp == m_framestamp ) { return; }
auto const timedelta { Timer::GetDeltaTime() };
// the opacities change step by step from where they are, and they start at 1. the first time the instance is
// drawn its lights take their current state at once, whether it came from the definition or from an event since:
// a fade from 1 showed a light that is off as lit for a moment, the first time it came into view
bool const transition { m_transition && m_lightsanimated };
// interpretacja ułamka zależnie od typu
// case ls_Off: ustalenie czasu migotania, t<1s (f>1Hz), np. 0.1 => t=0.1 (f=10Hz)
@@ -649,12 +653,13 @@ void TAnimModel::RaAnimate( unsigned int const Framestamp ) {
const float transitionontime = std::min(0.25f, std::min(ontime, offtime) * 0.95f);
const float transitionofftime = std::min(0.45f, std::min(ontime, offtime) * 0.95f);
if (on)
opacity += m_transition ? timedelta / transitionontime : 1.f; // increase to max value
opacity = static_cast<float>( opacity + ( transition ? timedelta / transitionontime : 1.0 ) ); // increase to max value
else
opacity -= m_transition ? timedelta / transitionofftime : 1.f; // reduce to zero
opacity = static_cast<float>( opacity - ( transition ? timedelta / transitionofftime : 1.0 ) ); // reduce to zero
// Clamp the opacity
opacity = std::clamp(opacity, 0.f, 1.f);
}
m_lightsanimated = true;
// Ra 2F1I: to by można pomijać dla modeli bez animacji, których jest większość
for (const auto entry : m_animlist) {

View File

@@ -225,6 +225,7 @@ public:
float fOffTime { 1.f / 2 };// { 60.f / 45.f / 2 }; // były stałymi, teraz mogą być zmienne dla każdego egzemplarza
// float fTransitionTime { fOnTime * 0.9f }; // time
bool m_transition { true }; // smooth transition between light states
bool m_lightsanimated { false }; // the lights were brought to their state at least once
unsigned int m_framestamp { 0 }; // id of last rendered gfx frame
// true if this instance is eligible for the batched/instanced render path:

View File

@@ -30,6 +30,7 @@ http://mozilla.org/MPL/2.0/.
#include "winheaders.h"
#include <bit>
#include <utility>
#define EU07_DEFERRED_TEXTURE_UPLOAD
@@ -356,6 +357,7 @@ GLint readback_format( GLint const Internalformat ) {
std::array<GLuint, gl::MAX_TEXTURES + gl::HELPER_TEXTURES> opengl_texture::units = { 0 };
GLint opengl_texture::m_activeunit = -1;
std::atomic<bool> opengl_texture::m_unitcachestale { false };
std::unordered_map<GLint, int> texture_source::precompressed_formats =
{
@@ -655,9 +657,104 @@ void opengl_texture::update_from_memory(size_t width, size_t height, const uint8
is_static = true;
is_ready = true;
}
// the name may be a new one, and the unit active took the texture past the unit cache
// the name may be a new one, and the unit active took the texture past the unit cache. this runs on the python upload
// thread too (python.threadedupload), so the cache is only marked here: clearing it outright raced the drawing thread
// filling it for a multi-bind, and an object got the texture of the one drawn before it for a frame
invalidate_unit_cache();
}
namespace
{
// gl objects of an image uploaded by another thread that was never put in place
void delete_thread_upload(GLuint const Texture, GLsync const Fence)
{
if (Fence != nullptr)
glDeleteSync(Fence);
glDeleteTextures(1, &Texture);
}
} // namespace
void opengl_texture::upload_from_thread(size_t width, size_t height, const uint8_t *raw)
{
// a texture of its own each time: the one drawn from may be in use by the gpu for the drawing context, and writing it from
// here, with nothing to order the two contexts, could leave it incomplete or half written for a frame
texture_thread_upload upload;
upload.width = width;
upload.height = height;
glGenTextures(1, &upload.id);
glBindTexture(target, upload.id);
set_filtering();
glTexParameteri(target, GL_TEXTURE_WRAP_S, wrap_mode_s);
glTexParameteri(target, GL_TEXTURE_WRAP_T, wrap_mode_t);
if (!glGenerateMipmap)
glTexParameteri(target, GL_GENERATE_MIPMAP, GL_TRUE);
glTexImage2D(target, 0, GL_SRGB8_ALPHA8, static_cast<GLsizei>(width), static_cast<GLsizei>(height), 0, GL_RGBA, GL_UNSIGNED_BYTE, raw);
if (glGenerateMipmap)
glGenerateMipmap(target);
glBindTexture(target, 0);
if (glFenceSync)
{
upload.fence = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
}
if (upload.fence == nullptr)
{
// no sync objects, or no fence made: the drawing context can only be sure of the image once the upload is done
glFinish();
}
else
{
// the drawing context asks for the fence, which signals only once it is on its way to the gpu
glFlush();
}
std::optional<texture_thread_upload> superseded;
{
std::scoped_lock lock{m_threaduploadmutex};
superseded = std::exchange(m_threadupload, upload);
}
if (superseded.has_value())
{
// the drawing thread didn't take it, so nothing drew from it
delete_thread_upload(superseded->id, superseded->fence);
}
}
bool opengl_texture::commit_thread_upload()
{
texture_thread_upload upload;
{
std::scoped_lock lock{m_threaduploadmutex};
if (false == m_threadupload.has_value())
return true;
// asked without waiting. the image goes in place only once the upload is done, so every context binding it after
// has it whole: the extra viewports and the windows of python.displaywindows draw in contexts of their own
if (m_threadupload->fence != nullptr)
{
// a failed query (GL_WAIT_FAILED) isn't asked again: the image would never go in place
if (glClientWaitSync(m_threadupload->fence, 0, 0) == GL_TIMEOUT_EXPIRED)
return false;
glDeleteSync(m_threadupload->fence);
}
upload = *m_threadupload;
m_threadupload.reset();
}
if (id != invalid_id)
glDeleteTextures(1, &id);
id = upload.id;
data_width = static_cast<unsigned int>(upload.width);
data_height = static_cast<unsigned int>(upload.height);
data_format = GL_SRGB8_ALPHA8;
data_components = GL_RGBA;
if (type == "internalsrc:")
{
is_static = true;
is_ready = true;
}
// the name deleted may come back for another texture, and a deleted texture isn't bound any more
for (auto &unit : units)
unit = -1;
return true;
}
void
@@ -1190,6 +1287,7 @@ opengl_texture::bind(size_t unit) {
return false;
}
sync_unit_cache();
if (units[unit] == id)
return true;
glBindTextureUnit(unit, id);
@@ -1552,6 +1650,25 @@ opengl_texture::reset_unit_cache() {
m_activeunit = -1;
}
void
opengl_texture::invalidate_unit_cache() {
m_unitcachestale.store( true );
}
void
opengl_texture::sync_unit_cache() {
// a plain load first: bind() calls this for every texture, and a read-modify-write each time would cost more
if( ( false == m_unitcachestale.load() )
|| ( false == m_unitcachestale.exchange( false ) ) ) {
return;
}
for( auto &unit : units ) {
unit = -1;
}
}
void
opengl_texture::set_filtering() const
{

View File

@@ -9,9 +9,11 @@ http://mozilla.org/MPL/2.0/.
#pragma once
#include <atomic>
#include <istream>
#include <chrono>
#include <limits>
#include <mutex>
#include <optional>
#include "winheaders.h"
#include "utilities/memorystats.h"
@@ -162,6 +164,14 @@ protected:
on_failed() {}
};
// an image uploaded by another thread into a texture of its own, waiting for the drawing thread to put it in place
struct texture_thread_upload {
GLuint id { 0 };
GLsync fence { nullptr }; // set after the upload in the uploading context; none where the driver has no sync objects
size_t width { 0 };
size_t height { 0 };
};
// a texture of the opengl renderer
struct opengl_texture : public texture_source {
// constructors
@@ -180,6 +190,13 @@ struct opengl_texture : public texture_source {
alloc_rendertarget( GLint format, GLint components, int width, int height, int layers = 1, int samples = 1, GLint wrap = GL_CLAMP_TO_EDGE );
static void
reset_unit_cache();
// marks the unit cache as not to be trusted. safe to call from any thread: the cache itself is
// cleared by the thread drawing with it, the next time it binds textures (sync_unit_cache)
static void
invalidate_unit_cache();
// clears the unit cache if a thread marked it as not to be trusted since. called before the cache is read
static void
sync_unit_cache();
// with gfx.resource.move, copies the gl texture back into the data before it's released. requires the texture bound
bool read_back_compressed();
void read_back_uncompressed();
@@ -188,6 +205,10 @@ struct opengl_texture : public texture_source {
get_id() const override {
return id; }
virtual void update_from_memory(size_t width, size_t height, const uint8_t *data) override;
// the image goes into a texture of its own, and the drawing thread swaps it in after the gpu has it (commit_thread_upload).
// the texture drawn from is never written by the uploading thread
void upload_from_thread(size_t width, size_t height, const uint8_t *data) override;
bool commit_thread_upload() override;
bool
resident() const override {
return id != invalid_id; }
@@ -203,6 +224,11 @@ struct opengl_texture : public texture_source {
GLenum target = GL_TEXTURE_2D;
static std::array<GLuint, gl::MAX_TEXTURES + gl::HELPER_TEXTURES> units;
static GLint m_activeunit;
// set by invalidate_unit_cache(), from the python upload thread too; the units themselves are touched only by the drawing thread
static std::atomic<bool> m_unitcachestale;
std::mutex m_threaduploadmutex;
std::optional<texture_thread_upload> m_threadupload; // guarded by m_threaduploadmutex
public:
// methods