Xbox LIVE Indie Games
Sort Discussions: Previous Discussion Next Discussion
Page 1 of 1 (4 posts)

Can't get antialiased picture from Direct3D

Last post 8/17/2018 8:11 AM by Sergey-Xa. 3 replies.
  • 8/16/2018 7:49 PM

    Can't get antialiased picture from Direct3D

    I'm taking my first steps in programming with Direct3D. I have a very basic pipeline setup, and all I want to get from it is an antialiased smooth image. But I get this:
    screenshot


    First, I can't get rid of stair effect though I have 4x MSAA enabled already in my pipeline (DXGI_SAMPLE_DESC::Count is 4 and Quality is 0): 
    stairs


    And second, I get this noisy texturing though I have mipmaps generated and LINEAR filtering set in the sampler state.
    noisy texturing


    Am I missing something or doing wrong?

    Here is my code:

    1) Renderer class:
    1 #include "Scene.h" // Custom class that contains vertex and index buffer contents for every rendered mesh. 
    2 #include "Camera.h" // Custom class that contains camera position and fov. 
    3
    4 #include <wrl/client.h> 
    5 using Microsoft::WRL::ComPtr; 
    6
    7 #include <DirectXMath.h> 
    8 using namespace DirectX; 
    9
    10 #include <map> 
    11
    12 #include "generated\VertexShader.h" 
    13 #include "generated\PixelShader.h" 
    14  
    15 class Renderer 
    16
    17 public
    18     Renderer(HWND hWnd, int wndWidth, int wndHeight, const Scene& scene, const Camera& camera); 
    19  
    20     void Render(); 
    21  
    22     void SwitchToWireframe(); 
    23     void SwitchToSolid(); 
    24  
    25 protected
    26     void CreateDeviceAndSwapChain(); 
    27     void CreateDepthStencil(); 
    28     void CreateInputLayout(); 
    29     void CreateVertexShader(); 
    30     void CreatePixelShader(); 
    31     void CreateRasterizerStates(); 
    32     void CreateBlendState(); 
    33     void CreateSamplerState(); 
    34     void CreateBuffer(ID3D11Buffer** buffer,  
    35         D3D11_USAGE usage, D3D11_BIND_FLAG bindFlags, 
    36         UINT cpuAccessFlags, UINT miscFlags, 
    37         UINT sizeOfBuffer, UINT sizeOfBufferElement, const void* initialData); 
    38     void CreateTexture2DAndSRV(const Scene::Texture& texture, ID3D11ShaderResourceView** view); 
    39     void CreateTexturesAndViews(); 
    40     void GenerateMips(); 
    41  
    42 protected
    43     const Scene& m_scene; 
    44  
    45     const Camera& m_camera; 
    46     DWORD m_cameraLastUpdateTickCount; 
    47  
    48     HWND m_windowHandle; 
    49     int m_windowWidth;  
    50     int m_windowHeight; 
    51  
    52     DXGI_SAMPLE_DESC m_sampleDesc; 
    53  
    54     ComPtr<IDXGISwapChain> m_swapChain; 
    55     ComPtr<ID3D11Texture2D> m_swapChainBuffer; 
    56     ComPtr<ID3D11RenderTargetView> m_swapChainBufferRTV; 
    57     ComPtr<ID3D11Device> m_device; 
    58     ComPtr<ID3D11DeviceContext> m_deviceContext; 
    59     ComPtr<ID3D11Debug> m_debugger; 
    60     ComPtr<ID3D11Texture2D> m_depthStencilTexture; 
    61     ComPtr<ID3D11DepthStencilState> m_depthStencilState; 
    62     ComPtr<ID3D11DepthStencilView> m_depthStencilView; 
    63     ComPtr<ID3D11InputLayout> m_inputLayout; 
    64     ComPtr<ID3D11VertexShader> m_vertexShader; 
    65     ComPtr<ID3D11PixelShader> m_pixelShader; 
    66     ComPtr<ID3D11RasterizerState> m_solidRasterizerState; 
    67     ComPtr<ID3D11RasterizerState> m_wireframeRasterizerState; 
    68     ComPtr<ID3D11BlendState> m_blendState; 
    69     ComPtr<ID3D11SamplerState> m_linearSamplerState; 
    70  
    71     std::map<std::string, ComPtr<ID3D11ShaderResourceView>> m_diffuseMapViews; 
    72     std::map<std::string, ComPtr<ID3D11ShaderResourceView>> m_normalMapViews; 
    73  
    74     XMMATRIX m_worldViewMatrix; 
    75  
    76     ID3D11RasterizerState* m_currentRasterizerState; 
    77 }; 
    78  
    79 void Renderer::CreateDeviceAndSwapChain() 
    80
    81     HRESULT hr; 
    82  
    83     DXGI_SWAP_CHAIN_DESC swapChainDesc = {}; 
    84     swapChainDesc.BufferDesc.Width = m_windowWidth; 
    85     swapChainDesc.BufferDesc.Height = m_windowHeight; 
    86     swapChainDesc.BufferDesc.RefreshRate.Numerator = 1; 
    87     swapChainDesc.BufferDesc.RefreshRate.Denominator = 60; 
    88     swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM_SRGB; 
    89     swapChainDesc.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_PROGRESSIVE; 
    90     swapChainDesc.BufferDesc.Scaling = DXGI_MODE_SCALING_CENTERED; 
    91     swapChainDesc.SampleDesc = m_sampleDesc; 
    92     swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; 
    93     swapChainDesc.BufferCount = 1; 
    94     swapChainDesc.OutputWindow = m_windowHandle; 
    95     swapChainDesc.Windowed = TRUE; 
    96     swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD; 
    97     swapChainDesc.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH; 
    98  
    99     D3D_FEATURE_LEVEL desiredFeatureLevels[] = { D3D_FEATURE_LEVEL_10_1 }; 
    100     D3D_FEATURE_LEVEL featureLevel; 
    101  
    102     hr = D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, 
    103         D3D11_CREATE_DEVICE_DEBUG | D3D11_CREATE_DEVICE_PREVENT_INTERNAL_THREADING_OPTIMIZATIONS, 
    104         desiredFeatureLevels, 1, D3D11_SDK_VERSION, &swapChainDesc, 
    105         m_swapChain.GetAddressOf(), m_device.GetAddressOf(), &featureLevel,  
    106         m_deviceContext.GetAddressOf()); 
    107  
    108     if (FAILED(hr)) 
    109     { 
    110         hr = D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_WARP, NULL, 
    111             D3D11_CREATE_DEVICE_DEBUG | D3D11_CREATE_DEVICE_PREVENT_INTERNAL_THREADING_OPTIMIZATIONS, 
    112             desiredFeatureLevels, 1, D3D11_SDK_VERSION, &swapChainDesc, 
    113             m_swapChain.GetAddressOf(), m_device.GetAddressOf(), &featureLevel, 
    114             m_deviceContext.GetAddressOf()); 
    115     } 
    116  
    117     if (FAILED(hr)) 
    118         throw std::exception("Failed to create device or swap chain"); 
    119  
    120     hr = m_device->QueryInterface(m_debugger.GetAddressOf()); 
    121  
    122     if (FAILED(hr)) 
    123         throw std::exception("Failed to get debugger interface"); 
    124  
    125     hr = m_swapChain->GetBuffer(0, __uuidof(m_swapChainBuffer), 
    126         (void**)m_swapChainBuffer.GetAddressOf()); 
    127  
    128     if (FAILED(hr)) 
    129         throw std::exception("Failed to get swap chain buffer"); 
    130  
    131     hr = m_device->CreateRenderTargetView(m_swapChainBuffer.Get(), NULL,  
    132         m_swapChainBufferRTV.GetAddressOf()); 
    133  
    134     if (FAILED(hr)) 
    135         throw std::exception("Failed to create RTV for swap chain buffer"); 
    136
    137  
    138 void Renderer::CreateDepthStencil() 
    139
    140     HRESULT hr; 
    141  
    142     D3D11_TEXTURE2D_DESC tdesc; 
    143     tdesc.Width = m_windowWidth; 
    144     tdesc.Height = m_windowHeight; 
    145     tdesc.MipLevels = 1; 
    146     tdesc.ArraySize = 1; 
    147     tdesc.Format = DXGI_FORMAT_D16_UNORM; 
    148     tdesc.SampleDesc = m_sampleDesc; 
    149     tdesc.Usage = D3D11_USAGE_DEFAULT; 
    150     tdesc.BindFlags = D3D11_BIND_DEPTH_STENCIL; 
    151     tdesc.CPUAccessFlags = 0; 
    152     tdesc.MiscFlags = 0; 
    153  
    154     hr = m_device->CreateTexture2D(&tdesc, NULL, m_depthStencilTexture.GetAddressOf()); 
    155  
    156     if (FAILED(hr)) 
    157         throw std::exception("Failed to create depth stencil texture"); 
    158  
    159     D3D11_DEPTH_STENCIL_VIEW_DESC dsvdesc; 
    160     dsvdesc.Format = DXGI_FORMAT_D16_UNORM; 
    161     dsvdesc.ViewDimension = m_sampleDesc.Count > 1  
    162         ? D3D11_DSV_DIMENSION_TEXTURE2DMS  
    163         : D3D11_DSV_DIMENSION_TEXTURE2D; 
    164     dsvdesc.Flags = 0; 
    165     dsvdesc.Texture2D.MipSlice = 0; 
    166  
    167     hr = m_device->CreateDepthStencilView(m_depthStencilTexture.Get(), &dsvdesc,  
    168         m_depthStencilView.GetAddressOf()); 
    169  
    170     if (FAILED(hr)) 
    171         throw std::exception("Failed to create depth stencil view"); 
    172  
    173     D3D11_DEPTH_STENCIL_DESC dsdesc; 
    174     dsdesc.DepthEnable = TRUE; 
    175     dsdesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL; 
    176     dsdesc.DepthFunc = D3D11_COMPARISON_LESS; 
    177     dsdesc.StencilEnable = FALSE; 
    178     dsdesc.StencilReadMask = 0; 
    179     dsdesc.StencilWriteMask = 0; 
    180     dsdesc.FrontFace = {}; 
    181     dsdesc.BackFace = {}; 
    182  
    183     hr = m_device->CreateDepthStencilState(&dsdesc, m_depthStencilState.GetAddressOf()); 
    184  
    185     if (FAILED(hr)) 
    186         throw std::exception("Failed to create depth stencil state"); 
    187
    188  
    189 void Renderer::CreateInputLayout() 
    190
    191     HRESULT hr; 
    192  
    193     D3D11_INPUT_ELEMENT_DESC iedescs[] = { 
    194         { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 }, 
    195     { "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 }, 
    196     { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 }, 
    197     }; 
    198  
    199     hr = m_device->CreateInputLayout(iedescs, 3, 
    200         g_vertexShader, sizeof(g_vertexShader), 
    201         m_inputLayout.GetAddressOf()); 
    202  
    203     if (FAILED(hr)) 
    204         throw std::exception("Failed to create input layout"); 
    205
    206  
    207 void Renderer::CreateVertexShader() 
    208
    209     HRESULT hr; 
    210  
    211     hr = m_device->CreateVertexShader(g_vertexShader, sizeof(g_vertexShader),  
    212         NULL, m_vertexShader.GetAddressOf()); 
    213  
    214     if (FAILED(hr)) 
    215         throw std::exception("Failed to create vertex shader"); 
    216
    217  
    218 void Renderer::CreatePixelShader() 
    219
    220     HRESULT hr; 
    221  
    222     hr = m_device->CreatePixelShader(g_pixelShader, sizeof(g_pixelShader),  
    223         NULL, m_pixelShader.GetAddressOf()); 
    224  
    225     if (FAILED(hr)) 
    226         throw std::exception("Failed to create pixel shader"); 
    227
    228  
    229 void Renderer::CreateRasterizerStates() 
    230
    231     HRESULT hr; 
    232  
    233     D3D11_RASTERIZER_DESC rdesc; 
    234     rdesc.FillMode = D3D11_FILL_SOLID; 
    235     rdesc.CullMode = D3D11_CULL_FRONT; 
    236     rdesc.FrontCounterClockwise = FALSE; 
    237     rdesc.DepthBias = 0; 
    238     rdesc.DepthBiasClamp = 0.0f; 
    239     rdesc.SlopeScaledDepthBias = 0.0f; 
    240     rdesc.DepthClipEnable = TRUE; 
    241     rdesc.ScissorEnable = FALSE; 
    242     rdesc.MultisampleEnable = m_sampleDesc.Count > 1 ? TRUE : FALSE; 
    243     rdesc.AntialiasedLineEnable = m_sampleDesc.Count > 1 ? TRUE : FALSE; 
    244  
    245     hr = m_device->CreateRasterizerState(&rdesc, m_solidRasterizerState.GetAddressOf()); 
    246  
    247     if (FAILED(hr)) 
    248         throw std::exception("Failed to create rasterizer state"); 
    249  
    250     rdesc.FillMode = D3D11_FILL_WIREFRAME; 
    251  
    252     hr = m_device->CreateRasterizerState(&rdesc, m_wireframeRasterizerState.GetAddressOf()); 
    253  
    254     if (FAILED(hr)) 
    255         throw std::exception("Failed to create rasterizer state"); 
    256  
    257     m_currentRasterizerState = m_solidRasterizerState.Get(); 
    258
    259  
    260 void Renderer::CreateSamplerState() 
    261
    262     HRESULT hr; 
    263  
    264     D3D11_SAMPLER_DESC smdesc; 
    265     smdesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR; 
    266     smdesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP; 
    267     smdesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP; 
    268     smdesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP; 
    269     smdesc.MipLODBias = 0.0f; 
    270     smdesc.MaxAnisotropy = 0; 
    271     smdesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS; 
    272     smdesc.BorderColor[4] = {}; 
    273     FLOAT MinLOD = 0.0; 
    274     FLOAT MaxLOD = 0.0; 
    275  
    276     hr = m_device->CreateSamplerState(&smdesc, m_linearSamplerState.GetAddressOf()); 
    277  
    278     if (FAILED(hr)) 
    279         throw new std::exception("Failed to create sampler state"); 
    280
    281  
    282 void Renderer::CreateBlendState() 
    283
    284     HRESULT hr; 
    285  
    286     D3D11_BLEND_DESC bdesc; 
    287     bdesc.AlphaToCoverageEnable = FALSE; 
    288     bdesc.IndependentBlendEnable = FALSE; 
    289     bdesc.RenderTarget[0].BlendEnable = FALSE; 
    290     bdesc.RenderTarget[0].SrcBlend = D3D11_BLEND_ONE; 
    291     bdesc.RenderTarget[0].DestBlend = D3D11_BLEND_ZERO; 
    292     bdesc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD; 
    293     bdesc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ONE; 
    294     bdesc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ZERO; 
    295     bdesc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD; 
    296     bdesc.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL; 
    297  
    298     hr = m_device->CreateBlendState(&bdesc, m_blendState.GetAddressOf()); 
    299  
    300     if (FAILED(hr)) 
    301         throw std::exception("Failed to create blend state"); 
    302
    303  
    304  void Renderer::CreateBuffer(ID3D11Buffer** buffer,  
    305      D3D11_USAGE usage, D3D11_BIND_FLAG bindFlags, 
    306     UINT cpuAccessFlags, UINT miscFlags,  
    307     UINT sizeOfBuffer, UINT sizeOfBufferElement, const void* initialData) 
    308
    309     HRESULT hr; 
    310  
    311     D3D11_BUFFER_DESC bdesc; 
    312     bdesc.ByteWidth = sizeOfBuffer; 
    313     bdesc.Usage = usage; 
    314     bdesc.BindFlags = bindFlags; 
    315     bdesc.CPUAccessFlags = cpuAccessFlags; 
    316     bdesc.MiscFlags = miscFlags; 
    317     bdesc.StructureByteStride = sizeOfBufferElement; 
    318  
    319     D3D11_SUBRESOURCE_DATA bdata; 
    320     bdata.pSysMem = initialData; 
    321     bdata.SysMemPitch = 0; 
    322     bdata.SysMemSlicePitch = 0; 
    323  
    324     hr = m_device->CreateBuffer(&bdesc, &bdata, buffer); 
    325  
    326     if (FAILED(hr)) 
    327         throw std::exception("Failed to create buffer"); 
    328
    329  
    330  void Renderer::CreateTexture2DAndSRV(const Scene::Texture& sceneTexture, ID3D11ShaderResourceView** view) 
    331  { 
    332      HRESULT hr; 
    333  
    334      constexpr DXGI_FORMAT texformat = DXGI_FORMAT_R32G32B32A32_FLOAT; 
    335  
    336      D3D11_TEXTURE2D_DESC tdesc; 
    337      tdesc.Width = sceneTexture.width; 
    338      tdesc.Height = sceneTexture.height; 
    339      tdesc.MipLevels = 0; 
    340      tdesc.ArraySize = 1; 
    341      tdesc.Format = texformat; 
    342      tdesc.SampleDesc.Count = 1; 
    343      tdesc.SampleDesc.Quality = 0; 
    344      tdesc.Usage = D3D11_USAGE_DEFAULT; 
    345      tdesc.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET; 
    346      tdesc.CPUAccessFlags = 0; 
    347      tdesc.MiscFlags = D3D11_RESOURCE_MISC_GENERATE_MIPS; 
    348  
    349      ComPtr<ID3D11Texture2D> texture2d; 
    350  
    351      hr = m_device->CreateTexture2D(&tdesc, NULL, texture2d.GetAddressOf()); 
    352  
    353      if (FAILED(hr)) 
    354          throw std::exception("Failed to create texture"); 
    355  
    356  
    357      D3D11_SUBRESOURCE_DATA srdata; 
    358      srdata.pSysMem = sceneTexture.data; 
    359      srdata.SysMemPitch = sceneTexture.width * sizeof(float) * 4; 
    360      srdata.SysMemSlicePitch = 0; 
    361  
    362      m_deviceContext->UpdateSubresource(texture2d.Get(), 0, NULL,  
    363          srdata.pSysMem, srdata.SysMemPitch, srdata.SysMemSlicePitch); 
    364  
    365  
    366      D3D11_SHADER_RESOURCE_VIEW_DESC srvdesc; 
    367      srvdesc.Format = texformat; 
    368      srvdesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D; 
    369      srvdesc.Texture2D.MostDetailedMip = 0; 
    370      srvdesc.Texture2D.MipLevels = -1; 
    371  
    372      ComPtr<ID3D11ShaderResourceView> shaderResourceView; 
    373  
    374      hr = m_device->CreateShaderResourceView(texture2d.Get(), &srvdesc, view); 
    375  
    376      if (FAILED(hr)) 
    377          throw std::exception("Failed to create shader resource view"); 
    378  } 
    379  
    380  void Renderer::CreateTexturesAndViews() 
    381  { 
    382      for (auto it = m_scene.materials.cbegin(); it != m_scene.materials.cend(); it++) 
    383      { 
    384          //don't know what's the problem but if I don't place initialized ComPtr<...> instance into a map 
    385          //then further .GetAddessOf() fails. 
    386          m_diffuseMapViews.emplace(it->first, ComPtr<ID3D11ShaderResourceView>()); 
    387          m_normalMapViews.emplace(it->first, ComPtr<ID3D11ShaderResourceView>());  
    388  
    389          CreateTexture2DAndSRV(it->second.diffuseMap, m_diffuseMapViews[it->first].GetAddressOf()); 
    390          CreateTexture2DAndSRV(it->second.normalMap, m_normalMapViews[it->first].GetAddressOf()); 
    391      } 
    392  } 
    393  
    394  void Renderer::GenerateMips() 
    395  { 
    396      for (auto it = m_diffuseMapViews.begin(); it != m_diffuseMapViews.end(); it++) 
    397          m_deviceContext->GenerateMips(it->second.Get()); 
    398      for (auto it = m_normalMapViews.begin(); it != m_normalMapViews.end(); it++) 
    399          m_deviceContext->GenerateMips(it->second.Get()); 
    400  } 
    401  
    402 Renderer::Renderer(HWND hWnd, int windowWidth, int windowHeight,  
    403     const Scene& scene, const Camera& camera) 
    404     : m_scene(scene) 
    405     , m_camera(camera) 
    406     , m_cameraLastUpdateTickCount(0) 
    407     , m_windowHandle(hWnd) 
    408     , m_windowWidth(windowWidth) 
    409     , m_windowHeight(windowHeight) 
    410
    411     m_sampleDesc.Count = 4; 
    412     m_sampleDesc.Quality = 0; 
    413  
    414     CreateDeviceAndSwapChain(); 
    415     CreateDepthStencil(); 
    416     CreateInputLayout(); 
    417     CreateVertexShader(); 
    418     CreatePixelShader(); 
    419     CreateRasterizerStates(); 
    420     CreateBlendState(); 
    421     CreateSamplerState(); 
    422     CreateTexturesAndViews(); 
    423     GenerateMips(); 
    424  
    425     // Setting up IA stage 
    426  
    427     m_deviceContext->IASetPrimitiveTopology( 
    428         D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); 
    429  
    430     m_deviceContext->IASetInputLayout(m_inputLayout.Get()); 
    431  
    432     // Setting up VS stage 
    433  
    434     m_deviceContext->VSSetShader(m_vertexShader.Get(), 0, 0); 
    435  
    436     // Setting up RS stage 
    437  
    438     D3D11_VIEWPORT viewport; 
    439     viewport.TopLeftX = 0.0f; 
    440     viewport.TopLeftY = 0.0f; 
    441     viewport.Width = static_cast<FLOAT>(m_windowWidth); 
    442     viewport.Height = static_cast<FLOAT>(m_windowHeight); 
    443     viewport.MinDepth = 0.0f; 
    444     viewport.MaxDepth = 1.0f; 
    445  
    446     m_deviceContext->RSSetViewports(1, &viewport); 
    447  
    448     // Setting up PS stage 
    449  
    450     m_deviceContext->PSSetSamplers(0, 1, m_linearSamplerState.GetAddressOf()); 
    451     m_deviceContext->PSSetShader(m_pixelShader.Get(), 0, 0); 
    452  
    453     // Setting up OM stage 
    454  
    455     m_deviceContext->OMSetBlendState(m_blendState.Get(), NULL, 0xffffffff); 
    456     m_deviceContext->OMSetDepthStencilState(m_depthStencilState.Get(), 0); 
    457     m_deviceContext->OMSetRenderTargets(1, m_swapChainBufferRTV.GetAddressOf(), m_depthStencilView.Get()); 
    458
    459  
    460 void Renderer::Render() 
    461
    462     constexpr float background[4] = { 0.047f, 0.0487f, 0.066f, 1.0f }; 
    463  
    464     // Setting up view matix 
    465  
    466     if (m_cameraLastUpdateTickCount 
    467         != m_camera.GetLastUpdateTickCount()) 
    468     { 
    469         const Float3& camFrom = m_camera.GetFrom(); 
    470         const Float3& camAt = m_camera.GetAt(); 
    471         const Float3& camUp = m_camera.GetUp(); 
    472  
    473         m_cameraLastUpdateTickCount = m_camera.GetLastUpdateTickCount(); 
    474  
    475         FXMVECTOR from = XMVectorSet(camFrom.x, camFrom.y, camFrom.z, 1.0f); 
    476         FXMVECTOR at = XMVectorSet(camAt.x, camAt.y, camAt.z, 1.0f); 
    477         FXMVECTOR up = XMVectorSet(camUp.x, camUp.y, camUp.z, 0.0f); 
    478  
    479         FXMVECTOR dir = XMVectorSubtract(at, from); 
    480  
    481         FXMVECTOR x = XMVector3Cross(dir, up); 
    482         FXMVECTOR up2 = XMVector3Cross(x, dir); 
    483  
    484         XMMATRIX lookTo = XMMatrixLookToRH(from, dir, up2); 
    485  
    486         float scalef = 1.0f / XMVectorGetByIndex(XMVector3Length(dir), 0); 
    487  
    488         XMMATRIX scale = XMMatrixScaling(scalef, scalef, scalef); 
    489  
    490         float aspect = float(m_windowWidth) / m_windowHeight; 
    491         float fov = m_camera.GetFov() / 180.0f * 3.14f; 
    492  
    493         XMMATRIX persp = XMMatrixPerspectiveFovRH(fov, aspect, 0.1f, 1000.0f); 
    494  
    495         m_worldViewMatrix = XMMatrixMultiply(XMMatrixMultiply(lookTo, scale), persp); 
    496     } 
    497     else 
    498     { 
    499         return
    500     } 
    501  
    502     m_deviceContext->ClearDepthStencilView(m_depthStencilView.Get(), D3D11_CLEAR_DEPTH, 1.0f, 0); 
    503     m_deviceContext->ClearRenderTargetView(m_swapChainBufferRTV.Get(), background); 
    504  
    505     for (auto imesh = m_scene.meshes.cbegin(); imesh != m_scene.meshes.cend(); imesh++) 
    506     { 
    507         // Creating vertex buffer 
    508  
    509         ComPtr<ID3D11Buffer> vertexBuffer; 
    510  
    511         CreateBuffer(vertexBuffer.GetAddressOf(),  
    512             D3D11_USAGE_DEFAULT, D3D11_BIND_VERTEX_BUFFER, 0, 0, 
    513             sizeof(Scene::Vertex) * imesh->vertices.size(), sizeof(Scene::Vertex), 
    514             imesh->vertices.data()); 
    515  
    516         // Creating index buffer 
    517  
    518         ComPtr<ID3D11Buffer> indexBuffer; 
    519  
    520         CreateBuffer(indexBuffer.GetAddressOf(), 
    521             D3D11_USAGE_DEFAULT, D3D11_BIND_INDEX_BUFFER, 0, 0, 
    522             sizeof(unsigned int) * imesh->indices.size(), sizeof(unsigned int), 
    523             imesh->indices.data()); 
    524  
    525         // Creating constant buffer 
    526  
    527         ComPtr<ID3D11Buffer> constantBuffer; 
    528  
    529         CreateBuffer(constantBuffer.GetAddressOf(), 
    530             D3D11_USAGE_IMMUTABLE, D3D11_BIND_CONSTANT_BUFFER, 0, 0, 
    531             sizeof(XMMATRIX), sizeof(XMMATRIX), 
    532             &m_worldViewMatrix); 
    533  
    534         // Setting up IA stage 
    535  
    536         ID3D11Buffer* vertexBuffers[8] = { vertexBuffer.Get() }; 
    537         unsigned int vertexBufferStrides[8] = { sizeof(Scene::Vertex) }; 
    538         unsigned int vertexBufferOffsets[8] = { 0 }; 
    539  
    540         m_deviceContext->IASetVertexBuffers(0, 8, 
    541             vertexBuffers, vertexBufferStrides, vertexBufferOffsets); 
    542  
    543         m_deviceContext->IASetIndexBuffer(indexBuffer.Get(), DXGI_FORMAT_R32_UINT, 0); 
    544  
    545         // Setting up VS stage 
    546  
    547         m_deviceContext->VSSetConstantBuffers(0, 1, constantBuffer.GetAddressOf()); 
    548  
    549         // Setting up RS stage 
    550  
    551         m_deviceContext->RSSetState(m_currentRasterizerState); 
    552  
    553         // Setting up PS stage 
    554  
    555         ID3D11ShaderResourceView* srvs[2] = { }; 
    556         srvs[0] = m_diffuseMapViews.at(imesh->material).Get(); 
    557         srvs[1] = m_normalMapViews.at(imesh->material).Get(); 
    558  
    559         m_deviceContext->PSSetShaderResources(0, 2, srvs); 
    560  
    561         // Drawing 
    562  
    563         m_deviceContext->DrawIndexed(imesh->indices.size(), 0, 0); 
    564     } 
    565  
    566     m_swapChain->Present(0, 0); 
    567
    568  
    569 void Renderer::SwitchToWireframe() 
    570
    571     m_currentRasterizerState = m_wireframeRasterizerState.Get(); 
    572     m_camera.UpdateLastUpdateTickCount(); 
    573
    574  
    575 void Renderer::SwitchToSolid() 
    576
    577     m_currentRasterizerState = m_solidRasterizerState.Get(); 
    578     m_camera.UpdateLastUpdateTickCount(); 
    579 }


    2) Vertex shader
    1struct VS_INPUT 
    2
    3    float3 position : POSITION; 
    4    float3 normal : NORMAL; 
    5    float2 texcoord : TEXCOORD; 
    6}; 
    7 
    8struct VS_OUTPUT 
    9
    10    float4 position : SV_POSITION; 
    11    float3 normal : NORMAL; 
    12    float2 texcoord : TEXCOORD; 
    13}; 
    14 
    15cbuffer Matrices 
    16
    17    matrix worldViewMatrix; 
    18
    19 
    20VS_OUTPUT main(VS_INPUT input) 
    21
    22    VS_OUTPUT output; 
    23 
    24    output.position = mul(worldViewMatrix, float4(input.position.xyz, 1.0)); 
    25    output.normal = input.normal; 
    26    output.texcoord = input.texcoord; 
    27 
    28    return output; 
    29
    3) Pixel shader:
    1 Texture2D DiffuseMap : register(t0); 
    2 Texture2D NormalMap: register(t1); 
    3  
    4 SamplerState LinearSampler : register(s0); 
    5  
    6 float4 main(VS_OUTPUT input) : SV_TARGET 
    7
    8     float3 light = normalize(float3(2.87, -0.36, 1.68)); 
    9  
    10     float3 diffuseColor = DiffuseMap.Sample(LinearSampler, input.texcoord); 
    11     float3 normalDisplace = float3(0.0, 0.0, 1.0) - NormalMap.Sample(LinearSampler, input.texcoord); 
    12  
    13     float illumination = clamp(dot(light, input.normal + normalDisplace), 0.2, 1.0); 
    14  
    15     return float4(mul(diffuseColor, illumination), 1.0); 
    16


    Thank you.
  • 8/17/2018 12:26 AM In reply to

    Re: Can't get antialiased picture from Direct3D

    How many triangles are in the scene? If you have a lot of sub-pixel triangles, then no amount of MSAA or mipmapping is going to help all that much.
  • 8/17/2018 7:16 AM In reply to

    Re: Can't get antialiased picture from Direct3D

    No, there isn't a lot of subpixel triangles there:
    View post on imgur.com
  • 8/17/2018 8:11 AM In reply to

    Re: Can't get antialiased picture from Direct3D

    Okay, I've just figured the reason of this stairs effect:

    The reason is that I passed the same width and height values for CreateWindow WinApi function, and for DXGI_SWAP_CHAIN_DESC::BufferDesc. Meanwhile, these should be different because CreateWindow takes outer width and height of a window to create (window rectangle), while BufferDesc should receive inner values (window client area rectangle). Because of that actual area on screen was smaller than swap chain buffer and the result of rendering was presumably resampled to fit the rectangle, which was introducing the aliasing after MSAA was already applied.

    Fixing the issue gave a much cleaner result (4x MSAA is applied here):

    But the question with texture aliasing is still open:

Page 1 of 1 (4 posts) Previous Discussion Next Discussion