]> gitweb.factorcode.org Git - factor.git/blob - basis/opengl/textures/textures.factor
Disposables are now registered in a global disposables set. To take advantage of...
[factor.git] / basis / opengl / textures / textures.factor
1 ! Copyright (C) 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors assocs cache colors.constants destructors kernel
4 opengl opengl.gl opengl.capabilities combinators images
5 images.tesselation grouping specialized-arrays.float sequences math
6 math.vectors math.matrices generalizations fry arrays namespaces
7 system locals literals ;
8 IN: opengl.textures
9
10 SYMBOL: non-power-of-2-textures?
11
12 : check-extensions ( -- )
13     #! ATI frglx driver doesn't implement GL_ARB_texture_non_power_of_two properly.
14     #! See thread 'Linux font display problem' April 2009 on Factor-talk
15     gl-vendor "ATI Technologies Inc." = not os macosx? or [
16         "2.0" { "GL_ARB_texture_non_power_of_two" }
17         has-gl-version-or-extensions?
18         non-power-of-2-textures? set
19     ] when ;
20
21 : gen-texture ( -- id ) [ glGenTextures ] (gen-gl-object) ;
22
23 : delete-texture ( id -- ) [ glDeleteTextures ] (delete-gl-object) ;
24
25 ERROR: unsupported-component-order component-order component-type ;
26
27 CONSTANT: image-internal-formats H{
28     { { A         ubyte-components          } $ GL_ALPHA8            }
29     { { A         ushort-components         } $ GL_ALPHA16           }
30     { { A         half-components           } $ GL_ALPHA16F_ARB      }
31     { { A         float-components          } $ GL_ALPHA32F_ARB      }
32     { { A         byte-integer-components   } $ GL_ALPHA8I_EXT       }
33     { { A         ubyte-integer-components  } $ GL_ALPHA8UI_EXT      }
34     { { A         short-integer-components  } $ GL_ALPHA16I_EXT      }
35     { { A         ushort-integer-components } $ GL_ALPHA16UI_EXT     }
36     { { A         int-integer-components    } $ GL_ALPHA32I_EXT      }
37     { { A         uint-integer-components   } $ GL_ALPHA32UI_EXT     }
38
39     { { L         ubyte-components          } $ GL_LUMINANCE8        }
40     { { L         ushort-components         } $ GL_LUMINANCE16       }
41     { { L         half-components           } $ GL_LUMINANCE16F_ARB  }
42     { { L         float-components          } $ GL_LUMINANCE32F_ARB  }
43     { { L         byte-integer-components   } $ GL_LUMINANCE8I_EXT   }
44     { { L         ubyte-integer-components  } $ GL_LUMINANCE8UI_EXT  }
45     { { L         short-integer-components  } $ GL_LUMINANCE16I_EXT  }
46     { { L         ushort-integer-components } $ GL_LUMINANCE16UI_EXT }
47     { { L         int-integer-components    } $ GL_LUMINANCE32I_EXT  }
48     { { L         uint-integer-components   } $ GL_LUMINANCE32UI_EXT }
49
50     { { R         ubyte-components          } $ GL_R8    }
51     { { R         ushort-components         } $ GL_R16   }
52     { { R         half-components           } $ GL_R16F  }
53     { { R         float-components          } $ GL_R32F  }
54     { { R         byte-integer-components   } $ GL_R8I   }
55     { { R         ubyte-integer-components  } $ GL_R8UI  }
56     { { R         short-integer-components  } $ GL_R16I  }
57     { { R         ushort-integer-components } $ GL_R16UI }
58     { { R         int-integer-components    } $ GL_R32I  }
59     { { R         uint-integer-components   } $ GL_R32UI }
60
61     { { INTENSITY ubyte-components          } $ GL_INTENSITY8        }
62     { { INTENSITY ushort-components         } $ GL_INTENSITY16       }
63     { { INTENSITY half-components           } $ GL_INTENSITY16F_ARB  }
64     { { INTENSITY float-components          } $ GL_INTENSITY32F_ARB  }
65     { { INTENSITY byte-integer-components   } $ GL_INTENSITY8I_EXT   }
66     { { INTENSITY ubyte-integer-components  } $ GL_INTENSITY8UI_EXT  }
67     { { INTENSITY short-integer-components  } $ GL_INTENSITY16I_EXT  }
68     { { INTENSITY ushort-integer-components } $ GL_INTENSITY16UI_EXT }
69     { { INTENSITY int-integer-components    } $ GL_INTENSITY32I_EXT  }
70     { { INTENSITY uint-integer-components   } $ GL_INTENSITY32UI_EXT }
71
72     { { DEPTH     ushort-components         } $ GL_DEPTH_COMPONENT16  }
73     { { DEPTH     u-24-components           } $ GL_DEPTH_COMPONENT24  }
74     { { DEPTH     uint-components           } $ GL_DEPTH_COMPONENT32  }
75     { { DEPTH     float-components          } $ GL_DEPTH_COMPONENT32F }
76
77     { { LA        ubyte-components          } $ GL_LUMINANCE8_ALPHA8       }
78     { { LA        ushort-components         } $ GL_LUMINANCE16_ALPHA16     }
79     { { LA        half-components           } $ GL_LUMINANCE_ALPHA16F_ARB  }
80     { { LA        float-components          } $ GL_LUMINANCE_ALPHA32F_ARB  }
81     { { LA        byte-integer-components   } $ GL_LUMINANCE_ALPHA8I_EXT   }
82     { { LA        ubyte-integer-components  } $ GL_LUMINANCE_ALPHA8UI_EXT  }
83     { { LA        short-integer-components  } $ GL_LUMINANCE_ALPHA16I_EXT  }
84     { { LA        ushort-integer-components } $ GL_LUMINANCE_ALPHA16UI_EXT }
85     { { LA        int-integer-components    } $ GL_LUMINANCE_ALPHA32I_EXT  }
86     { { LA        uint-integer-components   } $ GL_LUMINANCE_ALPHA32UI_EXT }
87
88     { { RG        ubyte-components          } $ GL_RG8    }
89     { { RG        ushort-components         } $ GL_RG16   }
90     { { RG        half-components           } $ GL_RG16F  }
91     { { RG        float-components          } $ GL_RG32F  }
92     { { RG        byte-integer-components   } $ GL_RG8I   }
93     { { RG        ubyte-integer-components  } $ GL_RG8UI  }
94     { { RG        short-integer-components  } $ GL_RG16I  }
95     { { RG        ushort-integer-components } $ GL_RG16UI }
96     { { RG        int-integer-components    } $ GL_RG32I  }
97     { { RG        uint-integer-components   } $ GL_RG32UI }
98
99     { { DEPTH-STENCIL u-24-8-components       } $ GL_DEPTH24_STENCIL8 }
100     { { DEPTH-STENCIL float-32-u-8-components } $ GL_DEPTH32F_STENCIL8 }
101
102     { { RGB       ubyte-components          } $ GL_RGB8               }
103     { { RGB       ushort-components         } $ GL_RGB16              }
104     { { RGB       half-components           } $ GL_RGB16F         }
105     { { RGB       float-components          } $ GL_RGB32F         }
106     { { RGB       byte-integer-components   } $ GL_RGB8I          }
107     { { RGB       ubyte-integer-components  } $ GL_RGB8UI         }
108     { { RGB       byte-integer-components   } $ GL_RGB8I          }
109     { { RGB       ubyte-integer-components  } $ GL_RGB8UI         }
110     { { RGB       short-integer-components  } $ GL_RGB16I         }
111     { { RGB       ushort-integer-components } $ GL_RGB16UI        }
112     { { RGB       int-integer-components    } $ GL_RGB32I         }
113     { { RGB       uint-integer-components   } $ GL_RGB32UI        }
114     { { RGB       u-5-6-5-components        } $ GL_RGB5               }
115     { { RGB       u-9-9-9-e5-components     } $ GL_RGB9_E5        }
116     { { RGB       float-11-11-10-components } $ GL_R11F_G11F_B10F }
117
118     { { RGBA      ubyte-components          } $ GL_RGBA8              }
119     { { RGBA      ushort-components         } $ GL_RGBA16             }
120     { { RGBA      half-components           } $ GL_RGBA16F        }
121     { { RGBA      float-components          } $ GL_RGBA32F        }
122     { { RGBA      byte-integer-components   } $ GL_RGBA8I         }
123     { { RGBA      ubyte-integer-components  } $ GL_RGBA8UI        }
124     { { RGBA      byte-integer-components   } $ GL_RGBA8I         }
125     { { RGBA      ubyte-integer-components  } $ GL_RGBA8UI        }
126     { { RGBA      short-integer-components  } $ GL_RGBA16I        }
127     { { RGBA      ushort-integer-components } $ GL_RGBA16UI       }
128     { { RGBA      int-integer-components    } $ GL_RGBA32I        }
129     { { RGBA      uint-integer-components   } $ GL_RGBA32UI       }
130     { { RGBA      u-5-5-5-1-components      } $ GL_RGB5_A1            }
131     { { RGBA      u-10-10-10-2-components   } $ GL_RGB10_A2           }
132 }
133
134 GENERIC: fix-internal-component-order ( order -- order' )
135
136 M: object fix-internal-component-order ;
137 M: BGR fix-internal-component-order drop RGB ;
138 M: BGRA fix-internal-component-order drop RGBA ;
139 M: ARGB fix-internal-component-order drop RGBA ;
140 M: ABGR fix-internal-component-order drop RGBA ;
141 M: RGBX fix-internal-component-order drop RGBA ;
142 M: BGRX fix-internal-component-order drop RGBA ;
143 M: XRGB fix-internal-component-order drop RGBA ;
144 M: XBGR fix-internal-component-order drop RGBA ;
145
146 : image-internal-format ( component-order component-type -- internal-format )
147     2dup
148     [ fix-internal-component-order ] dip 2array image-internal-formats at
149     [ 2nip ] [ unsupported-component-order ] if* ;
150
151 : reversed-type? ( component-type -- ? )
152     { u-9-9-9-e5-components float-11-11-10-components } member? ;
153
154 : (component-order>format) ( component-order component-type -- gl-format )
155     dup unnormalized-integer-components? [
156         swap {
157             { A [ drop GL_ALPHA_INTEGER_EXT ] }
158             { L [ drop GL_LUMINANCE_INTEGER_EXT ] }
159             { R [ drop GL_RED_INTEGER ] }
160             { LA [ drop GL_LUMINANCE_ALPHA_INTEGER_EXT ] }
161             { RG [ drop GL_RG_INTEGER ] }
162             { BGR [ drop GL_BGR_INTEGER ] }
163             { RGB [ drop GL_RGB_INTEGER ] }
164             { BGRA [ drop GL_BGRA_INTEGER ] }
165             { RGBA [ drop GL_RGBA_INTEGER ] }
166             { BGRX [ drop GL_BGRA_INTEGER ] }
167             { RGBX [ drop GL_RGBA_INTEGER ] }
168             [ swap unsupported-component-order ]
169         } case
170     ] [
171         swap {
172             { A [ drop GL_ALPHA ] }
173             { L [ drop GL_LUMINANCE ] }
174             { R [ drop GL_RED ] }
175             { LA [ drop GL_LUMINANCE_ALPHA ] }
176             { RG [ drop GL_RG ] }
177             { BGR [ reversed-type? GL_RGB GL_BGR ? ] }
178             { RGB [ reversed-type? GL_BGR GL_RGB ? ] }
179             { BGRA [ drop GL_BGRA ] }
180             { RGBA [ drop GL_RGBA ] }
181             { ARGB [ drop GL_BGRA ] }
182             { ABGR [ drop GL_RGBA ] }
183             { BGRX [ drop GL_BGRA ] }
184             { RGBX [ drop GL_RGBA ] }
185             { XRGB [ drop GL_BGRA ] }
186             { XBGR [ drop GL_RGBA ] }
187             { INTENSITY [ drop GL_INTENSITY ] }
188             { DEPTH [ drop GL_DEPTH_COMPONENT ] }
189             { DEPTH-STENCIL [ drop GL_DEPTH_STENCIL ] }
190             [ swap unsupported-component-order ]
191         } case
192     ] if ;
193
194 GENERIC: (component-type>type) ( component-order component-type -- gl-type )
195
196 M: object (component-type>type) unsupported-component-order ;
197
198 : four-channel-alpha-first? ( component-order component-type -- ? )
199     over component-count 4 =
200     [ drop alpha-channel-precedes-colors? ]
201     [ unsupported-component-order ] if ;
202
203 : not-alpha-first ( component-order component-type -- )
204     over alpha-channel-precedes-colors?
205     [ unsupported-component-order ]
206     [ 2drop ] if ;
207
208 M: ubyte-components          (component-type>type)
209     drop alpha-channel-precedes-colors?
210     [ GL_UNSIGNED_INT_8_8_8_8_REV ]
211     [ GL_UNSIGNED_BYTE ] if ;
212
213 M: ushort-components         (component-type>type) not-alpha-first GL_UNSIGNED_SHORT ;
214 M: uint-components           (component-type>type) not-alpha-first GL_UNSIGNED_INT   ;
215 M: half-components           (component-type>type) not-alpha-first GL_HALF_FLOAT ;
216 M: float-components          (component-type>type) not-alpha-first GL_FLOAT          ;
217 M: byte-integer-components   (component-type>type) not-alpha-first GL_BYTE           ;
218 M: ubyte-integer-components  (component-type>type) not-alpha-first GL_UNSIGNED_BYTE  ;
219 M: short-integer-components  (component-type>type) not-alpha-first GL_SHORT          ;
220 M: ushort-integer-components (component-type>type) not-alpha-first GL_UNSIGNED_SHORT ;
221 M: int-integer-components    (component-type>type) not-alpha-first GL_INT            ;
222 M: uint-integer-components   (component-type>type) not-alpha-first GL_UNSIGNED_INT   ;
223
224 M: u-5-5-5-1-components      (component-type>type)
225     four-channel-alpha-first?
226     [ GL_UNSIGNED_SHORT_1_5_5_5_REV ]
227     [ GL_UNSIGNED_SHORT_5_5_5_1     ] if ;
228
229 M: u-5-6-5-components        (component-type>type) 2drop GL_UNSIGNED_SHORT_5_6_5 ;
230
231 M: u-10-10-10-2-components   (component-type>type)
232     four-channel-alpha-first?
233     [ GL_UNSIGNED_INT_2_10_10_10_REV ]
234     [ GL_UNSIGNED_INT_10_10_10_2     ] if ;
235
236 M: u-24-components           (component-type>type)
237     over DEPTH =
238     [ 2drop GL_UNSIGNED_INT ] [ unsupported-component-order ] if ;
239
240 M: u-24-8-components         (component-type>type)
241     over DEPTH-STENCIL =
242     [ 2drop GL_UNSIGNED_INT_24_8 ] [ unsupported-component-order ] if ;
243
244 M: u-9-9-9-e5-components     (component-type>type)
245     over BGR =
246     [ 2drop GL_UNSIGNED_INT_5_9_9_9_REV ] [ unsupported-component-order ] if ;
247
248 M: float-11-11-10-components (component-type>type)
249     over BGR =
250     [ 2drop GL_UNSIGNED_INT_10F_11F_11F_REV ] [ unsupported-component-order ] if ;
251
252 : image-data-format ( component-order component-type -- gl-format gl-type )
253     [ (component-order>format) ] [ (component-type>type) ] 2bi ;
254
255 SLOT: display-list
256
257 : draw-texture ( texture -- ) display-list>> [ glCallList ] when* ;
258
259 GENERIC: draw-scaled-texture ( dim texture -- )
260
261 DEFER: make-texture
262
263 : (image-format) ( component-order component-type -- internal-format format type )
264     [ image-internal-format ] [ image-data-format ] 2bi ;
265
266 : image-format ( image -- internal-format format type )
267     [ component-order>> ] [ component-type>> ] bi (image-format) ;
268
269 <PRIVATE
270
271 TUPLE: single-texture < disposable image dim loc texture-coords texture display-list ;
272
273 : adjust-texture-dim ( dim -- dim' )
274     non-power-of-2-textures? get [
275         [ dup 1 = [ next-power-of-2 ] unless ] map
276     ] unless ;
277
278 :: tex-image ( image bitmap -- )
279     image image-format :> type :> format :> internal-format
280     GL_TEXTURE_2D 0 internal-format
281     image dim>> adjust-texture-dim first2 0
282     format type bitmap glTexImage2D ;
283
284 : tex-sub-image ( image -- )
285     [ GL_TEXTURE_2D 0 0 0 ] dip
286     [ dim>> first2 ]
287     [ image-format [ drop ] 2dip ]
288     [ bitmap>> ] tri
289     glTexSubImage2D ;
290
291 : init-texture ( -- )
292     GL_TEXTURE_2D GL_TEXTURE_MAG_FILTER GL_NEAREST glTexParameteri
293     GL_TEXTURE_2D GL_TEXTURE_MIN_FILTER GL_NEAREST glTexParameteri
294     GL_TEXTURE_2D GL_TEXTURE_WRAP_S GL_REPEAT glTexParameteri
295     GL_TEXTURE_2D GL_TEXTURE_WRAP_T GL_REPEAT glTexParameteri ;
296
297 : with-texturing ( quot -- )
298     GL_TEXTURE_2D [
299         GL_TEXTURE_BIT [
300             GL_TEXTURE_COORD_ARRAY [
301                 COLOR: white gl-color
302                 call
303             ] do-enabled-client-state
304         ] do-attribs
305     ] do-enabled ; inline
306
307 : (draw-textured-rect) ( dim texture -- )
308     [ loc>> ]
309     [ [ GL_TEXTURE_2D ] dip texture>> glBindTexture ]
310     [ init-texture texture-coords>> gl-texture-coord-pointer ] tri
311     swap gl-fill-rect ;
312
313 : draw-textured-rect ( dim texture -- )
314     [
315         [ image>> has-alpha? [ GL_BLEND glDisable ] unless ]
316         [ (draw-textured-rect) GL_TEXTURE_2D 0 glBindTexture ]
317         [ image>> has-alpha? [ GL_BLEND glEnable ] unless ]
318         tri
319     ] with-texturing ;
320
321 : texture-coords ( texture -- coords )
322     [ [ dim>> ] [ image>> dim>> adjust-texture-dim ] bi v/ ]
323     [
324         image>> upside-down?>>
325         { { 0 1 } { 1 1 } { 1 0 } { 0 0 } }
326         { { 0 0 } { 1 0 } { 1 1 } { 0 1 } } ?
327     ] bi
328     [ v* ] with map float-array{ } join ;
329
330 : make-texture-display-list ( texture -- dlist )
331     GL_COMPILE [ [ dim>> ] keep draw-textured-rect ] make-dlist ;
332
333 : <single-texture> ( image loc -- texture )
334     single-texture new-disposable swap >>loc swap [ >>image ] [ dim>> >>dim ] bi
335     dup image>> dim>> product 0 = [
336         dup texture-coords >>texture-coords
337         dup image>> make-texture >>texture
338         dup make-texture-display-list >>display-list
339     ] unless ;
340
341 M: single-texture dispose*
342     [ texture>> [ delete-texture ] when* ]
343     [ display-list>> [ delete-dlist ] when* ] bi ;
344
345 M: single-texture draw-scaled-texture
346     2dup dim>> = [ nip draw-texture ] [
347         dup texture>> [ draw-textured-rect ] [ 2drop ] if
348     ] if ;
349
350 TUPLE: multi-texture < disposable grid display-list loc ;
351
352 : image-locs ( image-grid -- loc-grid )
353     [ first [ dim>> first ] map ] [ [ first dim>> second ] map ] bi
354     [ 0 [ + ] accumulate nip ] bi@
355     cross-zip flip ;
356
357 : <texture-grid> ( image-grid loc -- grid )
358     [ dup image-locs ] dip
359     '[ [ _ v+ <single-texture> |dispose ] 2map ] 2map ;
360
361 : grid-has-alpha? ( grid -- ? )
362     first first image>> has-alpha? ;
363
364 : make-textured-grid-display-list ( grid -- dlist )
365     GL_COMPILE [
366         [
367             [ grid-has-alpha? [ GL_BLEND glDisable ] unless ]
368             [ [ [ [ dim>> ] keep (draw-textured-rect) ] each ] each ]
369             [ grid-has-alpha? [ GL_BLEND glEnable ] unless ] tri
370             GL_TEXTURE_2D 0 glBindTexture
371         ] with-texturing
372     ] make-dlist ;
373
374 : <multi-texture> ( image-grid loc -- multi-texture )
375     [
376         [ multi-texture new-disposable ] 2dip
377         [ nip >>loc ] [ <texture-grid> >>grid ] 2bi
378         dup grid>> make-textured-grid-display-list >>display-list
379     ] with-destructors ;
380
381 M: multi-texture draw-scaled-texture nip draw-texture ;
382
383 M: multi-texture dispose* grid>> [ [ dispose ] each ] each ;
384
385 CONSTANT: max-texture-size { 512 512 }
386
387 PRIVATE>
388
389 : make-texture ( image -- id )
390     #! We use glTexSubImage2D to work around the power of 2 texture size
391     #! limitation
392     gen-texture [
393         GL_TEXTURE_BIT [
394             GL_TEXTURE_2D swap glBindTexture
395             non-power-of-2-textures? get
396             [ dup bitmap>> tex-image ]
397             [ [ f tex-image ] [ tex-sub-image ] bi ] if
398         ] do-attribs
399     ] keep ;
400
401 : <texture> ( image loc -- texture )
402     over dim>> max-texture-size [ <= ] 2all?
403     [ <single-texture> ]
404     [ [ max-texture-size tesselate ] dip <multi-texture> ] if ;