Jump to content

  • Log In with Google      Sign In   
  • Create Account


Jeffreyp

Member Since 24 Jan 2011
Offline Last Active Jul 07 2013 04:39 AM
-----

Topics I've Started

Attempting to create a pool allocator

07 March 2013 - 06:53 AM

Hello,

 

Today I decided that I want to gain some knowledge about memory management.

I don't know much about memory management so i thought it would be a nice "exercise" to try to create a pool allocator.

Actually everything is working as expected but I think I did something wrong.

 

Here's the code :

 

PoolAllocator.h

#pragma once



class PoolAllocator

{

public:

    PoolAllocator(void);

    ~PoolAllocator(void);


    void allocate(const size_t amount);

    char *obtain(const size_t amount);

    void free(void *mem);


private:


    char *memory;

    char *at;

    int atAmount;

    int size;


};

 

 

PoolAllocator.cpp

#include "PoolAllocator.h"



PoolAllocator::PoolAllocator(void)

{

}



PoolAllocator::~PoolAllocator(void)

{

    delete[] memory;
    delete[] at;

}



void PoolAllocator::allocate(const size_t maxSize)

{

    memory = new char[maxSize];

    at = &memory[0];

    atAmount = 0;

    size = maxSize;

}


char *PoolAllocator::obtain(const size_t amount)

{

    if(atAmount + amount     > size - 1)

        return nullptr;


    at += amount;


    return at;

}


void PoolAllocator::free(void *mem)

{


}

 

main.cpp

#include <Windows.h>

#include <iostream>


#include "PoolAllocator.h"


class A

{

public:

    int val;


};


class B

{

public:

    int val;


};


int main()

{

    PoolAllocator allocator;

    allocator.allocate(1000);


    A *a = (A*)allocator.obtain(sizeof(A));


    a->val = 10;


    std::cout << "A : " << a->val << std::endl;


    B *b = (B*)allocator.obtain(sizeof(B));


    b->val = 20;


    std::cout << "A : " << a->val << std::endl << "B : " << b->val << std::endl;

    

    system("pause");

    return 0;

}

 

My knowledge in memory management is really limited, so it could be that I made some 'horrible' mistake or something.

Basically I just want someone to check this code.

 

Thanks in advance !


Need help with shadow mapping

18 November 2012 - 05:54 AM

Hi guys,

I need help with my shadow mapping because I get incorrect results.
I think I calculate my light view and light projection matrix wrong.
Because I got a wrong shadow map too.

Here is the code :

DirectX::XMMATRIX CreateLightViewProjectionMatrix(DirectX::XMFLOAT3 slightPos)
{
DirectX::BoundingFrustum frustom(DirectX::XMMatrixIdentity());
DirectX::XMVECTOR up = DirectX::XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);
DirectX::XMVECTOR lightDir = DirectX::XMLoadFloat3(&slightPos);
DirectX::XMMATRIX lightRotation = DirectX::XMMatrixLookAtLH(DirectX::XMVectorZero(),
DirectX::XMVectorSet(DirectX::XMVectorGetX(lightDir) * -1.0f,
DirectX::XMVectorGetY(lightDir) * -1.0f,
DirectX::XMVectorGetZ(lightDir) * -1.0f,
DirectX::XMVectorGetW(lightDir) * -1.0f), up);


DirectX::XMFLOAT3 *frustumCorners = new DirectX::XMFLOAT3[frustom.CORNER_COUNT];
frustom.GetCorners(frustumCorners);


for (int i = 0; i < frustom.CORNER_COUNT; i++)
{
  DirectX::XMVECTOR trans = DirectX::XMLoadFloat3(&frustumCorners[i]);
  trans = DirectX::XMVector3Transform(trans, lightRotation);
  DirectX::XMStoreFloat3(&frustumCorners[i], trans);
}


DirectX::BoundingBox lightBox;
lightBox.CreateFromPoints(lightBox, frustom.CORNER_COUNT, frustumCorners, sizeof(DirectX::XMFLOAT3));
delete[] frustumCorners;
DirectX::XMVECTOR lightBoxExtents = DirectX::XMLoadFloat3(&lightBox.Extents);
DirectX::XMVECTOR lightBoxCenter = DirectX::XMLoadFloat3(&lightBox.Center);
DirectX::XMVECTOR lightBoxMax = DirectX::XMVectorAdd(lightBoxCenter, lightBoxExtents);
DirectX::XMVECTOR lightBoxMin = DirectX::XMVectorSubtract(lightBoxCenter, lightBoxExtents);

