ncca-ngl 0.3.5__py3-none-any.whl → 0.5.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. ncca/ngl/PrimData/pack_arrays.py +2 -3
  2. ncca/ngl/__init__.py +3 -4
  3. ncca/ngl/base_mesh.py +28 -20
  4. ncca/ngl/image.py +1 -3
  5. ncca/ngl/mat2.py +79 -53
  6. ncca/ngl/mat3.py +104 -185
  7. ncca/ngl/mat4.py +144 -309
  8. ncca/ngl/prim_data.py +42 -36
  9. ncca/ngl/primitives.py +2 -2
  10. ncca/ngl/pyside_event_handling_mixin.py +0 -108
  11. ncca/ngl/quaternion.py +69 -36
  12. ncca/ngl/shader.py +0 -116
  13. ncca/ngl/shader_program.py +94 -117
  14. ncca/ngl/texture.py +5 -2
  15. ncca/ngl/util.py +0 -2
  16. ncca/ngl/vec2.py +59 -302
  17. ncca/ngl/vec2_array.py +79 -28
  18. ncca/ngl/vec3.py +60 -350
  19. ncca/ngl/vec3_array.py +76 -23
  20. ncca/ngl/vec4.py +90 -200
  21. ncca/ngl/vec4_array.py +78 -27
  22. ncca/ngl/vector_base.py +542 -0
  23. ncca/ngl/webgpu/__init__.py +20 -0
  24. ncca/ngl/webgpu/__main__.py +640 -0
  25. ncca/ngl/webgpu/__main__.py.backup +640 -0
  26. ncca/ngl/webgpu/base_webgpu_pipeline.py +354 -0
  27. ncca/ngl/webgpu/custom_shader_pipeline.py +288 -0
  28. ncca/ngl/webgpu/instanced_geometry_pipeline.py +594 -0
  29. ncca/ngl/webgpu/line_pipeline.py +405 -0
  30. ncca/ngl/webgpu/pipeline_factory.py +190 -0
  31. ncca/ngl/webgpu/pipeline_shaders.py +497 -0
  32. ncca/ngl/webgpu/point_list_pipeline.py +349 -0
  33. ncca/ngl/webgpu/point_pipeline.py +336 -0
  34. ncca/ngl/webgpu/triangle_pipeline.py +419 -0
  35. ncca/ngl/webgpu/webgpu_constants.py +31 -0
  36. ncca/ngl/webgpu/webgpu_widget.py +322 -0
  37. ncca/ngl/webgpu/wip/REFACTORING_SUMMARY.md +82 -0
  38. ncca/ngl/webgpu/wip/UNIFIED_SYSTEM.md +314 -0
  39. ncca/ngl/webgpu/wip/buffer_manager.py +396 -0
  40. ncca/ngl/webgpu/wip/pipeline_config.py +463 -0
  41. ncca/ngl/webgpu/wip/shader_constants.py +328 -0
  42. ncca/ngl/webgpu/wip/shader_templates.py +563 -0
  43. ncca/ngl/webgpu/wip/unified_examples.py +390 -0
  44. ncca/ngl/webgpu/wip/unified_factory.py +449 -0
  45. ncca/ngl/webgpu/wip/unified_pipeline.py +469 -0
  46. ncca/ngl/widgets/__init__.py +18 -2
  47. ncca/ngl/widgets/__main__.py +2 -1
  48. ncca/ngl/widgets/lookatwidget.py +2 -1
  49. ncca/ngl/widgets/mat4widget.py +2 -2
  50. ncca/ngl/widgets/vec2widget.py +1 -1
  51. ncca/ngl/widgets/vec3widget.py +1 -0
  52. {ncca_ngl-0.3.5.dist-info → ncca_ngl-0.5.0.dist-info}/METADATA +3 -2
  53. ncca_ngl-0.5.0.dist-info/RECORD +105 -0
  54. ncca/ngl/widgets/transformation_widget.py +0 -299
  55. ncca_ngl-0.3.5.dist-info/RECORD +0 -82
  56. {ncca_ngl-0.3.5.dist-info → ncca_ngl-0.5.0.dist-info}/WHEEL +0 -0
