• Advertisement
  • Popular Tags

  • Popular Now

  • Advertisement
  • Similar Content

    • By fleissi
      Hey guys!

      I'm new here and I recently started developing my own rendering engine. It's open source, based on OpenGL/DirectX and C++.
      The full source code is hosted on github:
      https://github.com/fleissna/flyEngine

      I would appreciate if people with experience in game development / engine desgin could take a look at my source code. I'm looking for honest, constructive criticism on how to improve the engine.
      I'm currently writing my master's thesis in computer science and in the recent year I've gone through all the basics about graphics programming, learned DirectX and OpenGL, read some articles on Nvidia GPU Gems, read books and integrated some of this stuff step by step into the engine.

      I know about the basics, but I feel like there is some missing link that I didn't get yet to merge all those little pieces together.

      Features I have so far:
      - Dynamic shader generation based on material properties
      - Dynamic sorting of meshes to be renderd based on shader and material
      - Rendering large amounts of static meshes
      - Hierarchical culling (detail + view frustum)
      - Limited support for dynamic (i.e. moving) meshes
      - Normal, Parallax and Relief Mapping implementations
      - Wind animations based on vertex displacement
      - A very basic integration of the Bullet physics engine
      - Procedural Grass generation
      - Some post processing effects (Depth of Field, Light Volumes, Screen Space Reflections, God Rays)
      - Caching mechanisms for textures, shaders, materials and meshes

      Features I would like to have:
      - Global illumination methods
      - Scalable physics
      - Occlusion culling
      - A nice procedural terrain generator
      - Scripting
      - Level Editing
      - Sound system
      - Optimization techniques

      Books I have so far:
      - Real-Time Rendering Third Edition
      - 3D Game Programming with DirectX 11
      - Vulkan Cookbook (not started yet)

      I hope you guys can take a look at my source code and if you're really motivated, feel free to contribute :-)
      There are some videos on youtube that demonstrate some of the features:
      Procedural grass on the GPU
      Procedural Terrain Engine
      Quadtree detail and view frustum culling

      The long term goal is to turn this into a commercial game engine. I'm aware that this is a very ambitious goal, but I'm sure it's possible if you work hard for it.

      Bye,

      Phil
    • By tj8146
      I have attached my project in a .zip file if you wish to run it for yourself.
      I am making a simple 2d top-down game and I am trying to run my code to see if my window creation is working and to see if my timer is also working with it. Every time I run it though I get errors. And when I fix those errors, more come, then the same errors keep appearing. I end up just going round in circles.  Is there anyone who could help with this? 
       
      Errors when I build my code:
      1>Renderer.cpp 1>c:\users\documents\opengl\game\game\renderer.h(15): error C2039: 'string': is not a member of 'std' 1>c:\program files (x86)\windows kits\10\include\10.0.16299.0\ucrt\stddef.h(18): note: see declaration of 'std' 1>c:\users\documents\opengl\game\game\renderer.h(15): error C2061: syntax error: identifier 'string' 1>c:\users\documents\opengl\game\game\renderer.cpp(28): error C2511: 'bool Game::Rendering::initialize(int,int,bool,std::string)': overloaded member function not found in 'Game::Rendering' 1>c:\users\documents\opengl\game\game\renderer.h(9): note: see declaration of 'Game::Rendering' 1>c:\users\documents\opengl\game\game\renderer.cpp(35): error C2597: illegal reference to non-static member 'Game::Rendering::window' 1>c:\users\documents\opengl\game\game\renderer.cpp(36): error C2597: illegal reference to non-static member 'Game::Rendering::window' 1>c:\users\documents\opengl\game\game\renderer.cpp(43): error C2597: illegal reference to non-static member 'Game::Rendering::window' 1>Done building project "Game.vcxproj" -- FAILED. ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========  
       
      Renderer.cpp
      #include <GL/glew.h> #include <GLFW/glfw3.h> #include "Renderer.h" #include "Timer.h" #include <iostream> namespace Game { GLFWwindow* window; /* Initialize the library */ Rendering::Rendering() { mClock = new Clock; } Rendering::~Rendering() { shutdown(); } bool Rendering::initialize(uint width, uint height, bool fullscreen, std::string window_title) { if (!glfwInit()) { return -1; } /* Create a windowed mode window and its OpenGL context */ window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL); if (!window) { glfwTerminate(); return -1; } /* Make the window's context current */ glfwMakeContextCurrent(window); glViewport(0, 0, (GLsizei)width, (GLsizei)height); glOrtho(0, (GLsizei)width, (GLsizei)height, 0, 1, -1); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glfwSwapInterval(1); glEnable(GL_SMOOTH); glEnable(GL_DEPTH_TEST); glEnable(GL_BLEND); glDepthFunc(GL_LEQUAL); glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); glEnable(GL_TEXTURE_2D); glLoadIdentity(); return true; } bool Rendering::render() { /* Loop until the user closes the window */ if (!glfwWindowShouldClose(window)) return false; /* Render here */ mClock->reset(); glfwPollEvents(); if (mClock->step()) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glfwSwapBuffers(window); mClock->update(); } return true; } void Rendering::shutdown() { glfwDestroyWindow(window); glfwTerminate(); } GLFWwindow* Rendering::getCurrentWindow() { return window; } } Renderer.h
      #pragma once namespace Game { class Clock; class Rendering { public: Rendering(); ~Rendering(); bool initialize(uint width, uint height, bool fullscreen, std::string window_title = "Rendering window"); void shutdown(); bool render(); GLFWwindow* getCurrentWindow(); private: GLFWwindow * window; Clock* mClock; }; } Timer.cpp
      #include <GL/glew.h> #include <GLFW/glfw3.h> #include <time.h> #include "Timer.h" namespace Game { Clock::Clock() : mTicksPerSecond(50), mSkipTics(1000 / mTicksPerSecond), mMaxFrameSkip(10), mLoops(0) { mLastTick = tick(); } Clock::~Clock() { } bool Clock::step() { if (tick() > mLastTick && mLoops < mMaxFrameSkip) return true; return false; } void Clock::reset() { mLoops = 0; } void Clock::update() { mLastTick += mSkipTics; mLoops++; } clock_t Clock::tick() { return clock(); } } TImer.h
      #pragma once #include "Common.h" namespace Game { class Clock { public: Clock(); ~Clock(); void update(); bool step(); void reset(); clock_t tick(); private: uint mTicksPerSecond; ufloat mSkipTics; uint mMaxFrameSkip; uint mLoops; uint mLastTick; }; } Common.h
      #pragma once #include <cstdio> #include <cstdlib> #include <ctime> #include <cstring> #include <cmath> #include <iostream> namespace Game { typedef unsigned char uchar; typedef unsigned short ushort; typedef unsigned int uint; typedef unsigned long ulong; typedef float ufloat; }  
      Game.zip
    • By lxjk
      Hi guys,
      There are many ways to do light culling in tile-based shading. I've been playing with this idea for a while, and just want to throw it out there.
      Because tile frustums are general small compared to light radius, I tried using cone test to reduce false positives introduced by commonly used sphere-frustum test.
      On top of that, I use distance to camera rather than depth for near/far test (aka. sliced by spheres).
      This method can be naturally extended to clustered light culling as well.
      The following image shows the general ideas

       
      Performance-wise I get around 15% improvement over sphere-frustum test. You can also see how a single light performs as the following: from left to right (1) standard rendering of a point light; then tiles passed the test of (2) sphere-frustum test; (3) cone test; (4) spherical-sliced cone test
       

       
      I put the details in my blog post (https://lxjk.github.io/2018/03/25/Improve-Tile-based-Light-Culling-with-Spherical-sliced-Cone.html), GLSL source code included!
       
      Eric
    • By Fadey Duh
      Good evening everyone!

      I was wondering if there is something equivalent of  GL_NV_blend_equation_advanced for AMD?
      Basically I'm trying to find more compatible version of it.

      Thank you!
    • By Jens Eckervogt
      Hello guys, 
       
      Please tell me! 
      How do I know? Why does wavefront not show for me?
      I already checked I have non errors yet.
      using OpenTK; using System.Collections.Generic; using System.IO; using System.Text; namespace Tutorial_08.net.sourceskyboxer { public class WaveFrontLoader { private static List<Vector3> inPositions; private static List<Vector2> inTexcoords; private static List<Vector3> inNormals; private static List<float> positions; private static List<float> texcoords; private static List<int> indices; public static RawModel LoadObjModel(string filename, Loader loader) { inPositions = new List<Vector3>(); inTexcoords = new List<Vector2>(); inNormals = new List<Vector3>(); positions = new List<float>(); texcoords = new List<float>(); indices = new List<int>(); int nextIdx = 0; using (var reader = new StreamReader(File.Open("Contents/" + filename + ".obj", FileMode.Open), Encoding.UTF8)) { string line = reader.ReadLine(); int i = reader.Read(); while (true) { string[] currentLine = line.Split(); if (currentLine[0] == "v") { Vector3 pos = new Vector3(float.Parse(currentLine[1]), float.Parse(currentLine[2]), float.Parse(currentLine[3])); inPositions.Add(pos); if (currentLine[1] == "t") { Vector2 tex = new Vector2(float.Parse(currentLine[1]), float.Parse(currentLine[2])); inTexcoords.Add(tex); } if (currentLine[1] == "n") { Vector3 nom = new Vector3(float.Parse(currentLine[1]), float.Parse(currentLine[2]), float.Parse(currentLine[3])); inNormals.Add(nom); } } if (currentLine[0] == "f") { Vector3 pos = inPositions[0]; positions.Add(pos.X); positions.Add(pos.Y); positions.Add(pos.Z); Vector2 tc = inTexcoords[0]; texcoords.Add(tc.X); texcoords.Add(tc.Y); indices.Add(nextIdx); ++nextIdx; } reader.Close(); return loader.loadToVAO(positions.ToArray(), texcoords.ToArray(), indices.ToArray()); } } } } } And It have tried other method but it can't show for me.  I am mad now. Because any OpenTK developers won't help me.
      Please help me how do I fix.

      And my download (mega.nz) should it is original but I tried no success...
      - Add blend source and png file here I have tried tried,.....  
       
      PS: Why is our community not active? I wait very longer. Stop to lie me!
      Thanks !
  • Advertisement
  • Advertisement
Sign in to follow this  

OpenGL [Vulkan] Descriptor binding point confusion / Uniform buffer memory barriers / Compressed images

This topic is 760 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

#Q1 - Descriptor Binding Points

I feel like I'm starting to get the hang of the API, but there's still a couple of things that are unclear to me.

For instance, how do I actually change the binding point for a descriptor?

I have a small test fragment shader with 2 uniform descriptor sets at binding point 0:

#version 400

#extension GL_ARB_separate_shader_objects : enable
#extension GL_ARB_shading_language_420pack : enable

out vec4 fs_color;

layout(std140, set = 0, binding = 0) uniform testa {
	vec4 color;
} u_testa;

layout(std140, set = 1, binding = 0) uniform testb {
	vec4 color;
} u_testb;

void main()
{
	fs_color = (u_testa.color +u_testb.color) /2.0;
}

In my test program they're initialized like this: (This is essentially a copy of the "multiple_sets" demo from the Vulkan SDK)

static const unsigned descriptor_set_count = 2;
const int binding_point = 0; // The shader binding point

VkDescriptorSetLayoutBinding uniform_binding[1] = {};
uniform_binding[0].binding = binding_point;
uniform_binding[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
uniform_binding[0].descriptorCount = 1;
uniform_binding[0].stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT;
uniform_binding[0].pImmutableSamplers = NULL;
VkDescriptorSetLayoutCreateInfo uniform_layout_info[1] = {};
uniform_layout_info[0].sType =
	VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
uniform_layout_info[0].pNext = NULL;
uniform_layout_info[0].bindingCount = 1;
uniform_layout_info[0].pBindings = uniform_binding;

VkDescriptorSetLayoutBinding uniform_binding2[1] = {};
uniform_binding2[0].binding = binding_point;
uniform_binding2[0].descriptorType =
	VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
uniform_binding2[0].descriptorCount = 1;
uniform_binding2[0].stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT;
uniform_binding2[0].pImmutableSamplers = NULL;
VkDescriptorSetLayoutCreateInfo uniform_layout_info2[1] = {};
uniform_layout_info2[0].sType =
	VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
uniform_layout_info2[0].pNext = NULL;
uniform_layout_info2[0].bindingCount = 1;
uniform_layout_info2[0].pBindings = uniform_binding2;

// Create multiple sets, using each createInfo
static const unsigned uniform_set_index = 0;
static const unsigned uniform_set_index2 = 1;
VkDescriptorSetLayout descriptor_layouts[descriptor_set_count] = {};
auto res = vkCreateDescriptorSetLayout(device, uniform_layout_info, NULL,
								  &descriptor_layouts[uniform_set_index]);
assert(res == VK_SUCCESS);
res = vkCreateDescriptorSetLayout(device, uniform_layout_info2, NULL,
								  &descriptor_layouts[uniform_set_index2]);
assert(res == VK_SUCCESS);

// Create pipeline layout with multiple descriptor sets
VkPipelineLayoutCreateInfo pipelineLayoutCreateInfo[1] = {};
pipelineLayoutCreateInfo[0].sType =
	VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
pipelineLayoutCreateInfo[0].pNext = NULL;
pipelineLayoutCreateInfo[0].pushConstantRangeCount = 0;
pipelineLayoutCreateInfo[0].pPushConstantRanges = NULL;
pipelineLayoutCreateInfo[0].setLayoutCount = descriptor_set_count;
pipelineLayoutCreateInfo[0].pSetLayouts = descriptor_layouts;
VkPipelineLayout pipeline_layout;
res = vkCreatePipelineLayout(device, pipelineLayoutCreateInfo, NULL,
							 &pipeline_layout);
assert(res == VK_SUCCESS);

// Create a single pool to contain data for our two descriptor sets
VkDescriptorPoolSize type_count[1] = {};
type_count[0].type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
type_count[0].descriptorCount = 2;

VkDescriptorPoolCreateInfo pool_info[1] = {};
pool_info[0].sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
pool_info[0].pNext = NULL;
pool_info[0].maxSets = descriptor_set_count;
pool_info[0].poolSizeCount =
	sizeof(type_count) / sizeof(VkDescriptorPoolSize);
pool_info[0].pPoolSizes = type_count;

VkDescriptorPool descriptor_pool[1] = {};
res = vkCreateDescriptorPool(device, pool_info, NULL, descriptor_pool);
assert(res == VK_SUCCESS);

VkDescriptorSetAllocateInfo alloc_info[1];
alloc_info[0].sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
alloc_info[0].pNext = NULL;
alloc_info[0].descriptorPool = descriptor_pool[0];
alloc_info[0].descriptorSetCount = descriptor_set_count;
alloc_info[0].pSetLayouts = descriptor_layouts;

// Populate descriptor sets
VkDescriptorSet descriptor_sets[descriptor_set_count];
descriptor_sets[descriptor_set_count] = {};
res = vkAllocateDescriptorSets(device, alloc_info, descriptor_sets);
assert(res == VK_SUCCESS);

// Using empty brace initializer on the next line triggers a bug in older
// versions of gcc, so memset instead
VkWriteDescriptorSet descriptor_writes[2];
memset(descriptor_writes, 0, sizeof(descriptor_writes));

VkDescriptorBufferInfo buffer_info;
buffer_info.buffer = colorBuffer;
buffer_info.offset = 0;
buffer_info.range = sizeof(glm::vec4);
// Populate with info about our uniform buffer
descriptor_writes[0] = {};
descriptor_writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
descriptor_writes[0].pNext = NULL;
descriptor_writes[0].dstSet = descriptor_sets[uniform_set_index];
descriptor_writes[0].descriptorCount = 1;
descriptor_writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
descriptor_writes[0].pBufferInfo = &buffer_info;
descriptor_writes[0].dstArrayElement = 0;
descriptor_writes[0].dstBinding = binding_point;

VkDescriptorBufferInfo buffer_info2;
buffer_info2.buffer = colorBuffer2;
buffer_info2.offset = 0;
buffer_info2.range = sizeof(glm::vec4);
// Populate with info about our sampled image
descriptor_writes[1] = {};
descriptor_writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
descriptor_writes[1].pNext = NULL;
descriptor_writes[1].dstSet = descriptor_sets[uniform_set_index2];
descriptor_writes[1].descriptorCount = 1;
descriptor_writes[1].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
descriptor_writes[1].pBufferInfo = &buffer_info2;
descriptor_writes[1].dstArrayElement = 0;
descriptor_writes[1].dstBinding = binding_point;

vkUpdateDescriptorSets(device, descriptor_set_count, descriptor_writes,
					   0, NULL);
[...]

'binding_point' is used in the 'VkDescriptorSetLayoutBinding' and 'VkDescriptorBufferInfo' structures.

 

During rendering, the descriptor sets are bound using:

vkCmdBindDescriptorSets(cmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS,
                        pipeline_layout, 0, descriptor_set_count,
                        descriptor_sets, 0, NULL);

This works fine, as long as the binding point is 0. If I change 'binding_point' to 1, and the binding points in the shader respectively, the program crashes at the 'vkUpdateDescriptorSets'-call with a write access violation.

 

What am I missing?

 

#Q2 - Uniform Buffer Memory Barriers

Another question, regarding uniform buffers:

Say I have a uniform for my MVP matrix. This uniform changes for every object that has to be rendered, so my approach was this:

1) Bind the shader pipeline

2) Map the memory for the MVP buffer, write the object matrix, unmap the memory

