violation access for my constantBuffer struct

Started by
1 comment, last by 21st Century Moose 12 years, 2 months ago
Hello everybody smile.png ,

I'm trying to make a little game with D3D11 and DXUT and I have a problem since 3 days. I have a violation access in my function "OnKeyboard()". This is my source code :




#include <Windows.h>
#include <d3d11.h>
#include <d3dx11.h>
#include <DxErr.h>
#include <DXUT.h>
#pragma comment(lib,"dxgi.lib")
#pragma comment(lib,"d3d11.lib")
#pragma comment(lib,"d3dx11.lib")
#pragma comment(lib,"DxErr.lib")
#pragma comment(lib,"dxguid.lib")
#pragma comment(lib,"dxut.lib")
#pragma comment(lib,"d3dx9.lib")
#pragma comment(lib,"comctl32.lib")
#pragma comment(linker, "/nodefaultlib:MSVCRT")

#define _V(x) { HRESULT h=x; if (FAILED(h)) { MessageBoxA(NULL, __FUNCTION__ ":" #x " failed.","Error",MB_OK); return h; } }
#define _Vc(x) { HRESULT h=x; if (FAILED(h)) { MessageBoxA(NULL,(LPCSTR)(pErrMsg->GetBufferPointer()),#x,MB_OK); return h; } }
inline UINT Mult16(UINT x) { if ((x & 0x0f) == 0) return x; else return ((x>>4) + 1)<<4; }

struct ConstantBuffer {
//D3DXCOLOR Color;
D3DXVECTOR4 Color;
};

struct Scene {
ID3D11Buffer *pcBufferGPU;
ID3D11Buffer *pVertexBuffer;
ID3D11InputLayout *pInputLayout;
ID3D11VertexShader *pVertexShader;
ID3D11PixelShader *pPixelShader;
ConstantBuffer *pcBufferCPU;
Scene() { memset(this,0,sizeof(Scene)); };
};

