🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Direct3D9 Rendering Problem

Started by
5 comments, last by 100101 4 years, 6 months ago

Good evening,

honestly I don't know what is going wrong. I'm trying to render some vertices but the result is looking something very special. The problem is the rendering right after post-vertex shader processing is fucked up. Everyting else is looking fine.


Yes I know the order of my vertices are not correct in this runfile, but anyways the viewport result is fucked up.

I've uploaded the PIX runfile to mega.


Regards


Advertisement

I obviously forgot some code.. First of all I'm using this vertex structure with a correct vertex declaration. CreateVertexDeclaration returns S_OK.

struct vertex
{
    float x, y, z, w;
    float r, g, b, a;
    float u, v;
};


static D3DVERTEXELEMENT9 elements[] =
{
    { 0, 0,                 D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
    { 0, sizeof(float) * 4, D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR,    0 },
    { 0, sizeof(float) * 8, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
    D3DDECL_END()
};

On each new frame, I call:

_device->BeginScene();

math::matrix4x4 identity;
identity.identity();

_device->SetTransform( D3DTS_WORLD, reinterpret_cast<D3DMATRIX*>( &identity ) );
_device->SetTransform( D3DTS_VIEW, reinterpret_cast<D3DMATRIX*>( &identity ) );
_device->SetTransform( D3DTS_PROJECTION, reinterpret_cast<D3DMATRIX*>( &identity ) );
_device->SetRenderState( D3DRS_ZENABLE, FALSE );

_device->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE );
_device->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_SRCALPHA );
_device->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA );
_device->SetRenderState( D3DRS_ALPHATESTENABLE, TRUE );
_device->SetRenderState( D3DRS_ALPHAREF, 0x08 );
_device->SetRenderState( D3DRS_ALPHAFUNC, D3DCMP_GREATEREQUAL );
_device->SetRenderState( D3DRS_LIGHTING, FALSE );

_device->SetRenderState( D3DRS_FILLMODE, D3DFILL_SOLID );
_device->SetRenderState( D3DRS_CULLMODE, D3DCULL_CCW );
_device->SetRenderState( D3DRS_STENCILENABLE, FALSE );
_device->SetRenderState( D3DRS_CLIPPING, TRUE );
_device->SetRenderState( D3DRS_CLIPPLANEENABLE, FALSE );
_device->SetRenderState( D3DRS_VERTEXBLEND, D3DVBF_DISABLE );
_device->SetRenderState( D3DRS_INDEXEDVERTEXBLENDENABLE, FALSE );
_device->SetRenderState( D3DRS_FOGENABLE, FALSE );
_device->SetRenderState( D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_RED | D3DCOLORWRITEENABLE_GREEN | D3DCOLORWRITEENABLE_BLUE | D3DCOLORWRITEENABLE_ALPHA );
_device->SetRenderState( D3DRS_MULTISAMPLEANTIALIAS, FALSE );
_device->SetRenderState( D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF );
_device->SetRenderState( D3DRS_ANTIALIASEDLINEENABLE, FALSE );

_device->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE );
_device->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
_device->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
_device->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_MODULATE );
_device->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
_device->SetTextureStageState( 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );
_device->SetTextureStageState( 0, D3DTSS_TEXCOORDINDEX, 0 );
_device->SetTextureStageState( 0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_DISABLE );
_device->SetTextureStageState( 1, D3DTSS_COLOROP, D3DTOP_DISABLE );
_device->SetTextureStageState( 1, D3DTSS_ALPHAOP, D3DTOP_DISABLE );
_device->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_POINT );
_device->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_POINT );
_device->SetSamplerState( 0, D3DSAMP_MIPFILTER, D3DTEXF_NONE );

_device->SetVertexDeclaration( _decl );


And finally execute the command list before calling EndScene() and Present().

void execute_commands( 
    commandlist* commands ) override
{
    auto cl = dynamic_cast<dx9_commandlist*>( commands );
    auto pl = cl->pipeline();

    _device->SetVertexShader( pl->vertex_shader() );
    _device->SetPixelShader( pl->pixel_shader() );
    auto pt = pl->type();

    auto begin = cl->begin();
    auto end = cl->end();

    while( begin.base != end.base ) {
        switch( begin.base->id ) {
        case detail::cmdid::bind_viewports:
            _device->SetViewport(&begin.bind_viewport->viewport);
            begin.base = &begin.bind_viewport->next;
            break;
        case detail::cmdid::bind_render_targets:
            _device->SetRenderTarget( begin.bind_render_targets->slot, begin.bind_render_targets->rts );
            _device->SetDepthStencilSurface( begin.bind_render_targets->dss );
            begin.base = &begin.bind_render_targets->next;
            break;
        // and some more commands but that's not the point I guess
        case detail::cmdid::draw:
            _device->DrawPrimitive( pt, begin.draw->vertex_offset, begin.draw->vertex_count );
            begin.base = &begin.draw->next;
            break;
        case detail::cmdid::draw_indexed:
            _device->DrawIndexedPrimitive( 
                pt,
                begin.draw_indexed->vertex_offset, 
                0, 
                begin.draw_indexed->vertex_count, 
                begin.draw_indexed->index_offset, 
                begin.draw_indexed->index_count
            );
            begin.base = &begin.draw_indexed->next;
            break;
        }
    }
}

Regards

Okay, that was a problem inside my projection matrix. I nulled out row(3, 0), row(3, 1) and row(3, 2). But the viewport is still not correct.

PIX runfile at mega

The vertices should be renedered at 5|5 but they aren't obv.

I found this article about constructing an ortographic matrix and my current implementation looks like the code posted on this page.

matrix_t& orthographic_matrix_lh(
    const float w,
    const float h,
    const float near_z,
    const float far_z )
{
    identity();
    // https://community.khronos.org/t/constructing-an-orthographic-matrix-for-2d-drawing/62270/8
    at( 0, 0 ) = 2.f / w;
    at( 0, 3 ) = -1.f;
    at( 1, 1 ) = -2.f / h;
    at( 2, 2 ) = 2.f / ( far_z - near_z );
    at( 3, 2 ) = ( near_z + far_z ) / ( near_z - far_z );        


    return *this;
}

The result on the X coord is correct, y isn't.

Link to the PIX runfile can be found here

When using D3DXMatrixOrthoLH, the pre- and post-vertex shader are looking correct I guess.


Result in viewport is still fucked up

Okay, I'm pretty sure that my matrix is correct. I'm using the exact interfaces in directx 11 (obv. with correct dx11 implementations), shader ported to 4.0 and it's working. Really want to know what the issue could be for that.

This topic is closed to new replies.

Advertisement