Browse Source

xbox: fix a few problems in the shader handling code; the first tutorial now

works on the Xbox.
legacy
Sam Hocevar sam 12 years ago
parent
commit
bd69c76f90
5 changed files with 44 additions and 38 deletions
  1. +17
    -26
      src/gpu/shader.cpp
  2. +2
    -0
      src/platform/xbox/xboxapp.cpp
  3. +4
    -0
      src/scene.cpp
  4. +3
    -3
      src/video.cpp
  5. +18
    -9
      test/tutorial/tut01.cpp

+ 17
- 26
src/gpu/shader.cpp View File

@@ -150,7 +150,7 @@ Shader::Shader(char const *vert, char const *frag)
data->frag_crc = Hash::Crc32(frag);
#if defined _XBOX
hr = D3DXCompileShader(frag, (UINT)strlen(frag), NULL, NULL, "main",
"vs_2_0", 0, &shader_code, &error_msg,
"ps_2_0", 0, &shader_code, &error_msg,
&data->frag_table);
if (FAILED(hr))
{
@@ -199,13 +199,8 @@ Shader::Shader(char const *vert, char const *frag)
int Shader::GetAttribLocation(char const *attr) const
{
#if defined _XBOX
D3DXHANDLE hr = data->frag_table->GetConstantByName(NULL, attr);
if (FAILED(hr))
return -1;
UINT count;
D3DXCONSTANT_DESC desc;
data->frag_table->GetConstantDesc(hr, &desc, &count);
return desc.RegisterIndex;
/* FIXME: do we have attribs? */
return 0;
#elif !defined __CELLOS_LV2__
return glGetAttribLocation(data->prog_id, attr);
#else
@@ -217,13 +212,9 @@ int Shader::GetAttribLocation(char const *attr) const
int Shader::GetUniformLocation(char const *uni) const
{
#if defined _XBOX
D3DXHANDLE hr = data->frag_table->GetConstantByName(NULL, uni);
if (FAILED(hr))
return -1;
UINT count;
D3DXCONSTANT_DESC desc;
data->frag_table->GetConstantDesc(hr, &desc, &count);
return desc.RegisterIndex;
D3DXHANDLE hr1 = data->frag_table->GetConstantByName(NULL, uni);
D3DXHANDLE hr2 = data->vert_table->GetConstantByName(NULL, uni);
return (int)(((uint32_t)hr1 << 16) | (uint32_t)hr2);
#elif !defined __CELLOS_LV2__
return glGetUniformLocation(data->prog_id, uni);
#else
@@ -239,9 +230,7 @@ int Shader::GetUniformLocation(char const *uni) const
void Shader::SetUniform(int uni, float f)
{
#if defined _XBOX
extern D3DDevice *g_d3ddevice;
vec4 tmp(f, 0.0f, 0.0f, 0.0f);
g_d3ddevice->SetPixelShaderConstantF(uni, &tmp[0], 1);
SetUniform(uni, vec4(f, 0, 0, 0));
#elif !defined __CELLOS_LV2__
glUniform1f(uni, f);
#else
@@ -252,9 +241,7 @@ void Shader::SetUniform(int uni, float f)
void Shader::SetUniform(int uni, vec2 const &v)
{
#if defined _XBOX
extern D3DDevice *g_d3ddevice;
vec4 tmp(v, 0.0f, 0.0f);
g_d3ddevice->SetPixelShaderConstantF(uni, &tmp[0], 1);
SetUniform(uni, vec4(v, 0, 0));
#elif !defined __CELLOS_LV2__
glUniform2f(uni, v.x, v.y);
#else
@@ -265,9 +252,7 @@ void Shader::SetUniform(int uni, vec2 const &v)
void Shader::SetUniform(int uni, vec3 const &v)
{
#if defined _XBOX
extern D3DDevice *g_d3ddevice;
vec4 tmp(v, 0.0f);
g_d3ddevice->SetPixelShaderConstantF(uni, &tmp[0], 1);
SetUniform(uni, vec4(v, 0));
#elif !defined __CELLOS_LV2__
glUniform3f(uni, v.x, v.y, v.z);
#else
@@ -280,7 +265,10 @@ void Shader::SetUniform(int uni, vec4 const &v)
/* FIXME: use the array versions of these functions */
#if defined _XBOX
extern D3DDevice *g_d3ddevice;
g_d3ddevice->SetPixelShaderConstantF(uni, &v[0], 1);
D3DXHANDLE hr1 = (uint32_t)uni >> 16;
D3DXHANDLE hr2 = (uint32_t)(uint16_t)uni;
g_d3ddevice->SetPixelShaderConstantF(hr1, &v[0], 1);
g_d3ddevice->SetVertexShaderConstantF(hr2, &v[0], 1);
#elif !defined __CELLOS_LV2__
glUniform4f(uni, v.x, v.y, v.z, v.w);
#else
@@ -292,7 +280,10 @@ void Shader::SetUniform(int uni, mat4 const &m)
{
#if defined _XBOX
extern D3DDevice *g_d3ddevice;
g_d3ddevice->SetPixelShaderConstantF(uni, &m[0][0], 4);
D3DXHANDLE hr1 = (uint32_t)uni >> 16;
D3DXHANDLE hr2 = (uint32_t)(uint16_t)uni;
g_d3ddevice->SetPixelShaderConstantF(hr1, &m[0][0], 4);
g_d3ddevice->SetVertexShaderConstantF(hr2, &m[0][0], 4);
#elif !defined __CELLOS_LV2__
glUniformMatrix4fv(uni, 1, GL_FALSE, &m[0][0]);
#else


+ 2
- 0
src/platform/xbox/xboxapp.cpp View File

@@ -44,6 +44,8 @@ XboxApp::XboxApp(char const *title, ivec2 res, float fps) :
data(new XboxAppData())
{
#if defined _XBOX
Ticker::Setup(fps);
Video::Setup(res);
#endif
}



+ 4
- 0
src/scene.cpp View File

@@ -143,6 +143,7 @@ void Scene::Reset()

void Scene::AddTile(TileSet *tileset, int id, vec3 pos, int o)
{
#if !defined _XBOX /* No WPOS on Xbox */
if ((data->ntiles % 1024) == 0)
data->tiles = (Tile *)realloc(data->tiles,
(data->ntiles + 1024) * sizeof(Tile));
@@ -153,10 +154,12 @@ void Scene::AddTile(TileSet *tileset, int id, vec3 pos, int o)
data->tiles[data->ntiles].pos = pos;
data->tiles[data->ntiles].o = o;
data->ntiles++;
#endif
}

void Scene::Render() // XXX: rename to Blit()
{
#if !defined _XBOX /* No WPOS on Xbox */
if (!stdshader)
{
#if !defined _XBOX && !defined __CELLOS_LV2__
@@ -483,6 +486,7 @@ void Scene::Render() // XXX: rename to Blit()
#endif
glDisable(GL_BLEND);
#endif
#endif /* _XBOX */
}

} /* namespace lol */


+ 3
- 3
src/video.cpp View File

@@ -94,9 +94,9 @@ void Video::Setup(ivec2 size)
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_ONE;

if (!VideoData::d3d_ctx->CreateDevice(0, D3DDEVTYPE_HAL, NULL,
D3DCREATE_HARDWARE_VERTEXPROCESSING,
&d3dpp, &VideoData::d3d_dev))
if (FAILED(VideoData::d3d_ctx->CreateDevice(0, D3DDEVTYPE_HAL, NULL,
D3DCREATE_HARDWARE_VERTEXPROCESSING,
&d3dpp, &VideoData::d3d_dev)))
{
Log::Error("cannot create D3D device\n");
exit(EXIT_FAILURE);


+ 18
- 9
test/tutorial/tut01.cpp View File

@@ -38,8 +38,13 @@ public:
Triangle()
{
m_vertices[0] = vec2( 0.0, 0.8);
#if defined _XBOX
m_vertices[2] = vec2(-0.8, -0.8);
m_vertices[1] = vec2( 0.8, -0.8);
#else
m_vertices[1] = vec2(-0.8, -0.8);
m_vertices[2] = vec2( 0.8, -0.8);
#endif
m_ready = false;
}

@@ -59,9 +64,10 @@ public:

"#version 120\n"
"void main(void) {"
" gl_FragColor[0] = 0.0;"
" gl_FragColor[1] = 0.0;"
" gl_FragColor[2] = 1.0;"
" gl_FragColor.r = 1.0;"
" gl_FragColor.g = 1.0;"
" gl_FragColor.b = 0.0;"
" gl_FragColor.a = 1.0;"
"}"
#else
"void main(float2 coord2d : POSITION,"
@@ -70,9 +76,10 @@ public:
"}",

"void main(out float4 out_FragColor : COLOR) {"
" out_FragColor[0] = 0.0;"
" out_FragColor[0] = 0.0;"
" out_FragColor[0] = 1.0;"
" out_FragColor.r = 1.0;"
" out_FragColor.g = 1.0;"
" out_FragColor.b = 0.0;"
" out_FragColor.a = 0.0;"
"}"
#endif
);
@@ -89,16 +96,18 @@ public:
/* Method 2: upload vertex information at each frame */
#elif defined _XBOX
extern D3DDevice *g_d3ddevice;
D3DVERTEXELEMENT9 const elements[3] =
D3DVERTEXELEMENT9 const elements[2] =
{
{ 0, 0, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
D3DDECL_END()
};
g_d3ddevice->CreateVertexDeclaration(elements, &m_vdecl);
g_d3ddevice->CreateVertexBuffer(sizeof(m_vertices), D3DUSAGE_WRITEONLY, NULL, D3DPOOL_MANAGED, &m_vbo, NULL);
if (FAILED(g_d3ddevice->CreateVertexBuffer(sizeof(m_vertices), D3DUSAGE_WRITEONLY, NULL, D3DPOOL_MANAGED, &m_vbo, NULL)))
exit(0);

vec2 *vertices;
m_vbo->Lock(0, 0, (void **)&vertices, 0);
if (FAILED(m_vbo->Lock(0, 0, (void **)&vertices, 0)))
exit(0);
memcpy(vertices, m_vertices, sizeof(m_vertices));
m_vbo->Unlock();
#else


Loading…
Cancel
Save