Welcome to mirror list, hosted at ThFree Co, Russian Federation.

pbuffer.cpp « rendering « intern « freestyle « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ec73fc52ae46c70df49ada7eec82eb1ffac23293 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#include <pbuffer.h>

#include <memory.h>

#define MAX_PFORMATS 256
#define MAX_ATTRIBS  32
#define PBUFFER_AS_TEXTURE 2048

//************************************************************
// Implementation of PBuffer
//************************************************************
/*!
 * Creates a PBuffer of size \p w by \p h, eith mode \p mode.
 */
PBuffer::PBuffer(const unsigned int w,
		 const unsigned int h,
		 const int format) 
  : format_(format),
    sharedContext_(false),
    sharedLists_(false),
    display_(NULL),
    glxPbuffer_(0),
    glxContext_(0), 
    width_(w),
    height_(h)
{
}    
/*!
 * Initialize the pbuffer. If \p shareContext is true, then the currently
 * active context with be shared by the pbuffer, meaning for example that
 * textures objects or display lists of the currently active GL context will
 * be available to the pbuffer.  If you want it to be the case just for
 * display lists, set \p sharedContext to false and \p shareLists to true.
 * 
 * This function is outside of the constructor for several reasons. First it
 * might raise exceptions so it cannot be done in the constructor. Secondly,
 * it is using the current context for sharing so you might want to create
 * the pbuffer at a moment where this context is not yet active, and then
 * initialise it once the context has been created and activated.
 *
 * Return false if creation failed.
 */
bool
PBuffer::create(const bool shareContext, const bool shareLists)
{
  // Set display and screen
  Display *pDisplay     = glXGetCurrentDisplay();
  if (pDisplay == NULL)
  {
    pDisplay = XOpenDisplay(NULL);
  }
  int iScreen           = DefaultScreen(pDisplay);
  GLXContext glxContext = glXGetCurrentContext();
  
  GLXFBConfig *glxConfig;
  int iConfigCount;   
    
  sharedContext_ = shareContext;
  sharedLists_ = shareLists;

  if (sharedContext_)
  {
    glxConfig = glXGetFBConfigs(pDisplay, iScreen, &iConfigCount);
    if (!glxConfig)
    {
//       "pbuffer creation error:  glXGetFBConfigs() failed"
      return false;
    }
  }  
  else
  {
    int iAttributes[2*MAX_ATTRIBS];
    int curAttrib = 0;
        
    memset(iAttributes, 0, 2*MAX_ATTRIBS*sizeof(int));        
            
    iAttributes[2*curAttrib  ] = GLX_DRAWABLE_TYPE;
    iAttributes[2*curAttrib+1] = GLX_PBUFFER_BIT;
    curAttrib++;
        
    if (format_ & ColorIndex)
    {
      iAttributes[2*curAttrib  ] = GLX_RENDER_TYPE;
      iAttributes[2*curAttrib+1] = GLX_COLOR_INDEX_BIT;
      curAttrib++;
    }
    else
    {
      iAttributes[2*curAttrib  ] = GLX_RENDER_TYPE;
      iAttributes[2*curAttrib+1] = GLX_RGBA_BIT;
      iAttributes[2*curAttrib  ] = GLX_ALPHA_SIZE;
      iAttributes[2*curAttrib+1] = 8;
      curAttrib++;
    }
        
    if (format_ & DoubleBuffer)
    {
      iAttributes[2*curAttrib  ] = GLX_DOUBLEBUFFER;
      iAttributes[2*curAttrib+1] = true;
      curAttrib++;
    }
    else
    {
      iAttributes[2*curAttrib  ] = GLX_DOUBLEBUFFER;
      iAttributes[2*curAttrib+1] = false;
      curAttrib++;
    }  
    if (format_ & DepthBuffer)
    {
      iAttributes[2*curAttrib  ] = GLX_DEPTH_SIZE;
      iAttributes[2*curAttrib+1] = 1;
      curAttrib++;
    }
    else
    {
      iAttributes[2*curAttrib  ] = GLX_DEPTH_SIZE;
      iAttributes[2*curAttrib+1] = 0;
      curAttrib++;
    }    
        
    if (format_ & StencilBuffer)
    {
      iAttributes[2*curAttrib  ] = GLX_STENCIL_SIZE;
      iAttributes[2*curAttrib+1] = 1;
      curAttrib++;
    }
    else
    {
      iAttributes[2*curAttrib  ] = GLX_STENCIL_SIZE;
      iAttributes[2*curAttrib+1] = 0;
      curAttrib++;
    }

    iAttributes[2*curAttrib  ] = None;
    
    glxConfig = glXChooseFBConfigSGIX(pDisplay, iScreen, iAttributes, &iConfigCount);
    if (!glxConfig)
    {
      // "pbuffer creation error:  glXChooseFBConfig() failed"
      return false;
    }
  }
   
  int attributes[5];
  int iCurAttrib = 0;
    
  memset(attributes, 0, 5*sizeof(int));

  attributes[2*iCurAttrib  ] = GLX_LARGEST_PBUFFER;
  attributes[2*iCurAttrib+1] = true;
  iCurAttrib++;

  attributes[2*iCurAttrib  ] = GLX_PRESERVED_CONTENTS;
  attributes[2*iCurAttrib+1] = true;
  iCurAttrib++;
    
  attributes[2*iCurAttrib  ] = None;

  glxPbuffer_ = glXCreateGLXPbufferSGIX(pDisplay, glxConfig[0],
					width_, height_, attributes);
    
  if (!glxPbuffer_)
  {
    // "pbuffer creation error:  glXCreatePbuffer() failed"
    return false;
  }
    
  if (sharedContext_)
  {
    glxContext_ = glxContext;
  }
  else
  {
    if (format_ & ColorIndex)
    {
      if (sharedLists_)
	glxContext_ = glXCreateContextWithConfigSGIX(pDisplay,
						     glxConfig[0],
						     GLX_COLOR_INDEX_TYPE,
						     glxContext, true);
      else
	glxContext_ = glXCreateContextWithConfigSGIX(pDisplay,
						     glxConfig[0],
						     GLX_COLOR_INDEX_TYPE,
						     NULL, true);
    }
    else
    {
      if (sharedLists_)
	glxContext_ = glXCreateContextWithConfigSGIX(pDisplay,
						     glxConfig[0],
						     GLX_RGBA_TYPE,
						     glxContext, true);
      else
	glxContext_ = glXCreateContextWithConfigSGIX(pDisplay,
						     glxConfig[0],
						     GLX_RGBA_TYPE,
						     NULL, true);
    }

    if (!glxConfig)
    {
      // "pbuffer creation error:  glXCreateNewContext() failed"
      return false;
    }
  }

  display_ = pDisplay;

  glXQueryGLXPbufferSGIX(display_, glxPbuffer_, GLX_WIDTH, &width_);
  glXQueryGLXPbufferSGIX(display_, glxPbuffer_, GLX_HEIGHT, &height_);

  return true;
}
/*!
 * Destroy the pbuffer
 */
