use attrib pointers in nativeVertexFormat

This commit is contained in:
degasus 2013-01-14 22:59:08 +01:00
parent 2f78986e2c
commit ff889c0e65
7 changed files with 51 additions and 41 deletions

View File

@ -229,24 +229,25 @@ const char *GenerateVertexShaderCode(u32 components, API_TYPE ApiType)
if(ApiType == API_OPENGL) if(ApiType == API_OPENGL)
{ {
if (components & VB_HAS_NRM0) WRITE(p, "ATTRIN float4 rawpos; // ATTR%d,\n", SHADER_POSITION_ATTRIB);
WRITE(p, " float3 rawnorm0 = gl_Normal; // NORMAL0,\n");
if (components & VB_HAS_POSMTXIDX) if (components & VB_HAS_POSMTXIDX)
WRITE(p, "ATTRIN float fposmtx; // ATTR%d,\n", SHADER_POSMTX_ATTRIB); WRITE(p, "ATTRIN float fposmtx; // ATTR%d,\n", SHADER_POSMTX_ATTRIB);
if (components & VB_HAS_NRM0)
WRITE(p, "ATTRIN float3 rawnorm0; // ATTR%d,\n", SHADER_NORM0_ATTRIB);
if (components & VB_HAS_NRM1) if (components & VB_HAS_NRM1)
WRITE(p, "ATTRIN float3 rawnorm1; // ATTR%d,\n", SHADER_NORM1_ATTRIB); WRITE(p, "ATTRIN float3 rawnorm1; // ATTR%d,\n", SHADER_NORM1_ATTRIB);
if (components & VB_HAS_NRM2) if (components & VB_HAS_NRM2)
WRITE(p, "ATTRIN float3 rawnorm2; // ATTR%d,\n", SHADER_NORM2_ATTRIB); WRITE(p, "ATTRIN float3 rawnorm2; // ATTR%d,\n", SHADER_NORM2_ATTRIB);
if (components & VB_HAS_COL0) if (components & VB_HAS_COL0)
WRITE(p, " float4 color0 = gl_Color; // COLOR0,\n"); WRITE(p, "ATTRIN float4 color0; // ATTR%d,\n", SHADER_COLOR0_ATTRIB);
if (components & VB_HAS_COL1) if (components & VB_HAS_COL1)
WRITE(p, " float4 color1 = gl_SecondaryColor; // COLOR1,\n"); WRITE(p, "ATTRIN float4 color1; // ATTR%d,\n", SHADER_COLOR1_ATTRIB);
for (int i = 0; i < 8; ++i) { for (int i = 0; i < 8; ++i) {
u32 hastexmtx = (components & (VB_HAS_TEXMTXIDX0<<i)); u32 hastexmtx = (components & (VB_HAS_TEXMTXIDX0<<i));
if ((components & (VB_HAS_UV0<<i)) || hastexmtx) if ((components & (VB_HAS_UV0<<i)) || hastexmtx)
WRITE(p, " float%d tex%d = gl_MultiTexCoord%d.xy%s; // TEXCOORD%d,\n", hastexmtx ? 3 : 2, i, i, hastexmtx ? "z" : "", i); WRITE(p, "ATTRIN float%d tex%d; // ATTR%d,\n", hastexmtx ? 3 : 2, i, SHADER_TEXTURE0_ATTRIB + i);
} }
// Let's set up attributes // Let's set up attributes
@ -272,7 +273,6 @@ const char *GenerateVertexShaderCode(u32 components, API_TYPE ApiType)
WRITE(p, "VARYOUT float%d uv%d_2;\n", i < 4 ? 4 : 3 , i); WRITE(p, "VARYOUT float%d uv%d_2;\n", i < 4 ? 4 : 3 , i);
} }
} }
WRITE(p, " float4 rawpos = gl_Vertex;\n");
WRITE(p, "VARYOUT float4 colors_02;\n"); WRITE(p, "VARYOUT float4 colors_02;\n");
WRITE(p, "VARYOUT float4 colors_12;\n"); WRITE(p, "VARYOUT float4 colors_12;\n");

View File

