1 //
2 // Copyright (c) 2008-2017 the Urho3D project.
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to deal
6 // in the Software without restriction, including without limitation the rights
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 // copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 // THE SOFTWARE.
21 //
22 
23 #pragma once
24 
25 #include "../../Container/HashMap.h"
26 #include "../../Core/Timer.h"
27 #include "../../Graphics/ConstantBuffer.h"
28 #include "../../Graphics/ShaderProgram.h"
29 #include "../../Graphics/Texture2D.h"
30 #include "../../Math/Color.h"
31 
32 #if defined(IOS) || defined(TVOS)
33 #include <OpenGLES/ES2/gl.h>
34 #include <OpenGLES/ES2/glext.h>
35 #elif defined(__ANDROID__) || defined (__arm__) || defined(__aarch64__) || defined (__EMSCRIPTEN__)
36 #include <GLES2/gl2.h>
37 #include <GLES2/gl2ext.h>
38 #else
39 #include <GLEW/glew.h>
40 #endif
41 
42 #ifndef GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
43 #define GL_COMPRESSED_RGBA_S3TC_DXT1_EXT 0x83f1
44 #endif
45 #ifndef GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
46 #define GL_COMPRESSED_RGBA_S3TC_DXT3_EXT 0x83f2
47 #endif
48 #ifndef GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
49 #define GL_COMPRESSED_RGBA_S3TC_DXT5_EXT 0x83f3
50 #endif
51 #ifndef GL_ETC1_RGB8_OES
52 #define GL_ETC1_RGB8_OES 0x8d64
53 #endif
54 #ifndef COMPRESSED_RGB_PVRTC_4BPPV1_IMG
55 #define COMPRESSED_RGB_PVRTC_4BPPV1_IMG 0x8c00
56 #endif
57 #ifndef COMPRESSED_RGB_PVRTC_2BPPV1_IMG
58 #define COMPRESSED_RGB_PVRTC_2BPPV1_IMG 0x8c01
59 #endif
60 #ifndef COMPRESSED_RGBA_PVRTC_4BPPV1_IMG
61 #define COMPRESSED_RGBA_PVRTC_4BPPV1_IMG 0x8c02
62 #endif
63 #ifndef COMPRESSED_RGBA_PVRTC_2BPPV1_IMG
64 #define COMPRESSED_RGBA_PVRTC_2BPPV1_IMG 0x8c03
65 #endif
66 
67 typedef void *SDL_GLContext;
68 
69 namespace Urho3D
70 {
71 
72 class Context;
73 
74 typedef HashMap<unsigned, SharedPtr<ConstantBuffer> > ConstantBufferMap;
75 typedef HashMap<Pair<ShaderVariation*, ShaderVariation*>, SharedPtr<ShaderProgram> > ShaderProgramMap;
76 
77 /// Cached state of a frame buffer object
78 struct FrameBufferObject
79 {
FrameBufferObjectFrameBufferObject80     FrameBufferObject() :
81         fbo_(0),
82         depthAttachment_(0),
83         readBuffers_(M_MAX_UNSIGNED),
84         drawBuffers_(M_MAX_UNSIGNED)
85     {
86         for (unsigned i = 0; i < MAX_RENDERTARGETS; ++i)
87             colorAttachments_[i] = 0;
88     }
89 
90     /// Frame buffer handle.
91     unsigned fbo_;
92     /// Bound color attachment textures.
93     RenderSurface* colorAttachments_[MAX_RENDERTARGETS];
94     /// Bound depth/stencil attachment.
95     RenderSurface* depthAttachment_;
96     /// Read buffer bits.
97     unsigned readBuffers_;
98     /// Draw buffer bits.
99     unsigned drawBuffers_;
100 };
101 
102 /// %Graphics subsystem implementation. Holds API-specific objects.
103 class URHO3D_API GraphicsImpl
104 {
105     friend class Graphics;
106 
107 public:
108     /// Construct.
109     GraphicsImpl();
110 
111     /// Return the GL Context.
GetGLContext()112     const SDL_GLContext& GetGLContext() { return context_; }
113 
114 private:
115     /// SDL OpenGL context.
116     SDL_GLContext context_;
117     /// iOS/tvOS system framebuffer handle.
118     unsigned systemFBO_;
119     /// Active texture unit.
120     unsigned activeTexture_;
121     /// Enabled vertex attributes bitmask.
122     unsigned enabledVertexAttributes_;
123     /// Vertex attributes bitmask used by the current shader program.
124     unsigned usedVertexAttributes_;
125     /// Vertex attribute instancing bitmask for keeping track of divisors.
126     unsigned instancingVertexAttributes_;
127     /// Current mapping of vertex attribute locations by semantic. The map is owned by the shader program, so care must be taken to switch a null shader program when it's destroyed.
128     const HashMap<Pair<unsigned char, unsigned char>, unsigned>* vertexAttributes_;
129     /// Currently bound frame buffer object.
130     unsigned boundFBO_;
131     /// Currently bound vertex buffer object.
132     unsigned boundVBO_;
133     /// Currently bound uniform buffer object.
134     unsigned boundUBO_;
135     /// Read frame buffer for multisampled texture resolves.
136     unsigned resolveSrcFBO_;
137     /// Write frame buffer for multisampled texture resolves.
138     unsigned resolveDestFBO_;
139     /// Current pixel format.
140     int pixelFormat_;
141     /// Map for FBO's per resolution and format.
142     HashMap<unsigned long long, FrameBufferObject> frameBuffers_;
143     /// OpenGL texture types in use.
144     unsigned textureTypes_[MAX_TEXTURE_UNITS];
145     /// Constant buffer search map.
146     ConstantBufferMap allConstantBuffers_;
147     /// Currently bound constant buffers.
148     ConstantBuffer* constantBuffers_[MAX_SHADER_PARAMETER_GROUPS * 2];
149     /// Dirty constant buffers.
150     PODVector<ConstantBuffer*> dirtyConstantBuffers_;
151     /// Last used instance data offset.
152     unsigned lastInstanceOffset_;
153     /// Map for additional depth textures, to emulate Direct3D9 ability to mix render texture and backbuffer rendering.
154     HashMap<int, SharedPtr<Texture2D> > depthTextures_;
155     /// Shader program in use.
156     ShaderProgram* shaderProgram_;
157     /// Linked shader programs.
158     ShaderProgramMap shaderPrograms_;
159     /// Need FBO commit flag.
160     bool fboDirty_;
161     /// Need vertex attribute pointer update flag.
162     bool vertexBuffersDirty_;
163     /// sRGB write mode flag.
164     bool sRGBWrite_;
165 };
166 
167 }
168