blob: c5a26238089d1f67aca2f9cac1e17d6ae490f7dc [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
2// Copyright (c) 2002-2010 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// Context.cpp: Implements the gl::Context class, managing all GL state and performing
8// rendering operations. It is the GLES2 specific implementation of EGLContext.
9
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +000010#include "Context.h"
daniel@transgaming.com16973022010-03-11 19:22:19 +000011
12#include <algorithm>
13
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000014#include "main.h"
15#include "Display.h"
16#include "Buffer.h"
17#include "Shader.h"
18#include "Program.h"
19#include "Texture.h"
20#include "FrameBuffer.h"
21#include "RenderBuffer.h"
22#include "mathutil.h"
23#include "utilities.h"
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000024#include "geometry/backend.h"
25#include "geometry/VertexDataManager.h"
26#include "geometry/dx9.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000027
daniel@transgaming.com86487c22010-03-11 19:41:43 +000028#undef near
29#undef far
30
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000031namespace gl
32{
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000033Context::Context(const egl::Config *config)
34 : mConfig(config)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000035{
36 setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
37 depthClearValue = 1.0f;
38 stencilClearValue = 0;
39
40 cullFace = false;
41 cullMode = GL_BACK;
42 frontFace = GL_CCW;
43 depthTest = false;
44 depthFunc = GL_LESS;
45 blend = false;
46 sourceBlendRGB = GL_ONE;
47 sourceBlendAlpha = GL_ONE;
48 destBlendRGB = GL_ZERO;
49 destBlendAlpha = GL_ZERO;
50 blendEquationRGB = GL_FUNC_ADD;
51 blendEquationAlpha = GL_FUNC_ADD;
52 blendColor.red = 0;
53 blendColor.green = 0;
54 blendColor.blue = 0;
55 blendColor.alpha = 0;
56 stencilTest = false;
57 stencilFunc = GL_ALWAYS;
58 stencilRef = 0;
59 stencilMask = -1;
60 stencilWritemask = -1;
61 stencilBackFunc = GL_ALWAYS;
62 stencilBackRef = 0;
63 stencilBackMask = - 1;
64 stencilBackWritemask = -1;
65 stencilFail = GL_KEEP;
66 stencilPassDepthFail = GL_KEEP;
67 stencilPassDepthPass = GL_KEEP;
68 stencilBackFail = GL_KEEP;
69 stencilBackPassDepthFail = GL_KEEP;
70 stencilBackPassDepthPass = GL_KEEP;
71 polygonOffsetFill = false;
72 sampleAlphaToCoverage = false;
73 sampleCoverage = false;
74 sampleCoverageValue = 1.0f;
75 sampleCoverageInvert = GL_FALSE;
76 scissorTest = false;
77 dither = true;
78
79 viewportX = 0;
80 viewportY = 0;
81 viewportWidth = config->mDisplayMode.Width;
82 viewportHeight = config->mDisplayMode.Height;
83 zNear = 0.0f;
84 zFar = 1.0f;
85
86 scissorX = 0;
87 scissorY = 0;
88 scissorWidth = config->mDisplayMode.Width;
89 scissorHeight = config->mDisplayMode.Height;
90
91 colorMaskRed = true;
92 colorMaskGreen = true;
93 colorMaskBlue = true;
94 colorMaskAlpha = true;
95 depthMask = true;
96
97 // [OpenGL ES 2.0.24] section 3.7 page 83:
98 // In the initial state, TEXTURE_2D and TEXTURE_CUBE_MAP have twodimensional
99 // and cube map texture state vectors respectively associated with them.
100 // In order that access to these initial textures not be lost, they are treated as texture
101 // objects all of whose names are 0.
102
103 mTexture2DZero = new Texture2D();
104 mTextureCubeMapZero = new TextureCubeMap();
105
106 mColorbufferZero = NULL;
107 mDepthbufferZero = NULL;
108 mStencilbufferZero = NULL;
109
110 activeSampler = 0;
111 arrayBuffer = 0;
112 elementArrayBuffer = 0;
113 bindTextureCubeMap(0);
114 bindTexture2D(0);
115 bindFramebuffer(0);
116 bindRenderbuffer(0);
117
daniel@transgaming.com416485f2010-03-16 06:23:23 +0000118 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000119 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +0000120 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
121 {
122 samplerTexture[type][sampler] = 0;
123 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000124 }
125
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000126 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
127 {
128 mIncompleteTextures[type] = NULL;
129 }
130
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000131 currentProgram = 0;
132
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000133 mBufferBackEnd = NULL;
134 mVertexDataManager = NULL;
135
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000136 mInvalidEnum = false;
137 mInvalidValue = false;
138 mInvalidOperation = false;
139 mOutOfMemory = false;
140 mInvalidFramebufferOperation = false;
141}
142
143Context::~Context()
144{
145 currentProgram = 0;
146
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000147 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
148 {
149 delete mIncompleteTextures[type];
150 }
151
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000152 delete mTexture2DZero;
153 delete mTextureCubeMapZero;
154
155 delete mColorbufferZero;
156 delete mDepthbufferZero;
157 delete mStencilbufferZero;
158
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000159 delete mBufferBackEnd;
160 delete mVertexDataManager;
161
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000162 while (!mBufferMap.empty())
163 {
164 deleteBuffer(mBufferMap.begin()->first);
165 }
166
167 while (!mProgramMap.empty())
168 {
169 deleteProgram(mProgramMap.begin()->first);
170 }
171
172 while (!mShaderMap.empty())
173 {
174 deleteShader(mShaderMap.begin()->first);
175 }
176
177 while (!mFramebufferMap.empty())
178 {
179 deleteFramebuffer(mFramebufferMap.begin()->first);
180 }
181
182 while (!mRenderbufferMap.empty())
183 {
184 deleteRenderbuffer(mRenderbufferMap.begin()->first);
185 }
186
187 while (!mTextureMap.empty())
188 {
189 deleteTexture(mTextureMap.begin()->first);
190 }
191}
192
193void Context::makeCurrent(egl::Display *display, egl::Surface *surface)
194{
195 IDirect3DDevice9 *device = display->getDevice();
196
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000197 if (!mBufferBackEnd)
198 {
199 mBufferBackEnd = new Dx9BackEnd(device);
200 mVertexDataManager = new VertexDataManager(this, mBufferBackEnd);
201 }
202
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000203 // Wrap the existing Direct3D 9 resources into GL objects and assign them to the '0' names
204 IDirect3DSurface9 *defaultRenderTarget = surface->getRenderTarget();
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000205 IDirect3DSurface9 *depthStencil = surface->getDepthStencil();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000206
207 Framebuffer *framebufferZero = new Framebuffer();
208 Colorbuffer *colorbufferZero = new Colorbuffer(defaultRenderTarget);
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000209 Depthbuffer *depthbufferZero = new Depthbuffer(depthStencil);
210 Stencilbuffer *stencilbufferZero = new Stencilbuffer(depthStencil);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000211
212 setFramebufferZero(framebufferZero);
213 setColorbufferZero(colorbufferZero);
214 setDepthbufferZero(depthbufferZero);
215 setStencilbufferZero(stencilbufferZero);
216
217 framebufferZero->setColorbuffer(GL_RENDERBUFFER, 0);
218 framebufferZero->setDepthbuffer(GL_RENDERBUFFER, 0);
219 framebufferZero->setStencilbuffer(GL_RENDERBUFFER, 0);
220
221 defaultRenderTarget->Release();
222
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000223 if (depthStencil)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000224 {
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000225 depthStencil->Release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000226 }
227}
228
229void Context::setClearColor(float red, float green, float blue, float alpha)
230{
231 colorClearValue.red = red;
232 colorClearValue.green = green;
233 colorClearValue.blue = blue;
234 colorClearValue.alpha = alpha;
235}
236
237void Context::setClearDepth(float depth)
238{
239 depthClearValue = depth;
240}
241
242void Context::setClearStencil(int stencil)
243{
244 stencilClearValue = stencil;
245}
246
247// Returns an unused buffer name
248GLuint Context::createBuffer()
249{
250 unsigned int handle = 1;
251
252 while (mBufferMap.find(handle) != mBufferMap.end())
253 {
254 handle++;
255 }
256
257 mBufferMap[handle] = NULL;
258
259 return handle;
260}
261
262// Returns an unused shader/program name
263GLuint Context::createShader(GLenum type)
264{
265 unsigned int handle = 1;
266
267 while (mShaderMap.find(handle) != mShaderMap.end() || mProgramMap.find(handle) != mProgramMap.end()) // Shared name space
268 {
269 handle++;
270 }
271
272 if (type == GL_VERTEX_SHADER)
273 {
274 mShaderMap[handle] = new VertexShader();
275 }
276 else if (type == GL_FRAGMENT_SHADER)
277 {
278 mShaderMap[handle] = new FragmentShader();
279 }
280 else UNREACHABLE();
281
282 return handle;
283}
284
285// Returns an unused program/shader name
286GLuint Context::createProgram()
287{
288 unsigned int handle = 1;
289
290 while (mProgramMap.find(handle) != mProgramMap.end() || mShaderMap.find(handle) != mShaderMap.end()) // Shared name space
291 {
292 handle++;
293 }
294
295 mProgramMap[handle] = new Program();
296
297 return handle;
298}
299
300// Returns an unused texture name
301GLuint Context::createTexture()
302{
303 unsigned int handle = 1;
304
305 while (mTextureMap.find(handle) != mTextureMap.end())
306 {
307 handle++;
308 }
309
310 mTextureMap[handle] = NULL;
311
312 return handle;
313}
314
315// Returns an unused framebuffer name
316GLuint Context::createFramebuffer()
317{
318 unsigned int handle = 1;
319
320 while (mFramebufferMap.find(handle) != mFramebufferMap.end())
321 {
322 handle++;
323 }
324
325 mFramebufferMap[handle] = NULL;
326
327 return handle;
328}
329
330// Returns an unused renderbuffer name
331GLuint Context::createRenderbuffer()
332{
333 unsigned int handle = 1;
334
daniel@transgaming.come2b22122010-03-11 19:22:14 +0000335 while (mRenderbufferMap.find(handle) != mRenderbufferMap.end())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000336 {
337 handle++;
338 }
339
340 mRenderbufferMap[handle] = NULL;
341
342 return handle;
343}
344
345void Context::deleteBuffer(GLuint buffer)
346{
347 BufferMap::iterator bufferObject = mBufferMap.find(buffer);
348
349 if (bufferObject != mBufferMap.end())
350 {
351 detachBuffer(buffer);
352
353 delete bufferObject->second;
354 mBufferMap.erase(bufferObject);
355 }
356}
357
358void Context::deleteShader(GLuint shader)
359{
360 ShaderMap::iterator shaderObject = mShaderMap.find(shader);
361
362 if (shaderObject != mShaderMap.end())
363 {
364 if (!shaderObject->second->isAttached())
365 {
366 delete shaderObject->second;
367 mShaderMap.erase(shaderObject);
368 }
369 else
370 {
371 shaderObject->second->flagForDeletion();
372 }
373 }
374}
375
376void Context::deleteProgram(GLuint program)
377{
378 ProgramMap::iterator programObject = mProgramMap.find(program);
379
380 if (programObject != mProgramMap.end())
381 {
382 if (program != currentProgram)
383 {
384 delete programObject->second;
385 mProgramMap.erase(programObject);
386 }
387 else
388 {
389 programObject->second->flagForDeletion();
390 }
391 }
392}
393
394void Context::deleteTexture(GLuint texture)
395{
396 TextureMap::iterator textureObject = mTextureMap.find(texture);
397
398 if (textureObject != mTextureMap.end())
399 {
400 detachTexture(texture);
401
402 if (texture != 0)
403 {
404 delete textureObject->second;
405 }
406
407 mTextureMap.erase(textureObject);
408 }
409}
410
411void Context::deleteFramebuffer(GLuint framebuffer)
412{
413 FramebufferMap::iterator framebufferObject = mFramebufferMap.find(framebuffer);
414
415 if (framebufferObject != mFramebufferMap.end())
416 {
417 detachFramebuffer(framebuffer);
418
419 delete framebufferObject->second;
420 mFramebufferMap.erase(framebufferObject);
421 }
422}
423
424void Context::deleteRenderbuffer(GLuint renderbuffer)
425{
426 RenderbufferMap::iterator renderbufferObject = mRenderbufferMap.find(renderbuffer);
427
428 if (renderbufferObject != mRenderbufferMap.end())
429 {
430 detachRenderbuffer(renderbuffer);
431
432 delete renderbufferObject->second;
433 mRenderbufferMap.erase(renderbufferObject);
434 }
435}
436
437void Context::bindArrayBuffer(unsigned int buffer)
438{
439 if (buffer != 0 && !getBuffer(buffer))
440 {
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000441 mBufferMap[buffer] = new Buffer(mBufferBackEnd);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000442 }
443
444 arrayBuffer = buffer;
445}
446
447void Context::bindElementArrayBuffer(unsigned int buffer)
448{
449 if (buffer != 0 && !getBuffer(buffer))
450 {
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000451 mBufferMap[buffer] = new Buffer(mBufferBackEnd);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000452 }
453
454 elementArrayBuffer = buffer;
455}
456
457void Context::bindTexture2D(GLuint texture)
458{
459 if (!getTexture(texture) || texture == 0)
460 {
461 if (texture != 0)
462 {
463 mTextureMap[texture] = new Texture2D();
464 }
465 else // Special case: 0 refers to different initial textures based on the target
466 {
467 mTextureMap[0] = mTexture2DZero;
468 }
469 }
470
471 texture2D = texture;
472
daniel@transgaming.com416485f2010-03-16 06:23:23 +0000473 samplerTexture[SAMPLER_2D][activeSampler] = texture;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000474}
475
476void Context::bindTextureCubeMap(GLuint texture)
477{
478 if (!getTexture(texture) || texture == 0)
479 {
480 if (texture != 0)
481 {
482 mTextureMap[texture] = new TextureCubeMap();
483 }
484 else // Special case: 0 refers to different initial textures based on the target
485 {
486 mTextureMap[0] = mTextureCubeMapZero;
487 }
488 }
489
490 textureCubeMap = texture;
491
daniel@transgaming.com416485f2010-03-16 06:23:23 +0000492 samplerTexture[SAMPLER_CUBE][activeSampler] = texture;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000493}
494
495void Context::bindFramebuffer(GLuint framebuffer)
496{
497 if (!getFramebuffer(framebuffer))
498 {
499 mFramebufferMap[framebuffer] = new Framebuffer();
500 }
501
502 this->framebuffer = framebuffer;
503}
504
505void Context::bindRenderbuffer(GLuint renderbuffer)
506{
507 if (renderbuffer != 0 && !getRenderbuffer(renderbuffer))
508 {
509 mRenderbufferMap[renderbuffer] = new Renderbuffer();
510 }
511
512 this->renderbuffer = renderbuffer;
513}
514
515void Context::useProgram(GLuint program)
516{
517 Program *programObject = getCurrentProgram();
518
519 if (programObject && programObject->isFlaggedForDeletion())
520 {
521 deleteProgram(currentProgram);
522 }
523
524 currentProgram = program;
525}
526
527void Context::setFramebufferZero(Framebuffer *buffer)
528{
529 delete mFramebufferMap[0];
530 mFramebufferMap[0] = buffer;
531}
532
533void Context::setColorbufferZero(Colorbuffer *buffer)
534{
535 delete mColorbufferZero;
536 mColorbufferZero = buffer;
537}
538
539void Context::setDepthbufferZero(Depthbuffer *buffer)
540{
541 delete mDepthbufferZero;
542 mDepthbufferZero = buffer;
543}
544
545void Context::setStencilbufferZero(Stencilbuffer *buffer)
546{
547 delete mStencilbufferZero;
548 mStencilbufferZero = buffer;
549}
550
551void Context::setRenderbuffer(Renderbuffer *buffer)
552{
553 delete mRenderbufferMap[renderbuffer];
554 mRenderbufferMap[renderbuffer] = buffer;
555}
556
557Buffer *Context::getBuffer(unsigned int handle)
558{
559 BufferMap::iterator buffer = mBufferMap.find(handle);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +0000560
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000561 if (buffer == mBufferMap.end())
562 {
563 return NULL;
564 }
565 else
566 {
567 return buffer->second;
568 }
569}
570
571Shader *Context::getShader(unsigned int handle)
572{
573 ShaderMap::iterator shader = mShaderMap.find(handle);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +0000574
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000575 if (shader == mShaderMap.end())
576 {
577 return NULL;
578 }
579 else
580 {
581 return shader->second;
582 }
583}
584
585Program *Context::getProgram(unsigned int handle)
586{
587 ProgramMap::iterator program = mProgramMap.find(handle);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +0000588
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000589 if (program == mProgramMap.end())
590 {
591 return NULL;
592 }
593 else
594 {
595 return program->second;
596 }
597}
598
599Texture *Context::getTexture(unsigned int handle)
600{
601 TextureMap::iterator texture = mTextureMap.find(handle);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +0000602
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000603 if (texture == mTextureMap.end())
604 {
605 return NULL;
606 }
607 else
608 {
609 return texture->second;
610 }
611}
612
613Framebuffer *Context::getFramebuffer(unsigned int handle)
614{
615 FramebufferMap::iterator framebuffer = mFramebufferMap.find(handle);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +0000616
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000617 if (framebuffer == mFramebufferMap.end())
618 {
619 return NULL;
620 }
621 else
622 {
623 return framebuffer->second;
624 }
625}
626
627Renderbuffer *Context::getRenderbuffer(unsigned int handle)
628{
629 RenderbufferMap::iterator renderbuffer = mRenderbufferMap.find(handle);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +0000630
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000631 if (renderbuffer == mRenderbufferMap.end())
632 {
633 return NULL;
634 }
635 else
636 {
637 return renderbuffer->second;
638 }
639}
640
641Colorbuffer *Context::getColorbuffer(GLuint handle)
642{
643 if (handle != 0)
644 {
645 Renderbuffer *renderbuffer = getRenderbuffer(handle);
646
daniel@transgaming.come2b22122010-03-11 19:22:14 +0000647 if (renderbuffer && renderbuffer->isColorbuffer())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000648 {
649 return static_cast<Colorbuffer*>(renderbuffer);
650 }
651 }
652 else // Special case: 0 refers to different initial render targets based on the attachment type
653 {
654 return mColorbufferZero;
655 }
656
657 return NULL;
658}
659
660Depthbuffer *Context::getDepthbuffer(GLuint handle)
661{
662 if (handle != 0)
663 {
664 Renderbuffer *renderbuffer = getRenderbuffer(handle);
665
daniel@transgaming.come2b22122010-03-11 19:22:14 +0000666 if (renderbuffer && renderbuffer->isDepthbuffer())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000667 {
668 return static_cast<Depthbuffer*>(renderbuffer);
669 }
670 }
671 else // Special case: 0 refers to different initial render targets based on the attachment type
672 {
673 return mDepthbufferZero;
674 }
675
676 return NULL;
677}
678
679Stencilbuffer *Context::getStencilbuffer(GLuint handle)
680{
681 if (handle != 0)
682 {
683 Renderbuffer *renderbuffer = getRenderbuffer(handle);
684
daniel@transgaming.come2b22122010-03-11 19:22:14 +0000685 if (renderbuffer && renderbuffer->isStencilbuffer())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000686 {
687 return static_cast<Stencilbuffer*>(renderbuffer);
688 }
689 }
690 else
691 {
692 return mStencilbufferZero;
693 }
694
695 return NULL;
696}
697
698Buffer *Context::getArrayBuffer()
699{
700 return getBuffer(arrayBuffer);
701}
702
703Buffer *Context::getElementArrayBuffer()
704{
705 return getBuffer(elementArrayBuffer);
706}
707
708Program *Context::getCurrentProgram()
709{
710 return getProgram(currentProgram);
711}
712
713Texture2D *Context::getTexture2D()
714{
715 if (texture2D == 0) // Special case: 0 refers to different initial textures based on the target
716 {
717 return mTexture2DZero;
718 }
719
720 return (Texture2D*)getTexture(texture2D);
721}
722
723TextureCubeMap *Context::getTextureCubeMap()
724{
725 if (textureCubeMap == 0) // Special case: 0 refers to different initial textures based on the target
726 {
727 return mTextureCubeMapZero;
728 }
729
730 return (TextureCubeMap*)getTexture(textureCubeMap);
731}
732
daniel@transgaming.com416485f2010-03-16 06:23:23 +0000733Texture *Context::getSamplerTexture(unsigned int sampler, SamplerType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000734{
daniel@transgaming.com416485f2010-03-16 06:23:23 +0000735 return getTexture(samplerTexture[type][sampler]);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000736}
737
738Framebuffer *Context::getFramebuffer()
739{
740 return getFramebuffer(framebuffer);
741}
742
743// Applies the render target surface, depth stencil surface, viewport rectangle and
744// scissor rectangle to the Direct3D 9 device
745bool Context::applyRenderTarget(bool ignoreViewport)
746{
747 IDirect3DDevice9 *device = getDevice();
748 Framebuffer *framebufferObject = getFramebuffer();
749
750 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
751 {
752 return false;
753 }
754
755 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
756 IDirect3DSurface9 *depthStencil = framebufferObject->getDepthStencil();
757
758 device->SetRenderTarget(0, renderTarget);
759 device->SetDepthStencilSurface(depthStencil);
760
761 D3DVIEWPORT9 viewport;
762 D3DSURFACE_DESC desc;
763 renderTarget->GetDesc(&desc);
764
765 if (ignoreViewport)
766 {
767 viewport.X = 0;
768 viewport.Y = 0;
769 viewport.Width = desc.Width;
770 viewport.Height = desc.Height;
771 viewport.MinZ = 0.0f;
772 viewport.MaxZ = 1.0f;
773 }
774 else
775 {
daniel@transgaming.com16973022010-03-11 19:22:19 +0000776 viewport.X = std::max(viewportX, 0);
777 viewport.Y = std::max(viewportY, 0);
778 viewport.Width = std::min(viewportWidth, (int)desc.Width - (int)viewport.X);
779 viewport.Height = std::min(viewportHeight, (int)desc.Height - (int)viewport.Y);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000780 viewport.MinZ = clamp01(zNear);
781 viewport.MaxZ = clamp01(zFar);
782 }
783
784 device->SetViewport(&viewport);
785
786 if (scissorTest)
787 {
788 RECT rect = {scissorX,
789 scissorY,
790 scissorX + scissorWidth,
791 scissorY + scissorHeight};
792
793 device->SetScissorRect(&rect);
794 device->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE);
795 }
796 else
797 {
798 device->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
799 }
800
801 if (currentProgram)
802 {
803 D3DSURFACE_DESC description;
804 renderTarget->GetDesc(&description);
805 Program *programObject = getCurrentProgram();
806
daniel@transgaming.com79b820b2010-03-16 05:48:57 +0000807 GLint halfPixelSize = programObject->getUniformLocation("gl_HalfPixelSize");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000808 GLfloat xy[2] = {1.0f / description.Width, 1.0f / description.Height};
809 programObject->setUniform2fv(halfPixelSize, 1, (GLfloat*)&xy);
daniel@transgaming.com86487c22010-03-11 19:41:43 +0000810
daniel@transgaming.com79b820b2010-03-16 05:48:57 +0000811 GLint window = programObject->getUniformLocation("gl_Window");
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +0000812 GLfloat whxy[4] = {viewportWidth / 2.0f, viewportHeight / 2.0f, (float)viewportX + viewportWidth / 2.0f, (float)viewportY + viewportHeight / 2.0f};
813 programObject->setUniform4fv(window, 1, (GLfloat*)&whxy);
814
daniel@transgaming.com79b820b2010-03-16 05:48:57 +0000815 GLint depth = programObject->getUniformLocation("gl_Depth");
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +0000816 GLfloat dz[2] = {(zFar - zNear) / 2.0f, (zNear + zFar) / 2.0f};
817 programObject->setUniform2fv(depth, 1, (GLfloat*)&dz);
818
daniel@transgaming.com79b820b2010-03-16 05:48:57 +0000819 GLint near = programObject->getUniformLocation("gl_DepthRange.near");
daniel@transgaming.com86487c22010-03-11 19:41:43 +0000820 programObject->setUniform1fv(near, 1, &zNear);
821
daniel@transgaming.com79b820b2010-03-16 05:48:57 +0000822 GLint far = programObject->getUniformLocation("gl_DepthRange.far");
daniel@transgaming.com86487c22010-03-11 19:41:43 +0000823 programObject->setUniform1fv(far, 1, &zFar);
824
daniel@transgaming.com79b820b2010-03-16 05:48:57 +0000825 GLint diff = programObject->getUniformLocation("gl_DepthRange.diff");
daniel@transgaming.com86487c22010-03-11 19:41:43 +0000826 GLfloat zDiff = zFar - zNear;
827 programObject->setUniform1fv(diff, 1, &zDiff);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000828 }
829
830 return true;
831}
832
833// Applies the fixed-function state (culling, depth test, alpha blending, stenciling, etc) to the Direct3D 9 device
834void Context::applyState()
835{
836 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com79b820b2010-03-16 05:48:57 +0000837 Program *programObject = getCurrentProgram();
838
839 GLint frontCCW = programObject->getUniformLocation("__frontCCW");
840 GLint ccw = (frontFace == GL_CCW);
841 programObject->setUniform1iv(frontCCW, 1, &ccw);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000842
843 if (cullFace)
844 {
845 device->SetRenderState(D3DRS_CULLMODE, es2dx::ConvertCullMode(cullMode, frontFace));
846 }
847 else
848 {
849 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
850 }
851
852 if (depthTest)
853 {
854 device->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
855 device->SetRenderState(D3DRS_ZFUNC, es2dx::ConvertComparison(depthFunc));
856 }
857 else
858 {
859 device->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
860 }
861
862 if (blend)
863 {
864 device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
865
866 if (sourceBlendRGB != GL_CONSTANT_ALPHA && sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
867 destBlendRGB != GL_CONSTANT_ALPHA && destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
868 {
869 device->SetRenderState(D3DRS_BLENDFACTOR, es2dx::ConvertColor(blendColor));
870 }
871 else
872 {
873 device->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(unorm<8>(blendColor.alpha),
874 unorm<8>(blendColor.alpha),
875 unorm<8>(blendColor.alpha),
876 unorm<8>(blendColor.alpha)));
877 }
878
879 device->SetRenderState(D3DRS_SRCBLEND, es2dx::ConvertBlendFunc(sourceBlendRGB));
880 device->SetRenderState(D3DRS_DESTBLEND, es2dx::ConvertBlendFunc(destBlendRGB));
881 device->SetRenderState(D3DRS_BLENDOP, es2dx::ConvertBlendOp(blendEquationRGB));
882
883 if (sourceBlendRGB != sourceBlendAlpha || destBlendRGB != destBlendAlpha || blendEquationRGB != blendEquationAlpha)
884 {
885 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
886
887 device->SetRenderState(D3DRS_SRCBLENDALPHA, es2dx::ConvertBlendFunc(sourceBlendAlpha));
888 device->SetRenderState(D3DRS_DESTBLENDALPHA, es2dx::ConvertBlendFunc(destBlendAlpha));
889 device->SetRenderState(D3DRS_BLENDOPALPHA, es2dx::ConvertBlendOp(blendEquationAlpha));
890
891 }
892 else
893 {
894 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
895 }
896 }
897 else
898 {
899 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
900 }
901
902 if (stencilTest)
903 {
904 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
905 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
906
907 // FIXME: Unsupported by D3D9
908 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
909 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
910 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
911 ASSERT(stencilRef == stencilBackRef);
912 ASSERT(stencilMask == stencilBackMask);
913 ASSERT(stencilWritemask == stencilBackWritemask);
914
915 device->SetRenderState(frontFace == GL_CCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, stencilWritemask);
916 device->SetRenderState(frontFace == GL_CCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC, es2dx::ConvertComparison(stencilFunc));
917
918 device->SetRenderState(frontFace == GL_CCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, stencilRef); // FIXME: Clamp to range
919 device->SetRenderState(frontFace == GL_CCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, stencilMask);
920
921 device->SetRenderState(frontFace == GL_CCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL, es2dx::ConvertStencilOp(stencilFail));
922 device->SetRenderState(frontFace == GL_CCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL, es2dx::ConvertStencilOp(stencilPassDepthFail));
923 device->SetRenderState(frontFace == GL_CCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS, es2dx::ConvertStencilOp(stencilPassDepthPass));
924
925 device->SetRenderState(frontFace == GL_CW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, stencilBackWritemask);
926 device->SetRenderState(frontFace == GL_CW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC, es2dx::ConvertComparison(stencilBackFunc));
927
928 device->SetRenderState(frontFace == GL_CW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, stencilBackRef); // FIXME: Clamp to range
929 device->SetRenderState(frontFace == GL_CW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, stencilBackMask);
930
931 device->SetRenderState(frontFace == GL_CW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL, es2dx::ConvertStencilOp(stencilBackFail));
932 device->SetRenderState(frontFace == GL_CW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL, es2dx::ConvertStencilOp(stencilBackPassDepthFail));
933 device->SetRenderState(frontFace == GL_CW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS, es2dx::ConvertStencilOp(stencilBackPassDepthPass));
934 }
935 else
936 {
937 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
938 }
939
940 device->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(colorMaskRed, colorMaskGreen, colorMaskBlue, colorMaskAlpha));
941 device->SetRenderState(D3DRS_ZWRITEENABLE, depthMask ? TRUE : FALSE);
942
943 if (polygonOffsetFill)
944 {
945 UNIMPLEMENTED(); // FIXME
946 }
947
948 if (sampleAlphaToCoverage)
949 {
950 UNIMPLEMENTED(); // FIXME
951 }
952
953 if (sampleCoverage)
954 {
955 UNIMPLEMENTED(); // FIXME: Ignore when SAMPLE_BUFFERS is not one
956 }
957
958 device->SetRenderState(D3DRS_DITHERENABLE, dither ? TRUE : FALSE);
959}
960
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000961// Fill in the programAttribute field of the array of TranslatedAttributes based on the active GLSL program.
962void Context::lookupAttributeMapping(TranslatedAttribute *attributes)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000963{
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000964 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000965 {
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000966 if (attributes[i].enabled)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000967 {
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000968 attributes[i].programAttribute = getCurrentProgram()->getInputMapping(i);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000969 }
970 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000971}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000972
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000973// The indices parameter to glDrawElements can have two interpretations:
974// - as a pointer into client memory
975// - as an offset into the current GL_ELEMENT_ARRAY_BUFFER buffer
976// Handle these cases here and return a pointer to the index data.
977const Index *Context::adjustIndexPointer(const void *indices)
978{
979 if (elementArrayBuffer)
980 {
981 Buffer *buffer = getBuffer(elementArrayBuffer);
982 return reinterpret_cast<const Index*>(static_cast<unsigned char*>(buffer->data()) + reinterpret_cast<GLsizei>(indices));
983 }
984 else
985 {
986 return static_cast<const Index*>(indices);
987 }
988}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000989
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000990void Context::applyVertexBuffer(GLint first, GLsizei count)
991{
992 TranslatedAttribute translated[MAX_VERTEX_ATTRIBS];
993
994 mVertexDataManager->preRenderValidate(first, count, translated);
995
996 lookupAttributeMapping(translated);
997
998 mBufferBackEnd->preDraw(translated);
999}
1000
1001void Context::applyVertexBuffer(GLsizei count, const void *indices, GLenum indexType)
1002{
1003 TranslatedAttribute translated[MAX_VERTEX_ATTRIBS];
1004
1005 mVertexDataManager->preRenderValidate(adjustIndexPointer(indices), count, translated);
1006
1007 lookupAttributeMapping(translated);
1008
1009 mBufferBackEnd->preDraw(translated);
1010}
1011
1012// Applies the indices and element array bindings to the Direct3D 9 device
1013void Context::applyIndexBuffer(const void *indices, GLsizei count)
1014{
1015 GLsizei length = count * sizeof(Index);
1016
1017 IDirect3DDevice9 *device = getDevice();
1018
1019 IDirect3DIndexBuffer9 *indexBuffer = NULL;
1020 void *data;
1021
1022 HRESULT result = device->CreateIndexBuffer(length, 0, D3DFMT_INDEX16, D3DPOOL_MANAGED, &indexBuffer, NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001023
1024 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
1025 {
1026 return error(GL_OUT_OF_MEMORY);
1027 }
1028
1029 ASSERT(SUCCEEDED(result));
1030
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001031 if (indexBuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001032 {
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001033 indexBuffer->Lock(0, length, &data, 0);
1034 memcpy(data, adjustIndexPointer(indices), length);
1035 indexBuffer->Unlock();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001036
1037 device->SetIndices(indexBuffer);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001038 indexBuffer->Release(); // Will only effectively be deleted when no longer in use
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001039 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001040
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001041 startIndex = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001042}
1043
1044// Applies the shaders and shader constants to the Direct3D 9 device
1045void Context::applyShaders()
1046{
1047 IDirect3DDevice9 *device = getDevice();
1048 Program *programObject = getCurrentProgram();
1049 IDirect3DVertexShader9 *vertexShader = programObject->getVertexShader();
1050 IDirect3DPixelShader9 *pixelShader = programObject->getPixelShader();
1051
1052 device->SetVertexShader(vertexShader);
1053 device->SetPixelShader(pixelShader);
1054
1055 programObject->applyUniforms();
1056}
1057
1058// Applies the textures and sampler states to the Direct3D 9 device
1059void Context::applyTextures()
1060{
1061 IDirect3DDevice9 *device = getDevice();
1062 Program *programObject = getCurrentProgram();
1063
1064 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
1065 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001066 int textureUnit = programObject->getSamplerMapping(sampler);
1067 if (textureUnit != -1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001068 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001069 SamplerType textureType = programObject->getSamplerType(sampler);
1070
1071 Texture *texture = getSamplerTexture(textureUnit, textureType);
1072
daniel@transgaming.com12d54072010-03-16 06:23:26 +00001073 if (texture->isComplete())
1074 {
1075 GLenum wrapS = texture->getWrapS();
1076 GLenum wrapT = texture->getWrapT();
1077 GLenum minFilter = texture->getMinFilter();
1078 GLenum magFilter = texture->getMagFilter();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001079
daniel@transgaming.com12d54072010-03-16 06:23:26 +00001080 device->SetSamplerState(sampler, D3DSAMP_ADDRESSU, es2dx::ConvertTextureWrap(wrapS));
1081 device->SetSamplerState(sampler, D3DSAMP_ADDRESSV, es2dx::ConvertTextureWrap(wrapT));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001082
daniel@transgaming.com12d54072010-03-16 06:23:26 +00001083 device->SetSamplerState(sampler, D3DSAMP_MAGFILTER, es2dx::ConvertMagFilter(magFilter));
1084 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
1085 es2dx::ConvertMinFilter(minFilter, &d3dMinFilter, &d3dMipFilter);
1086 device->SetSamplerState(sampler, D3DSAMP_MINFILTER, d3dMinFilter);
1087 device->SetSamplerState(sampler, D3DSAMP_MIPFILTER, d3dMipFilter);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001088
daniel@transgaming.com12d54072010-03-16 06:23:26 +00001089 device->SetTexture(sampler, texture->getTexture());
1090 }
1091 else
1092 {
1093 device->SetTexture(sampler, getIncompleteTexture(textureType)->getTexture());
1094 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001095 }
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001096 else
1097 {
1098 device->SetTexture(sampler, NULL);
1099 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001100 }
1101}
1102
1103void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void* pixels)
1104{
1105 Framebuffer *framebuffer = getFramebuffer();
1106 IDirect3DSurface9 *renderTarget = framebuffer->getRenderTarget();
1107 IDirect3DDevice9 *device = getDevice();
1108
1109 D3DSURFACE_DESC desc;
1110 renderTarget->GetDesc(&desc);
1111
1112 IDirect3DSurface9 *systemSurface;
1113 HRESULT result = device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
1114
1115 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
1116 {
1117 return error(GL_OUT_OF_MEMORY);
1118 }
1119
1120 ASSERT(SUCCEEDED(result));
1121
1122 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
1123 {
1124 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
1125 }
1126
1127 result = device->GetRenderTargetData(renderTarget, systemSurface);
1128
1129 if (result == D3DERR_DRIVERINTERNALERROR)
1130 {
1131 systemSurface->Release();
1132
1133 return error(GL_OUT_OF_MEMORY);
1134 }
1135
1136 if (FAILED(result))
1137 {
1138 UNREACHABLE();
1139 systemSurface->Release();
1140
1141 return; // No sensible error to generate
1142 }
1143
1144 D3DLOCKED_RECT lock;
daniel@transgaming.com16973022010-03-11 19:22:19 +00001145 RECT rect = {std::max(x, 0),
1146 std::max(y, 0),
1147 std::min(x + width, (int)desc.Width),
1148 std::min(y + height, (int)desc.Height)};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001149
1150 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
1151
1152 if (FAILED(result))
1153 {
1154 UNREACHABLE();
1155 systemSurface->Release();
1156
1157 return; // No sensible error to generate
1158 }
1159
1160 unsigned char *source = (unsigned char*)lock.pBits;
1161 unsigned char *dest = (unsigned char*)pixels;
1162
1163 for (int j = 0; j < rect.bottom - rect.top; j++)
1164 {
1165 for (int i = 0; i < rect.right - rect.left; i++)
1166 {
1167 float r;
1168 float g;
1169 float b;
1170 float a;
1171
1172 switch (desc.Format)
1173 {
1174 case D3DFMT_R5G6B5:
1175 {
1176 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
1177
1178 a = 1.0f;
1179 b = (rgb & 0x001F) * (1.0f / 0x001F);
1180 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
1181 r = (rgb & 0xF800) * (1.0f / 0xF800);
1182 }
1183 break;
1184 case D3DFMT_X1R5G5B5:
1185 {
1186 unsigned short xrgb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
1187
1188 a = 1.0f;
1189 b = (xrgb & 0x001F) * (1.0f / 0x001F);
1190 g = (xrgb & 0x03E0) * (1.0f / 0x03E0);
1191 r = (xrgb & 0x7C00) * (1.0f / 0x7C00);
1192 }
1193 break;
1194 case D3DFMT_A1R5G5B5:
1195 {
1196 unsigned short argb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
1197
1198 a = (argb & 0x8000) ? 1.0f : 0.0f;
1199 b = (argb & 0x001F) * (1.0f / 0x001F);
1200 g = (argb & 0x03E0) * (1.0f / 0x03E0);
1201 r = (argb & 0x7C00) * (1.0f / 0x7C00);
1202 }
1203 break;
1204 case D3DFMT_A8R8G8B8:
1205 {
1206 unsigned int argb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
1207
1208 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
1209 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
1210 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
1211 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
1212 }
1213 break;
1214 case D3DFMT_X8R8G8B8:
1215 {
1216 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
1217
1218 a = 1.0f;
1219 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
1220 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
1221 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
1222 }
1223 break;
1224 case D3DFMT_A2R10G10B10:
1225 {
1226 unsigned int argb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
1227
1228 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
1229 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
1230 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
1231 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
1232 }
1233 break;
1234 default:
1235 UNIMPLEMENTED(); // FIXME
1236 UNREACHABLE();
1237 }
1238
1239 switch (format)
1240 {
1241 case GL_RGBA:
1242 switch (type)
1243 {
1244 case GL_UNSIGNED_BYTE:
1245 dest[4 * (i + j * width) + 0] = (unsigned char)(255 * r + 0.5f);
1246 dest[4 * (i + j * width) + 1] = (unsigned char)(255 * g + 0.5f);
1247 dest[4 * (i + j * width) + 2] = (unsigned char)(255 * b + 0.5f);
1248 dest[4 * (i + j * width) + 3] = (unsigned char)(255 * a + 0.5f);
1249 break;
1250 default: UNREACHABLE();
1251 }
1252 break;
1253 case IMPLEMENTATION_COLOR_READ_FORMAT:
1254 switch (type)
1255 {
1256 case IMPLEMENTATION_COLOR_READ_TYPE:
1257 break;
1258 default: UNREACHABLE();
1259 }
1260 break;
1261 default: UNREACHABLE();
1262 }
1263 }
1264 }
1265
1266 systemSurface->UnlockRect();
1267
1268 systemSurface->Release();
1269}
1270
1271void Context::clear(GLbitfield mask)
1272{
1273 IDirect3DDevice9 *device = getDevice();
1274 DWORD flags = 0;
1275
1276 if (mask & GL_COLOR_BUFFER_BIT)
1277 {
1278 mask &= ~GL_COLOR_BUFFER_BIT;
1279 flags |= D3DCLEAR_TARGET;
1280 }
1281
1282 if (mask & GL_DEPTH_BUFFER_BIT)
1283 {
1284 mask &= ~GL_DEPTH_BUFFER_BIT;
1285 if (depthMask)
1286 {
1287 flags |= D3DCLEAR_ZBUFFER;
1288 }
1289 }
1290
1291 Framebuffer *framebufferObject = getFramebuffer();
1292 IDirect3DSurface9 *depthStencil = framebufferObject->getDepthStencil();
1293
1294 GLuint stencilUnmasked = 0x0;
1295
1296 if ((mask & GL_STENCIL_BUFFER_BIT) && depthStencil)
1297 {
1298 D3DSURFACE_DESC desc;
1299 depthStencil->GetDesc(&desc);
1300
1301 mask &= ~GL_STENCIL_BUFFER_BIT;
1302 unsigned int stencilSize = es2dx::GetStencilSize(desc.Format);
1303 stencilUnmasked = (0x1 << stencilSize) - 1;
1304
1305 if (stencilUnmasked != 0x0)
1306 {
1307 flags |= D3DCLEAR_STENCIL;
1308 }
1309 }
1310
1311 if (mask != 0)
1312 {
1313 return error(GL_INVALID_VALUE);
1314 }
1315
1316 applyRenderTarget(true); // Clips the clear to the scissor rectangle but not the viewport
1317
1318 D3DCOLOR color = D3DCOLOR_ARGB(unorm<8>(colorClearValue.alpha), unorm<8>(colorClearValue.red), unorm<8>(colorClearValue.green), unorm<8>(colorClearValue.blue));
1319 float depth = clamp01(depthClearValue);
1320 int stencil = stencilClearValue & 0x000000FF;
1321
1322 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
1323
1324 D3DSURFACE_DESC desc;
1325 renderTarget->GetDesc(&desc);
1326
1327 bool alphaUnmasked = (es2dx::GetAlphaSize(desc.Format) == 0) || colorMaskAlpha;
1328
1329 const bool needMaskedStencilClear = (flags & D3DCLEAR_STENCIL) &&
1330 (stencilWritemask & stencilUnmasked) != stencilUnmasked;
1331 const bool needMaskedColorClear = (flags & D3DCLEAR_TARGET) &&
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00001332 !(colorMaskRed && colorMaskGreen &&
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001333 colorMaskBlue && alphaUnmasked);
1334
1335 if (needMaskedColorClear || needMaskedStencilClear)
1336 {
1337 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
1338 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
1339 device->SetRenderState(D3DRS_ZENABLE, FALSE);
1340 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
1341 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
1342 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
1343 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
1344 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
1345
1346 if (flags & D3DCLEAR_TARGET)
1347 {
1348 device->SetRenderState(D3DRS_COLORWRITEENABLE, (colorMaskRed ? D3DCOLORWRITEENABLE_RED : 0) |
1349 (colorMaskGreen ? D3DCOLORWRITEENABLE_GREEN : 0) |
1350 (colorMaskBlue ? D3DCOLORWRITEENABLE_BLUE : 0) |
1351 (colorMaskAlpha ? D3DCOLORWRITEENABLE_ALPHA : 0));
1352 }
1353 else
1354 {
1355 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
1356 }
1357
1358 if (stencilUnmasked != 0x0 && (flags & D3DCLEAR_STENCIL))
1359 {
1360 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
1361 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
1362 device->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
1363 device->SetRenderState(D3DRS_STENCILREF, stencil);
1364 device->SetRenderState(D3DRS_STENCILWRITEMASK, stencilWritemask);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00001365 device->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001366 device->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
1367 device->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
1368 }
1369 else
1370 {
1371 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1372 }
1373
1374 device->SetPixelShader(NULL);
1375 device->SetVertexShader(NULL);
1376 device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
1377
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00001378 struct Vertex
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001379 {
1380 float x, y, z, w;
1381 D3DCOLOR diffuse;
1382 };
1383
1384 Vertex quad[4];
1385 quad[0].x = 0.0f;
1386 quad[0].y = (float)desc.Height;
1387 quad[0].z = 0.0f;
1388 quad[0].w = 1.0f;
1389 quad[0].diffuse = color;
1390
1391 quad[1].x = (float)desc.Width;
1392 quad[1].y = (float)desc.Height;
1393 quad[1].z = 0.0f;
1394 quad[1].w = 1.0f;
1395 quad[1].diffuse = color;
1396
1397 quad[2].x = 0.0f;
1398 quad[2].y = 0.0f;
1399 quad[2].z = 0.0f;
1400 quad[2].w = 1.0f;
1401 quad[2].diffuse = color;
1402
1403 quad[3].x = (float)desc.Width;
1404 quad[3].y = 0.0f;
1405 quad[3].z = 0.0f;
1406 quad[3].w = 1.0f;
1407 quad[3].diffuse = color;
1408
1409 device->BeginScene();
1410 device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(Vertex));
1411 device->EndScene();
1412
1413 if (flags & D3DCLEAR_ZBUFFER)
1414 {
1415 device->SetRenderState(D3DRS_ZENABLE, TRUE);
1416 device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
1417 device->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
1418 }
1419 }
1420 else
1421 {
1422 device->Clear(0, NULL, flags, color, depth, stencil);
1423 }
1424}
1425
1426void Context::drawArrays(GLenum mode, GLint first, GLsizei count)
1427{
1428 if (!currentProgram)
1429 {
1430 return error(GL_INVALID_OPERATION);
1431 }
1432
1433 IDirect3DDevice9 *device = getDevice();
1434 D3DPRIMITIVETYPE primitiveType;
1435 int primitiveCount;
1436
1437 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
1438 return error(GL_INVALID_ENUM);
1439
1440 if (primitiveCount <= 0)
1441 {
1442 return;
1443 }
1444
1445 if (!applyRenderTarget(false))
1446 {
1447 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
1448 }
1449
1450 applyState();
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001451 applyVertexBuffer(first, count);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001452 applyShaders();
1453 applyTextures();
1454
1455 device->BeginScene();
1456 device->DrawPrimitive(primitiveType, first, primitiveCount);
1457 device->EndScene();
1458}
1459
1460void Context::drawElements(GLenum mode, GLsizei count, GLenum type, const void* indices)
1461{
1462 if (!currentProgram)
1463 {
1464 return error(GL_INVALID_OPERATION);
1465 }
1466
1467 if (!indices && !elementArrayBuffer)
1468 {
1469 return error(GL_INVALID_OPERATION);
1470 }
1471
1472 IDirect3DDevice9 *device = getDevice();
1473 D3DPRIMITIVETYPE primitiveType;
1474 int primitiveCount;
1475
1476 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
1477 return error(GL_INVALID_ENUM);
1478
1479 if (primitiveCount <= 0)
1480 {
1481 return;
1482 }
1483
1484 if (!applyRenderTarget(false))
1485 {
1486 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
1487 }
1488
1489 applyState();
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001490 applyVertexBuffer(count, indices, type);
1491 applyIndexBuffer(indices, count);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001492 applyShaders();
1493 applyTextures();
1494
1495 device->BeginScene();
1496 device->DrawIndexedPrimitive(primitiveType, 0, 0, count, startIndex, primitiveCount);
1497 device->EndScene();
1498}
1499
1500void Context::finish()
1501{
1502 IDirect3DDevice9 *device = getDevice();
1503 IDirect3DQuery9 *occlusionQuery = NULL;
1504
1505 HRESULT result = device->CreateQuery(D3DQUERYTYPE_OCCLUSION, &occlusionQuery);
1506
1507 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
1508 {
1509 return error(GL_OUT_OF_MEMORY);
1510 }
1511
1512 ASSERT(SUCCEEDED(result));
1513
1514 if (occlusionQuery)
1515 {
1516 occlusionQuery->Issue(D3DISSUE_BEGIN);
1517
1518 // Render something outside the render target
1519 device->SetPixelShader(NULL);
1520 device->SetVertexShader(NULL);
1521 device->SetFVF(D3DFVF_XYZRHW);
1522 float data[4] = {-1.0f, -1.0f, -1.0f, 1.0f};
1523 device->BeginScene();
1524 device->DrawPrimitiveUP(D3DPT_POINTLIST, 1, data, sizeof(data));
1525 device->EndScene();
1526
1527 occlusionQuery->Issue(D3DISSUE_END);
1528
1529 while (occlusionQuery->GetData(NULL, 0, D3DGETDATA_FLUSH) == S_FALSE)
1530 {
1531 // Keep polling, but allow other threads to do something useful first
1532 Sleep(0);
1533 }
1534
1535 occlusionQuery->Release();
1536 }
1537}
1538
1539void Context::flush()
1540{
1541 IDirect3DDevice9 *device = getDevice();
1542 IDirect3DQuery9 *eventQuery = NULL;
1543
1544 HRESULT result = device->CreateQuery(D3DQUERYTYPE_EVENT, &eventQuery);
1545
1546 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
1547 {
1548 return error(GL_OUT_OF_MEMORY);
1549 }
1550
1551 ASSERT(SUCCEEDED(result));
1552
1553 if (eventQuery)
1554 {
1555 eventQuery->Issue(D3DISSUE_END);
1556
1557 while (eventQuery->GetData(NULL, 0, D3DGETDATA_FLUSH) == S_FALSE)
1558 {
1559 // Keep polling, but allow other threads to do something useful first
1560 Sleep(0);
1561 }
1562
1563 eventQuery->Release();
1564 }
1565}
1566
1567void Context::recordInvalidEnum()
1568{
1569 mInvalidEnum = true;
1570}
1571
1572void Context::recordInvalidValue()
1573{
1574 mInvalidValue = true;
1575}
1576
1577void Context::recordInvalidOperation()
1578{
1579 mInvalidOperation = true;
1580}
1581
1582void Context::recordOutOfMemory()
1583{
1584 mOutOfMemory = true;
1585}
1586
1587void Context::recordInvalidFramebufferOperation()
1588{
1589 mInvalidFramebufferOperation = true;
1590}
1591
1592// Get one of the recorded errors and clear its flag, if any.
1593// [OpenGL ES 2.0.24] section 2.5 page 13.
1594GLenum Context::getError()
1595{
1596 if (mInvalidEnum)
1597 {
1598 mInvalidEnum = false;
1599
1600 return GL_INVALID_ENUM;
1601 }
1602
1603 if (mInvalidValue)
1604 {
1605 mInvalidValue = false;
1606
1607 return GL_INVALID_VALUE;
1608 }
1609
1610 if (mInvalidOperation)
1611 {
1612 mInvalidOperation = false;
1613
1614 return GL_INVALID_OPERATION;
1615 }
1616
1617 if (mOutOfMemory)
1618 {
1619 mOutOfMemory = false;
1620
1621 return GL_OUT_OF_MEMORY;
1622 }
1623
1624 if (mInvalidFramebufferOperation)
1625 {
1626 mInvalidFramebufferOperation = false;
1627
1628 return GL_INVALID_FRAMEBUFFER_OPERATION;
1629 }
1630
1631 return GL_NO_ERROR;
1632}
1633
1634void Context::detachBuffer(GLuint buffer)
1635{
1636 // [OpenGL ES 2.0.24] section 2.9 page 22:
1637 // If a buffer object is deleted while it is bound, all bindings to that object in the current context
1638 // (i.e. in the thread that called Delete-Buffers) are reset to zero.
1639
1640 if (arrayBuffer == buffer)
1641 {
1642 arrayBuffer = 0;
1643 }
1644
1645 if (elementArrayBuffer == buffer)
1646 {
1647 elementArrayBuffer = 0;
1648 }
1649
1650 for (int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
1651 {
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001652 if (vertexAttribute[attribute].mBoundBuffer == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001653 {
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001654 vertexAttribute[attribute].mBoundBuffer = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001655 }
1656 }
1657}
1658
1659void Context::detachTexture(GLuint texture)
1660{
1661 // [OpenGL ES 2.0.24] section 3.8 page 84:
1662 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
1663 // rebound to texture object zero
1664
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001665 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001666 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001667 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001668 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001669 if (samplerTexture[type][sampler] == texture)
1670 {
1671 samplerTexture[type][sampler] = 0;
1672 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001673 }
1674 }
1675
1676 // [OpenGL ES 2.0.24] section 4.4 page 112:
1677 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
1678 // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this
1679 // image was attached in the currently bound framebuffer.
1680
1681 Framebuffer *framebuffer = getFramebuffer();
1682
1683 if (framebuffer)
1684 {
1685 framebuffer->detachTexture(texture);
1686 }
1687}
1688
1689void Context::detachFramebuffer(GLuint framebuffer)
1690{
1691 // [OpenGL ES 2.0.24] section 4.4 page 107:
1692 // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though
1693 // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero.
1694
1695 if (this->framebuffer == framebuffer)
1696 {
1697 bindFramebuffer(0);
1698 }
1699}
1700
1701void Context::detachRenderbuffer(GLuint renderbuffer)
1702{
1703 // [OpenGL ES 2.0.24] section 4.4 page 109:
1704 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
1705 // had been executed with the target RENDERBUFFER and name of zero.
1706
1707 if (this->renderbuffer == renderbuffer)
1708 {
1709 bindRenderbuffer(0);
1710 }
1711
1712 // [OpenGL ES 2.0.24] section 4.4 page 111:
1713 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
1714 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
1715 // point to which this image was attached in the currently bound framebuffer.
1716
1717 Framebuffer *framebuffer = getFramebuffer();
1718
1719 if (framebuffer)
1720 {
1721 framebuffer->detachRenderbuffer(renderbuffer);
1722 }
1723}
daniel@transgaming.com12d54072010-03-16 06:23:26 +00001724
1725Texture *Context::getIncompleteTexture(SamplerType type)
1726{
1727 Texture *t = mIncompleteTextures[type];
1728
1729 if (t == NULL)
1730 {
1731 static const GLubyte color[] = { 0, 0, 0, 255 };
1732
1733 switch (type)
1734 {
1735 default:
1736 UNREACHABLE();
1737 // default falls through to SAMPLER_2D
1738
1739 case SAMPLER_2D:
1740 {
1741 Texture2D *incomplete2d = new Texture2D;
1742 incomplete2d->setImage(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, color);
1743 t = incomplete2d;
1744 }
1745 break;
1746
1747 case SAMPLER_CUBE:
1748 {
1749 TextureCubeMap *incompleteCube = new TextureCubeMap;
1750
1751 incompleteCube->setImagePosX(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, color);
1752 incompleteCube->setImageNegX(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, color);
1753 incompleteCube->setImagePosY(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, color);
1754 incompleteCube->setImageNegY(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, color);
1755 incompleteCube->setImagePosZ(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, color);
1756 incompleteCube->setImageNegZ(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, color);
1757
1758 t = incompleteCube;
1759 }
1760 break;
1761 }
1762
1763 mIncompleteTextures[type] = t;
1764 }
1765
1766 return t;
1767}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001768}
1769
1770extern "C"
1771{
1772gl::Context *glCreateContext(const egl::Config *config)
1773{
1774 return new gl::Context(config);
1775}
1776
1777void glDestroyContext(gl::Context *context)
1778{
1779 delete context;
1780
1781 if (context == gl::getContext())
1782 {
1783 gl::makeCurrent(NULL, NULL, NULL);
1784 }
1785}
1786
1787void glMakeCurrent(gl::Context *context, egl::Display *display, egl::Surface *surface)
1788{
1789 gl::makeCurrent(context, display, surface);
1790}
1791
1792gl::Context *glGetCurrentContext()
1793{
1794 return gl::getContext();
1795}
1796}