Skip to content

Commit

Permalink
-Mip map experiments
Browse files Browse the repository at this point in the history
  • Loading branch information
maluoi committed Jul 28, 2019
1 parent 62c30a6 commit b4b0120
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 25 deletions.
12 changes: 6 additions & 6 deletions StereoKitC/stereokit.h
Expand Up @@ -123,12 +123,12 @@ SK_API void mesh_set_inds (mesh_t mesh, uint16_t *indices, int index_count);

SK_DeclarePrivateType(tex2d_t);

SK_API tex2d_t tex2d_create (const char *name);
SK_API tex2d_t tex2d_create_cubemap(const char *id, const char **files); // x+, x-, y+, y-, z+, z-
SK_API tex2d_t tex2d_create_file (const char *file);
SK_API void tex2d_release (tex2d_t texture);
SK_API void tex2d_set_colors (tex2d_t texture, int width, int height, uint8_t *data_rgba32);
SK_API void tex2d_set_colors_cube(tex2d_t texture, int width, int height, uint8_t **data_faces_rgba32); // x+, x-, y+, y-, z+, z-
SK_API tex2d_t tex2d_create (const char *name);
SK_API tex2d_t tex2d_create_cubemap (const char *id, const char **files, int make_mip_maps = true); // x+, x-, y+, y-, z+, z-
SK_API tex2d_t tex2d_create_file (const char *file, int make_mip_maps = true);
SK_API void tex2d_release (tex2d_t texture);
SK_API void tex2d_set_colors (tex2d_t texture, int width, int height, uint8_t *data_rgba32, int make_mip_maps = true);
SK_API void tex2d_set_colors_cube(tex2d_t texture, int width, int height, uint8_t **data_faces_rgba32, int make_mip_maps = true); // x+, x-, y+, y-, z+, z-

///////////////////////////////////////////

