Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
pfeodrippe committed Jan 11, 2025
1 parent 248aad9 commit d34b946
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 34 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## Master

- TBD
- [VybeC] Don't make things const by default as we don't get `const` info from the jextracts anyway

## v0.7.469

Expand Down
2 changes: 1 addition & 1 deletion resources/shaders/edge_2d.fs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ out vec4 finalColor;
// #define BLUENOISE_TEXTURE_RESOLUTION u_noiseResolution

#include "lygia/sample/clamp2edge.glsl"
#define EDGE_SAMPLER_FNC(TEX, UV) sampleClamp2edge(TEX, UV).r
#define EDGE_SAMPLER_FNC(TEX, UV) sampleClamp2edge(TEX, UV).g
#include "lygia/filter/edge.glsl"

void main (void) {
Expand Down
36 changes: 29 additions & 7 deletions src/vybe/c.clj
Original file line number Diff line number Diff line change
Expand Up @@ -232,12 +232,20 @@
"char*"

:else
(name type))
(try
(name type)
(catch Exception e
(throw (ex-info "Error on `comp->c` when getting name"
{:k k
:type type
:component component}
e)))))
" " (name k) ";"))))
(str/join "\n"))
c-name)))
#_ (comp->c VybeAllocator)
#_ (comp->c Unit)
#_ (comp->c vybe.flecs/system_desc_t)