3) Bind the descriptor set for the uniform

4) Bind the vertex buffer

5) Run the draw-call

6) Repeat for all objects from step 2)

 

This works if there's just 1 object. If there's more than one, they all use the MVP from the very last object. Most likely because the draw-calls are delayed, so the MVP memory is overwritten before the previous draw-call has finished.

What can I do to prevent this? My first thought was to create a memory pipeline barrier with VK_ACCESS_UNIFORM_READ_BIT as source mask and VK_ACCESS_HOST_WRITE_BIT as destination access mask (=All uniform reads should finish before the host tries to write to the memory again), that didn't seem to have any effect however.

 

#Q3 - Compressed Images

One more question, regarding compressed images.

How can I achieve the equivalent of glGetCompressedTexImage with DXT1, DXT3 and DXT5 compressions in Vulkan? The equivalent format should be:

GL_COMPRESSED_RGBA_S3TC_DXT1_EXT -> VkFormat::VK_FORMAT_BC1_RGBA_SRGB_BLOCK

GL_COMPRESSED_RGBA_S3TC_DXT3_EXT -> VkFormat::VK_FORMAT_BC2_SRGB_BLOCK

GL_COMPRESSED_RGBA_S3TC_DXT5_EXT -> VkFormat::VK_FORMAT_BC3_SRGB_BLOCK

