Jump to content
  • Advertisement

JWColeman

Member
  • Content Count

    92
  • Joined

  • Last visited

Community Reputation

102 Neutral

About JWColeman

  • Rank
    Member

Personal Information

  • Role
    Amateur / Hobbyist
  • Interests
    Programming

Recent Profile Visitors

1366 profile views
  1. JWColeman

    Silly Input Layout Problem

    You're not wrong chuck, I finally got it working... Instance * instances; UINT instanceCount = 1; instances = new Instance[instanceCount]; D3DXMatrixTranslation(&instances[0].position, 100, 0, 0); D3DXMatrixTranspose(&instances[0].position, &instances[0].position); for (int i = 0; i < instanceCount; i++) { InstanceBuffer.data_container.push_back(instances[i]); } InstanceBuffer.Initialize(dev, D3D11_USAGE_DYNAMIC, D3D11_BIND_VERTEX_BUFFER, D3D11_CPU_ACCESS_WRITE); InstanceBuffer.Update(devcon); 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 }, { "INSTANCE", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 1, 0, D3D11_INPUT_PER_INSTANCE_DATA, 1 }, { "INSTANCE", 1, DXGI_FORMAT_R32G32B32A32_FLOAT, 1, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_INSTANCE_DATA, 1 }, { "INSTANCE", 2, DXGI_FORMAT_R32G32B32A32_FLOAT, 1, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_INSTANCE_DATA, 1 }, { "INSTANCE", 3, DXGI_FORMAT_R32G32B32A32_FLOAT, 1, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_INSTANCE_DATA, 1 }, }; hr = dev->CreateInputLayout(ied, ARRAYSIZE(ied), Shader->GetBufferPointer(), Shader->GetBufferSize(), &pLayout); Finally the shader multiplications you are mentioning: VOut VShader(float4 position : POSITION, float4 color : COLOR, float4x4 instancePosition : INSTANCE) { VOut output; // Apply all instance position translations //position.x += instancePosition.x; //position.y += instancePosition.y; //position.z += instancePosition.z; //output.position = position; //output.position = mul(position, world); // Apply world translation matrix output.position = position; output.position = mul(output.position, instancePosition); // apply instance translation matrix output.position = mul(output.position, projection); // Apply ortho projection matrix output.color = color; // Apply color from vertex input return output; } This draws my single instanced quad :).
  2. JWColeman

    Silly Input Layout Problem

    Hey you got it man, that fixed the input layout problem, now unfortunately I don't think my shader is correct :(.
  3. JWColeman

    Silly Input Layout Problem

    Okay, switched it up, makes sense what you said: Same error
  4. JWColeman

    Silly Input Layout Problem

    Thanks for the assist Chuck, I've made changes as you recommended but I 'm still getting the same errors: 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 }, { "INSTANCE", 0, DXGI_FORMAT_R32G32B32_FLOAT, 1, 0, D3D11_INPUT_PER_INSTANCE_DATA, 1 }, { "INSTANCE", 1, DXGI_FORMAT_R32G32B32_FLOAT, 1, 16, D3D11_INPUT_PER_INSTANCE_DATA, 1 }, { "INSTANCE", 2, DXGI_FORMAT_R32G32B32_FLOAT, 1, 32, D3D11_INPUT_PER_INSTANCE_DATA, 1 }, { "INSTANCE", 3, DXGI_FORMAT_R32G32B32_FLOAT, 1, 48, D3D11_INPUT_PER_INSTANCE_DATA, 1 }, }; You are in fact helping me understand the concept at work here. A float4 is 16 bytes, im trying to pass a D3DXMatrix which is 64 bytes, and the target data type is a float4x4, which is a float4 (16 bytes) * 4, which is 64 bytes. So I have to adjust my input layout accordingly, wouldn't it be nice if there was just a single DXGI_FORMAT for the D3DXMatrix? Anyways, Any idea why I might be getting the same error?
  5. Hi all, I was changing my instance input from a D3DVector3 to D3DXMATRIX and I'm running into trouble. I chose DXGI_FORMAT_R16G16B16A16_FLOAT as the format for my matrix input, I don't know if this is right but it's not complaining, its for 64 bits and my matrix is 64 bits So I am running into this error: D3D11 ERROR: ID3D11Device::CreateInputLayout: The provided input signature expects to read an element with SemanticName/Index: 'INSTANCE'/1, but the declaration doesn't provide a matching name. [ STATE_CREATION ERROR #163: CREATEINPUTLAYOUT_MISSINGELEMENT] Actually 3 of them, 'INSTANCE'/1 'INSTANCE'/2 'INSTANCE'3 It's saying the declaration doesn't provide a matching name, I'm assuming its talking about this? VOut VShader(float4 position : POSITION, float4 color : COLOR, float4x4 instancePosition : INSTANCE) But its there? Here is all the code, this is probably a really silly oversight: My instance struct in my C++ code: struct Instance { D3DXMATRIX position; }; The input layout creation: bool Renderer::createInputLayout(ID3D11Device * dev, ID3D11DeviceContext * devcon, ID3D10Blob * Shader) { HRESULT hr; // 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 }, { "INSTANCE", 0, DXGI_FORMAT_R16G16B16A16_FLOAT, 1, 0, D3D11_INPUT_PER_INSTANCE_DATA, 1 }, }; hr = dev->CreateInputLayout(ied, 3, Shader->GetBufferPointer(), Shader->GetBufferSize(), &pLayout); if (!FAILED(hr)) { devcon->IASetInputLayout(pLayout); return true; } else { return false; } } Finally, the shader code (Note, I also still need to figure out what to do with my new float4x4 as opposed to my float3 I was previously using): cbuffer ProjectionConstantBuffer : register(b0) { float4x4 projection; } cbuffer WorldConstantBuffer : register(b1) { float4x4 world; } struct VOut { float4 position : SV_POSITION; float4 color : COLOR; }; VOut VShader(float4 position : POSITION, float4 color : COLOR, float4x4 instancePosition : INSTANCE) { VOut output; // Apply all instance position translations //position.x += instancePosition.x; //position.y += instancePosition.y; //position.z += instancePosition.z; output.position = position; output.position = mul(position, world); // Apply world translation matrix output.position = mul(position, instancePosition); // Apply instance translation matrix output.position = mul(output.position, projection); // Apply ortho projection matrix output.color = color; // Apply color from vertex input return output; } Thanks for your help! EDIT: I learned that 'INSTANCE'/1, 'INSTANCE'/2, and 'INSTANCE'/3 could possibly be referring to something like this: 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 }, { "INSTANCE", 0,DXGI_FORMAT_R32G32B32A32_FLOAT, 1, 0, D3D11_INPUT_PER_INSTANCE_DATA, 1 }, { "INSTANCE", 1,DXGI_FORMAT_R32G32B32A32_FLOAT, 1, 0, D3D11_INPUT_PER_INSTANCE_DATA, 1 }, { "INSTANCE", 2,DXGI_FORMAT_R32G32B32A32_FLOAT, 1, 0, D3D11_INPUT_PER_INSTANCE_DATA, 1 }, { "INSTANCE", 3,DXGI_FORMAT_R32G32B32A32_FLOAT, 1, 0, D3D11_INPUT_PER_INSTANCE_DATA, 1 }, }; But unfortunately, I'm still missing something, as I am getting the same error. Is there something I need to do with the aligned byte offset? There is some science here I'm definitely not picking up on.
  6. Hey guys, just thought I'd like to update you on the stupidity of my error, now resolved. Never do this: InstanceBuffer * instances; instances = new InstanceBuffer[instanceCount]; instances[0].position = D3DXVECTOR3(-300, -100, 0); instances[1].position = D3DXVECTOR3(-200, 100, 0); instances[2].position = D3DXVECTOR3(-100, -100, 0); /* instances[3].position = D3DXVECTOR3(0, 100, 0); instances[5].position = D3DXVECTOR3(100, -100, 0); instances[6].position = D3DXVECTOR3(200, 100, 0); instances[7].position = D3DXVECTOR3(300, -100, 0); */ instanceCount = 3; Instead, do this: InstanceBuffer * instances; instanceCount = 3; instances = new InstanceBuffer[instanceCount]; instances[0].position = D3DXVECTOR3(-300, -100, 0); instances[1].position = D3DXVECTOR3(-200, 100, 0); instances[2].position = D3DXVECTOR3(-100, -100, 0); /* instances[3].position = D3DXVECTOR3(0, 100, 0); instances[5].position = D3DXVECTOR3(100, -100, 0); instances[6].position = D3DXVECTOR3(200, 100, 0); instances[7].position = D3DXVECTOR3(300, -100, 0); */ Anyways, I'll quit posting here unless someone responds, I'll move my commentary to my blog!
  7. Okay, maybe I should do this in a blog or something instead of a post, but now I am having difficulty with my instance buffer. I create it like this: bool Renderer::createInstanceBuffer(ID3D11Device * dev, UINT bufferSize) { HRESULT hr; D3D11_BUFFER_DESC bd; // Set up the description of the instance buffer. bd.Usage = D3D11_USAGE_DYNAMIC; bd.ByteWidth = bufferSize; bd.BindFlags = D3D11_BIND_VERTEX_BUFFER; bd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; bd.MiscFlags = 0; bd.StructureByteStride = 0; hr = dev->CreateBuffer(&bd, NULL, &pIBuffer); if (!FAILED(hr)) { return true; } else { return false; } } it's so weird, I reduced to one instance, to make sure all my functional parameters and such were correct, then like, every other time I run, it throws an exception, and then it doesn't, and then it does. And then, I fill it like this: bool Renderer::UpdateBuffer(ID3D11DeviceContext * devcon, ID3D11Buffer * buffer, void * data, size_t size) { HRESULT hr; D3D11_MAPPED_SUBRESOURCE ms; hr = devcon->Map(buffer, NULL, D3D11_MAP_WRITE_DISCARD, NULL, &ms); // map the buffer if (!FAILED(hr)) { memcpy(ms.pData, data, size); // copy the data devcon->Unmap(buffer, NULL); // unmap the buffer return true; } else { return false; } } I'm making the two calls like this: for (int i = 0; i < instanceCount; i++) { OurInstances.push_back(instances[i]); } if (createInstanceBuffer(dev, sizeof(InstanceBuffer) * instanceCount)) { UpdateBuffer(devcon, pIBuffer, OurInstances.data(), sizeof(InstanceBuffer) * OurInstances.size()); // Initialize Vertex Buffer } Eventually, another bit of code will take care of preparing the OurInstances vector (something in my renderable_object) Both calls to create instance buffer and updatebuffer seem to be working successfully, they aren't creating any immediate exceptions or explicitly failing, I even see my instances drawn on my screen! But on exiting the application, the thing seems to shut down and throw an exception that I can't really track down, is this something I should turn debug mode on to explain to me? Can any of you identify what I'm doing wrong?
  8. So, I just realized that I'm gonna have some difficulty separating the creation of my vertex buffer and the actual filling of the vertex buffer due to the fact that creating the vertex buffer requires me to enter byte width. But to know that I need to know how many vertices I have. Hmmm, I should be able to get this information another way. Just trying to organize things in a way that makes sense. I'm sure someone here has gone through these design questions.
  9. Hi all, I'm trying to cut down on some of the spaghetti in my code after running through a few lessons and tutorials. Currently, I have everything grouped in a "Renderer" class and I'm trying to break my larger functions down into more manageable bits. I have three main functions that are initializing all my d3d and graphics: void Renderer::InitD3D(HWND hWnd) { // create a struct to hold information about the swap chain 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 = SCREEN_WIDTH; // set the back buffer width scd.BufferDesc.Height = SCREEN_HEIGHT; // set the back buffer height scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; // how swap chain is to be used scd.OutputWindow = 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; // allow full-screen switching // 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, &swapchain, &dev, NULL, &devcon); // get the address of the back buffer ID3D11Texture2D *pBackBuffer; swapchain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&pBackBuffer); // use the back buffer address to create the render target dev->CreateRenderTargetView(pBackBuffer, NULL, &backbuffer); pBackBuffer->Release(); // set the render target as the back buffer devcon->OMSetRenderTargets(1, &backbuffer, NULL); // Set the viewport D3D11_VIEWPORT viewport; ZeroMemory(&viewport, sizeof(D3D11_VIEWPORT)); viewport.TopLeftX = 0; viewport.TopLeftY = 0; viewport.Width = SCREEN_WIDTH; viewport.Height = SCREEN_HEIGHT; devcon->RSSetViewports(1, &viewport); } void Renderer::InitPipeline(ID3D11Device * dev, ID3D11DeviceContext * devcon) { // load and compile the two shaders ID3D10Blob *VS, *PS; D3DX11CompileFromFile("Shaders.shader", 0, 0, "VShader", "vs_4_0", 0, 0, 0, &VS, 0, 0); D3DX11CompileFromFile("Shaders.shader", 0, 0, "PShader", "ps_4_0", 0, 0, 0, &PS, 0, 0); // encapsulate both shaders into shader objects dev->CreateVertexShader(VS->GetBufferPointer(), VS->GetBufferSize(), NULL, &pVS); dev->CreatePixelShader(PS->GetBufferPointer(), PS->GetBufferSize(), NULL, &pPS); // set the shader objects devcon->VSSetShader(pVS, 0, 0); devcon->PSSetShader(pPS, 0, 0); // create the input layout object createInputLayout(dev, devcon, VS); } void Renderer::InitGraphics(std::vector<Renderable_Object*> Game_Objects) { for (int i = 0; i < Game_Objects.size(); i++) { for (int j = 0; j < Game_Objects[i]->getVertices().size(); j++) { OurVertices.push_back(Game_Objects[i]->getVertices()[j]); } } createVertexBuffer(dev); createInstanceBuffer(dev); createProjectionBuffer(dev); createWorldBuffer(dev); } I have InitD3D, which creates my device, swapchain, and device context. This seems to make sense to me, its all of the "background" work. The comes InitPipeline, this function in my mind, should be in charge of preparing the pathway in which we will shove data into our shaders for rendering. Then, I have my InitGraphics, which really all I want it to do is setup the data to be shoved down previously mentioned pipeline, right now in my mind i think it is doing more than that. Am I right in thinking creating buffers is part of the pipeline setup, and then after the setup is done, updating those buffers is part of the graphics initiation I am thinking of. Again, three Phases: Initialize D3D window and device and crap -> initialize pipeline and way of sending data to it -> push data onto the pipeline for initial rendering One more side question: do I need to use the same subresource that I used to initialize a buffer when I go to update it with map/unmap? Or do I set a new subresource_data? I'm starting to think I need the original subresource_data, or at least a pointer to it, because when I call CreateBuffer I pass it a reference to a subresource_data object... Sorry all, I tend to ask before I invetigate, given that this code here works just fine for me, it would suggest that I do not need the original subresource, all I need is the data I'm going to update with. if (createVertexBuffer(dev)) { // copy the vertices into the buffer D3D11_MAPPED_SUBRESOURCE ms; devcon->Map(pVBuffer, NULL, D3D11_MAP_WRITE_DISCARD, NULL, &ms); // map the buffer memcpy(ms.pData, OurVertices.data(), sizeof(Vertex) * OurVertices.size()); // copy the data devcon->Unmap(pVBuffer, NULL); // unmap the buffer } So, this means I can successfully separate my buffer creation, from my buffer initialization. I can create my buffers as the cars that will carry crap down the pipeline, in the init pipeline funciton, then I can put people in said cars in the initgraphics function.
  10. Thanks for this tidbit, so as far as you know, theres no true difference to map/unmap and UpdateSubresource calls? I could just use UpdateSubresource to peform my instance updates?
  11. Oh how shortsighted I am! I know nothing!
  12. Looks fancy Chuck, for now, I'm going to leave my shader as a one trick pony and only use instanced drawing. I think, using only 2d for now, most of my stuff is going to be instanced geometry, or a couple very small differences. Right now, I'm going to focus on getting this thing to work dynamically and take input, I want to be able to move the instanced squares around using input. That, along with generally scrubbing my code and adding in error handling and things of that nature. I also need to figure out how my Renderable_Object class will come into play now that there is only one copy of the vertices on hand at a given time due to instancing... Right now I have a class renderable object that is storing vertex information, as well as other various bits of info, such as position, velocity, min and max values, etc.. Now with instancing, I only need 1 copy of vertex data and many copies of position and velocity data. I had this setup previously in opengl and have since lost it. Oh well, onward I go! class Renderable_Object { public: Renderable_Object(float minX, float minY, float maxX, float maxY) { d_min.x = minX; d_min.y = minY; d_max.x = maxX; d_max.y = maxY; /* position.x = posX; position.y = posY; velocity.x = 0; velocity.y = 0; isVisible = visible; isPhysical = physical; */ Vertex OurVertices[] = { { D3DXVECTOR2(maxX, maxY), D3DXCOLOR(1.0f, 0.0f, 0.0f, 1.0f) }, { D3DXVECTOR2(maxX, minY), D3DXCOLOR(0.0f, 1.0f, 0.0f, 1.0f) }, { D3DXVECTOR2(minX, minY), D3DXCOLOR(0.0f, 0.0f, 1.0f, 1.0f) }, { D3DXVECTOR2(minX, minY), D3DXCOLOR(1.0f, 0.0f, 0.0f, 1.0f) }, { D3DXVECTOR2(minX, maxY), D3DXCOLOR(0.0f, 1.0f, 0.0f, 1.0f) }, { D3DXVECTOR2(maxX, maxY), D3DXCOLOR(0.0f, 0.0f, 1.0f, 1.0f) } }; for (int i = 0; i < 6; i++) { Vertices.push_back(OurVertices[i]); } } D3DXVECTOR2 & getVelocity() { return velocity; } void setVelocity(float x, float y) { velocity.x = x; velocity.y = y; } D3DXVECTOR2 & getLocation() { return position; } D3DXVECTOR2 & getMinimum() { return d_min; } D3DXVECTOR2 & getMaximum() { return d_max; } std::vector<Vertex> & getVertices() { return Vertices; } private: D3DXVECTOR2 position; D3DXVECTOR2 velocity; D3DXVECTOR2 d_min; D3DXVECTOR2 d_max; std::vector<Vertex> Vertices; bool isPhysical; // can you touch it? bool isVisible; // render this object? bool AABB; // compatible with AABB collision detection };
  13. Cool, so map, unmap, I've seen that before So, maybe you can help me understand, I'm doing two seemingly different things to set data in my subresources... For my vertex buffer, I'm doing a map and unmap like you're talking about: devcon->Map(pVBuffer, NULL, D3D11_MAP_WRITE_DISCARD, NULL, &ms); // map the buffer memcpy(ms.pData, OurVertices.data(), sizeof(Vertex) * OurVertices.size()); // copy the data devcon->Unmap(pVBuffer, NULL); // unmap the buffer For my instance buffer, which was created by checking out the rastertek tutorial, I'm doing this: // Give the subresource structure a pointer to the instance data. instanceData.pSysMem = instances; instanceData.SysMemPitch = 0; instanceData.SysMemSlicePitch = 0; The vertex buffer, I notice data is getting set AFTER the CreateBuffer call, for the instance buffer, the data is being set BEFORE. Can I just map/unmap my buffer I created for my instance buffer after I have already set data like this? Chuck, you're correct, I did revert back to a float3, in the form of a D3DXVECTOR3 because I was having trouble getting things to work. Now that things are working, I'll give switching my per instance data back to a D3DXMATRIX. This should simplify my shader code. So, on that note chuck, the world matrix in my constant buffer, should be a separate matrix from the one in my instances correct? In this respect, the constant buffer world matrix is a translation on a set of instances, rather than individually?
  14. Dumb question guys, now that I have instancing set up, and my squares are getting drawn on the screen in a location determined by the instance, what role does my world matrix play? Now I translate with my instance buffer, rather than the world constant buffer. Also, I assumed it was possible to update my instance buffer during my update loop, do I need to create a new instance buffer each time an object moves, how do I go about updating my instance buffer during runtime? After some googling, I found this little Q&A here: https://gamedev.stackexchange.com/questions/48179/directx-instance-buffer-how-to-use-instance-buffers-to-enable-reuse-of-verte Now, the guy is suggesting he use constant buffer updates to move his stuff around the world, but how do I use a constant buffer to change my world translation for each instance with one "drawinstanced" call... Maybe someone can help me piece things together. I think i found my answer in that link: I can use this capability of the DrawInstanced function, to set the constant buffer between calls. But then this defeats the purpose of passing position data via the instance? I feel conflicted, can someone help me sort this logic out? Why pass an instance position if I'm going to be updating the constant buffer to translate my instances?
  15. Man you real programmers make me look bad
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!