From 13a10ac4826d7fb18219c705f51529dcc7468f05 Mon Sep 17 00:00:00 2001 From: bjorn Date: Wed, 7 Jul 2021 18:40:16 -0700 Subject: [PATCH] love.graphics.newComputeShader; --- src/modules/graphics/Graphics.cpp | 12 +++++++++++ src/modules/graphics/Graphics.h | 1 + src/modules/graphics/wrap_Graphics.cpp | 29 ++++++++++++++++++++++++++ 3 files changed, 42 insertions(+) diff --git a/src/modules/graphics/Graphics.cpp b/src/modules/graphics/Graphics.cpp index 1cffbebb2..51e00a8d8 100644 --- a/src/modules/graphics/Graphics.cpp +++ b/src/modules/graphics/Graphics.cpp @@ -294,6 +294,18 @@ Shader *Graphics::newShader(const std::vector &stagessource) return newShaderInternal(stages); } +Shader *Graphics::newComputeShader(const std::string &source) +{ + Shader::SourceInfo info = Shader::getSourceInfo(source); + + if (info.stages[SHADERSTAGE_COMPUTE] == Shader::ENTRYPOINT_NONE) + throw love::Exception("Could not parse compute shader code (missing 'computemain' function?)"); + + StrongRef stages[SHADERSTAGE_MAX_ENUM]; + stages[SHADERSTAGE_COMPUTE].set(newShaderStage(SHADERSTAGE_COMPUTE, source, info)); + return newShaderInternal(stages); +} + Buffer *Graphics::newBuffer(const Buffer::Settings &settings, DataFormat format, const void *data, size_t size, size_t arraylength) { std::vector dataformat = {{"", format, 0}}; diff --git a/src/modules/graphics/Graphics.h b/src/modules/graphics/Graphics.h index 57254289e..13a8867c7 100644 --- a/src/modules/graphics/Graphics.h +++ b/src/modules/graphics/Graphics.h @@ -439,6 +439,7 @@ public: ParticleSystem *newParticleSystem(Texture *texture, int size); Shader *newShader(const std::vector &stagessource); + Shader *newComputeShader(const std::string &source); virtual Buffer *newBuffer(const Buffer::Settings &settings, const std::vector &format, const void *data, size_t size, size_t arraylength) = 0; virtual Buffer *newBuffer(const Buffer::Settings &settings, DataFormat format, const void *data, size_t size, size_t arraylength); diff --git a/src/modules/graphics/wrap_Graphics.cpp b/src/modules/graphics/wrap_Graphics.cpp index 1e12dc1d0..ee0338093 100644 --- a/src/modules/graphics/wrap_Graphics.cpp +++ b/src/modules/graphics/wrap_Graphics.cpp @@ -1458,6 +1458,34 @@ int w_newShader(lua_State *L) return 1; } +int w_newComputeShader(lua_State* L) +{ + std::vector stages; + w_getShaderSource(L, 1, stages); + + bool should_error = false; + try + { + Shader *shader = instance()->newComputeShader(stages[0]); + luax_pushtype(L, shader); + shader->release(); + } + catch (love::Exception &e) + { + luax_getfunction(L, "graphics", "_transformGLSLErrorMessages"); + lua_pushstring(L, e.what()); + + // Function pushes the new error string onto the stack. + lua_pcall(L, 1, 1, 0); + should_error = true; + } + + if (should_error) + return lua_error(L); + + return 1; +} + int w_validateShader(lua_State *L) { bool gles = luax_checkboolean(L, 1); @@ -3415,6 +3443,7 @@ static const luaL_Reg functions[] = { "newSpriteBatch", w_newSpriteBatch }, { "newParticleSystem", w_newParticleSystem }, { "newShader", w_newShader }, + { "newComputeShader", w_newComputeShader }, { "newBuffer", w_newBuffer }, { "newVertexBuffer", w_newVertexBuffer }, { "newIndexBuffer", w_newIndexBuffer },