How do I get WGL to create a 32 bit depth buffer? - winapi

If I set cDepthBits, cStencilBits to 24, 8 it works fine, but 32, 0 doesn't. Here's what I'm doing:
PIXELFORMATDESCRIPTOR pfd = {0};
pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 32;
pfd.cDepthBits = 32;
pfd.cStencilBits = 0;
pfd.dwLayerMask = PFD_MAIN_PLANE;
i32 pixelFormatIndex = ChoosePixelFormat(windows.hdc, &pfd);
SetPixelFormat(windows.hdc, pixelFormatIndex, &pfd);
windows.hrc = wglCreateContext(windows.hdc);
wglMakeCurrent(windows.hdc, windows.hrc);
Then I query the PFD with DescribePixelFormat(windows.hdc, GetPixelFormat(windows.hdc), ..), and cDepthBits is only 24.

Well, it is unclear if your implementation actually does support a 32 bit depth buffer for the default framebuffer.
However, using the WGL_ARB_pixel_format extension gives you much finer control over the pixel formats. Especially, wglChoosePixelFormatARB() will return a set of pixel formats mathcing (more or less strictly) the attributes you queried for. You can then iterate over those and use wglGetPixelFormatAttribivARB()/wglGetPixelFormatAttribfvARB() to query for the detailed attributes of the returned formats, and select the one you like best - and you can check if any 32 bit depth format is among them.

Related

MPSImageIntegral returning all zeros

I am trying to use MPSImageIntegral to calculate the sum of some elements in an MTLTexture. This is what I'm doing:
std::vector<float> integralSumData;
for(int i = 0; i < 10; i++)
integralSumData.push_back((float)i);
MTLTextureDescriptor *textureDescriptor = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatR32Float
width:(integralSumData.size()) height:1 mipmapped:NO];
textureDescriptor.usage = MTLTextureUsageShaderRead | MTLTextureUsageShaderWrite;
id<MTLTexture> texture = [_device newTextureWithDescriptor:textureDescriptor];
// Calculate the number of bytes per row in the image.
NSUInteger bytesPerRow = integralSumData.size() * sizeof(float);
MTLRegion region =
{
{ 0, 0, 0 }, // MTLOrigin
{integralSumData.size(), 1, 1} // MTLSize
};
// Copy the bytes from the data object into the texture
[texture replaceRegion:region
mipmapLevel:0
withBytes:integralSumData.data()
bytesPerRow:bytesPerRow];
MTLTextureDescriptor *textureDescriptor2 = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatR32Float
width:(integralSumData.size()) height:1 mipmapped:NO];
textureDescriptor2.usage = MTLTextureUsageShaderRead | MTLTextureUsageShaderWrite;
id<MTLTexture> outtexture = [_device newTextureWithDescriptor:textureDescriptor2];
// Create a MPS filter.
MPSImageIntegral *integral = [[MPSImageIntegral alloc] initWithDevice: _device];
MPSOffset offset = { 0,0,0};
[integral setOffset:offset];
[integral setEdgeMode:MPSImageEdgeModeZero];
[integral encodeToCommandBuffer:commandBuffer sourceTexture:texture destinationTexture:outtexture];
[commandBuffer commit];
[commandBuffer waitUntilCompleted];
But, when I check my outtexture values, its all zeroes. Am I doing something wrong? Is this a correct way in which I shall use MPSImageIntegral?
I'm using the following code to read values written into the outTexture:
float outData[100];
[outtexture getBytes:outData bytesPerRow:bytesPerRow fromRegion:region mipmapLevel:0];
for(int i = 0; i < 100; i++)
std::cout << outData[i] << "\n";
Thanks
As pointed out by #Matthijis: All I had to do was use an MTLBlitEncoder to make sure I synchronise my MTLTexture before reading it into CPU, and it worked like charm!

Issue in plotting resultant bit map of two bit maps difference

