1/*
2 * Copyright (c) 2009-2012 jMonkeyEngine
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 *   notice, this list of conditions and the following disclaimer.
11 *
12 * * Redistributions in binary form must reproduce the above copyright
13 *   notice, this list of conditions and the following disclaimer in the
14 *   documentation and/or other materials provided with the distribution.
15 *
16 * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
17 *   may be used to endorse or promote products derived from this software
18 *   without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33package com.jme3.renderer;
34
35import com.jme3.shader.Shader;
36import com.jme3.texture.FrameBuffer;
37import com.jme3.texture.FrameBuffer.RenderBuffer;
38import com.jme3.texture.Image;
39import com.jme3.texture.Image.Format;
40import com.jme3.texture.Texture;
41import java.util.Collection;
42
43/**
44 * <code>Caps</code> is an enum specifying a capability that the {@link Renderer}
45 * supports.
46 *
47 * @author Kirill Vainer
48 */
49public enum Caps {
50
51    /**
52     * Supports {@link FrameBuffer FrameBuffers}.
53     * <p>
54     * OpenGL: Renderer exposes the GL_EXT_framebuffer_object extension.<br>
55     * OpenGL ES: Renderer supports OpenGL ES 2.0.
56     */
57    FrameBuffer,
58
59    /**
60     * Supports framebuffer Multiple Render Targets (MRT)
61     * <p>
62     * OpenGL: Renderer exposes the GL_ARB_draw_buffers extension
63     */
64    FrameBufferMRT,
65
66    /**
67     * Supports framebuffer multi-sampling
68     * <p>
69     * OpenGL: Renderer exposes the GL EXT framebuffer multisample extension<br>
70     * OpenGL ES: Renderer exposes GL_APPLE_framebuffer_multisample or
71     * GL_ANGLE_framebuffer_multisample.
72     */
73    FrameBufferMultisample,
74
75    /**
76     * Supports texture multi-sampling
77     * <p>
78     * OpenGL: Renderer exposes the GL_ARB_texture_multisample extension<br>
79     * OpenGL ES: Renderer exposes the GL_IMG_multisampled_render_to_texture
80     * extension.
81     */
82    TextureMultisample,
83
84    /**
85     * Supports OpenGL 2.0 or OpenGL ES 2.0.
86     */
87    OpenGL20,
88
89    /**
90     * Supports OpenGL 2.1
91     */
92    OpenGL21,
93
94    /**
95     * Supports OpenGL 3.0
96     */
97    OpenGL30,
98
99    /**
100     * Supports OpenGL 3.1
101     */
102    OpenGL31,
103
104    /**
105     * Supports OpenGL 3.2
106     */
107    OpenGL32,
108
109    /**
110     * Supports OpenGL ARB program.
111     * <p>
112     * OpenGL: Renderer exposes ARB_vertex_program and ARB_fragment_program
113     * extensions.
114     */
115    ARBprogram,
116
117    /**
118     * Supports GLSL 1.0
119     */
120    GLSL100,
121
122    /**
123     * Supports GLSL 1.1
124     */
125    GLSL110,
126
127    /**
128     * Supports GLSL 1.2
129     */
130    GLSL120,
131
132    /**
133     * Supports GLSL 1.3
134     */
135    GLSL130,
136
137    /**
138     * Supports GLSL 1.4
139     */
140    GLSL140,
141
142    /**
143     * Supports GLSL 1.5
144     */
145    GLSL150,
146
147    /**
148     * Supports GLSL 3.3
149     */
150    GLSL330,
151
152    /**
153     * Supports reading from textures inside the vertex shader.
154     */
155    VertexTextureFetch,
156
157    /**
158     * Supports geometry shader.
159     */
160    GeometryShader,
161
162    /**
163     * Supports texture arrays
164     */
165    TextureArray,
166
167    /**
168     * Supports texture buffers
169     */
170    TextureBuffer,
171
172    /**
173     * Supports floating point textures (Format.RGB16F)
174     */
175    FloatTexture,
176
177    /**
178     * Supports floating point FBO color buffers (Format.RGB16F)
179     */
180    FloatColorBuffer,
181
182    /**
183     * Supports floating point depth buffer
184     */
185    FloatDepthBuffer,
186
187    /**
188     * Supports Format.RGB111110F for textures
189     */
190    PackedFloatTexture,
191
192    /**
193     * Supports Format.RGB9E5 for textures
194     */
195    SharedExponentTexture,
196
197    /**
198     * Supports Format.RGB111110F for FBO color buffers
199     */
200    PackedFloatColorBuffer,
201
202    /**
203     * Supports Format.RGB9E5 for FBO color buffers
204     */
205    SharedExponentColorBuffer,
206
207    /**
208     * Supports Format.LATC for textures, this includes
209     * support for ATI's 3Dc texture compression.
210     */
211    TextureCompressionLATC,
212
213    /**
214     * Supports Non-Power-Of-Two (NPOT) textures and framebuffers
215     */
216    NonPowerOfTwoTextures,
217
218    /// Vertex Buffer features
219    MeshInstancing,
220
221    /**
222     * Supports VAO, or vertex buffer arrays
223     */
224    VertexBufferArray,
225
226    /**
227     * Supports multisampling on the screen
228     */
229    Multisample;
230
231    /**
232     * Returns true if given the renderer capabilities, the texture
233     * can be supported by the renderer.
234     * <p>
235     * This only checks the format of the texture, non-power-of-2
236     * textures are scaled automatically inside the renderer
237     * if are not supported natively.
238     *
239     * @param caps The collection of renderer capabilities {@link Renderer#getCaps() }.
240     * @param tex The texture to check
241     * @return True if it is supported, false otherwise.
242     */
243    public static boolean supports(Collection<Caps> caps, Texture tex){
244        if (tex.getType() == Texture.Type.TwoDimensionalArray
245         && !caps.contains(Caps.TextureArray))
246            return false;
247
248        Image img = tex.getImage();
249        if (img == null)
250            return true;
251
252        Format fmt = img.getFormat();
253        switch (fmt){
254            case Depth32F:
255                return caps.contains(Caps.FloatDepthBuffer);
256            case LATC:
257                return caps.contains(Caps.TextureCompressionLATC);
258            case RGB16F_to_RGB111110F:
259            case RGB111110F:
260                return caps.contains(Caps.PackedFloatTexture);
261            case RGB16F_to_RGB9E5:
262            case RGB9E5:
263                return caps.contains(Caps.SharedExponentTexture);
264            default:
265                if (fmt.isFloatingPont())
266                    return caps.contains(Caps.FloatTexture);
267
268                return true;
269        }
270    }
271
272    /**
273     * Returns true if given the renderer capabilities, the framebuffer
274     * can be supported by the renderer.
275     *
276     * @param caps The collection of renderer capabilities {@link Renderer#getCaps() }.
277     * @param fb The framebuffer to check
278     * @return True if it is supported, false otherwise.
279     */
280    public static boolean supports(Collection<Caps> caps, FrameBuffer fb){
281        if (!caps.contains(Caps.FrameBuffer))
282            return false;
283
284        if (fb.getSamples() > 1
285         && !caps.contains(Caps.FrameBufferMultisample))
286            return false;
287
288        RenderBuffer colorBuf = fb.getColorBuffer();
289        RenderBuffer depthBuf = fb.getDepthBuffer();
290
291        if (depthBuf != null){
292            Format depthFmt = depthBuf.getFormat();
293            if (!depthFmt.isDepthFormat()){
294                return false;
295            }else{
296                if (depthFmt == Format.Depth32F
297                 && !caps.contains(Caps.FloatDepthBuffer))
298                    return false;
299            }
300        }
301        if (colorBuf != null){
302            Format colorFmt = colorBuf.getFormat();
303            if (colorFmt.isDepthFormat())
304                return false;
305
306            if (colorFmt.isCompressed())
307                return false;
308
309            switch (colorFmt){
310                case RGB111110F:
311                    return caps.contains(Caps.PackedFloatColorBuffer);
312                case RGB16F_to_RGB111110F:
313                case RGB16F_to_RGB9E5:
314                case RGB9E5:
315                    return false;
316                default:
317                    if (colorFmt.isFloatingPont())
318                        return caps.contains(Caps.FloatColorBuffer);
319
320                    return true;
321            }
322        }
323        return true;
324    }
325
326    /**
327     * Returns true if given the renderer capabilities, the shader
328     * can be supported by the renderer.
329     *
330     * @param caps The collection of renderer capabilities {@link Renderer#getCaps() }.
331     * @param shader The shader to check
332     * @return True if it is supported, false otherwise.
333     */
334    public static boolean supports(Collection<Caps> caps, Shader shader){
335        String lang = shader.getLanguage();
336        if (lang.startsWith("GLSL")){
337            int ver = Integer.parseInt(lang.substring(4));
338            switch (ver){
339                case 100:
340                    return caps.contains(Caps.GLSL100);
341                case 110:
342                    return caps.contains(Caps.GLSL110);
343                case 120:
344                    return caps.contains(Caps.GLSL120);
345                case 130:
346                    return caps.contains(Caps.GLSL130);
347                case 140:
348                    return caps.contains(Caps.GLSL140);
349                case 150:
350                    return caps.contains(Caps.GLSL150);
351                case 330:
352                    return caps.contains(Caps.GLSL330);
353                default:
354                    return false;
355            }
356        }
357        return false;
358    }
359
360}
361