@@ -84,6 +84,7 @@ class ShaderProgram:
84
84
  For array uniforms, it also registers individual array elements.
85
85
  """
86
86
  uniform_count = gl.glGetProgramiv(self._id, gl.GL_ACTIVE_UNIFORMS)
87
+
87
88
  for i in range(uniform_count):
88
89
  name, size, shader_type = gl.glGetActiveUniform(self._id, i, 256)
89
90
 
@@ -92,9 +93,7 @@ class ShaderProgram:
92
93
 
93
94
  # Handle array uniforms - OpenGL returns name with [0] for arrays
94
95
  is_array = size > 1
95
- base_name = name_str
96
- if name_str.endswith("[0]"):
97
- base_name = name_str[:-3]
96
+ base_name = name_str[:-3] if name_str.endswith("[0]") else name_str
98
97
 
99
98
  location = gl.glGetUniformLocation(self._id, name)
100
99
 
@@ -103,33 +102,31 @@ class ShaderProgram:
103
102
 
104
103
  # For arrays, also register individual elements
105
104
  if is_array:
106
- for element_idx in range(size):
107
- element_name = f"{base_name}[{element_idx}]"
108
- element_location = gl.glGetUniformLocation(
109
- self._id, element_name.encode("utf-8")
110
- )
111
- if element_location != -1:
112
- # Store individual array element: (location, shader_type, 1, False)
113
- self._uniforms[element_name] = (
114
- element_location,
115
- shader_type,
116
- 1,
117
- False,
118
- )
119
-
120
- # Log array uniforms differently
121
- if is_array:
105
+ self._register_array_elements(base_name, size, shader_type)
122
106
  logger.info(
123
107
  f"Registered array uniform: {base_name}[{size}] (type: {self.get_gl_type_string(shader_type)}, location: {location})"
124
108
  )
125
- logger.info(
126
- f" Also registered individual elements: {base_name}[0] to {base_name}[{size - 1}]"
127
- )
109
+ logger.info(f" Also registered individual elements: {base_name}[0] to {base_name}[{size - 1}]")
128
110
  else:
129
111
  logger.info(
130
112
  f"Registered uniform: {base_name} (type: {self.get_gl_type_string(shader_type)}, location: {location})"
131
113
  )
132
114
 
115
+ def _register_array_elements(self, base_name: str, size: int, shader_type) -> None:
116
+ """Register individual elements of an array uniform."""
117
+ for element_idx in range(size):
118
+ element_name = f"{base_name}[{element_idx}]"
119
+ element_location = gl.glGetUniformLocation(self._id, element_name.encode("utf-8"))
120
+
121
+ if element_location != -1:
122
+ # Store individual array element: (location, shader_type, 1, False)
123
+ self._uniforms[element_name] = (
124
+ element_location,
125
+ shader_type,
126
+ 1,
127
+ False,
128
+ )
129
+
133
130
  def auto_register_uniform_blocks(self) -> None:
134
131
  """
135
132
  Automatically register uniform blocks for this shader program.
@@ -147,14 +144,8 @@ class ShaderProgram:
147
144
  name_buffer = (ctypes.c_char * 256)()
148
145
  length = ctypes.c_int()
149
146
 
150
- gl.glGetActiveUniformBlockName(
151
- self._id, i, 256, ctypes.byref(length), name_buffer
152
- )
153
- name_str = (
154
- name_buffer.value.decode("utf-8")
155
- if name_buffer.value
156
- else f"UniformBlock_{i}"
157
- )
147
+ gl.glGetActiveUniformBlockName(self._id, i, 256, ctypes.byref(length), name_buffer)
148
+ name_str = name_buffer.value.decode("utf-8") if name_buffer.value else f"UniformBlock_{i}"
158
149
 
159
150
  # Create uniform block data structure
