• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Such1

DX11
DirectX 11 Pixel Shader Problem

5 posts in this topic

Hi, I was reading the www.directxtutorial.com and tried to put texture on my cube.
The only problem is that the pixel shader always receive (0.0f, 0.0f) as texture coordinates. the vertex shader gets the right one.
I have no idea of what can cause that.

And another problem with my code is that the far-plane is too close, around 1.5f and I can't change it, I tried changing all the values that have something to do with the z buffer or the view matrix but still.

The partial code is here:

Obs: CStaticModel is not the problem, it was working before I start using DirectX 11

[CODE]
#pragma once
#include <Windows.h>
#include <d3d11.h>
#include <d3dx11.h>
#include <d3dx10.h>
#include "StaticModel.h"
#define DIRECTX_WINDOW_CLASSNAME "Such DirectX class Name"
#define DIRECTX_WINDOW_CAPTION "Such DirectX caption"
struct VERTEX
{
FLOAT X, Y, Z; // position
FLOAT U, V;
};
struct CONSTANT_BUFFER
{
D3DXMATRIX finalMatrix;
};
class CSDirectX
{
protected:
//WIN32 Initialization and management
HWND m_hWnd;
HINSTANCE m_hInstance;
UINT m_screenWidth;
UINT m_screenHeight;
void createWindow(const char* caption, HINSTANCE hInstance);
bool registerClass(const char* className)
{
WNDCLASSEX wc;
ZeroMemory(&wc, sizeof(WNDCLASSEX));
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = staticProc;
wc.hInstance = m_hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.lpszClassName = className;
return RegisterClassEx(&wc) != 0;
}
void showWindow(bool show)
{
if(show)
ShowWindow(m_hWnd, SW_SHOW);
else
ShowWindow(m_hWnd, SW_HIDE);
}
static LRESULT CALLBACK staticProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
} break;
}
return DefWindowProc (hWnd, message, wParam, lParam);
return 0;
}
//DirectX initialization and management
ID3D11Device* m_device;
IDXGISwapChain* m_swapChain;
ID3D11DeviceContext* m_deviceContext;
ID3D11RenderTargetView* m_backBuffer;
ID3D11DepthStencilView* m_zBuffer;
D3DXCOLOR m_backgroundColor;
ID3D11VertexShader* m_vertexShader;
ID3D11PixelShader* m_pixelShader;
ID3D11Buffer* m_vertexBuffer;
ID3D11InputLayout* m_inputLayout;
ID3D11Buffer* m_constantBuffer;
ID3D11ShaderResourceView* textureTest;
CStaticModel teste;
D3DXMATRIX m_cameraPosition;
D3DXMATRIX m_projectionMatrix;
void init3D();
void clean3D();
void initPipeline();
void initGraphics();
void setCameraPosition(D3DXVECTOR3* position, D3DXVECTOR3* lookAt, D3DXVECTOR3* upDirection = &D3DXVECTOR3(0.0f, 1.0f, 0.0f));
public:
void init(HINSTANCE hInstance)
{
createWindow(DIRECTX_WINDOW_CAPTION, hInstance);
init3D();
}
void setBackgroundColor(D3DXCOLOR color){m_backgroundColor = color;}
void setFullscreen(BOOL fullscreen=true){m_swapChain->SetFullscreenState(fullscreen, NULL);}
void renderLoop()
{
m_deviceContext->ClearRenderTargetView(m_backBuffer, m_backgroundColor);
m_deviceContext->ClearDepthStencilView(m_zBuffer, D3D11_CLEAR_DEPTH, 1.0f, 0);
D3DXMATRIX rotation;
D3DXMATRIX translation;
static float a = 0.0f;a += 0.0001f;
D3DXMatrixRotationY(&rotation, a);
D3DXMATRIX final = rotation*m_cameraPosition*m_projectionMatrix*translation;
UINT stride = sizeof(VERTEX);
UINT offset = 0;
m_deviceContext->IASetVertexBuffers(0, 1, &m_vertexBuffer, &stride, &offset);
// select which primtive type we are using
m_deviceContext->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
// draw the vertex buffer to the back buffer
D3DXMatrixScaling(&translation, 0.5f, 0.5f, 0.5f);
final = rotation*m_cameraPosition*m_projectionMatrix*translation;
m_deviceContext->UpdateSubresource(m_constantBuffer, 0, 0, &final, 0, 0);
m_deviceContext->PSSetShaderResources(0, 1, &textureTest);
teste.draw(this);
m_swapChain->Present(0, 0);
}
void createVertexBuffer(ID3D11Buffer** pBuffer, size_t size);
void createIndexBuffer(ID3D11Buffer** pBuffer, size_t size);
void createConstantBuffer(ID3D11Buffer** pBuffer, size_t size);
ID3D11DeviceContext* context()
{
return m_deviceContext;
}
CSDirectX(void);
~CSDirectX(void);
};
[/CODE]

