Browse Source

Fix ProfilerAutoInstrument, use fprintf, tests

* Use fprintf because it doesn't buffer its output
* Move Tracy test into auto-test block
* Test Tracy and ProfilerAutoInstrument
windows-imgui
Macoy Madson 2 years ago
parent
commit
8bf746aebf
  1. 12
      src/Aubio.cake
  2. 10
      src/ImGui.cake
  3. 26
      src/Math.cake
  4. 2
      src/OgreInitialize.cake
  5. 2
      src/OpenGL.cake
  6. 2
      src/ProfilerAutoInstrument.cake
  7. 18
      src/SDL.cake
  8. 49
      src/Tracy.cake
  9. 27
      test/src/GameLibTests.cake
  10. 25
      test/src/TestAutoInstrument.cake

12
src/Aubio.cake

@ -28,10 +28,10 @@
(var detected-pitch-buffer (* fvec_t) (new_fvec buffer-size))
(aubio_pitch_do pitch-detection buffer-float detected-pitch-buffer)
;; (printf "Buffer:\n")
;; (fprintf stderr "Buffer:\n")
;; (fvec_print buffer-float)
;; (printf "Pitch buffer:\n")
;; (fprintf stderr "Pitch buffer:\n")
;; (fvec_print detected-pitch-buffer)
(var detected-pitch float (fvec_get_sample detected-pitch-buffer 0))
@ -46,7 +46,7 @@
;;
(defun test--aubio (&return int)
(printf "Hello, aubio!\n")
(fprintf stderr "Hello, aubio!\n")
(var buffer-size uint_t 4096)
(var hop-size uint_t 4096) ;; TODO: What is this?
@ -61,13 +61,13 @@
(new_aubio_source "assets/Tone_440.wav" sample-rate hop-size))
(var frames-read uint_t 0)
(aubio_source_do sound-source buffer (addr frames-read))
(printf "Read %d frames\n" frames-read)
(fprintf stderr "Read %d frames\n" frames-read)
(var detected-pitch (* fvec_t) (new_fvec buffer-size))
(aubio_pitch_do pitch-detection buffer detected-pitch)
(printf "Pitch: %f\n" (fvec_get_sample detected-pitch 0))
(printf "\n")
(fprintf stderr "Pitch: %f\n" (fvec_get_sample detected-pitch 0))
(fprintf stderr "\n")
(del_fvec buffer)
(del_fvec detected-pitch)

10
src/ImGui.cake

@ -89,10 +89,10 @@
(make-imgui-sdl-gl3w-application ;; If you want your own entry point, check this macro's body
test--imgui-main
"GameLib ImGui test"
(scope (printf "Initializing\n")) ;; Initialization
(scope (fprintf stderr "Initializing\n")) ;; Initialization
(scope ;; Once per frame
(imgui-call ShowDemoWindow))
(scope (printf "Shut down\n"))))) ;; Shutdown
(scope (fprintf stderr "Shut down\n"))))) ;; Shutdown
;; Different loaders are available. Loader selection is done via macro to allow that flexibility
(defmacro imgui-use-sdl-gl3w ()
@ -156,7 +156,7 @@
(SDL_GL_SetSwapInterval 1) ;; Enable vsync
(unless (ImGuiSDLOpenGL_InitializeGLLoader)
(printf "Failed ImGuiSDLOpenGL_InitializeGLLoader\n")
(fprintf stderr "Failed ImGuiSDLOpenGL_InitializeGLLoader\n")
(return 1))
(imgui-call CreateContext)
@ -193,7 +193,7 @@
;; Note: Passing in null for the context will break Viewports branch of ImGui
(unless (ImGui_ImplSDL2_InitForOpenGL window gl-context)
(printf "Failed ImGui_ImplSDL2_InitForOpenGL\n")
(fprintf stderr "Failed ImGui_ImplSDL2_InitForOpenGL\n")
(return 1))
(ImGui_ImplOpenGL3_Init glsl-version)
@ -233,7 +233,7 @@
(when exit-reason
(SDL_HideWindow window)
(printf "Exiting. Reason: %s\n" exit-reason))
(fprintf stderr "Exiting. Reason: %s\n" exit-reason))
(token-splice shutdown)

26
src/Math.cake

