Page MenuHome

GPU module: Initial implementation of the `gpu.shader` module.
ClosedPublic

Authored by Germano Cavalcante (mano-wii) on Sep 12 2018, 6:08 PM.

Details

Summary

This patch exposes the functions of GPU_shader.h.

*Methods:*

>>> gpu.types.GPUShader.
                        bind(
                        attr_from_name(
                        program
                        transform_feedback_disable(
                        transform_feedback_enable(
                        uniform_int(
                        uniform_from_name(
                        uniform_block_from_name(
                        uniform_vector_float(
                        uniform_vector_int(

*Functions and shader_id objects:*

>>> gpu.shader.
               GPU_SHADER_2D_FLAT_COLOR
               GPU_SHADER_2D_SMOOTH_COLOR
               GPU_SHADER_2D_UNIFORM_COLOR
               GPU_SHADER_3D_FLAT_COLOR
               GPU_SHADER_3D_SMOOTH_COLOR
               GPU_SHADER_3D_UNIFORM_COLOR
               shader_from_builtin(
               shader_code_from_builtin(
               unbind(

Here some codes if you want to test:


Diff Detail

Repository
rB Blender

Event Timeline

  • Cleanup: Deduplicate uniform_vector code.
  • Silence GNUC/clang warnings.
  • Fix crash when return NULL without setting an error message.
  • Cleanup: avoid confusions with int and uint.

Rename shader functions

  • get_builtin_shader -> shader_from_builtin
  • read_builtin_shader_code -> shader_code_from_builtin

PyAPI: no need to use PyC_UnicodeFromByte

Add check for invalid int arg

Rename API functions:

  • get_uniform -> uniform_from_name
  • get_uniform_block -> uniform_block_from_name
  • uniform_vector -> uniform_vector_float
  • get_attribute -> attr_from_name

Use rst formatting for docs

  • Merge branch 'blender2.8' into arcpatch-D3688
  • Fix module description.
  • Add sub-sub-module gpu.shader.builtin
This revision was not accepted when it landed; it landed in state Needs Review.Sep 14 2018, 3:09 PM
This revision was automatically updated to reflect the committed changes.

Here are some changes to the first example file to make it work on current Blender.

@Germano Cavalcante (mano-wii) what is the purpose of the del batch statement in your example Python code? From what I see it's at the end of the function anyway (the name batch goes out of scope there), so AFAIK the statement doesn't have any purpose.

@Germano Cavalcante (mano-wii) what is the purpose of the del batch statement in your example Python code? From what I see it's at the end of the function anyway (the name batch goes out of scope there), so AFAIK the statement doesn't have any purpose.

Yeah, sounds right.

Also checkout these examples in the release notes: https://wiki.blender.org/wiki/Reference/Release_Notes/2.80/Python_API/Draw_API#Examples

@Germano Cavalcante (mano-wii) what is the purpose of the del batch statement in your example Python code?

In this case really makes no difference. (it was more to indicate that the batch was removed, which would be good to avoid).

We might want to have a batch.free(), for resources which can use a lot of memory and accidentally be left in scope it can be useful to explicitly end them.

BMesh has this for example, useful so you get an error operating on a resource that's intended to be finished with (like operating on a closed file).

We might want to have a batch.free(), for resources which can use a lot of memory and accidentally be left in scope it can be useful to explicitly end them.

Sounds good to me.

BMesh has this for example, useful so you get an error operating on a resource that's intended to be finished with (like operating on a closed file).

Maybe it would be possible to allow using a batch as context manager? That would allow something like this:

with gpu.types.GPUBatch(type="LINE_STRIP", buf=vbo) as batch:
    batch.program_set(self.shader)
    batch.draw()

That'll also give us a cleaner way to free the batch in case of exceptions, etc.

Maybe it would be possible to allow using a batch as context manager? That would allow something like this:

with gpu.types.GPUBatch(type="LINE_STRIP", buf=vbo) as batch:
    batch.program_set(self.shader)
    batch.draw()

That'll also give us a cleaner way to free the batch in case of exceptions, etc.

We talked about different uses of context managers before but decided it was not worth it. E.g. you could also something like this:

with shader.bind() as active:
    active.uniform_float("color", (1, 1, 1, 1))
    ...

I don't think your suggested use of context managers should be implemented because it encourages a bad habit. More specifically it makes you want to recreate the batch every time the viewport is redrawn.
It is better to separate construction from use in many cases. So, if possible, you create the batch ones and then use it many times.
The best way to create a batch is to use the gpu_extras.batch.batch_for_shader function currently. Read D3779 for more details.