160
151
  data = {
@@ -252,9 +243,7 @@ class ShaderProgram:
252
243
  bool: True if successful, False otherwise
253
244
  """
254
245
  if uniform_block_name not in self._registered_uniform_blocks:
255
- logger.error(
256
- f"Uniform block '{uniform_block_name}' not found in shader '{self._name}'"
257
- )
246
+ logger.error(f"Uniform block '{uniform_block_name}' not found in shader '{self._name}'")
258
247
  return False
259
248
 
260
249
  block = self._registered_uniform_blocks[uniform_block_name]
@@ -292,73 +281,75 @@ class ShaderProgram:
292
281
  *value: The value(s) to set
293
282
  """
294
283
  loc = self.get_uniform_location(name)
284
+
295
285
  if loc == -1:
296
286
  logger.warning(f"Uniform location not found for '{name}'")
297
287
  return
298
288
 
299
- # Get uniform info for better type handling
300
- uniform_info = self.get_uniform_info(name)
301
- _, uniform_type, array_size, is_array = uniform_info
302
-
303
289
  if len(value) == 1:
304
- val = value[0]
305
- if isinstance(val, int):
306
- gl.glUniform1i(loc, val)
307
- elif isinstance(val, float):
308
- gl.glUniform1f(loc, val)
309
- elif isinstance(val, Mat2):
310
- gl.glUniformMatrix2fv(
311
- loc, 1, gl.GL_FALSE, (ctypes.c_float * 4)(*val.get_matrix())
312
- )
313
- elif isinstance(val, Mat3):
314
- gl.glUniformMatrix3fv(
315
- loc, 1, gl.GL_FALSE, (ctypes.c_float * 9)(*val.get_matrix())
316
- )
317
- elif isinstance(val, Mat4):
318
- gl.glUniformMatrix4fv(
319
- loc, 1, gl.GL_FALSE, (ctypes.c_float * 16)(*val.get_matrix())
320
- )
321
- elif isinstance(val, Vec2):
322
- gl.glUniform2f(loc, *val)
323
- elif isinstance(val, Vec3):
324
- gl.glUniform3f(loc, *val)
325
- elif isinstance(val, Vec4):
326
- gl.glUniform4f(loc, *val)
327
- else:
328
- try:
329
- val = list(value[0])
330
- if len(val) == 4:
331
- gl.glUniformMatrix2fv(
332
- loc, 1, gl.GL_FALSE, (ctypes.c_float * 4)(*val)
333
- )
334
- elif len(val) == 9:
335
- gl.glUniformMatrix3fv(
336
- loc, 1, gl.GL_FALSE, (ctypes.c_float * 9)(*val)
337
- )
338
- elif len(val) == 16:
339
- gl.glUniformMatrix4fv(
340
- loc, 1, gl.GL_FALSE, (ctypes.c_float * 16)(*val)
341
- )
342
- except TypeError:
343
- logger.warning(
344
- f"Warning: uniform '{name}' has unknown type: {type(val)}"
345
- )
346
-
347
- elif len(value) == 2:
348
- if isinstance(value[0], int):
349
- gl.glUniform2i(loc, *value)
350
- else:
351
- gl.glUniform2f(loc, *value)
352
- elif len(value) == 3:
353
- if isinstance(value[0], int):
354
- gl.glUniform3i(loc, *value)
355
- else:
356
- gl.glUniform3f(loc, *value)
357
- elif len(value) == 4:
358
- if isinstance(value[0], int):
359
- gl.glUniform4i(loc, *value)
360
- else:
361
- gl.glUniform4f(loc, *value)
290
+ self._set_single_value_uniform(loc, name, value[0])
291
+ else:
292
+ self._set_multi_value_uniform(loc, value)
293
+
294
+ def _set_single_value_uniform(self, loc: int, name: str, val: Any) -> None:
295
+ """Handle setting a uniform from a single value."""
296
+ if isinstance(val, int):
297
+ gl.glUniform1i(loc, val)
298
+ elif isinstance(val, float):
299
+ gl.glUniform1f(loc, val)
300
+ elif isinstance(val, (Mat2, Mat3, Mat4)):
301
+ self._set_matrix_uniform(loc, val)
302
+ elif isinstance(val, (Vec2, Vec3, Vec4)):
303
+ self._set_vector_uniform(loc, val)
304
+ else:
305
+ self._set_list_based_uniform(loc, name, val)
306
+
307
+ def _set_matrix_uniform(self, loc: int, matrix: Any) -> None:
308
+ """Set a matrix uniform value."""
309
+ matrix_configs = {
310
+ Mat2: (4, gl.glUniformMatrix2fv),
311
+ Mat3: (9, gl.glUniformMatrix3fv),
312
+ Mat4: (16, gl.glUniformMatrix4fv),
313
+ }
314
+
315
+ size, func = matrix_configs[type(matrix)]
316
+ data = (ctypes.c_float * size)(*matrix.get_matrix())
317
+ func(loc, 1, gl.GL_FALSE, data)
318
+
319
+ def _set_vector_uniform(self, loc: int, vector: Any) -> None:
320
+ """Set a vector uniform value."""
321
+ vector_funcs = {Vec2: gl.glUniform2f, Vec3: gl.glUniform3f, Vec4: gl.glUniform4f}
322
+
323
+ func = vector_funcs[type(vector)]
324
+ func(loc, *vector)
325
+
326
+ def _set_list_based_uniform(self, loc: int, name: str, val: Any) -> None:
327
+ """Handle setting uniform from list-like values (potential matrices)."""
328
+ try:
329
+ val_list = list(val)
330
+ matrix_sizes = {4: gl.glUniformMatrix2fv, 9: gl.glUniformMatrix3fv, 16: gl.glUniformMatrix4fv}
331
+
332
+ if len(val_list) in matrix_sizes:
333
+ func = matrix_sizes[len(val_list)]
334
+ data = (ctypes.c_float * len(val_list))(*val_list)
335
+ func(loc, 1, gl.GL_FALSE, data)
336
+ except TypeError:
337
+ logger.warning(f"Warning: uniform '{name}' has unknown type: {type(val)}")
338
+
339
+ def _set_multi_value_uniform(self, loc: int, value: tuple) -> None:
340
+ """Handle setting uniform from multiple values (vectors)."""
341
+ value_len = len(value)
342
+ is_int = isinstance(value[0], int)
343
+
344
+ uniform_funcs = {
345
+ 2: (gl.glUniform2i if is_int else gl.glUniform2f),
346
+ 3: (gl.glUniform3i if is_int else gl.glUniform3f),
347
+ 4: (gl.glUniform4i if is_int else gl.glUniform4f),
348
+ }
349
+
350
+ if value_len in uniform_funcs:
351
+ func = uniform_funcs[value_len]
352
+ func(loc, *value)
362
353
 
363
354
  def set_uniform_1fv(self, name: str, values: List[float]) -> None:
364
355
  """
@@ -385,9 +376,7 @@ class ShaderProgram:
385
376
  loc = self.get_uniform_location(name)
386
377
  if loc != -1:
387
378
  flat_values = [item for vec in values for item in vec]
388
- gl.glUniform2fv(
389
- loc, len(values), (ctypes.c_float * len(flat_values))(*flat_values)
390
- )
379
+ gl.glUniform2fv(loc, len(values), (ctypes.c_float * len(flat_values))(*flat_values))
391
380
 
392
381
  def set_uniform_3fv(self, name: str, values: List[List[float]]) -> None:
393
382
  """
@@ -401,9 +390,7 @@ class ShaderProgram:
401
390
  loc = self.get_uniform_location(name)
402
391
  if loc != -1:
403
392
  flat_values = [item for vec in values for item in vec]
404
- gl.glUniform3fv(
405
- loc, len(values), (ctypes.c_float * len(flat_values))(*flat_values)
406
- )
393
+ gl.glUniform3fv(loc, len(values), (ctypes.c_float * len(flat_values))(*flat_values))
407
394
 
408
395
  def set_uniform_4fv(self, name: str, values: List[List[float]]) -> None:
409
396
  """
@@ -417,9 +404,7 @@ class ShaderProgram:
417
404
  loc = self.get_uniform_location(name)
418
405
  if loc != -1:
419
406
  flat_values = [item for vec in values for item in vec]
420
- gl.glUniform4fv(
421
- loc, len(values), (ctypes.c_float * len(flat_values))(*flat_values)
422
- )
407
+ gl.glUniform4fv(loc, len(values), (ctypes.c_float * len(flat_values))(*flat_values))
423
408
 
424
409
  def set_uniform_1iv(self, name: str, values: List[int]) -> None:
425
410
  """
@@ -861,9 +846,7 @@ class ShaderProgram:
861
846
  base_name = name.split("[")[0]
862
847
  if base_name not in array_elements:
863
848
  array_elements[base_name] = []
864
- array_elements[base_name].append(
865
- (name, location, uniform_type, size, is_array)
866
- )
849
+ array_elements[base_name].append((name, location, uniform_type, size, is_array))
867
850
  else:
868
851
  base_uniforms[name] = (location, uniform_type, size, is_array)
869
852
 
@@ -871,20 +854,16 @@ class ShaderProgram:
871
854
  for name, (location, uniform_type, size, is_array) in base_uniforms.items():
872
855
  type_str = self.get_gl_type_string(uniform_type)
873
856
  if is_array:
874
- logger.info(
875
- f" {name}[{size}] (type: {type_str}, location: {location})"
876
- )
857
+ logger.info(f" {name}[{size}] (type: {type_str}, location: {location})")
877
858
  else:
878
859
  logger.info(f" {name} (type: {type_str}, location: {location})")
879
860
 
880
861
  # Print array elements grouped by base name
881
862
  for base_name, elements in array_elements.items():
882
863
  logger.info(f" Array elements for {base_name}:")
883
- for element_name, location, uniform_type, size, is_array in elements:
864
+ for element_name, location, uniform_type, _, _ in elements:
884
865
  type_str = self.get_gl_type_string(uniform_type)
885
- logger.info(
886
- f" {element_name} (type: {type_str}, location: {location})"
887
- )
866
+ logger.info(f" {element_name} (type: {type_str}, location: {location})")
888
867
 
889
868
  def print_registered_uniform_blocks(self) -> None:
890
869
  """
@@ -922,6 +901,4 @@ class ShaderProgram:
922
901
  if self._registered_uniform_blocks:
923
902
  logger.info(" Registered uniform blocks:")
924
903
  for name, data in self._registered_uniform_blocks.items():
925
- logger.info(
926
- f" {name} (index: {data['loc']}, buffer: {data['buffer']})"
927
- )
904
+ logger.info(f" {name} (index: {data['loc']}, buffer: {data['buffer']})")
ncca/ngl/texture.py CHANGED
@@ -50,6 +50,10 @@ class Texture:
50
50
  return self._image.get_pixels().tobytes()
51
51
 
52
52
  def set_texture_gl(self) -> int:
53
+ """ "
54
+ Generate a texture ID and set the texture parameters, if the image is valid if not return 0
55
+ which is the default texture ID for not active in OpenGL
56
+ """
53
57
  if self._image.width > 0 and self._image.height > 0:
54
58
  self._texture_id = gl.glGenTextures(1)
55
59
  gl.glActiveTexture(gl.GL_TEXTURE0 + self._multi_texture_id)
@@ -68,8 +72,7 @@ class Texture:
68
72
  self.get_pixels(),
69
73
  )
70
74
  gl.glGenerateMipmap(gl.GL_TEXTURE_2D)
71
- return self._texture_id
72
- return 0
75
+ return self._texture_id
73
76
 
74
77
  def set_multi_texture(self, id: int) -> None:
75
78
  self._multi_texture_id = id
ncca/ngl/util.py CHANGED
@@ -161,5 +161,3 @@ def hash_combine(seed, h):
161
161
  seed = (seed + 0x9E3779B9 + ((seed << 6) & 0xFFFFFFFFFFFFFFFF) + (seed >> 2)) & 0xFFFFFFFFFFFFFFFF
162
162
  seed ^= h
163
163
  return seed
164
-
165
-