Is that correct? Since compressed images are supported directly by the hardware, I should be able to use the same image data I've used for glGetCompressedTexImage, and map it to the memory of a VkImage, true? So far the image has always ended up corrupted, is there anything else I need to take into account?

Edited by Silverlan

Share this post


Link to post
Share on other sites
Advertisement

I'm still struggling with compressed images.

Here's what the specification says about that:

 

Compressed texture images stored using the S3TC compressed image formats are represented as a collection of 4×4 texel blocks, where each block contains 64 or 128 bits of texel data. The image is encoded as a normal 2D raster image in which each 4×4 block is treated as a single pixel.

Source: https://www.khronos.org/registry/dataformat/specs/1.1/dataformat.1.1.html#S3TC

 

 

For images created with linear tiling, rowPitch, arrayPitch and depthPitch describe the layout of the subresource in linear memory. For uncompressed formats, rowPitch is the number of bytes between texels with the same x coordinate in adjacent rows (y coordinates differ by one). arrayPitch is the number of bytes between texels with the same x and y coordinate in adjacent array layers of the image (array layer values differ by one). depthPitch is the number of bytes between texels with the same x and y coordinate in adjacent slices of a 3D image (z coordinates differ by one). Expressed as an addressing formula, the starting byte of a texel in the subresource has address:

