Design : GLSL compile #118862

Open
opened 2024-02-28 18:31:22 +01:00 by Gangneron · 8 comments

The GLSL language was implemented in the text editor in 462c144f41. The objective now is to be able to compile the shaders created so that they can be applied to objects.
For this I have two solutions but I don't know which one to choose.
The first is to create a new file which would contain the code below and allow it to compile. These two parts of code are together in the same file.
#include <cstdio> GLuint LoadShader(GLenum type, const char *filename) { Gluint shader = 0; GLsizei logsize = 0; GLint compile_status = GL_TRUE; char *log = NULL; char *src = NULL;

/* creating a vertex shader */
shader = glCreateShader(type);
if(shader == 0)
{
    std::fprintf(stderr, "unable to create shader\n");
    return 0;
}

/* loading source code */
src = LoadSource(filename);
if(src == NULL)
{
   std::fprintf(std::err, "unable to load file")
    /* theoretically, the LoadSource function has already displayed a message
       error, we will just delete our shader
       and return 0 */
    
    glDeleteShader(shader);
    return 0;
}

/* source code assignment */
glShaderSource(shader, 1, (const GLchar**)&src, NULL);

/* shader compilation */
glCompileShader(shader);

/* free source code memory */
free(src);
src = NULL;

/* check for compilation success */
glGetShaderiv(shader, GL_COMPILE_STATUS, &compile_status);
if(compile_status != GL_TRUE)
{
    /* compilation error recovery of error log */
    
    /* we get the size of the error message */
    glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &logsize);
    
    /* we allocate a memory space in which OpenGL will write the message */
    log = malloc(logsize + 1);
    if(log == NULL)
    {
        std::fprintf(stderr, "unable to allocate memory!\n");
        return 0;
    }
    /* content initialization */
    memset(log, '\0', logsize + 1);
    
    glGetShaderInfoLog(shader, logsize, &logsize, log);
    std::fprintf(stderr, "unable to compile shader'%s':\n%s",
            filename,log);
    
    /* don't forget to free the memory and our shader */
    free(log);
    glDeleteShader(shader);
    
    return 0;
}

return shader;

}
`

the advantage is that there is normally not much additional development to be done and the code seems simple. The downside is that you will probably need to include OpenGL or other files and this possibly creates a 2nd compilation system in Blender.

The 2nd solution would be to use an already existing system (if it exists) and use it. The advantage is that there is no new implementation to do. The disadvantage is that you have to do longer searches and the results will require more code, take longer to develop and increase the risk of reference problems, bugs, conflicts possibly...
I could try to finish Pr #113938 to have better performance but without guarantees and without rapid results

but before starting development I wanted to collect opinions or advice on the subject

The GLSL language was implemented in the text editor in 462c144f41. The objective now is to be able to compile the shaders created so that they can be applied to objects. For this I have two solutions but I don't know which one to choose. The first is to create a new file which would contain the code below and allow it to compile. These two parts of code are together in the same file. `#include <cstdio> GLuint LoadShader(GLenum type, const char *filename) { Gluint shader = 0; GLsizei logsize = 0; GLint compile_status = GL_TRUE; char *log = NULL; char *src = NULL;` /* creating a vertex shader */ shader = glCreateShader(type); if(shader == 0) { std::fprintf(stderr, "unable to create shader\n"); return 0; } /* loading source code */ src = LoadSource(filename); if(src == NULL) { std::fprintf(std::err, "unable to load file") /* theoretically, the LoadSource function has already displayed a message error, we will just delete our shader and return 0 */ glDeleteShader(shader); return 0; } /* source code assignment */ glShaderSource(shader, 1, (const GLchar**)&src, NULL); /* shader compilation */ glCompileShader(shader); /* free source code memory */ free(src); src = NULL; /* check for compilation success */ glGetShaderiv(shader, GL_COMPILE_STATUS, &compile_status); if(compile_status != GL_TRUE) { /* compilation error recovery of error log */ /* we get the size of the error message */ glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &logsize); /* we allocate a memory space in which OpenGL will write the message */ log = malloc(logsize + 1); if(log == NULL) { std::fprintf(stderr, "unable to allocate memory!\n"); return 0; } /* content initialization */ memset(log, '\0', logsize + 1); glGetShaderInfoLog(shader, logsize, &logsize, log); std::fprintf(stderr, "unable to compile shader'%s':\n%s", filename,log); /* don't forget to free the memory and our shader */ free(log); glDeleteShader(shader); return 0; } return shader; } ` the advantage is that there is normally not much additional development to be done and the code seems simple. The downside is that you will probably need to include OpenGL or other files and this possibly creates a 2nd compilation system in Blender. The 2nd solution would be to use an already existing system (if it exists) and use it. The advantage is that there is no new implementation to do. The disadvantage is that you have to do longer searches and the results will require more code, take longer to develop and increase the risk of reference problems, bugs, conflicts possibly... I could try to finish Pr #113938 to have better performance but without guarantees and without rapid results but before starting development I wanted to collect opinions or advice on the subject
Gangneron added the
Type
Design
label 2024-02-28 18:31:22 +01:00
Iliya Katushenock added this to the Python API project 2024-02-28 18:32:08 +01:00
Iliya Katushenock added the
Module
EEVEE & Viewport
label 2024-02-28 18:32:11 +01:00
Member

