Trouble properly setting up descriptor sets for multiple models in Vulkan - graphics

I'm having a problem trying to implement multiple models in Vulkan. I used the official Vulkan tutorial to set up most of the boilerplate code, then the rest is custom. At the moment, I am able to get one model to render perfectly, while the other identical model renders in about 60% of frames and doesn't render at all in others, as shown in the gif: Engine output
I've tried changing many things to fix it, but I can't pinpoint what is causing the issue. Any help would be appreciated wholly. I'm attaching the code snippets I think most relevant but I can include more if it helps solve the issue.
CreateCommandBuffers():
...
vkCmdBindVertexBuffers(commandBuffers[i], 0, 1, vertexBuffers, offsets);
vkCmdBindIndexBuffer(commandBuffers[i], indexBuffer, 0, VK_INDEX_TYPE_UINT32);
vkCmdBindDescriptorSets(commandBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &descriptorSets[i], 0, nullptr); //COMEBACKTO
vkCmdDrawIndexed(commandBuffers[i], models[0].getModelIndicesSize(), 1, 0, 0, 0);
vkCmdBindDescriptorSets(commandBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &descriptorSets[i + 1], 0, nullptr);
vkCmdDrawIndexed(commandBuffers[i], models[0].getModelIndicesSize(), 1, 0, 0, 0);
...
createDescriptorSets():
std::vector<VkDescriptorSetLayout> layouts(swapChainImages.size() * models.size(), descriptorSetLayout);
VkDescriptorSetAllocateInfo allocInfo{};
allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
allocInfo.descriptorPool = descriptorPool;
allocInfo.descriptorSetCount = static_cast<uint32_t>(swapChainImages.size() * models.size());
allocInfo.pSetLayouts = layouts.data();
descriptorSets.resize(swapChainImages.size() * models.size());
if (vkAllocateDescriptorSets(device, &allocInfo, descriptorSets.data()) != VK_SUCCESS) {
throw std::runtime_error("failed to allocate descriptor sets!");
}
for (int i = 0; i < swapChainImages.size(); i++) {
std::vector<VkWriteDescriptorSet> descWrites;
descWrites.resize(models.size() * 2);
for (int j = 0; j < models.size(); j++) {
VkDescriptorBufferInfo bufferInfo{};
bufferInfo.offset = 0;
bufferInfo.range = sizeof(UniformBufferObject);
//bufferInfo.buffer = uniformBuffers[i];
bufferInfo.buffer = uniformBuffers[j + (i * (models.size() - 1))];
VkDescriptorImageInfo imageInfo{};
imageInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
imageInfo.imageView = textureImageView; //set these up
imageInfo.sampler = textureSampler; //set these up
int tmp = j * 2;
int tmp1 = tmp + 1;
descWrites[tmp].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
descWrites[tmp].dstSet = descriptorSets[j + (i * (models.size() - 1))];
descWrites[tmp].dstBinding = 0;
descWrites[tmp].dstArrayElement = 0;
descWrites[tmp].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
descWrites[tmp].descriptorCount = 1;
descWrites[tmp].pBufferInfo = &bufferInfo;
descWrites[tmp1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
descWrites[tmp1].dstSet = descriptorSets[j + (i * (models.size() - 1))];
descWrites[tmp1].dstBinding = 1;
descWrites[tmp1].dstArrayElement = 0;
descWrites[tmp1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
descWrites[tmp1].descriptorCount = 1;
descWrites[tmp1].pImageInfo = &imageInfo;
}
vkUpdateDescriptorSets(device, static_cast<uint32_t>(descWrites.size()), descWrites.data(), 0, nullptr);
}

Related

linux set Consol

https://en.wikipedia.org/wiki/ANSI_escape_code#CSI_sequences
I tried to use this site but I couldn't, can you help me?
this cod is running in windows system but i want to this code change linux. What should i do?
tetro is my class vector. I want it to go slowly down the map
for (int i = 0; i < tetro.size(); i++) {
string tetrodemo = tetro[i].print();
tetro[i].Board(board);
for (int j = 0; j < 10; j++) {
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos = { 1, (j + 1) };
SetConsoleCursorPosition(hConsole, pos);
WriteConsoleA(hConsole, tetrodemo.c_str(), tetrodemo.size(), NULL, NULL);
Sleep(WAIT_LOOP);
}

Cs50 edges filter is failing to process corner and edges pixels

In the problem description, we were told to take outer pixels as having a value of "0".
I discriminate de outer pixels so as to do nothing with them, they won´t add a value, because multiplying by 0 gets 0.
I can´t seem to find where i went wrong. Can somebody give me a clue. Thanks a lot.
here is my code
// Detect edges
void edges(int height, int width, RGBTRIPLE image[height][width])
{
// Make copy
RGBTRIPLE copy[height][width];
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
copy[i][j] = image[i][j];
}
}
double gxB, gxR, gxG, gyR, gyB, gyG;
int GX[3][3] = {{-1, 0, 1}, {-2, 0, 2}, {-1, 0, 1}};
int GY[3][3] = {{-1, -2, -1}, {0, 0, 0}, {1, 2, 1}};
// Loop through pixels
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
gxB = 0;
gxR = 0;
gxG = 0;
gyR = 0;
gyB = 0;
gyG = 0;
// Loop through 3x3 box
for (int row = (i - 1); row < (i + 2); row++)
{
for (int col = (j - 1); col < (j +2); col++)
{
// Discriminate border pixels
if (exists(row, col, height, width))
{
gxR += copy[row][col].rgbtRed * GX[row][col];
gxG += copy[row][col].rgbtGreen * GX[row][col];
gxB += copy[row][col].rgbtBlue * GX[row][col];
gyR += copy[row][col].rgbtRed * GY[row][col];
gyG += copy[row][col].rgbtGreen * GY[row][col];
gyB += copy[row][col].rgbtBlue * GY[row][col];
}
else
{
gxR += 0.0;
gxG += 0.0;
gxB += 0.0;
gyR += 0.0;
gyG += 0.0;
gyB += 0.0;
}
}
}
image[i][j].rgbtRed = trimm(round(sqrt((gxR * gxR) + (gyR * gyR))));
image[i][j].rgbtGreen = trimm(round(sqrt((gxG * gxG) + (gyG * gyG))));
image[i][j].rgbtBlue = trimm(round(sqrt((gxB * gxB) + (gyB * gyB))));
}
}
return;
}
bool exists(int ROW, int COL, int H, int W)
{
if (((ROW >= 0) && (ROW < H)) && ((COL >= 0) && (COL < W)))
{
return true;
}
else
{
return false;
}
}
int trimm(int x)
{
if (x < 256)
{
return x;
}
else
{
return 255;
}
}

