• 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
kolarz3

DirectX 11 maping ID3D11Texture2D

17 posts in this topic

I'd like to change ID3D11Texture2D to data. This is my funtion: 

void getByte(ID3D11Texture2D* texture)
{
	ID3D11Texture2D* textureBuf;
	
	D3D11_TEXTURE2D_DESC textureDesc;

	ZeroMemory(&textureDesc, sizeof(textureDesc));
	textureDesc.Width = Width;
	textureDesc.Height = Height;
	textureDesc.MipLevels = 1;
	textureDesc.ArraySize = 1;
	textureDesc.Format = DXGI_FORMAT_R32G32B32A32_FLOAT;
	textureDesc.SampleDesc.Count = 1;
	textureDesc.SampleDesc.Quality = 0;
	textureDesc.Usage = D3D11_USAGE_STAGING ;
	textureDesc.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE;
	textureDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
	textureDesc.MiscFlags = 0;
	
	d3d11Device->CreateTexture2D(&textureDesc, NULL, &textureBuf);
	d3d11DevCon->CopyResource(textureBuf,texture);
	D3D11_MAPPED_SUBRESOURCE  mapResource;
	d3d11DevCon->Map(texture,0,D3D11_MAP_READ,NULL,&mapResource);
}

How can I now get pixel data ?

0

Share this post


Link to post
Share on other sites

Your pixel data is in mapResource.pData. Typecast it into this structure (I hope I wrote correct order):
 

struct Color {
    float r, g, b, a;
};

Then access it like 1 dimensional array..

 

0

Share this post


Link to post
Share on other sites

I do what you say but my bufor is empty (when I try save sample data to file visual throws null ptr).

	struct Color {
    float r, g, b, a;
		};
	Color* obj;
	obj=(Color *)mapResource.pData;
	std::fstream file( "color.txt", std::ios::out );
	std::string str;
	std::stringstream ss (std::stringstream::in | std::stringstream::out);
	ss << obj[0].r;
	str=ss.str();
	file.write( & str[ 0 ], str.length() );
0

Share this post


Link to post
Share on other sites

Verify that calls to CreateTexture2D and Map functions are not giving you error. Basically assign them to HRESULT hr variable. and check that SUCCEEDED(hr) is true.

Or enable debug runtime - either pass D3D11_CREATE_DEVICE_DEBUG flag for D3D11CreateDevice function, or go to DirectX Control Panel. That will show you errors in debugger output.

2

Share this post


Link to post
Share on other sites