Python already has a GPU module that allows compiling GLSL shaders.
Unless I'm missing something that already covers what you're describing, and it has the advantage of working on Metal too.

Python already has a [GPU module](https://docs.blender.org/api/current/gpu.html) that allows compiling GLSL shaders. Unless I'm missing something that already covers what you're describing, and it has the advantage of working on Metal too.
Author

The goal is not to use python to create a shader but to be able to press the execute button and execute custom GLSL shaders. so you need a system to compile the shaders

The goal is not to use python to create a shader but to be able to press the execute button and execute custom GLSL shaders. so you need a system to compile the shaders
Author

Plus I would like it to work on all platforms.

Plus I would like it to work on all platforms.
Member

The goal is not to use python to create a shader but to be able to press the execute button and execute custom GLSL shaders.

Executing shaders in what context? Could you provide a specific example of how a user would use this?

> The goal is not to use python to create a shader but to be able to press the execute button and execute custom GLSL shaders. Executing shaders in what context? Could you provide a specific example of how a user would use this?
Author

For example you write the following shader in the blender text editor.

you will want to execute it on an

object in your scene for example. You must therefore compile the shader and execute it and this is where the compilation system comes in. When you press run this compiles the shader and runs it on where the objects.

For example you write the following shader in the blender text editor. you will want to execute it on an object in your scene for example. You must therefore compile the shader and execute it and this is where the compilation system comes in. When you press run this compiles the shader and runs it on where the objects.
Member

I think for something like what you describe, compiling a shader is the least of your problems, and something that is already supported anyway.
Your example seems to be missing many details, and still doesn't clarify what the actual goal is (why is this better than a draw_handler?). And your GLSL code doesn't make sense (you're mixing a compute and a vertex shader on the same file ?)

I would suggest you to explore your ideas by building an addon first.

I think for something like what you describe, compiling a shader is the least of your problems, and something that is already supported anyway. Your example seems to be missing many details, and still doesn't clarify what the actual goal is (why is this better than a `draw_handler`?). And your GLSL code doesn't make sense (you're mixing a compute and a vertex shader on the same file ?) I would suggest you to explore your ideas by building an addon first.
Author

the code of shader have create by Clément Foucault in #116793 and i don't check if was good.

the code of shader have create by Clément Foucault in #116793 and i don't check if was good.
Author

@pragma37 For me GLSL no compile when write the script and no execute

@pragma37 For me GLSL no compile when write the script and no execute
Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset System
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Asset Browser Project
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#118862
No description provided.