@ -219,12 +219,12 @@
(defun mat4-print (mat mat4)
(var vec-index int 0)
(while (< vec-index 4)
(printf "%d [%f %f %f %f]\n"
vec-index
(at 0 vec-index mat)
(at 1 vec-index mat)
(at 2 vec-index mat)
(at 3 vec-index mat))
(fprintf stderr "%d [%f %f %f %f]\n"
vec-index
(at 0 vec-index mat)
(at 1 vec-index mat)
(at 2 vec-index mat)
(at 3 vec-index mat))
(incr vec-index)))
;; Useful when e.g. passing the matrix into OpenGL
@ -244,32 +244,32 @@
(comptime-cond
('auto-test ;; Wrapped so we don't require compiling all the macros if unused
(defun test--math (&return int)
(printf "%f radians = %f degrees = %f radians\n"
(fprintf stderr "%f radians = %f degrees = %f radians\n"
g-pi (radians-to-degrees g-pi) (degrees-to-radians 180.f))
(var my-vec vec3 (array 1.f 2.f 3.f))
(printf "%f %f %f\n" (vec-xyz my-vec))
(fprintf stderr "%f %f %f\n" (vec-xyz my-vec))
(var vec-a vec3 (array 0.f 1.f 2.f))
(printf "%f %f %f magnitude = %f\n" (vec-xyz vec-a) (vec3-length vec-a))
(fprintf stderr "%f %f %f magnitude = %f\n" (vec-xyz vec-a) (vec3-length vec-a))
(printf "\nTranslation matrix:\n")
(fprintf stderr "\nTranslation matrix:\n")
(var mat-a mat4 (mat4-identity))
(mat4-print (mat4-multiply mat-a (mat4-translate (array 1.f 2.f 3.f))))
(printf "\n45 degrees about the Y axis matrix:\n")
(fprintf stderr "\n45 degrees about the Y axis matrix:\n")
(mat4-print (mat4-rotate-degrees 45.f g-up-axis))
;; Demonstrate rotating, then translating. Due to column-vector interpretation, multiplication
;; order is reversed
(printf "\n45 degrees about the Y axis, then translate:\n")
(fprintf stderr "\n45 degrees about the Y axis, then translate:\n")
(var transformation-mat mat4
(mat4-multiply (mat4-translate (array 1.f 2.f 3.f))
(mat4-rotate-degrees 45.f g-up-axis)))
(mat4-print transformation-mat)
(var transformed-vec vec3 (mat4-transform-vec3 transformation-mat (array 1.f 0.f 0.f)))
(printf "%f %f %f\n" (vec-xyz transformed-vec))
(fprintf stderr "%f %f %f\n" (vec-xyz transformed-vec))
(return 0))))

2
src/OgreInitialize.cake

@ -157,7 +157,7 @@
(var renderSystem (* (in Ogre RenderSystem))
(call-on-ptr getRenderSystemByName g-ogre-root "OpenGL 3+ Rendering Subsystem"))
(unless renderSystem
(printf "Render system not found!\n")
(fprintf stderr "Render system not found!\n")
(return false))
;; renderSystem->setConfigOption("Display Frequency", "N/A");

2
src/OpenGL.cake

@ -150,7 +150,7 @@ void main()\n
(SDL_GL_SwapWindow window))
(when exit-reason
(printf "Exiting. Reason: %s\n" exit-reason))
(fprintf stderr "Exiting. Reason: %s\n" exit-reason))
(sdl-shutdown window)
(return 0))))

2
src/ProfilerAutoInstrument.cake

@ -118,4 +118,4 @@
(Log "Profiling: Auto-instrumenting code completed.\n")
(return true))
(add-compile-time-hook post-references-resolved profiling-auto-instrument))
(add-compile-time-hook post-references-resolved profiling-auto-instrument)

18
src/SDL.cake

@ -15,7 +15,7 @@
(struct SDL_AudioSpec))
(defun sdl-print-error ()
(printf "SDL_Error: %s\n" (SDL_GetError)))
(fprintf stderr "SDL_Error: %s\n" (SDL_GetError)))
;; This supports drawing using SDL functions
(defun sdl-initialize-for-2d (window-out (* (* SDL_Window))
@ -141,21 +141,21 @@
(set (deref device-names-out) (type-cast
(calloc (sizeof (type (* (const char)))) num-devices)
(* (* (const char)))))
(printf "Available %s devices:\n" (? is-capture "recording" "playback"))
(fprintf stderr "Available %s devices:\n" (? is-capture "recording" "playback"))
(var i int 0)
(while (< i num-devices)
(var device-name (* (const char)) (SDL_GetAudioDeviceName i is-capture))
(when device-name
(printf "\t[%d] %s\n" i device-name)
(fprintf stderr "\t[%d] %s\n" i device-name)
(set (at i (deref device-names-out)) (strdup device-name)))
(incr i))
(return num-devices))
(defun sdl-audio-list-specification (spec (* SDL_AudioSpec))
(printf "freq: %d\n" (path spec > freq))
(printf "format: %d\n" (path spec > format))
(printf "channels: %d\n" (path spec > channels))
(printf "samples: %d\n" (path spec > samples)))
(fprintf stderr "freq: %d\n" (path spec > freq))
(fprintf stderr "format: %d\n" (path spec > format))
(fprintf stderr "channels: %d\n" (path spec > channels))
(fprintf stderr "samples: %d\n" (path spec > samples)))
(defun sdl-audio-free-device-list (device-names (* (* (const char))) num-devices int)
(var i int 0)
@ -188,7 +188,7 @@
;;
(defun test--sdl-main (&return int)
(printf "Hello, SDL!\n")
(fprintf stderr "Hello, SDL!\n")
(var window (* SDL_Window) null)
(unless (sdl-initialize-for-2d (addr window) "GameLib" 640 480) (return 1))
@ -206,7 +206,7 @@
(SDL_UpdateWindowSurface window))
(when exit-reason
(printf "Exiting. Reason: %s\n" exit-reason))
(fprintf stderr "Exiting. Reason: %s\n" exit-reason))
(sdl-shutdown window)
(return 0))

