visual c++ - DirectX 11 changing the pixel bytes -


followed guide here

i tasked "using map , unmap methods draw line across screen setting pixel byte data rgb red values".

i have sprite , background displaying have no idea how data.

i tried doing this:

//create device d3d11_texture2d_desc desc; zeromemory(&desc, sizeof(d3d11_texture2d_desc)); desc.width = 500; desc.height = 300; desc.format = dxgi_format_b8g8r8a8_unorm; desc.usage = d3d11_usage_dynamic; desc.cpuaccessflags = d3d11_cpu_access_write; desc.miscflags = 0; desc.miplevels = 1; desc.arraysize = 1; desc.sampledesc.count = 1; desc.sampledesc.quality = 0; desc.bindflags = d3d11_bind_shader_resource;   m_d3ddevice->createtexture2d(&desc, nullptr, &texture); m_d3ddevice->createshaderresourceview(texture, 0, &textureview);  // render d3d11_mapped_subresource mapped; m_d3dcontext->map(texture, 0, d3d11_map_write_discard, 0, &mapped);  data = (byte*)mapped.pdata; rows = (byte)sizeof(data);  std::cout << "hi" << std::endl; m_d3dcontext->unmap(texture, 0); 

problem in case data array size 0 has pointer. means pointing texture doesn't have data or not getting this?

edit: found

d3d11_shader_resource_view_desc desc; m_background->getdesc(&desc); desc.buffer; // buffer 

first of all, of functions return hresult values ignoring. that's not safe miss important errors invalidate remaining code. can use if(failed(...)) if want, or can use throwiffailed, can't ignore return value in functioning app.

hresult hr = m_d3ddevice->createtexture2d(&desc, nullptr, &texture); if (failed(hr))     // error! hr = m_d3ddevice->createshaderresourceview(texture, 0, &textureview); if (failed(hr))     // error!  // render d3d11_mapped_subresource mapped; hr = m_d3dcontext->map(texture, 0, d3d11_map_write_discard, 0, &mapped); if (failed(hr))     // error! 

second, should enable debug device , diagnostic output point reason failure.

sizeof(data) going 4 or 8 since data byte* i.e. size of pointer. has nothing size of data array. locked buffer pointed mapped.pdata going mapped.rowpitch * desc.height bytes in size.

you have copy pixel data row-by-row. depending on format , other factors, mapped.rowpitch not going 4 * desc.width--4 bytes per pixel because using format of dxgi_format_b8g8r8a8_unorm. should @ least big, bigger align overall size.

this pseudo-code , not efficient way it, but:

for(uint y = 0; y < desc.height; ++y ) {     for(uint x = 0; x < desc.width; ++x )     {         // find memory location of pixel @ (x,y)         int pixel = y * mapped.rowpitch + (x*4)         byte* blue = data[pixel];         byte* green = data[pixel] + 1;         byte* red = data[pixel] + 2;         byte* alpha = data[pixel] + 3;          *blue = /* value between 0 , 255 */;         *green = /* value between 0 , 255 */;         *red = /* value between 0 , 255 */;         *alpha = /* value between 0 , 255 */;     } } 

you should take @ directxtex lot of kind of row-by-row processing.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -