blob: d77b784ebcf5402547b3ba26d6749a7f933bf54f [file] [log] [blame]
John Bauman66b8ab22014-05-06 15:57:45 -04001// SwiftShader Software Renderer
2//
John Baumand4ae8632014-05-06 16:18:33 -04003// Copyright(c) 2005-2013 TransGaming Inc.
John Bauman66b8ab22014-05-06 15:57:45 -04004//
5// All rights reserved. No part of this software may be copied, distributed, transmitted,
6// transcribed, stored in a retrieval system, translated into any human or computer
7// language by any means, or disclosed to third parties without the explicit written
8// agreement of TransGaming Inc. Without such an agreement, no rights or licenses, express
9// or implied, including but not limited to any patent rights, are granted to you.
10//
11
12// main.cpp: DLL entry point and management of thread-local data.
13
14#include "main.h"
15
Nicolas Capensa2308052015-04-15 16:50:21 -040016#include "libGLESv2.hpp"
John Bauman66b8ab22014-05-06 15:57:45 -040017#include "Framebuffer.h"
Nicolas Capensa2308052015-04-15 16:50:21 -040018#include "libEGL/main.h"
John Bauman66b8ab22014-05-06 15:57:45 -040019#include "libEGL/Surface.h"
20#include "Common/Thread.hpp"
Nicolas Capensafd1f9f2014-10-28 02:53:50 -040021#include "Common/SharedLibrary.hpp"
John Bauman66b8ab22014-05-06 15:57:45 -040022#include "common/debug.h"
23
John Bauman66b8ab22014-05-06 15:57:45 -040024#if !defined(_MSC_VER)
25#define CONSTRUCTOR __attribute__((constructor))
26#define DESTRUCTOR __attribute__((destructor))
27#else
28#define CONSTRUCTOR
29#define DESTRUCTOR
30#endif
31
32static void glAttachThread()
33{
34 TRACE("()");
John Bauman66b8ab22014-05-06 15:57:45 -040035}
36
37static void glDetachThread()
38{
39 TRACE("()");
John Bauman66b8ab22014-05-06 15:57:45 -040040}
41
Nicolas Capensa2308052015-04-15 16:50:21 -040042CONSTRUCTOR static void glAttachProcess()
John Bauman66b8ab22014-05-06 15:57:45 -040043{
44 TRACE("()");
45
John Bauman66b8ab22014-05-06 15:57:45 -040046 glAttachThread();
John Bauman66b8ab22014-05-06 15:57:45 -040047}
48
49DESTRUCTOR static void glDetachProcess()
50{
51 TRACE("()");
52
53 glDetachThread();
John Bauman66b8ab22014-05-06 15:57:45 -040054}
55
56#if defined(_WIN32)
57extern "C" BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, LPVOID reserved)
58{
59 switch(reason)
60 {
61 case DLL_PROCESS_ATTACH:
Nicolas Capensa2308052015-04-15 16:50:21 -040062 glAttachProcess();
John Bauman66b8ab22014-05-06 15:57:45 -040063 break;
64 case DLL_THREAD_ATTACH:
65 glAttachThread();
66 break;
67 case DLL_THREAD_DETACH:
68 glDetachThread();
69 break;
70 case DLL_PROCESS_DETACH:
71 glDetachProcess();
72 break;
73 default:
74 break;
75 }
76
77 return TRUE;
78}
79#endif
80
Nicolas Capens14ee7622014-10-28 23:48:41 -040081namespace es2
John Bauman66b8ab22014-05-06 15:57:45 -040082{
Nicolas Capens14ee7622014-10-28 23:48:41 -040083es2::Context *getContext()
John Baumand4ae8632014-05-06 16:18:33 -040084{
Nicolas Capensa2308052015-04-15 16:50:21 -040085 egl::Context *context = libEGL->clientGetCurrentContext();
Nicolas Capens8d869e02014-12-08 16:52:06 -050086
Nicolas Capensb97ad2e2015-02-11 17:40:30 -050087 if(context && (context->getClientVersion() == 2 ||
88 context->getClientVersion() == 3))
John Baumand4ae8632014-05-06 16:18:33 -040089 {
Nicolas Capens14ee7622014-10-28 23:48:41 -040090 return static_cast<es2::Context*>(context);
John Baumand4ae8632014-05-06 16:18:33 -040091 }
Nicolas Capens8d869e02014-12-08 16:52:06 -050092
Nicolas Capensafd1f9f2014-10-28 02:53:50 -040093 return 0;
John Bauman66b8ab22014-05-06 15:57:45 -040094}
95
96egl::Display *getDisplay()
97{
Nicolas Capensa2308052015-04-15 16:50:21 -040098 return libEGL->clientGetCurrentDisplay();
John Bauman66b8ab22014-05-06 15:57:45 -040099}
100
101Device *getDevice()
102{
Nicolas Capensc78445c2014-10-27 17:29:04 -0400103 Context *context = getContext();
John Bauman66b8ab22014-05-06 15:57:45 -0400104
Nicolas Capensc78445c2014-10-27 17:29:04 -0400105 return context ? context->getDevice() : 0;
John Bauman66b8ab22014-05-06 15:57:45 -0400106}
Nicolas Capensd76b5df2014-10-28 15:54:46 -0400107
John Bauman66b8ab22014-05-06 15:57:45 -0400108// Records an error code
109void error(GLenum errorCode)
110{
Nicolas Capens14ee7622014-10-28 23:48:41 -0400111 es2::Context *context = es2::getContext();
John Bauman66b8ab22014-05-06 15:57:45 -0400112
113 if(context)
114 {
115 switch(errorCode)
116 {
117 case GL_INVALID_ENUM:
118 context->recordInvalidEnum();
119 TRACE("\t! Error generated: invalid enum\n");
120 break;
121 case GL_INVALID_VALUE:
122 context->recordInvalidValue();
123 TRACE("\t! Error generated: invalid value\n");
124 break;
125 case GL_INVALID_OPERATION:
126 context->recordInvalidOperation();
127 TRACE("\t! Error generated: invalid operation\n");
128 break;
129 case GL_OUT_OF_MEMORY:
130 context->recordOutOfMemory();
131 TRACE("\t! Error generated: out of memory\n");
132 break;
133 case GL_INVALID_FRAMEBUFFER_OPERATION:
134 context->recordInvalidFramebufferOperation();
135 TRACE("\t! Error generated: invalid framebuffer operation\n");
136 break;
137 default: UNREACHABLE();
138 }
139 }
140}
Nicolas Capensa2308052015-04-15 16:50:21 -0400141}
Nicolas Capensafd1f9f2014-10-28 02:53:50 -0400142
143namespace egl
144{
Nicolas Capensa2308052015-04-15 16:50:21 -0400145GLint getClientVersion()
Nicolas Capensd76b5df2014-10-28 15:54:46 -0400146{
Nicolas Capensa2308052015-04-15 16:50:21 -0400147 Context *context = libEGL->clientGetCurrentContext();
148
149 return context ? context->getClientVersion() : 0;
150}
Nicolas Capensd76b5df2014-10-28 15:54:46 -0400151}
152
Nicolas Capensa2308052015-04-15 16:50:21 -0400153egl::Context *es2CreateContext(const egl::Config *config, const egl::Context *shareContext, int clientVersion);
154extern "C" __eglMustCastToProperFunctionPointerType es2GetProcAddress(const char *procname);
155egl::Image *createBackBuffer(int width, int height, const egl::Config *config);
156egl::Image *createDepthStencil(unsigned int width, unsigned int height, sw::Format format, int multiSampleDepth, bool discard);
157sw::FrameBuffer *createFrameBuffer(EGLNativeDisplayType display, EGLNativeWindowType window, int width, int height);
158
159LibGLESv2exports::LibGLESv2exports()
160{
161 this->es2CreateContext = ::es2CreateContext;
162 this->es2GetProcAddress = ::es2GetProcAddress;
163 this->createBackBuffer = ::createBackBuffer;
164 this->createDepthStencil = ::createDepthStencil;
165 this->createFrameBuffer = ::createFrameBuffer;
166}
167
168extern "C" LibGLESv2exports *libGLESv2exports()
169{
170 static LibGLESv2exports libGLESv2;
171 return &libGLESv2;
172}
173
174LibEGL libEGL;
175LibGLES_CM libGLES_CM;