Good idea, because those flags look incompatible (a staging resource can't be a render target or shader resource view).

 

Furthermore: The mapped memory you receive is not necessarily contiguous. You have to take the D3D11_MAPPED_SUBRESOURCE.RowPitch into account - which gives you the memory offset in byte from one row to the next.

 

0

Share this post


Link to post
Share on other sites

Now I change desc but still i have problem. Maping is not succeeded.

        textureDesc.Width = Width;
	textureDesc.Height = Height;
	textureDesc.MipLevels = 1;
	textureDesc.ArraySize = 1;
	textureDesc.Format = DXGI_FORMAT_R32G32B32A32_FLOAT;
	textureDesc.SampleDesc.Count = 1;
	textureDesc.SampleDesc.Quality = 0;
	textureDesc.Usage = D3D11_USAGE_STAGING;
	textureDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
	textureDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
	textureDesc.MiscFlags = 0;
	
	d3d11Device->CreateTexture2D(&textureDesc, NULL, &textureBuf);
	d3d11DevCon->CopyResource(textureBuf,texture);
	D3D11_MAPPED_SUBRESOURCE  mapResource;
	hr=d3d11DevCon->Map(texture,0,D3D11_MAP_READ,NULL,&mapResource);
Edited by kolarz3
0

Share this post


Link to post
Share on other sites
  1. I don't think you can use D3D11_BIND_SHADER_RESOURCE flag either.
  2. You seem to be using DXGI_FORMAT_R32G32B32A32_FLOAT format, are you sure your source texture is same format?
  3. It seems you try to Map() wrong texture.
0

Share this post


Link to post
Share on other sites

Now work(with copying with "obj=(Color *)mapResource.pData;"), but I don't known why I cant copy with memcpy. How can I copy this data with memcpy?

memcpy ( obj,mapResource.pData, sizeof(Color) );
0

Share this post


Link to post
Share on other sites

sizeof(Color) is 16. You are copying only 16 bytes. You need to copy Height * RowPitch bytes.

0

Share this post


Link to post
Share on other sites

I cant copy anything because debuger stop on memcpy, if I continue debuging visual jump into memcpy.asm file :

rep     movsd           ;N - move all of our dwords
memcpy(obj,mapResource.pData,mapResource.RowPitch);

Edit: I forget to allocate memory/

0

Share this post


Link to post
Share on other sites

I suspect he is failing on the 2D Texture creation. Try setting the BindFlags to 0. I have just spent several hours trying to create a 2D Texture instance that the CPU can read from with no success until i set the BindFlags to 0.

0

Share this post


Link to post
Share on other sites

I dont known why i cant move pointer of my structer. Any solution ?

	struct Color {
    float r, g, b, a;
		};
	Color* obj;
	obj=new Color[mapResource.RowPitch*Height];
	BYTE* mappedData = reinterpret_cast<BYTE*>(mapResource.pData);
	for(UINT i = 0; i < Height; ++i)
	{
	memcpy(obj, mappedData, mapResource.RowPitch);
	mappedData += mapResource.RowPitch;
	obj += mapResource.RowPitch;//without this line program work
	}
0

Share this post


Link to post
Share on other sites

Pointer arithmetic. obj does not not increase by RowPitch but by RowPitch*sizeof(Color). Use a BYTE pointer for the loop.

 

Edit: Similarly, your allocated array is 16 times too big.

 

But since you're copying everything anyway you could as well use only one memcpy.

Edited by unbird
0

Share this post


Link to post
Share on other sites

Now everything work.I found also thet all rows of pixel have extra 4 pixel data(I dont known what they mean) . If anybody later searching for solution I put code .

void Pick(ID3D11Texture2D* texture)
{
	ID3D11Texture2D* textureBuf;
	D3D11_TEXTURE2D_DESC textureDesc;
	ZeroMemory(&textureDesc, sizeof(textureDesc));
	textureDesc.Width = Width;
	textureDesc.Height = Height;
	textureDesc.MipLevels = 1;
	textureDesc.ArraySize = 1;
	textureDesc.Format = DXGI_FORMAT_R32G32B32A32_FLOAT;
	textureDesc.SampleDesc.Count = 1;
	textureDesc.SampleDesc.Quality = 0;
	textureDesc.Usage = D3D11_USAGE_STAGING;
	textureDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
	textureDesc.MiscFlags = 0;
	d3d11Device->CreateTexture2D(&textureDesc, NULL, &textureBuf);

	d3d11DevCon->CopyResource(textureBuf,texture);
	D3D11_MAPPED_SUBRESOURCE  mapResource;
	hr=d3d11DevCon->Map(textureBuf,0,D3D11_MAP_READ,NULL,&mapResource);
	
	struct Color {float r, g, b, a;};
	Color* obj;
	obj=new Color[(mapResource.RowPitch/sizeof(Color))*Height];
	memcpy(obj, mapResource.pData,mapResource.RowPitch*Height);

	if(mousePos.x>0&&mousePos.x<Width&&mousePos.y>0&&mousePos.y<Height)
	if(obj[(mousePos.y*Width)+(4*mousePos.y)+mousePos.x].r==1.0/*If object that we was pick mouse have 1.0 on red bit we draw little cloud*/)model.rysujOtoczk?(model.dolny,model.gorny);
	
	d3d11DevCon->Unmap(textureBuf,0);
	textureBuf->Release();
	delete [] obj;
}
Edited by kolarz3
0

Share this post


Link to post
Share on other sites

You don't need to copy all memory just to access one element of mapped memory.

hr=d3d11DevCon->Map(textureBuf,0,D3D11_MAP_READ,NULL,&mapResource);
if (FAILED(hr)) abort();
    
struct Color {float r, g, b, a;};
Color* obj = (Color*)mapResource.pData;
 
if(mousePos.x>0&&mousePos.x<Width&&mousePos.y>0&&mousePos.y<Height)
  if(obj[(mousePos.y*(mapResource.RowPitch)/sizeof(Color))+(4*mousePos.y)+mousePos.x].r==1.0/*If object that we was pick mouse have 1.0 on red bit we draw little cloud*/)
    model.rysujOtoczk?(model.dolny,model.gorny);
    
d3d11DevCon->Unmap(textureBuf,0);
textureBuf->Release();
Edited by Martins Mozeiko
0

Share this post


Link to post
Share on other sites

But in this case we must to cast data from D3D11_MAPPED_SUBRESOURCE to BYTE.

Edited by kolarz3
0

Share this post


Link to post
Share on other sites

What?

Only thing you need to cast is pData member of D3D11_MAPPED_SUBRESOURCE. It has void* type, but you need Color*

0

Share this post


Link to post
Share on other sites

Anyway "Color* obj = (Color*)mapResource.pData;" do the same that "memcpy(obj, mapResource.pData,mapResource.RowPitch*Height);"

0

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