Confusion about Texture2D and ShaderResourceViews

I am new to Direct3D11 and I am currently trying to create a texture programatically within my code using this code I found online:
// Some Constants
int w = 256;
int h = 256;
int bpp = 4;
int *buf = new int[w*h];
//declarations
ID3D11Texture2D* tex;
D3D11_TEXTURE2D_DESC sTexDesc;
D3D11_SUBRESOURCE_DATA tbsd;
// filling the image
for (int i = 0; i<h; i++)
for (int j = 0; j<w; j++)
{
if ((i & 32) == (j & 32))
buf[i*w + j] = 0x00000000;
else
buf[i*w + j] = 0xffffffff;
}
// setting up D3D11_SUBRESOURCE_DATA
tbsd.pSysMem = (void *)buf;
tbsd.SysMemPitch = w*bpp;
tbsd.SysMemSlicePitch = w*h*bpp; // Not needed since this is a 2d texture
// initializing sTexDesc
sTexDesc.Width = w;
sTexDesc.Height = h;
sTexDesc.MipLevels = 1;
sTexDesc.ArraySize = 1;
sTexDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
sTexDesc.SampleDesc.Count = 1;
sTexDesc.SampleDesc.Quality = 0;
sTexDesc.Usage = D3D11_USAGE_DEFAULT;
sTexDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
sTexDesc.CPUAccessFlags = 0;
sTexDesc.MiscFlags = 0;
hr = m_pd3dDevice->CreateTexture2D(&sTexDesc, &tbsd, &tex);
and that' all fine and dandy, but I am a bit confused about how to actually load this into the shader. Below I initialized this ID3D11ShaderResourceView:
ID3D11ShaderResourceView* m_pTextureRV = nullptr;
I found on the Microsoft tutorials I need to use the CreateShaderResourceView. But how exactly do I use it? I tried this:
hr = m_pd3dDevice->CreateShaderResourceView(tex, NULL , m_pTextureRV);
but it gives me an error, telling me that m_pTextureRV is not a valid argument for the function. What am I doing wrong here?
The correct way to call that function is:
hr = m_pd3dDevice->CreateShaderResourceView(tex, nullptr, &m_pTextureRV);
Remember that ID3D11ShaderResourceView* is a pointer to an interface. You need a pointer-to-a-pointer to get a new instance of one back.
You should really consider using a COM smart-pointer like Microsoft::WRL::ComPtr instead of raw pointers for these interfaces.
Once you have created the shader resource view for your texture object, then you need to associate it with whatever slot the HLSL expects to find it in. So, for example, if you were to write an HLSL source file as:
Texture2D texture : register( t0 );
SamplerState sampler: register( s0 );
float4 PS(float2 tex : TEXCOORD0) : SV_Target
{
return texture.Sample( sampler, tex );
}
Then compile it as a Pixel Shader, and bind it to the render pipeline via PSSetShader. Then you'd need to call:
ID3D11ShaderResourceView* srv[1] = { m_pTextureRV };
m_pImmediateContext->PSSetShaderResources( 0, 1, srv );
Of course you also need a ID3D11SamplerState* sampler bound as well:
ID3D11SamplerState* m_pSamplerLinear = nullptr;
D3D11_SAMPLER_DESC sampDesc = {};
sampDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
sampDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
sampDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
sampDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
sampDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;
sampDesc.MinLOD = 0;
sampDesc.MaxLOD = D3D11_FLOAT32_MAX;
hr = m_pd3dDevice->CreateSamplerState( &sampDesc, &m_pSamplerLinear );
Then when you are about to draw:
m_pImmediateContext->PSSetSamplers( 0, 1, &m_pSamplerLinear );
I strongly recommend you check out the DirectX Tool Kit and the tutorials there.

