· 5 years ago · Nov 04, 2020, 03:12 PM
1
2 if (cmd == RETRO_ENVIRONMENT_SET_HW_RENDER) //14
3 {
4 if (!AA_LIBRETRO_3D)
5 {
6 DevMsg("libretro: Denying request for OpenGL context.\n");
7 return false;
8 }
9 else
10 {
11 DevMsg("libretro: Core requesting HW context: ");
12
13 struct retro_hw_render_callback * render = (struct retro_hw_render_callback*)data;
14 render->get_current_framebuffer = v3d_get_current_framebuffer;
15 render->get_proc_address = v3d_get_proc_address;
16
17 info->raw->context_reset = (retro_hw_context_reset_t)render->context_reset;
18 info->raw->context_destroy = (retro_hw_context_reset_t)render->context_destroy;
19 info->context_type = render->context_type;
20 info->depth = render->depth;
21 info->stencil = render->stencil;
22 info->bottom_left_origin = render->bottom_left_origin;
23 //info->version_major;
24 //info->version_minor;
25 info->cache_context = render->cache_context;
26 info->debug_context = render->debug_context;
27
28 //GLFW_ORIGIN_UL_BIT;
29 //glfwReadImage with the GLFW_ORIGIN_UL_BIT
30
31 unsigned api;
32 switch (info->context_type)
33 {
34 case RETRO_HW_CONTEXT_NONE:
35 DevMsg("NONE (UNSUPPORTED)\n");
36 api = GLFW_NO_API;
37 info->version_major = 0;
38 info->version_minor = 0;
39 break;
40
41 case RETRO_HW_CONTEXT_OPENGL:
42 DevMsg("OpenGL (2.x)\n");
43 api = GLFW_OPENGL_API;
44 info->version_major = 2;
45 info->version_minor = 0;
46 break;
47
48 case RETRO_HW_CONTEXT_OPENGLES2:
49 DevMsg("OpenGL ES (2.0)\n");
50 api = GLFW_OPENGL_ES_API;
51 info->version_major = 2;
52 info->version_minor = 0;
53 break;
54
55 case RETRO_HW_CONTEXT_OPENGL_CORE:
56 DevMsg("OpenGL (%u.%u)\n", render->version_major, render->version_minor);
57 api = GLFW_OPENGL_API;
58 info->version_major = render->version_major;
59 info->version_minor = render->version_minor;
60 break;
61
62 case RETRO_HW_CONTEXT_OPENGLES3:
63 DevMsg("OpenGL ES (3.0)\n");
64 api = GLFW_OPENGL_ES_API;
65 info->version_major = 3;
66 info->version_minor = 0;
67 break;
68
69 case RETRO_HW_CONTEXT_OPENGLES_VERSION:
70 DevMsg("OpenGL ES (%u.%u)\n", render->version_major, render->version_minor);
71 api = GLFW_OPENGL_ES_API;
72 info->version_major = render->version_major;
73 info->version_minor = render->version_minor;
74 break;
75
76 case RETRO_HW_CONTEXT_VULKAN:
77 DevMsg("Vulkan (UNSUPPORTED)\n");
78 api = GLFW_NO_API;
79 info->version_major = 0;
80 info->version_minor = 0;
81 break;
82
83 default:
84 DevMsg("UNKNOWN (UNSUPPORTED)\n");
85 api = GLFW_NO_API;
86 info->version_major = 0;
87 info->version_minor = 0;
88 break;
89 }
90
91 DevMsg("\tdepth: %i\n", info->depth);
92 DevMsg("\tstencil: %i\n", info->stencil);
93 DevMsg("\tbottom_left_origin: %i\n", info->bottom_left_origin);
94 DevMsg("\tversion_major: %u\n", info->version_major);
95 DevMsg("\tversion_minor: %u\n", info->version_minor);
96 DevMsg("\tcache_context: %i\n", info->cache_context);
97 DevMsg("\tdebug_context: %i\n", info->debug_context);
98
99 // init 3d
100 if (glfwInit())
101 {
102 //glfwWindowHint(GLFW_SAMPLES, 4);
103 //glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
104 //glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
105 glfwWindowHint(GLFW_CLIENT_API, api);
106 glfwWindowHint(GLFW_CONTEXT_CREATION_API, GLFW_NATIVE_CONTEXT_API);
107 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, info->version_major);
108 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, info->version_minor);
109 glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
110 glfwWindowHint(GLFW_DECORATED, GL_FALSE);
111 //glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // To make MacOS happy; should not be needed
112 //glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_COMPAT_PROFILE);// GLFW_OPENGL_ANY_PROFILE);// GLFW_OPENGL_CORE_PROFILE);
113 glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE); // invisible window
114
115 //GLFWwindow* window = glfwCreateWindow(640, 480, "", NULL, NULL);
116 //HWND myHWnd = FindWindow(null, "AArcade: Source");
117 GLFWwindow* window = glfwCreateWindow(1280, 720, "My OPENGL", NULL, NULL);
118 //glfwGetWGLContext(window);
119
120 if (window)
121 {
122 // render->get_current_framebuffer = v3d_get_current_framebuffer;
123 //render->get_proc_address = v3d_get_proc_address;
124 // return true;
125
126 glewExperimental = GL_TRUE; // Needed in core profile
127 glfwMakeContextCurrent(window);
128 glewInit();
129
130 DevMsg("Window created!\n");
131 info->window = window;
132 glfwSetFramebufferSizeCallback(info->window, framebuffer_size_callback); // to detect framebuffer size changes (probably not needed for libretro)
133
134 //glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); // to detect framebuffer size changes (probably not needed for libretro)
135
136 // get version info
137 const GLubyte* renderer = glGetString(GL_RENDERER); // get renderer string
138 const GLubyte* version = glGetString(GL_VERSION); // version as a string
139 DevMsg("Renderer: %s\n", renderer);
140 DevMsg("OpenGL version supported %s\n", version);
141 DevMsg("=========================\n");
142
143 //info->framebuffer = new GLuint[1];
144 //glGenFramebuffers(1, &info->framebuffer[0]);
145 //glBindFramebuffer(GL_FRAMEBUFFER, info->framebuffer[0]);
146
147
148 /*
149 info->framebuffer = new GLuint[1];
150 GLuint* tex = new GLuint[1];
151 glGenTextures(1, tex);
152 glBindTexture(GL_TEXTURE_2D, tex[0]);
153 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1280, 720, 0, GL_RGB8, GL_UNSIGNED_BYTE, null); // GL_RGB
154 glGenFramebuffers(1, &info->framebuffer[0]);
155 glBindFramebuffer(GL_FRAMEBUFFER, info->framebuffer[0]);
156 glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, tex[0], 0);
157 if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
158 DevMsg("Something went wrong.\n");
159 else
160 {
161 int width, height;
162 glfwGetFramebufferSize(window, &width, &height);
163 DevMsg("Frame buffer ready: %i x %i\n", width, height);
164 }
165 */
166
167 //glfwPollEvents();
168
169 //glDisable(GL_DEPTH_TEST); // here for illustrative purposes, depth test is initially DISABLED (key!)
170 //glClearColor(0.3f, 0.4f, 0.1f, 1.0f);
171 //glClear(GL_COLOR_BUFFER_BIT);
172 //glfwSwapBuffers(info->window);
173 //glfwPollEvents();
174
175 //render->context_reset = v3d_context_reset;
176 //render->context_destroy = v3d_context_destroy;
177
178 glDisable(GL_DEPTH_TEST);
179 //glfwSwapInterval(1);
180
181 // The depth buffer
182 /*
183 GLuint depthrenderbuffer;
184 glGenRenderbuffers(1, &depthrenderbuffer);
185 glBindRenderbuffer(GL_RENDERBUFFER, depthrenderbuffer);
186 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, 1280, 720);
187 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthrenderbuffer);
188 glEnable(GL_DEPTH_TEST);
189 */
190
191 //glEnable(GL_DOUBLEBUFFER);
192 //glEnable(GL_RGB);
193 //glPixelStorei(GL_PACK_ALIGNMENT, 3);
194 glClearColor(0.3f, 0.4f, 0.1f, 1.0f);
195 /*
196 if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
197 DevMsg("Something went wrong.\n");
198 else
199 {
200 int width, height;
201 glfwGetFramebufferSize(window, &width, &height);
202 DevMsg("Frame buffer ready: %i x %i\n", width, height);
203 }
204 */
205
206 //glViewport(0, 0, 1280, 720); // Render on the whole framebuffer, complete from the lower left corner to the upper right
207 // reset the context (TODO: Make sure the window is ready to reset its context!
208 info->raw->context_reset();
209
210 DevMsg("done.\n");
211
212
213
214
215
216
217
218
219 // The texture we're going to render to
220 //GLuint renderedTexture;
221 //glGenTextures(1, &renderedTexture);
222
223 // "Bind" the newly created texture : all future texture functions will modify this texture
224 //glBindTexture(GL_TEXTURE_2D, renderedTexture);
225
226 // Give an empty image to OpenGL ( the last "0" )
227 //glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1024, 1024, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
228
229 // Poor filtering. Needed !
230 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
231 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
232
233 // just make sure to tell glfw you want depth/stencil buffers; if you're using fb0, you can't change that after creating the window
234 // Alcaro: glfw deals with context creation, if you're using that then you can ignore anything in the ifdefs
235
236 // The depth buffer
237 //GLuint depthrenderbuffer;
238 //glGenRenderbuffers(1, &depthrenderbuffer);
239 //glBindRenderbuffer(GL_RENDERBUFFER, depthrenderbuffer);
240 //glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, 1024, 1024);
241 //glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthrenderbuffer);
242
243 // Set "renderedTexture" as our colour attachement #0
244 //glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, renderedTexture, 0);
245
246 // Set the list of draw buffers.
247 //GLenum DrawBuffers[1] = { GL_COLOR_ATTACHMENT0 };
248 //glGenFramebuffers(1, &info->framebuffer);
249 //glBindFramebuffer(GL_FRAMEBUFFER, info->framebuffer);
250 //glDrawBuffers(1, DrawBuffers); // "1" is the size of DrawBuffers
251
252 // Always check that our framebuffer is ok
253 //if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
254 //{
255 //// DevMsg("Something went wrong.\n");
256 //}
257 //else
258 // DevMsg("Frame buffer ready.\n");
259
260 // Render to our framebuffer
261 //glBindFramebuffer(GL_FRAMEBUFFER, info->framebuffer);
262 //glViewport(0, 0, 1024, 1024); // Render on the whole framebuffer, complete from the lower left corner to the upper right
263
264 //glCreateContex()
265
266 //glfwGetWGLContext(window);
267
268 //glfwGetWGLContext
269
270 //DevMsg("done changing frame buffer.\n");
271 }
272 else
273 DevMsg("Failed to create openGL window.\n");
274
275 //if (info->window)
276 //{
277 //DevMsg("Painting initial frame...\n");
278 /*
279 // background color
280 glDisable(GL_DEPTH_TEST); // here for illustrative purposes, depth test is initially DISABLED (key!)
281 glClearColor(0.3f, 0.4f, 0.1f, 1.0f);
282 glClear(GL_COLOR_BUFFER_BIT);
283
284 glfwSwapBuffers(info->window);
285 glfwPollEvents();
286 */
287
288 //glfwPollEvents();
289 //render->context_reset = v3d_context_reset;
290 //render->context_destroy = v3d_context_destroy;
291 //render->get_current_framebuffer = //(uintptr_t)GL_FRAMEBUFFER;// (uintptr_t)info->framebuffer;// info->framebuffer;
292 //render->get_current_framebuffer = v3d_get_current_framebuffer;
293 //render->get_proc_address = v3d_get_proc_address;
294 //}
295
296 DevMsg("Fin\n");
297 return true;
298 }