I'm trying to get a keras model i converted to tensorflow js, to work in react native but the model keeps giving bad responses. Did some digging and found realized that the tensor i passed into model.predict is some how being changed causing it to give the same incorrect prediction. Any suggestions would be appreciated. I'm pretty much hard stuck. Code below:
import React, {useState, useEffect} from 'react';
import {View, Text, Button} from 'react-native';
import * as tf from '#tensorflow/tfjs';
import {
bundleResourceIO
} from '#tensorflow/tfjs-react-native';
import * as mobilenet from '#tensorflow-models/mobilenet';
function thing() {
const [model, setModel] = useState(null);
const [tensor, setTensor] = useState(null);
async function loadModel() {
const modelJson = require('./assets/model.json');
const weight = require('./assets/group1-shard1of1.bin');
const backend = await tf.ready();
const item = await tf.loadLayersModel(
bundleResourceIO(modelJson, weight)
);
const tfTensor = tf.tensor([[0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]);
setModel(item);
setTensor(tfTensor);
}
useEffect(() => {
loadModel();
}, []);
async function test() {
if(tensor !== null && model !== null) {
const result = await model.predict(tensor);
console.log(result.dataSync())
}
}
return (
<View>
<Button
onPress={test}
title="click"
color="#841584"
accessibilityLabel="Learn more about this purple button"
/>
</View>
);
}
export default thing;
Just like changing a single pixel in an image doesn't change the image, changing one bit in an array doesn't significantly adjust the prediction.
I ran mobilenet on a black 224x224 image and it predicted class 819 (whatever that is). Then I changed the top-left pixel to white and re-ran mobilenet and it still classifies as class 819.
See example code here
Changing a single bit does not have a cascading effect like a hash function. Mobilenet, by its nature is resilient to noise.
// Connections
int mreset = 10;
int shift = 11;
int store = 12;
int data = 13;
int taster = 9;
int a, tasterstatus = 0;
// Delays
int tasterdelay = 250;
int fast = 5;
int stage1 = 1000;
int stage2 = 1000;
unsigned long time_now = 0;
// Pattern
int firstlevel[24] = {1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0};
int secondlevel[24] = {0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0};
int thirdlevel[24] = {0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0};
int fourthlevel[24] = {0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0};
int leftside[24] = {1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int ndleftside[24] = {1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int ndrightside[24] = {1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0};
int rightside[24] = {1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0};
int backside[24] = {1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0};
int ndbackside[24] = {1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0};
int ndfrontside[24] = {1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0};
int frontside[24] = {1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0};
int cube1[24] = {1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int cube2_1[24] = {1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int cube2_2[24] = {0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int bigcube1_1[24] = {1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0};
int bigcube1_2[24] = {0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0};
int cube3_1[24] = {0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0};
int cube3_2[24] = {0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0};
int cube4[24] = {0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0};
int cube5_1[24] = {1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0};
int cube5_2[24] = {0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0};
int cube6[24] = {1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int cube7_1[24] = {0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0};
int cube7_2[24] = {0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0};
int cube8[24] = {0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0};
int custom1[24] = {1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0};
int custom2[24] = {0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0};
void setup() {
//Set Outputs
pinMode(shift , OUTPUT);
pinMode(store , OUTPUT);
pinMode(data , OUTPUT);
pinMode(mreset , OUTPUT);
pinMode(taster , INPUT);
// Set Shift Register Master Reset On/Off
digitalWrite(mreset, LOW);
digitalWrite(store, HIGH);
digitalWrite(mreset, HIGH);
digitalWrite(store, LOW);
}
void loop() {
//Waiting for Button PRess
tasterstatus = digitalRead(taster);
if (tasterstatus == HIGH) {
a++;
delay(tasterdelay);
}
//Choose Pattern
if (a == 1) {
pattern1();
}
if (a == 2) {
pattern2();
}
}
void pattern1() {
//Equate Millis with time_now
time_now = millis();
// First Sample for LED Cube
for (int i = 0; i < 24; i++) {
digitalWrite(shift, LOW);
digitalWrite(data, firstlevel[i]);
digitalWrite(shift, HIGH);
}
digitalWrite(store, HIGH);
digitalWrite(store, LOW);
// "Delay"
while (millis() < time_now + stage1) {
tasterstatus = digitalRead(taster);
// Checking Button State
if (tasterstatus == HIGH) {
delay(tasterdelay);
a++;
pattern2();
}
}
// Clearing Shift Registers with Master Reset
digitalWrite(mreset, LOW);
digitalWrite(store, HIGH);
digitalWrite(mreset, HIGH);
digitalWrite(store, LOW);
time_now = millis();
for (int i = 0; i < 24; i++) {
digitalWrite(shift, LOW);
digitalWrite(data, secondlevel[i]);
digitalWrite(shift, HIGH);
}
digitalWrite(store, HIGH);
digitalWrite(store, LOW);
while (millis() < time_now + stage1) {
tasterstatus = digitalRead(taster);
if (tasterstatus == HIGH) {
delay(tasterdelay);
a++;
pattern2();
}
}
digitalWrite(mreset, LOW);
digitalWrite(store, HIGH);
digitalWrite(mreset, HIGH);
digitalWrite(store, LOW);
time_now = millis();
for (int i = 0; i < 24; i++) {
digitalWrite(shift, LOW);
digitalWrite(data, thirdlevel[i]);
digitalWrite(shift, HIGH);
}
digitalWrite(store, HIGH);
digitalWrite(store, LOW);
while (millis() < time_now + stage1) {
tasterstatus = digitalRead(taster);
if (tasterstatus == HIGH) {
delay(tasterdelay);
a++;
pattern2();
}
}
digitalWrite(mreset, LOW);
digitalWrite(store, HIGH);
digitalWrite(mreset, HIGH);
digitalWrite(store, LOW);
time_now = millis();
for (int i = 0; i < 24; i++) {
digitalWrite(shift, LOW);
digitalWrite(data, fourthlevel[i]);
digitalWrite(shift, HIGH);
}
digitalWrite(store, HIGH);
digitalWrite(store, LOW);
while (millis() < time_now + stage1) {
tasterstatus = digitalRead(taster);
if (tasterstatus == HIGH) {
delay(tasterdelay);
a++;
pattern2();
}
}
digitalWrite(mreset, LOW);
digitalWrite(store, HIGH);
digitalWrite(mreset, HIGH);
digitalWrite(store, LOW);
}
void pattern2() {
time_now = millis();
for (int i = 0; i < 24; i++) {
digitalWrite(shift, LOW);
digitalWrite(data, cube1[i]);
digitalWrite(shift, HIGH);
}
digitalWrite(store, HIGH);
digitalWrite(store, LOW);
}`enter code here
My Problem Is that when i press the button in Pattern1, Pattern2 Starts 1x time, after that Pattern1 continues where i pressed the Button, runs 1x trough, and after that pattern 2 repeats normaly. Any ideas how to fix it ? Like when i press the Button First Pattern1 repeats infinity as it should. But when i press it again, while Pattern1 is running, its goes to Pattern2 as it should, but it only repeats Pattern2 1x time, than it resumes pattern1, where i pressed the button, and then it repeats pattern2 as it should.
So the Main Problem is that Pattern1 Runs 1x time in pattern2;
Thanks for help
i fixed it by putting a return; after pattern()2; ;)
I wrote a simple OpenGL program which merely renders a cube from an angle. It's as simple as you can get: vertex buffer only (no index buffer), a vertex shader which only multiplies the vertices by an MVP matrix from a uniform buffer, and a static fragment shader which just returns red. More recently, I have tried writing this same program in Vulkan, but I have run into some issues.
I started by following the Intel API without secrets tutorial to setup a simple 2d texture rendering program, but when I took the leap into 3d, I started having issues. In order to debug this, I simplified the program to match my older OpenGL program (removed texturing and some other extra stuff I did in Vulkan), and even went as far as to use the exact same vertex and MVP data. However, I just can't get the cube to render correctly in Vulkan.
I am aware that OpenGL coordinates do not map directly to Vulkan coordinates, as the Y coordinate is flipped, but if anything that should just flip the image upside down, and I already tried switching the Y values in the MVP. I feel like there is some other detail I am missing here with coordinates, but I just can't figure it out searching around and looking at guides about converting OpenGL code bases to Vulkan.
I'm including the data I am uploading to the shaders, and some of the core code from the Vulkan code base. The Vulkan code is in D, so it's similar to C++, but a little different. With the library I'm using for wrapping Vulkan (erupted), the device level functions are loaded into a device dispatch (access as device.dispatch in the code), and when they are called on the dispatch without the vk prefix, the device and command buffer (which is assigned to the dispatch in code) arguments of the function are auto populated.
Vertex Data:
[ [1, 1, 1, 1],
[1, 1, -1, 1],
[-1, 1, -1, 1],
[1, 1, 1, 1],
[-1, 1, -1, 1],
[-1, 1, 1, 1],
[1, 1, 1, 1],
[1, -1, 1, 1],
[1, -1, -1, 1],
[1, 1, 1, 1],
[1, -1, -1, 1],
[1, 1, -1, 1],
[1, 1, -1, 1],
[1, -1, -1, 1],
[-1, -1, -1, 1],
[1, 1, -1, 1],
[-1, -1, -1, 1],
[-1, 1, -1, 1],
[-1, 1, -1, 1],
[-1, -1, -1, 1],
[-1, -1, 1, 1],
[-1, 1, -1, 1],
[-1, -1, 1, 1],
[-1, 1, 1, 1],
[-1, 1, 1, 1],
[-1, -1, 1, 1],
[1, -1, 1, 1],
[-1, 1, 1, 1],
[1, -1, 1, 1],
[1, 1, 1, 1],
[1, -1, 1, 1],
[1, -1, -1, 1],
[-1, -1, -1, 1],
[1, -1, 1, 1],
[-1, -1, -1, 1],
[-1, -1, 1, 1] ]
MVP:
[ [-1.0864, -0.993682, -0.687368, -0.685994],
[0, 2.07017, 0.515526, -0.514496],
[-1.44853, 0.745262, 0.515526, 0.514496],
[-8.04095e-16, 0, 5.64243, 5.83095] ]
Graphics Pipeline Setup:
VkPipelineShaderStageCreateInfo[] shader_stage_infos = [
{
stage: VK_SHADER_STAGE_VERTEX_BIT,
_module: vertex_shader,
pName: "main"
},
{
stage: VK_SHADER_STAGE_FRAGMENT_BIT,
_module: fragment_shader,
pName: "main"
}
];
VkVertexInputBindingDescription[] vertex_binding_descriptions = [
{
binding: 0,
stride: VertexData.sizeof,
inputRate: VK_VERTEX_INPUT_RATE_VERTEX
}
];
VkVertexInputAttributeDescription[] vertex_attribute_descriptions = [
{
location: 0,
binding: vertex_binding_descriptions[0].binding,
format: VK_FORMAT_R32G32B32A32_SFLOAT,
offset: VertexData.x.offsetof
},
{
location: 1,
binding: vertex_binding_descriptions[0].binding,
format: VK_FORMAT_R32G32_SFLOAT,
offset: VertexData.u.offsetof
}
];
VkPipelineVertexInputStateCreateInfo vertex_input_state_info = {
vertexBindingDescriptionCount: vertex_binding_descriptions.length.to!uint,
pVertexBindingDescriptions: vertex_binding_descriptions.ptr,
vertexAttributeDescriptionCount: vertex_attribute_descriptions.length.to!uint,
pVertexAttributeDescriptions: vertex_attribute_descriptions.ptr
};
VkPipelineInputAssemblyStateCreateInfo input_assembly_state_info = {
topology: VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST,
primitiveRestartEnable: VK_FALSE
};
VkPipelineViewportStateCreateInfo viewport_state_info = {
viewportCount: 1,
pViewports: null,
scissorCount: 1,
pScissors: null
};
VkPipelineRasterizationStateCreateInfo rasterization_state_info = {
depthBiasClamp: 0.0,
polygonMode: VK_POLYGON_MODE_FILL,
cullMode: VK_CULL_MODE_FRONT_AND_BACK,
frontFace: VK_FRONT_FACE_COUNTER_CLOCKWISE,
lineWidth: 1
};
VkPipelineMultisampleStateCreateInfo multisample_state_info = {
rasterizationSamples: VK_SAMPLE_COUNT_1_BIT,
minSampleShading: 1
};
VkPipelineColorBlendAttachmentState[] color_blend_attachment_states = [
{
blendEnable: VK_FALSE,
srcColorBlendFactor: VK_BLEND_FACTOR_ONE,
dstColorBlendFactor: VK_BLEND_FACTOR_ZERO,
colorBlendOp: VK_BLEND_OP_ADD,
srcAlphaBlendFactor: VK_BLEND_FACTOR_ONE,
dstAlphaBlendFactor: VK_BLEND_FACTOR_ZERO,
alphaBlendOp: VK_BLEND_OP_ADD,
colorWriteMask:
VK_COLOR_COMPONENT_R_BIT |
VK_COLOR_COMPONENT_G_BIT |
VK_COLOR_COMPONENT_B_BIT |
VK_COLOR_COMPONENT_A_BIT
}
];
VkPipelineColorBlendStateCreateInfo color_blend_state_info = {
logicOpEnable: VK_FALSE,
logicOp: VK_LOGIC_OP_COPY,
attachmentCount: color_blend_attachment_states.length.to!uint,
pAttachments: color_blend_attachment_states.ptr,
blendConstants: [ 0, 0, 0, 0 ]
};
VkDynamicState[] dynamic_states = [
VK_DYNAMIC_STATE_VIEWPORT,
VK_DYNAMIC_STATE_SCISSOR
];
VkPipelineDynamicStateCreateInfo dynamic_state_info = {
dynamicStateCount: dynamic_states.length.to!uint,
pDynamicStates: dynamic_states.ptr
};
VkGraphicsPipelineCreateInfo pipeline_info = {
stageCount: shader_stage_infos.length.to!uint,
pStages: shader_stage_infos.ptr,
pVertexInputState: &vertex_input_state_info,
pInputAssemblyState: &input_assembly_state_info,
pTessellationState: null,
pViewportState: &viewport_state_info,
pRasterizationState: &rasterization_state_info,
pMultisampleState: &multisample_state_info,
pDepthStencilState: null,
pColorBlendState: &color_blend_state_info,
pDynamicState: &dynamic_state_info,
layout: pipeline_layout,
renderPass: render_pass,
subpass: 0,
basePipelineHandle: VK_NULL_HANDLE,
basePipelineIndex: -1
};
VkPipeline[1] pipelines;
checkVk(device.dispatch.CreateGraphicsPipelines(VK_NULL_HANDLE, 1, [pipeline_info].ptr, pipelines.ptr));
pipeline = pipelines[0];
Drawing:
if(device.dispatch.WaitForFences(1, [fence].ptr, VK_FALSE, 1000000000) != VK_SUCCESS)
throw new StringException("timed out waiting for fence");
device.dispatch.ResetFences(1, [fence].ptr);
uint image_index;
switch(device.dispatch.AcquireNextImageKHR(swapchain.swapchain, uint64_t.max, image_available_semaphore, VK_NULL_HANDLE, &image_index)) {
case VK_SUCCESS:
case VK_SUBOPTIMAL_KHR:
break;
case VK_ERROR_OUT_OF_DATE_KHR:
on_window_size_changed();
break;
default:
throw new StringException("unhandled vk result on swapchain image acquisition");
}
if(framebuffer != VK_NULL_HANDLE) device.dispatch.DestroyFramebuffer(framebuffer);
VkFramebufferCreateInfo framebuffer_info = {
renderPass: swapchain.render_pass,
attachmentCount: 1,
pAttachments: [swapchain.image_resources[image_index].image_view].ptr,
width: swapchain.extent.width,
height: swapchain.extent.height,
layers: 1
};
checkVk(device.dispatch.CreateFramebuffer(&framebuffer_info, &framebuffer));
VkCommandBufferBeginInfo cmd_begin_info = { flags: VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT };
VkImageSubresourceRange image_subresource_range = {
aspectMask: VK_IMAGE_ASPECT_COLOR_BIT,
baseMipLevel: 0,
levelCount: 1,
baseArrayLayer: 0,
layerCount: 1,
};
VkImageMemoryBarrier barrier_from_present_to_draw = {
srcAccessMask: VK_ACCESS_MEMORY_READ_BIT,
dstAccessMask: VK_ACCESS_MEMORY_READ_BIT,
oldLayout: VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
newLayout: VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
srcQueueFamilyIndex: device.present_queue.family_index,
dstQueueFamilyIndex: device.graphics_queue.family_index,
image: swapchain.image_resources[image_index].image,
subresourceRange: image_subresource_range
};
VkImageMemoryBarrier barrier_from_draw_to_present = {
srcAccessMask: VK_ACCESS_MEMORY_READ_BIT,
dstAccessMask: VK_ACCESS_MEMORY_READ_BIT,
oldLayout: VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
newLayout: VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
srcQueueFamilyIndex: device.graphics_queue.family_index,
dstQueueFamilyIndex: device.present_queue.family_index,
image: swapchain.image_resources[image_index].image,
subresourceRange: image_subresource_range
};
VkViewport viewport = {
x: 0,
y: 0,
width: swapchain.extent.width,
height: swapchain.extent.height,
minDepth: 0,
maxDepth: 1
};
VkRect2D scissor = {
offset: {
x: 0,
y: 0
},
extent: swapchain.extent
};
VkClearValue[] clear_values = [
{ color: { [ 1.0, 0.8, 0.4, 0.0 ] } }
];
VkRenderPassBeginInfo render_pass_begin_info = {
renderPass: swapchain.render_pass,
framebuffer: framebuffer,
renderArea: {
offset: {
x: 0,
y: 0
},
extent: swapchain.extent
},
clearValueCount: clear_values.length.to!uint,
pClearValues: clear_values.ptr
};
device.dispatch.commandBuffer = command_buffer;
device.dispatch.BeginCommandBuffer(&cmd_begin_info);
if(device.graphics_queue.handle != device.present_queue.handle)
device.dispatch.CmdPipelineBarrier(
VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
0, 0, null, 0, null, 1,
&barrier_from_present_to_draw
);
device.dispatch.CmdBeginRenderPass(&render_pass_begin_info, VK_SUBPASS_CONTENTS_INLINE);
device.dispatch.CmdBindPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS, swapchain.pipeline);
device.dispatch.CmdSetViewport(0, 1, &viewport);
device.dispatch.CmdSetScissor(0, 1, &scissor);
const(ulong) vertex_buffer_offset = 0;
device.dispatch.CmdBindVertexBuffers(0, 1, &vertex_buffer, &vertex_buffer_offset);
device.dispatch.CmdBindDescriptorSets(VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline_layout, 0, 1, &descriptor_set, 0, null);
device.dispatch.CmdDraw(draw_count, 1, 0, 0);
device.dispatch.CmdEndRenderPass();
if(device.graphics_queue.handle != device.present_queue.handle)
device.dispatch.CmdPipelineBarrier(
VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
0, 0, null, 0, null, 1,
&barrier_from_draw_to_present
);
checkVk(device.dispatch.EndCommandBuffer());
device.dispatch.commandBuffer = VK_NULL_HANDLE;
VkSubmitInfo submit_info = {
waitSemaphoreCount: 1,
pWaitSemaphores: [image_available_semaphore].ptr,
pWaitDstStageMask: castFrom!(VkPipelineStageFlagBits*).to!(const(uint)*)([VK_PIPELINE_STAGE_TRANSFER_BIT].ptr),
commandBufferCount: 1,
pCommandBuffers: [command_buffer].ptr,
signalSemaphoreCount: 1,
pSignalSemaphores: [rendering_finished_semaphore].ptr
};
checkVk(device.dispatch.vkQueueSubmit(device.graphics_queue.handle, 1, [submit_info].ptr, fence));
VkPresentInfoKHR present_info = {
waitSemaphoreCount: 1,
pWaitSemaphores: [rendering_finished_semaphore].ptr,
swapchainCount: 1,
pSwapchains: [swapchain.swapchain].ptr,
pImageIndices: [image_index].ptr
};
switch(device.dispatch.vkQueuePresentKHR(device.present_queue.handle, &present_info)) {
case VK_SUCCESS:
break;
case VK_ERROR_OUT_OF_DATE_KHR:
case VK_SUBOPTIMAL_KHR:
on_window_size_changed();
break;
default:
throw new StringException("unhandled vk result on presentation");
}
(I can't embed the images because my rep is too low, sorry)
Program Outputs:
OpenGL draws the cube as expected
OpenGL Output
Vulkan does not render anything except for the clear color.
UPDATE:
After fixing the cull mode by changing it to VK_CULL_MODE_NONE, this is the result I get:
Output after cull mode fix
VK_CULL_MODE_FRONT_AND_BACK
I think this is your problem :)
After cull mode fix, seems that your problem in your vertex data layout. Vulkan expects (accordingly to your layout binding) something like
struct Vertex {
vec4 x;
vec2 u;
};
Vertex VertexData[] = {...};
because you set VK_VERTEX_INPUT_RATE_VERTEX in your vertex_binding_descriptions.inputRate field.
And it seems that in your case you should set VK_VERTEX_INPUT_RATE_INSTANCE instead to work with buffers after each other.
Fix: Have seen your new comment, it looks like i misunderstood your vertices layout, so it won't help.