@@ -201,56 +201,136 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
201201;; textures
202202
203203(cond-expand ;; CONSTRUCTION-CASE
204- ((or debug) ;; tentative changes
204+ ((or gambit debug) ;; tentative changes
205205 ;; ; intentions:
206206 ;; ; 1. hide globals glCore:textures and glCore:tidx (at least)
207207 ;; ; 2. (short term) replace vector with distinct type
208208
209+ ;; (: (%%glCore:textures-ref t d) <<== (table-ref [abstract:glCore:textures] t d))
209210 (define %%glCore: textures-ref)
211+ ;; glCoreTextureCreate EXPORTED - ubiquitious
212+ ;; ;
213+ ;; ; (: (glCoreTextureCreate w h data #!optional (interpolation GL_LINEAR) (wrap GL_CLAMP))
214+ ;; ; -> fixnum)
210215 (define glCoreTextureCreate)
216+ ;; glCoreTextureReset -- TBD: unknown usage status
217+ ;; ;
218+ ;; ; (: glCoreTextureReset -> undefined)
219+ ;; ;
220+ ;; ; purpose: clear resources
211221 (define glCoreTextureReset)
212222
223+ ;; Implementation (volatile)
224+
225+ (define-type glCore: texture
226+ macros: prefix: %MATURITY+3%texture%macro-
227+ %%valid ;; FIXME: factor out from immutable components
228+ glidx ;; index (for opengl and internal table)
229+ %%-???-u32vector ;; what is this? mutable?
230+ width
231+ height
232+ (%%-???-u8vector: data unprintable: )
233+ pixeltype
234+ interpolation
235+ wrap
236+ )
237+
238+ (define (glCore: texture? x) (%MATURITY+3%texture%macro-glCore: texture? x)) ;; avoid eventually!
239+
240+ (define (glCore: texture-valid? texture)
241+ (%MATURITY+3%texture%macro-glCore: texture-%%valid texture))
242+
243+ (define (glCore: texture-invalidate! texture)
244+ (%MATURITY+3%texture%macro-glCore: texture-%%valid-set! texture #f))
245+
246+ (define (glCore: texture-valid! texture)
247+ (%MATURITY+3%texture%macro-glCore: texture-%%valid-set! texture #t))
248+
249+ (define (glCore: texture-%%-???-u32vector texture) ;; was vector-ref t 1
250+ (%MATURITY+3%texture%macro-glCore: texture-%%-???-u32vector texture))
251+
252+ (define (glCore: texture-width texture)
253+ (%MATURITY+3%texture%macro-glCore: texture-width texture))
254+
255+ (define (glCore: texture-height texture)
256+ (%MATURITY+3%texture%macro-glCore: texture-height texture))
257+
258+ (define (glCore: texture-data texture)
259+ (%MATURITY+3%texture%macro-glCore: texture-%%-???-u8vector: data texture))
260+
261+ (define (glCore: texture-pixeltype texture)
262+ (%MATURITY+3%texture%macro-glCore: texture-pixeltype texture))
263+
264+ (define (glCore: texture-pixeltype-set! texture)
265+ (%MATURITY+3%texture%macro-glCore: texture-pixeltype texture))
266+
267+ (define (glCore: texture-interpolation texture)
268+ (%MATURITY+3%texture%macro-glCore: texture-interpolation texture))
269+
270+ (define (glCore: texture-interpolation-set! texture)
271+ (%MATURITY+3%texture%macro-glCore: texture-interpolation texture))
272+
273+ (define (glCore: texture-wrap texture)
274+ (%MATURITY+3%texture%macro-glCore: texture-wrap texture))
275+
276+ (define (glCore: texture-wrap-set! texture)
277+ (%MATURITY+3%texture%macro-glCore: texture-wrap texture))
278+
213279 (let (;; TBD: not thread safe, assert exclusive access at least in debug
214280 (glCore: textures (make-table))
215- (glCore: tidx 0))
216- ;; should we use `(##still-copy (make-table))` for glCore:textures?
281+ (glCore: tidx 0)
282+ ;; TBD: now never using ##still-copy
283+ (maturity: use-still-copy/-1 (if #f ##still-copy identity)))
284+
285+ ;; ?? should we use `(##still-copy (make-table))` for glCore:textures?
217286 (define (glCore: textures-ref texture default)
218- (table-ref glCore: textures texture default))
219-
220- (define (%%glCoreTextureCreate w h data . aux)
221- (glcore: log 5 "glCoreTextureCreate")
222- (let* ((o1x (pair? aux))
223- (o2 (and o1x (cdr aux))))
224- (let ((idx glCore: tidx)
225- (pixeltype
226- (cond
227- ((fx= (u8vector-length data) (* w h)) GL_ALPHA)
228- ((fx= (u8vector-length data) (* 3 w h)) GL_RGB)
229- ((fx= (u8vector-length data) (* 4 w h)) GL_RGBA)
230- (else (log-error "glCoreTextureCreate: Invalid data range") #f)))
231- (interpolation (if o1x (car aux) GL_LINEAR))
232- (wrap (if (pair? o2) (car o2) GL_CLAMP)))
233- (table-set!
234- glCore: textures idx
235- (##still-copy
236- (vector #f (u32vector 0) w h (##still-copy data) pixeltype interpolation wrap)))
237- (set! glCore: tidx (fx+ glCore: tidx 1))
238- idx)))
287+ (if (%MATURITY+3%texture%macro-glCore: texture? texture)
288+ texture
289+ (table-ref glCore: textures texture default)))
290+
291+ (define (%%glCoreTextureCreate w h data #!optional (interpolation GL_LINEAR) (wrap GL_CLAMP))
292+ ;; (glcore:log 5 "glCoreTextureCreate")
293+ #;(MATURITY -1 "legacy; TBD: ensure resources are actually released" 'glCoreTextureCreate)
294+ (let ((idx glCore: tidx)
295+ (pixeltype
296+ (cond
297+ ((fx= (u8vector-length data) (* w h)) GL_ALPHA)
298+ ((fx= (u8vector-length data) (* 3 w h)) GL_RGB)
299+ ((fx= (u8vector-length data) (* 4 w h)) GL_RGBA)
300+ (else (log-error "glCoreTextureCreate: Invalid data range") #f))))
301+ (table-set!
302+ glCore: textures idx
303+ (%MATURITY+3%texture%macro-make-glCore: texture
304+ #f ;; volatile
305+ idx
306+ (u32vector 0) ;; unknown
307+ w h ;; 2d interval
308+ (maturity: use-still-copy/-1 data)
309+ pixeltype interpolation wrap))
310+ (set! glCore: tidx (fx+ glCore: tidx 1))
311+ idx))
239312
240313 ;; clear all textures
241- (define (%%glCoreTextureReset)
242- (glcore: log 5 "glCoreTextureReset")
243- (let ((tlist '()))
244- (table-for-each (lambda (k v) (set! tlist (append tlist (list k)))) glCore: textures)
245- (for-each (lambda (t) (_glCoreTextureReset t)) tlist)
246- ))
247-
248- (set! glCoreTextureReset %%glCoreTextureReset)
314+ (define (%%glCoreTextureReset!)
315+ (table-for-each
316+ (lambda (k entry)
317+ (when (glCore: texture-valid? entry)
318+ (glDeleteTextures 1 (%MATURITY+3%texture%macro-glCore: texture-%%-???-u32vector entry))
319+ (glCore: texture-invalidate! entry)))
320+ glCore: textures)
321+ (when #f ;; should we clean references too?
322+ (set! glCore: textures (make-table))
323+ (set! glCore: tidx 0)))
324+
325+ (unless glCore: textures (%%reset!))
326+
327+ (set! glCoreTextureReset %%glCoreTextureReset!)
249328 (set! %%glCore: textures-ref glCore: textures-ref)
250329 (set! glCoreTextureCreate %%glCoreTextureCreate))
251330
252331 ) ;; end of tentative changes
253332 (else ;; old version
333+
254334 ;; each entry is a vector of initflag,texure,w,h,u8data,pixeltype
255335 (define glCore: textures (##still-copy (make-table)))
256336 (define glCore: tidx 0)
@@ -259,6 +339,36 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
259339 (define (%%glCore: textures-ref texture default)
260340 (table-ref glCore: textures texture default))
261341
342+ (define (glCore: texture-valid? texture)
343+ (vector-ref texture 0))
344+
345+ (define (glCore: texture-invalidate! texture)
346+ (vector-set! texture 0 #f))
347+
348+ (define (glCore: texture-valid! texture)
349+ (vector-set! texture 0 #t))
350+
351+ (define (glCore: texture-%%-???-u32vector texture) ;; was vector-ref t 1
352+ (vector-ref texture 1))
353+
354+ (define (glCore: texture-width texture)
355+ (vector-ref texture 2))
356+
357+ (define (glCore: texture-height texture)
358+ (vector-ref texture 3))
359+
360+ (define (glCore: texture-data texture)
361+ (vector-ref texture 4))
362+
363+ (define (glCore: texture-pixeltype texture)
364+ (vector-ref texture 5))
365+
366+ (define (glCore: texture-interpolation texture)
367+ (vector-ref texture 6))
368+
369+ (define (glCore: texture-wrap texture)
370+ (vector-ref texture 7))
371+
262372 (define (glCoreTextureCreate w h data . aux)
263373 (glcore: log 5 "glCoreTextureCreate")
264374 (let* ((o1x (pair? aux))
@@ -278,38 +388,54 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
278388 (vector #f (u32vector 0) w h (##still-copy data) pixeltype interpolation wrap)))
279389 (set! glCore: tidx (fx+ glCore: tidx 1))
280390 idx)))
391+ ;; reset a texture entry
392+ (define (_glCoreTextureReset t)
393+ (glcore: log 5 "_glCoreTextureReset")
394+ (let ((entry (%%glCore: textures-ref t #f)))
395+ (if (and entry (glCore: texture-valid? entry))
396+ (begin
397+ (glDeleteTextures 1 (glCore: texture-%%-???-u32vector entry))
398+ (glCore: texture-invalidate! entry)))))
281399
282400 ;; clear all textures
283401 (define (glCoreTextureReset)
284402 (glcore: log 5 "glCoreTextureReset")
285- (let ((tlist '()))
403+ (let ((tlist '())) ;; collect list of entries
404+ ;; ;
405+ ;; ; Jikes: by ... no way!
286406 (table-for-each (lambda (k v) (set! tlist (append tlist (list k)))) glCore: textures)
287- (for-each (lambda (t) (_glCoreTextureReset t)) tlist)
288- ))
407+ (for-each (lambda (t) (_glCoreTextureReset t)) tlist)))
289408
290409 ) ;; end of old version
291410 ) ;; end of CONSTRUCTION-CASE
292411
293412
413+ (define (glCore: textures-ref
414+ num #!optional
415+ (failure (lambda (num) (error "glCore: textures-ref: unbound index" num))))
416+ (cond
417+ ((fixnum? num) (or (%%glCore: textures-ref t #f) (failure num)))
418+ (else (error "not a fixnum" num glCore: textures-ref))))
419+
294420;; return texture width
295421(define (glCoreTextureWidth t)
296422 (glcore: log 5 "glCoreTextureWidth")
297423 (let ((entry (%%glCore: textures-ref t #f)))
298- (if entry (vector-ref entry 2 ) (begin
424+ (if entry (glCore: texture-width entry) (begin
299425 (log-error "glCoreTextureWidth: unbound index " t) #f))))
300426
301427;; return texture height
302428(define (glCoreTextureHeight t)
303429 (glcore: log 5 "glCoreTextureWidth")
304430 (let ((entry (%%glCore: textures-ref t #f)))
305- (if entry (vector-ref entry 3 ) (begin
431+ (if entry (glCore: texture-height entry) (begin
306432 (log-error "glCoreTextureHeight: unbound index " t) #f))))
307433
308434;; return texture data
309435(define (glCoreTextureData t)
310436 (glcore: log 5 "glCoreTextureData")
311437 (let ((entry (%%glCore: textures-ref t #f)))
312- (if entry (vector-ref entry 4 ) (begin
438+ (if entry (glCore: texture-data entry) (begin
313439 (log-error "glCoreTextureData: unbound index " t) #f))))
314440
315441;; %%%%%%%%%%%%%%%%%%%%
@@ -353,8 +479,8 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
353479(define (glCoreTextureDraw x y w0 h0 t x1 y1 x2 y2 r . colors)
354480 (let ((entry (%%glCore: textures-ref t #f)))
355481 (if entry
356- (let ((w (flo (if (fx= (fix w0) 0) (vector-ref entry 2 ) w0)))
357- (h (flo (if (fx= (fix h0) 0) (vector-ref entry 3 ) h0))))
482+ (let ((w (flo (if (fx= (fix w0) 0) (glCore: texture-width entry) w0)))
483+ (h (flo (if (fx= (fix h0) 0) (glCore: texture-height entry) h0))))
358484 (if (null? glcore: cliplist)
359485 (if (pair? colors)
360486 (glCore: TextureDrawUnClipped
@@ -476,22 +602,46 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
476602;; to use this, first modify data returned with glCoreTextureData..
477603(define (glCoreTextureUpdate t)
478604 (glcore: log 5 "glCoreTextureUpdate")
605+ (if (fixnum? t) (set! t (%%glCore: textures-ref t #f)))
479606 (_glCoreTextureBind t) ;; select the texture as current
480- (let* ((entry (%%glCore: textures-ref t #f))
481- (w (vector-ref entry 2))
482- (h (vector-ref entry 3))
483- (data (vector-ref entry 4))
484- (pixeltype (vector-ref entry 5)))
485- (glTexSubImage2D GL_TEXTURE_2D 0 0 0 w h pixeltype GL_UNSIGNED_BYTE data)
486- ))
607+ (let ((entry t))
608+ (let ((w (glCore: texture-width entry))
609+ (h (glCore: texture-height entry))
610+ (data (glCore: texture-data entry))
611+ (pixeltype (glCore: texture-pixeltype entry)))
612+ (glTexSubImage2D GL_TEXTURE_2D 0 0 0 w h pixeltype GL_UNSIGNED_BYTE data))))
613+
614+ (define (%%glCoreTextureInit! texture) ;; texture structure
615+ (let ((u32t (glCore: texture-%%-???-u32vector texture))
616+ (w (glCore: texture-width texture))
617+ (h (glCore: texture-height texture))
618+ (data (glCore: texture-data texture))
619+ (pixeltype (glCore: texture-pixeltype texture))
620+ (interp (glCore: texture-interpolation texture))
621+ (wrap (glCore: texture-wrap texture)))
622+ (glGenTextures 1 u32t)
623+ (if (or (= (u32vector-ref u32t 0) GL_INVALID_VALUE)
624+ ;; this is a general check that gl is working in this thread
625+ (= (glIsEnabled GL_TEXTURE_2D) 0))
626+ (glcore: log 5 "_glCoreTextureInit: failed to generate texture")
627+ (begin
628+ (glCore: texture-valid! texture) ;; mark as initialized
629+ (glBindTexture GL_TEXTURE_2D (u32vector-ref u32t 0))
630+ (glTexParameteri GL_TEXTURE_2D GL_TEXTURE_MIN_FILTER interp)
631+ (glTexParameteri GL_TEXTURE_2D GL_TEXTURE_MAG_FILTER interp)
632+ (glTexParameteri GL_TEXTURE_2D GL_TEXTURE_WRAP_S wrap)
633+ (glTexParameteri GL_TEXTURE_2D GL_TEXTURE_WRAP_T wrap)
634+ (glTexImage2D GL_TEXTURE_2D 0 pixeltype w h 0 pixeltype GL_UNSIGNED_BYTE data)))))
487635
488636(define (_glCoreTextureBind t)
489637 (glcore: log 5 "_glCoreTextureBind")
490- (let ((entry (%%glCore: textures-ref t #f)))
638+ (let ((entry (if (fixnum? t) ( %%glCore: textures-ref t #f) t )))
491639 (if entry
492640 (begin
493- (unless (vector-ref entry 0) (_glCoreTextureInit t)) ;; cache texture `t`
494- (let ((tx (u32vector-ref (vector-ref entry 1) 0)))
641+ (unless (glCore: texture-valid? entry)
642+ ;; TBD: maybe move into the reference operation?
643+ (%%glCoreTextureInit! entry))
644+ (let ((tx (u32vector-ref (glCore: texture-%%-???-u32vector entry) 0)))
495645 (if (not (= glCore: curtexture tx))
496646 (begin
497647 (glBindTexture GL_TEXTURE_2D tx)
@@ -500,40 +650,10 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
500650
501651(define (_glCoreTextureInit t)
502652 (glcore: log 5 "_glCoreTextureInit")
503- (let* ((entry (%%glCore: textures-ref t #f))
504- (u32t (vector-ref entry 1))
505- (w (vector-ref entry 2))
506- (h (vector-ref entry 3))
507- (data (vector-ref entry 4))
508- (pixeltype (vector-ref entry 5))
509- (interp (vector-ref entry 6))
510- (wrap (vector-ref entry 7)))
511- (glGenTextures 1 u32t)
512- (if (or (= (u32vector-ref u32t 0) GL_INVALID_VALUE)
513- ;this is a general check that gl is working in this thread
514- (= (glIsEnabled GL_TEXTURE_2D) 0))
515- (glcore: log 5 "_glCoreTextureInit: failed to generate texture")
516- (begin
517- (vector-set! entry 0 #t) ;; mark as initialized
518- (glBindTexture GL_TEXTURE_2D (u32vector-ref u32t 0))
519- (glTexParameteri GL_TEXTURE_2D GL_TEXTURE_MIN_FILTER interp)
520- (glTexParameteri GL_TEXTURE_2D GL_TEXTURE_MAG_FILTER interp)
521- (glTexParameteri GL_TEXTURE_2D GL_TEXTURE_WRAP_S wrap)
522- (glTexParameteri GL_TEXTURE_2D GL_TEXTURE_WRAP_T wrap)
523- (glTexImage2D GL_TEXTURE_2D 0 pixeltype w h 0 pixeltype GL_UNSIGNED_BYTE data)
524- ))
525- ))
526-
527- ;; reset a texture entry
528- (define (_glCoreTextureReset t)
529- (glcore: log 5 "_glCoreTextureReset")
530- (let* ((entry (%%glCore: textures-ref t #f))
531- (u32t (vector-ref entry 1)))
532- (if (vector-ref entry 0) (begin
533- (glDeleteTextures 1 u32t)
534- (vector-set! entry 0 #f) ;; mark as uninitialized
535- ))
536- ))
653+ (unless (fixnum? t) (error "_glCoreTextureInit: wrong argument type"))
654+ (let ((entry (%%glCore: textures-ref t #f)))
655+ (if entry (%%glCoreTextureInit! entry)
656+ (log-error "_glCoreTextureInit: unknown index " t))))
537657
538658;; take screen shot
539659(define (glCoreReadPixels x y w h)
0 commit comments