# shader-make
Drom project to make shader translator. shader-make currently only works with GLSL. It uses a decorator to transform itself into a GPU based function, here for OpenGL. You cannot currently reuse a shader function as a default function but we prepared the setup to do that. We haven't implemented every python feature that we don't think are usefull for the purpose of what we will be using it for, such as while loops that could be interesting.
# Compiling a simple function to shader code
First you will need to import the decorator and the shader engine you want to use. We currently only support OpenGL with GLSL support. shader-make will then do type analysis on the variables to generate C code that you can compile to GLSL (you have to specify the type of your input for it to work). Here is a simple GLSL function that takes two coordinates a and b and creates a vec3 with (x, y, x + y) :
```python
from shadermake.decorator import make_shader
from shadermake.engines.opengl import OpenGLEngine, vec3
@make_shader(OpenGLEngine, argument_types=[ float, float ])
def transform(x, y):
return vec3(x, y, x + y)
```
And the resulting C code is the following when you do transform.c_code():
```c
vec3 transform (float x, float y) {
return vec3(x, y, x + y);
}
```
# Linking multiple functions
We will admit you have a cool python function called magic that you would like to use in multiple shader functions. You can do that using the parameter bound_shaders. For this example we will use the cool function f(x) = x + 1 and add it to our transform function :
```python
from shadermake.decorator import make_shader
from shadermake.engines.opengl import OpenGLEngine, vec3
@make_shader(OpenGLEngine, argument_types=[ float ])
def f(x):
return x + 1
@make_shader(OpenGLEngine, argument_types=[ float, float ], bound_shaders=[ f ])
def transform(x, y):
y = f(y)
return vec3(x, y, x + y)
```
```c
float f (float x) {
return x + 1;
}
vec3 transform (float x, float y) {
y = f(y);
return vec3(x, y, x + y);
}
```
# Inputs, outputs and uniforms
When you want to compile a shader, it can be needed to import existing inputs, setup outputs or use uniform variables shared between every vertex. For this you can use a ShaderOptions element and use addInput, addOutput and addUniform to append metadata. You can also generate the default vertex shader data or fragment shader data using the useVertex or useFragment functions. The following example generates a function with an uniform displacement :
```python
from shadermake.decorator import make_shader
from shadermake.engines.opengl import OpenGLEngine, ShaderOptions, vec2, vec3, vec4
options = ShaderOptions() \
.useVertex() \
.addInput( vec4, "pos", 0 ) \
.addUniform( vec4, "delta" )
@make_shader(OpenGLEngine, shader_options=options)
def shader():
gl_Position = pos + delta
```
```c
layout(location = 0) in vec4 pos;
uniform vec4 delta;
int shader () {
gl_Position = pos + delta;
return 0;
}
```
# If statements
The usage of if statement is implemented by default but the usage of elif strongly increments the C final code size. Here is the example of the shader used in the testing and one of the following results (the one on the github actions architecture) :
```python
@make_shader(OpenGLEngine)
def main():
x = 0
y = 1
z = x + y
if z > 0.5:
y = 2
elif z >= 1:
y = 4
else:
y = 3
u= 0
if u + z > 0.2:
a = u - z
else: a = u + y
```
```c
int main () {
int x = 0;
int y = 1;
int z = x + y;
if (z > 0.5) {
y = 2;
} else {
if (z >= 1) {
y = 4;
} else {
y = 3;
}
}
int u = 0;
if (u + z > 0.2) {
int a = u - z;
} else {
int a = u + y;
}
return 0;
}
```
As you can see here, the a variable is only created in the if scope. This isn't the default behavior of python so you might have to create your variable and initialize it before the if statement with a placeholder value like 0 to avoid your data being destroyed.
Raw data
{
"_id": null,
"home_page": "https://github.com/EngDrom/ShaderMake",
"name": "shadermake",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "python,opengl,glsl",
"author": "EngDrom project",
"author_email": "<engdrom.project@gmail.com>",
"download_url": "https://files.pythonhosted.org/packages/97/72/3b1bc7bae73c9455822c8f0ce486fe2da64d920793824f4cc6ca26882788/shadermake-0.0.5.tar.gz",
"platform": null,
"description": "\n\n# shader-make\n\nDrom project to make shader translator. shader-make currently only works with GLSL. It uses a decorator to transform itself into a GPU based function, here for OpenGL. You cannot currently reuse a shader function as a default function but we prepared the setup to do that. We haven't implemented every python feature that we don't think are usefull for the purpose of what we will be using it for, such as while loops that could be interesting.\n\n# Compiling a simple function to shader code\n\nFirst you will need to import the decorator and the shader engine you want to use. We currently only support OpenGL with GLSL support. shader-make will then do type analysis on the variables to generate C code that you can compile to GLSL (you have to specify the type of your input for it to work). Here is a simple GLSL function that takes two coordinates a and b and creates a vec3 with (x, y, x + y) :\n\n```python\nfrom shadermake.decorator import make_shader\nfrom shadermake.engines.opengl import OpenGLEngine, vec3\n\n@make_shader(OpenGLEngine, argument_types=[ float, float ])\ndef transform(x, y):\n return vec3(x, y, x + y)\n```\n\nAnd the resulting C code is the following when you do transform.c_code():\n\n```c\nvec3 transform (float x, float y) {\n return vec3(x, y, x + y);\n}\n```\n\n# Linking multiple functions\n\nWe will admit you have a cool python function called magic that you would like to use in multiple shader functions. You can do that using the parameter bound_shaders. For this example we will use the cool function f(x) = x + 1 and add it to our transform function :\n\n```python\nfrom shadermake.decorator import make_shader\nfrom shadermake.engines.opengl import OpenGLEngine, vec3\n\n@make_shader(OpenGLEngine, argument_types=[ float ])\ndef f(x):\n return x + 1\n\n@make_shader(OpenGLEngine, argument_types=[ float, float ], bound_shaders=[ f ])\ndef transform(x, y):\n y = f(y)\n return vec3(x, y, x + y)\n```\n\n```c\nfloat f (float x) {\n return x + 1;\n}\nvec3 transform (float x, float y) {\n y = f(y);\n return vec3(x, y, x + y);\n}\n```\n\n# Inputs, outputs and uniforms\n\nWhen you want to compile a shader, it can be needed to import existing inputs, setup outputs or use uniform variables shared between every vertex. For this you can use a ShaderOptions element and use addInput, addOutput and addUniform to append metadata. You can also generate the default vertex shader data or fragment shader data using the useVertex or useFragment functions. The following example generates a function with an uniform displacement :\n\n```python\nfrom shadermake.decorator import make_shader\nfrom shadermake.engines.opengl import OpenGLEngine, ShaderOptions, vec2, vec3, vec4\n\noptions = ShaderOptions() \\\n .useVertex() \\\n .addInput( vec4, \"pos\", 0 ) \\\n .addUniform( vec4, \"delta\" )\n@make_shader(OpenGLEngine, shader_options=options)\ndef shader():\n gl_Position = pos + delta\n```\n\n```c\nlayout(location = 0) in vec4 pos;\nuniform vec4 delta;\n\nint shader () {\n gl_Position = pos + delta;\n return 0;\n}\n```\n\n# If statements\n\nThe usage of if statement is implemented by default but the usage of elif strongly increments the C final code size. Here is the example of the shader used in the testing and one of the following results (the one on the github actions architecture) :\n\n```python\n@make_shader(OpenGLEngine)\ndef main():\n x = 0\n y = 1\n z = x + y\n if z > 0.5:\n y = 2\n elif z >= 1:\n y = 4\n else:\n y = 3\n u= 0\n if u + z > 0.2:\n a = u - z\n else: a = u + y\n```\n\n```c\nint main () {\n int x = 0;\n int y = 1;\n int z = x + y;\n if (z > 0.5) {\n y = 2;\n } else {\n if (z >= 1) {\n y = 4;\n } else {\n y = 3;\n }\n }\n int u = 0;\n if (u + z > 0.2) {\n int a = u - z;\n } else {\n int a = u + y;\n }\n return 0;\n}\n```\n\nAs you can see here, the a variable is only created in the if scope. This isn't the default behavior of python so you might have to create your variable and initialize it before the if statement with a placeholder value like 0 to avoid your data being destroyed.\n\n\n",
"bugtrack_url": null,
"license": "",
"summary": "Transforming python code to GLSL shaders.",
"version": "0.0.5",
"split_keywords": [
"python",
"opengl",
"glsl"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "ef124552edb4d231e2610bfbecad7781f6925ad5c835c6806e122eef29bd5a7f",
"md5": "db938414aa87b24417e8403b3c25c5fc",
"sha256": "324a219c607c9fb7768ef890dbfbf6d423846fd61f33a449285034717cc9b22f"
},
"downloads": -1,
"filename": "shadermake-0.0.5-py3-none-any.whl",
"has_sig": false,
"md5_digest": "db938414aa87b24417e8403b3c25c5fc",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 8943,
"upload_time": "2023-01-10T17:07:07",
"upload_time_iso_8601": "2023-01-10T17:07:07.969955Z",
"url": "https://files.pythonhosted.org/packages/ef/12/4552edb4d231e2610bfbecad7781f6925ad5c835c6806e122eef29bd5a7f/shadermake-0.0.5-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "97723b1bc7bae73c9455822c8f0ce486fe2da64d920793824f4cc6ca26882788",
"md5": "d607327d6f6329130f0bdca53853837c",
"sha256": "04ca43cf94bf55ba879e5faa3b60b67ff1f4c1539adb2c809ca8d59c841d3e81"
},
"downloads": -1,
"filename": "shadermake-0.0.5.tar.gz",
"has_sig": false,
"md5_digest": "d607327d6f6329130f0bdca53853837c",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 9880,
"upload_time": "2023-01-10T17:07:09",
"upload_time_iso_8601": "2023-01-10T17:07:09.842048Z",
"url": "https://files.pythonhosted.org/packages/97/72/3b1bc7bae73c9455822c8f0ce486fe2da64d920793824f4cc6ca26882788/shadermake-0.0.5.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-01-10 17:07:09",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "EngDrom",
"github_project": "ShaderMake",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [],
"lcname": "shadermake"
}