Remove bleed from Pdf and merge

I am trying remove 3mm bleed size from pdf. by using below criteria
My source file is Source file
I am using below code to trim left and right
public void TrimLeftandRight(string sourceFilePath, string outputFilePath)
{
PdfReader pdfReader = new PdfReader(sourceFilePath);
float width = (float)GetPDFwidth(sourceFilePath);
float height = (float)GetPDFHeight(sourceFilePath);
float widthTo_Trim = iTextSharp.text.Utilities.MillimetersToPoints(3);
PdfRectangle rectrightside = new PdfRectangle(0, 0, width - widthTo_Trim, height);
PdfRectangle rectLeftside = new PdfRectangle(widthTo_Trim, 0, width, height);
// int[] pagealignment = new int[] { 8, 1, 2, 7, 6, 3, 4, 5 };
int[] pagealignment = new int[] { 6, 1, 2, 5, 4, 3 };
using (var output = new FileStream(outputFilePath, FileMode.CreateNew, FileAccess.Write))
{
// Create a new document
Document doc = new Document();
// Make a copy of the document
PdfSmartCopy smartCopy = new PdfSmartCopy(doc, output);
// Open the newly created document
doc.Open();
// Loop through all pages of the source document
for (int i = 1; i <= pdfReader.NumberOfPages; i++)
{
// Get a page
var page = pdfReader.GetPageN(i);
// Apply the rectangle filter we created
switch (i)
{
case 6:
page.Put(PdfName.CROPBOX, rectLeftside);
page.Put(PdfName.MEDIABOX, rectrightside);
break;
case 2:
page.Put(PdfName.MEDIABOX, rectrightside);
break;
case 4:
page.Put(PdfName.MEDIABOX, rectLeftside);
break;
case 1:
page.Put(PdfName.MEDIABOX, rectLeftside);
break;
case 5:
page.Put(PdfName.MEDIABOX, rectrightside);
// page.Put(PdfName.CROPBOX, rectLeftside);
break;
case 3:
page.Put(PdfName.CROPBOX, rectLeftside);
page.Put(PdfName.MEDIABOX, rectrightside);
break;
}
// Copy the content and insert into the new document
var copiedPage = smartCopy.GetImportedPage(pdfReader, i);
smartCopy.AddPage(copiedPage);
}
// Close the output document
smartCopy.Close();
doc.Close();
doc.Dispose();
}
}
the output of above code produces
Trimmed left and right file
and I used below code to merge trimmed files
public void CreategateFinalOutput(string inputfile)
{
double widthinpoints = iTextSharp.text.Utilities.MillimetersToPoints(897);
string onlyfilename = Path.GetFileName(inputfile);
// string originalfilename = Server.MapPath("~/Uploads/" + onlyfilename);
int Noofpagesinpdf = GetNoofpagesofpdf(inputfile);
// var a3doc = new Document(PageSize.A3.Rotate(), 0, 0, 0, 0);
double originalwidth = GetPDFwidth(inputfile);
float widthTo_Trim = iTextSharp.text.Utilities.MillimetersToPoints(3);
double width = (GetPDFwidth(inputfile) * 3);
width = widthinpoints;
double height = GetPDFHeight(inputfile);
var a3reader = new PdfReader(inputfile);
var a3doc = new Document(new Rectangle((float)width, (float)height));
var a3writer = PdfWriter.GetInstance(a3doc, new FileStream(Server.MapPath("~/RP/" + onlyfilename), FileMode.Create));
a3doc.Open();
var a3cb = a3writer.DirectContent;
PdfImportedPage page;
int totalPages = a3reader.NumberOfPages;
// int[] pagealignment = new int[] { 8, 1, 2, 7, 6, 3, 4, 5 };
int[] pagealignment = new int[] { 5, 6, 1, 2, 3, 4 };
int iteration = 1;
for (int i = 1; i <= totalPages; i++)
{
a3doc.NewPage();
var a3size = new Document(new Rectangle((float)width, (float)height));
//new code
int fistpage = 0;
int secpage = 0;
int thirdpage = 0;
switch (iteration)
{
case 1:
fistpage = 5;
secpage = 6;
thirdpage = 1;
break;
case 2:
fistpage = 2;
secpage = 3;
thirdpage = 4;
break;
}
double trimwidth = iTextSharp.text.Utilities.MillimetersToPoints(3);
page = a3writer.GetImportedPage(a3reader, fistpage);
double pagewidth = page.Width;
a3cb.AddTemplate(page, 0, 0);
i++;
page = a3writer.GetImportedPage(a3reader, secpage);
double pagewidtha = page.Width;
a3cb.AddTemplate(page, (float)(pagewidtha), 0);
i++;
page = a3writer.GetImportedPage(a3reader, thirdpage);
double pagewidthaThird = page.Width;
// a3cb.AddTemplate(page, (int)(a3size.Width / 2), 0); //commented
a3cb.AddTemplate(page, (float)(pagewidthaThird + pagewidth), 0);
iteration++;
a3doc.Close();
}
}
When i merged pdf by using above code the out put is not as per desire
Final output
Here we have removed borders of page 5 and 6 but when we merged there is border appearing .
You can see it downloading pdfs.. apologies for a such a big code. the help will be highly appreciated
I request to download pdfs and check pdfs for better views

j2me program to create a GRID menu?

I want to create a list of operation's in a grid view. For example visit this URL.
http://cdn-static.cnet.co.uk/i/product_media/40000186/nokia1616_01.jpg
You can look at this question or this page(and use LWUIT or CustomItems) or extend "canvas".In this way you need to two pictures for every operation in grid view.One for normal state and another for highlighted.Here is a simple canvas that represents 4 operations in 2*2 grid:
public class GridCanvas extends Canvas {
int highlightedRow = 0;
int highlightedColumn = 0;
Image[][] normalImageMat;
Image[][] highlightedImageMat;
Image[][] imageMat;
int gridColumnNo;
int gridRowNo;
/**
* constructor
*/
public GridCanvas() {
gridColumnNo = 2;
gridRowNo = 2;
normalImageMat = new Image[gridRowNo][gridColumnNo];
highlightedImageMat = new Image[gridRowNo][gridColumnNo];
imageMat = new Image[gridRowNo][gridColumnNo];
try {
for (int i = 0; i < gridRowNo; i++) {
for (int j = 0; j < gridColumnNo; j++) {
normalImageMat[i][j] = Image.createImage("/hello/normalImage" + i + j + ".png");
}
}
for (int i = 0; i < gridRowNo; i++) {
for (int j = 0; j < gridColumnNo; j++) {
highlightedImageMat[i][j] = Image.createImage("/hello/highlightedImage" + i + j + ".png");
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* paint
*/
public void paint(Graphics g) {
g.setColor(255, 255, 255);
g.fillRect(0, 0, getWidth(), getHeight());
for (int i = 0; i < gridRowNo; i++) {
System.arraycopy(normalImageMat[i], 0, imageMat[i], 0, 2);
}
imageMat[highlightedRow][highlightedColumn] = highlightedImageMat[highlightedRow][highlightedColumn];
int width = 0;
int height = 0;
for (int i = 0; i < gridRowNo; i++) {
for (int j = 0; j < gridColumnNo; j++) {
g.drawImage(imageMat[i][j], width, height, 0);
width = width + imageMat[i][j].getWidth();
}
width = 0;
height = height + imageMat[0][0].getHeight();
}
}
/**
* Called when a key is pressed.
*/
protected void keyPressed(int keyCode) {
int gameAction = this.getGameAction(keyCode);
if (gameAction == RIGHT) {
highlightedColumn = Math.min(highlightedColumn + 1, gridColumnNo - 1);
} else if (gameAction == LEFT) {
highlightedColumn = Math.max(highlightedColumn - 1, 0);
} else if (gameAction == UP) {
highlightedRow = Math.max(0, highlightedRow - 1);
} else if (gameAction == DOWN) {
highlightedRow = Math.min(gridRowNo - 1, highlightedRow + 1);
}
repaint();
}
}
In real samples you would to detect gridColumnNo and gridRowNo due to screen and your icons dimensions.
If you can not go with LWUIT (license, library size, etc) and do not want to leave the screen rendering to LCDUI (CustomItem), you should extend Canvas.
I have shared code for an adaptive grid at http://smallandadaptive.blogspot.com.br/2010/12/touch-menu.html Feel free to use it.
At this sample all items are Strings, but you can change the TouchItem to draw Images instead.

Resources