From 2644e5e2ed3163bcbd41a381c6afe3de0b7c43e7 Mon Sep 17 00:00:00 2001 From: Victor Carvalho Date: Sun, 25 Dec 2022 17:03:21 +0000 Subject: [PATCH 1/2] fix stride calculation on types bigger than a vec4 If the data definition via defstruct-g or a built-in type exceeds a vec4 , the usage of `%gl:vertex-attrib-*pointer` calls changes. In CEPL, the stride was previously always 0, meaning the generic vertex attributes are tightly packed (as per the spec). The splitting of a type into multiple pointers and their offsets was already done. However, in both Fedora 37 and macOS 12.6 it gave unexpected results. The reason, though not documented (AFAICT), is likely due to the stride being necessary in cases for types longer than a vec4 for vertex attributes. By always providing it, we get the expected results. A sample pipeline is given: ``` (defun-g vert ((data g-pnt) (instance :mat4) &uniform (view :mat4) (proj :mat4)) (* proj view instance (v! (pos data) 1.0))) (defun-g frag () (vec4 1.0)) (defpipeline-g test-instancing () (vert g-pnt :mat4) (frag)) ``` The data definition and map-g is left to the patch reader. --- core/internals.lisp | 11 ++++++++--- core/types/structs.lisp | 13 +++++++++---- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/core/internals.lisp b/core/internals.lisp index 84eb2b41..2245d2ae 100644 --- a/core/internals.lisp +++ b/core/internals.lisp @@ -19,9 +19,14 @@ (let ((type (or (varjo.internals::try-type-spec->type array-type nil) (error 'bad-type-for-buffer-stream-data :type array-type)))) (if (and (varjo:core-typep type) (not (varjo:v-typep type 'v-sampler))) - (let ((slot-layout (cepl.types::expand-slot-to-layout - nil type normalized)) - (stride 0)) + (let* ((slot-layout (cepl.types::expand-slot-to-layout + nil type normalized)) + (stride (reduce (lambda (accum attr) + (incf accum (* (first attr) + (gl-type-size (second attr))))) + slot-layout + :initial-value 0))) + (break) (loop :for attr :in slot-layout :for i :from 0 :with offset = 0 diff --git a/core/types/structs.lisp b/core/types/structs.lisp index 46bbda1f..a969324c 100644 --- a/core/types/structs.lisp +++ b/core/types/structs.lisp @@ -260,7 +260,7 @@ (defun+ make-varjo-struct-def (name slots) (let ((hidden-name (symb-package (symbol-package name) - 'v_ name ))) + 'v_ name))) `(v-defstruct (,name :shadowing ,hidden-name) () @@ -465,11 +465,16 @@ (defun+ make-struct-attrib-assigner (type-name slots) (when (every #'buffer-stream-compatible-typep slots) - (let* ((stride (if (> (length slots) 1) + (let* ((def-sets (mapcat #'expand-slot-to-layout slots)) + (stride (if (> (length slots) 1) `(cepl.internals:gl-type-size ',type-name) - 0)) + (reduce (lambda (accum attr) + (incf accum (* (first attr) + (cepl.internals:gl-type-size + (second attr))))) + def-sets + :initial-value 0))) (stride-sym (gensym "stride")) - (def-sets (mapcat #'expand-slot-to-layout slots)) (definitions (loop :for (len cffi-type normalized gl-type) :in def-sets From 6c89e921e7c238321e3eb002fa4ffa349086087f Mon Sep 17 00:00:00 2001 From: Victor Carvalho Date: Sun, 25 Dec 2022 17:12:01 +0000 Subject: [PATCH 2/2] remove rogue breaking point --- core/internals.lisp | 1 - 1 file changed, 1 deletion(-) diff --git a/core/internals.lisp b/core/internals.lisp index 2245d2ae..eb4843d6 100644 --- a/core/internals.lisp +++ b/core/internals.lisp @@ -26,7 +26,6 @@ (gl-type-size (second attr))))) slot-layout :initial-value 0))) - (break) (loop :for attr :in slot-layout :for i :from 0 :with offset = 0