// (x,y,z,layer) are in texel coordinates

address(x,y,z,layer) = layer*arrayPitch + z*depthPitch + y*rowPitch + x*texelSize + offset

For compressed formats, the rowPitch is the number of bytes between compressed blocks in adjacent rows. arrayPitch is the number of bytes between blocks in adjacent array layers. depthPitch is the number of bytes between blocks in adjacent slices of a 3D image.

// (x,y,z,layer) are in block coordinates

address(x,y,z,layer) = layer*arrayPitch + z*depthPitch + y*rowPitch + x*blockSize + offset;

arrayPitch is undefined for images that were not created as arrays. depthPitch is defined only for 3D images.

For color formats, the aspectMask member of VkImageSubresource must be VK_IMAGE_ASPECT_COLOR_BIT. For depth/stencil formats, aspect must be either VK_IMAGE_ASPECT_DEPTH_BIT or VK_IMAGE_ASPECT_STENCIL_BIT. On implementations that store depth and stencil aspects separately, querying each of these subresource layouts will return a different offset and size representing the region of memory used for that aspect. On implementations that store depth and stencil aspects interleaved, the same offset and size are returned and represent the interleaved memory allocation.

 

Source: https://www.khronos.org/registry/vulkan/specs/1.0/xhtml/vkspec.html#resources-images

 

