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

Bgl replacement (and other 3.5 / 4.0 curiosities) #4983

Merged
merged 45 commits into from
Sep 3, 2024
Merged

Conversation

zeffii
Copy link
Collaborator

@zeffii zeffii commented Aug 15, 2023

alternative bgl abstraction.

continuation of #4936

  • remove bgl from current node set and modules/utils
  • adjust blf.text to handle 3 vs 2 parameters
  • implement texture/image loading for texture/buffer using nodes (texture viewers and console node)
    • https://docs.blender.org/api/4.0/gpu.types.html
    • affected nodes ( TextViewer, TVLite, Stethoscope ( nice mode ), Console Node ) , These are not the most
      essential nodes but
      • "nice mode" for stethoscope is pretty cool and if I get that to work then console node will also work
        automatically (they share the same code)
      • Texture Viewer nodes use some Gl code to place the texture in a state to receive data which i'm only tangentially
        familiar with :)
  • bug fix front_facing polygon drawing thru
    • lines are not being obscured by polygons.
  • polygon offset alternative
    • avoid z-fighting ( proof of concept: at the moment implements a normal shader in custom-shader mode )
    • reimplement custom shader
    • implement shader for flat/facet/smooth
Node works in < 4.0 works in > 4.0 feature parity issue
Viewer Draw yes yes no zfighting lines
Stethoscope MK2 yes yes no advanced drawing to buffer not implemented, no colour highlighting
Matrix View yes yes maybe plane seems to always draw
Viewer Index yes yes yes ...
Viewer 2D yes yes yes ...
Viewer Draw Curve yes yes yes ...
Viewer Draw Surface yes yes yes ...
Texture Viewer yes ? ? ?
Texture Viewer Lite yes ? ? ?
SvWaveformViewer yes yes yes ...
Console Node yes ? ? ?
Solid Viewer (did not test) yes ? ? ?
Easing 0..1 yes yes yes ...

utils/modules/drawing_abstractions.py Outdated Show resolved Hide resolved
utils/modules/drawing_abstractions.py Outdated Show resolved Hide resolved
@zeffii zeffii changed the title Bgl replacement Bgl replacement (and other 3.5 / 4.0 curiosities) Aug 18, 2023
@zeffii
Copy link
Collaborator Author

zeffii commented Aug 18, 2023

with minimal testing I think the branch runs both pre and post B3.5, without spitting errors. However there is a massive degrade in feature set for vdmk4.(and any of the derivatives).. namely:

image

I like to think I kind of know what i'm doing in GL space, but the gpu.state stuff is not a direct analogue.

@zeffii
Copy link
Collaborator Author

zeffii commented Aug 18, 2023

image

well that seems to work now..

@zeffii
Copy link
Collaborator Author

zeffii commented Aug 23, 2023

resolves: #4961
resolves: #4895

@zeffii
Copy link
Collaborator Author

zeffii commented Sep 3, 2023

i'm not sure i'll be around to finish this PR, but here is one solution to the removal of the OffsetPolygon feature. We push polygons along their normal, by some small amount, from within a Geometry Shader. I'm not certain that Blender supports Geometry Shaders, however. (fact check? it does!)

https://www.informit.com/articles/article.aspx?p=2120983&seqNum=2

according to this each Geometry Shader call will have a default variable called gl_in which holds (in our case) 3 vectors. Our input primitives are all triangles. .and we can emit new vertices for each of these.

// Calculate two vectors in the plane of the input triangle
vec3 ab = gl_in[1].gl_Position.xyz - gl_in[0].gl_Position.xyz;
vec3 ac = gl_in[2].gl_Position.xyz - gl_in[0].gl_Position.xyz;
vec3 normal = normalize(cross(ab, ac));

and

for (i = 0; i < gl_in.length(); i++)
{
    gl_Position = gl_in[i].gl_Position;
    EmitVertex();
}
//EmitPrimitive();  no need for this if you only emit one primitive 
EndPrimitive();

here another intro https://computergraphics.stackexchange.com/questions/5086/how-can-i-offset-shrink-a-triangular-polygon-in-glsl


and possibly

mrdoob/three.js#5295 (comment)

where instead of modifying the polygon geometry at "shadertime" , the suggestion seems to be to modify the vectors z coordinate by ~0.0003 from a custom Geometry Shader for Edge geometry.

@zeffii
Copy link
Collaborator Author

zeffii commented Sep 4, 2023

https://docs.blender.org/api/current/gpu.types.html#gpu.types.GPUShader

here it does appear we can feed a geometry shader.

Parameters
vertexcode (str) – Vertex shader code.

fragcode – Fragment shader code.

geocode – Geometry shader code.

libcode – Code with functions and presets to be shared between shaders.

defines – Preprocessor directives.

name – Name of shader code, for debugging purposes.

@zeffii
Copy link
Collaborator Author

zeffii commented Sep 5, 2023

going down the geometry shader rabbit hole https://stackoverflow.com/questions/14909796/simple-pass-through-geometry-shader-with-normal-and-color

this was useful..

Vertex Shader -> Geometry Shader -> Fragment Shader

image

https://www.youtube.com/watch?v=C8FK9Xn1gUM

I read somewhere that glsl on Metal doesn't support geometry shaders, not sure if that's true but that does mean a few issues for the codeflow.

@zeffii
Copy link
Collaborator Author

zeffii commented Sep 18, 2023

OK!

image

