Changeset 1214


Ignore:
Timestamp:
Apr 11, 2012, 1:14:16 AM (8 years ago)
Author:
sam
Message:

win32: the uniform handling code was completely broken; we now properly
retrieve the constant's register index in the description table. This also
allows us to use a single uniform handle for both the vertex and pixel
shaders in a PS3 Cg program.

Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/gpu/shader.cpp

    r1212 r1214  
    202202#endif
    203203
    204 #if !defined USE_D3D9 && !defined _XBOX && !defined __CELLOS_LV2__
     204#if defined USE_D3D9 || defined _XBOX
     205    /* FIXME: this is only debug code, we don't need it. */
     206    D3DXCONSTANTTABLE_DESC desc;
     207    data->frag_table->GetDesc(&desc);
     208    for (int i = 0; i < desc.Constants; i++)
     209    {
     210        D3DXCONSTANT_DESC cdesc;
     211        UINT count = 1;
     212        D3DXHANDLE h = data->frag_table->GetConstant(NULL, i);
     213        data->frag_table->GetConstantDesc(h, &cdesc, &count);
     214    }
     215    data->vert_table->GetDesc(&desc);
     216    for (int i = 0; i < desc.Constants; i++)
     217    {
     218        D3DXCONSTANT_DESC cdesc;
     219        UINT count = 1;
     220        D3DXHANDLE h = data->vert_table->GetConstant(NULL, i);
     221        data->frag_table->GetConstantDesc(h, &cdesc, &count);
     222    }
     223#elif !defined __CELLOS_LV2__
    205224    /* Create program */
    206225    data->prog_id = glCreateProgram();
     
    216235{
    217236#if defined USE_D3D9 || defined _XBOX
    218     /* FIXME: do we have attribs? */
     237    /* FIXME: do we have attribs on these platforms? */
    219238    return 0;
    220239#elif !defined __CELLOS_LV2__
     
    226245}
    227246
    228 int Shader::GetUniformLocation(char const *uni) const
    229 {
    230 #if defined USE_D3D9 || defined _XBOX
    231     UINT hr1 = (uintptr_t)data->frag_table->GetConstantByName(NULL, uni);
    232     UINT hr2 = (uintptr_t)data->vert_table->GetConstantByName(NULL, uni);
    233     return (int)(((uint32_t)hr1 << 16) | (uint32_t)hr2);
    234 #elif !defined __CELLOS_LV2__
    235     return glGetUniformLocation(data->prog_id, uni);
    236 #else
    237     /* FIXME: need to differentiate between vertex and fragment program,
    238      * and replace the ugly cast. */
    239     CGparameter tmp = cgGetNamedParameter(data->vert_id, uni);
    240     if (tmp == NULL)
    241         tmp = cgGetNamedParameter(data->frag_id, uni);
    242     return (int)(intptr_t)tmp;
    243 #endif
    244 }
    245 
    246 void Shader::SetUniform(int uni, float f)
     247ShaderUniform Shader::GetUniformLocation(char const *uni) const
     248{
     249    ShaderUniform ret;
     250#if defined USE_D3D9 || defined _XBOX
     251    /* Global variables are prefixed with "$" */
     252    char tmpname[128];
     253    sprintf(tmpname, "$%s", uni);
     254    D3DXCONSTANT_DESC cdesc;
     255    D3DXHANDLE hr;
     256    UINT count;
     257
     258    count = 0;
     259    hr = data->frag_table->GetConstantByName(NULL, tmpname);
     260    if (hr)
     261        data->frag_table->GetConstantDesc(hr, &cdesc, &count);
     262    if (count)
     263    {
     264        ret.frag = cdesc.RegisterIndex;
     265        ret.flags |= 1;
     266    }
     267
     268    count = 0;
     269    hr = data->vert_table->GetConstantByName(NULL, tmpname);
     270    if (hr)
     271        data->vert_table->GetConstantDesc(hr, &cdesc, &count);
     272    if (count)
     273    {
     274        ret.vert = cdesc.RegisterIndex;
     275        ret.flags |= 2;
     276    }
     277#elif !defined __CELLOS_LV2__
     278    ret.frag = (uintptr_t)glGetUniformLocation(data->prog_id, uni);
     279    ret.vert = 0;
     280#else
     281    ret.frag = (uintptr_t)cgGetNamedParameter(data->frag_id, uni);
     282    ret.vert = (uintptr_t)cgGetNamedParameter(data->vert_id, uni);
     283#endif
     284    return ret;
     285}
     286
     287void Shader::SetUniform(ShaderUniform const &uni, float f)
    247288{
    248289#if defined USE_D3D9 || defined _XBOX
    249290    SetUniform(uni, vec4(f, 0, 0, 0));
    250291#elif !defined __CELLOS_LV2__
    251     glUniform1f(uni, f);
    252 #else
    253     cgGLSetParameter1f((CGparameter)(intptr_t)uni, f);
    254 #endif
    255 }
    256 
    257 void Shader::SetUniform(int uni, vec2 const &v)
     292    glUniform1f(uni.frag, f);
     293#else
     294    if (uni.frag)
     295        cgGLSetParameter1f((CGparameter)uni.frag, f);
     296    if (uni.vert)
     297        cgGLSetParameter1f((CGparameter)uni.vert, f);
     298#endif
     299}
     300
     301void Shader::SetUniform(ShaderUniform const &uni, vec2 const &v)
    258302{
    259303#if defined USE_D3D9 || defined _XBOX
    260304    SetUniform(uni, vec4(v, 0, 0));
    261305#elif !defined __CELLOS_LV2__
    262     glUniform2f(uni, v.x, v.y);
    263 #else
    264     cgGLSetParameter2f((CGparameter)(intptr_t)uni, v.x, v.y);
    265 #endif
    266 }
    267 
    268 void Shader::SetUniform(int uni, vec3 const &v)
     306    glUniform2f(uni.frag, v.x, v.y);
     307#else
     308    if (uni.frag)
     309        cgGLSetParameter2f((CGparameter)uni.frag, v.x, v.y);
     310    if (uni.vert)
     311        cgGLSetParameter2f((CGparameter)uni.vert, v.x, v.y);
     312#endif
     313}
     314
     315void Shader::SetUniform(ShaderUniform const &uni, vec3 const &v)
    269316{
    270317#if defined USE_D3D9 || defined _XBOX
    271318    SetUniform(uni, vec4(v, 0));
    272319#elif !defined __CELLOS_LV2__
    273     glUniform3f(uni, v.x, v.y, v.z);
    274 #else
    275     cgGLSetParameter3f((CGparameter)(intptr_t)uni, v.x, v.y, v.z);
    276 #endif
    277 }
    278 
    279 void Shader::SetUniform(int uni, vec4 const &v)
     320    glUniform3f(uni.frag, v.x, v.y, v.z);
     321#else
     322    if (uni.frag)
     323        cgGLSetParameter3f((CGparameter)uni.frag, v.x, v.y, v.z);
     324    if (uni.vert)
     325        cgGLSetParameter3f((CGparameter)uni.vert, v.x, v.y, v.z);
     326#endif
     327}
     328
     329void Shader::SetUniform(ShaderUniform const &uni, vec4 const &v)
    280330{
    281331    /* FIXME: use the array versions of these functions */
    282332#if defined USE_D3D9 || defined _XBOX
    283     UINT hr1 = (uint32_t)uni >> 16;
    284     UINT hr2 = (uint32_t)(uint16_t)uni;
    285     g_d3ddevice->SetPixelShaderConstantF(hr1, &v[0], 1);
    286     g_d3ddevice->SetVertexShaderConstantF(hr2, &v[0], 1);
     333    if (uni.flags & 1)
     334        g_d3ddevice->SetPixelShaderConstantF((UINT)uni.frag, &v[0], 1);
     335    if (uni.flags & 2)
     336        g_d3ddevice->SetVertexShaderConstantF((UINT)uni.vert, &v[0], 1);
    287337#elif !defined __CELLOS_LV2__
    288338    glUniform4f(uni, v.x, v.y, v.z, v.w);
    289339#else
    290     cgGLSetParameter4f((CGparameter)(intptr_t)uni, v.x, v.y, v.z, v.w);
    291 #endif
    292 }
    293 
    294 void Shader::SetUniform(int uni, mat4 const &m)
    295 {
    296 #if defined USE_D3D9 || defined _XBOX
    297     UINT hr1 = (uint32_t)uni >> 16;
    298     UINT hr2 = (uint32_t)(uint16_t)uni;
    299     g_d3ddevice->SetPixelShaderConstantF(hr1, &m[0][0], 4);
    300     g_d3ddevice->SetVertexShaderConstantF(hr2, &m[0][0], 4);
     340    cgGLSetParameter4f((CGparameter)uni, v.x, v.y, v.z, v.w);
     341#endif
     342}
     343
     344void Shader::SetUniform(ShaderUniform const &uni, mat4 const &m)
     345{
     346#if defined USE_D3D9 || defined _XBOX
     347    if (uni.flags & 1)
     348        g_d3ddevice->SetPixelShaderConstantF((UINT)uni.frag, &m[0][0], 4);
     349    if (uni.flags & 2)
     350        g_d3ddevice->SetVertexShaderConstantF((UINT)uni.vert, &m[0][0], 4);
    301351#elif !defined __CELLOS_LV2__
    302352    glUniformMatrix4fv(uni, 1, GL_FALSE, &m[0][0]);
    303353#else
    304     cgGLSetMatrixParameterfc((CGparameter)(intptr_t)uni, &m[0][0]);
     354    cgGLSetMatrixParameterfc((CGparameter)uni, &m[0][0]);
    305355#endif
    306356}
  • trunk/src/gpu/shader.h

    r1180 r1214  
    1717#define __LOL_SHADER_H__
    1818
     19#include <stdint.h>
     20
    1921namespace lol
    2022{
     23
     24struct ShaderUniform
     25{
     26    friend class Shader;
     27
     28public:
     29    ShaderUniform() : flags(0) {}
     30
     31private:
     32    uintptr_t frag, vert;
     33    /* FIXME: do we really need this to indicate which locations are valid? */
     34    uint32_t flags;
     35};
    2136
    2237class ShaderData;
     
    3045    int GetAttribLocation(char const *attr) const;
    3146
    32     int GetUniformLocation(char const *uni) const;
    33     void SetUniform(int uni, float f);
    34     void SetUniform(int uni, vec2 const &v);
    35     void SetUniform(int uni, vec3 const &v);
    36     void SetUniform(int uni, vec4 const &v);
    37     void SetUniform(int uni, mat4 const &m);
     47    ShaderUniform GetUniformLocation(char const *uni) const;
     48    void SetUniform(ShaderUniform const &uni, float f);
     49    void SetUniform(ShaderUniform const &uni, vec2 const &v);
     50    void SetUniform(ShaderUniform const &uni, vec3 const &v);
     51    void SetUniform(ShaderUniform const &uni, vec4 const &v);
     52    void SetUniform(ShaderUniform const &uni, mat4 const &m);
    3853
    3954    void Bind() const;
  • trunk/test/tutorial/tut01.cpp

    r1208 r1214  
    138138
    139139#if defined _XBOX || defined USE_D3D9
    140         g_d3ddevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 3);
     140        g_d3ddevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);
    141141#else
    142142        glDrawArrays(GL_TRIANGLES, 0, 3);
  • trunk/test/tutorial/tut02.cpp

    r1208 r1214  
    257257#endif
    258258#endif
    259     int m_mvp;
     259    ShaderUniform m_mvp;
    260260    bool m_ready;
    261261};
  • trunk/test/tutorial/tut03.cpp

    r1213 r1214  
    573573                "    float t2 = step(dd.x, dd.y);"
    574574                     /* ret.x <-- max(ret.x, ret.y); */
    575                      /* ret.y <-- max(ret.z, ret.yw; */
     575                     /* ret.y <-- max(ret.z, ret.w); */
    576576                "    ret.xy = mix(ret.xz, ret.yw, t2);"
    577577                "\n#else\n"
     
    609609                "          uniform float4 u_TexelSize,"
    610610                "          uniform float4 u_ScreenSize,"
    611                 "          out float4 out_Position : POSITION,"
    612                 "          out float4 v_CenterX,"
    613                 "          out float4 v_CenterY,"
    614                 "          out float4 v_IndexX,"
    615                 "          out float4 v_IndexY)"
     611                "          out float4 out_Position : POSITION0,"
     612                "          out float4 v_CenterX : TEXCOORD0,"
     613                "          out float4 v_CenterY : TEXCOORD1,"
     614                "          out float4 v_IndexX : TEXCOORD2,"
     615                "          out float4 v_IndexY : TEXCOORD3)"
    616616                "{"
    617617                "    out_Position = a_Vertex;"
     
    637637                "}",
    638638
    639                 "void main(in float4 v_CenterX,"
    640                 "          in float4 v_CenterY,"
    641                 "          in float4 v_IndexX,"
    642                 "          in float4 v_IndexY,"
    643                 "          uniform float4 u_TexelSize2,"
     639                "void main(in float4 v_CenterX : TEXCOORD0,"
     640                "          in float4 v_CenterY : TEXCOORD1,"
     641                "          in float4 v_IndexX : TEXCOORD2,"
     642                "          in float4 v_IndexY : TEXCOORD3,"
     643                "          uniform float4 u_TexelSize,"
    644644                "          uniform sampler2D u_Texture,"
    645645                "          out float4 out_FragColor : COLOR)"
     
    647647                "    float4 v05 = float4(0.5, 0.5, 0.5, 0.5);"
    648648                "    float4 rx, ry, t0, dx, dy, dd;"
    649                 "    rx = u_TexelSize2.x + u_TexelSize2.z * floor(v_IndexX);"
    650                 "    ry = u_TexelSize2.y + u_TexelSize2.w * floor(v_IndexY);"
     649                "    rx = u_TexelSize.x + u_TexelSize.z * floor(v_IndexX);"
     650                "    ry = u_TexelSize.y + u_TexelSize.w * floor(v_IndexY);"
    651651                "    t0 = step(abs(rx - v05), v05) * step(abs(ry - v05), v05);"
    652652                "    dx = rx - v_CenterX;"
     
    669669#endif
    670670            m_texeluni = m_shader->GetUniformLocation("u_TexelSize");
    671 #if defined __CELLOS_LV2__
    672             m_texeluni2 = m_shader->GetUniformLocation("u_TexelSize2");
    673 #endif
    674671            m_screenuni = m_shader->GetUniformLocation("u_ScreenSize");
    675672            m_zoomuni = m_shader->GetUniformLocation("u_ZoomSettings");
     
    692689            {
    693690                { 0, 0, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
    694                 { 0, 0, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
     691                { 1, 0, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
    695692                D3DDECL_END()
    696693            };
     
    699696            if (FAILED(g_d3ddevice->CreateVertexBuffer(sizeof(vertices), D3DUSAGE_WRITEONLY, NULL, D3DPOOL_MANAGED, &m_vbo, NULL)))
    700697                exit(0);
    701 
    702698            vec2 *tmp1;
    703699            if (FAILED(m_vbo->Lock(0, 0, (void **)&tmp1, 0)))
     
    708704            if (FAILED(g_d3ddevice->CreateVertexBuffer(sizeof(texcoords), D3DUSAGE_WRITEONLY, NULL, D3DPOOL_MANAGED, &m_tbo, NULL)))
    709705                exit(0);
    710 
    711706            vec2 *tmp2;
    712707            if (FAILED(m_tbo->Lock(0, 0, (void **)&tmp2, 0)))
     
    743738            {
    744739                u8vec4 *line = (u8vec4 *)rect.pBits + j * rect.Pitch / 4;
    745                 for (int i = 0; i < m_size.x / 2; j++)
     740                for (int i = 0; i < m_size.x / 2; i++)
    746741                    line[i] = m_pixels[m_size.x / 2 * j + i];
    747742            }
     
    763758        m_shader->Bind();
    764759        m_shader->SetUniform(m_texeluni, m_texel_settings);
    765 #if defined __CELLOS_LV2__
    766         m_shader->SetUniform(m_texeluni2, m_texel_settings);
    767 #endif
    768760        m_shader->SetUniform(m_screenuni, m_screen_settings);
    769761        m_shader->SetUniform(m_zoomuni, m_zoom_settings);
    770762#if defined _XBOX || defined USE_D3D9
    771         g_d3ddevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW);
     763        g_d3ddevice->SetTexture(0, m_tex);
     764        //g_d3ddevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW);
     765        g_d3ddevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
    772766        g_d3ddevice->SetVertexDeclaration(m_vdecl);
    773767        g_d3ddevice->SetStreamSource(0, m_vbo, 0, sizeof(*vertices));
     
    793787
    794788#if defined _XBOX || defined USE_D3D9
    795         g_d3ddevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 6);
     789        /* FIXME: what the fuck? Why does "2" not work here instead of 3? */
     790        g_d3ddevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 3);
    796791#else
    797792        glDrawArrays(GL_TRIANGLES, 0, 6);
     
    840835#   endif
    841836#endif
    842     int m_vertexattrib, m_texattrib, m_texeluni, m_screenuni, m_zoomuni;
    843 #if defined __CELLOS_LV2__
    844     int m_texeluni2;
    845 #endif
     837    int m_vertexattrib, m_texattrib;
     838    ShaderUniform m_texeluni, m_screenuni, m_zoomuni;
    846839    int m_frame, m_slices, m_dirty[4];
    847840    bool m_ready, m_drag;
Note: See TracChangeset for help on using the changeset viewer.