mirror of https://github.com/stenzek/duckstation
MetalDevice: Support multisampling
parent
6fbea12ed3
commit
a2f19143da
@ -0,0 +1,47 @@
|
||||
# Borrowed from PCSX2.
|
||||
|
||||
if(APPLE)
|
||||
function(add_metal_sources target sources)
|
||||
if(CMAKE_GENERATOR MATCHES "Xcode")
|
||||
# If we're generating an xcode project, you can just add the shaders to the main pcsx2 target and xcode will deal with them properly
|
||||
# This will make sure xcode supplies code completion, etc (if you use a custom command, it won't)
|
||||
set_target_properties(${target} PROPERTIES
|
||||
XCODE_ATTRIBUTE_MTL_ENABLE_DEBUG_INFO INCLUDE_SOURCE
|
||||
)
|
||||
foreach(shader IN LISTS sources)
|
||||
target_sources(${target} PRIVATE ${shader})
|
||||
set_source_files_properties(${shader} PROPERTIES LANGUAGE METAL)
|
||||
endforeach()
|
||||
else()
|
||||
function(generateMetallib std triple outputName)
|
||||
set(MetalShaderOut)
|
||||
set(flags
|
||||
-ffast-math
|
||||
$<$<NOT:$<CONFIG:Release,MinSizeRel>>:-gline-tables-only>
|
||||
$<$<NOT:$<CONFIG:Release,MinSizeRel>>:-MO>
|
||||
)
|
||||
foreach(shader IN LISTS sources)
|
||||
file(RELATIVE_PATH relativeShader "${CMAKE_SOURCE_DIR}" "${shader}")
|
||||
set(shaderOut ${CMAKE_CURRENT_BINARY_DIR}/${outputName}/${relativeShader}.air)
|
||||
list(APPEND MetalShaderOut ${shaderOut})
|
||||
get_filename_component(shaderDir ${shaderOut} DIRECTORY)
|
||||
add_custom_command(OUTPUT ${shaderOut}
|
||||
COMMAND ${CMAKE_COMMAND} -E make_directory ${shaderDir}
|
||||
COMMAND xcrun metal ${flags} -std=${std} -target ${triple} -o ${shaderOut} -c ${shader}
|
||||
DEPENDS ${shader}
|
||||
)
|
||||
set(metallib ${CMAKE_CURRENT_BINARY_DIR}/${outputName}.metallib)
|
||||
endforeach()
|
||||
add_custom_command(OUTPUT ${metallib}
|
||||
COMMAND xcrun metallib -o ${metallib} ${MetalShaderOut}
|
||||
DEPENDS ${MetalShaderOut}
|
||||
)
|
||||
target_sources(${target} PRIVATE ${metallib})
|
||||
set_source_files_properties(${metallib} PROPERTIES MACOSX_PACKAGE_LOCATION Resources)
|
||||
endfunction()
|
||||
generateMetallib(macos-metal2.0 air64-apple-macos10.13 default)
|
||||
generateMetallib(macos-metal2.2 air64-apple-macos10.15 Metal22)
|
||||
generateMetallib(macos-metal2.3 air64-apple-macos11.0 Metal23)
|
||||
endif()
|
||||
endfunction()
|
||||
endif()
|
||||
@ -0,0 +1,42 @@
|
||||
/// A custom resolve kernel that averages color at all sample points.
|
||||
#include <metal_stdlib>
|
||||
using namespace metal;
|
||||
|
||||
// https://developer.apple.com/documentation/metal/metal_sample_code_library/improving_edge-rendering_quality_with_multisample_antialiasing_msaa?language=objc
|
||||
kernel void
|
||||
colorResolveKernel(texture2d_ms<float, access::read> multisampledTexture [[texture(0)]],
|
||||
texture2d<float, access::write> resolvedTexture [[texture(1)]],
|
||||
uint2 gid [[thread_position_in_grid]])
|
||||
{
|
||||
const uint count = multisampledTexture.get_num_samples();
|
||||
|
||||
float4 resolved_color = 0;
|
||||
|
||||
for (uint i = 0; i < count; ++i)
|
||||
{
|
||||
resolved_color += multisampledTexture.read(gid, i);
|
||||
}
|
||||
|
||||
resolved_color /= count;
|
||||
|
||||
resolvedTexture.write(resolved_color, gid);
|
||||
}
|
||||
|
||||
kernel void
|
||||
depthResolveKernel(texture2d_ms<float, access::read> multisampledTexture [[texture(0)]],
|
||||
texture2d<float, access::write> resolvedTexture [[texture(1)]],
|
||||
uint2 gid [[thread_position_in_grid]])
|
||||
{
|
||||
const uint count = multisampledTexture.get_num_samples();
|
||||
|
||||
float resolved_depth = 0;
|
||||
|
||||
for (uint i = 0; i < count; ++i)
|
||||
{
|
||||
resolved_depth += multisampledTexture.read(gid, i).r;
|
||||
}
|
||||
|
||||
resolved_depth /= count;
|
||||
|
||||
resolvedTexture.write(float4(resolved_depth, 0, 0, 0), gid);
|
||||
}
|
||||
Loading…
Reference in New Issue