(this image doesn't do it justice)

@zeffii
Copy link
Collaborator Author

zeffii commented Sep 18, 2023

this branch doesn't work in the lastest 4.0 alphas @Durman bpy.types.NodeSocketInterface doesn't exist anymore?

@zeffii
Copy link
Collaborator Author

zeffii commented Sep 19, 2023

i guess this technique fails when polygons are facing away from the viewer, therefore it would need to flip the normal in that case.

  • but - currently away facing polygons are hidden due to the gpu.state configuration. ( gpu.state.depth_test and ..... . . face_culling_set

fragment_shader, has a built in variable gl_FrontFacing , it's a bool.

    if(gl_FrontFacing)
        FragColor = ... ;
    else
        FragColor = ...  ;

@zeffii
Copy link
Collaborator Author

zeffii commented Sep 19, 2023

image

HARRR!!!

@Durman
Copy link
Collaborator

Durman commented Sep 26, 2023

bpy.types.NodeSocketInterface doesn't exist anymore?

I guess it was renamed - https://docs.blender.org/api/4.0/bpy.types.NodeTreeInterfaceSocket.html#bpy.types.NodeTreeInterfaceSocket

But it seems is not the only change, there is new attribute for setting tree interface - https://docs.blender.org/api/4.0/bpy.types.NodeTree.html#bpy.types.NodeTree.interface

@satabol
Copy link
Collaborator

satabol commented Nov 18, 2023

Hi @zeffii !
I try this PR in Blender 4.0.1. It is not start, but I found some changes in API:

  1. bpy.types.NodeSocketInterface -> bpy.types.NodeTreeInterfaceSocket

image

Now I can start this PR and it work now:

image

Your work are very awaited !!! )))

@Durman Durman added this to the Sverchok v1.3 milestone Nov 20, 2023
@zeffii
Copy link
Collaborator Author

zeffii commented Nov 21, 2023

satabol, OK. i have some time before christmas to spend on sverchok. will try to merge master into this PR and make a version that loads bpy.types.NodeSocketInterface or bpy.types.NodeTreeInterfaceSocket.

i feel OK about merging this soon-ish, as experimental for 4.0 - so people can at least use some of these "GL/BLF" viewers. i'll update the checklist at the top to reflect all that I think is still missing.

@zeffii zeffii marked this pull request as ready for review November 21, 2023 17:31
@Durman Durman linked an issue Nov 27, 2023 that may be closed by this pull request
@nortikin
Copy link
Owner

nortikin commented Jan 3, 2024

How can we enforse or help with tasks?

@zeffii
Copy link
Collaborator Author

zeffii commented Jan 4, 2024

This branch does not yet reach feature parity, but i'm interested to see how it behaves in the wild. If anyone feels like merging this as is, I will be more likely to keep updating the 4.0+ parts (to solve the zfighting in solid/facetted/smooth modes)

@zeffii
Copy link
Collaborator Author

zeffii commented Jan 4, 2024

How can we enforse or help with tasks?

my primary goal was to get SV loading regardless of the Blender version, which it does seem to do now. Some graphical glitches (like zfighting edges/polygons) will occur in 4.0+ until a dedicated shader is implemented for 4.0+, but it needs to be executed only when edges and polygons are displayed at the same time)

it's not a big task, I purely haven't sat at a computer for much of the past month.

@nortikin
Copy link
Owner

nortikin commented Jan 7, 2024

image
In 3.6 there is something backfaced

@zeffii
Copy link
Collaborator Author

zeffii commented Jan 7, 2024

Weird. I probably didn't test on 3.6.. This is not good.

@gerroon
Copy link

gerroon commented Mar 9, 2024

Is this merged?

@nortikin
Copy link
Owner

nortikin commented May 6, 2024

when adding node in blender 4.1: works.
when adding node in blender 3.6:

File "D:\users\***\INSTALL\blender-3.6.0-alpha+main.097492b326c7-windows.amd64-release\3.6\scripts\startup\bl_operators\node.py", line 126, in invoke                      
result = self.execute(context)                            
File "C:\Users\***\AppData\Roaming\Blender Foundation\Blender\3.6\scripts\addons\sverchok\ui\nodeview_space_menu.py", line 535, in execute                             
self.deselect_nodes(context)                                  
File "D:\users\***\INSTALL\blender-3.6.0-alpha+main.097492b326c7-windows.amd64-release\3.6\scripts\modules\bpy_types.py", line 816, in __getattribute__                             
return super().__getattribute__(attr)                             
AttributeError: 'SvNodeAddOperator' object has no attribute 'deselect_nodes'


P.S. It works in 4.1 and 3.6 also. It was some kind of exception for unrestarted blender

@nortikin
Copy link
Owner

nortikin commented May 6, 2024

4.1:
image

File "C:\Users\***\AppData\Roaming\Blender Foundation\Blender\4.1\scripts\addons\sverchok\utils\sv_3dview_tools.py", line 97, in sv_execute bpy.ops.view3d.view_center_cursor(ctx) File "D:\users\***\INSTALL\blender-4.1.1-windows-x64\4.1\scripts\modules\bpy\ops.py", line 106, in __call__ C_exec, C_undo = _BPyOpsSubModOp._parse_args(args) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\users\***\INSTALL\blender-4.1.1-windows-x64\4.1\scripts\modules\bpy\ops.py", line 60, in _parse_args raise ValueError("1-2 args execution context is supported") ValueError: 1-2 args execution context is supported

3.6: works

@nortikin
Copy link
Owner

nortikin commented May 6, 2024

image
4.1:
21:23:25.488 [ERROR] sverchok.node_error vertex_utils.py:141 - setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (34,) + inhomogeneous part.

3.6: works

@nortikin nortikin merged commit 27653dc into master Sep 3, 2024
1 check passed
@nortikin nortikin deleted the bgl_replacement branch September 3, 2024 13:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Viewer Draw Not Working in blender 4.0
6 participants