source: trunk/src/gpu/framebuffer.cpp @ 1761

Last change on this file since 1761 was 1761, checked in by sam, 8 years ago

gpu: on the PS3, use the OES version of the framebuffer GL calls.

  • Property svn:keywords set to Id
File size: 4.3 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2012 Sam Hocevar <sam@hocevar.net>
5//   This program is free software; you can redistribute it and/or
6//   modify it under the terms of the Do What The Fuck You Want To
7//   Public License, Version 2, as published by Sam Hocevar. See
8//   http://sam.zoy.org/projects/COPYING.WTFPL for more details.
9//
10
11#if defined HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
15#include "core.h"
16#include "lolgl.h"
17
18#if defined _WIN32 && defined USE_D3D9
19#   define FAR
20#   define NEAR
21#   include <d3d9.h>
22#endif
23
24using namespace std;
25
26#if defined USE_D3D9
27extern IDirect3DDevice9 *g_d3ddevice;
28#elif defined _XBOX
29extern D3DDevice *g_d3ddevice;
30#endif
31
32namespace lol
33{
34
35//
36// The FrameBufferData class
37// -------------------------
38//
39
40class FrameBufferData
41{
42    friend class FrameBuffer;
43
44    ivec2 m_size;
45
46#if defined USE_D3D9
47#elif defined _XBOX
48#else
49    GLuint m_fbo, m_texture, m_depth;
50#endif
51};
52
53//
54// The FrameBuffer class
55// ----------------------
56//
57
58FrameBuffer::FrameBuffer(ivec2 size)
59  : m_data(new FrameBufferData)
60{
61    m_data->m_size = size;
62#if defined USE_D3D9 || defined _XBOX
63    /* FIXME: not implemented on Direct3D */
64#else
65#   if GL_VERSION_1_1
66    GLenum internal_format = GL_RGBA8;
67    GLenum format = GL_BGRA;
68    GLenum depth = GL_DEPTH_COMPONENT;
69#   else
70    GLenum internal_format = GL_RGBA;
71    GLenum format = GL_RGBA;
72#   endif
73    GLenum wrapmode = GL_REPEAT;
74    GLenum filtering = GL_NEAREST;
75
76#   if GL_VERSION_1_1 || GL_ES_VERSION_2_0
77    glGenFramebuffers(1, &m_data->m_fbo);
78    glBindFramebuffer(GL_FRAMEBUFFER, m_data->m_fbo);
79#   else
80    glGenFramebuffersOES(1, &m_data->m_fbo);
81    glBindFramebufferOES(GL_FRAMEBUFFER_OES, m_data->m_fbo);
82#   endif
83
84    glGenTextures(1, &m_data->m_texture);
85    glActiveTexture(GL_TEXTURE0);
86    glBindTexture(GL_TEXTURE_2D, m_data->m_texture);
87    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, (GLenum)wrapmode);
88    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, (GLenum)wrapmode);
89    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, (GLenum)filtering);
90    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, (GLenum)filtering);
91    glTexImage2D(GL_TEXTURE_2D, 0, internal_format, size.x, size.y, 0,
92                 format, GL_UNSIGNED_BYTE, NULL);
93
94#   if GL_VERSION_1_1 || GL_ES_VERSION_2_0
95    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
96                           GL_TEXTURE_2D, m_data->m_texture, 0);
97#   else
98    glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_EXT,
99                              GL_TEXTURE_2D, m_data->m_texture, 0);
100#   endif
101
102    m_data->m_depth = GL_INVALID_ENUM;
103#   if GL_VERSION_1_1
104    /* FIXME: not implemented on GL ES, see
105     * http://stackoverflow.com/q/4041682/111461 */
106    if (depth != GL_INVALID_ENUM)
107    {
108        glGenRenderbuffers(1, &m_data->m_depth);
109        glBindRenderbuffer(GL_RENDERBUFFER, m_data->m_depth);
110        glRenderbufferStorage(GL_RENDERBUFFER, depth, size.x, size.y);
111        glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
112                                  GL_RENDERBUFFER, m_data->m_depth);
113    }
114#   endif
115
116#   if GL_VERSION_1_1 || GL_ES_VERSION_2_0
117    glCheckFramebufferStatus(GL_FRAMEBUFFER);
118#   endif
119
120    Unbind();
121#endif
122}
123
124FrameBuffer::~FrameBuffer()
125{
126#if defined USE_D3D9 || defined _XBOX
127#else
128#   if GL_VERSION_1_1 || GL_ES_VERSION_2_0
129    glDeleteFramebuffers(1, &m_data->m_fbo);
130#   else
131    glDeleteFramebuffersOES(1, &m_data->m_fbo);
132#   endif
133    glDeleteTextures(1, &m_data->m_texture);
134#   if GL_VERSION_1_1
135    if (m_data->m_depth != GL_INVALID_ENUM)
136        glDeleteRenderbuffers(1, &m_data->m_depth);
137#   endif
138#endif
139    delete m_data;
140}
141
142int FrameBuffer::GetTexture() const
143{
144#if defined USE_D3D9 || defined _XBOX
145    return 0;
146#else
147    return m_data->m_texture;
148#endif
149}
150
151void FrameBuffer::Bind()
152{
153#if defined USE_D3D9 || defined _XBOX
154#else
155#   if GL_VERSION_1_1 || GL_ES_VERSION_2_0
156    glBindFramebuffer(GL_FRAMEBUFFER, m_data->m_fbo);
157#   else
158    glBindFramebufferOES(GL_FRAMEBUFFER_OES, m_data->m_fbo);
159#   endif
160#endif
161}
162
163void FrameBuffer::Unbind()
164{
165#if defined USE_D3D9 || defined _XBOX
166#else
167#   if GL_VERSION_1_1 || GL_ES_VERSION_2_0
168    glBindFramebuffer(GL_FRAMEBUFFER, NULL);
169#   else
170    glBindFramebufferOES(GL_FRAMEBUFFER_OES, NULL);
171#   endif
172#endif
173}
174
175} /* namespace lol */
176
Note: See TracBrowser for help on using the repository browser.