Skip to content

Commit 14fa877

Browse files
committed
Documentation: Add comprehensive docstrings for Vulkan compatibility methods - improves code maintainability and developer understanding
1 parent e3b1e2c commit 14fa877

File tree

2 files changed

+67
-4
lines changed

2 files changed

+67
-4
lines changed

model/base_entity.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,30 @@ def is_dirty(self, value: bool):
6969
self.dirty = value
7070

7171
def _is_vulkan_backend(self):
72-
"""Check if the current GPU backend is Vulkan."""
72+
"""
73+
Check if the current GPU backend is Vulkan.
74+
75+
Uses cached backend detection for performance. This method is called
76+
frequently during rendering operations, so caching prevents expensive
77+
repeated GPU queries.
78+
79+
Returns:
80+
bool: True if backend is Vulkan, False for OpenGL or other backends
81+
"""
7382
return BackendCache.is_vulkan()
7483

7584
@property
7685
def _shader(self):
86+
"""
87+
Get the appropriate shader for this entity based on GPU backend.
88+
89+
Vulkan and OpenGL require different shaders for optimal rendering:
90+
- Vulkan: Uses built-in POLYLINE_UNIFORM_COLOR for proper line width support
91+
- OpenGL: Uses custom shaders with dash pattern support
92+
93+
Returns:
94+
GPUShader: Appropriate shader for current backend and entity type
95+
"""
7796
is_vulkan = self._is_vulkan_backend()
7897

7998
if self.is_point():
@@ -233,6 +252,19 @@ def is_dashed(self):
233252
return False
234253

235254
def draw(self, context):
255+
"""
256+
Render this entity using GPU-appropriate methods.
257+
258+
This method handles rendering differences between Vulkan and OpenGL:
259+
- Vulkan: Uses POLYLINE_UNIFORM_COLOR shader with lineWidth/viewportSize uniforms
260+
- OpenGL: Uses custom shaders with traditional gpu.state line width setting
261+
262+
For points on Vulkan, entities should override update() to create triangle
263+
geometry instead of using point primitives.
264+
265+
Args:
266+
context: Blender context containing viewport and region information
267+
"""
236268
if not self.is_visible(context):
237269
return
238270

utilities/constants.py

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,18 @@
1111

1212
# Rendering constants for Vulkan compatibility
1313
class RenderingConstants:
14-
"""Centralized rendering constants for consistent visual appearance."""
14+
"""
15+
Centralized rendering constants for consistent visual appearance across GPU backends.
16+
17+
These constants ensure consistent rendering between Vulkan and OpenGL backends,
18+
particularly for geometry-based rendering on Vulkan where traditional point/line
19+
primitives have limitations.
20+
21+
Vulkan Compatibility Notes:
22+
- Point sizes are used for triangle geometry (rectangles/cubes)
23+
- Line widths apply to POLYLINE_UNIFORM_COLOR shader uniforms
24+
- Dash patterns create actual geometry gaps rather than shader effects
25+
"""
1526

1627
# Point sizes for Vulkan geometry-based rendering
1728
VULKAN_POINT_2D_SIZE = 0.06 # Size of 2D point rectangles
@@ -27,12 +38,32 @@ class RenderingConstants:
2738

2839
@classmethod
2940
def dash_pattern_length(cls):
30-
"""Total length of one dash pattern (dash + gap)."""
41+
"""
42+
Calculate total length of one complete dash pattern.
43+
44+
Returns:
45+
float: Combined length of dash + gap for pattern calculations
46+
"""
3147
return cls.DASH_LENGTH + cls.GAP_LENGTH
3248

3349
# GPU Backend detection cache
3450
class BackendCache:
35-
"""Cache GPU backend detection to avoid repeated expensive queries."""
51+
"""
52+
Performance cache for GPU backend detection to avoid repeated expensive queries.
53+
54+
The gpu.platform.backend_type_get() call is expensive and happens frequently
55+
during rendering operations. This cache stores the result after the first call
56+
and provides fast subsequent access.
57+
58+
Thread Safety:
59+
This cache is designed for single-threaded use within Blender's main thread.
60+
61+
Usage:
62+
if BackendCache.is_vulkan():
63+
# Use Vulkan-specific rendering path
64+
else:
65+
# Use OpenGL fallback
66+
"""
3667
_backend_type = None
3768
_is_vulkan = None
3869

0 commit comments

Comments
 (0)