I'm using GLI to load the dds-data (Which is supposed to work with Vulkan, but I've also tried other libraries).

Here's my code for loading and mapping the data:

struct dds load_dds(const char *fileName)
{
    auto tex = gli::load_dds(fileName);
    auto format = tex.format();
    VkFormat vkFormat = static_cast<VkFormat>(format);
    auto extents = tex.extent();
    auto r = dds {};
    r.texture = new gli::texture(tex);
    r.width = extents.x;
    r.height = extents.y;
    r.format = vkFormat;
    return r;
}
void map_data_dds(struct dds *r,void *imgData,VkSubresourceLayout layout)
{
    auto &tex = *static_cast<gli::texture*>(r->texture);
    gli::storage storage {tex.format(),tex.extent(),tex.layers(),tex.faces(),tex.levels()};

    auto *srcData = static_cast<uint8_t*>(tex.data(0,0,0));
    auto *destData = static_cast<uint8_t*>(imgData); // Pointer to mapped memory of VkImage
    destData += layout.offset; // layout = VkImageLayout of the image
    auto extents = tex.extent();
    auto w = extents.x;
    auto h = extents.y;
    auto blockSize = storage.block_size();
    auto blockCount = storage.block_count(0);
    //auto blockExtent = storage.block_extent();

    auto method = 0; // All methods have the same result
    if(method == 0)
    {
        for(auto y=decltype(blockCount.y){0};y<blockCount.y;++y)
        {
            auto *rowDest = destData +y *layout.rowPitch;
            auto *rowSrc = srcData +y *(blockCount.x *blockSize);
            for(auto x=decltype(blockCount.x){0};x<blockCount.x;++x)
            {
                auto *pxDest = rowDest +x *blockSize;
                auto *pxSrc = rowSrc +x *blockSize; // 4x4 image block
                memcpy(pxDest,pxSrc,blockSize); // 64Bit per block
                //memset(pxDest,128,blockSize); // 64Bit per block
            }
        }
    }
    else if(method == 1)
        memcpy(destData,srcData,storage.size());
    else
    {
        memcpy(destData,tex.data(0,0,0),tex.size(0)); // Just one layer for now
        //destData += tex.size(0);
    }
}