PBuffer::~PBuffer()
{
  if (glxPbuffer_)
  {
    glXDestroyGLXPbufferSGIX(display_, glxPbuffer_);        
  }
}
/*!
 * Activate the pbuffer as the current GL context. All subsequents GL
 * commands will now affect the pbuffer. If you want to push/pop the current
 * OpenGL context use subclass PBufferEx instead.
 *
 * Return false if it failed.
 */
bool
PBuffer::makeCurrent()
{
  return glXMakeCurrent(display_, glxPbuffer_, glxContext_);
}
/*!
 * Return the width of the pbuffer
 */
unsigned int
PBuffer::width() const
{
  return width_;
}
/*!
 * Return the height of the pbuffer
 */
unsigned int
PBuffer::height() const
{
  return height_;
}
//************************************************************
// Implementation of PBufferEx
//************************************************************
PBufferEx::PBufferEx(const unsigned int width,
		     const unsigned int height,
		     const int mode)
  : PBuffer(width, height, mode),
    oldDisplay_(NULL),
    glxOldDrawable_(0),
    glxOldContext_(0)
{
}
/*!
 * Activate the pbuffer as the current GL context. All subsequents GL
 * commands will now affect the pbuffer. Once you are done with you GL
 * commands, you can call endCurrent() to restore the context that was active
 * when you call makeCurrent().
 *
 * Return false if it failed.
 */
bool
PBufferEx::makeCurrent()
{
  oldDisplay_ = glXGetCurrentDisplay();
  glxOldDrawable_ = glXGetCurrentDrawable();
  glxOldContext_ = glXGetCurrentContext();
  
  return PBuffer::makeCurrent();
}
/*!
 * Restore the GL context that was active when makeCurrent() was called.
 *
 * Return false if it failed.
 */
bool
PBufferEx::endCurrent()
{
  return glXMakeCurrent(oldDisplay_, glxOldDrawable_, glxOldContext_);
}