Expand Down
45 changes: 30 additions & 15 deletions StereoKitC/texture.cpp
Expand Up @@ -9,7 +9,7 @@
tex2d_t tex2d_create(const char *name) {
return (tex2d_t)assets_allocate(asset_type_texture, name);
}
tex2d_t tex2d_create_cubemap(const char *id, const char **files) {
tex2d_t tex2d_create_cubemap(const char *id, const char **files, int make_mip_maps) {
tex2d_t result = (tex2d_t)assets_find(files[0]);
if (result != nullptr) {
assets_addref(result->header);
Expand Down Expand Up @@ -49,11 +49,11 @@ tex2d_t tex2d_create_cubemap(const char *id, const char **files) {

// Create with the data we have
result = tex2d_create(id);
tex2d_set_colors_cube(result, final_width, final_height, &data[0]);
tex2d_set_colors_cube(result, final_width, final_height, &data[0], make_mip_maps);

return result;
}
tex2d_t tex2d_create_file(const char *file) {
tex2d_t tex2d_create_file(const char *file, int make_mip_maps) {
tex2d_t result = (tex2d_t)assets_find(file);
if (result != nullptr) {
assets_addref(result->header);
Expand All @@ -70,12 +70,12 @@ tex2d_t tex2d_create_file(const char *file) {
}
result = tex2d_create(file);

tex2d_set_colors(result, width, height, data);
tex2d_set_colors(result, width, height, data, make_mip_maps);
free(data);

return result;
}
tex2d_t tex2d_create_mem(const char *id, void *data, size_t data_size) {
tex2d_t tex2d_create_mem(const char *id, void *data, size_t data_size, int make_mip_maps) {
tex2d_t result = (tex2d_t)assets_find(id);
if (result != nullptr) {
assets_addref(result->header);
Expand All @@ -92,7 +92,7 @@ tex2d_t tex2d_create_mem(const char *id, void *data, size_t data_size) {
}
result = tex2d_create(id);

tex2d_set_colors(result, width, height, col_data);
tex2d_set_colors(result, width, height, col_data, make_mip_maps);
free(col_data);

return result;
Expand All @@ -107,7 +107,7 @@ void tex2d_destroy(tex2d_t tex) {
*tex = {};
}

void tex2d_set_colors(tex2d_t texture, int width, int height, uint8_t *data_rgba32) {
void tex2d_set_colors(tex2d_t texture, int width, int height, uint8_t *data_rgba32, int make_mip_maps) {
if (texture->width != width || texture->height != height || (texture->resource != nullptr && texture->can_write == false)) {
if (texture->resource != nullptr) {
texture->resource->Release();
Expand All @@ -117,17 +117,21 @@ void tex2d_set_colors(tex2d_t texture, int width, int height, uint8_t *data_rgba
D3D11_TEXTURE2D_DESC desc = {};
desc.Width = width;
desc.Height = height;
desc.MipLevels = 1;
desc.MipLevels = 1;//(int)(log(min(width,height)) / log(2)) - 1;
desc.ArraySize = 1;
desc.SampleDesc.Count = 1;
desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
desc.BindFlags = D3D11_BIND_SHADER_RESOURCE | (make_mip_maps?D3D11_BIND_RENDER_TARGET:0);
desc.Usage = texture->can_write ? D3D11_USAGE_DYNAMIC : D3D11_USAGE_DEFAULT;
desc.CPUAccessFlags = texture->can_write ? D3D11_CPU_ACCESS_WRITE : 0;
desc.MiscFlags = make_mip_maps ? D3D11_RESOURCE_MISC_GENERATE_MIPS : 0;
if (desc.MipLevels < 1)
desc.MipLevels = 1;

D3D11_SUBRESOURCE_DATA data;
data.pSysMem = data_rgba32;
data.SysMemPitch = sizeof(uint8_t) * 4 * width;
data.SysMemSlicePitch = 0;

if (FAILED(d3d_device->CreateTexture2D(&desc, &data, &texture->texture))) {
printf("Create texture error!\n");
Expand All @@ -136,10 +140,13 @@ void tex2d_set_colors(tex2d_t texture, int width, int height, uint8_t *data_rgba

D3D11_SHADER_RESOURCE_VIEW_DESC res_desc = {};
res_desc.Format = desc.Format;
res_desc.Texture2D.MipLevels = desc.MipLevels;
res_desc.Texture2D.MipLevels = -1;// desc.MipLevels;
res_desc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
d3d_device->CreateShaderResourceView(texture->texture, &res_desc, &texture->resource);

if (make_mip_maps)
d3d_context->GenerateMips(texture->resource);

texture->width = width;
texture->height = height;
return;
Expand All @@ -161,22 +168,27 @@ void tex2d_set_colors(tex2d_t texture, int width, int height, uint8_t *data_rgba
src_line += width * sizeof(uint8_t) * 4;
}
d3d_context->Unmap(texture->texture, 0);

if (make_mip_maps)
d3d_context->GenerateMips(texture->resource);
}

void tex2d_set_colors_cube(tex2d_t texture, int width, int height, uint8_t **data_faces_rgba32) {
void tex2d_set_colors_cube(tex2d_t texture, int width, int height, uint8_t **data_faces_rgba32, int make_mip_maps) {
if (texture->resource != nullptr)
texture->resource->Release();
D3D11_TEXTURE2D_DESC desc = {};
desc.Width = width;
desc.Height = height;
desc.MipLevels = 1;
desc.MipLevels = 1;// (int)(log(min(width, height)) / log(2)) - 1;
desc.ArraySize = 6;
desc.SampleDesc.Count = 1;
desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
desc.BindFlags = D3D11_BIND_SHADER_RESOURCE | (make_mip_maps?D3D11_BIND_RENDER_TARGET:0);
desc.Usage = D3D11_USAGE_DEFAULT;
desc.CPUAccessFlags = 0;
desc.MiscFlags = D3D11_RESOURCE_MISC_TEXTURECUBE;
desc.MiscFlags = D3D11_RESOURCE_MISC_TEXTURECUBE | (make_mip_maps?D3D11_RESOURCE_MISC_GENERATE_MIPS:0);
if (desc.MipLevels < 1)
desc.MipLevels = 1;

D3D11_SUBRESOURCE_DATA data[6];
for (size_t i = 0; i < 6; i++) {
Expand All @@ -192,10 +204,13 @@ void tex2d_set_colors_cube(tex2d_t texture, int width, int height, uint8_t **dat

D3D11_SHADER_RESOURCE_VIEW_DESC res_desc = {};
res_desc.Format = desc.Format;
res_desc.Texture2D.MipLevels = desc.MipLevels;
res_desc.Texture2D.MipLevels = -1;// desc.MipLevels;
res_desc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURECUBE;
d3d_device->CreateShaderResourceView(texture->texture, &res_desc, &texture->resource);

if (make_mip_maps)
d3d_context->GenerateMips(texture->resource);

texture->width = width;
texture->height = height;
}
Expand Down
2 changes: 1 addition & 1 deletion StereoKitC/texture.h
Expand Up @@ -14,6 +14,6 @@ struct _tex2d_t {
ID3D11Texture2D *texture;
};

tex2d_t tex2d_create_mem(const char *id, void *data, size_t data_size);
tex2d_t tex2d_create_mem(const char *id, void *data, size_t data_size, int make_mip_maps = true);
void tex2d_set_active(tex2d_t texture, int slot);
void tex2d_destroy (tex2d_t texture);
7 changes: 4 additions & 3 deletions StereoKitCTest/Assets/pbr.hlsl
Expand Up @@ -78,8 +78,9 @@ float4 ps(psIn input) : SV_TARGET {
float NdotV = (dot(normal,view));

float3 reflection = reflect(-view, normal);
float3 reflection_color = sk_cubemap.Sample(tex_sampler, reflection).rgb;
return float4(reflection_color, 1);
float3 reflection_color = sk_cubemap.SampleLevel(tex_sampler, reflection, rough*8).rgb;
//return float4(reflection_color, 1);
float3 specular_color = lerp(albedo, reflection_color, metal);

// Lighting an object is a combination of two types of light reflections,
// a diffuse reflection, and a specular reflection. These reflections
Expand Down Expand Up @@ -120,7 +121,7 @@ float4 ps(psIn input) : SV_TARGET {
// h is the half vector, h = normalize(l + v)
float3 specular =
(DistributionGGX(normal, half_vec, rough) *
FresnelSchlick (NdotV, albedo, metal) *
FresnelSchlick (NdotV, specular_color, metal) *
GeometrySmith (max(NdotL, 0), max(NdotV, 0), rough))
//GeometrySchlickGGX(max(NdotV, 0), rough))

Expand Down

0 comments on commit b4b0120

Please sign in to comment.