| 
									
										
										
										
											2015-05-24 06:55:12 +02:00
										 |  |  | // Copyright 2008 Dolphin Emulator Project
 | 
					
						
							| 
									
										
										
										
											2015-05-18 01:08:10 +02:00
										 |  |  | // Licensed under GPLv2+
 | 
					
						
							| 
									
										
										
										
											2013-04-17 23:29:41 -04:00
										 |  |  | // Refer to the license.txt file included.
 | 
					
						
							| 
									
										
										
										
											2008-12-08 05:25:12 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-01 10:56:13 -05:00
										 |  |  | #include "Common/CommonTypes.h"
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  | #include "Common/GL/GLUtil.h"
 | 
					
						
							| 
									
										
										
										
											2017-02-01 10:56:13 -05:00
										 |  |  | #include "Common/MsgHandler.h"
 | 
					
						
							| 
									
										
										
										
											2008-12-08 05:25:12 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-17 05:18:15 -05:00
										 |  |  | #include "VideoBackends/OGL/ProgramShaderCache.h"
 | 
					
						
							|  |  |  | #include "VideoBackends/OGL/VertexManager.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "VideoCommon/NativeVertexFormat.h"
 | 
					
						
							|  |  |  | #include "VideoCommon/VertexShaderGen.h"
 | 
					
						
							| 
									
										
										
										
											2008-12-08 05:25:12 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | // Here's some global state. We only use this to keep track of what we've sent to the OpenGL state
 | 
					
						
							|  |  |  | // machine.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-11-26 09:25:08 +00:00
										 |  |  | namespace OGL | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  | NativeVertexFormat* | 
					
						
							|  |  |  | VertexManager::CreateNativeVertexFormat(const PortableVertexDeclaration& vtx_decl) | 
					
						
							| 
									
										
										
										
											2008-12-25 15:56:36 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   return new GLVertexFormat(vtx_decl); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:25:12 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-15 14:16:04 -04:00
										 |  |  | static inline GLuint VarToGL(VarType t) | 
					
						
							| 
									
										
										
										
											2008-12-08 05:25:12 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   static const GLuint lookup[5] = {GL_UNSIGNED_BYTE, GL_BYTE, GL_UNSIGNED_SHORT, GL_SHORT, | 
					
						
							|  |  |  |                                    GL_FLOAT}; | 
					
						
							|  |  |  |   return lookup[t]; | 
					
						
							| 
									
										
										
										
											2008-12-08 05:25:12 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  | static void SetPointer(u32 attrib, u32 stride, const AttributeFormat& format) | 
					
						
							| 
									
										
										
										
											2014-01-24 14:46:05 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   if (!format.enable) | 
					
						
							|  |  |  |     return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   glEnableVertexAttribArray(attrib); | 
					
						
							|  |  |  |   if (format.integer) | 
					
						
							|  |  |  |     glVertexAttribIPointer(attrib, format.components, VarToGL(format.type), stride, | 
					
						
							|  |  |  |                            (u8*)nullptr + format.offset); | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |     glVertexAttribPointer(attrib, format.components, VarToGL(format.type), true, stride, | 
					
						
							|  |  |  |                           (u8*)nullptr + format.offset); | 
					
						
							| 
									
										
										
										
											2014-01-24 14:46:05 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-21 10:32:07 +01:00
										 |  |  | GLVertexFormat::GLVertexFormat(const PortableVertexDeclaration& _vtx_decl) | 
					
						
							| 
									
										
										
										
											2008-12-08 05:25:12 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   this->vtx_decl = _vtx_decl; | 
					
						
							|  |  |  |   u32 vertex_stride = _vtx_decl.stride; | 
					
						
							| 
									
										
										
										
											2008-12-08 05:25:12 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   // We will not allow vertex components causing uneven strides.
 | 
					
						
							|  |  |  |   if (vertex_stride & 3) | 
					
						
							|  |  |  |     PanicAlert("Uneven vertex stride: %i", vertex_stride); | 
					
						
							| 
									
										
										
										
											2013-10-29 01:23:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   VertexManager* const vm = static_cast<VertexManager*>(g_vertex_manager.get()); | 
					
						
							| 
									
										
										
										
											2013-10-29 01:23:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   glGenVertexArrays(1, &VAO); | 
					
						
							|  |  |  |   glBindVertexArray(VAO); | 
					
						
							| 
									
										
										
										
											2013-10-29 01:23:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   // the element buffer is bound directly to the vao, so we must it set for every vao
 | 
					
						
							|  |  |  |   glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vm->m_index_buffers); | 
					
						
							|  |  |  |   glBindBuffer(GL_ARRAY_BUFFER, vm->m_vertex_buffers); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:25:12 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   SetPointer(SHADER_POSITION_ATTRIB, vertex_stride, _vtx_decl.position); | 
					
						
							| 
									
										
										
										
											2013-10-29 01:23:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   for (int i = 0; i < 3; i++) | 
					
						
							|  |  |  |     SetPointer(SHADER_NORM0_ATTRIB + i, vertex_stride, _vtx_decl.normals[i]); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:25:12 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   for (int i = 0; i < 2; i++) | 
					
						
							|  |  |  |     SetPointer(SHADER_COLOR0_ATTRIB + i, vertex_stride, _vtx_decl.colors[i]); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:25:12 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   for (int i = 0; i < 8; i++) | 
					
						
							|  |  |  |     SetPointer(SHADER_TEXTURE0_ATTRIB + i, vertex_stride, _vtx_decl.texcoords[i]); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:25:12 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   SetPointer(SHADER_POSMTX_ATTRIB, vertex_stride, _vtx_decl.posmtx); | 
					
						
							| 
									
										
										
										
											2008-12-26 17:02:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   vm->m_last_vao = VAO; | 
					
						
							| 
									
										
										
										
											2008-12-26 17:02:46 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-21 10:32:07 +01:00
										 |  |  | GLVertexFormat::~GLVertexFormat() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   glDeleteVertexArrays(1, &VAO); | 
					
						
							| 
									
										
										
										
											2015-11-21 10:32:07 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-15 14:09:53 -04:00
										 |  |  | void GLVertexFormat::SetupVertexPointers() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2012-10-26 11:34:02 -03:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2008-12-26 17:02:46 +00:00
										 |  |  | } |