Here's my code for initializing the texture (Which is 1:1 the same as the cube demo from the SDK, except for the dds-code):

static void demo_prepare_texture_image(struct demo *demo, const char *filename,
                                       struct texture_object *tex_obj,
                                       VkImageTiling tiling,
                                       VkImageUsageFlags usage,
                                       VkFlags required_props) {
    VkResult U_ASSERT_ONLY err;
    bool U_ASSERT_ONLY pass;
   /* const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
    int32_t tex_width;
    int32_t tex_height;
    if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height)) {
        printf("Failed to load textures\n");
        fflush(stdout);
        exit(1);
    }
    */
    tiling = VK_IMAGE_TILING_OPTIMAL;
    struct dds ddsData = load_dds("C:\\VulkanSDK\\1.0.5.0\\Demos\\x64\\Debug\\iron01.dds");

    VkFormat tex_format = ddsData.format;
    int32_t tex_width = ddsData.width;
    int32_t tex_height = ddsData.height;

    tex_obj->tex_width = tex_width;
    tex_obj->tex_height = tex_height;

    const VkImageCreateInfo image_create_info = {
        .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
        .pNext = NULL,
        .imageType = VK_IMAGE_TYPE_2D,
        .format = tex_format,
        .extent = {tex_width, tex_height, 1},
        .mipLevels = 1,
        .arrayLayers = 1,
        .samples = VK_SAMPLE_COUNT_1_BIT,
        .tiling = tiling,
        .usage = usage,
        .flags = 0,
        .initialLayout = VK_IMAGE_LAYOUT_PREINITIALIZED,
    };

    VkMemoryRequirements mem_reqs;

    err =
        vkCreateImage(demo->device, &image_create_info, NULL, &tex_obj->image);
    assert(!err);

    vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);

    tex_obj->mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
    tex_obj->mem_alloc.pNext = NULL;
    tex_obj->mem_alloc.allocationSize = mem_reqs.size;
    tex_obj->mem_alloc.memoryTypeIndex = 0;

    pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
                                       required_props,
                                       &tex_obj->mem_alloc.memoryTypeIndex);
    assert(pass);

    /* allocate memory */
    err = vkAllocateMemory(demo->device, &tex_obj->mem_alloc, NULL,
                           &(tex_obj->mem));
    assert(!err);

    /* bind memory */
    err = vkBindImageMemory(demo->device, tex_obj->image, tex_obj->mem, 0);
    assert(!err);

    if (required_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
        const VkImageSubresource subres = {
            .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
            .mipLevel = 0,
            .arrayLayer = 0,
        };
        VkSubresourceLayout layout;
        void *data;

        vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres,
                                    &layout);

        err = vkMapMemory(demo->device, tex_obj->mem, 0,
                          tex_obj->mem_alloc.allocationSize, 0, &data);
        assert(!err);

        // DDS
        map_data_dds(&ddsData,data,layout);
        //

       // if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
       //     fprintf(stderr, "Error loading texture: %s\n", filename);
        //}

        vkUnmapMemory(demo->device, tex_obj->mem);
    }

    tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
    demo_set_image_layout(demo, tex_obj->image, VK_IMAGE_ASPECT_COLOR_BIT,
                          VK_IMAGE_LAYOUT_PREINITIALIZED, tex_obj->imageLayout,
                          VK_ACCESS_HOST_WRITE_BIT);
    /* setting the image layout does not reference the actual memory so no need
     * to add a mem ref */
}