(def -clz-h
"From clz.h in SC."
Expand All @@ -249,6 +257,8 @@
# include <stdbool.h>
#endif // __cplusplus
typedef bool boolean;
typedef int SCErr;
typedef int64_t int64;
Expand Down Expand Up @@ -670,9 +680,10 @@ static inline int32 NEXTPOWEROFTWO(int32 x) { return (int32)1L << LOG2CEIL(x); }
(->name (resolve name))
(->> params
(mapv (fn [{:keys [tag form]}]
(str (if (:mut (meta form))
(str #_(if (:mut (meta form))
""
"const ")
""
(case (.getName ^Class tag)
"[F"
"float*"
Expand Down Expand Up @@ -978,9 +989,10 @@ signal(SIGSEGV, sighandler);
""
;; Variables are defined as
;; `const` by default.
(if (:mut (meta form))
"__auto_type "
"const __auto_type "))
#_(if (:mut (meta form))
"__auto_type "
"const __auto_type ")
"__auto_type ")
form
(emit init))]
(-> acc
Expand Down Expand Up @@ -1498,7 +1510,13 @@ long long int: \"long long int\", unsigned long long int: \"unsigned long long i
(defonce -*vybec-fn-watchers (atom {}))

(defmacro defn*
"Transpiles Clojure code into a C function."
"Transpiles Clojure code into a C function.
E.g.
(vc/defn* simple-10 :- :int
[v :- Translation]
(simple v))"
{:clj-kondo/lint-as 'schema.core/defn}
[n _ ret-schema args & fn-tail]
`(let [args-desc-1# (quote ~(->> args
Expand Down Expand Up @@ -1687,7 +1705,7 @@ long long int: \"long long int\", unsigned long long int: \"unsigned long long i
:metadata ~(let [{:keys [line column]} (meta form)]
(str *ns* ":" line ":" column))
:form ~(str form)
:data (vp/address arg#)}))
:data (vp/& arg#)}))
arg#))
;; ================= c-invoke methods ===================
Expand Down Expand Up @@ -1777,6 +1795,10 @@ long long int: \"long long int\", unsigned long long int: \"unsigned long long i
[{:keys [args]}]
(format "&%s" (emit (first args))))

(defmethod c-invoke #'vp/&
[{:keys [args]}]
(format "&%s" (emit (first args))))

(defmethod c-invoke #'vp/sizeof
[{:keys [args]}]
(if (= (count args) 2)
Expand Down
12 changes: 7 additions & 5 deletions src/vybe/flecs.clj
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
ecs_os_api_log_t ecs_os_api_log_t$Function
ecs_os_api_abort_t ecs_os_api_abort_t$Function
ecs_os_api_t ecs_ref_t ecs_system_t ecs_query_t
ecs_system_stats_t ecs_query_stats_t)
ecs_system_stats_t ecs_query_stats_t ecs_system_desc_t
ecs_observer_t)
(java.lang.foreign AddressLayout MemoryLayout$PathElement MemoryLayout
ValueLayout ValueLayout$OfDouble ValueLayout$OfLong
ValueLayout$OfInt ValueLayout$OfBoolean ValueLayout$OfFloat
Expand All @@ -31,9 +32,9 @@

;; -- Flecs types
(vp/defcomp ecs_type_t (org.vybe.flecs.ecs_type_t/layout))
(vp/defcomp ecs_system_desc_t (org.vybe.flecs.ecs_system_desc_t/layout))
(vp/defcomp ecs_observer_desc_t (org.vybe.flecs.ecs_observer_desc_t/layout))
(vp/defcomp observer_t (org.vybe.flecs.ecs_observer_t/layout))

(vp/defcomp observer_t (ecs_observer_t/layout))
(vp/defcomp iter_t (ecs_iter_t/layout))
(vp/defcomp query_desc_t (ecs_query_desc_t/layout))
(vp/defcomp app_desc_t (ecs_app_desc_t/layout))
Expand All @@ -43,7 +44,8 @@
(vp/defcomp query_t (ecs_query_t/layout))
(vp/defcomp ref_t (ecs_ref_t/layout))
(vp/defcomp os_api_t (ecs_os_api_t/layout))
(vp/defcomp os_api_t (ecs_os_api_t/layout))
(vp/defcomp system_desc_t (ecs_system_desc_t/layout))
(vp/defcomp entity_desc_t (ecs_entity_desc_t/layout))

(vp/defcomp DocDescription (EcsDocDescription/layout))
(vp/defcomp Identifier (org.vybe.flecs.EcsIdentifier/layout))
Expand Down Expand Up @@ -1841,7 +1843,7 @@
opts

_system-id (vf.c/ecs-system-init
w (ecs_system_desc_t
w (system_desc_t
(merge {:entity e
:immediate immediate
:query (parse-query-expr w query-expr)}
Expand Down
3 changes: 3 additions & 0 deletions src/vybe/game.clj
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,9 @@
(defn- -safe-eval-model-meta
[node v]
(cond
(nil? v)
v

(vector? v)
(mapv #(-safe-eval-model-meta node %) v)

Expand Down
5 changes: 5 additions & 0 deletions src/vybe/panama.clj
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,11 @@
[v]
(.address (mem v)))

(defn &
"Get address from a value (same as `address`)."
[v]
(.address (mem v)))

(declare -vybe-popaque-rep)

(deftype+ VybePOpaque [-mem-segment identifier -component]
Expand Down
8 changes: 4 additions & 4 deletions test/vybe/c_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
;; From https://github.com/supercollider/example-plugins/blob/main/03-AnalogEcho/AnalogEcho.cpp
(vc/defn* mydsp :- :void
[unit :- [:* vc/Unit]
^:mut echo :- [:* AnalogEcho]
echo :- [:* AnalogEcho]
n-samples :- :int]
(let [[input] (:in_buf @unit)
[output] (:out_buf @unit)
Expand All @@ -87,8 +87,8 @@
coeff 0.95
buf (:buf @echo)
mask (:mask @echo)
^:mut write_phase (:write_phase @echo)
^:mut s1 (:s1 @echo)
write_phase (:write_phase @echo)
s1 (:s1 @echo)

max-delay (:max_delay @echo)
delay (if (> 0.01 max-delay)
Expand Down Expand Up @@ -259,7 +259,7 @@
(is (> (myflecs 4) 500)))

(vc/defn* myraylib :- vt/Vector2
[^:mut myint :- :int]
[myint :- :int]
;; Check that myint is really mutable.
(swap! myint + 3)

Expand Down
29 changes: 23 additions & 6 deletions test/vybe/flecs_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
[clojure.edn :as edn]
[vybe.panama :as vp]
[vybe.type :as vt]
[vybe.c :as vc]
#_[matcher-combinators.test])
(:import
(java.lang.foreign Arena ValueLayout MemorySegment)
Expand Down Expand Up @@ -89,19 +90,35 @@
["alice" {:x 10.0, :y 20.0}]}
(set @*acc))))))

(vc/defn* ^:debug default-systems :- :void
[^:mut w :- :*]
(let [e (vf.c/ecs-entity-init w (vp/& (vf/entity_desc_t)))]
(vf.c/ecs-system-init
w (vf/system_desc_t
#_{:entity e
#_ #_ #_ #_ #_ #_:immediate immediate
:query (parse-query-expr w query-expr)
:callback (-system-callback
(fn [it-p]
(let [it (vp/jx-p->map it-p ecs_iter_t)
f-idx (mapv (fn [f] (f it)) f-arr)]
(doseq [idx (range (ecs_iter_t/count it-p))]
(each-handler (mapv (fn [f] (f idx)) f-idx))))))}))))

;; Based on https://github.com/SanderMertens/flecs/blob/master/examples/c/entities/basics/src/main.c
(deftest ex-1-w-map
;; Create the world.
(let [w (vf/make-world)]
#_(def w (vf/make-world))
#_(def w w)

(vf/eid w vt/Translation)
(vf/eid w vt/Rotation)
(vf/eid w vt/Scale)
(vf/eid w vt/Transform)
(vf/eid w :global)
(vf.c/vybe-default-systems-c w)
#_(do
(vf/eid w vt/Translation)
(vf/eid w vt/Rotation)
(vf/eid w vt/Scale)
(vf/eid w vt/Transform)
(vf/eid w :global)
(vf.c/vybe-default-systems-c w))

;; Create a observer.
(vf/with-observer w [:vf/name :ex-1-observer
Expand Down
28 changes: 18 additions & 10 deletions todo.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,24 @@
- [x] square
- [x] circle
- [x] animate blob
- [ ] VybeC
- [ ] move existing system into VybeC from C
- [ ] direct translation
- [ ] with-system working in VybeC
- [ ] `defsystem`?
- [ ] read from source if some function has a inline (or not) metadata telling
us the fn spec
- [ ] eval
- [ ] form to quickly evaluate stuff
- [ ] REPL plugin like portal does for cljs?
- [ ] better way to jump to definition
- [ ] allocator setting
- [ ] comptime
- [ ] exe mode
- [ ] check that we don't have unitialized resources in the init function if
in this mode
- [ ] interior light
- [ ] point light
- [ ] multiple worlds, render twice?
- [ ] 2-player where you pass one object to the other?
- [ ] 3d?
Expand Down Expand Up @@ -361,16 +379,6 @@
- [ ] charts in TVs
- [ ] press button
- [ ] GUI sound
- [ ] VybeC
- [ ] better way to jump to definition
- [ ] allocator setting
- [ ] comptime
- [ ] exe mode
- [ ] check that we don't have unitialized resources in the init function if
in this mode
- [ ] eval
- [ ] form to quickly evaluate stuff
- [ ] REPL plugin like portal does for cljs?
- [ ] use portal
- [ ] memory segment
- [ ] component
Expand Down

0 comments on commit d34b946

Please sign in to comment.