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