DirectX::XMVECTOR boxSize = DirectX::XMVectorSubtract(lightBoxMax, lightBoxMin);
DirectX::XMVECTOR halfBoxSize = DirectX::XMVectorScale(boxSize, 0.5f);

DirectX::XMVECTOR lightPosition = DirectX::XMVectorAdd(lightBoxMin, halfBoxSize);
DirectX::XMVectorSetZ(lightPosition, DirectX::XMVectorGetZ(lightBoxMin));

DirectX::XMVECTOR det = DirectX::XMVectorSet(-1.0f, -1.0f, -1.0f, -1.0f);
lightPosition = DirectX::XMVector3Transform(lightPosition,
DirectX::XMMatrixInverse(&det, lightRotation));

DirectX::XMMATRIX lightView = DirectX::XMMatrixLookAtLH(lightPosition,
DirectX::XMVectorSubtract(lightPosition, lightDir),
												   up);

lightProjection = DirectX::XMMatrixOrthographicLH(DirectX::XMVectorGetX(boxSize), DirectX::XMVectorGetY(boxSize),
															   -DirectX::XMVectorGetZ(boxSize), DirectX::XMVectorGetZ(boxSize));
return lightView * lightProjection;
}


This is the same code is used in the msdn shadow mapping example(for XNA).
So I don't know what I did wrong.

Thanks in advance.

DirectXTex question

03 September 2012 - 03:03 PM

Hello,

I have a question about loading multiple images and combine them into a texture array.
Since the D3DX library is deprecated i use the DirectXTex library to load textures, but i can't find
a way to load multiple jpg files and combine them into a texure array(I have no problem with DDS files).
I searched the internet but i couldn't find anything useful.

Any help will be appreciated.

Multiple direct x applications on the same computer

20 July 2012 - 04:06 AM

Heey guys,

I got a question about running(in visual studio) multiple(2) direct x applications on the same computer.
When i do this i get a warning :


D3D11: WARNING: ID3D11DeviceContext::Draw: Vertex Buffer at the input vertex slot 0 is not big enough for what the Draw*() call expects to traverse. This is OK, as reading off the end of the Buffer is defined to return 0. However the developer probably did not intend to make use of this behavior.  [ EXECUTION WARNING #356: DEVICE_DRAW_VERTEX_BUFFER_TOO_SMALL ]



I already debugged my code but this only happens when i try to debug 2 instances.\
My guess would be that the slot is already in use by the other app and that's why it throws this error.

Thanks in advance.

CreateVertexShader fails

30 June 2012 - 06:08 AM

Hello,

I'm getting some strange error when trying to compile my vertex shader :

D3D11: ERROR: ID3D11Device::CreateVertexShader: Encoded Vertex Shader size doesn't match specified size. [ STATE_CREATION ERROR #166: CREATEVERTEXSHADER_INVALIDSHADERBYTECODE ]



I already searched with Google but i couldn't find anything useful.
Here is my code :

c++



ID3DBlob *VS, *PS;

ID3D11VertexShader *m_pVS;

ID3D11PixelShader *m_pPS;



if(FAILED(D3DReadFileToBlob(pixelShader.c_str(), &PS)))

{

shader.isValid = false;

return;

}



if(FAILED(D3DReadFileToBlob(vertexShader.c_str(), &VS)))

{

shader.isValid = false;

return;

}



auto i = VS->GetBufferSize();



if(FAILED(m_pimpl->m_pDevice->CreateVertexShader(VS->GetBufferPointer(), VS->GetBufferSize(), NULL, &m_pVS)))

{

std::cout << std::endl <<"Warning : failed to create vertex shader at file : " << " " << vertexShader.c_str() << std::endl;

shader.isValid = false;

return;

}



if(FAILED(m_pimpl->m_pDevice->CreatePixelShader(PS->GetBufferPointer(), PS->GetBufferSize(), NULL, &m_pPS)))

{

std::cout << std::endl <<"Warning : failed to create pixel shader at file : " << " " << pixelShader.c_str() << std::endl;

shader.isValid = false;

return;

}


Vertex shader :



cbuffer ConstantBuffer

{

	float4x4 matFinal;

}



struct VOut

{

	float4 position : SV_POSITION;

	float4 color : COLOR;

};



VOut main(float4 position : POSITION, float4 color : COLOR)

{

	VOut output;



	output.position = mul(matFinal, position);

	output.color = color;



	return output;

}



Pixel Shader :



float4 main(float4 position : SV_POSITION, float4 color : COLOR) : SV_TARGET

{

	return color;

}



I'm using SM 4.

Thanks in advance.

PARTNERS