Setup the opengl context on a separate thread

This requires all opengl functions to be executed on this separate
thread.
This commit is contained in:
Renaud Casenave-Péré 2015-08-25 18:24:11 +02:00
parent 99e97f7b71
commit 6d3a0e19e7
2 changed files with 76 additions and 31 deletions

View file

@ -8,25 +8,52 @@
:stoe/core/utils
:stoe/core/time
:stoe/core/modules
:stoe/core/thread
:stoe/core/jobs
:stoe/engine/viewport
:stoe/engine/scene))
(in-package :stoe/engine/render)
(defclass render-thread (specialized-thread)
())
(defvar *render-thread* nil)
(defun initialize (&optional argv)
"Initialize the render module.
Create an opengl context attached to a window."
(format t "Initialize Render module~%")
(viewport-initialize argv)
(compile-all-shaders))
(viewport-configure argv)
(setf *render-thread* (push-new-thread 'render-thread "Render thread")))
(defun finalize ()
"Finalize the render module.
Destroy the opengl context and the related resources."
(format t "Finalize Render module~%")
(viewport-finalize))
(terminate-thread *render-thread*))
(defun update (delta-time)
(declare (ignore delta-time))
(during-one-frame))
(declare (ignore delta-time)))
(defmodule stoe/engine/render)
(defmethod thread-initialize ((thread render-thread))
(format t "Initialize ~a~%" (name thread))
(viewport-initialize)
(compile-all-shaders))
(defmethod thread-finalize ((thread render-thread))
(format t "Finalize ~a~%" (name thread))
(destroy-all-shaders)
(viewport-finalize))
(defmethod thread-process ((thread render-thread))
(let ((clock (make-clock)))
(loop until (thread-terminate-p thread)
do (restartable
(during-one-frame
(loop for job = (get-next-job thread)
while job
do (progn
(format t "Thread ~a: Running job ~a~%" (name thread) (id job))
(job-run job thread))))
(update-clock clock)))))

View file

@ -9,23 +9,40 @@
:stoe/engine/input)
(:export #:glsl-version
#:support-gl-version-p
#:viewport-configure
#:viewport-initialize
#:viewport-finalize
#:during-one-frame))
(in-package :stoe/engine/viewport)
(defvar *gl-major-version* nil)
(defvar *gl-minor-version* nil)
(defvar *glsl-version* nil)
(defstruct gl-config
major minor glsl)
(defstruct viewport-config
title width height)
(defvar *gl-config* nil)
(defvar *main-conf* nil)
(defvar *main-window* nil)
(defun support-gl-version-p (version)
(multiple-value-bind (maj min) (floor version 10)
(or (< maj *gl-major-version*)
(and (= maj *gl-major-version*)
(<= min *gl-minor-version*)))))
(and *gl-config*
(multiple-value-bind (maj min) (floor version 10)
(or (< maj (gl-config-major *gl-config*))
(and (= maj (gl-config-major *gl-config*))
(<= min (gl-config-minor *gl-config*)))))))
(defun glsl-version () *glsl-version*)
(defun glsl-version () (and *gl-config* (gl-config-glsl *gl-config*)))
(defun viewport-configure (&optional argv)
(let ((config (make-viewport-config :title (get-command-line-option argv "--title" "Stoe")
:width (get-command-line-option-number argv "--width" 800)
:height (get-command-line-option-number argv "--height" 600)))
(version (get-command-line-option-number argv "--opengl")))
(setf *main-conf* config)
(when version
(multiple-value-bind (maj min) (floor version 10)
(setf *gl-config* (make-gl-config :major maj :minor min))))))
(defun initialize-context ()
(gl:enable :cull-face)
@ -36,24 +53,24 @@
(gl:depth-func :lequal)
(gl:depth-range 0.0 1.0))
(defun viewport-initialize (&optional argv)
(defun viewport-initialize ()
"Initialize the viewport."
(let ((title (get-command-line-option argv "--title" "Stoe"))
(width (get-command-line-option-number argv "--width" 800))
(height (get-command-line-option-number argv "--height" 600))
(version (get-command-line-option-number argv "--opengl")))
(if version
(multiple-value-bind (maj min) (floor version 10)
(setf *gl-major-version* maj
*gl-minor-version* min
*main-window* (create-window title width height :major maj :minor min)))
(progn
(setf *main-window* (create-window title width height))
(setf *gl-major-version* (gl:get-integer :major-version)
*gl-minor-version* (gl:get-integer :minor-version))))
(setf *glsl-version* (with-input-from-string (in (gl:get-string :shading-language-version))
(stoe/core/file:safe-read in)))
(initialize-context)))
(if *gl-config*
(setf *main-window* (create-window (viewport-config-title *main-conf*)
(viewport-config-width *main-conf*)
(viewport-config-height *main-conf*)
:major (gl-config-major *gl-config*)
:minor (gl-config-minor *gl-config*)))
(progn
(setf *main-window* (create-window (viewport-config-title *main-conf*)
(viewport-config-width *main-conf*)
(viewport-config-height *main-conf*)))
(setf *gl-config* (make-gl-config :major (gl:get-integer :major-version)
:minor (gl:get-integer :minor-version)))))
(setf (gl-config-glsl *gl-config*)
(with-input-from-string (in (gl:get-string :shading-language-version))
(stoe/core/file:safe-read in)))
(initialize-context))
(defun viewport-finalize ()
"Finalize the viewport."
@ -73,7 +90,8 @@
`(progn
(clear-buffers)
,@body
(swap-main-buffers)))
(swap-main-buffers)
(poll-events)))
(defun poll-events ()
"Poll events from the window manager.