@ -22,12 +22,23 @@
#include "VideoCommon.h" #include "VideoCommon.h"
// TODO should be reordered // TODO should be reordered
#define SHADER_POSITION_ATTRIB 0
#define SHADER_POSMTX_ATTRIB 1 #define SHADER_POSMTX_ATTRIB 1
#define SHADER_POSITION_ATTRIB 2 #define SHADER_NORM0_ATTRIB 2
#define SHADER_TEXTURE0_ATTRIB 3 #define SHADER_NORM1_ATTRIB 3
#define SHADER_COLOR0_ATTRIB 4 #define SHADER_NORM2_ATTRIB 4
#define SHADER_NORM1_ATTRIB 6 #define SHADER_COLOR0_ATTRIB 5
#define SHADER_NORM2_ATTRIB 7 #define SHADER_COLOR1_ATTRIB 6
#define SHADER_TEXTURE0_ATTRIB 8
#define SHADER_TEXTURE1_ATTRIB 9
#define SHADER_TEXTURE2_ATTRIB 10
#define SHADER_TEXTURE3_ATTRIB 11
#define SHADER_TEXTURE4_ATTRIB 12
#define SHADER_TEXTURE5_ATTRIB 13
#define SHADER_TEXTURE6_ATTRIB 14
#define SHADER_TEXTURE7_ATTRIB 15
// shader variables // shader variables

View File

@ -73,39 +73,28 @@ void GLVertexFormat::Initialize(const PortableVertexDeclaration &_vtx_decl)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vm->m_index_buffers); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vm->m_index_buffers);
glBindBuffer(GL_ARRAY_BUFFER, vm->m_vertex_buffers); glBindBuffer(GL_ARRAY_BUFFER, vm->m_vertex_buffers);
glEnableClientState(GL_VERTEX_ARRAY); glEnableVertexAttribArray(SHADER_POSITION_ATTRIB);
glVertexPointer(3, GL_FLOAT, vtx_decl.stride, (u8*)NULL); glVertexAttribPointer(SHADER_POSITION_ATTRIB, 3, GL_FLOAT, GL_FALSE, vtx_decl.stride, (u8*)NULL);
if (vtx_decl.num_normals >= 1) { for (int i = 0; i < 3; i++) {
glEnableClientState(GL_NORMAL_ARRAY); if (vtx_decl.num_normals > i) {
glNormalPointer(VarToGL(vtx_decl.normal_gl_type), vtx_decl.stride, (u8*)NULL + vtx_decl.normal_offset[0]); glEnableVertexAttribArray(SHADER_NORM0_ATTRIB+i);
if (vtx_decl.num_normals == 3) { glVertexAttribPointer(SHADER_NORM0_ATTRIB+i, vtx_decl.normal_gl_size, VarToGL(vtx_decl.normal_gl_type), GL_TRUE, vtx_decl.stride, (u8*)NULL + vtx_decl.normal_offset[i]);
glEnableVertexAttribArray(SHADER_NORM1_ATTRIB);
glEnableVertexAttribArray(SHADER_NORM2_ATTRIB);
glVertexAttribPointer(SHADER_NORM1_ATTRIB, vtx_decl.normal_gl_size, VarToGL(vtx_decl.normal_gl_type), GL_TRUE, vtx_decl.stride, (u8*)NULL + vtx_decl.normal_offset[1]);
glVertexAttribPointer(SHADER_NORM2_ATTRIB, vtx_decl.normal_gl_size, VarToGL(vtx_decl.normal_gl_type), GL_TRUE, vtx_decl.stride, (u8*)NULL + vtx_decl.normal_offset[2]);
} }
} }
for (int i = 0; i < 2; i++) { for (int i = 0; i < 2; i++) {
if (vtx_decl.color_offset[i] != -1) { if (vtx_decl.color_offset[i] != -1) {
if (i == 0) { glEnableVertexAttribArray(SHADER_COLOR0_ATTRIB+i);
glEnableClientState(GL_COLOR_ARRAY); glVertexAttribPointer(SHADER_COLOR0_ATTRIB+i, 4, GL_UNSIGNED_BYTE, GL_TRUE, vtx_decl.stride, (u8*)NULL + vtx_decl.color_offset[i]);
glColorPointer(4, GL_UNSIGNED_BYTE, vtx_decl.stride, (u8*)NULL + vtx_decl.color_offset[i]);
} else {
glEnableClientState(GL_SECONDARY_COLOR_ARRAY);
glSecondaryColorPointer(4, GL_UNSIGNED_BYTE, vtx_decl.stride, (u8*)NULL + vtx_decl.color_offset[i]);
}
} }
} }
for (int i = 0; i < 8; i++) { for (int i = 0; i < 8; i++) {
if (vtx_decl.texcoord_offset[i] != -1) { if (vtx_decl.texcoord_offset[i] != -1) {
int id = GL_TEXTURE0 + i; glEnableVertexAttribArray(SHADER_TEXTURE0_ATTRIB+i);
glClientActiveTexture(id); glVertexAttribPointer(SHADER_TEXTURE0_ATTRIB+i, vtx_decl.texcoord_size[i], VarToGL(vtx_decl.texcoord_gl_type[i]),
glEnableClientState(GL_TEXTURE_COORD_ARRAY); GL_FALSE, vtx_decl.stride, (u8*)NULL + vtx_decl.texcoord_offset[i]);
glTexCoordPointer(vtx_decl.texcoord_size[i], VarToGL(vtx_decl.texcoord_gl_type[i]),
vtx_decl.stride, (u8*)NULL + vtx_decl.texcoord_offset[i]);
} }
} }

View File

@ -111,11 +111,21 @@ void ProgramShaderCache::SetProgramBindings ( ProgramShaderCache::PCacheEntry& e
// Need to set some attribute locations // Need to set some attribute locations
glBindAttribLocation(entry.prog_id, SHADER_POSITION_ATTRIB, "rawpos"); glBindAttribLocation(entry.prog_id, SHADER_POSITION_ATTRIB, "rawpos");
glBindAttribLocation(entry.prog_id, SHADER_POSMTX_ATTRIB, "fposmtx"); glBindAttribLocation(entry.prog_id, SHADER_POSMTX_ATTRIB, "fposmtx");
glBindAttribLocation(entry.prog_id, SHADER_TEXTURE0_ATTRIB, "texture0");
glBindAttribLocation(entry.prog_id, SHADER_COLOR0_ATTRIB, "color0"); glBindAttribLocation(entry.prog_id, SHADER_COLOR0_ATTRIB, "color0");
glBindAttribLocation(entry.prog_id, SHADER_COLOR1_ATTRIB, "color1");
glBindAttribLocation(entry.prog_id, SHADER_NORM0_ATTRIB, "rawnorm0");
glBindAttribLocation(entry.prog_id, SHADER_NORM1_ATTRIB, "rawnorm1"); glBindAttribLocation(entry.prog_id, SHADER_NORM1_ATTRIB, "rawnorm1");
glBindAttribLocation(entry.prog_id, SHADER_NORM2_ATTRIB, "rawnorm2"); glBindAttribLocation(entry.prog_id, SHADER_NORM2_ATTRIB, "rawnorm2");
for(int i=0; i<8; i++) {
char attrib_name[8];
snprintf(attrib_name, 8, "tex%d", i);
glBindAttribLocation(entry.prog_id, SHADER_TEXTURE0_ATTRIB+i, attrib_name);
}
} }

View File

@ -132,11 +132,11 @@ static const char *s_vertexShaderSrc =
"#version 130\n" "#version 130\n"
"uniform vec2 charSize;\n" "uniform vec2 charSize;\n"
"in vec2 rawpos;\n" "in vec2 rawpos;\n"
"in vec2 texture0;\n" "in vec2 tex0;\n"
"out vec2 uv0;\n" "out vec2 uv0;\n"
"void main(void) {\n" "void main(void) {\n"
" gl_Position = vec4(rawpos,0,1);\n" " gl_Position = vec4(rawpos,0,1);\n"
" uv0 = texture0 * charSize;\n" " uv0 = tex0 * charSize;\n"
"}\n"; "}\n";
static const char *s_fragmentShaderSrc = static const char *s_fragmentShaderSrc =

View File

@ -528,11 +528,11 @@ TextureCache::TextureCache()
const char *VProgram = const char *VProgram =
"#version 130\n" "#version 130\n"
"in vec2 rawpos;\n" "in vec2 rawpos;\n"
"in vec2 texture0;\n" "in vec2 tex0;\n"
"out vec2 uv0;\n" "out vec2 uv0;\n"
"void main()\n" "void main()\n"
"{\n" "{\n"
" uv0 = texture0;\n" " uv0 = tex0;\n"
" gl_Position = vec4(rawpos,0,1);\n" " gl_Position = vec4(rawpos,0,1);\n"
"}\n"; "}\n";
if (!VertexShaderCache::CompileVertexShader(s_vProgram, VProgram)) if (!VertexShaderCache::CompileVertexShader(s_vProgram, VProgram))

View File

@ -115,11 +115,11 @@ void CreatePrograms()
const char *VProgram = const char *VProgram =
"#version 130\n" "#version 130\n"
"in vec2 rawpos;\n" "in vec2 rawpos;\n"
"in vec2 texture0;\n" "in vec2 tex0;\n"
"out vec2 uv0;\n" "out vec2 uv0;\n"
"void main()\n" "void main()\n"
"{\n" "{\n"
" uv0 = texture0;\n" " uv0 = tex0;\n"
" gl_Position = vec4(rawpos,0,1);\n" " gl_Position = vec4(rawpos,0,1);\n"
"}\n"; "}\n";
if (!VertexShaderCache::CompileVertexShader(s_vProgram, VProgram)) if (!VertexShaderCache::CompileVertexShader(s_vProgram, VProgram))