• Content count

  • Joined

  • Last visited

Community Reputation

137 Neutral

About Joyal

  • Rank
  1. D3D11 DrawIndexed confusion

    You don't need to change the vertex buffer, you can use one quad , to draw all your quads , the only thing you need is update the matrix transformation to update their position   i had a issue not related with this but i posted some of my code, how i did the batch
  2. After i read all your answer , it seems to me that the issue is because my "Game" is doing anything, apart of just showing some sprite, but not logic or anything, and because of that, the loop is being process very fast , so when the loop has more logic, this CPU usage should go down (ironically),  right? 
  3.   how to avoid that ?
  4.   i have an FPS counter, is around 2000 cicles per second
  5. Hello,   Im working making a 2D game from scracth using Directx 11 , Windows 8.1 , and Visual Studios 2012   I create a simple window and show some sprites, but i realized that my "Game" is using a lot of CPU Usage. After debuggin i spotted that PeekMessage was the one causing this, i even try to remove all the code and just leave a Blank Window and still using a lot of CPU without anything, it uses between 15-30 % of CPU , this is my game loop         MSG msg;         ZeroMemory( &msg, sizeof(msg) );         long lastTime = GetTickCount();         while( msg.message!=WM_QUIT)         {             if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )             {                 TranslateMessage( &msg );                 DispatchMessage( &msg );             }else{                 //Gametime Calc                 long deltaTime = GetTickCount() - lastTime;                 lastTime = GetTickCount();                              float gameTime = (float) deltaTime / 1000;                 Update(gameTime);                 Render(gameTime);             }               //Sleep(1);         } you will notice that i have a commeted line "Sleep", if i add that line , the CPU usage go down to 0 - 0.7%  , but it drops the FPS count like 70%   i also tried remove everything         MSG msg;         ZeroMemory( &msg, sizeof(msg) );         long lastTime = GetTickCount();         while( msg.message!=WM_QUIT)         {             if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )             {                 TranslateMessage( &msg );                 DispatchMessage( &msg );             }else{             }          }  and even without the IF statment , or any other code, just the blank window, and still using cpu about 15-30 %
  6. i also realized the quad was mirror-like (all inverse), after i applied those changes, but i was able to fix it changing the vertex position when i create the vertex buffer       VERTEX QuadVertices[] =     {         { XMFLOAT3( 0.0f,  1.0f, 0.0f), XMFLOAT4(1.0f, 0.0f, 0.0f, 1.0f)},         { XMFLOAT3( 0.0f,  0.0f, 0.0f), XMFLOAT4(0.0f, 1.0f, 0.0f, 1.0f)},         { XMFLOAT3( 1.0f,  1.0f, 0.0f), XMFLOAT4(0.0f, 0.0f, 1.0f, 1.0f)},         { XMFLOAT3 (1.0f,  0.0f, 0.0f), XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f)},       };
  7. Square not showing

        i found this link that would help to create a Debug Device
  8. How to get texture width and size?

    you have a very good tutorial to load textures on DX11   basically you need to call this function HRESULT D3DX11CreateShaderResourceViewFromFile( _In_   ID3D11Device *pDevice, _In_   LPCTSTR pSrcFile, _In_   D3DX11_IMAGE_LOAD_INFO *pLoadInfo, _In_   ID3DX11ThreadPump *pPump, _Out_  ID3D11ShaderResourceView **ppShaderResourceView, _Out_  HRESULT *pHResult ); ex: result = D3DX11CreateShaderResourceViewFromFile(device, filename, NULL, NULL, &m_texture, NULL); and to get the size you would need to cast the ID3D11ShaderResourceView  to ID3D11Texture2D and get its Desc   ID3D11Resource resource; ID3D11Texture2D texture2D; D3D11_TEXTURE2D_DESC desc;    m_texture->GetResource(&resource); //Casting resource.As(&texture2D); texture2D->GetDesc(&desc); //desc.Width //desc.Height     if you only want to know the size of the texture without loading you can use this  HRESULT D3DX11GetImageInfoFromFile( _In_   LPCTSTR pSrcFile, _In_   ID3DX11ThreadPump *pPump, _In_   D3DX11_IMAGE_INFO *pSrcInfo, _Out_  HRESULT *pHResult );   ex: D3DX11_IMAGE_INFO *m_info; D3DX11GetImageInfoFromFile( "path/to/img.jpg", NULL, &m_info, NULL ); this will fill m_info with the struct information below typedef struct D3DX11_IMAGE_INFO { UINT                     Width; UINT                     Height; UINT                     Depth; UINT                     ArraySize; UINT                     MipLevels; UINT                     MiscFlags; DXGI_FORMAT              Format; D3D11_RESOURCE_DIMENSION ResourceDimension; D3DX11_IMAGE_FILE_FORMAT ImageFileFormat; } D3DX11_IMAGE_INFO, *LPD3DX11_IMAGE_INFO;                  
  9.   @unbird , indeep change the culling to D3D11_CULL_NONE, is showing the QUAD but now i have this error , i create the rasterizer after i create the Render Target     D3D11_RASTERIZER_DESC rasterizerState;     ZeroMemory(&rasterizerState, sizeof(D3D11_RASTERIZER_DESC));     rasterizerState.AntialiasedLineEnable = false;     rasterizerState.CullMode = D3D11_CULL_NONE; // D3D11_CULL_FRONT or D3D11_CULL_NONE D3D11_CULL_BACK     rasterizerState.FillMode = D3D11_FILL_SOLID; // D3D11_FILL_SOLID  D3D11_FILL_WIREFRAME     rasterizerState.DepthBias = 0;     rasterizerState.DepthBiasClamp = 0.0f;     rasterizerState.DepthClipEnable = true;     rasterizerState.FrontCounterClockwise = false;     rasterizerState.MultisampleEnable = false;     rasterizerState.ScissorEnable = false;     rasterizerState.SlopeScaledDepthBias = 0.0f;     result = m_pDevice->CreateRasterizerState( &rasterizerState, &m_pRasterState );     m_pDeviceContext->RSSetState(m_pRasterState);   just tried but nothing showing on the window, all black, i change the position of the sprite, but nothing   @davehunt after i change the culling settings, is showing at the TOP LEFT using the XMMatrixOrthographicOffCenterLH you gave me     spriteWVP multiplication was in deep wrong, i change to scale * rotation * translation, and is also positioning the sprite where they should   i create a worldMatrix that is the global matrix of the world, in this case is the screen coords , viewMatrix is XMMatrixIdentity  because i don't new a Look Up view that is normaly used for 3d,   then i need the actual matrix from the sprite, that needs to be multiply by the Screen coords, that is why is multiply with the "worldmatrix"   the last one you mention is the shader , that position is not the sprite position, is the position of each vertex , that need to be multiply with the entire worldmatrix to be converted to world coordinates, in this case, screen coords
  10.   just tried but nothing showing on the window, all black, i change the position of the sprite, but nothing
  11. I'm following some tutorials of directx 11,and i was able to draw a quad, but my problem began when i was applying transforms , for some reason, my quad at position 0,0, start  at bottom left of my screen, but i want to it starts at Top Left , i don't figure out why   this happend when i used XMMatrixOrthographicOffCenterLH , but if i use XMMatrixOrthographicLH , it start at the center of the screen, im doing something wrong?    here is some screenshots:   using XMMatrixOrthographicOffCenterLH :   using XMMatrixOrthographicLH :     SpriteTest.h      #pragma once     #ifndef JDX_SPRITE_TEST_H     #define JDX_SPRITE_TEST_H     //DX Includes     #include <DirectXMath.h>     using namespace DirectX;         class SpriteTest{     public:     void Draw();     private:     struct ShaderParameters { XMMATRIX worldMatrix; };     struct VERTEX { XMFLOAT3 Position; XMFLOAT4 Color; };       ...     ...     };     #endif SpriteTest.cpp       #include "SpriteTest.h"     using namespace DirectX;          void SpriteTest::Initialize(){          // load and compile the two shaders     ID3D10Blob *VS = NULL, *PS = NULL;          m_pRenderManager->CompileShader("Shaders/SpriteBatchVS.hlsl","main","vs_5_0",&VS);     m_pRenderManager->CompileShader("Shaders/SpriteBatchPS.hlsl","main","ps_5_0",&PS);               // encapsulate both shaders into shader objects     m_pDevice->CreateVertexShader(VS->GetBufferPointer(), VS->GetBufferSize(), NULL, &m_pVertexShader);     m_pDevice->CreatePixelShader(PS->GetBufferPointer(), PS->GetBufferSize(), NULL, &m_pPixelShader);          // set the shader objects     m_pDeviceContext->VSSetShader(m_pVertexShader, 0, 0);     m_pDeviceContext->PSSetShader(m_pPixelShader, 0, 0);          // create the input layout object     D3D11_INPUT_ELEMENT_DESC ied[] =     {     {"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0},     {"COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0},     };          m_pDevice->CreateInputLayout(ied, 2, VS->GetBufferPointer(), VS->GetBufferSize(), &m_pInputLayout);     m_pDeviceContext->IASetInputLayout(m_pInputLayout);               // __________ Init Vertex Buffer          // create a triangle using the VERTEX struct     /*     1__3     |\ |     | \|     0--2          struct VERTEX { XMFLOAT3 Position; XMFLOAT4 Color; };       */     VERTEX QuadVertices[] =     {     { XMFLOAT3(-1.0f, -1.0f, 0.0f), XMFLOAT4(1.0f, 0.0f, 0.0f, 1.0f)},     { XMFLOAT3(-1.0f,  1.0f, 0.0f), XMFLOAT4(0.0f, 1.0f, 0.0f, 1.0f)},     { XMFLOAT3( 1.0f, -1.0f, 0.0f), XMFLOAT4(0.0f, 0.0f, 1.0f, 1.0f)},     { XMFLOAT3 (1.0f,  1.0f, 0.0f), XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f)},          };          // create the vertex buffer     D3D11_BUFFER_DESC bd;     ZeroMemory(&bd, sizeof(bd));          bd.Usage = D3D11_USAGE_DYNAMIC;                // write access access by CPU and GPU     bd.ByteWidth = sizeof(VERTEX) * 4;             // size is the VERTEX struct * 4     bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;       // use as a vertex buffer     bd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;    // allow CPU to write in buffer          m_pDevice->CreateBuffer(&bd, NULL, &m_pVertexBuffer);       // create the buffer          // copy the vertices into the buffer     D3D11_MAPPED_SUBRESOURCE ms;     m_pDeviceContext->Map(m_pVertexBuffer, NULL, D3D11_MAP_WRITE_DISCARD, NULL, &ms);    // map the buffer     memcpy(ms.pData, QuadVertices, sizeof(QuadVertices)); // copy the data     m_pDeviceContext->Unmap(m_pVertexBuffer, NULL);                                      // unmap the buffer              // __________ Init Index Buffer          WORD indices[] =     {     0,1,2,     2,1,3,     };               D3D11_BUFFER_DESC indexDesc;      ZeroMemory( &indexDesc, sizeof( indexDesc ) );         indexDesc.Usage = D3D11_USAGE_DEFAULT;     indexDesc.ByteWidth = sizeof( WORD ) * 6;     indexDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;     indexDesc.CPUAccessFlags = 0;          D3D11_SUBRESOURCE_DATA indexData;      ZeroMemory( &indexData, sizeof( indexData ) );      indexData.pSysMem = indices;          m_pDevice->CreateBuffer( &indexDesc, &indexData, &m_pIndexBuffer );               // __________ Init Constant Buffer     D3D11_BUFFER_DESC constDesc;      ZeroMemory( &constDesc, sizeof( constDesc ) );         constDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;      constDesc.ByteWidth = sizeof( XMMATRIX );      constDesc.Usage = D3D11_USAGE_DEFAULT;         m_pDevice->CreateBuffer( &constDesc, 0, &m_pCostantBuffer );          }          void SpriteTest::Draw(){     // select which vertex buffer to display         UINT stride = sizeof(VERTEX);     UINT offset = 0;     m_pDeviceContext->IASetVertexBuffers(0, 1, &m_pVertexBuffer, &stride, &offset);         // select which primtive type we are using     m_pDeviceContext->IASetIndexBuffer( m_pIndexBuffer, DXGI_FORMAT_R16_UINT, 0 );     m_pDeviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);          XMMATRIX view = XMMatrixIdentity();      //XMMATRIX projection = XMMatrixOrthographicLH( 1024, 768, 0.0f, 100.0f );     XMMATRIX projection = XMMatrixOrthographicOffCenterLH( 0.0f,(float)m_pRenderManager->GetWidth(),0.0f,(float) m_pRenderManager->GetHeight(), 0.0f, 100.0f );     XMMATRIX worldMatrix = XMMatrixMultiply( view, projection );          ShaderParameters shaderParameters;          // __________ Sprite World Coordinates     float spriteWidth = 32.0f,      spriteHeight = 32.0f,      spritePosX = 0.0f,      spritePosY = 0.0f;          XMMATRIX l_translation = XMMatrixTranslation(spritePosX, spritePosY, 0.0f );      XMMATRIX l_rotationZ = XMMatrixRotationZ( 0.0f );      XMMATRIX l_scale = XMMatrixScaling( 1.0f * spriteWidth,1.0f * spriteHeight, 1.0f );     XMMATRIX l_spriteWVP = l_translation * l_rotationZ * l_scale;          // __________ Prepare World Coordinates to send to the shader     XMMATRIX l_worldMatrix = XMMatrixMultiply( l_spriteWVP, worldMatrix );      l_worldMatrix = XMMatrixTranspose( l_worldMatrix );          shaderParameters.worldMatrix = l_worldMatrix;         m_pDeviceContext->UpdateSubresource( m_pCostantBuffer, 0, NULL, &shaderParameters, 0, 0 );       m_pDeviceContext->VSSetConstantBuffers( 0, 1, &m_pCostantBuffer );         m_pDeviceContext->DrawIndexed( 6, 0, 0 );         } vertex shader:       cbuffer cbParameters : register( b0 ) {      float4x4 worldMatrix;      };          struct VOut     {     float4 position : SV_POSITION;     float4 color : COLOR;     };          VOut main(float4 position : POSITION, float4 color : COLOR)     {     VOut output;          output.position = mul(position,worldMatrix);     output.color = color;          return output;     }   pixel shader:       float4 main(float4 position : SV_POSITION, float4 color : COLOR) : SV_TARGET     {     return color;     }  
  12. @DgekGD , i like your approach because i can use the same VS and PS in differents classes !     @Racoonacoon well right now i don`t have the implementation made of Quad2D on my current "Engine"(DX11), i wanted to ask before i do it because DX10-11 works different,  but i do have my DX9 version ,    void RenderManager::DrawQuad2D (CTexture* texture, Vector2 position, int w, int h) { // position = [0] // // [0]------[2] // | | // | | // | | // [1]------[3] Vector2 coord_text[4]; coord_text[0].x = 0.f; coord_text[0].y = 0.f; coord_text[1].x = 0.f; coord_text[1].y = 1.f; coord_text[2].x = 1.f; coord_text[2].y = 0.f; coord_text[3].x = 1.f; coord_text[3].y = 1.f; unsigned short indices[6]={0,2,1,1,2,3}; SCREEN_TEXTURE_VERTEX v[4] = { { (float)position.x, (float)position.y, 0.f,1.f, coord_text[0].x, coord_text[0].y} //(x,y) sup_esq. , { (float)position.x, (float)position.y+h, 0.f,1.f, coord_text[1].x, coord_text[1].y} //(x,y) inf_esq. , { (float)position.x+w, (float)position.y, 0.f,1.f, coord_text[2].x, coord_text[2].y} //(x,y) sup_dr. , { (float)position.x+w, (float)position.y+h, 0.f,1.f, coord_text[3].x, coord_text[3].y} //(x,y) inf_dr. }; m_pD3DDevice->SetFVF( SCREEN_TEXTURE_VERTEX::getFlags() ); m_pD3DDevice->SetTexture(0, texture->GetD3DXTexture() ); m_pD3DDevice->DrawIndexedPrimitiveUP( D3DPT_TRIANGLELIST,0, 4, 2,indices,D3DFMT_INDEX16, v, sizeof( SCREEN_TEXTURE_VERTEX ) ); }     Well i based in your suggestion , i think i will do some sort of XNA's "SpriteBranch", and that class can be instantiated once, and be passed on the "Render" method,  instead of the "RenderManager",    Thank for your greats advice 
  13. OpenGL a way to port a code?

    first of all, DX is not Better than OpenGL , is just other alternative, and because OpenGL is not been built by a hugee company, sometimes takes a lot of time to be update   so the best way to "Port" a code, is based actually how you make your code, and OOP is the way to go,   a good way is usually to make a interface "RenderManager" with some methods and you used that in your entire code, and that way you can have two implementations of the "RenderManager", one can be RenderManagerOGL , and RenderManagerDX        class RenderManagerOGL : RenderManager{}  class RenderManagerDX : RenderManager{} ... RenderManager *rm = NULL; #ifdef WIN32 rm = new RenderManagerDX(); #else rm = new RenderManagerOGL(); #endif   so in that way you make sure you have the same methods and your game is not going to have problem using the same code,   this is just a way to do it, im sure there are better solution, but that is how i would do it