[CODE]
#include "SDirectX.h"
#pragma comment (lib, "d3d11.lib")
#pragma comment (lib, "d3dx11.lib")
#pragma comment (lib, "d3dx10.lib")
CSDirectX::CSDirectX(void)
{
m_hWnd = 0;
m_hInstance = 0;
m_screenWidth = 800;
m_screenHeight = 600;
m_swapChain = 0;
m_device = 0;
m_deviceContext = 0;
m_backBuffer = 0;
m_vertexShader = 0;
m_pixelShader = 0;
m_constantBuffer = 0;
m_zBuffer = 0;
m_backgroundColor = D3DXCOLOR(0.0f, 0.0f, 0.0f, 1.0f);
}
CSDirectX::~CSDirectX(void)
{
textureTest->Release();
clean3D();
}
void CSDirectX::init3D()
{
DXGI_SWAP_CHAIN_DESC scd;
// clear out the struct for use
ZeroMemory(&scd, sizeof(DXGI_SWAP_CHAIN_DESC));
// fill the swap chain description struct
scd.BufferCount = 1; // one back buffer
scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; // use 32-bit color
scd.BufferDesc.Width = m_screenWidth;
scd.BufferDesc.Height = m_screenHeight;
scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; // how swap chain is to be used
scd.OutputWindow = m_hWnd; // the window to be used
scd.SampleDesc.Count = 4; // how many multisamples
scd.Windowed = TRUE; // windowed/full-screen mode
scd.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;
// create a device, device context and swap chain using the information in the scd struct
D3D11CreateDeviceAndSwapChain(NULL,
D3D_DRIVER_TYPE_HARDWARE,
NULL,
NULL,
NULL,
NULL,
D3D11_SDK_VERSION,
&scd,
&m_swapChain,
&m_device,
NULL,
&m_deviceContext);
ID3D11Texture2D *pBackBuffer;
m_swapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&pBackBuffer);
// use the back buffer address to create the render target
m_device->CreateRenderTargetView(pBackBuffer, NULL, &m_backBuffer);
pBackBuffer->Release();
D3D11_TEXTURE2D_DESC texd;
ZeroMemory(&texd, sizeof(texd));
texd.Width = m_screenWidth;
texd.Height = m_screenHeight;
texd.ArraySize = 1;
texd.MipLevels = 1;
texd.SampleDesc.Count = 4;
texd.Format = DXGI_FORMAT_D32_FLOAT;
texd.BindFlags = D3D11_BIND_DEPTH_STENCIL;
ID3D11Texture2D* depthBuffer;
m_device->CreateTexture2D(&texd, NULL, &depthBuffer);
D3D11_DEPTH_STENCIL_VIEW_DESC dsvd;
ZeroMemory(&dsvd, sizeof(dsvd));
dsvd.Format = DXGI_FORMAT_D32_FLOAT;
dsvd.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2DMS;
m_device->CreateDepthStencilView(depthBuffer, &dsvd, &m_zBuffer);
depthBuffer->Release();
m_deviceContext->OMSetRenderTargets(1, &m_backBuffer, m_zBuffer);
D3D11_VIEWPORT viewport;
ZeroMemory(&viewport, sizeof(D3D11_VIEWPORT));
viewport.TopLeftX = 0;
viewport.TopLeftY = 0;
viewport.Width = (float)m_screenWidth;
viewport.Height = (float)m_screenHeight;
viewport.MinDepth = 0.0f; // the closest an object can be on the depth buffer is 0.0
viewport.MaxDepth = 1.0f; // the farthest an object can be on the depth buffer is 1.0
m_deviceContext->RSSetViewports(1, &viewport);

teste.generateCube(0.5f, 0);
teste.loadBuffers(this);
initPipeline();
initGraphics();
}
void CSDirectX::clean3D()
{
m_swapChain->SetFullscreenState(FALSE, NULL);
if(m_swapChain)
m_swapChain->Release();
if(m_device)
m_device->Release();
if(m_deviceContext)
m_deviceContext->Release();
if(m_backBuffer)
m_backBuffer->Release();
if(m_vertexShader)
m_vertexShader->Release();
if(m_pixelShader)
m_pixelShader->Release();
if(m_constantBuffer)
m_constantBuffer->Release();
if(m_zBuffer)
m_zBuffer->Release();
m_swapChain = 0;
m_device = 0;
m_deviceContext = 0;
m_backBuffer = 0;
m_zBuffer = 0;
m_vertexShader = 0;
m_pixelShader = 0;
m_constantBuffer = 0;
}
void CSDirectX::initPipeline()
{
ID3D10Blob *VS, *PS;
D3DX11CompileFromFile("shaders.hlsl", 0, 0, "VShader", "vs_5_0", 0, 0, 0, &VS, 0, 0);
D3DX11CompileFromFile("shaders.hlsl", 0, 0, "PShader", "ps_5_0", 0, 0, 0, &PS, 0, 0);
m_device->CreateVertexShader(VS->GetBufferPointer(), VS->GetBufferSize(), NULL, &m_vertexShader);
m_device->CreatePixelShader(PS->GetBufferPointer(), PS->GetBufferSize(), NULL, &m_pixelShader);
m_deviceContext->VSSetShader(m_vertexShader, 0, 0);
m_deviceContext->PSSetShader(m_pixelShader, 0, 0);
createConstantBuffer(&m_constantBuffer, 64);//Multiple of 16
m_deviceContext->VSSetConstantBuffers(0, 1, &m_constantBuffer);
// create the input layout object
D3D11_INPUT_ELEMENT_DESC ied[] =
{
{"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0},
{"TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0},
//{"COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0},
};
m_device->CreateInputLayout(ied, 2, VS->GetBufferPointer(), VS->GetBufferSize(), &m_inputLayout);
m_deviceContext->IASetInputLayout(m_inputLayout);
}
void CSDirectX::initGraphics()
{
createVertexBuffer(&m_vertexBuffer, sizeof(VERTEX)*3);
VERTEX OurVertices[] =
{
{0.0f, 0.5f, 0.0f, D3DXCOLOR(1.0f, 0.0f, 0.0f, 1.0f)},
{0.45f, -0.5, 0.0f, D3DXCOLOR(0.0f, 1.0f, 0.0f, 1.0f)},
{-0.45f, -0.5f, 0.0f, D3DXCOLOR(0.0f, 0.0f, 1.0f, 1.0f)}
};
D3D11_MAPPED_SUBRESOURCE ms;
m_deviceContext->Map(m_vertexBuffer, NULL, D3D11_MAP_WRITE_DISCARD, NULL, &ms); // map the buffer
memcpy(ms.pData, OurVertices, sizeof(OurVertices)); // copy the data
m_deviceContext->Unmap(m_vertexBuffer, NULL);
D3DXMatrixPerspectiveFovLH(&m_projectionMatrix,
D3DXToRadian(45), // the horizontal field of view
(FLOAT)m_screenWidth / (FLOAT)m_screenHeight, // aspect ratio
0.1f, // the near view-plane
100.0f); // the far view-plane
setCameraPosition(&D3DXVECTOR3(0.0f, 1.0f, 1.5f), &D3DXVECTOR3(0.0f, 0.0f, 0.0f));
D3DX11CreateShaderResourceViewFromFile(m_device, // the Direct3D device
"Wood1.png", // load Wood.png in the local folder
NULL, // no additional information
NULL, // no multithreading
&textureTest, // address of the shader-resource-view
NULL); // no multithreading
}
void CSDirectX::createVertexBuffer(ID3D11Buffer** pBuffer, size_t size)
{
D3D11_BUFFER_DESC bd;
ZeroMemory(&bd, sizeof(bd));
bd.Usage = D3D11_USAGE_DYNAMIC; // write access access by CPU and GPU
bd.ByteWidth = size; // size is the VERTEX struct * 3
bd.BindFlags = D3D11_BIND_VERTEX_BUFFER; // use as a vertex buffer
bd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; // allow CPU to write in buffer
m_device->CreateBuffer(&bd, NULL, pBuffer); // create the buffer
}
void CSDirectX::createIndexBuffer(ID3D11Buffer** pBuffer, size_t size)
{
D3D11_BUFFER_DESC bd;
ZeroMemory(&bd, sizeof(bd));
bd.Usage = D3D11_USAGE_DYNAMIC;
bd.ByteWidth = size;
bd.BindFlags = D3D11_BIND_INDEX_BUFFER;
bd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
m_device->CreateBuffer(&bd, NULL, pBuffer);
}
void CSDirectX::createConstantBuffer(ID3D11Buffer** pBuffer, size_t size)
{
D3D11_BUFFER_DESC bd;
ZeroMemory(&bd, sizeof(bd));
bd.Usage = D3D11_USAGE_DEFAULT;
bd.ByteWidth = size;
bd.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
m_device->CreateBuffer(&bd, NULL, pBuffer);
}
void CSDirectX::setCameraPosition(D3DXVECTOR3* position, D3DXVECTOR3* lookAt, D3DXVECTOR3* upDirection)
{
D3DXMatrixLookAtLH(&m_cameraPosition,
position,
lookAt,
upDirection);
}
void CSDirectX::createWindow(const char* caption, HINSTANCE hInstance)
{
m_hInstance = hInstance;
registerClass(DIRECTX_WINDOW_CLASSNAME);
m_hWnd = CreateWindow(DIRECTX_WINDOW_CLASSNAME, caption, WS_OVERLAPPEDWINDOW|WS_VISIBLE, 300, 300, m_screenWidth, m_screenHeight, NULL, NULL, hInstance, NULL);
}
[/CODE]
0

Share this post


Link to post
Share on other sites
If the vertex shader gets the right texture coordinates the problem is either in the vertex or the pixel shader... post them so we can help.

Regarding the far clip plane distance, it should be at 100.0f, are you sure its at 1.5f?
0

Share this post


Link to post
Share on other sites
About the distance I am not sure, but I created a rotating cube and the most distant vertex was not visible.

here is the shader:
[CODE]
cbuffer ConstantBuffer
{
float4x4 finalMatrix;
};
Texture2D Texture;
SamplerState ss;
struct VOut
{
float4 color : COLOR;
float4 position : SV_POSITION;
float2 texcoord : TEXCOORD;
};
VOut VShader(float4 position : POSITION, float2 texcoord : TEXCOORD)
{
VOut output;
output.position = mul(finalMatrix, position);
output.texcoord = texcoord;
output.color = (1.0f, 1.0f, 1.0f, 1.0f);
output.color.xy = texcoord;
return output;
}

float4 PShader(float4 color : COLOR, float2 texcoord : TEXCOORD) : SV_TARGET
{
return Texture.Sample(ss, texcoord);
}
[/CODE]

I am not able to test the code right now because I changed my computer with my brother, and in this computer the driver crashes when I compile...great
The driver crashes when the code gets to swapChain->Present()...anyone knows what is going on?

Ok, the program doesn't crash anymore, but I still can't render the texture Edited by Such1
0

Share this post


Link to post
Share on other sites
Ok, it is drawing the texture now, but the texture quality is really bad, blurred on the sides and other problems, how do I fix that?? Edited by Such1
0

Share this post


Link to post
Share on other sites
Blurry at boundaries sounds like trilinear filtering when looking from low angles.
Maybe the filter isn’t ideal. What about anisotropic filtering?

[CODE]D3D11_SAMPLER_DESC SamplerStateTexture = {
D3D11_FILTER_ANISOTROPIC,
D3D11_TEXTURE_ADDRESS_WRAP,
D3D11_TEXTURE_ADDRESS_WRAP,
D3D11_TEXTURE_ADDRESS_WRAP,
LodBias, MaxAnisotropy,
D3D11_COMPARISON_ALWAYS,
0, 0, 0, 0, 0,
D3D11_FLOAT32_MAX
};
pD3D11Device->CreateSamplerState(&SamplerStateTexture, &mSamplerTexture);[/CODE]

You can play with the LodBias and with MaxAnisotropy.
The LodBias moves your sample to more detailed mipmap levels at the risk of aliasing and more texture cache misses.
MaxAnisotropy increases the quality as well. The maximum value is hardware dependent, but probably somewhere between 1 and 16.

Could you show us a picture, such that we can get a better idea of what's wrong with your textures? Thanks!

Best regards
1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Similar Content

    • By bowerbirdcn
      hi, guys, how to understand the math used in CDXUTDirectionWidget ::UpdateLightDir 
      the  following code snippet is taken from MS DXTU source code
       
        D3DXMATRIX mInvView;
          D3DXMatrixInverse( &mInvView, NULL, &m_mView );
          mInvView._41 = mInvView._42 = mInvView._43 = 0;
          D3DXMATRIX mLastRotInv;
          D3DXMatrixInverse( &mLastRotInv, NULL, &m_mRotSnapshot );
          D3DXMATRIX mRot = *m_ArcBall.GetRotationMatrix();
          m_mRotSnapshot = mRot;
          // Accumulate the delta of the arcball's rotation in view space.
          // Note that per-frame delta rotations could be problematic over long periods of time.
          m_mRot *= m_mView * mLastRotInv * mRot * mInvView;
          // Since we're accumulating delta rotations, we need to orthonormalize 
          // the matrix to prevent eventual matrix skew
          D3DXVECTOR3* pXBasis = ( D3DXVECTOR3* )&m_mRot._11;
          D3DXVECTOR3* pYBasis = ( D3DXVECTOR3* )&m_mRot._21;
          D3DXVECTOR3* pZBasis = ( D3DXVECTOR3* )&m_mRot._31;
          D3DXVec3Normalize( pXBasis, pXBasis );
          D3DXVec3Cross( pYBasis, pZBasis, pXBasis );
          D3DXVec3Normalize( pYBasis, pYBasis );
          D3DXVec3Cross( pZBasis, pXBasis, pYBasis );
       
       
      https://github.com/Microsoft/DXUT/blob/master/Optional/DXUTcamera.cpp
    • By YixunLiu
      Hi,
      I have a surface mesh and I want to use a cone to cut a hole on the surface mesh.
      Anybody know a fast method to calculate the intersected boundary of these two geometries?
       
      Thanks.
       
      YL
       
    • By hiya83
      Hi, I tried searching for this but either I failed or couldn't find anything. I know there's D11/D12 interop and there are extensions for GL/D11 (though not very efficient). I was wondering if there's any Vulkan/D11 or Vulkan/D12 interop?
      Thanks!
    • By lonewolff
      Hi Guys,
      I am just wondering if it is possible to acquire the address of the backbuffer if an API (based on DX11) only exposes the 'device' and 'context' pointers?
      Any advice would be greatly appreciated
    • By MarcusAseth
      bool InitDirect3D::Init() { if (!D3DApp::Init()) { return false; } //Additional Initialization //Disable Alt+Enter Fullscreen Toggle shortkey IDXGIFactory* factory; CreateDXGIFactory(__uuidof(IDXGIFactory), reinterpret_cast<void**>(&factory)); factory->MakeWindowAssociation(mhWindow, DXGI_MWA_NO_WINDOW_CHANGES); factory->Release(); return true; }  
      As stated on the title and displayed on the code above, regardless of it Alt+Enter still takes effect...
      I recall something from the book during the swapChain creation, where in order to create it one has to use the same factory used to create the ID3D11Device, therefore I tested and indeed using that same factory indeed it work.
      How is that one particular factory related to my window and how come the MakeWindowAssociation won't take effect with a newly created factory?
      Also what's even the point of being able to create this Factories if they won't work,?(except from that one associated with the ID3D11Device) 
  • Popular Now