rate up
0
rate down
Keeps Failing To Compile The Shader Code
this is my code: (sorry if it is disorganized) #define PI 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148f #include <DXGI.h> #include <D3D11.h> #include <D3DX11.h> #include <D3DX10.h> #include <xnamath.h> #include "resource.h" int mwidth, mheight; HWND window; LPCSTR appName; HRESULT hr; IDXGISwapChain *sc; ID3D11Device *device; ID3D11DeviceContext *context; ID3D11RenderTargetView *rtv; ID3D11Buffer *triIndxBuffer; ID3D11Buffer *triVertBuffer; ID3D11DepthStencilView *dsv; ID3D11Texture2D *dsb; ID3D11VertexShader *triVS; ID3D11PixelShader *triPS; ID3D10Blob *VS_Buffer; ID3D10Blob *PS_Buffer; ID3D11InputLayout *vertLayout; ID3D11Buffer *pob; ID3D11RasterizerState *wf; ID3D11ShaderResourceView *cubesTexture; ID3D11SamplerState *cubesTexSamplerState; XMMATRIX MVP; XMMATRIX cube1World; XMMATRIX cube2World; XMMATRIX model; XMMATRIX view; XMMATRIX projection; XMVECTOR camPosition; XMVECTOR camTarget; XMVECTOR camUp; XMMATRIX rotation; XMMATRIX scale; XMMATRIX translation; float rot = 0.01f; struct perOBJ { XMMATRIX MVP; }; perOBJ po; D3D11_INPUT_ELEMENT_DESC layout[] = { { "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 } }; UINT elements = ARRAYSIZE(layout); LRESULT WINAPI WinProc(HWND window, UINT msg, WPARAM wParam, LPARAM lParam) { switch (msg) { case WM_QUIT: DestroyWindow(window); PostQuitMessage(0); return 0; case WM_DESTROY: PostQuitMessage(0); return 0; case WM_CLOSE: DestroyWindow(window); PostQuitMessage(0); return 0; case WM_KEYDOWN: if (wParam == VK_ESCAPE) { DestroyWindow(window); PostQuitMessage(0); return 0; } default: return DefWindowProc(window, msg, wParam, lParam); } } namespace Math { struct Vertex { XMFLOAT3 pos; XMFLOAT2 texCoord; Vertex() {} Vertex(float x, float y, float z, float u, float v) : pos(x, y, z), texCoord(u, v) {} }; } namespace DX { bool init(HINSTANCE instance) { DXGI_MODE_DESC bufferDesc; ZeroMemory(&bufferDesc, sizeof(DXGI_MODE_DESC)); bufferDesc.Width = mwidth; bufferDesc.Height = mheight; bufferDesc.RefreshRate.Numerator = 60; bufferDesc.RefreshRate.Denominator = 1; bufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; bufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED; bufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED; DXGI_SWAP_CHAIN_DESC scd; ZeroMemory(&scd, sizeof(DXGI_SWAP_CHAIN_DESC)); scd.BufferDesc = bufferDesc; scd.SampleDesc.Count = 1; scd.SampleDesc.Quality = 0; scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; scd.BufferCount = 1; scd.OutputWindow = window; scd.Windowed = TRUE; scd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD; hr = D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, NULL, NULL, NULL, D3D11_SDK_VERSION, &scd, &sc, &device, NULL, &context); ID3D11Texture2D *backBuffer; hr = sc->GetBuffer(0, __uuidof(ID3D11Texture2D), (void**)&backBuffer); hr = device->CreateRenderTargetView(backBuffer, NULL, &rtv); backBuffer->Release(); D3D11_TEXTURE2D_DESC dsd; dsd.Width = mwidth; dsd.Height = mheight; dsd.MipLevels = 1; dsd.ArraySize = 1; dsd.Format = DXGI_FORMAT_D24_UNORM_S8_UINT; dsd.SampleDesc.Count = 1; dsd.SampleDesc.Quality = 0; dsd.Usage = D3D11_USAGE_DEFAULT; dsd.BindFlags = D3D11_BIND_DEPTH_STENCIL; dsd.CPUAccessFlags = NULL; dsd.MiscFlags = NULL; device->CreateTexture2D(&dsd, NULL, &dsb); device->CreateDepthStencilView(dsb, NULL, &dsv); context->OMSetRenderTargets(1, &rtv, dsv); return true; } bool initScene() { hr = D3DX11CompileFromFile("triSH.fx", NULL, NULL, "VS", "vs_4_0", NULL, NULL, NULL, &VS_Buffer, NULL, NULL); hr = D3DX11CompileFromFile("triSH.fx", NULL, NULL, "PS", "ps_4_0", NULL, NULL, NULL, &PS_Buffer, NULL, NULL); hr = D3DX11CreateShaderResourceViewFromFile(device, "Sun.png", NULL, NULL, &cubesTexture, NULL); if (FAILED(hr)) { MessageBox(NULL, "Failed to compile shaders", "ERROR", MB_OK); return false; } hr = device->CreateVertexShader(VS_Buffer->GetBufferPointer(), VS_Buffer->GetBufferSize(), NULL, &triVS); hr = device->CreatePixelShader(PS_Buffer->GetBufferPointer(), PS_Buffer->GetBufferSize(), NULL, &triPS); if (FAILED(hr)) { MessageBox(NULL, "Failed to create shaders", "ERROR", MB_OK); return false; } context->VSSetShader(triVS, NULL, NULL); context->PSSetShader(triPS, NULL, NULL); Math::Vertex v[] = { Math::Vertex(-1.0f, -1.0f, -1.0f, 0.0f, 1.0f), Math::Vertex(-1.0f, 1.0f, -1.0f, 0.0f, 0.0f), Math::Vertex(1.0f, 1.0f, -1.0f, 1.0f, 0.0f), Math::Vertex(1.0f, -1.0f, -1.0f, 1.0f, 1.0f), // Back Face Math::Vertex(-1.0f, -1.0f, 1.0f, 1.0f, 1.0f), Math::Vertex(1.0f, -1.0f, 1.0f, 0.0f, 1.0f), Math::Vertex(1.0f, 1.0f, 1.0f, 0.0f, 0.0f), Math::Vertex(-1.0f, 1.0f, 1.0f, 1.0f, 0.0f), // Top Face Math::Vertex(-1.0f, 1.0f, -1.0f, 0.0f, 1.0f), Math::Vertex(-1.0f, 1.0f, 1.0f, 0.0f, 0.0f), Math::Vertex(1.0f, 1.0f, 1.0f, 1.0f, 0.0f), Math::Vertex(1.0f, 1.0f, -1.0f, 1.0f, 1.0f), // Bottom Face Math::Vertex(-1.0f, -1.0f, -1.0f, 1.0f, 1.0f), Math::Vertex(1.0f, -1.0f, -1.0f, 0.0f, 1.0f), Math::Vertex(1.0f, -1.0f, 1.0f, 0.0f, 0.0f), Math::Vertex(-1.0f, -1.0f, 1.0f, 1.0f, 0.0f), // Left Face Math::Vertex(-1.0f, -1.0f, 1.0f, 0.0f, 1.0f), Math::Vertex(-1.0f, 1.0f, 1.0f, 0.0f, 0.0f), Math::Vertex(-1.0f, 1.0f, -1.0f, 1.0f, 0.0f), Math::Vertex(-1.0f, -1.0f, -1.0f, 1.0f, 1.0f), // Right Face Math::Vertex(1.0f, -1.0f, -1.0f, 0.0f, 1.0f), Math::Vertex(1.0f, 1.0f, -1.0f, 0.0f, 0.0f), Math::Vertex(1.0f, 1.0f, 1.0f, 1.0f, 0.0f), Math::Vertex(1.0f, -1.0f, 1.0f, 1.0f, 1.0f) }; DWORD indices[] = { // Front Face 0, 1, 2, 0, 2, 3, // Back Face 4, 5, 6, 4, 6, 7, // Top Face 8, 9, 10, 8, 10, 11, // Bottom Face 12, 13, 14, 12, 14, 15, // Left Face 16, 17, 18, 16, 18, 19, // Right Face 20, 21, 22, 20, 22, 23 }; D3D11_BUFFER_DESC ibd; ZeroMemory(&ibd, sizeof(ibd)); ibd.Usage = D3D11_USAGE_DEFAULT; ibd.ByteWidth = sizeof(DWORD) * 12 * 3; ibd.BindFlags = D3D11_BIND_INDEX_BUFFER; ibd.CPUAccessFlags = NULL; ibd.MiscFlags = NULL; D3D11_SUBRESOURCE_DATA iInitData; iInitData.pSysMem = indices; device->CreateBuffer(&ibd, &iInitData, &triIndxBuffer); context->IASetIndexBuffer(triIndxBuffer, DXGI_FORMAT_R32_UINT, 0); D3D11_BUFFER_DESC vbd; ZeroMemory(&vbd, sizeof(vbd)); vbd.Usage = D3D11_USAGE_DEFAULT; vbd.ByteWidth = sizeof(Math::Vertex) * 24; vbd.BindFlags = D3D11_BIND_VERTEX_BUFFER; vbd.CPUAccessFlags = NULL; vbd.MiscFlags = NULL; D3D11_SUBRESOURCE_DATA vbdata; ZeroMemory(&vbdata, sizeof(vbdata)); vbdata.pSysMem = v; hr = device->CreateBuffer(&vbd, &vbdata, &triVertBuffer); if (FAILED(hr)) { MessageBox(NULL, "Failed to create buffer", "ERROR", MB_OK); return false; } UINT stride = sizeof(Math::Vertex); UINT offset = 0; context->IASetVertexBuffers(0, 1, &triVertBuffer, &stride, &offset); device->CreateInputLayout(layout, elements, VS_Buffer->GetBufferPointer(), VS_Buffer->GetBufferSize(), &vertLayout); context->IASetInputLayout(vertLayout); context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); D3D11_VIEWPORT viewport; ZeroMemory(&viewport, sizeof(viewport)); viewport.TopLeftX = 0; viewport.TopLeftY = 0; viewport.Width = mwidth; viewport.Height = mheight; viewport.MinDepth = 0; viewport.MaxDepth = 1; context->RSSetViewports(1, &viewport); D3D11_BUFFER_DESC cbbd; ZeroMemory(&cbbd, sizeof(cbbd)); cbbd.Usage = D3D11_USAGE_DEFAULT; cbbd.ByteWidth = sizeof(perOBJ); cbbd.BindFlags = D3D11_BIND_CONSTANT_BUFFER; cbbd.CPUAccessFlags = NULL; cbbd.MiscFlags = NULL; hr = device->CreateBuffer(&cbbd, NULL, &pob); camPosition = XMVectorSet(0, 3, -8, 0); camTarget = XMVectorSet(0, 0, 0, 0); camUp = XMVectorSet(0, 1, 0, 0); view = XMMatrixLookAtLH(camPosition, camTarget, camUp); projection = XMMatrixPerspectiveFovLH(0.4f * PI, (float)mwidth / mheight, 1, 1000); D3D11_RASTERIZER_DESC wfd; ZeroMemory(&wfd, sizeof(wfd)); wfd.FillMode = D3D11_FILL_WIREFRAME; wfd.CullMode = D3D11_CULL_NONE; hr = device->CreateRasterizerState(&wfd, &wf); D3D11_SAMPLER_DESC sampDesc; ZeroMemory(&sampDesc, sizeof(sampDesc)); sampDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR; sampDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP; sampDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP; sampDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP; sampDesc.ComparisonFunc = D3D11_COMPARISON_NEVER; sampDesc.MinLOD = 0; sampDesc.MaxLOD = D3D11_FLOAT32_MAX; hr = device->CreateSamplerState(&sampDesc, &cubesTexSamplerState); return true; } void releaseOBJS() { sc->Release(); device->Release(); context->Release(); triVertBuffer->Release(); triIndxBuffer->Release(); triVS->Release(); triPS->Release(); VS_Buffer->Release(); PS_Buffer->Release(); vertLayout->Release(); dsv->Release(); dsb->Release(); pob->Release(); } void update() { rot += .0001f; if (rot >= 360) { rot = 0; } cube1World = XMMatrixIdentity(); XMVECTOR rotaxis = XMVectorSet(0, 1, 0, 0); rotation = XMMatrixRotationAxis(rotaxis, rot); translation = XMMatrixTranslation(0, 0, 4); cube1World = translation * rotation; cube2World = XMMatrixIdentity(); rotation = XMMatrixRotationAxis(rotaxis, -rot); scale = XMMatrixScaling(1.3f, 1.3f, 1.3f); cube2World = rotation * scale; } void draw() { D3DXCOLOR bg(0, 0, 0, 0); context->ClearRenderTargetView(rtv, bg); context->ClearDepthStencilView(dsv, D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1, 0); MVP = cube1World * view * projection; po.MVP = XMMatrixTranspose(MVP); context->UpdateSubresource(pob, 0, NULL, &po, 0, 0); context->VSSetConstantBuffers(0, 1, &pob); context->PSSetShaderResources(0, 1, &cubesTexture); context->PSSetSamplers(0, 1, &cubesTexSamplerState); context->DrawIndexed(36, 0, 0); MVP = cube2World * view * projection; po.MVP = XMMatrixTranspose(MVP); context->UpdateSubresource(pob, 0, NULL, &po, 0, 0); context->VSSetConstantBuffers(0, 1, &pob); context->PSSetShaderResources(0, 1, &cubesTexture); context->PSSetSamplers(0, 1, &cubesTexSamplerState); context->DrawIndexed(36, 0, 0); sc->Present(0, 0); } } namespace Windows { bool init(HINSTANCE instance, int width, int height, LPCSTR title) { mwidth = width; mheight = height; appName = title; WNDCLASSEX wc; wc.cbClsExtra = NULL; wc.cbSize = sizeof(WNDCLASSEX); wc.cbWndExtra = NULL; wc.hbrBackground = (HBRUSH)8; wc.hCursor = LoadCursor(instance, IDC_ARROW); wc.hIcon = LoadIcon(NULL, MAKEINTRESOURCE(101)); wc.hIconSm = wc.hIcon; wc.hInstance = instance; wc.lpfnWndProc = WinProc; wc.lpszClassName = appName; wc.lpszMenuName = NULL; wc.style = CS_HREDRAW | CS_VREDRAW; if (!RegisterClassEx(&wc)) { MessageBox(NULL, "Class Register Failed", "ERROR", MB_OK); return false; } int x = (GetSystemMetrics(SM_CXSCREEN) - width) / 2; int y = (GetSystemMetrics(SM_CYSCREEN) - height) / 2; window = CreateWindowEx(NULL, appName, appName, WS_OVERLAPPEDWINDOW, x, y, width, height, NULL, NULL, instance, NULL); if (!window) { MessageBox(NULL, "Window Register Failed", "ERROR", MB_OK); return false; } ShowWindow(window, TRUE); UpdateWindow(window); return true; } void loop() { MSG msg; ZeroMemory(&msg, sizeof(MSG)); while (msg.message != WM_QUIT) { if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { TranslateMessage(&msg); DispatchMessage(&msg); } else { DX::update(); DX::draw(); } } } } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int mCmdShow) { if (!Windows::init(hInstance, 1024, 768, "Portal Stories: The Scientific Method")) { return -1; } if (!DX::init(hInstance)) { return -1; } if (!DX::initScene()) { return -1; } Windows::loop(); DX::releaseOBJS(); return 0; } And here is my shader code: cbuffer po { float4x4 MVP; }; Texture2D objTexture; SamplerState objSampState; struct VS_OUTPUT { float4 Pos : SV_POSITION; float2 TexCoord : TEXCOORD; }; VS_OUTPUT VS(float4 inPos : POSITION, float2 inTexCoord : TEXCOORD) { VS_OUTPUT output; output.Pos = mul(inPos, MVP); output.TexCoord = inTexCoord; return output; } float4 PS(VS_OUTPUT input) : SV_TARGET { return objTexture.Sample(objSampState, input.TexCoord); }
Comments
could you edit your question, highlight your code and then press tab? that will move it all into a code block
on Dec 15 `15
iedoc
also, what's the error you are getting? is it failing to compile while you run your code? does it break on D3DX11CompileFromFile, or does D3DX11CompileFromFile return FAILED? or does it give you an error when you click build?
on Dec 15 `15
iedoc
it is failing to compile the shaders
on Dec 15 `15
Caseofgames
how do you know it's failing? does it say it's failing? if it says something, anything, can you update your question so i could see the actual error thats happening?
on Dec 17 `15
iedoc
in the meantime, here are a couple things i have seen that cause the shader to fail to compile. visual studio will try to compile hlsl files using fxc.exe. by default it looks for a main method, in yours i see your have VS and PS as your function names. you can tell visual studio to not compile the shaders by right clicking on the filename in the solution explorer and going to properties and select yes for excluded from build and select does not participate in build for the compiler type. Another possibility is that your graphics card does not support hsls version 4.0 and you'll have to use a lower version
on Dec 17 `15
iedoc
Sign in to answer!