Commit 922a9f22 authored by Johannes Maul's avatar Johannes Maul
Browse files

Advancing Samplebase (VeinsSample) + Advancing Profiling with TimeStamps and fences

parent a9fcc8cc
This diff is collapsed.
function [attenuationValues, attenuationPicture] = attenuationCalculationWrapper (detectorPositionCoordinates, faces, nodes, source, detectorSizeUint,attenuationCoefficentValues, photons)
function [attenuationValues, attenuationPicture, times] = attenuationCalculationWrapper (detectorPositionCoordinates, faces, nodes, source, detectorSizeUint,attenuationCoefficentValues, photons)
[attenuationValues, profiling] = nv_ray_tracing_basic(detectorPositionCoordinates, faces, nodes, source, detectorSizeUint,attenuationCoefficentValues); %parameters always in this order %profiling can be activated all the time. Doesnt effect the execution time for big examples
attenuationValues = exp(attenuationValues * photons);%for correctnes of Beer-Lambert-Law % EXP and LOG not on GPU because GLSL just offers single precision. Same Example'll lose details.
......
function easyExample(exampleType, buildDefault)
if buildDefault
raytracingBuilderWrapper();
end
if isfile(filename)
[detectorSizeX, detectorSizeY, detectorPositionCoordinates] = createOwnExample(exampleType);
detectorSizeUint = uint32([detectorSizeX detectorSizeY]);
if exampleType == "test"
testVolumeSize = [100 120 2];
testVolume = ones(testVolumeSize)+2;
isoValues = [1];
source = [testVolumeSize(2)/2 testVolumeSize(1)/2 testVolumeSize(3)*1000];%centered above the test volume
end
if exampleType == "easy"
testVolumeSize = [100 120 50];
testVolume = ones(testVolumeSize);
testVolume(10:90,10:90,10:40) = 2;
testVolume(30:70,30:70,15:35) =3;
isoValues = [0.5, 1.5,2.5];
source = [testVolumeSize(2)/2 testVolumeSize(1)/2 testVolumeSize(3)*10];%centered above the test volume
end
if exampleType == "real"
%load();
end
[nodes,faces] = vol2surf(testVolume,1:testVolumeSize(1),1:testVolumeSize(2),1:testVolumeSize(3),1.5,1,'cgalsurf',isoValues);
faces = unique(faces,'rows'); %special for vol2mesh were the outer layer got an air layer which destroys the calculation
faces(:,4) = faces(:,4) - 1; %currently non dynamic labels
nodes(:,1:2) = nodes(:,1:2) + 0;
[projection, profiling] = nv_ray_tracing_basic(detectorPositionCoordinates, faces, nodes, source, detectorSizeUint); %parameters always in this order
times = reformatProfile(profiling);
% debugMissFormated = reshape(debugMiss,[200 detectorSizeX detectorSizeY]);
projectionAsImage = reshape(projection,detectorSizeUint);
figure; imagesc(projectionAsImage); axis image; colormap('gray');
end
function [sizeX, sizeY, detectorPositions] = createOwnExample(exampleType)
sizeX = 800;
sizeY = sizeX * 16 / 9;
if exampleType == "test"
sizeX = 150;
sizeY = 200;
end
if exampleType == "easy"
sizeX = 250;
sizeY = 200;
end
[x,y] = ndgrid(1:sizeX,1:sizeY);
z = ones(sizeX,sizeY)-3;
detectorPositions = [x(:) y(:) z(:)];
end
\ No newline at end of file
function easyExample(exampleType, buildDefault)
function [times] = easyExample(exampleType, buildDefault)
overwriteLoad = 0;
if buildDefault
raytracingBuilderWrapper();
......@@ -15,6 +15,13 @@ function easyExample(exampleType, buildDefault)
Disp("exampleReal.mat not found, try another example");
return;
end
elseif exampleType == "veins" && ~overwriteLoad
if isfile("VeinsPhantom.mat")
load('VeinsPhantom.mat');
else
Disp("exampleReal.mat not found, try another example");
return;
end
else %else is just running if exampleFiles are missing. But the iso2mesh files are required. See Doku.
[detectorSizeX, detectorSizeY, detectorPositionCoordinates] = createOwnExample(exampleType);
detectorSizeUint = uint32([detectorSizeX detectorSizeY]);
......@@ -34,16 +41,16 @@ function easyExample(exampleType, buildDefault)
isoValues = [0.5,1.5,2.5];
source = [testVolumeSize(2)/2 testVolumeSize(1)/2 testVolumeSize(3)*10];%centered above the test volume
attenuationCoefficentValues = [-0.372, -0.25, -0.38];%1 = gland, 2=fat,3=muscle %K. Bliznakova et al. "A three-dimensional breast software phantom for mammography simulation" , 2003, Phys. Med. Biol., 48, 3699
end
end
[nodes,~,faces] = vol2mesh(uint8(testVolume),1:testVolumeSize(1),1:testVolumeSize(2),1:testVolumeSize(3),1.5,1.5,1,'cgalmesh',isoValues); %just usable with iso2mesh repo! http://iso2mesh.sourceforge.net/cgi-bin/index.cgi
faces = unique(faces,'rows'); %special for vol2mesh were the outer layer got an air layer which destroys the calculation
% nodes(:,1:2) = nodes(:,1:2) +10;
end
tic;
[~, attenuationPicture] = attenuationCalculationWrapper(detectorPositionCoordinates, faces, nodes, source, detectorSizeUint,attenuationCoefficentValues,1);%photons = 1 => relativ Image, just Attenuation
[~, attenuationPicture, times] = attenuationCalculationWrapper(detectorPositionCoordinates, faces, nodes, source, detectorSizeUint,attenuationCoefficentValues,1);%photons = 1 => relativ Image, just Attenuation
toc;
figure; imagesc(attenuationPicture); axis image; colormap('gray');
%figure; imagesc(attenuationPicture); axis image; colormap('gray'); title(exampleType);
end
function [sizeX, sizeY, detectorPositions] = createOwnExample(exampleType)
......
for i =1:20
timesTmp = easyExample('real',0);
timesAll(i) = timesTmp(7).time;
end
figure; boxplot(timesAll(:)); title('real');
for i =1:20
timesTmp = easyExample('veins',0);
timesAll(i) = timesTmp(7).time;
end
figure; boxplot(timesAll(:)); title('veins');
for i =1:20
timesTmp = easyExample('easy',0);
timesAll(i) = timesTmp(7).time;
end
figure; boxplot(timesAll(:)); title('easy');
for i =1:20
timesTmp = easyExample('test',0);
timesAll(i) = timesTmp(7).time;
end
figure; boxplot(timesAll(:)); title('test');
\ No newline at end of file
function printAll(nodes,faces,source,detectors, detectorId)
%Zum Ausgeben des Mesh, der Quelle, detectoren, strahlen
%Funktion die das Mesh ausgibt
% figure;
tiledlayout('flow')
nexttile;
hold on;
title('Projektionssimulation');
plotsurf(nodes(:,1:3),faces);
%Einzeichnen der Quelle
plot3(source(1),source(2),source(3),'-o');
%Einzeichnen der Detectoren
%Kann getauscht werden, um statt einer soliden Fläche nur den Rahmen zu
%bekommen
%plot3([min(detectors(:,1)) min(detectors(:,1)) max(detectors(:,1)) max(detectors(:,1)) min(detectors(:,1))], min(detectors(:,2))+zeros(5,1), [min(detectors(:,3)) max(detectors(:,3)) max(detectors(:,3)) min(detectors(:,3)) min(detectors(:,3))]);
%Komplizierte darstellung ist aber einfach mit folgender erklärung:
%plot3d( [x1 x2 x3 x4 x1], [y1 y2 y3 y4 y1], [z z z z z]) xn,yn, sind die
%Ecken des Vierecks, z die Ebene
xData = [min(detectors(:,1)) max(detectors(:,1)) max(detectors(:,1)) min(detectors(:,1))];
yData = [min(detectors(:,2)) min(detectors(:,2)) max(detectors(:,1)) max(detectors(:,2))];
zData = [min(detectors(:,3)) max(detectors(:,3)) max(detectors(:,3)) min(detectors(:,3))];
patch(xDat'r')
%Einzeichnen der Strahlen
startIndex = 1;
endIndex = size(detectors,1);
if detectorId ~= 0 %&& endIndex == 1 %0 zeigt alle Strahlen
startIndex = detectorId;
endIndex = detectorId;
end
for i = startIndex:endIndex
plot3([source(1) detectors(i,1)], [source(2) detectors(i,2)], [source(3) detectors(i,3)]);
end
hold off;
end
\ No newline at end of file
function [times] = reformatProfile(profile)
ProfileDesciptions = ["daten aus mex laden(mesh)" "daten aus mex laden(rest)" "outputprepare(primitives,distances)"...
"vulkanConstructor" "vulkan init" "prepareData(Transfer etc.)"...
"Render des Raytracing (QueueSubmit)" "Datenzurückholen" "rendern komplett"...
"RaytracingCommand Execution" "Render des Raytracing (QueueSubmit)" "Datenzurückholen" ...
"vulkan abreisen" "output für mex(debug,profile)"];
if size(profile, 2) ~= size(ProfileDesciptions,2)
disp("Es hat zu wenige Descriptions oder times");
......
......@@ -888,9 +888,11 @@ public:
/*
Command buffer generation
*/
VkQueryPool_T* queryPool;
VkCommandBufferBeginInfo cmdBufInfo;
void buildCommandBuffers()
{
VkCommandBufferBeginInfo cmdBufInfo = vks::initializers::commandBufferBeginInfo();
cmdBufInfo = vks::initializers::commandBufferBeginInfo();
//VkImageSubresourceRange subresourceRange = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 };
......@@ -919,6 +921,23 @@ public:
VkDeviceSize bindingOffsetHitShader = rayTracingProperties.shaderGroupHandleSize * INDEX_ANY_HIT;
VkDeviceSize bindingStride = rayTracingProperties.shaderGroupHandleSize;
#ifdef profile
//timeStamp
VkQueryPoolCreateInfo createInfo = {};
createInfo.sType = VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO;
createInfo.pNext = nullptr;
createInfo.flags = 0;
createInfo.queryType = VK_QUERY_TYPE_TIMESTAMP;
createInfo.queryCount = 2;
VK_CHECK_RESULT(vkCreateQueryPool(device, &createInfo, nullptr, &queryPool));
vkCmdResetQueryPool(drawCmdBuffers[currentBuffer], queryPool, 0, 2);
vkCmdWriteTimestamp(drawCmdBuffers[currentBuffer], VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, queryPool, 0);
#endif
vkCmdTraceRaysNV(drawCmdBuffers[i],
shaderBindingTable.buffer,
bindingOffsetRayGenShader,
......@@ -934,7 +953,10 @@ public:
detectorSize[0],
detectorSize[1],
1);
#ifdef profile
vkCmdWriteTimestamp(drawCmdBuffers[currentBuffer], VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, queryPool, 1);
#endif
/*
Copy raytracing output to swap chain image
*/
......@@ -1087,14 +1109,46 @@ public:
void draw()
{
////VulkanExampleBase::prepareFrame();
//for (int i = 0; i < 10000; i++)
//{
submitInfo.commandBufferCount = 1;
submitInfo.pCommandBuffers = &drawCmdBuffers[currentBuffer];
VK_CHECK_RESULT(vkQueueSubmit(queue, 1, &submitInfo, VK_NULL_HANDLE));
//VulkanExampleBase::submitFrame();
//}
#ifdef profile
//fences
VkFence fence;
VkFenceCreateInfo fence_create_info = {VK_STRUCTURE_TYPE_FENCE_CREATE_INFO, nullptr,VK_FENCE_CREATE_SIGNALED_BIT};
VK_CHECK_RESULT(vkCreateFence(device, &fence_create_info, VK_NULL_HANDLE, &fence));
VK_CHECK_RESULT(vkWaitForFences(device, 1, &fence, VK_FALSE, 1000000000));
VK_CHECK_RESULT(vkResetFences(device, 1, &fence));
//fences end
#endif
//QueueSubmit
submitInfo.commandBufferCount = 1;
submitInfo.pCommandBuffers = &drawCmdBuffers[currentBuffer];
VK_CHECK_RESULT(vkQueueSubmit(queue, 1, &submitInfo, fence));
//QueueSubmit end
#ifdef profile
//fence wait start
if (vkWaitForFences(device, 1, &fence, VK_FALSE, 10000000000) != VK_SUCCESS)
printf("Wait for fence after submit timed out");
VK_CHECK_RESULT(vkResetFences(device, 1, &fence));
vkDestroyFence(device, fence, nullptr);
//fence wait end
VkResult result;
uint64_t timingRes[2]={ 0 };
result = vkGetQueryPoolResults(device, queryPool, 0, 2, sizeof(uint64_t) * 2, timingRes, sizeof(uint64_t), VK_QUERY_RESULT_64_BIT);
double tmpTimestampTime = 0;
if (result == VK_SUCCESS)
tmpTimestampTime = static_cast<double>(timingRes[1] - timingRes[0]) / (1000 * 1000); //convert nano to milli (nano -> mikro -> milli)
vkResetQueryPool(device, queryPool, 0, 2);
vkDestroyQueryPool(device, queryPool, VK_NULL_HANDLE);
//timeStamp end
times.push_back(tmpTimestampTime); //7
#endif
}
void createDetectorBuffer()
......@@ -1420,7 +1474,7 @@ public:
if (!prepared)
return;
#ifdef profile
sw.Start();
sw.Start(); //8 (-107)
#endif
draw();
#ifdef profile
......@@ -1429,7 +1483,7 @@ public:
#endif
#ifdef profile
sw.Start();
sw.Start(); //9
#endif
#ifdef advancedOutput
getOutputValue();
......@@ -1465,7 +1519,7 @@ void mexFunction(int interfaceOutputs,
{
size_t outputPosition = 0;
#ifdef profile
sw.Start();
sw.Start(); //1
#endif
//Input
//mesh = getMeshedObject(input);
......@@ -1475,7 +1529,7 @@ void mexFunction(int interfaceOutputs,
times.push_back(sw.ElapsedMilliseconds());
#endif
#ifdef profile
sw.Start();
sw.Start(); //2
#endif
source = getSource(input);
detectors = getDetectorLinear(input);
......@@ -1490,7 +1544,7 @@ void mexFunction(int interfaceOutputs,
#endif
#ifdef profile
sw.Start();
sw.Start(); //3
#endif
#ifdef advancedOutput
output[outputPosition] = mxCreateNumericArray(1,
......@@ -1520,7 +1574,7 @@ void mexFunction(int interfaceOutputs,
outputPosition++;
#ifdef profile
sw.Start();
sw.Start(); //4
#endif
//Ausfhrung
VulkanExample* vulkanExample = new VulkanExample();
......@@ -1530,7 +1584,7 @@ void mexFunction(int interfaceOutputs,
#endif
#ifdef profile
sw.Start();
sw.Start(); //5
#endif
vulkanExample->initVulkan();
#ifdef profile
......@@ -1539,7 +1593,7 @@ void mexFunction(int interfaceOutputs,
#endif
#ifdef profile
sw.Start();
sw.Start(); //6
#endif
vulkanExample->prepare();
#ifdef profile
......@@ -1547,18 +1601,18 @@ void mexFunction(int interfaceOutputs,
times.push_back(sw.ElapsedMilliseconds());
#endif
#ifdef profile
sw.Start();
#endif
//#ifdef profile
// sw.Start(); //7
//#endif
vulkanExample->render();
#ifdef profile
sw.Stop();
times.push_back(sw.ElapsedMilliseconds());
#endif
//#ifdef profile
// sw.Stop();
// times.push_back(sw.ElapsedMilliseconds());
//#endif
#ifdef profile
sw.Start();
sw.Start(); //10
#endif
#ifdef debug
......@@ -1581,17 +1635,16 @@ void mexFunction(int interfaceOutputs,
detectorSize[0] * detectorSize[1] * static_cast<uint32_t>(atCoVal.sizeZ.z) * sizeof(double));
outputPosition++;
#endif
#ifdef profile
sw.Start();
#endif
vulkanExample->cleanUp();
vulkanExample->cleanBase();
#ifdef profile
sw.Stop();
times.push_back(sw.ElapsedMilliseconds());
#endif
#ifdef profile
sw.Start(); //11
#endif
vulkanExample->cleanUp();
vulkanExample->cleanBase();
#ifdef profile
sw.Stop();
times.push_back(sw.ElapsedMilliseconds());
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment