mojoshader_vulkan.c
changeset 1275 7fc13cff18ff
parent 1272 cddbd25553fe
child 1276 89c389e4112f
equal deleted inserted replaced
1274:dda5d1bed2c7 1275:7fc13cff18ff
   179 } // create_ubo
   179 } // create_ubo
   180 
   180 
   181 static uint32_t uniform_data_size(MOJOSHADER_vkShader *shader)
   181 static uint32_t uniform_data_size(MOJOSHADER_vkShader *shader)
   182 {
   182 {
   183     int32_t i;
   183     int32_t i;
   184     int32_t uniformSize;
       
   185     int32_t buflen = 0;
   184     int32_t buflen = 0;
       
   185     const int32_t uniformSize = 16; // Yes, even the bool registers
   186     for (i = 0; i < shader->parseData->uniform_count; i++)
   186     for (i = 0; i < shader->parseData->uniform_count; i++)
   187     {
   187     {
   188         const int32_t arrayCount = shader->parseData->uniforms[i].array_count;
   188         const int32_t arrayCount = shader->parseData->uniforms[i].array_count;
   189         uniformSize = 16;
       
   190         if (shader->parseData->uniforms[i].type == MOJOSHADER_UNIFORM_BOOL)
       
   191             uniformSize = 1;
       
   192         buflen += (arrayCount ? arrayCount : 1) * uniformSize;
   189         buflen += (arrayCount ? arrayCount : 1) * uniformSize;
   193     } // for
   190     } // for
   194 
   191 
   195     return buflen;
   192     return buflen;
   196 } // uniform_data_size
   193 } // uniform_data_size
   228         return ctx->fragUboBuffers[ctx->fragUboCurrentIndex]->currentBlockSize;
   225         return ctx->fragUboBuffers[ctx->fragUboCurrentIndex]->currentBlockSize;
   229 } // get_uniform_size
   226 } // get_uniform_size
   230 
   227 
   231 static void update_uniform_buffer(MOJOSHADER_vkShader *shader)
   228 static void update_uniform_buffer(MOJOSHADER_vkShader *shader)
   232 {
   229 {
   233     int32_t i;
   230     int32_t i, j;
   234     void *map;
   231     void *map;
   235     int32_t offset;
   232     int32_t offset;
   236     uint8_t *contents;
   233     uint8_t *contents;
       
   234     uint32_t *contentsI;
   237     float *regF; int *regI; uint8_t *regB;
   235     float *regF; int *regI; uint8_t *regB;
   238     MOJOSHADER_vkUniformBuffer *ubo;
   236     MOJOSHADER_vkUniformBuffer *ubo;
   239     VkDeviceMemory uboMemory;
   237     VkDeviceMemory uboMemory;
   240 
   238 
   241     if (shader == NULL || shader->parseData->uniform_count == 0)
   239     if (shader == NULL || shader->parseData->uniform_count == 0)
   319 
   317 
   320         switch (shader->parseData->uniforms[i].type)
   318         switch (shader->parseData->uniforms[i].type)
   321         {
   319         {
   322             case MOJOSHADER_UNIFORM_FLOAT:
   320             case MOJOSHADER_UNIFORM_FLOAT:
   323                 memcpy(
   321                 memcpy(
   324                     contents + (offset * 16),
   322                     contents + offset,
   325                     &regF[4 * index],
   323                     &regF[4 * index],
   326                     size * 16
   324                     size * 16
   327                 );
   325                 );
   328                 break;
   326                 break;
   329 
   327 
   330             case MOJOSHADER_UNIFORM_INT:
   328             case MOJOSHADER_UNIFORM_INT:
   331                 memcpy(
   329                 memcpy(
   332                     contents + (offset * 16),
   330                     contents + offset,
   333                     &regI[4 * index],
   331                     &regI[4 * index],
   334                     size * 16
   332                     size * 16
   335                 );
   333                 );
   336                 break;
   334                 break;
   337 
   335 
   338             case MOJOSHADER_UNIFORM_BOOL:
   336             case MOJOSHADER_UNIFORM_BOOL:
   339                 memcpy(
   337                 contentsI = (uint32_t *) (contents + offset);
   340                     contents + offset,
   338                 for (j = 0; j < size; j++)
   341                     &regB[index],
   339                     contentsI[j * 4] = regB[index + j];
   342                     size
       
   343                 );
       
   344                 break;
   340                 break;
   345 
   341 
   346             default:
   342             default:
   347                 set_error(
   343                 set_error(
   348                     "SOMETHING VERY WRONG HAPPENED WHEN UPDATING UNIFORMS"
   344                     "SOMETHING VERY WRONG HAPPENED WHEN UPDATING UNIFORMS"
   349                 );
   345                 );
   350                 assert(0);
   346                 assert(0);
   351                 break;
   347                 break;
   352         } // switch
   348         } // switch
   353 
   349 
   354         offset += size;
   350         offset += size * 16;
   355     } // for
   351     } // for
   356 
   352 
   357     ctx->vkUnmapMemory(
   353     ctx->vkUnmapMemory(
   358         *ctx->logical_device,
   354         *ctx->logical_device,
   359         uboMemory
   355         uboMemory