HRESULT WINAPI OnCreateDevice(ID3D11Device *device, CONST DXGI_SURFACE_DESC * pBackBufferSurfaceDesc, void* pUserContext) {
Scene &s = *static_cast<Scene*>(pUserContext);

//constant buffer
D3D11_BUFFER_DESC cBufDesc;
ZeroMemory(&cBufDesc,sizeof(cBufDesc));
cBufDesc.ByteWidth = Mult16(sizeof( ConstantBuffer ) );
cBufDesc.Usage = D3D11_USAGE_DYNAMIC;
cBufDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
cBufDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
_V( device->CreateBuffer( &cBufDesc, NULL, &s.pcBufferGPU ));

//vertexBuffer
D3DXVECTOR2 vertex[4] = { D3DXVECTOR2(-0.5f,-0.5f), D3DXVECTOR2(-0.5f,0.5f), D3DXVECTOR2(0.5f,-0.5f), D3DXVECTOR2(0.5f,0.5f) };
D3D11_BUFFER_DESC BufDesc = { 4*sizeof(D3DXVECTOR2), D3D11_USAGE_DEFAULT, D3D11_BIND_VERTEX_BUFFER, 0,0,0 };
D3D11_SUBRESOURCE_DATA BufData = { vertex, 0, 0 };
_V( device->CreateBuffer(&BufDesc, &BufData, &s.pVertexBuffer) );
// compilation des shaders + création de l'input layout
ID3D10Blob *pCode = NULL, *pErrMsg = NULL;
_Vc( D3DX11CompileFromFileA("Shader.fx", NULL, NULL, "VSmain", "vs_5_0", 0, 0, NULL, &pCode, &pErrMsg, NULL) );
_V( device->CreateVertexShader( pCode->GetBufferPointer(), pCode->GetBufferSize(), NULL, &s.pVertexShader) );
D3D11_INPUT_ELEMENT_DESC layout={ "POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 };
_V( device->CreateInputLayout( &layout, 1, pCode->GetBufferPointer(), pCode->GetBufferSize(), &s.pInputLayout) );
SAFE_RELEASE(pCode);
SAFE_RELEASE(pErrMsg);
_Vc( D3DX11CompileFromFileA("Shader.fx", NULL, NULL, "PSmain", "ps_5_0", 0, 0, NULL, &pCode, &pErrMsg, NULL) );
_V( device->CreatePixelShader( pCode->GetBufferPointer(), pCode->GetBufferSize(), NULL, &s.pPixelShader) );
SAFE_RELEASE(pCode);
SAFE_RELEASE(pErrMsg);
return S_OK;
};

void WINAPI OnFrameMove( double fTime, float fElapsedTime, void* pUserContext ) {

};

void WINAPI OnFrameRender(ID3D11Device *device, ID3D11DeviceContext *context, DOUBLE fTime, FLOAT fElapsedTime, void* pUserContext) {
Scene &s = *static_cast<Scene*>(pUserContext);
float ClearColor[4] = { 0.0f, 0.0f, 0.0f, 1.0f }; //red,green,blue,alpha
context->ClearRenderTargetView( DXUTGetD3D11RenderTargetView(), ClearColor );
context->ClearDepthStencilView( DXUTGetD3D11DepthStencilView(), D3D10_CLEAR_DEPTH, 1.f, 0);
UINT stride=sizeof(D3DXVECTOR2), offset=0;

// détache le buffer de constante du pipeline
ID3D11Buffer *pCBnull = NULL;
context->VSSetConstantBuffers(0, 1, &pCBnull);
context->PSSetConstantBuffers(0, 1, &pCBnull);
// mise à jour du buffer de constantes
HRESULT h;
D3D11_MAPPED_SUBRESOURCE Mapped;
h = context->Map(s.pcBufferGPU, 0, D3D11_MAP_WRITE_DISCARD, 0, &Mapped);
CopyMemory(Mapped.pData, &s.pcBufferCPU, sizeof(ConstantBuffer) );
context->Unmap(s.pcBufferGPU, 0);
// réattache le buffer de constante du pipeline
context->VSSetConstantBuffers(0, 1, &s.pcBufferGPU);
context->PSSetConstantBuffers(0, 1, &s.pcBufferGPU);



context->IASetVertexBuffers(0,1,&s.pVertexBuffer,&stride,&offset);
context->IASetInputLayout(s.pInputLayout);
context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
context->VSSetShader(s.pVertexShader,NULL,0);
context->PSSetShader(s.pPixelShader,NULL,0);



context->Draw(4,0); //4 sommets dans le vertex buffer, commence à la position 0 du vertex buffer
}


void WINAPI OnDestroyDevice(void* pUserContext) {
Scene *s = static_cast<Scene*>(pUserContext);
// devrait être dans le destructeur
SAFE_RELEASE(s->pVertexBuffer);
SAFE_RELEASE(s->pInputLayout);
SAFE_RELEASE(s->pVertexShader);
SAFE_RELEASE(s->pPixelShader);
SAFE_RELEASE(s->pcBufferGPU);
delete s;
}

void WINAPI OnKeyboard(UINT nChar, bool bKeyDown, bool bAltDown, void* pUserContext) {
Scene &maScene = *static_cast<Scene*>(pUserContext);
D3DXVECTOR4 colorTmp;
//Elements &mesElements = *static_cast<Elements*>(pUserContext);
//float maCouleur[4];
if( bKeyDown ) {
switch( nChar ) {
case '1':
colorTmp.x = 0;
colorTmp.y = 1;
colorTmp.z = 0;
colorTmp.w = 1;
//maScene.pcBufferCPU->Color = D3DXCOLOR(1,0,0,1);
//maScene.pcBufferCPU->Color = D3DXVECTOR4(1,0,0,1);
maScene.pcBufferCPU->Color = colorTmp;
break;
case '2':
//maScene.pcBufferCPU->Color = D3DXCOLOR(0,1,0,1);
maScene.pcBufferCPU->Color = D3DXVECTOR4(1,0,0,1);
break;
case '3':
//maScene.pcBufferCPU->Color = D3DXCOLOR(0,0,1,1);
maScene.pcBufferCPU->Color = D3DXVECTOR4(1,0,0,1);
break;
}
}
}


INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR, int ) {
// environnement principal
Scene *scene = new Scene();
// pointeur vers les fonctions
DXUTSetCallbackD3D11DeviceCreated( OnCreateDevice, scene );
DXUTSetCallbackFrameMove( OnFrameMove, scene );
DXUTSetCallbackD3D11FrameRender( OnFrameRender, scene );
DXUTSetCallbackD3D11DeviceDestroyed( OnDestroyDevice, scene );
DXUTSetCallbackKeyboard( OnKeyboard, scene );
// initialialisation et boucle principale
DXUTInit();
DXUTCreateWindow( L"Demo" );
DXUTCreateDevice( D3D_FEATURE_LEVEL_11_0, true, 800, 600 );
DXUTMainLoop();
return DXUTGetExitCode();
}


I'm trying to modify the color, but it's forbidden :/. Can you help me ? Thanks.
Advertisement
Scene::pcBufferCPU; is pointer, but I can't see where you actually allocate memory. If you don't that's the issue.
Even if you do allocate memory, this will also cause you trouble:
CopyMemory(Mapped.pData, &s.pcBufferCPU, sizeof(ConstantBuffer) );

Should be
CopyMemory(Mapped.pData, s.pcBufferCPU, sizeof(ConstantBuffer) );

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

This topic is closed to new replies.

Advertisement