I've uploaded the entire demo here. The only things I've changed from the cube demo from the Vulkan SDK are the functions above.

I've tried various different images, with different compressions (BC1/2/3), none of them work.

 

Examples:

#1:

i_view32_2016-03-18_14-35-51.png

turns into:

tri_2016-03-18_14-37-03.png

(Not the cube demo, but same principle)

 

#2:

metalbare2.png

turns into:

cube_2016-03-22_18-05-44.png

 

 

Any hints would be much appreciated.

Share this post


Link to post
Share on other sites

Based on my understanding of the spec, it's actually because you're mapping into an image with tiling VK_IMAGE_TILING_OPTIMAL layout, which does not necessarily have to have things exist in linear scanlines. The fix is to create a staging resource with identical format and VK_IMAGE_TILING_LINEAR layout instead, and then copy from the LINEAR image to the OPTIMAL one by way of a vkCmdCopyImage.

Share this post


Link to post
Share on other sites

Based on my understanding of the spec, it's actually because you're mapping into an image with tiling VK_IMAGE_TILING_OPTIMAL layout, which does not necessarily have to have things exist in linear scanlines. The fix is to create a staging resource with identical format and VK_IMAGE_TILING_LINEAR layout instead, and then copy from the LINEAR image to the OPTIMAL one by way of a vkCmdCopyImage.

I've tried setting the tiling to linear without copying, and I've tried copying from a linear staging image to optimal, but the result is always the same.

cube_2016-03-25_08-49-12.png

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement