From 491a4e28c0161fa860fa7e632ad83bfac0bb3282 Mon Sep 17 00:00:00 2001 From: Magnus Norddahl Date: Sat, 15 Oct 2016 15:04:14 +0200 Subject: [PATCH] Move true color sky drawing to its own drawers and chamge r_stretchsky to false as the new drawers can fade to a solid color --- src/CMakeLists.txt | 1 + .../fixedfunction/drawskycodegen.cpp | 127 +++++++++++++ src/r_compiler/fixedfunction/drawskycodegen.h | 39 ++++ src/r_compiler/llvmdrawers.cpp | 41 +++++ src/r_compiler/llvmdrawers.h | 28 +++ src/r_compiler/ssa/ssa_int.cpp | 5 + src/r_compiler/ssa/ssa_int.h | 1 + src/r_compiler/ssa/ssa_vec4i.cpp | 14 -- src/r_compiler/ssa/ssa_vec4i.h | 2 - src/r_draw.h | 5 + src/r_draw_rgba.cpp | 65 +++++++ src/r_plane.cpp | 169 ++++++++++++++++++ src/r_sky.cpp | 2 +- 13 files changed, 482 insertions(+), 17 deletions(-) create mode 100644 src/r_compiler/fixedfunction/drawskycodegen.cpp create mode 100644 src/r_compiler/fixedfunction/drawskycodegen.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 40aa5ae374..a900b6352e 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1459,6 +1459,7 @@ set (PCH_SOURCES r_compiler/fixedfunction/drawspancodegen.cpp r_compiler/fixedfunction/drawwallcodegen.cpp r_compiler/fixedfunction/drawcolumncodegen.cpp + r_compiler/fixedfunction/drawskycodegen.cpp r_data/sprites.cpp r_data/voxels.cpp r_data/renderstyle.cpp diff --git a/src/r_compiler/fixedfunction/drawskycodegen.cpp b/src/r_compiler/fixedfunction/drawskycodegen.cpp new file mode 100644 index 0000000000..f2ba148fdd --- /dev/null +++ b/src/r_compiler/fixedfunction/drawskycodegen.cpp @@ -0,0 +1,127 @@ + +#include "i_system.h" +#include "r_compiler/llvm_include.h" +#include "r_compiler/fixedfunction/drawskycodegen.h" +#include "r_compiler/ssa/ssa_function.h" +#include "r_compiler/ssa/ssa_scope.h" +#include "r_compiler/ssa/ssa_for_block.h" +#include "r_compiler/ssa/ssa_if_block.h" +#include "r_compiler/ssa/ssa_stack.h" +#include "r_compiler/ssa/ssa_function.h" +#include "r_compiler/ssa/ssa_struct_type.h" +#include "r_compiler/ssa/ssa_value.h" + +void DrawSkyCodegen::Generate(DrawSkyVariant variant, bool fourColumns, SSAValue args, SSAValue thread_data) +{ + dest = args[0][0].load(true); + source0[0] = args[0][1].load(true); + source0[1] = args[0][2].load(true); + source0[2] = args[0][3].load(true); + source0[3] = args[0][4].load(true); + source1[0] = args[0][5].load(true); + source1[1] = args[0][6].load(true); + source1[2] = args[0][7].load(true); + source1[3] = args[0][8].load(true); + pitch = args[0][9].load(true); + count = args[0][10].load(true); + dest_y = args[0][11].load(true); + texturefrac[0] = args[0][12].load(true); + texturefrac[1] = args[0][13].load(true); + texturefrac[2] = args[0][14].load(true); + texturefrac[3] = args[0][15].load(true); + iscale[0] = args[0][16].load(true); + iscale[1] = args[0][17].load(true); + iscale[2] = args[0][18].load(true); + iscale[3] = args[0][19].load(true); + textureheight0 = args[0][20].load(true); + textureheight1 = args[0][21].load(true); + top_color = SSAVec4i::unpack(args[0][22].load(true)); + bottom_color = SSAVec4i::unpack(args[0][23].load(true)); + + thread.core = thread_data[0][0].load(true); + thread.num_cores = thread_data[0][1].load(true); + thread.pass_start_y = thread_data[0][2].load(true); + thread.pass_end_y = thread_data[0][3].load(true); + + count = count_for_thread(dest_y, count, thread); + dest = dest_for_thread(dest_y, pitch, dest, thread); + + pitch = pitch * thread.num_cores; + + int numColumns = fourColumns ? 4 : 1; + for (int i = 0; i < numColumns; i++) + { + stack_frac[i].store(texturefrac[i] + iscale[i] * skipped_by_thread(dest_y, thread)); + fracstep[i] = iscale[i] * thread.num_cores; + } + + Loop(variant, fourColumns); +} + +void DrawSkyCodegen::Loop(DrawSkyVariant variant, bool fourColumns) +{ + int numColumns = fourColumns ? 4 : 1; + + stack_index.store(SSAInt(0)); + { + SSAForBlock loop; + SSAInt index = stack_index.load(); + loop.loop_block(index < count); + + SSAInt frac[4]; + for (int i = 0; i < numColumns; i++) + frac[i] = stack_frac[i].load(); + + SSAInt offset = index * pitch * 4; + + if (fourColumns) + { + SSAVec4i colors[4]; + for (int i = 0; i < 4; i++) + colors[i] = FadeOut(frac[i], Sample(frac[i], i, variant)); + + SSAVec16ub color(SSAVec8s(colors[0], colors[1]), SSAVec8s(colors[2], colors[3])); + dest[offset].store_unaligned_vec16ub(color); + } + else + { + SSAVec4i color = FadeOut(frac[0], Sample(frac[0], 0, variant)); + dest[offset].store_vec4ub(color); + } + + stack_index.store(index.add(SSAInt(1), true, true)); + for (int i = 0; i < numColumns; i++) + stack_frac[i].store(frac[i] + fracstep[i]); + loop.end_block(); + } +} + +SSAVec4i DrawSkyCodegen::Sample(SSAInt frac, int index, DrawSkyVariant variant) +{ + SSAInt sample_index = (((frac << 8) >> FRACBITS) * textureheight0) >> FRACBITS; + if (variant == DrawSkyVariant::Single) + { + return source0[index][sample_index * 4].load_vec4ub(false); + } + else + { + SSAInt sample_index2 = SSAInt::MIN(sample_index, textureheight1); + SSAVec4i color0 = source0[index][sample_index * 4].load_vec4ub(false); + SSAVec4i color1 = source1[index][sample_index2 * 4].load_vec4ub(false); + return blend_alpha_blend(color0, color1); + } +} + +SSAVec4i DrawSkyCodegen::FadeOut(SSAInt frac, SSAVec4i color) +{ + int start_fade = 2; // How fast it should fade out + + SSAInt alpha_top = SSAInt::MAX(SSAInt::MIN(frac.ashr(16 - start_fade), SSAInt(256)), SSAInt(0)); + SSAInt alpha_bottom = SSAInt::MAX(SSAInt::MIN(((2 << 24) - frac).ashr(16 - start_fade), SSAInt(256)), SSAInt(0)); + SSAInt inv_alpha_top = 256 - alpha_top; + SSAInt inv_alpha_bottom = 256 - alpha_bottom; + + color = (color * alpha_top + top_color * inv_alpha_top) / 256; + color = (color * alpha_bottom + bottom_color * inv_alpha_bottom) / 256; + return color.insert(3, 255); +} diff --git a/src/r_compiler/fixedfunction/drawskycodegen.h b/src/r_compiler/fixedfunction/drawskycodegen.h new file mode 100644 index 0000000000..2b90b2a9d0 --- /dev/null +++ b/src/r_compiler/fixedfunction/drawskycodegen.h @@ -0,0 +1,39 @@ + +#pragma once + +#include "drawercodegen.h" + +enum class DrawSkyVariant +{ + Single, + Double +}; + +class DrawSkyCodegen : public DrawerCodegen +{ +public: + void Generate(DrawSkyVariant variant, bool fourColumns, SSAValue args, SSAValue thread_data); + +private: + void Loop(DrawSkyVariant variant, bool fourColumns); + SSAVec4i Sample(SSAInt frac, int index, DrawSkyVariant variant); + SSAVec4i FadeOut(SSAInt frac, SSAVec4i color); + + SSAStack stack_index, stack_frac[4]; + + SSAUBytePtr dest; + SSAUBytePtr source0[4]; + SSAUBytePtr source1[4]; + SSAInt pitch; + SSAInt count; + SSAInt dest_y; + SSAInt texturefrac[4]; + SSAInt iscale[4]; + SSAInt textureheight0; + SSAInt textureheight1; + SSAVec4i top_color; + SSAVec4i bottom_color; + SSAWorkerThread thread; + + SSAInt fracstep[4]; +}; diff --git a/src/r_compiler/llvmdrawers.cpp b/src/r_compiler/llvmdrawers.cpp index 8dcb73a2c6..4d4aeccbe7 100644 --- a/src/r_compiler/llvmdrawers.cpp +++ b/src/r_compiler/llvmdrawers.cpp @@ -4,6 +4,7 @@ #include "r_compiler/fixedfunction/drawspancodegen.h" #include "r_compiler/fixedfunction/drawwallcodegen.h" #include "r_compiler/fixedfunction/drawcolumncodegen.h" +#include "r_compiler/fixedfunction/drawskycodegen.h" #include "r_compiler/ssa/ssa_function.h" #include "r_compiler/ssa/ssa_scope.h" #include "r_compiler/ssa/ssa_for_block.h" @@ -48,10 +49,12 @@ private: void CodegenDrawColumn(const char *name, DrawColumnVariant variant, DrawColumnMethod method); void CodegenDrawSpan(const char *name, DrawSpanVariant variant); void CodegenDrawWall(const char *name, DrawWallVariant variant, int columns); + void CodegenDrawSky(const char *name, DrawSkyVariant variant, int columns); static llvm::Type *GetDrawColumnArgsStruct(llvm::LLVMContext &context); static llvm::Type *GetDrawSpanArgsStruct(llvm::LLVMContext &context); static llvm::Type *GetDrawWallArgsStruct(llvm::LLVMContext &context); + static llvm::Type *GetDrawSkyArgsStruct(llvm::LLVMContext &context); static llvm::Type *GetWorkerThreadDataStruct(llvm::LLVMContext &context); LLVMProgram mProgram; @@ -140,6 +143,10 @@ LLVMDrawersImpl::LLVMDrawersImpl() CodegenDrawWall("tmvline4_subclamp", DrawWallVariant::SubClamp, 4); CodegenDrawWall("tmvline1_revsubclamp", DrawWallVariant::RevSubClamp, 1); CodegenDrawWall("tmvline4_revsubclamp", DrawWallVariant::RevSubClamp, 4); + CodegenDrawSky("DrawSky1", DrawSkyVariant::Single, 1); + CodegenDrawSky("DrawSky4", DrawSkyVariant::Single, 4); + CodegenDrawSky("DrawDoubleSky1", DrawSkyVariant::Double, 1); + CodegenDrawSky("DrawDoubleSky4", DrawSkyVariant::Double, 4); mProgram.CreateEE(); @@ -201,6 +208,10 @@ LLVMDrawersImpl::LLVMDrawersImpl() tmvline4_subclamp = mProgram.GetProcAddress("tmvline4_subclamp"); tmvline1_revsubclamp = mProgram.GetProcAddress("tmvline1_revsubclamp"); tmvline4_revsubclamp = mProgram.GetProcAddress("tmvline4_revsubclamp"); + DrawSky1 = mProgram.GetProcAddress("DrawSky1"); + DrawSky4 = mProgram.GetProcAddress("DrawSky4"); + DrawDoubleSky1 = mProgram.GetProcAddress("DrawDoubleSky1"); + DrawDoubleSky4 = mProgram.GetProcAddress("DrawDoubleSky4"); #if 0 std::vector foo(1024 * 4); @@ -292,6 +303,25 @@ void LLVMDrawersImpl::CodegenDrawWall(const char *name, DrawWallVariant variant, I_FatalError("verifyFunction failed for " __FUNCTION__); } +void LLVMDrawersImpl::CodegenDrawSky(const char *name, DrawSkyVariant variant, int columns) +{ + llvm::IRBuilder<> builder(mProgram.context()); + SSAScope ssa_scope(&mProgram.context(), mProgram.module(), &builder); + + SSAFunction function(name); + function.add_parameter(GetDrawSkyArgsStruct(mProgram.context())); + function.add_parameter(GetWorkerThreadDataStruct(mProgram.context())); + function.create_public(); + + DrawSkyCodegen codegen; + codegen.Generate(variant, columns == 4, function.parameter(0), function.parameter(1)); + + builder.CreateRetVoid(); + + if (llvm::verifyFunction(*function.func)) + I_FatalError("verifyFunction failed for " __FUNCTION__); +} + llvm::Type *LLVMDrawersImpl::GetDrawColumnArgsStruct(llvm::LLVMContext &context) { std::vector elements; @@ -375,6 +405,17 @@ llvm::Type *LLVMDrawersImpl::GetDrawWallArgsStruct(llvm::LLVMContext &context) return llvm::StructType::create(context, elements, "DrawWallArgs", false)->getPointerTo(); } +llvm::Type *LLVMDrawersImpl::GetDrawSkyArgsStruct(llvm::LLVMContext &context) +{ + std::vector elements; + elements.push_back(llvm::Type::getInt8PtrTy(context)); + for (int i = 0; i < 8; i++) + elements.push_back(llvm::Type::getInt8PtrTy(context)); + for (int i = 0; i < 15; i++) + elements.push_back(llvm::Type::getInt32Ty(context)); + return llvm::StructType::create(context, elements, "DrawSkyArgs", false)->getPointerTo(); +} + llvm::Type *LLVMDrawersImpl::GetWorkerThreadDataStruct(llvm::LLVMContext &context) { std::vector elements; diff --git a/src/r_compiler/llvmdrawers.h b/src/r_compiler/llvmdrawers.h index 7f5f6658e2..64d73eeee3 100644 --- a/src/r_compiler/llvmdrawers.h +++ b/src/r_compiler/llvmdrawers.h @@ -133,6 +133,29 @@ struct DrawColumnArgs } }; +struct DrawSkyArgs +{ + uint32_t *dest; + const uint32_t *source0[4]; + const uint32_t *source1[4]; + int32_t pitch; + int32_t count; + int32_t dest_y; + uint32_t texturefrac[4]; + uint32_t iscale[4]; + uint32_t textureheight0; + uint32_t textureheight1; + uint32_t top_color; + uint32_t bottom_color; + + FString ToString() + { + FString info; + info.Format("dest_y = %i, count = %i", dest_y, count); + return info; + } +}; + class LLVMDrawers { public: @@ -203,6 +226,11 @@ public: void(*tmvline1_revsubclamp)(const DrawWallArgs *, const WorkerThreadData *) = nullptr; void(*tmvline4_revsubclamp)(const DrawWallArgs *, const WorkerThreadData *) = nullptr; + void(*DrawSky1)(const DrawSkyArgs *, const WorkerThreadData *) = nullptr; + void(*DrawSky4)(const DrawSkyArgs *, const WorkerThreadData *) = nullptr; + void(*DrawDoubleSky1)(const DrawSkyArgs *, const WorkerThreadData *) = nullptr; + void(*DrawDoubleSky4)(const DrawSkyArgs *, const WorkerThreadData *) = nullptr; + private: static LLVMDrawers *Singleton; }; diff --git a/src/r_compiler/ssa/ssa_int.cpp b/src/r_compiler/ssa/ssa_int.cpp index 1815985c5f..6f2afce7f1 100644 --- a/src/r_compiler/ssa/ssa_int.cpp +++ b/src/r_compiler/ssa/ssa_int.cpp @@ -47,6 +47,11 @@ SSAInt SSAInt::add(SSAInt b, bool no_unsigned_wrap, bool no_signed_wrap) return SSAInt::from_llvm(SSAScope::builder().CreateAdd(v, b.v, SSAScope::hint(), no_unsigned_wrap, no_signed_wrap)); } +SSAInt SSAInt::ashr(int bits) +{ + return SSAInt::from_llvm(SSAScope::builder().CreateAShr(v, bits, SSAScope::hint())); +} + SSAInt operator+(const SSAInt &a, const SSAInt &b) { return SSAInt::from_llvm(SSAScope::builder().CreateAdd(a.v, b.v, SSAScope::hint())); diff --git a/src/r_compiler/ssa/ssa_int.h b/src/r_compiler/ssa/ssa_int.h index e9ce978c47..ef71e064a2 100644 --- a/src/r_compiler/ssa/ssa_int.h +++ b/src/r_compiler/ssa/ssa_int.h @@ -20,6 +20,7 @@ public: static SSAInt MAX(SSAInt a, SSAInt b); SSAInt add(SSAInt b, bool no_unsigned_wrap, bool no_signed_wrap); + SSAInt ashr(int bits); llvm::Value *v; }; diff --git a/src/r_compiler/ssa/ssa_vec4i.cpp b/src/r_compiler/ssa/ssa_vec4i.cpp index 3be0ec194a..60d6161a58 100644 --- a/src/r_compiler/ssa/ssa_vec4i.cpp +++ b/src/r_compiler/ssa/ssa_vec4i.cpp @@ -171,20 +171,6 @@ SSAVec4i SSAVec4i::sqrt(SSAVec4i f) return SSAVec4i::from_llvm(SSAScope::builder().CreateCall(SSAScope::intrinsic(llvm::Intrinsic::x86_sse2_sqrt_pd), f.v, SSAScope::hint())); } -/* -SSAVec4i SSAVec4i::min_sse41(SSAVec4i a, SSAVec4i b) -{ - llvm::Value *values[2] = { a.v, b.v }; - return SSAVec4i::from_llvm(SSAScope::builder().CreateCall(SSAScope::intrinsic(llvm::Intrinsic::x86_sse41_pminsd), values, SSAScope::hint())); -} - -SSAVec4i SSAVec4i::max_sse41(SSAVec4i a, SSAVec4i b) -{ - llvm::Value *values[2] = { a.v, b.v }; - return SSAVec4i::from_llvm(SSAScope::builder().CreateCall(SSAScope::intrinsic(llvm::Intrinsic::x86_sse41_pmaxsd), values, SSAScope::hint())); -} -*/ - SSAVec4i operator+(const SSAVec4i &a, const SSAVec4i &b) { return SSAVec4i::from_llvm(SSAScope::builder().CreateAdd(a.v, b.v, SSAScope::hint())); diff --git a/src/r_compiler/ssa/ssa_vec4i.h b/src/r_compiler/ssa/ssa_vec4i.h index f8ef92f1e2..420ab021e9 100644 --- a/src/r_compiler/ssa/ssa_vec4i.h +++ b/src/r_compiler/ssa/ssa_vec4i.h @@ -35,8 +35,6 @@ public: static SSAVec4i combinehi(SSAVec8s v0, SSAVec8s v1); static SSAVec4i combinelo(SSAVec8s v0, SSAVec8s v1); static SSAVec4i sqrt(SSAVec4i f); - //static SSAVec4i min_sse41(SSAVec4i a, SSAVec4i b); - //static SSAVec4i max_sse41(SSAVec4i a, SSAVec4i b); static SSAVec4i from_llvm(llvm::Value *v) { return SSAVec4i(v); } static llvm::Type *llvm_type(); diff --git a/src/r_draw.h b/src/r_draw.h index 0708b8714f..4c478a7262 100644 --- a/src/r_draw.h +++ b/src/r_draw.h @@ -381,6 +381,11 @@ void R_SetDSColorMapLight(FSWColormap *base_colormap, float light, int shade); void R_SetTranslationMap(lighttable_t *translation); +void R_DrawSingleSkyCol1(uint32_t solid_top, uint32_t solid_bottom); +void R_DrawSingleSkyCol4(uint32_t solid_top, uint32_t solid_bottom); +void R_DrawDoubleSkyCol1(uint32_t solid_top, uint32_t solid_bottom); +void R_DrawDoubleSkyCol4(uint32_t solid_top, uint32_t solid_bottom); + extern bool r_swtruecolor; EXTERN_CVAR(Bool, r_multithreaded); diff --git a/src/r_draw_rgba.cpp b/src/r_draw_rgba.cpp index 5114b95f06..4b3d98c04b 100644 --- a/src/r_draw_rgba.cpp +++ b/src/r_draw_rgba.cpp @@ -377,6 +377,47 @@ public: } }; +class DrawSkyLLVMCommand : public DrawerCommand +{ +protected: + DrawSkyArgs args; + + WorkerThreadData ThreadData(DrawerThread *thread) + { + WorkerThreadData d; + d.core = thread->core; + d.num_cores = thread->num_cores; + d.pass_start_y = thread->pass_start_y; + d.pass_end_y = thread->pass_end_y; + return d; + } + +public: + DrawSkyLLVMCommand(uint32_t solid_top, uint32_t solid_bottom) + { + args.dest = (uint32_t*)dc_dest; + args.dest_y = _dest_y; + args.count = dc_count; + args.pitch = dc_pitch; + for (int i = 0; i < 4; i++) + { + args.texturefrac[i] = vplce[i]; + args.iscale[i] = vince[i]; + args.source0[i] = (const uint32_t *)bufplce[i]; + args.source1[i] = (const uint32_t *)bufplce2[i]; + } + args.textureheight0 = bufheight[0]; + args.textureheight1 = bufheight[1]; + args.top_color = solid_top; + args.bottom_color = solid_bottom; + } + + FString DebugInfo() override + { + return "DrawSkyLLVMCommand\n" + args.ToString(); + } +}; + #define DECLARE_DRAW_COMMAND(name, func, base) \ class name##LLVMCommand : public base \ { \ @@ -416,6 +457,10 @@ DECLARE_DRAW_COMMAND(FillColumnAdd, FillColumnAdd, DrawColumnLLVMCommand); DECLARE_DRAW_COMMAND(FillColumnAddClamp, FillColumnAddClamp, DrawColumnLLVMCommand); DECLARE_DRAW_COMMAND(FillColumnSubClamp, FillColumnSubClamp, DrawColumnLLVMCommand); DECLARE_DRAW_COMMAND(FillColumnRevSubClamp, FillColumnRevSubClamp, DrawColumnLLVMCommand); +DECLARE_DRAW_COMMAND(DrawSingleSky1, DrawSky1, DrawSkyLLVMCommand); +DECLARE_DRAW_COMMAND(DrawSingleSky4, DrawSky4, DrawSkyLLVMCommand); +DECLARE_DRAW_COMMAND(DrawDoubleSky1, DrawDoubleSky1, DrawSkyLLVMCommand); +DECLARE_DRAW_COMMAND(DrawDoubleSky4, DrawDoubleSky4, DrawSkyLLVMCommand); ///////////////////////////////////////////////////////////////////////////// @@ -1203,6 +1248,26 @@ void ApplySpecialColormapRGBACommand::Execute(DrawerThread *thread) ///////////////////////////////////////////////////////////////////////////// +void R_DrawSingleSkyCol1(uint32_t solid_top, uint32_t solid_bottom) +{ + DrawerCommandQueue::QueueCommand(solid_top, solid_bottom); +} + +void R_DrawSingleSkyCol4(uint32_t solid_top, uint32_t solid_bottom) +{ + DrawerCommandQueue::QueueCommand(solid_top, solid_bottom); +} + +void R_DrawDoubleSkyCol1(uint32_t solid_top, uint32_t solid_bottom) +{ + DrawerCommandQueue::QueueCommand(solid_top, solid_bottom); +} + +void R_DrawDoubleSkyCol4(uint32_t solid_top, uint32_t solid_bottom) +{ + DrawerCommandQueue::QueueCommand(solid_top, solid_bottom); +} + void R_DrawColumn_rgba() { DrawerCommandQueue::QueueCommand(); diff --git a/src/r_plane.cpp b/src/r_plane.cpp index 0d87be4b9b..8f2a7f6308 100644 --- a/src/r_plane.cpp +++ b/src/r_plane.cpp @@ -985,8 +985,177 @@ static const BYTE *R_GetTwoSkyColumns (FTexture *fronttex, int x) } } +static void R_DrawSkyColumn(int start_x, int y1, int y2, int columns) +{ + uint32_t height = frontskytex->GetHeight(); + + for (int i = 0; i < columns; i++) + { + int x = start_x + i; + + int uv_fracbits = 24 - frontskytex->HeightBits; + double uv_stepd = skyiscale * frontskytex->Scale.Y; + double v = (skymid * frontskytex->Scale.Y + uv_stepd * (y1 - CenterY + 0.5)) / height; + v = v + 1.0f; + v *= height; + v *= (1 << uv_fracbits); + uint32_t uv_pos = (uint32_t)v; + uint32_t uv_step = xs_ToFixed(uv_fracbits, uv_stepd); + if (uv_step == 0) // To prevent divide by zero elsewhere + uv_step = 1; + + if (MirrorFlags & RF_XFLIP) + x = (viewwidth - x); + + DWORD ang, angle1, angle2; + + if (r_linearsky) + { + angle_t xangle = (angle_t)((0.5 - x / (double)viewwidth) * FocalTangent * ANGLE_90); + ang = (skyangle + xangle) ^ skyflip; + } + else + { + ang = (skyangle + xtoviewangle[x]) ^ skyflip; + } + angle1 = (DWORD)((UMulScale16(ang, frontcyl) + frontpos) >> FRACBITS); + angle2 = (DWORD)((UMulScale16(ang, backcyl) + backpos) >> FRACBITS); + + bufplce[i] = (const BYTE *)frontskytex->GetColumnBgra(angle1, nullptr); + bufplce2[i] = backskytex ? (const BYTE *)backskytex->GetColumnBgra(angle2, nullptr) : nullptr; + buftexturefracx[i] = 0; + vince[i] = uv_step; + vplce[i] = uv_pos; + } + + bufheight[0] = height; + bufheight[1] = backskytex ? backskytex->GetHeight() : height; + dc_dest = (ylookup[y1] + start_x) * 4 + dc_destorg; + dc_count = y2 - y1; + + // To do: figure out how GZDoom calculates the solid top and bottom colors + uint32_t solid_top = 0xff7f7f7f; + uint32_t solid_bottom = 0xff3f3f3f; + + if (columns == 4) + if (!backskytex) + R_DrawSingleSkyCol4(solid_top, solid_bottom); + else + R_DrawDoubleSkyCol4(solid_top, solid_bottom); + else + if (!backskytex) + R_DrawSingleSkyCol1(solid_top, solid_bottom); + else + R_DrawDoubleSkyCol1(solid_top, solid_bottom); +} + +static void R_DrawTruecolorSky(visplane_t *pl) +{ + R_SetColorMapLight(fixedcolormap, 0, 0); + palookupoffse[0] = dc_colormap; + palookupoffse[1] = dc_colormap; + palookupoffse[2] = dc_colormap; + palookupoffse[3] = dc_colormap; + palookuplight[0] = 0; + palookuplight[1] = 0; + palookuplight[2] = 0; + palookuplight[3] = 0; + setupvline(FRACBITS); + + int x1 = pl->left; + int x2 = pl->right; + short *uwal = (short *)pl->top; + short *dwal = (short *)pl->bottom; + + // Calculate where 4 column alignment begins and ends: + int aligned_x1 = clamp((x1 + 3) / 4 * 4, x1, x2); + int aligned_x2 = clamp(x2 / 4 * 4, x1, x2); + + // First unaligned columns: + for (int x = x1; x < aligned_x1; x++) + { + int y1 = uwal[x]; + int y2 = dwal[x]; + if (y2 <= y1) + continue; + + R_DrawSkyColumn(x, y1, y2, 1); + } + + // The aligned columns + for (int x = aligned_x1; x < aligned_x2; x += 4) + { + // Find y1, y2, light and uv values for four columns: + int y1[4] = { uwal[x], uwal[x + 1], uwal[x + 2], uwal[x + 3] }; + int y2[4] = { dwal[x], dwal[x + 1], dwal[x + 2], dwal[x + 3] }; + + // Figure out where we vertically can start and stop drawing 4 columns in one go + int middle_y1 = y1[0]; + int middle_y2 = y2[0]; + for (int i = 1; i < 4; i++) + { + middle_y1 = MAX(y1[i], middle_y1); + middle_y2 = MIN(y2[i], middle_y2); + } + + // If we got an empty column in our set we cannot draw 4 columns in one go: + bool empty_column_in_set = false; + for (int i = 0; i < 4; i++) + { + if (y2[i] <= y1[i]) + empty_column_in_set = true; + } + + if (empty_column_in_set || middle_y2 <= middle_y1) + { + for (int i = 0; i < 4; i++) + { + if (y2[i] <= y1[i]) + continue; + + R_DrawSkyColumn(x + i, y1[i], y2[i], 1); + } + continue; + } + + // Draw the first rows where not all 4 columns are active + for (int i = 0; i < 4; i++) + { + if (y1[i] < middle_y1) + R_DrawSkyColumn(x + i, y1[i], middle_y1, 1); + } + + // Draw the area where all 4 columns are active + R_DrawSkyColumn(x, middle_y1, middle_y2, 4); + + // Draw the last rows where not all 4 columns are active + for (int i = 0; i < 4; i++) + { + if (middle_y2 < y2[i]) + R_DrawSkyColumn(x + i, middle_y2, y2[i], 1); + } + } + + // The last unaligned columns: + for (int x = aligned_x2; x < x2; x++) + { + int y1 = uwal[x]; + int y2 = dwal[x]; + if (y2 <= y1) + continue; + + R_DrawSkyColumn(x, y1, y2, 1); + } +} + static void R_DrawSky (visplane_t *pl) { + if (r_swtruecolor) + { + R_DrawTruecolorSky(pl); + return; + } + int x; float swal; diff --git a/src/r_sky.cpp b/src/r_sky.cpp index 9ea44db481..cf3f37367f 100644 --- a/src/r_sky.cpp +++ b/src/r_sky.cpp @@ -49,7 +49,7 @@ fixed_t sky1cyl, sky2cyl; double sky1pos, sky2pos; // [RH] Stretch sky texture if not taller than 128 pixels? -CUSTOM_CVAR (Bool, r_stretchsky, true, CVAR_ARCHIVE) +CUSTOM_CVAR (Bool, r_stretchsky, false, CVAR_ARCHIVE) { R_InitSkyMap (); }