I want to compare one bitmap with another bitmap (reference bitmap) and draw all the difference of it in resultant bit map.
Using below code I am able to draw only difference area but not with exact color of it.
Here is my code
Bitmap ResultantBitMap = new Bitmap(bitMap1.Height, bitMap2.Height);
BitmapData bitMap1Data = bitMap1.LockBits(new Rectangle(0, 0, bitMap1.Width, bitMap1.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
BitmapData bitMap2Data = bitMap2.LockBits(new Rectangle(0, 0, bitMap2.Width, bitMap2.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
BitmapData bitMapResultantData = ResultantBitMap.LockBits(new Rectangle(0, 0, ResultantBitMap.Width, ResultantBitMap.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
IntPtr scan0 = bitMap1Data.Scan0;
IntPtr scan02 = bitMap2Data.Scan0;
IntPtr scan0ResImg1 = bitMapResultantData.Scan0;
int bitMap1Stride = bitMap1Data.Stride;
int bitMap2Stride = bitMap2Data.Stride;
int ResultantImageStride = bitMapResultantData.Stride;
for (int y = 0; y < bitMap1.Height; y++)
{
//define the pointers inside the first loop for parallelizing
byte* p = (byte*)scan0.ToPointer();
p += y * bitMap1Stride;
byte* p2 = (byte*)scan02.ToPointer();
p2 += y * bitMap2Stride;
byte* pResImg1 = (byte*)scan0ResImg1.ToPointer();
pResImg1 += y * ResultantImageStride;
for (int x = 0; x < bitMap1.Width; x++)
{
//always get the complete pixel when differences are found
if (Math.Abs(p[0] - p2[0]) >= 20 || Math.Abs(p[1] - p2[1]) >= 20 || Math.Abs(p[2] - p2[2]) >= 20)
{
pResImg1[0] = p2[0];// B
pResImg1[1] = p2[1];//R
pResImg1[2] = p2[2];//G
pResImg1[3] = p2[3];//A (Opacity)
}
p += 4;
p2 += 4;
pResImg1 += 4;
}
}
bitMap1.UnlockBits(bitMap1Data);
bitMap2.UnlockBits(bitMap2Data);
ResultantBitMap.UnlockBits(bitMapResultantData);
ResultantBitMap.Save(#"c:\\abcd\abcd.jpeg");
What I want is the difference image with exact color of the reference image.
It's hard to tell what's going on without knowing what all those library calls and "+= 4" are but, are you sure p and p2 correspond to the first and second images of your diagram?
Also, your "Format32bppArgb" format suggests that [0] corresponds to alpha, not to red. Maybe there's a problem with that, too.

DirectX 11 Map deviceContext failed

I have an application that render a cube and do some other syuff. But unfortunately, when I want to move some vertices with the mouse. I have an error. When I map the vertex buffer, the vertices recorded in another struct array is empty. For testing my application, I just put a map after creating the vertexbuffer in order to see if the debugger show me the real numbers. It fails too. Everything is populated with a 0.00 value (positions, normals, tangents...).What is the problem ?
Here you can find the code.
D3D11_BUFFER_DESC bd;
ZeroMemory( &bd, sizeof(bd) );
bd.Usage = D3D11_USAGE_DYNAMIC; //D3D11_USAGE_DEFAULT; //D3D11_USAGE_DYNAMIC;// D3D11_USAGE_DEFAULT;
bd.ByteWidth = CBSize( sizeof( VERTEX_PNTTB)* NumVertices);
bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
bd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; // 0; // D3D11_CPU_ACCESS_READ; //D3D11_CPU_ACCESS_WRITE; // 0;
bd.StructureByteStride = 0;
bd.MiscFlags = 0;
//bd.StructureByteStride
//bd.MiscFlags = 0;
D3D11_SUBRESOURCE_DATA InitData;
ZeroMemory( &InitData, sizeof(InitData) );
InitData.pSysMem = (void*)vertices; //(void*)(mesh->GetVertexes().data()); //vertices; //(float*)vertices; // (UINT*)vertices;
InitData.SysMemPitch=0;
ID3D11Buffer* vbuff = NULL;
hr = device->CreateBuffer(&bd, &InitData, &vbuff); // &(m->vertexBuffer)); //&m_pVertexBuffer );
//if( FAILED( hr ) )
// return hr;
//}
m->vertexBuffer = vbuff;
D3D11_MAPPED_SUBRESOURCE mappedResource;
ID3D11Buffer* &buff = vbuff;
//g_pImmediateContext->CopyResource(buff, mElements[ 0 ]->getElement().model ->Meshes()[selectedMeshIndex]->VertexBuffer());
hr = g_pImmediateContext->Map( buff, 0, D3D11_MAP_WRITE_DISCARD ,0, &mappedResource); // D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
if (SUCCEEDED(hr))
{
VERTEX_PNTTB *vertices = (VERTEX_PNTTB *)mappedResource.pData;
// Fill vertex buffer
//vertices[0].position = vertices[0]; // XMFLOAT3(toX0,toY0,0); //-1;//toX0; //-1;//vf1.x; // x0;
/*for(UINT i=0; i<faces_indices.size(); i++)
{
vertices[ faces_indices[i] ].position.x = vertices[faces_indices[i] ].position.x + DirectX::XMFLOAT3(20*dx,20*dy,0).x;
vertices[ faces_indices[i] ].position.y = vertices[faces_indices[i] ].position.y + DirectX::XMFLOAT3(20*dx,20*dy,0).y;
vertices[ faces_indices[i] ].position.z = vertices[faces_indices[i] ].position.z + DirectX::XMFLOAT3(20*dx,20*dy,0).z;
}*/
//g_pImmediateContext->Unmap( mElements[ 0 ]->getElement().model ->Meshes()[selectedMeshIndex]->VertexBuffer(), 0);
g_pImmediateContext->Unmap( buff, 0);
}
Generally you don't want to create Vertex Buffer or Index Buffers in CPU-readable memory as it has a major negative performance impact. You'll find it's much better to have static VB/IB and another copy of the data in standard RAM for the CPU to modify.
That said, you can create the Direct3D 11 buffer in shared memory by using D3D11_USAGE_DYNAMIC and D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE. You then call Map with D3D11_MAP_READ_WRITE.
You definitely should not use D3D11_MAP_WRITE_DISCARD which gives you a fresh piece of memory that has no data in it which will overwrite the old data when you call Unmap.

Compose new 32bit bitmap with alpha channel from other 32bit bitmaps

I several a 32bit bitmap with Alpha channel.
I need to compose a new Bitmap that has again an alpha channel. So the final bitmap is later used with AlphaBlend.
There is no need for stretching. If there would be no alpha channel, I would just use BitBlt to create the new bitmap.
I am not using managed code, I just want to do this with standard GDI / WinAPI functions. Also I am interested in a solution that there is no need for some special libraries.
TIA
Note: I know that I can use several AphaBlend functions to do the same composition in the final output. But for the ease of use in my program I would prefer to compose such a bitmap once.
You can go through every pixel and compose them manually:
void ComposeBitmaps(BITMAP* bitmaps, int bitmapCount, BITMAP& outputBitmap)
{
for(int y=0; y<outputBitmap.bmHeight; ++y)
{
for(int x=0; x<outputBitmap.bmWidth; ++x)
{
int b = 0;
int g = 0;
int r = 0;
int a = 0;
for(int i=0; i<bitmapCount; ++i)
{
unsigned char* samplePtr = (unsigned char*)bitmaps[i].bmBits+(y*outputBitmap.bmWidth+x)*4;
b += samplePtr[0]*samplePtr[3];
g += samplePtr[1]*samplePtr[3];
r += samplePtr[2]*samplePtr[3];
a += samplePtr[3];
}
unsigned char* outputSamplePtr = (unsigned char*)outputBitmap.bmBits+(y*outputBitmap.bmWidth+x)*4;
if(a>0)
{
outputSamplePtr[0] = b/a;
outputSamplePtr[1] = g/a;
outputSamplePtr[2] = r/a;
outputSamplePtr[3] = a/bitmapCount;
}
else
outputSamplePtr[3] = 0;
}
}
(Assuming all bitmaps are 32-bit and have the same width and height)
Or, if you want to draw bitmaps one on top of another, rather than mix them in equal proportions:
unsigned char* outputSamplePtr = (unsigned char*)outputBitmap.bmBits+(y*outputBitmap.bmWidth+x)*4;
outputSamplePtr[3] = 0;
for(int i=0; i<bitmapCount; ++i)
{
unsigned char* samplePtr = (unsigned char*)bitmaps[i].bmBits+(y*outputBitmap.bmWidth+x)*4;
outputSamplePtr[0] = (outputSamplePtr[0]*outputSamplePtr[3]*(255-samplePtr[3])+samplePtr[0]*samplePtr[3]*255)/(255*255);
outputSamplePtr[1] = (outputSamplePtr[1]*outputSamplePtr[3]*(255-samplePtr[3])+samplePtr[1]*samplePtr[3]*255)/(255*255);
outputSamplePtr[2] = (outputSamplePtr[2]*outputSamplePtr[3]*(255-samplePtr[3])+samplePtr[2]*samplePtr[3]*255)/(255*255);
outputSamplePtr[3] = samplePtr[3]+outputSamplePtr[3]*(255-samplePtr[3])/255;
}
I found the following solution that fits best for me.
I Create a new target bitmap with CreateDIBSection
I prefill the new bitmap with fully transparent pixels. (FillMemory/ZeroMemory)
I Receive the Pixel that needs to be copied with GetDIBits. If possible form the width I directly copy the rows into the buffer I previously created. Otherwise I copy the data row by row into the buffer created in step.
The resulting bitmap can be used with AlphaBlend and in CImageList objects.
Because the bitmaps don't overlap I don't need take care about the target data.

How to change default sound playback device programmatically?

How to change the default default audio device for playback and recording in vista programmatically ?
Is there any registry setting for it like sound manager in window XP?
Which API does it?
System Tray Audio Device Switcher uses "Software\Microsoft\Multimedia\Sound Mapper", "Playback" to set the index of the sound device which was obtained by enumeration the devices.
mciSendCommand from "winmm.dll" is also used
In this source code you will find the registry keys used to achieve that.
If this doesn't work you could give Process Monitor a try and monitor all registry activities of windows when you change the default device. On my Vista installation the control panel twiddles with "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\MMDevices\Audio\Render\"
For Vista see
http://www.vistaaudiochanger.com/
There is no public API which allows you to change the default audio device, that is functionality that is considered to be under the users control. This has always been the case in Windows.
Having said that, if you search the web, there are a number of people who have reverse engineered the APIs that are used in Windows Vista to do this, but I'm not going to point you to them (the reverse engineered APIs are internal unsupported APIs and may change without notice from Microsoft). You use these solutions at your own peril.
I really don't know if anyone still needs this, but here is my solution.
Actually, it's for the capture device, but it can be changed easily to the render device.
It sets 3 registry values in the device's key to the current time. Magic, but that's how it works.
Note: only tested on Win7 x64
void SetDefaultRecordDevice(tstring strDeviceName){
const int BUFF_LEN = 260;
//HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\MMDevices\Audio\Capture\{79434968-09f6-4dff-8086-c5e618b21473}\Role:0:
//"DE 07 08 00 06 00 10 00 15 00 38 00 1E 00 48 03"
HKEY hkCaptureDevices;
RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\MMDevices\\Audio\\Capture") , 0, KEY_ENUMERATE_SUB_KEYS | KEY_WOW64_64KEY, &hkCaptureDevices);
TCHAR lpwstrDeviceGuidKey[BUFF_LEN];
DWORD dwDeviceGuidKeySize = BUFF_LEN;
for(int i=0;RegEnumKeyEx(hkCaptureDevices, i, lpwstrDeviceGuidKey, &dwDeviceGuidKeySize, 0, 0, 0, 0) != ERROR_NO_MORE_ITEMS; ++i){
dwDeviceGuidKeySize = BUFF_LEN;
HKEY hkProps;
RegOpenKeyEx(hkCaptureDevices, (tstring(lpwstrDeviceGuidKey) + _T("\\Properties")).c_str() , 0, KEY_READ | KEY_WOW64_64KEY, &hkProps);
TCHAR data[BUFF_LEN];
DWORD dwDataSize = BUFF_LEN;
if(RegQueryValueEx(hkProps, _T("{a45c254e-df1c-4efd-8020-67d146a850e0},2"), 0, 0, (LPBYTE)data, &dwDataSize) != ERROR_SUCCESS){
continue;
} else {
tstring strCurrentDeviceName(data);
// TODO név általánosítás
if(strDeviceName == strCurrentDeviceName){
HKEY hkGuid;
RegOpenKeyEx(hkCaptureDevices, lpwstrDeviceGuidKey , 0, KEY_READ | KEY_SET_VALUE | KEY_QUERY_VALUE | KEY_WOW64_64KEY | KEY_NOTIFY , &hkGuid);
time_t CurrentTime;
time(&CurrentTime);
time_t now = time(0);
struct tm tstruct;
gmtime_s(&tstruct, &now);
// Visit http://en.cppreference.com/w/cpp/chrono/c/strftime
// for more information about date/time format
char CustomRegistryDateValue[16];
WORD year = tstruct.tm_year + 1900;
WORD month = tstruct.tm_mon+1;
WORD dayOfTheWeek = tstruct.tm_wday;
WORD day = tstruct.tm_mday;
WORD hour = tstruct.tm_hour;
WORD minute = tstruct.tm_min;
WORD second = tstruct.tm_sec;
WORD millisec = 0x0; // hasrautés
int k = 0;
*((WORD*)CustomRegistryDateValue + k++) = year;
*((WORD*)CustomRegistryDateValue + k++) = month;
*((WORD*)CustomRegistryDateValue + k++) = dayOfTheWeek;
*((WORD*)CustomRegistryDateValue + k++) = day;
*((WORD*)CustomRegistryDateValue + k++) = hour;
*((WORD*)CustomRegistryDateValue + k++) = minute;
*((WORD*)CustomRegistryDateValue + k++) = second;
*((WORD*)CustomRegistryDateValue + k++) = millisec;
RegSetValueExA(hkGuid, ("Role:0"), 0, REG_BINARY, (LPBYTE)CustomRegistryDateValue, 16);
RegSetValueExA(hkGuid, ("Role:1"), 0, REG_BINARY, (LPBYTE)CustomRegistryDateValue, 16);
RegSetValueExA(hkGuid, ("Role:2"), 0, REG_BINARY, (LPBYTE)CustomRegistryDateValue, 16);
RegFlushKey(hkGuid);
RegCloseKey(hkGuid);
}
}
RegCloseKey(hkProps);
}
RegCloseKey(hkCaptureDevices);
}

Resources