Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.x] Add shader tracking & precomp #80345

Open
wants to merge 1 commit into
base: 3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions doc/classes/VisualServer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2676,6 +2676,52 @@
Returns the parameters of a shader.
</description>
</method>
<method name="shader_preload_canvas">
<return type="int" enum="Error" />
<argument index="0" name="path" type="String" />
<description>
Add the canvas (2D) shaders contained in the file at the given path to the pre compile queue.
</description>
</method>
<method name="shader_preload_get_stage" qualifiers="const">
<return type="int" />
<description>
Returns the progress of the preloader.
You can use this in combination with [code]shader_preload_get_stage_count()[/code] to calculate the progress in percent.
</description>
</method>
<method name="shader_preload_get_stage_count" qualifiers="const">
<return type="int" />
<description>
Returns the total number of all shaders that have been added to the preloader queue.
</description>
</method>
<method name="shader_preload_is_running" qualifiers="const">
<return type="bool" />
<description>
Returns true if the preloader is currently processing the queue.
</description>
</method>
<method name="shader_preload_particle">
<return type="int" enum="Error" />
<argument index="0" name="path" type="String" />
<description>
Add the particle shaders contained in the file at the given path to the pre compile queue.
</description>
</method>
<method name="shader_preload_spatial">
<return type="int" enum="Error" />
<argument index="0" name="path" type="String" />
<description>
Add the spatial (3D) shaders contained in the file at the given path to the pre compile queue.
</description>
</method>
<method name="shader_preload_start">
<return type="void" />
<description>
Tell the preloader to start processing the queue.
</description>
</method>
<method name="shader_set_code">
<return type="void" />
<argument index="0" name="shader" type="RID" />
Expand Down
8 changes: 8 additions & 0 deletions drivers/dummy/rasterizer_dummy.h
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,14 @@ class RasterizerStorageDummy : public RasterizerStorage {
void set_shader_async_hidden_forbidden(bool p_forbidden) {}
bool is_shader_async_hidden_forbidden() { return false; }

virtual Error shader_preload_spatial(const String &p_file_path) { return Error::OK; }
virtual Error shader_preload_canvas(const String &p_file_path) { return Error::OK; }
virtual Error shader_preload_particle(const String &p_file_path) { return Error::OK; }
virtual void shader_preload_start() {}
virtual bool shader_preload_is_running() const { return false; }
virtual int shader_preload_get_stage() const { return 0; }
virtual int shader_preload_get_stage_count() const { return 0; }

/* COMMON MATERIAL API */

RID material_create() { return RID(); }
Expand Down
8 changes: 8 additions & 0 deletions drivers/gles2/rasterizer_storage_gles2.h
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,14 @@ class RasterizerStorageGLES2 : public RasterizerStorage {
void _update_shader(Shader *p_shader) const;
void update_dirty_shaders();

virtual Error shader_preload_spatial(const String &p_file_path) { return Error::OK; }
virtual Error shader_preload_canvas(const String &p_file_path) { return Error::OK; }
virtual Error shader_preload_particle(const String &p_file_path) { return Error::OK; }
virtual void shader_preload_start() {}
virtual bool shader_preload_is_running() const { return false; }
virtual int shader_preload_get_stage() const { return 0; }
virtual int shader_preload_get_stage_count() const { return 0; }

/* COMMON MATERIAL API */

struct Material : public RID_Data {
Expand Down
4 changes: 4 additions & 0 deletions drivers/gles3/rasterizer_gles3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
#include "core/os/os.h"
#include "core/project_settings.h"

#include "shader_tracker_gles3.h"

RasterizerStorage *RasterizerGLES3::get_storage() {
return storage;
}
Expand Down Expand Up @@ -415,6 +417,8 @@ void RasterizerGLES3::end_frame(bool p_swap_buffers) {

ShaderGLES3::advance_async_shaders_compilation();

ShaderPreLoader::compile();

if (p_swap_buffers) {
OS::get_singleton()->swap_buffers();
} else {
Expand Down
43 changes: 43 additions & 0 deletions drivers/gles3/rasterizer_storage_gles3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2329,6 +2329,10 @@ void RasterizerStorageGLES3::_update_shader(Shader *p_shader) const {
_material_make_dirty(E->self());
}

if (shaders.tracker) {
shaders.tracker->add_shader(p_shader->mode, p_shader->code, actions, p_shader->shader->get_conditional_version());
}

p_shader->valid = true;
p_shader->version++;
}
Expand Down Expand Up @@ -2525,6 +2529,34 @@ bool RasterizerStorageGLES3::is_shader_async_hidden_forbidden() {
return ShaderGLES3::async_hidden_forbidden;
}

Error RasterizerStorageGLES3::shader_preload_spatial(const String &p_file_path) {
return shaders.preLoader->load_spatial(p_file_path);
}

Error RasterizerStorageGLES3::shader_preload_canvas(const String &p_file_path) {
return shaders.preLoader->load_canvas(p_file_path);
}

Error RasterizerStorageGLES3::shader_preload_particle(const String &p_file_path) {
return shaders.preLoader->load_particle(p_file_path);
}

void RasterizerStorageGLES3::shader_preload_start() {
shaders.preLoader->start();
}

bool RasterizerStorageGLES3::shader_preload_is_running() const {
return shaders.preLoader->is_running();
}

int RasterizerStorageGLES3::shader_preload_get_stage() const {
return shaders.preLoader->get_stage();
}

int RasterizerStorageGLES3::shader_preload_get_stage_count() const {
return shaders.preLoader->get_stage_count();
}

/* COMMON MATERIAL API */

void RasterizerStorageGLES3::_material_make_dirty(Material *p_material) const {
Expand Down Expand Up @@ -8169,6 +8201,11 @@ void RasterizerStorageGLES3::initialize() {

shaders.compile_queue = nullptr;
shaders.cache = nullptr;
shaders.tracker = nullptr;
if (Main::shader_tracke_enabled()) {
shaders.tracker = memnew(ShaderTrackerGLES3);
}
shaders.preLoader = memnew(ShaderPreLoader);
shaders.cache_write_queue = nullptr;
bool effectively_on = false;
if (config.async_compilation_enabled) {
Expand Down Expand Up @@ -8450,6 +8487,12 @@ RasterizerStorageGLES3::~RasterizerStorageGLES3() {
if (shaders.cache) {
memdelete(shaders.cache);
}
if (shaders.tracker) {
memdelete(shaders.tracker);
}
if (shaders.preLoader) {
memdelete(shaders.preLoader);
}
if (shaders.cache_write_queue) {
memdelete(shaders.cache_write_queue);
}
Expand Down
11 changes: 11 additions & 0 deletions drivers/gles3/rasterizer_storage_gles3.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "shader_cache_gles3.h"
#include "shader_compiler_gles3.h"
#include "shader_gles3.h"
#include "shader_tracker_gles3.h"

#include "shaders/blend_shape.glsl.gen.h"
#include "shaders/canvas.glsl.gen.h"
Expand Down Expand Up @@ -123,6 +124,8 @@ class RasterizerStorageGLES3 : public RasterizerStorage {

ShaderCompilerGLES3 compiler;
ShaderCacheGLES3 *cache;
ShaderTrackerGLES3 *tracker;
ShaderPreLoader *preLoader;
ThreadedCallableQueue<GLuint> *cache_write_queue;
ThreadedCallableQueue<GLuint> *compile_queue;

Expand Down Expand Up @@ -566,6 +569,14 @@ class RasterizerStorageGLES3 : public RasterizerStorage {

void update_dirty_shaders();

virtual Error shader_preload_spatial(const String &p_file_path);
virtual Error shader_preload_canvas(const String &p_file_path);
virtual Error shader_preload_particle(const String &p_file_path);
virtual void shader_preload_start();
virtual bool shader_preload_is_running() const;
virtual int shader_preload_get_stage() const;
virtual int shader_preload_get_stage_count() const;

/* COMMON MATERIAL API */

struct Material : public RID_Data {
Expand Down
9 changes: 9 additions & 0 deletions drivers/gles3/shader_gles3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,15 @@ void ShaderGLES3::advance_async_shaders_compilation() {
}
}

void ShaderGLES3::set_conditional_version(uint64_t p_version) {
conditional_version = p_version;
new_conditional_version = p_version;
}

uint64_t ShaderGLES3::get_conditional_version() {
return conditional_version.key;
}

void ShaderGLES3::_log_active_compiles() {
#ifdef DEBUG_ENABLED
if (log_active_async_compiles_count) {
Expand Down
3 changes: 3 additions & 0 deletions drivers/gles3/shader_gles3.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ class ShaderGLES3 {

static void advance_async_shaders_compilation();

void set_conditional_version(uint64_t p_version);
uint64_t get_conditional_version();

private:
union VersionKey {
static const uint32_t UBERSHADER_FLAG = ((uint32_t)1) << 31;
Expand Down
Loading