49
src/Tracy.cake

@ -5,26 +5,6 @@
(c-import &with-decls "Tracy.hpp" "TracyC.h")
;; TODO: Linux only
(c-import "unistd.h")
(defun test--tracy-main (&return int)
(ZoneScopedN "main")
(printf "Waiting for profiler to connect...\n")
(while (not (call-on IsConnected (call (in tracy GetProfiler))))
(ZoneScopedN "wait for profiler")
(sleep 1))
(var i int 0)
(var num-times (const int) 10)
(while (< i num-times)
(ZoneScopedN "hot loop")
(printf "hot loop %d / %d\n" i num-times)
(sleep 1)
(incr i))
(return 0))
(defmacro scope-timed (scope-label string &rest body any)
(tokenize-push output
(scope
@ -68,6 +48,35 @@
(tokenize-push output (TracyCZoneEnd (token-splice varname)))
(return true))
;;
;; Testing
;;
(comptime-cond
('auto-test
;; TODO: Linux only
(c-import "unistd.h")
(defun test--tracy-main (&return int)
(ZoneScopedN "main")
(fprintf stderr "Waiting for profiler to connect...\n")
(while (not (call-on IsConnected (call (in tracy GetProfiler))))
(ZoneScopedN "wait for profiler")
(sleep 1))
(var i int 0)
(var num-times (const int) 10)
(while (< i num-times)
(ZoneScopedN "hot loop")
(fprintf stderr "hot loop %d / %d\n" i num-times)
(sleep 1)
(incr i))
(return 0))
;; Workaround for auto-test, which needs to include infectious Tracy.hpp
(add-c-search-directory-global "Dependencies/tracy")))
;;
;; Building
;;

27
test/src/GameLibTests.cake

@ -43,6 +43,9 @@
;; (var test-opengl-only bool true)
(var test-opengl-only bool false)
(var test-ogre bool false)
;; (var test-ogre bool true)
(cond
(test-minimal
(gamelib-run-test
@ -63,19 +66,25 @@
(array platform-config
"../src/AutoTest.cake" "../src/SDL.cake" "../src/Math.cake"
"../src/Aubio.cake" "../src/ImGui.cake" "../src/Dictionary.cake"
"../src/DynamicArray.cake" "../src/OpenGL.cake"))
"../src/DynamicArray.cake" "../src/OpenGL.cake" "../src/Tracy.cake"))
;; Because auto-instrumentation does not play nice with AutoTest
(gamelib-run-test "Profiler auto-instrument"
(array platform-config "../src/ProfilerAutoInstrument.cake"
"src/TestAutoInstrument.cake"))
(gamelib-run-test "Ogre" (array platform-config "src/OgreApp.cake"))
(gamelib-run-test "SDL Ogre" (array platform-config "src/SDLOgreApp.cake"))
(when test-ogre
(gamelib-run-test "Ogre" (array platform-config "src/OgreApp.cake"))
(gamelib-run-test "SDL Ogre" (array platform-config "src/SDLOgreApp.cake"))
(gamelib-build "Vocal Game (hot reload)"
(array platform-config "src/MakeHotReload.cake" "src/VocalGame.cake"))
(gamelib-run-test "Hot-loader"
(array platform-config "src/Loader.cake"))
(gamelib-run-test "Hot-loader"
(array platform-config "src/Loader.cake"))
(gamelib-build "Vocal Game (hot reload)"
(array platform-config "src/MakeHotReload.cake" "src/VocalGame.cake"))
(gamelib-run-test "Vocal Game (no reload)"
(array platform-config "src/NoHotReload.cake" "src/VocalGame.cake"))))
(gamelib-run-test "Vocal Game (no reload)"
(array platform-config "src/NoHotReload.cake" "src/VocalGame.cake")))))
(Log "\nGameLibTests: All tests succeeded!\n")
(return true))

25
test/src/TestAutoInstrument.cake

@ -0,0 +1,25 @@
;; This might not be strictly necessary because auto-instrument imports Tracy for us. For now I'll
;; keep it until I figure out a better way to have instrument handle profiler selection
(import &comptime-only "Tracy.cake")
(c-import "<stdio.h>")
;; TODO: Linux only. For sleep()
(c-import "unistd.h")
(defun-local hot-loop-body ()
(sleep 1))
(defun main (&return int)
(fprintf stderr "Waiting for profiler to connect...\n")
(while (not (call-on IsConnected (call (in tracy GetProfiler))))
(time-this-scope wait-for-profiler "wait for profiler")
(sleep 1))
(var i int 0)
(var num-times (const int) 10)
(while (< i num-times)
(fprintf stderr "hot loop %d / %d\n" i num-times)
(hot-loop-body)
(incr i))
(return 0))
Loading…
Cancel
Save